/* disable touch-based zooming */
html, body {
    touch-action: manipulation;
    margin: 0;
    padding: 0;
}

/* =========================
   Theme variables (flat)
   ========================= */
:root {
    --bg: #0b0e14;                 /* dark default */
    --text: #e7eaf3;
    --muted: rgba(231, 234, 243, 0.68);

    --ui-border: rgba(231, 234, 243, 0.22);
    --ui-press: rgba(231, 234, 243, 0.08);

    /* Grid: same color everywhere, thickness changes for 3x3 separators */
    --grid: rgba(231, 234, 243, 0.30);

    /* Selection: outline only (no shading) */
    --selected-border: rgba(110, 231, 255, 0.75);

    --success: #4be3b0;
    --warn: #ffcc66;

    /* Approx reserved height above board (title + topbar + spacing) */
    --ui-top-reserve: 140px;

    /* Approx numpad height reserve (2 rows + padding). Keeps everything visible. */
    --numpad-reserve: 210px;
}

html[data-theme="light"] {
    --bg: #ffffff;
    --text: #111827;
    --muted: rgba(17, 24, 39, 0.62);

    --ui-border: rgba(17, 24, 39, 0.20);
    --ui-press: rgba(17, 24, 39, 0.06);

    --grid: rgba(17, 24, 39, 0.28);

    --selected-border: rgba(14, 165, 233, 0.70);

    --success: #059669;
    --warn: #b45309;
}

/* =========================
   Layout
   ========================= */
body {
    display: flex;
    flex-direction: column;
    align-items: center;
    font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
    padding: 12px;
    background: var(--bg);
    color: var(--text);
    -webkit-font-smoothing: antialiased;
    text-rendering: optimizeLegibility;

    /* IMPORTANT: make room so fixed numpad never overlaps content */
    padding-bottom: calc(var(--numpad-reserve) + env(safe-area-inset-bottom, 0px));
}

h1 {
    margin: 10px 0 10px;
    font-size: clamp(1.4rem, 5vw, 2.2rem);
    font-weight: 900;
    letter-spacing: 0.2px;
}

/* =========================
   Topbar (all in one line)
   ========================= */
#topbar {
    width: min(96vw, 560px);
    display: flex;
    align-items: center;
    gap: 10px;
    flex-wrap: nowrap;
    margin-bottom: 10px;
}

#new-game,
#theme-toggle {
    padding: 8px 10px;
    font-size: 0.95rem;
    font-weight: 700;
    cursor: pointer;
    border-radius: 6px;
    border: 1px solid var(--ui-border);
    background: transparent;
    color: var(--text);
    line-height: 1;
}

#new-game:active,
#theme-toggle:active {
    background: var(--ui-press);
}

#theme-toggle {
    width: 44px;
    text-align: center;
}

#difficulty-container {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
    flex: 1 1 auto;
}

#difficulty-container label {
    font-weight: 750;
    color: var(--muted);
    white-space: nowrap;
    font-size: 0.95rem;
}

#difficulty {
    width: 100%;
    min-width: 120px;
    max-width: 220px;
    padding: 7px 10px;
    font-size: 0.95rem;
    border-radius: 6px;
    border: 1px solid var(--ui-border);
    background: transparent;
    color: var(--text);
    outline: none;
}

#timer {
    font-weight: 850;
    font-size: 0.95rem;
    color: var(--muted);
    white-space: nowrap;
}

/* =========================
   Sudoku board
   - auto scales so numpad is always visible without scrolling
   ========================= */
.sudoku-container {
    /*
        Board should fit vertically in the viewport above the fixed numpad.
        Since board is square, constrain by BOTH width and available height.
    */
    width: min(
        96vw,
        560px,
        calc(100vh - var(--ui-top-reserve) - var(--numpad-reserve) - env(safe-area-inset-bottom, 0px))
    );
    overflow: hidden;
}

#sudoku-board {
    width: 100%;
    border-collapse: collapse;
    table-layout: fixed;

    /* outer border slightly thicker */
    border: 2px solid var(--grid);
}

#sudoku-board td {
    padding: 0;
}

/* Flat inputs */
#sudoku-board input {
    width: 100%;
    aspect-ratio: 1 / 1;
    font-size: clamp(1.05rem, 3.2vw, 1.7rem);
    font-weight: 800;
    text-align: center;

    border: 1px solid var(--grid);
    box-sizing: border-box;

    background: transparent;
    color: var(--text);

    cursor: pointer;
    border-radius: 0;
    outline: none;
    box-shadow: none;
}

#sudoku-board input:disabled {
    cursor: default;
    font-weight: 900;
    background: transparent;
    color: rgba(231, 234, 243, 0.86);
}

html[data-theme="light"] #sudoku-board input:disabled {
    color: rgba(17, 24, 39, 0.78);
}

/* Selection: outline only */
input.selected-cell {
    background: transparent;
    outline: 2px solid var(--selected-border);
    outline-offset: -2px;
}

/* 3x3 separators: same color, thicker lines */
#sudoku-board td:nth-child(3n) input {
    border-right-width: 3px;
}
#sudoku-board tr:nth-child(3n) td input {
    border-bottom-width: 3px;
}
#sudoku-board td:nth-child(1) input {
    border-left-width: 3px;
}
#sudoku-board tr:nth-child(1) td input {
    border-top-width: 3px;
}

/* =========================
   Numpad
   - fixed at bottom on ALL screen sizes so it is never "below the fold"
   ========================= */
#numpad {
    position: fixed;
    left: 0;
    right: 0;

    /* IMPORTANT: lift above OS/browser UI */
    bottom: calc(env(safe-area-inset-bottom, 0px) + 8px);

    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 8px;

    width: min(96vw, 560px);
    margin: 0 auto;

    padding: 10px 12px calc(12px + env(safe-area-inset-bottom, 0px));
    background: var(--bg);

    border: 1px solid var(--ui-border);
    border-radius: 10px;

    z-index: 9999;
    box-sizing: border-box;
}

/* Ensure the fixed element is truly centered */
@supports (left: max(0px)) {
    #numpad {
        left: 50%;
        right: auto;
        transform: translateX(-50%);
    }
}

.num-btn {
    padding: 12px 10px;
    font-size: clamp(1.0rem, 3.6vw, 1.45rem);
    font-weight: 900;
    background: transparent;
    color: var(--text);
    border: 1px solid var(--ui-border);
    border-radius: 6px;
    cursor: pointer;
    user-select: none;
}

.num-btn:active {
    background: var(--ui-press);
}

/* If the screen is short, compress the numpad so ALL rows always fit */
@media (max-height: 760px) {
    :root {
        --numpad-reserve: 190px;
    }
    #numpad {
        gap: 6px;
        padding-top: 8px;
        padding-bottom: calc(10px + env(safe-area-inset-bottom, 0px));
    }
    .num-btn {
        padding: 10px 8px;
        font-size: clamp(0.95rem, 3.4vw, 1.25rem);
    }
}

/* Message */
#message {
    margin-top: 12px;
    font-size: 1.1em;
    font-weight: 850;
    color: var(--success);
    width: min(96vw, 560px);
    text-align: center;
}

/* Tighten on very small widths so topbar stays one line */
@media (max-width: 380px) {
    #difficulty-container label {
        display: none;
    }
    #difficulty {
        max-width: 160px;
    }
    #timer {
        font-size: 0.9rem;
    }
}
