/* ── Grid wrapper ──────────────────────────────────────────────────────── */
.grid-wrapper { position: relative; }

/* ── Loading overlay (used by 22×22 solve/generate) ───────────────────── */
.grid-loading-overlay {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: .75rem;
    border-radius: 12px;
    z-index: 20;
    background: color-mix(in srgb, var(--card) 90%, transparent);
    backdrop-filter: blur(2px);
}
.loading-spinner {
    width: 44px; height: 44px;
    border: 3px solid var(--border);
    border-top-color: var(--accent);
    border-radius: 50%;
    animation: gridSpin .7s linear infinite;
}
@keyframes gridSpin { to { transform: rotate(360deg); } }
.loading-msg {
    font-family: 'Space Grotesk', sans-serif;
    font-size: .9rem;
    color: var(--muted);
    margin: 0;
}
.grid-wrapper::after {
    content: '';
    position: absolute; top: 0; left: 0; width: 100%; height: 3px;
    background: linear-gradient(90deg, transparent, var(--accent), transparent);
    opacity: 0; pointer-events: none; z-index: 3;
}
.grid-wrapper.scanning::after {
    opacity: .4;
    animation: scanDown 2s linear infinite;
}
@keyframes scanDown { 0%{ top:0; } 100%{ top:100%; } }
@keyframes solvedGlow {
    0%   { box-shadow: 0 2px 20px var(--accent-glow), 0 8px 32px rgba(0,0,0,.06); }
    50%  { box-shadow: 0 0 40px rgba(194,65,12,.18), 0 0 80px rgba(22,163,74,.08), 0 8px 32px rgba(0,0,0,.06); }
    100% { box-shadow: 0 2px 20px var(--accent-glow), 0 8px 32px rgba(0,0,0,.06); }
}
.grid-solved { animation: solvedGlow 1.8s ease; }

/* ── Sudoku grid ───────────────────────────────────────────────────────── */
.sudoku-grid {
    /* size is applied via inline style grid-template-columns/rows */
    width: min(94vw, 540px);
    aspect-ratio: 1;
    border: 2.5px solid var(--accent);
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 2px 20px var(--accent-glow), 0 8px 32px rgba(0,0,0,.06);
    background: var(--border);   /* gap color */
    position: relative;
    container-type: inline-size;
    gap: 1px;
    display: grid;
}

/* Wider grids for bigger sizes */
.sudoku-grid.size-12 { width: min(94vw, 680px); }
.sudoku-grid.size-16 { width: min(94vw, 720px); }
.sudoku-grid.size-22 { width: min(94vw, 900px); }

/* ── Cells ─────────────────────────────────────────────────────────────── */
.cell {
    display: flex; align-items: center; justify-content: center;
    background: var(--card);
    font-family: 'Space Grotesk', sans-serif !important;
    font-size: 6cqi !important;
    font-weight: 600 !important;
    font-variant-numeric: tabular-nums;
    line-height: 1;
    cursor: pointer;
    transition: background .1s, box-shadow .1s, transform .1s;
    transform: none;
    user-select: none; -webkit-user-select: none;
    position: relative;
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    color: var(--fg);
    -webkit-tap-highlight-color: transparent;
    min-height: 0; min-width: 0; overflow: hidden;
}
.cell:hover { background: var(--card-hover); }

/* Value types */
.cell.given       { color: var(--fg); }
.cell.user        { color: var(--accent); }
.cell.solver-fill { color: var(--success); }

/* Selection & highlighting */
.cell.selected {
    background: var(--accent-glow) !important;
    box-shadow: inset 0 0 0 2px var(--accent);
    z-index: 2;
}
.cell.highlighted  { background: rgba(194,65,12,.03); }
.cell.same-number  { background: rgba(194,65,12,.07); }
.cell.error-cell   { color: var(--error) !important; background: var(--error-bg) !important; }

/* Animation states */
.cell.trying {
    color: var(--accent-light) !important;
    background: rgba(194,65,12,.08) !important;
    transition: background .05s, color .05s, transform .1s;
    transform: scale(1.05);
}
.cell.backtracking {
    background: var(--error-bg) !important;
    transition: background .05s, transform .1s;
    transform: scale(.92);
    opacity: .6;
}
.cell.solved-cell {
    color: var(--success) !important;
    animation: cellPop .3s cubic-bezier(.34,1.56,.64,1);
}
@keyframes cellPop {
    0%  { transform: scale(1); }
    50% { transform: scale(1.2); }
    100%{ transform: scale(1); }
}

/* Thick box borders - applied by JS via class names */
.cell.box-right  { border-right:  2.5px solid var(--accent); }
.cell.box-bottom { border-bottom: 2.5px solid var(--accent); }

/* Remove duplicate outer borders */
.cell:last-child { border-right: none; }

/* ── Grid hint overlay ─────────────────────────────────────────────────── */
.grid-hint {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: center;
    pointer-events: none; z-index: 5;
    transition: opacity .4s ease;
}
.grid-hint.hidden { opacity: 0; }
.grid-hint span {
    font-size: 14px; color: var(--muted); text-align: center;
    line-height: 1.6; padding: 20px;
    background: rgba(255,255,255,.9);
    backdrop-filter: blur(4px); border-radius: 10px;
}

/* ── Responsive cell sizes ─────────────────────────────────────────────── */
@media (max-width: 480px) {
    .sudoku-grid { width: min(96vw, 540px); }
    .sudoku-grid.size-12 { width: min(96vw, 680px); }
    .sudoku-grid.size-16 { width: min(96vw, 720px); }
}

@media print {
    @page { margin: 1.2cm; size: auto; }
    html, body {
        background: #fff !important;
        color: #000 !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }

    /* Hide everything by default, then reveal the grid and its contents */
    body * { visibility: hidden !important; }
    .sudoku-grid, .sudoku-grid *,
    .samurai-grid, .samurai-grid * { visibility: visible !important; }

    /* Neutralize ancestor positioning so the grid's absolute pos is relative to body */
    body, main, section, .play-section, .solver-card, .grid-wrapper,
    .header-section, .sudoku-wrapper {
        position: static !important;
        overflow: visible !important;
    }
    body { margin: 0 !important; padding: 0 !important; }

    /* Center the grid horizontally at the top of the page */
    .sudoku-grid, .samurai-grid {
        position: absolute !important;
        left: 50% !important;
        top: 0 !important;
        transform: translateX(-50%) !important;
        margin: 0 !important;
        box-shadow: none !important;
        page-break-inside: avoid;
        background: #fff !important;
    }

    /* Main grid sizing */
    .sudoku-grid {
        width: 17cm !important;
        max-width: 17cm !important;
        height: 17cm !important;
        border: 2.5px solid #000 !important;
    }
    .sudoku-grid.size-12 { width: 18cm !important; height: 18cm !important; }
    .sudoku-grid.size-16 { width: 18cm !important; height: 18cm !important; }
    .sudoku-grid.size-22 { width: 19cm !important; height: 19cm !important; }

    /* Samurai grid (21×21) */
    .samurai-grid {
        width: 19cm !important;
        max-width: 19cm !important;
        height: 19cm !important;
    }

    /* Cells */
    .cell {
        background: #fff !important;
        color: #000 !important;
        border-right: 1px solid #999 !important;
        border-bottom: 1px solid #999 !important;
        box-shadow: none !important;
        -webkit-print-color-adjust: exact !important;
        print-color-adjust: exact !important;
    }
    .cell.box-right  { border-right:  2.5px solid #000 !important; }
    .cell.box-bottom { border-bottom: 2.5px solid #000 !important; }
    .cell.given {
        background: #e5e5e5 !important;
        color: #000 !important;
        font-weight: 700 !important;
    }
    .cell.user, .cell.solver-fill, .cell.solved-cell {
        background: #fff !important;
        color: #000 !important;
    }
    /* Kill hover/selection/highlight styles in print */
    .cell.selected, .cell.highlighted, .cell.same-number,
    .cell.trying, .cell.backtracking { background: #fff !important; box-shadow: none !important; }
}
