/* ============================================
   BULLETPROOF GEOMETRY & DARK MODE FALLBACKS
   ============================================ */
#number-pad {
    display: grid !important;
    grid-template-columns: repeat(3, 1fr) !important;
    gap: 0.75rem !important;
}

/* Responsive Tools */
#numpad-utils button {
    width: 48px !important;
    height: 48px !important;
    border-radius: 50% !important;
}

@media (min-width: 640px) {
    #numpad-utils button {
        width: 64px !important;
        height: 64px !important;
    }
}

/* Force Check Button Visibility */
#check-btn {
    background-color: #1e40af !important;
    /* Blue-800: High contrast on white */
    color: #ffffff !important;
    transition: background-color 0.2s ease !important;
}

#check-btn:hover {
    background-color: #1e3a8a !important;
    /* Blue-900 */
}

.dark #check-btn {
    background-color: #2563eb !important;
    /* Blue-600: Compliant on dark backgrounds vs white text */
}

.dark #check-btn:hover {
    background-color: #1d4ed8 !important;
    /* Blue-700 */
}

#puzzle-grid .sudoku-cell {
    background-color: #ffffff !important;
}

.dark #puzzle-grid .sudoku-cell {
    background-color: #1e293b !important;
}

.dark #puzzle-grid .sudoku-cell span {
    color: #f8fafc !important;
}

.dark #puzzle-grid .sudoku-cell.font-black span {
    color: #60a5fa !important;
}

/* ============================================
   GRID BORDER SYSTEM (CALCUDOKU STYLE)
   ============================================ */

#puzzle-grid {
    display: grid !important;
    grid-template-columns: repeat(9, 1fr) !important;
    aspect-ratio: 1 / 1 !important;
    background-color: #ffffff !important;
    /* Default: Light Background */
    border: 4px solid #0f172a !important;
    /* Increased to 4px Thick Border */
    width: 100% !important;
    max-width: 650px !important;
    gap: 0 !important;
    margin: 0 auto !important;
}

.dark #puzzle-grid {
    background-color: #0f172a !important;
    /* Dark Mode: Dark Background */
    border-color: #ffffff !important;
    /* Dark Mode: White Thick Border */
}

.sudoku-cell {
    border: 1px solid #94a3b8 !important;
    /* Lighter 1px dividers for maximum contrast */
    margin: 0 !important;
    padding: 0 !important;
    box-sizing: border-box !important;
    background-color: transparent !important;
    /* Let container bg show through or handle in modes */
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
}

/* 3x3 Box Thick Borders */
/* Default: Dark Borders for Light Mode (4px Weight) */
/* Vertical Dividers */
.sudoku-cell:nth-child(9n+3),
.sudoku-cell:nth-child(9n+6) {
    border-right: 4px solid #0f172a !important;
}

/* Horizontal Dividers */
.sudoku-cell:nth-child(n+19):nth-child(-n+27),
.sudoku-cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 4px solid #0f172a !important;
}

/* Dark Mode Overrides: White Borders (4px Weight) */
.dark .sudoku-cell:nth-child(9n+3),
.dark .sudoku-cell:nth-child(9n+6) {
    border-right-color: #ffffff !important;
}

.dark .sudoku-cell:nth-child(n+19):nth-child(-n+27),
.dark .sudoku-cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom-color: #ffffff !important;
}

/* Inner Layout: Board Left, Controls Right (Inside Game Column) */
#game-interface-wrapper {
    display: flex;
    flex-direction: column;
    width: 100%;
    gap: 1rem;
    /* Reduced from 2rem for mobile */
}

@media (min-width: 1024px) {
    #game-interface-wrapper {
        display: grid !important;
        grid-template-columns: 1fr 300px !important;
        /* 1fr forces the grid to take remaining space, 300px locks the control panel size */
        align-items: start;
        gap: 2rem;
    }

    /* Scaling desktop controls (~20% increase) */
    .numpad-btn {
        padding: 1rem !important;
    }

    .numpad-btn span.text-3xl {
        font-size: 2.25rem !important;
        /* ~20% larger */
    }

    #numpad-utils button {
        width: 76px !important;
        /* Increased from 64px */
        height: 76px !important;
        font-size: 2rem !important;
    }

    #new-game,
    #check-btn {
        padding: 1.25rem !important;
        font-size: 1.2rem !important;
    }
}

/* ============================================
   VISUAL HIERARCHY & LEGIBILITY
   ============================================ */

#game-status-header {
    font-size: 1.1rem !important;
    /* Scaled up by ~20% */
}

#current-difficulty-display,
#mistake-counter {
    font-weight: 700 !important;
    /* Stronger weight for values */
}

/* Utility Badges (Dynamic) */
.hint-badge {
    width: 28px !important;
    /* Increased from 24px */
    height: 28px !important;
    font-size: 14px !important;
    /* Increased from text-xs */
    top: -8px !important;
    right: -8px !important;
    display: flex !important;
    align-items: center !important;
    justify-content: center !important;
    line-height: normal !important;
}

/* ============================================
   ACCESSIBILITY
   ============================================ */
.calcudoku-cell:focus {
    outline: 3px solid var(--calcudoku-selected);
    outline-offset: -3px;
}

.numpad-btn:focus {
    outline: 2px solid var(--calcudoku-selected);
    outline-offset: 2px;
}

/* Reduced motion preference */
@media (prefers-reduced-motion: reduce) {

    .calcudoku-cell,
    .numpad-btn,
    #new-game,
    #check-btn,
    #hint-btn {
        transition: none;
    }

    .calcudoku-cell.incorrect {
        animation: none;
    }

    .calcudoku-cell.success {
        animation: none;
    }

    .calcudoku-toast {
        animation: none;
    }
}

/* ============================================
   MONEY LAYOUT ENFORCEMENT (REFINED)
   ============================================ */
/* Desktop Layout Rules */
.page-container {
    display: flex;
    flex-wrap: wrap;
    /* Allow wrapping on mobile */
    max-width: 1200px;
    margin: 0 auto;
    width: 100%;
    position: relative;
    align-items: flex-start;
    /* Ensure proper alignment */
}

/* Columns - Mobile Default */
.game-column,
.sidebar-column {
    width: 100%;
    display: flex;
    flex-direction: column;
}

/* Desktop Styles (> 1024px to match 'lg') */
/* Default / Mobile Styles (< 1024px) */
.main-layout-wrapper {
    display: flex;
    flex-direction: column;
    align-items: center;
    width: 100%;
    margin: 0 auto;
    padding-top: 20px;
}

.game-container {
    width: 100%;
    display: flex;
    flex-direction: column;
}

.sticky-sidebar {
    display: none;
    /* Hide on mobile per request */
}

/* Desktop Styles (> 1024px to match 'lg') */
@media (min-width: 1024px) {
    .main-layout-wrapper {
        display: grid;
        grid-template-columns: 1fr 300px;
        gap: 32px;
        align-items: start;
        max-width: 1280px;
        margin: 0 auto;
        padding-top: 20px;
    }

    .game-container {
        flex: 1;
        width: calc(100% - 320px) !important;
        max-width: calc(100% - 320px) !important;
        display: block !important;
    }

    .sticky-sidebar {
        flex: 0 0 300px;
        width: 300px;
        margin-top: 40px;
        display: block;
        position: sticky;
        top: 2rem;
        height: auto;
        padding-left: 0;
    }

    .ad-container-desktop {
        display: block;
        min-height: 600px;
        width: 100%;
        margin-top: 2rem;
    }

    .ad-container-mobile {
        display: none !important;
    }
}

.ad-container-mobile {
    display: block;
    margin: 2rem auto;
    width: 100%;
    max-width: 650px;
}

.ad-container-desktop {
    display: none;
}

footer {
    clear: both;
}

.sudoku-cell.fixed {
    font-weight: 800;
    color: #0f172a;
    background-color: #f1f5f9;
}

.dark .sudoku-cell.fixed {
    color: #f1f5f9;
    background-color: #0f172a;
}

.sudoku-cell.selected {
    background-color: #bae6fd !important;
    outline: 2px solid #0284c7;
    outline-offset: -2px;
    z-index: 10;
}

.dark .sudoku-cell.selected {
    background-color: #0c4a6e !important;
    outline: 2px solid #38bdf8;
}

.sudoku-cell.highlighted {
    background-color: #e0f2fe;
}

.dark .sudoku-cell.highlighted {
    background-color: #164e63;
}

.sudoku-cell.conflict {
    color: #ef4444 !important;
    background-color: #fee2e2;
}

.dark .sudoku-cell.conflict {
    color: #f87171 !important;
    background-color: #7f1d1d;
}

.candidates-grid {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    grid-template-rows: repeat(3, 1fr);
    pointer-events: none;
}

.candidate {
    font-size: clamp(8px, 2.5vw, 12px);
    font-weight: 500;
    color: #64748b;
    display: flex;
    align-items: center;
    justify-content: center;
}

.dark .candidate {
    color: #94a3b8;
}