/* --- Root Variables & Themes --- */
:root {
    /* Dark Theme (Default) */
    --bg-color: #1a1b26;
    --fg-color: #c0caf5;
    --primary-color: #7aa2f7;
    --secondary-color: #bb9af7;
    --error-color: #f7768e;
    --correct-color: #9ece6a;
    --subtle-color: #565f89;
    --card-bg: rgba(26, 27, 38, 0.8);
    --ad-bg: #24283b;
    --border-color: #414868;
    --accent-color: #449dab;
    --transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.light-theme {
    --bg-color: #f2f4f8;
    --fg-color: #24283b;
    --primary-color: #3d59a1;
    --secondary-color: #897af3;
    --error-color: #db4b4b;
    --correct-color: #485e30;
    --subtle-color: #a9b1d6;
    --card-bg: rgba(255, 255, 255, 0.8);
    --ad-bg: #e1e7f0;
    --border-color: #cfd9e6;
    --accent-color: #2ac3de;
}

/* --- Resets & Global --- */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

html {
    font-size: 16px;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--fg-color);
    line-height: 1.6;
    transition: var(--transition);
    overflow-x: hidden;
}

.container {
    max-width: 1200px;
    margin: 0 auto;
    padding: 0 2rem;
}

h1,
h2,
h3,
.logo span {
    font-family: 'Outfit', sans-serif;
}

/* --- Header & Navigation --- */
.header {
    height: 80px;
    display: flex;
    align-items: center;
    border-bottom: 1px solid var(--border-color);
    position: sticky;
    top: 0;
    background-color: var(--bg-color);
    z-index: 100;
}

.nav {
    display: flex;
    justify-content: space-between;
    align-items: center;
    width: 100%;
    gap: 5rem;
    /* Minimum gap between the three main groups */
}

.nav-link {
    color: var(--subtle-color);
    text-decoration: none;
    font-weight: 500;
    font-size: 0.95rem;
    transition: var(--transition);
}

.nav-link:hover {
    color: var(--primary-color);
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    font-size: 1.5rem;
    font-weight: 700;
    color: var(--primary-color);
    cursor: pointer;
}

.header-left,
.header-right {
    display: flex;
    align-items: center;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 5rem;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 5rem;
    justify-content: flex-end;
}

.action-group {
    display: flex;
    align-items: center;
    gap: 5rem;
}

.main-nav {
    display: flex;
    gap: 5rem;
}

/* Responsive Header Adjustment */
@media (max-width: 1000px) {
    .header {
        height: auto;
        padding: 1rem 0;
    }

    .nav {
        flex-direction: column;
        gap: 1.5rem;
    }

    .main-nav {
        gap: 1rem;
    }

    .header-right {
        width: 100%;
        justify-content: center;
        gap: 2rem;
    }
}

.main-nav .nav-link {
    background: none;
    border: none;
    color: var(--subtle-color);
    font-weight: 500;
    cursor: pointer;
    font-family: inherit;
    font-size: 0.95rem;
    padding: 0.5rem 0;
    border-bottom: 2px solid transparent;
}

.main-nav .nav-link.active {
    color: var(--primary-color);
    border-bottom-color: var(--primary-color);
}

.user-stats-pill {
    display: flex;
    align-items: center;
    gap: 1rem;
    background: var(--ad-bg);
    padding: 0.4rem 1.25rem;
    border-radius: 12px;
    border: 1px solid var(--border-color);
    transition: var(--transition);
    cursor: pointer;
}

.user-stats-pill:hover {
    border-color: var(--primary-color);
    box-shadow: 0 0 15px rgba(122, 162, 247, 0.2);
    transform: translateY(-2px);
}

.stat-group {
    display: flex;
    flex-direction: column;
    align-items: flex-start;
    line-height: 1.1;
}

.stat-group .label {
    font-size: 0.7rem;
    color: var(--subtle-color);
    font-weight: 700;
}

.stat-group .val {
    font-weight: 700;
    font-size: 1rem;
    color: var(--fg-color);
}

.stat-group.streak {
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    color: #ff9e64;
}

.divider {
    width: 1px;
    height: 20px;
    background: var(--border-color);
}

/* --- Dashboard View --- */
.dashboard-view {
    padding: 2rem 0;
    animation: fadeIn 0.4s ease;
}

.dashboard-header {
    margin-bottom: 2rem;
}

.dashboard-header h2 {
    font-size: 2rem;
    color: var(--primary-color);
}

.dashboard-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 2rem;
    margin-bottom: 2rem;
}

.dash-card {
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 20px;
    padding: 2rem;
}

.stat-item {
    display: flex;
    justify-content: space-between;
    padding: 1rem 0;
    border-bottom: 1px solid var(--border-color);
}

.level-showcase {
    display: flex;
    align-items: center;
    gap: 2.5rem;
}

.lvl-circle {
    width: 100px;
    height: 100px;
    border: 4px solid var(--primary-color);
    border-radius: 50%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    box-shadow: 0 0 20px rgba(122, 162, 247, 0.2);
}

.lvl-circle span {
    font-size: 2.5rem;
    font-weight: 700;
}

.lvl-circle label {
    font-size: 0.7rem;
    text-transform: uppercase;
    font-weight: 700;
    color: var(--subtle-color);
}

.progress-bar.big {
    height: 12px;
    margin: 1rem 0;
}

.badge {
    background: var(--primary-color);
    color: var(--bg-color);
    padding: 0.25rem 0.75rem;
    border-radius: 99px;
    font-weight: 600;
    font-size: 0.85rem;
}

.streak-badge {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    color: #ff9e64;
    font-weight: 700;
}

.icon-btn {
    background: none;
    border: none;
    color: var(--fg-color);
    font-size: 1.25rem;
    cursor: pointer;
    transition: var(--transition);
}

.icon-btn:hover {
    color: var(--primary-color);
    transform: scale(1.1);
}

/* --- Ad Placeholders --- */
.ad-placeholder {
    display: flex;
    justify-content: center;
    align-items: center;
    background-color: var(--ad-bg);
    border: 1px dashed var(--border-color);
    margin: 1rem auto;
    color: var(--subtle-color);
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 1px;
}

.banner-ad {
    width: 100%;
    max-width: 728px;
    height: 90px;
}

.side-ad {
    width: 300px;
    height: 250px;
    margin-bottom: 2rem;
}

/* --- Main Layout --- */
.game-layout {
    display: grid;
    grid-template-columns: 1fr 320px;
    gap: 3rem;
    margin-top: 2rem;
}

.game-content {
    display: flex;
    flex-direction: column;
    gap: 2rem;
}

/* --- Settings Bar --- */
.settings-bar {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 1rem;
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    border-radius: 12px;
    border: 1px solid var(--border-color);
    flex-wrap: wrap;
    gap: 1rem;
}

.mode-selectors,
.timer-selectors {
    display: flex;
    gap: 0.5rem;
}

.mode-btn,
.time-btn,
.diff-btn {
    background: none;
    border: 1px solid transparent;
    color: var(--subtle-color);
    padding: 0.5rem 1rem;
    border-radius: 8px;
    cursor: pointer;
    font-weight: 500;
    transition: var(--transition);
}

.mode-btn:hover,
.time-btn:hover,
.diff-btn:hover {
    color: var(--fg-color);
}

.mode-btn.active,
.time-btn.active,
.diff-btn.active {
    background: var(--primary-color);
    color: var(--bg-color);
    box-shadow: 0 4px 12px rgba(122, 162, 247, 0.3);
}

/* --- Stats Panel --- */
.stats-overview {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 1.5rem;
}

.stat-card {
    background: var(--card-bg);
    padding: 1.5rem;
    border-radius: 16px;
    border: 1px solid var(--border-color);
    display: flex;
    flex-direction: column;
    align-items: center;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.1);
}

.stat-label {
    font-size: 0.85rem;
    color: var(--subtle-color);
    font-weight: 600;
    text-transform: uppercase;
}

.stat-value {
    font-size: 2.5rem;
    font-weight: 700;
    color: var(--primary-color);
    font-family: 'Outfit', sans-serif;
}

.stat-value.secondary {
    color: var(--secondary-color);
}

/* --- Typing Box --- */
.typing-container {
    position: relative;
    background: var(--card-bg);
    padding: 1rem 2rem;
    /* Tighter padding */
    border-radius: 20px;
    border: 1px solid var(--border-color);
    height: 110px;
    /* Perfectly fits 2 lines with 44px line-height + padding */
    overflow: hidden;
    transition: var(--transition);
}

.main-input {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    opacity: 0;
    cursor: pointer;
    z-index: 20;
    /* On top when NOT focused to capture the click */
    border: none;
    background: transparent;
}

.main-input:focus {
    outline: none;
    cursor: text;
    z-index: 5;
    /* Sit behind words so we can see text but stays focused */
}

/* When focused, the overlay should hide */
.typing-container:focus-within .focus-overlay {
    opacity: 0;
    pointer-events: none;
}

.focus-overlay {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(26, 27, 38, 0.85);
    /* Slightly darker */
    backdrop-filter: blur(10px);
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 1rem;
    z-index: 15;
    /* Between main-input focus levels */
    color: var(--fg-color);
    font-size: 1.25rem;
    font-weight: 600;
    opacity: 1;
    transition: opacity 0.3s ease;
    pointer-events: none;
    /* Let clicks pass through to main-input */
}

.words-container {
    position: relative;
    font-size: 1.75rem;
    line-height: 44px;
    /* Matches JS lineHeight for perfect scrolling */
    font-weight: 400;
    display: flex;
    flex-wrap: wrap;
    gap: 0.75rem;
    user-select: none;
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    color: var(--subtle-color);
}

.word {
    display: inline-block;
    padding-bottom: 2px;
    border-bottom: 2px solid transparent;
    transition: border-color 0.2s ease;
}

.word.active {
    border-bottom-color: var(--primary-color);
    background: rgba(122, 162, 247, 0.05);
    border-radius: 4px;
}

.char {
    display: inline-block;
    position: relative;
    transition: color 0.1s ease;
}

.char.correct {
    color: var(--correct-color);
}

.char.incorrect {
    color: var(--error-color);
    background: rgba(247, 118, 142, 0.1);
    border-radius: 4px;
}

.char.extra {
    color: #db4b4b;
    opacity: 0.6;
}

/* Caret */
.caret {
    position: absolute;
    width: 3px;
    /* Thicker for better visibility */
    height: 1.8rem;
    background-color: var(--primary-color);
    border-radius: 2px;
    top: 2rem;
    left: 2rem;
    animation: blink 1s infinite;
    pointer-events: none;
    transition: left 0.08s ease-out, top 0.1s ease-out;
    /* Slightly faster */
    z-index: 10;
}

@keyframes blink {
    50% {
        opacity: 0;
    }
}

.visually-hidden {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    border: 0;
    opacity: 0;
    pointer-events: none;
}

/* --- Controls --- */
.controls {
    display: flex;
    justify-content: center;
    margin-top: 1rem;
}

.btn {
    padding: 0.75rem 2rem;
    border-radius: 12px;
    border: none;
    font-weight: 600;
    cursor: pointer;
    transition: var(--transition);
    font-size: 1rem;
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.btn.primary {
    background: var(--primary-color);
    color: var(--bg-color);
}

.btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 20px rgba(0, 0, 0, 0.2);
}

/* --- Side Panel --- */
.side-panel {
    display: flex;
    flex-direction: column;
}

.glass {
    background: var(--card-bg);
    backdrop-filter: blur(10px);
    border-radius: 20px;
    border: 1px solid var(--border-color);
    padding: 2rem;
}

.user-card h3 {
    margin-bottom: 1.5rem;
    color: var(--primary-color);
}

.level-progress {
    margin-bottom: 2rem;
}

.progress-bar {
    width: 100%;
    height: 10px;
    background: var(--border-color);
    border-radius: 5px;
    margin: 0.75rem 0;
    overflow: hidden;
}

.progress-fill {
    height: 100%;
    background: linear-gradient(90deg, var(--primary-color), var(--secondary-color), var(--accent-color));
    background-size: 200% 100%;
    transition: width 0.8s cubic-bezier(0.34, 1.56, 0.64, 1);
    animation: gradientShift 3s linear infinite;
}

@keyframes gradientShift {
    0% {
        background-position: 0% 50%;
    }

    100% {
        background-position: 200% 50%;
    }
}

.stats-row {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
}

.mini-stat {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.m-label {
    color: var(--subtle-color);
    font-size: 0.9rem;
}

.m-val {
    font-weight: 700;
}

/* --- Results Modal --- */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.8);
    backdrop-filter: blur(5px);
    z-index: 1000;
    justify-content: center;
    align-items: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.modal.active {
    display: flex;
    opacity: 1;
}

.modal-content {
    background: var(--bg-color);
    border: 1px solid var(--border-color);
    width: 95%;
    max-width: 900px;
    max-height: 90vh;
    padding: 2.5rem;
    border-radius: 24px;
    overflow-y: auto;
    position: relative;
    box-shadow: 0 30px 60px rgba(0, 0, 0, 0.5);
}

/* Modal Tabs */
.modal-tabs {
    display: flex;
    gap: 0.5rem;
    margin-bottom: 2rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 1rem;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.modal-tabs::-webkit-scrollbar {
    display: none;
}

.tab-btn {
    background: none;
    border: none;
    color: var(--subtle-color);
    padding: 0.5rem 1.5rem;
    cursor: pointer;
    font-size: 1rem;
    font-weight: 600;
    transition: var(--transition);
    border-radius: 8px;
}

.tab-btn:hover {
    color: var(--fg-color);
    background: rgba(255, 255, 255, 0.05);
}

.icon-btn:hover {
    color: var(--primary-color);
    background: rgba(122, 162, 247, 0.1);
}

#restart-btn:focus {
    outline: none;
    box-shadow: 0 0 0 4px rgba(122, 162, 247, 0.3);
    border: 1px solid var(--primary-color);
    transform: translateY(-2px);
    background: rgba(122, 162, 247, 0.2);
}

.tab-btn.active {
    color: var(--primary-color);
    background: rgba(122, 162, 247, 0.1);
}

.tab-content {
    display: none;
    animation: fadeIn 0.3s ease;
}

.tab-content.active {
    display: block;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

/* Interactive Word Breakdown */
.word-breakdown-sentence {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    padding: 2rem;
    background: var(--card-bg);
    border-radius: 16px;
    line-height: 2;
}

.word-chip {
    position: relative;
    padding: 0.2rem 0.6rem;
    border-radius: 8px;
    cursor: help;
    transition: var(--transition);
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid transparent;
}

.word-chip.correct {
    color: var(--correct-color);
    border-color: rgba(158, 206, 106, 0.1);
}

.word-chip.incorrect {
    color: var(--error-color);
    border-color: rgba(247, 118, 142, 0.1);
}

.word-chip:hover {
    background: rgba(122, 162, 247, 0.1);
    border-color: var(--primary-color);
    transform: translateY(-2px);
}

/* Tooltip */
.word-tooltip {
    position: absolute;
    bottom: 120%;
    left: 50%;
    transform: translateX(-50%) translateY(10px);
    background: #24283b;
    color: #fff;
    padding: 1rem;
    border-radius: 12px;
    font-size: 0.85rem;
    white-space: nowrap;
    z-index: 1000;
    opacity: 0;
    visibility: hidden;
    transition: all 0.2s ease;
    box-shadow: 0 10px 25px rgba(0, 0, 0, 0.3);
    border: 1px solid var(--border-color);
    pointer-events: none;
}

.word-chip:hover .word-tooltip {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%) translateY(0);
}

.tooltip-stat {
    display: flex;
    justify-content: space-between;
    gap: 1.5rem;
    margin-bottom: 0.25rem;
}

.tooltip-val {
    font-weight: 700;
    color: var(--primary-color);
}

.tab-subtitle {
    color: var(--subtle-color);
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

/* Analysis Cards */
.analysis-grid {
    display: grid;
    grid-template-columns: 1fr;
    gap: 1.5rem;
}

.analysis-card {
    background: var(--ad-bg);
    padding: 1.5rem;
    border-radius: 16px;
}

.analysis-card h3 {
    font-size: 1rem;
    margin-bottom: 1rem;
    color: var(--primary-color);
}

.mistake-list {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.mistake-item {
    display: flex;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--bg-color);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

.mistake-char {
    font-weight: 700;
    font-family: monospace;
    font-size: 1.2rem;
}

.mistake-expected {
    color: var(--correct-color);
}

.mistake-input {
    color: var(--error-color);
}

.results-grid {
    display: grid;
    grid-template-columns: repeat(5, 1fr);
    gap: 0.75rem;
    margin-bottom: 2.5rem;
}

.result-item {
    text-align: center;
}

.res-label {
    display: block;
    font-size: 0.8rem;
    color: var(--subtle-color);
    margin-bottom: 0.5rem;
}

.res-val {
    font-size: 2rem;
    font-weight: 700;
}

.res-val.highlight {
    color: var(--primary-color);
}

.xp-val {
    color: var(--accent-color);
}

.graph-container {
    width: 100%;
    height: 250px;
    margin: 2rem 0;
    background: var(--ad-bg);
    border-radius: 12px;
    padding: 1rem;
}

.leaderboard-section {
    margin-top: 2rem;
}

.leaderboard-section h3 {
    font-size: 1.1rem;
    margin-bottom: 1rem;
    color: var(--subtle-color);
}

#leaderboard-list {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.lb-entry {
    display: flex;
    justify-content: space-between;
    padding: 0.75rem 1rem;
    background: var(--card-bg);
    border-radius: 8px;
    border: 1px solid var(--border-color);
}

/* Toast System */
.toast-container {
    position: fixed;
    bottom: 2rem;
    right: 2rem;
    z-index: 10000;
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.toast {
    background: var(--bg-color);
    border: 1px solid var(--primary-color);
    padding: 1rem 2rem;
    border-radius: 12px;
    box-shadow: 0 10px 30px rgba(0, 0, 0, 0.4);
    display: flex;
    align-items: center;
    gap: 1rem;
    color: var(--fg-color);
    animation: slideIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

.toast.level-up {
    border-color: var(--accent-color);
    background: linear-gradient(135deg, var(--bg-color) 0%, rgba(255, 159, 107, 0.1) 100%);
}

.toast i {
    font-size: 1.5rem;
    color: var(--accent-color);
}

@keyframes slideIn {
    from {
        transform: translateX(120%);
        opacity: 0;
    }

    to {
        transform: translateX(0);
        opacity: 1;
    }
}

@keyframes fadeOut {
    to {
        transform: translateY(-20px);
        opacity: 0;
    }
}

.toast.fade-out {
    animation: fadeOut 0.5s forwards;
}

/* Pulse animation for focused restart */
@keyframes pulse {
    0% {
        box-shadow: 0 0 0 0 rgba(122, 162, 247, 0.4);
    }

    70% {
        box-shadow: 0 0 0 10px rgba(122, 162, 247, 0);
    }

    100% {
        box-shadow: 0 0 0 0 rgba(122, 162, 247, 0);
    }
}

#restart-btn:focus {
    animation: pulse 1.5s infinite;
}

.loading-screen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: var(--bg-color);
    z-index: 9999;
    display: flex;
    justify-content: center;
    align-items: center;
    transition: opacity 0.5s ease;
}

.loader {
    width: 50px;
    height: 50px;
    border: 4px solid var(--border-color);
    border-top: 4px solid var(--primary-color);
    border-radius: 50%;
    animation: spin 1s linear infinite;
}

@keyframes spin {
    to {
        transform: rotate(360deg);
    }
}

/* --- Mobile Responsiveness --- */
@media (max-width: 992px) {
    .game-layout {
        grid-template-columns: 1fr;
    }

    .side-panel {
        display: none;
        /* Hide side ads/progress on small layout */
    }
}

@media (max-width: 768px) {
    .container {
        padding: 0 1rem;
    }

    .stats-overview {
        grid-template-columns: 1fr;
        gap: 0.75rem;
    }

    .stat-card {
        padding: 1rem;
    }

    .stat-value {
        font-size: 1.75rem;
    }

    .words-container {
        font-size: 1.25rem;
    }

    .results-grid {
        grid-template-columns: repeat(2, 1fr);
    }
}

/* Footer */
.footer {
    padding: 3rem 0;
    text-align: center;
    color: var(--subtle-color);
    font-size: 0.9rem;
}