/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: system-ui, -apple-system, sans-serif;
    background: var(--color-bg, #fafbfc);
    color: #333;
    min-height: 100vh;
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
}

#app {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
    padding-top: 52px; /* account for game-page fixed navbar */
}

.offline-indicator {
    display: none;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.75rem;
    color: #dc2626;
}

.offline-indicator.show {
    display: flex;
}

.offline-dot {
    width: 8px;
    height: 8px;
    background: #dc2626;
    border-radius: 50%;
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Screen Management */
.screen {
    display: none;
    flex: 1;
    padding: 0.5rem 0.75rem;
}

.screen.active {
    display: flex;
    flex-direction: column;
    min-height: 0;
    overflow: hidden;
}

/* Menu Styles */
.menu-container {
    max-width: 600px;
    margin: 0 auto;
    width: 100%;
}

.menu-title {
    text-align: center;
    font-size: 2rem;
    font-weight: 700;
    color: #1f2937;
    margin-bottom: 2rem;
}

.difficulty-cards {
    display: grid;
    gap: 1rem;
    grid-template-columns: 1fr;
}

@media (min-width: 640px) {
    .difficulty-cards {
        grid-template-columns: repeat(3, 1fr);
    }
}

.difficulty-card {
    background: rgba(255, 255, 255, 0.95);
    border-radius: 16px;
    padding: 2rem;
    text-align: center;
    cursor: pointer;
    transition: all 0.3s ease;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
    border: 2px solid transparent;
}

.difficulty-card:hover {
    transform: translateY(-4px);
    box-shadow: 0 8px 30px rgba(0, 0, 0, 0.15);
    border-color: #2563eb;
}

.difficulty-card:active {
    transform: translateY(-2px);
}

.difficulty-card h3 {
    font-size: 1.5rem;
    font-weight: 600;
    color: #1f2937;
    margin-bottom: 0.5rem;
}

.difficulty-card p {
    color: #6b7280;
    font-size: 0.875rem;
}

.how-to-play-link {
    text-align: center;
    margin-top: 1.25rem;
    font-size: 0.875rem;
    color: #6b7280;
}

.how-to-play-link a {
    color: #2563eb;
    text-decoration: none;
}

.how-to-play-link a:hover {
    text-decoration: underline;
}

/* Back Button */
.back-btn {
    background: #f3f4f6;
    border: none;
    color: #374151;
    padding: 0.4rem 0.75rem;
    border-radius: 8px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.3s ease;
    white-space: nowrap;
}

.back-btn:hover {
    background: #e5e7eb;
}

/* Game Screen */
.game-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255, 255, 255, 0.95);
    padding: 0.35rem 0.6rem;
    border-radius: 10px;
    margin-bottom: 0.4rem;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
    gap: 0.5rem;
}

.game-info {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    flex: 1;
    justify-content: center;
}

.game-header-right {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}

.game-header-sep {
    color: #d1d5db;
    font-size: 0.75rem;
}

#currentDifficulty {
    font-size: 0.75rem;
    font-weight: 600;
    color: #6b7280;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.new-game-btn {
    background: #2563eb;
    color: white;
    border: none;
    padding: 0.4rem 0.75rem;
    border-radius: 8px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.3s ease;
    white-space: nowrap;
}

.new-game-btn:hover {
    background: #1d4ed8;
    transform: translateY(-1px);
}

/* Game Container */
.game-container {
    flex: 1;
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0.25rem 0;
    min-height: 0;
    overflow: hidden;
}

.game-board {
    display: none;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
    width: 100%;
    max-width: 500px;
    overflow: hidden;
}

.game-board.active {
    display: flex;
}

/* Sudoku Styles */
.sudoku-grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    grid-template-rows: repeat(9, 1fr);
    gap: 1px;
    background: #1f2937;
    border: 3px solid #1f2937;
    border-radius: 8px;
    width: min(100%, calc(100svh - 240px), 400px);
    aspect-ratio: 1;
}

.sudoku-cell {
    background: white;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: clamp(1rem, min(6vw, calc((100svh - 240px) / 9 * 0.65)), 2.25rem);
    font-weight: 700;
    cursor: pointer;
    transition: all 0.2s ease;
    position: relative;
}

.sudoku-cell:nth-child(3n):not(:nth-child(9n)) {
    border-right: 2px solid #1f2937;
}

.sudoku-cell:nth-child(n+19):nth-child(-n+27),
.sudoku-cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 2px solid #1f2937;
}

.sudoku-cell.given {
    background: #f3f4f6;
    color: #9ca3af;
    cursor: default;
}

.sudoku-cell.typed {
    color: #1e3a8a;
}

.sudoku-cell.selected {
    background: #dbeafe;
    border: 2px solid #2563eb;
}

.sudoku-cell.error {
    background: #fecaca;
    color: #dc2626;
}

.sudoku-cell.highlight {
    background: #fef3c7;
}

.sudoku-cell.same-digit {
    color: #15803d !important;
    font-weight: 700;
}

/* Number Pad */
.number-pad {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.5rem;
    width: 100%;
    max-width: 300px;
    flex-shrink: 0;
}

.number-btn, .erase-btn {
    aspect-ratio: 1;
    background: white;
    border: 2px solid #e5e7eb;
    border-radius: 8px;
    font-size: 1.2rem;
    font-weight: 600;
    cursor: pointer;
    transition: background 0.3s ease, border-color 0.2s ease, transform 0.2s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    position: relative;
    overflow: hidden;
}

.number-btn {
    background: linear-gradient(to top, rgba(37, 99, 235, 0.18) var(--fill-height, 0%), white var(--fill-height, 0%));
}

.number-btn:hover, .erase-btn:hover {
    border-color: #2563eb;
    background: #f0f9ff;
}

.number-btn:active, .erase-btn:active {
    transform: scale(0.95);
}

.erase-btn {
    background: #fee2e2;
    border-color: #fca5a5;
    color: #dc2626;
}

.erase-btn:hover {
    background: #fecaca;
    border-color: #f87171;
}

.number-btn.fully-used {
    opacity: 0.35;
    cursor: not-allowed;
    background: #f3f4f6 !important;
}

.timer, .progress {
    font-size: 0.75rem;
    font-weight: 600;
    color: #1f2937;
}

/* Modal */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    padding: 1rem;
}

.modal.show {
    display: flex;
}

.modal-content {
    background: white;
    border-radius: 16px;
    padding: 2rem;
    text-align: center;
    max-width: 400px;
    width: 100%;
    box-shadow: 0 20px 40px rgba(0, 0, 0, 0.3);
    animation: modalSlideIn 0.3s ease;
}

@keyframes modalSlideIn {
    from {
        opacity: 0;
        transform: translateY(-20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.modal-content h2 {
    color: #1f2937;
    margin-bottom: 1rem;
    font-size: 1.5rem;
}

.modal-content p {
    color: #6b7280;
    margin-bottom: 2rem;
}

.modal.success-state .modal-content {
    border-top: 4px solid #16a34a;
}

.modal.success-state h2 {
    color: #15803d;
}

.modal.error-state .modal-content {
    border-top: 4px solid #dc2626;
}

.modal.error-state h2 {
    color: #dc2626;
}

.btn-continue {
    background: #ea580c;
}

.btn-continue:hover {
    background: #c2410c;
}

.modal-buttons {
    display: flex;
    gap: 1rem;
    justify-content: center;
}

.btn-primary, .btn-secondary {
    padding: 0.75rem 1.5rem;
    border: none;
    border-radius: 8px;
    font-size: 0.875rem;
    cursor: pointer;
    transition: all 0.3s ease;
    font-weight: 600;
}

.btn-primary {
    background: #2563eb;
    color: white;
}

.btn-primary:hover {
    background: #1d4ed8;
}

.btn-secondary {
    background: #f3f4f6;
    color: #374151;
}

.btn-secondary:hover {
    background: #e5e7eb;
}

/* Responsive Design */
@media (max-width: 480px) {
    .menu-title {
        font-size: 1.5rem;
        margin-bottom: 1.5rem;
    }

    .difficulty-card {
        padding: 1.5rem;
    }

    .sudoku-cell {
        font-size: clamp(1rem, min(6vw, calc((100svh - 240px) / 9 * 0.65)), 2.1rem);
    }

    .number-pad {
        max-width: 250px;
    }

    .number-btn, .erase-btn {
        font-size: 1rem;
    }
}

@media (min-width: 768px) {
    .screen {
        padding: 1rem 1.5rem;
    }

    .menu-title {
        font-size: 2.5rem;
    }

    .sudoku-grid {
        width: min(100%, calc(100svh - 260px), 450px);
    }

    .sudoku-cell {
        /* scale font with available grid height so it never forces rows taller than 1/9 of grid */
        font-size: min(2.55rem, calc((100svh - 260px) / 9 * 0.62));
    }

    .number-pad {
        max-width: 350px;
    }

    .number-btn, .erase-btn {
        font-size: 1.4rem;
    }
}

/* Touch Feedback */
@media (hover: none) and (pointer: coarse) {
    .difficulty-card:active,
    .number-btn:active,
    .erase-btn:active,
    .sudoku-cell:active {
        transform: scale(0.98);
        transition: transform 0.1s ease;
    }
}

/* Wide screen: grid and number pad side by side to prevent vertical overflow */
@media (min-width: 900px) {
    .game-board {
        flex-direction: row;
        align-items: center;
        max-width: min(860px, 92vw);
        gap: 2rem;
    }

    .sudoku-grid {
        width: min(calc(100svh - 164px), 500px, 58vw);
        flex-shrink: 0;
    }

    .sudoku-cell {
        font-size: min(2.55rem, calc((100svh - 164px) / 9 * 0.62));
    }

    #sudokuNumberPad {
        grid-template-columns: repeat(3, 1fr);
        max-width: 200px;
        gap: 0.4rem;
    }

    #sudokuNumberPad .erase-btn {
        grid-column: span 3;
        aspect-ratio: auto;
        padding: 0.5rem;
    }
}
