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

:root {
    --cell-size: min(10vw, 56px);
    --border-color: #344861;
    --blue: #325aaf;
    --blue-select: #bbdefb;
    --blue-related: #e2ebf3;
    --blue-same: #c3d7ea;
    --blue-lighter: #e8f0fe;
    --red: #e55c6c;
    --red-light: #f7cfd6;
    --gray-50: #fafbfc;
    --gray-100: #f0f3f7;
    --gray-200: #d4dbe5;
    --gray-300: #bec6d0;
    --gray-500: #7b8794;
    --gray-700: #4a5568;
    --gray-900: #1a202c;
    --green: #22c55e;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    background: #f0f4f8;
    display: flex;
    justify-content: center;
    min-height: 100dvh;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
}

#app {
    width: calc(var(--cell-size) * 9 + 18px + 28px); /* board width + padding */
    max-width: 100%;
    padding: 12px 14px;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 8px;
}

/* ========== Header ========== */
header {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding-bottom: 6px;
    border-bottom: 1px solid var(--gray-200);
}

h1 {
    font-size: 22px;
    font-weight: 700;
    color: var(--border-color);
}

#header-controls {
    display: flex;
    gap: 8px;
    align-items: center;
}

select {
    padding: 6px 10px;
    border: 1px solid var(--gray-200);
    border-radius: 6px;
    font-size: 13px;
    background: white;
    color: var(--gray-700);
    cursor: pointer;
    touch-action: manipulation;
}

button {
    padding: 6px 14px;
    border: none;
    border-radius: 6px;
    font-size: 13px;
    background: white;
    color: var(--gray-700);
    cursor: pointer;
    transition: background 0.15s;
    touch-action: manipulation;
}

button:hover { background: var(--gray-100); }
button:active { background: var(--gray-200); }

#header-controls #new-game {
    background: var(--blue);
    color: white;
    font-weight: 600;
    padding: 7px 14px;
    border-radius: 8px;
}
#header-controls #new-game:hover { background: #2a4e9a; }

/* Language toggle */
#lang-btn {
    font-size: 20px;
    width: 36px;
    padding: 4px 0;
    background: transparent;
    border: none;
    cursor: pointer;
    line-height: 1;
    text-align: center;
}
#lang-btn:hover { background: var(--gray-100); border-radius: 6px; }

/* ========== Game area (vertical default) ========== */
#game-area {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 10px;
    width: 100%;
}

/* ========== Board ========== */
#board-container {
    position: relative;
    width: fit-content;
}

#board {
    display: grid;
    grid-template-columns:
        repeat(3, var(--cell-size)) 2px
        repeat(3, var(--cell-size)) 2px
        repeat(3, var(--cell-size));
    grid-template-rows:
        repeat(3, var(--cell-size)) 2px
        repeat(3, var(--cell-size)) 2px
        repeat(3, var(--cell-size));
    border: 3px solid var(--border-color);
    gap: 1px;
    background: var(--gray-200);
}

/* Thick divider tracks between 3x3 boxes */
.box-divider {
    background: var(--border-color);
}

.cell {
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--cell-size) * 0.52);
    font-weight: 500;
    color: var(--gray-900);
    cursor: pointer;
    position: relative;
    transition: background 0.08s;
    touch-action: manipulation;
}

/* Given clues — dark, bold */
.cell.given {
    color: var(--gray-900);
    font-weight: 700;
}

/* User entries — blue */
.cell.user-value {
    color: var(--blue);
    font-weight: 600;
}

/* Wrong answer — red */
.cell.wrong {
    color: var(--red) !important;
    font-weight: 600;
}

/* ---- Highlights (sudoku.com style: light blue, not inverted) ---- */
.cell.highlight-selected {
    background: var(--blue-select) !important;
}

.cell.highlight-selected.wrong {
    background: var(--red-light) !important;
}

.cell.highlight-related {
    background: var(--blue-related);
}

.cell.highlight-same-value {
    background: var(--blue-same);
}

.cell.given.highlight-related {
    background: var(--blue-related);
}

.cell.given.highlight-same-value {
    background: var(--blue-same);
}

/* Conflicts (peer duplicates) */
.cell.conflict {
    color: var(--red) !important;
}

/* ========== Notes ========== */
.cell .notes-grid {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    width: 100%;
    height: 100%;
    position: absolute;
    top: 0;
    left: 0;
}

.cell .notes-grid span {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: calc(var(--cell-size) * 0.24);
    color: var(--gray-500);
    font-weight: 500;
    line-height: 1;
}

/* ========== Loading overlay ========== */
#loading-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(255, 255, 255, 0.92);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 12px;
    z-index: 10;
    font-size: 14px;
    color: var(--gray-500);
    border-radius: 2px;
}

.spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--gray-200);
    border-top-color: var(--blue);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.hidden { display: none !important; }

/* ========== Controls (vertical default) ========== */
#controls {
    width: calc(var(--cell-size) * 9 + 18px);
    display: flex;
    flex-direction: column;
    gap: 10px;
}

#controls.disabled {
    filter: grayscale(1) opacity(0.4);
    pointer-events: none;
}

/* ---- Status bar (difficulty + timer) ---- */
#status-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 2px;
}

.status-label {
    font-size: 12px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--blue);
}

.status-value {
    font-size: 14px;
    font-weight: 500;
    font-variant-numeric: tabular-nums;
    color: var(--gray-700);
}

/* ---- Action bar ---- */
#action-bar {
    display: flex;
    gap: 0;
    justify-content: space-around;
}

#action-bar button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    background: none;
    border: none;
    padding: 0;
    position: relative;
    cursor: pointer;
}

#action-bar button .action-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 52px;
    height: 52px;
    border-radius: 50%;
    background: var(--gray-100);
    font-size: 22px;
    line-height: 1;
    color: var(--gray-700);
}

#action-bar button:hover .action-icon {
    background: var(--gray-200);
}

#undo-btn .action-icon svg,
#auto-notes-btn .action-icon svg {
    display: block;
}

.action-label {
    font-size: 10px;
    font-weight: 500;
    color: var(--gray-600);
    line-height: 1;
    white-space: nowrap;
}

/* Keyboard shortcut hint */
.action-shortcut {
    font-size: 9px;
    color: var(--gray-400);
    line-height: 1;
    white-space: nowrap;
    font-weight: 400;
}


/* Notes badge — positioned relative to the icon circle */
.notes-badge {
    position: absolute;
    top: 2px;
    right: 0px;
    font-size: 8px;
    font-weight: 700;
    background: var(--gray-300);
    color: white;
    padding: 1px 4px;
    border-radius: 6px;
    line-height: 1.2;
    z-index: 1;
}

/* Notes/pencil active state */
#notes-btn.active .action-icon {
    background: var(--blue) !important;
}

#notes-btn.active .action-icon {
    color: white;
}

#notes-btn.active .action-label {
    color: var(--blue);
}

#notes-btn.active .notes-badge {
    background: white;
    color: var(--blue);
}

/* ---- Game over banner ---- */
#gameover-banner {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, 0.5);
    z-index: 40;
    cursor: pointer;
}

#gameover-banner span {
    font-size: 32px;
    font-weight: 700;
    color: white;
    text-shadow: 0 2px 8px rgba(0, 0, 0, 0.5);
}

/* ---- Assist bar ---- */
#assist-bar {
    display: flex;
    justify-content: space-around;
}

#assist-bar button {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 4px;
    background: none;
    border: none;
    padding: 0;
    position: relative;
    cursor: pointer;
}

#assist-bar button .action-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    border-radius: 50%;
    background: #e8f5e9;
    font-size: 20px;
    line-height: 1;
    color: #2e7d32;
}

#assist-bar button:hover .action-icon {
    background: #c8e6c9;
}

#assist-bar button.disabled .action-icon {
    background: var(--gray-100);
    color: var(--gray-300);
}

#assist-bar button.disabled {
    pointer-events: none;
}

#assist-bar .action-label {
    font-size: 10px;
    font-weight: 500;
    color: var(--gray-600);
    line-height: 1;
    white-space: nowrap;
}

/* ---- Hint cell highlights (overlay style — uses box-shadow to not replace backgrounds) ---- */
.cell.hint-base { box-shadow: inset 0 0 0 1px #6366f1; }
.cell.hint-cover { box-shadow: inset 0 0 0 1px #a3a3a3; }
.cell.hint-cell { box-shadow: inset 0 0 0 2.5px #22c55e; }
.cell.hint-elim { box-shadow: inset 0 0 0 2px #f59e0b; }
.cell.hint-error { box-shadow: inset 0 0 0 2px var(--red); }
.cell.hint-color-a { box-shadow: inset 0 0 0 2.5px #3b82f6; }
.cell.hint-color-b { box-shadow: inset 0 0 0 2.5px #f97316; }

.cell .notes-grid.ghost span { opacity: 0.3; }
.cell .notes-grid span.hint-digit { color: var(--blue) !important; font-weight: 700; opacity: 1; }
.cell .notes-grid span.hint-elim-digit { color: var(--red) !important; font-weight: 700; text-decoration: line-through; opacity: 1; }

/* ---- Numpad (mobile: 9-across row) ---- */
#numpad {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    gap: 0;
}

#numpad button {
    aspect-ratio: 1;
    font-size: calc(var(--cell-size) * 0.55);
    font-weight: 700;
    padding: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 0;
    color: var(--blue);
    border: none;
    background: transparent;
}

#numpad button:hover {
    background: var(--blue-related);
}

#numpad button.completed {
    opacity: 0.2;
    pointer-events: none;
}

/* ========== Disabled action button ========== */
#action-bar button.disabled {
    pointer-events: none;
    cursor: default;
}

#action-bar button.disabled .action-icon {
    opacity: 0.35;
}

/* ========== Menu (kebab) ========== */
#menu-container {
    position: relative;
}

#menu-btn {
    font-size: 20px;
    width: 36px;
    padding: 4px 0;
    background: transparent;
    border: none;
    cursor: pointer;
    line-height: 1;
    text-align: center;
}

#menu-btn:hover {
    background: var(--gray-100);
    border-radius: 6px;
}

#menu-dropdown {
    position: absolute;
    top: 100%;
    right: 0;
    margin-top: 4px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
    min-width: 140px;
    z-index: 50;
    overflow: hidden;
}

#menu-dropdown button {
    display: block;
    width: 100%;
    text-align: left;
    padding: 10px 16px;
    font-size: 14px;
    border: none;
    background: none;
    color: var(--gray-700);
    cursor: pointer;
    border-radius: 0;
}

#menu-dropdown button:hover {
    background: var(--gray-100);
}


/* ========== Footer ========== */
footer {
    margin-top: 4px;
    font-size: 11px;
    color: var(--gray-300);
    text-align: center;
}

footer a {
    color: var(--blue);
    text-decoration: none;
    font-weight: 500;
}

footer a:hover {
    text-decoration: underline;
}

/* ========== Win overlay ========== */
#win-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 100;
}

#win-message {
    background: white;
    border-radius: 16px;
    padding: 32px 40px;
    text-align: center;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
}

#win-message h2 {
    font-size: 24px;
    color: var(--green);
    margin-bottom: 8px;
}

#win-message p {
    color: var(--gray-500);
    margin-bottom: 20px;
}

#play-again {
    background: var(--blue);
    color: white;
    border: none;
    padding: 10px 24px;
    font-size: 16px;
    font-weight: 600;
    border-radius: 8px;
}

#play-again:hover { background: #2a4e9a; }

/* ========== Wide / landscape — horizontal layout ========== */
@media (min-width: 780px) {
    :root {
        --cell-size: min(7.5vh, 56px);
    }

    #app {
        width: fit-content;
        max-width: 100%;
        padding: 16px 20px;
        gap: 10px;
    }

    header {
        width: 100%;
    }

    #game-area {
        flex-direction: row;
        align-items: flex-start;
        gap: 24px;
    }

    #controls {
        width: 200px;
        gap: 16px;
        padding-top: 4px;
    }

    /* Status bar stacks vertically in side panel */
    #status-bar {
        flex-direction: column;
        align-items: flex-start;
        gap: 2px;
    }

    .status-label {
        font-size: 13px;
    }

    .status-value {
        font-size: 16px;
    }

    /* Action buttons in side panel */
    #action-bar {
        display: flex;
        justify-content: center;
        gap: 8px;
    }

    #action-bar button .action-icon {
        width: 48px;
        height: 48px;
        font-size: 20px;
    }

    .action-icon { font-size: 20px; }
    .action-label { font-size: 9px; }

    #assist-bar {
        justify-content: center;
        gap: 8px;
    }

    #assist-bar button .action-icon {
        width: 40px;
        height: 40px;
        font-size: 18px;
    }

    /* Numpad: 3x3 grid matching action bar columns */
    #numpad {
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
        justify-items: center;
    }

    #numpad button {
        width: 52px;
        height: 52px;
        aspect-ratio: auto;
        font-size: 28px;
        font-weight: 700;
        border-radius: 50%;
        background: var(--gray-100);
        color: var(--blue);
    }

    #numpad button:hover {
        background: var(--blue-related);
    }

}

/* ========== Narrow / mobile ========== */
@media (max-width: 420px) {
    :root {
        --cell-size: calc((100vw - 40px) / 9);
    }

    #app {
        padding: 10px;
        gap: 6px;
    }

    h1 { font-size: 18px; }

    header { gap: 6px; }

    #header-controls {
        gap: 4px;
        flex-shrink: 0;
    }

    select {
        max-width: 76px;
        padding: 5px 6px;
        font-size: 12px;
    }

    #header-controls #new-game {
        padding: 6px 10px;
    }

    #lang-btn,
    #menu-btn {
        width: 32px;
    }

    #action-bar button {
        width: auto;
        height: auto;
        min-width: 42px;
    }

    #action-bar button .action-icon {
        width: 44px;
        height: 44px;
    }

    .action-icon { font-size: 18px; }
    .action-label { font-size: 9px; }
    .action-shortcut { display: none; }

    #assist-bar button .action-icon {
        width: 38px;
        height: 38px;
        font-size: 17px;
    }

    #numpad button {
        font-size: calc(var(--cell-size) * 0.5);
    }
}

@media (max-width: 340px) {
    h1 { display: none; }
    header { justify-content: flex-end; }
}
