:root {
    --bg-color: #0b1120;
    --panel-bg: #1e293bb3;
    --accent-primary: #38bdf8;
    --accent-secondary: #38bdf8;
    --text-main: #f1f5f9;
    --text-dim: #94a3b8;
    --grid-line: #334155;
    --grid-bold: #64748b;
    --cell-fixed: #ffffff;
    --cell-user: #eaf9db;
    --cell-note: #94a3b8;
    --cell-related: rgba(56, 189, 248, 0.25);
    --cell-hint-related: #00a3d9;
    --cell-selected: #38bdf888;
    --error: #ef4444;
    --success: #10b981;
    --font-sans: 'Outfit', 'Noto Sans JP', sans-serif;
    --glass: blur(16px);
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    background-color: var(--bg-color);
    background-image:
        radial-gradient(at 0% 0%, rgba(56, 189, 248, 0.1) 0px, transparent 50%),
        radial-gradient(at 100% 100%, rgba(129, 140, 248, 0.1) 0px, transparent 50%);
    color: var(--text-main);
    font-family: var(--font-sans);
    line-height: 1.5;
    min-height: 100vh;
}

#app {
    max-width: 1200px;
    margin: 0 auto;
    padding: 1.5rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

/* Header */
header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: var(--panel-bg);
    padding: 1.2rem 2rem;
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    -webkit-backdrop-filter: var(--glass);
    backdrop-filter: var(--glass);
    gap: 2rem;
}

.logo-area h1 {
    font-size: 2.2rem;
    font-weight: 600;
    letter-spacing: 0.1em;
    background: linear-gradient(to right, var(--accent-primary), var(--accent-secondary));
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
    white-space: nowrap;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 2rem;
}

.stats {
    display: flex;
    gap: 2rem;
}

.stat-item {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
}

.stat-item .label {
    font-size: 0.7rem;
    color: var(--text-dim);
    text-transform: uppercase;
    letter-spacing: 0.1em;
}

.stat-item span:not(.label) {
    font-size: 1.3rem;
    font-weight: 600;
    color: var(--accent-primary);
    white-space: nowrap;
}

.lang-btn {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid var(--accent-primary);
    color: var(--accent-primary);
    padding: 0.4rem 1rem;
    border-radius: 20px;
    font-weight: 600;
    font-size: 0.8rem;
    cursor: pointer;
    transition: all 0.3s;
    text-decoration: none;
    display: inline-block;
}

.lang-btn:hover {
    background: var(--accent-primary);
    color: var(--bg-color);
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 1rem;
}

.auth-status {
    display: flex;
    align-items: center;
    gap: 0.8rem;
}

.auth-status .user-greeting {
    font-size: 0.85rem;
    color: var(--text-main);
    opacity: 0.9;
}

.back-link {
    background: rgba(129, 140, 248, 0.1);
    border-color: rgba(129, 140, 248, 0.5);
    color: #a5b4fc;
}

.back-link:hover {
    background: #818cf8;
    border-color: #818cf8;
    color: var(--bg-color);
}

/* Daily View */
#view-daily {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.view-header {
    text-align: center;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1.5rem;
}

.list-nav-header {
    display: flex;
    align-items: center;
    gap: 2rem;
}

.icon-btn {
    background: var(--panel-bg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--accent-primary);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.2rem;
    cursor: pointer;
    transition: all 0.3s;
}

.icon-btn:hover {
    background: var(--accent-primary);
    color: var(--bg-color);
    box-shadow: 0 0 15px var(--accent-primary);
}

.icon-btn:disabled {
    opacity: 0.2;
    cursor: not-allowed;
    background: rgba(255, 255, 255, 0.05);
}

.diff-selector {
    display: flex;
    justify-content: center;
    gap: 0.5rem;
    background: var(--panel-bg);
    padding: 0.4rem;
    border-radius: 50px;
    width: fit-content;
    margin: 0 auto;
    border: 1px solid rgba(255, 255, 255, 0.05);
}

.diff-tab {
    padding: 0.6rem 1.5rem;
    border: none;
    background: none;
    color: var(--text-dim);
    font-weight: 600;
    cursor: pointer;
    border-radius: 50px;
    transition: all 0.3s;
}

.diff-tab.active {
    background: var(--accent-primary);
    color: var(--bg-color);
}

.challenge-list {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
    gap: 1.5rem;
}

.challenge-card {
    background: var(--panel-bg);
    padding: 2rem;
    border-radius: 24px;
    border: 1px solid rgba(255, 255, 255, 0.05);
    text-align: center;
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.challenge-card:hover {
    transform: translateY(-8px);
    border-color: var(--accent-primary);
    box-shadow: 0 10px 30px -10px rgba(56, 189, 248, 0.4);
}

.challenge-card-header {
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 0.8rem;
    margin-bottom: 0.5rem;
}

.check-badge {
    width: 24px;
    height: 24px;
    background: var(--success);
    color: white;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.8rem;
    font-weight: 900;
    box-shadow: 0 0 10px rgba(16, 185, 129, 0.4);
}

.challenge-card.completed {
    border-color: var(--success);
    background: rgba(16, 185, 129, 0.05);
}

.challenge-status {
    margin-top: 1rem;
    padding: 0.4rem;
    border-radius: 12px;
    font-size: 0.85rem;
    font-weight: 700;
    background: rgba(255, 255, 255, 0.05);
}

.completed .challenge-status {
    color: var(--success);
    background: rgba(16, 185, 129, 0.1);
}

.card-stats {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    font-size: 0.8rem;
    line-height: 1.2;
}

.card-mistakes {
    font-size: 0.7rem;
    opacity: 0.7;
    font-weight: 500;
}

/* Game Grid */
.game-layout {
    display: flex;
    gap: 3rem;
    align-items: flex-start;
    justify-content: center;
    width: 100%;
}

.sudoku-grid {
    display: grid;
    grid-template-columns: repeat(9, 1fr);
    width: min(90vw, 540px);
    aspect-ratio: 1;
    background-color: var(--grid-line);
    border: 4px solid var(--grid-bold);
    border-radius: 8px;
    gap: 1px;
    box-shadow: 0 25px 50px -12px rgba(0, 0, 0, 0.5);
    overflow: hidden;
}

.cell {
    background-color: var(--bg-color);
    display: flex;
    justify-content: center;
    align-items: center;
    cursor: pointer;
    position: relative;
    -webkit-user-select: none;
    user-select: none;
    width: 100%;
    height: 100%;
}

.cell::before {
    content: "";
    display: block;
    padding-bottom: 100%;
}

.cell-value {
    position: absolute;
    inset: 0;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: min(2.5rem, 6vw);
    transition: color 0.2s;
}

.cell:nth-child(3n):not(:nth-child(9n)) {
    border-right: 2px solid var(--grid-bold);
}

.cell:nth-child(n+19):nth-child(-n+27),
.cell:nth-child(n+46):nth-child(-n+54) {
    border-bottom: 2px solid var(--grid-bold);
}

.cell.selected {
    background-color: rgba(56, 189, 248, 0.5) !important;
    outline: 3px solid #ffffff;
    outline-offset: -3px;
    box-shadow: inset 0 0 20px var(--accent-primary);
    z-index: 5;
}

.cell.related {
    background-color: var(--cell-related) !important;
}

.cell.hint-related-val .cell-value {
    color: var(--accent-secondary) !important;
    font-weight: 800;
    text-shadow: 0 0 10px rgba(129, 140, 248, 0.4);
}

.cell.fixed {
    background-color: rgba(255, 255, 255, 0.05);
}

.cell.fixed .cell-value {
    font-weight: 700;
    color: var(--cell-fixed);
    text-shadow: 0 0 5px rgba(255, 255, 255, 0.2);
}

.cell.user-val .cell-value {
    color: var(--cell-user);
    font-weight: 600;
}

.cell.error .cell-value {
    color: var(--error) !important;
    font-weight: 800;
}

.cell.error {
    background: rgba(239, 68, 68, 0.15) !important;
}

.notes-grid {
    position: absolute;
    inset: 3px;
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    pointer-events: none;
}

.note-item {
    font-size: min(0.8rem, 2vw);
    color: var(--cell-note);
    display: flex;
    justify-content: center;
    align-items: center;
    opacity: 0;
}

.note-item.visible {
    opacity: 1;
}

.note-item.highlighted {
    color: var(--accent-primary);
    font-weight: 700;
    transform: scale(1.3);
}

/* Sidebar */
.game-sidebar {
    width: 340px;
    display: flex;
    flex-direction: column;
    gap: 1.2rem;
}

.input-mode-selector {
    display: flex;
    background: var(--panel-bg);
    padding: 0.4rem;
    border-radius: 16px;
    border: 1px solid rgba(255, 255, 255, 0.1);
}

.mode-tab {
    flex: 1;
    background: none;
    border: none;
    color: var(--text-dim);
    padding: 0.7rem;
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s;
}

.mode-tab.active {
    background: var(--accent-primary);
    color: var(--bg-color);
}

.confirm-area {
    padding: 0.2rem;
}

.primary-btn {
    width: 100%;
    padding: 1.1rem;
    border-radius: 16px;
    border: none;
    background: linear-gradient(to right, var(--accent-primary), var(--accent-secondary));
    color: white;
    font-weight: 700;
    font-size: 1.1rem;
    cursor: pointer;
    box-shadow: 0 10px 20px -10px rgba(56, 189, 248, 0.5);
    transition: all 0.3s;
}

.primary-btn:disabled {
    background: #1e293b;
    color: #475569;
    border: 1px solid #334155;
    opacity: 0.5;
    cursor: not-allowed;
    box-shadow: none;
}

.numpad {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 0.7rem;
}

.num-btn {
    aspect-ratio: 1.1;
    background: var(--panel-bg);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 16px;
    color: var(--text-main);
    cursor: pointer;
    position: relative;
    transition: all 0.2s;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
}

.num-btn span:first-child {
    font-size: 1.8rem;
    font-weight: 600;
}

.num-btn .count {
    font-size: 0.65rem;
    color: var(--text-dim);
}

.num-btn.active {
    background: var(--accent-secondary);
    border-color: var(--accent-secondary);
    box-shadow: 0 0 15px var(--accent-secondary);
}

.num-btn.active .count {
    color: var(--bg-color);
    opacity: 0.8;
}

.num-btn.completed {
    opacity: 0.25;
}

.utility-btn {
    grid-column: span 3;
    height: 3.5rem;
    font-size: 0.95rem;
    color: var(--accent-secondary);
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 12px;
}

.game-actions {
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    gap: 0.8rem;
}

.secondary-btn {
    flex: 1;
    padding: 0.9rem;
    background: none;
    border: 1px solid var(--grid-bold);
    color: var(--text-main);
    border-radius: 12px;
    font-weight: 600;
    cursor: pointer;
}

/* Overlays */
.overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.85);
    -webkit-backdrop-filter: var(--glass);
    backdrop-filter: var(--glass);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 4000;
}

.modal {
    background: var(--bg-color);
    padding: 3rem;
    border-radius: 32px;
    border: 1px solid rgba(255, 255, 255, 0.1);
    text-align: center;
    max-width: 440px;
    width: 90%;
}

.modal h2 {
    font-size: 2rem;
    color: var(--accent-primary);
    margin-bottom: 1rem;
}

.modal-btns {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

/* Replay */
.replay-bar {
    position: fixed;
    bottom: 0;
    left: 0;
    width: 100%;
    background: var(--panel-bg);
    padding: 1.2rem 2rem;
    z-index: 5000;
    border-top: 1px solid rgba(255, 255, 255, 0.1);
    -webkit-backdrop-filter: var(--glass);
    backdrop-filter: var(--glass);
    box-shadow: 0 -10px 30px rgba(0, 0, 0, 0.5);
}

.replay-content {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 1rem;
}

.replay-info-row {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.replay-controls {
    display: flex;
    gap: 1rem;
    align-items: center;
}

.replay-controls button {
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid rgba(255, 255, 255, 0.1);
    color: var(--text-main);
    padding: 0.8rem 1.5rem;
    border-radius: 16px;
    font-size: 1.4rem;
    cursor: pointer;
    transition: all 0.2s;
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 70px;
    min-height: 56px;
}

.replay-controls button:hover {
    background: var(--accent-primary);
    color: var(--bg-color);
    border-color: var(--accent-primary);
    transform: translateY(-2px);
}

.replay-controls button:active {
    transform: translateY(0);
}

#replay-play {
    min-width: 140px;
    font-weight: 700;
    font-size: 1.1rem;
    background: rgba(56, 189, 248, 0.15);
    border-color: var(--accent-primary);
    color: var(--accent-primary);
}

#replay-exit {
    font-size: 1rem;
    font-weight: 700;
}

.progress-bar-container {
    width: 100%;
    height: 8px;
    background: rgba(255, 255, 255, 0.1);
    border-radius: 4px;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: var(--accent-primary);
    width: 0%;
    transition: width 0.3s;
}

/* Game Mode Adjustments */
body:not(.game-mode) #seed-container,
body:not(.game-mode) #status-time,
body:not(.game-mode) #status-mistakes {
    display: none;
}

body.game-mode .logo-area {
    display: none;
}

body.game-mode header {
    padding: 0.8rem 2rem;
    justify-content: flex-end;
}

body.game-mode #app {
    gap: 1rem;
}

.challenge-seed {
    font-size: 0.85rem;
    color: var(--text-dim);
    margin: 0.5rem 0;
    font-family: monospace;
    letter-spacing: 0.05em;
}

/* Hide controls during replay */
body.replaying #btn-erase,
body.replaying .game-actions,
body.replaying .confirm-area {
    display: none !important;
}

.hidden {
    display: none !important;
}

@media (max-width: 900px) {
    header {
        flex-direction: column;
        gap: 1rem;
        border-radius: 0;
        padding: 1rem;
    }

    .header-right {
        width: 100%;
        justify-content: space-between;
    }

    .logo-area {
        text-align: center;
    }

    .game-layout {
        flex-direction: column;
        align-items: center;
    }

    .game-sidebar {
        width: 100%;
        max-width: 540px;
    }

    .numpad {
        grid-template-columns: repeat(5, 1fr);
    }

    .utility-btn {
        grid-column: span 1;
        height: auto;
    }
}

/* Tablet Landscape Specific (Optimizing for vertical space) */
@media (min-width: 901px) and (max-height: 850px) {
    #app {
        padding: 0.4rem 1rem;
        gap: 0.4rem;
    }

    body.game-mode header {
        padding: 0.4rem 1.5rem;
        border-radius: 0 0 20px 20px;
    }

    .sudoku-grid {
        width: min(82vh, 520px);
        /* Use vh to ensure it fits vertically */
    }

    .game-layout {
        gap: 1.5rem;
    }

    .game-sidebar {
        width: 300px;
        gap: 0.5rem;
    }

    .numpad {
        gap: 0.4rem;
    }

    .num-btn {
        aspect-ratio: 1.3;
        border-radius: 12px;
    }

    .num-btn span:first-child {
        font-size: 1.5rem;
    }

    .replay-bar {
        padding: 0.6rem 1.5rem;
    }

    .replay-content {
        gap: 0.4rem;
    }

    .replay-controls button {
        padding: 0.8rem 1.4rem;
        min-height: 60px;
        /* Larger touch target */
        min-width: 80px;
        font-size: 1.6rem;
    }

    #replay-play {
        min-width: 160px;
        font-size: 1.2rem;
    }
}

/* Micro-interactions & Animations */
.cell.error-anim {
    animation: shake 0.4s cubic-bezier(.36,.07,.19,.97) both;
}

.cell-value {
    animation: fadeIn 0.3s ease-out;
}

@keyframes shake {
    10%, 90% { transform: translate3d(-1px, 0, 0); }
    20%, 80% { transform: translate3d(2px, 0, 0); }
    30%, 50%, 70% { transform: translate3d(-4px, 0, 0); }
    40%, 60% { transform: translate3d(4px, 0, 0); }
}

@keyframes fadeIn {
    from { opacity: 0; transform: scale(0.5); }
    to { opacity: 1; transform: scale(1); }
}

/* Loading Overlay & Spinner */
.loading-overlay {
    position: fixed;
    inset: 0;
    background: rgba(11, 17, 32, 0.8);
    -webkit-backdrop-filter: blur(8px);
    backdrop-filter: blur(8px);
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    z-index: 9999;
    color: var(--accent-primary);
    font-weight: 600;
    gap: 1rem;
}

.spinner {
    width: 40px;
    height: 40px;
    border: 3px solid rgba(56, 189, 248, 0.2);
    border-top-color: var(--accent-primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}