* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --bg: #1a1a2e;
    --surface: #16213e;
    --surface-light: #1e2d4a;
    --border: #2a3a5c;
    --text: #e0e0e0;
    --text-muted: #8892a4;
    --accent: #4fc3f7;
    --accent-dim: #2a6f8f;
    --given: #ffffff;
    --given-bg: rgba(79, 195, 247, 0.10);
    --pen: #81d4fa;
    --pencil: #607d8b;
    --highlight-manual: rgba(232, 162, 42, 0.6);
    --highlight-tutor: rgba(79, 195, 247, 0.25);
    --highlight-selected: rgba(232, 162, 42, 0.65);
    --highlight-related: rgba(255, 230, 160, 0.12);
    --danger: #ef5350;
    --danger-hover: #c62828;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
    background: var(--bg);
    color: var(--text);
    height: 100vh;
    overflow: hidden;
}

.app {
    display: flex;
    flex-direction: column;
    height: 100vh;
    max-width: 1200px;
    margin: 0 auto;
    padding: 16px 24px;
}

/* Header */
.header {
    margin-bottom: 16px;
}

.header-top {
    display: flex;
    align-items: center;
}

.header h1 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 12px;
    color: var(--accent);
}

.toolbar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    gap: 12px;
}

.mode-toggle {
    display: flex;
    gap: 4px;
    background: var(--surface);
    border-radius: 8px;
    padding: 3px;
}

.mode-btn {
    padding: 6px 14px;
    border: none;
    border-radius: 6px;
    background: transparent;
    color: var(--text-muted);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}

.mode-btn.active {
    background: var(--accent-dim);
    color: var(--text);
}

.mode-btn:hover:not(.active) {
    color: var(--text);
}

.toolbar-right {
    display: flex;
    gap: 8px;
}

.tool-btn {
    padding: 6px 12px;
    border: 1px solid var(--border);
    border-radius: 6px;
    background: var(--surface);
    color: var(--text-muted);
    font-size: 13px;
    cursor: pointer;
    transition: all 0.15s;
}

.tool-btn:hover {
    border-color: var(--accent-dim);
    color: var(--text);
}

.tool-btn.active {
    border-color: var(--accent);
    color: var(--accent);
}

.tool-btn.danger {
    border-color: var(--danger);
    color: var(--danger);
    opacity: 0.7;
}

.tool-btn.danger:hover {
    opacity: 1;
    background: var(--danger);
    color: white;
}

/* Main layout */
.main {
    display: flex;
    gap: 24px;
    flex: 1;
    min-height: 0;
}

/* Grid */
.grid-container {
    flex-shrink: 0;
}

.sudoku-grid {
    border-collapse: collapse;
    border: 3px solid var(--accent-dim);
    user-select: none;
}

.sudoku-grid td {
    width: 56px;
    height: 56px;
    border: 1px solid var(--border);
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    position: relative;
    transition: background 0.1s;
}

/* 3x3 box borders */
.sudoku-grid td:nth-child(3),
.sudoku-grid td:nth-child(6) {
    border-right: 2px solid var(--accent-dim);
}

.sudoku-grid tr:nth-child(3) td,
.sudoku-grid tr:nth-child(6) td {
    border-bottom: 2px solid var(--accent-dim);
}

/* Cell states — ordered from lowest to highest priority */
.sudoku-grid td.related {
    background: var(--highlight-related);
}

.sudoku-grid td.given-cell {
    background: var(--given-bg);
}

.sudoku-grid td.highlight-tutor {
    background: var(--highlight-tutor);
}

.sudoku-grid td.highlight-manual {
    background: var(--highlight-manual);
}

.sudoku-grid td.selected {
    background: var(--highlight-selected);
    outline: 2px solid var(--accent);
    outline-offset: -2px;
}

.sudoku-grid td.error {
    background: rgba(239, 83, 80, 0.25);
}

.sudoku-grid td.error .cell-value {
    color: var(--danger);
}

.tool-btn.valid {
    border-color: #66bb6a;
    color: #66bb6a;
    background: rgba(102, 187, 106, 0.15);
    box-shadow: 0 0 12px rgba(102, 187, 106, 0.4);
}

.tool-btn.invalid {
    border-color: var(--danger);
    color: var(--danger);
    background: rgba(239, 83, 80, 0.15);
    box-shadow: 0 0 12px rgba(239, 83, 80, 0.4);
}

/* Cell content */
.cell-value {
    font-size: 24px;
    font-weight: 400;
    line-height: 56px;
}

.cell-value.given {
    color: var(--given);
    font-weight: 700;
}

.cell-value.pen {
    color: var(--pen);
}

/* Pencil marks - 3x3 sub-grid */
.pencil-marks {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
    padding: 2px;
}

.pencil-mark {
    font-size: 11px;
    color: var(--pencil);
    display: flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
}

.pencil-mark.auto {
    color: var(--pencil);
    opacity: 0.5;
}

.sudoku-grid td.selected .pencil-mark,
.sudoku-grid td.selected .pencil-mark.auto {
    color: #fff;
    opacity: 1;
}

.grid-status {
    display: flex;
    justify-content: space-between;
    margin-top: 8px;
    font-size: 12px;
    color: var(--text-muted);
}

/* Chat */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-radius: 12px;
    border: 1px solid var(--border);
    min-height: 0;
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border);
}

.chat-header h2 {
    font-size: 15px;
    font-weight: 600;
}

.chat-header-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.provider-status {
    font-size: 11px;
    padding: 3px 8px;
    border-radius: 4px;
    background: var(--bg);
}

.provider-status.connected {
    color: #66bb6a;
}

.provider-status.disconnected {
    color: var(--text-muted);
}

.hint-btn {
    padding: 6px 16px;
    border: none;
    border-radius: 6px;
    background: var(--accent-dim);
    color: var(--text);
    font-size: 13px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.15s;
}

.hint-btn:hover {
    background: var(--accent);
    color: var(--bg);
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.message {
    padding: 10px 14px;
    border-radius: 10px;
    font-size: 14px;
    line-height: 1.5;
    max-width: 90%;
}

.message.tutor {
    background: var(--surface-light);
    align-self: flex-start;
    border: 1px solid var(--border);
}

.message.user {
    background: var(--accent-dim);
    align-self: flex-end;
}

.message p {
    margin: 0;
}

.message strong {
    color: var(--accent);
}

.technique-tooltip {
    color: var(--accent);
    font-weight: 600;
    cursor: help;
    position: relative;
    border-bottom: 1px dotted var(--accent-dim);
}

.technique-tooltip:hover::after {
    content: attr(data-tooltip);
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: 6px 10px;
    background: var(--bg);
    color: var(--text);
    border: 1px solid var(--border);
    border-radius: 6px;
    font-size: 12px;
    font-weight: 400;
    white-space: normal;
    width: max-content;
    max-width: 250px;
    text-align: center;
    z-index: 10;
    pointer-events: none;
    margin-bottom: 4px;
}

.technique-tooltip:hover::before {
    content: '';
    position: absolute;
    bottom: 100%;
    left: 50%;
    transform: translateX(-50%);
    border: 5px solid transparent;
    border-top-color: var(--border);
    z-index: 10;
    pointer-events: none;
}

.chat-input-area {
    display: flex;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border);
}

.chat-input {
    flex: 1;
    padding: 8px 12px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--bg);
    color: var(--text);
    font-size: 14px;
    outline: none;
}

.chat-input:focus {
    border-color: var(--accent-dim);
}

.chat-send-btn {
    padding: 8px 16px;
    border: none;
    border-radius: 8px;
    background: var(--accent-dim);
    color: var(--text);
    font-size: 13px;
    cursor: pointer;
    transition: background 0.15s;
}

.chat-send-btn:hover {
    background: var(--accent);
    color: var(--bg);
}

/* Footer */
.footer {
    text-align: center;
    padding: 8px 0;
    font-size: 12px;
    color: var(--text-muted);
}

.footer a {
    color: var(--text-muted);
}

.footer a:hover {
    color: var(--text);
}

/* Modal */
.modal {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.6);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

.modal.hidden {
    display: none;
}

.modal-content {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 12px;
    width: 480px;
    max-height: 80vh;
    display: flex;
    flex-direction: column;
}

.modal-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid var(--border);
}

.modal-header h2 {
    font-size: 16px;
}

.modal-close {
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 24px;
    cursor: pointer;
    padding: 0 4px;
}

.modal-close:hover {
    color: var(--text);
}

.modal-body {
    padding: 20px;
    overflow-y: auto;
}

.skill-tier {
    margin-bottom: 20px;
}

.skill-tier h3 {
    font-size: 14px;
    font-weight: 600;
    color: var(--accent);
    margin-bottom: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.skill-tier label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 4px 0;
    font-size: 13px;
    color: var(--text-muted);
    cursor: pointer;
}

.skill-tier label:hover {
    color: var(--text);
}

.skill-tier input[type="checkbox"] {
    accent-color: var(--accent);
}

.tier-select-all {
    font-size: 12px;
    color: var(--accent-dim);
    cursor: pointer;
    border: none;
    background: none;
}

.tier-select-all:hover {
    color: var(--accent);
}

/* Mobile-only elements — hidden on desktop */
.mobile-header-icons,
.numpad,
.mobile-tools,
.bottom-sheet-handle {
    display: none;
}

/* Mobile header icon buttons (camera, gear) */
.mobile-header-icons {
    flex-direction: row;
    gap: 8px;
    align-items: center;
}

.mobile-icon-btn {
    background: none;
    border: 1px solid var(--border);
    border-radius: 8px;
    color: var(--text-muted);
    padding: 6px;
    cursor: pointer;
    transition: all 0.15s;
    -webkit-tap-highlight-color: transparent;
    display: flex;
    align-items: center;
    justify-content: center;
}

.mobile-icon-btn:hover {
    color: var(--text);
    border-color: var(--accent-dim);
}

/* Numpad base styles */
.numpad {
    flex-direction: column;
    gap: 8px;
    padding: 8px 0;
}

.numpad-row {
    display: flex;
    gap: 8px;
    justify-content: center;
}

.numpad-btn {
    width: 56px;
    height: 48px;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--surface);
    color: var(--text);
    font-size: 20px;
    font-weight: 500;
    cursor: pointer;
    transition: background 0.1s;
    -webkit-tap-highlight-color: transparent;
}

.numpad-btn:active {
    background: var(--accent-dim);
}

.numpad-btn.numpad-delete {
    color: var(--danger);
    font-size: 24px;
}

/* Bottom sheet handle */
.bottom-sheet-handle {
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    padding: 10px 16px;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    touch-action: none;
}

.handle-bar {
    width: 36px;
    height: 4px;
    border-radius: 2px;
    background: var(--text-muted);
    opacity: 0.5;
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
}

.handle-label {
    font-size: 15px;
    font-weight: 600;
    color: var(--text);
}

.handle-hint-btn {
    display: none;
}

/* Mobile tools row */
.mobile-tools {
    flex-direction: row;
    justify-content: center;
    gap: 8px;
    padding: 4px 0;
}

/* OCR Import Modal */
.ocr-modal-content {
    width: 420px;
}

.ocr-paste-zone {
    border: 2px dashed var(--border);
    border-radius: 10px;
    padding: 32px 16px;
    text-align: center;
    color: var(--text-muted);
    font-size: 14px;
    cursor: pointer;
    transition: border-color 0.15s, background 0.15s;
    outline: none;
}

.ocr-paste-zone:focus,
.ocr-paste-zone:hover {
    border-color: var(--accent-dim);
    background: rgba(79, 195, 247, 0.05);
}

.ocr-divider {
    display: flex;
    align-items: center;
    gap: 12px;
    margin: 16px 0;
    color: var(--text-muted);
    font-size: 13px;
}

.ocr-divider::before,
.ocr-divider::after {
    content: '';
    flex: 1;
    height: 1px;
    background: var(--border);
}

.ocr-file-row {
    display: flex;
    gap: 10px;
    justify-content: center;
}

.ocr-file-btn {
    cursor: pointer;
    text-align: center;
}

.ocr-camera-btn {
    display: none;
}

.ocr-preview {
    margin-top: 16px;
    text-align: center;
}

.ocr-preview img {
    max-width: 100%;
    max-height: 200px;
    border-radius: 8px;
    border: 1px solid var(--border);
}

.ocr-status {
    margin-top: 12px;
    text-align: center;
    font-size: 13px;
    min-height: 20px;
}

.ocr-status.ocr-loading {
    color: var(--accent);
}

.ocr-status.ocr-error {
    color: var(--danger);
}

/* =========================================
   Mobile breakpoint
   ========================================= */
@media screen and (max-width: 768px) {
    body {
        overflow: auto;
        height: 100%;
        -webkit-overflow-scrolling: touch;
    }

    .app {
        padding: 8px 8px 0;
        padding-bottom: env(safe-area-inset-bottom, 0);
        height: auto;
        min-height: 100vh;
    }

    /* Header */
    .header {
        margin-bottom: 8px;
    }

    .header-top {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 8px;
    }

    .header h1 {
        font-size: 18px;
        margin-bottom: 0;
    }

    .mobile-header-icons {
        display: flex;
    }

    /* Hide desktop toolbar-right */
    .toolbar-right {
        display: none !important;
    }

    .toolbar {
        justify-content: center;
    }

    .mode-toggle {
        width: 100%;
        justify-content: center;
    }

    .mode-btn {
        flex: 1;
        padding: 10px 8px;
        font-size: 14px;
        text-align: center;
    }

    /* Main layout: vertical stack */
    .main {
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 8px;
        padding-bottom: 16px;
    }

    /* Grid: fill width dynamically, capped for tablets */
    .grid-container {
        width: 100%;
        max-width: 500px;
        display: flex;
        flex-direction: column;
        align-items: center;
    }

    .sudoku-grid {
        width: min(calc(100vw - 16px), 500px);
        table-layout: fixed;
    }

    .sudoku-grid td {
        width: calc(min(100vw - 16px, 500px) / 9);
        height: calc(min(100vw - 16px, 500px) / 9);
        font-size: 0;
    }

    .cell-value {
        font-size: calc(min(100vw - 16px, 500px) / 9 * 0.45);
        line-height: calc(min(100vw - 16px, 500px) / 9);
    }

    .pencil-mark {
        font-size: calc(min(100vw - 16px, 500px) / 9 * 0.2);
    }

    .grid-status {
        width: min(calc(100vw - 16px), 500px);
    }

    /* Show numpad */
    .numpad {
        display: flex;
        flex-direction: column;
        width: 100%;
        max-width: 500px;
    }

    .numpad-row {
        display: flex;
        justify-content: center;
    }

    .numpad-btn {
        flex: 1;
        max-width: 64px;
        height: 44px;
    }

    /* Show mobile tools */
    .mobile-tools {
        display: flex;
        flex-direction: row;
        width: 100%;
        max-width: 500px;
    }

    .mobile-tools .tool-btn {
        flex: 1;
        padding: 10px 8px;
        font-size: 13px;
        text-align: center;
    }

    /* Chat as bottom sheet — fully hidden by default on mobile */
    .chat-container {
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        height: 70vh;
        width: 100%;
        flex: none;
        border-radius: 16px 16px 0 0;
        border: 1px solid var(--border);
        border-bottom: none;
        z-index: 50;
        transform: translateY(100%);
        transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        padding-bottom: env(safe-area-inset-bottom, 0);
    }

    .chat-container.expanded {
        transform: translateY(0);
    }

    .bottom-sheet-handle {
        display: flex;
        position: relative;
    }

    .handle-hint-btn {
        display: block;
    }

    /* Hide desktop chat header on mobile when collapsed */
    .chat-container .chat-header {
        display: none;
    }

    .chat-container.expanded .bottom-sheet-handle .handle-label,
    .chat-container.expanded .bottom-sheet-handle .handle-hint-btn {
        display: none;
    }

    .chat-container.expanded .bottom-sheet-handle {
        justify-content: center;
        padding: 8px 16px 4px;
    }

    .chat-container.expanded .chat-header {
        display: flex;
    }

    .chat-container:not(.expanded) .chat-messages,
    .chat-container:not(.expanded) .chat-input-area {
        display: none;
    }

    .footer {
        padding: 4px 0;
    }

    /* Modal full-screen on mobile */
    .modal-content {
        width: 100%;
        height: 100%;
        max-height: 100vh;
        border-radius: 0;
    }

    /* OCR modal: show camera button on mobile */
    .ocr-camera-btn {
        display: block;
    }

    .ocr-modal-content {
        width: 100%;
    }

    /* Prevent double-tap zoom */
    * {
        touch-action: manipulation;
    }
}
