:root {
    --paper: #F5F6F2;
    --ink: #00273A;
    --ink-untyped: rgba(0, 39, 58, .32);
    --ink-typed: #00273A;
    --ink-soft: #5B645F;
    --indigo: #00689B;
    --course-button: #00689B;
    --line: #DCDFD6;
    --card: #FFFFFF;
    --error: #E11D48;
    --error-bg: rgba(225, 29, 72, .13);
    --gold: #F59E0B;
    --shadow: 0 12px 32px rgba(0, 39, 58, .08);
}

body.dark {
    --paper: #0c0d0f;
    --ink: #E8EAE4;
    --ink-untyped: #777F88;
    --ink-typed: #FFFFFF;
    --ink-soft: #A2AAA5;
    --indigo: #0088C7;
    --course-button: #0088C7;
    --line: #2B3036;
    --card: #131518;
    --error: #FB7185;
    --error-bg: rgba(251, 113, 133, .17);
    --shadow: 0 12px 32px rgba(0, 0, 0, .25);
}

/* Base */

* {
    box-sizing: border-box;
}

[hidden] {
    display: none !important;
}

html {
    min-height: 100%;
    -webkit-text-size-adjust: 100%;
    scroll-behavior: smooth;
}

body {
    margin: 0;
    min-height: 100vh;
    min-height: 100dvh;
    display: flex;
    flex-direction: column;
    background: var(--paper);
    color: var(--ink);
    font-family: "Tajawal", sans-serif;
    transition: background .2s, color .2s;
    overflow-x: hidden;
}

button {
    font-family: inherit;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

button:focus-visible {
    outline: 2px solid var(--indigo);
    outline-offset: 3px;
}

button svg,
button i,
a svg,
a i {
    width: 17px;
    height: 17px;
    flex-shrink: 0;
}

/* Progress */

.top-progress-line {
    position: fixed;
    inset: 0 0 auto;
    height: 3px;
    z-index: 100;
    background: transparent;
}

.top-progress-fill {
    width: 0%;
    height: 100%;
    background: linear-gradient(90deg, var(--indigo), #38BDF8);
    transition: width .2s ease;
}

/* Header */

.top-header {
    position: relative;
    z-index: 10;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 20px;
    padding: 14px clamp(16px, 3vw, 40px);
    background: var(--card);
    border-bottom: 1px solid var(--line);
}

.brand {
    display: flex;
    align-items: center;
    gap: 10px;
    min-width: 0;
}

.brand-logo {
    width: 40px;
    height: 40px;
    object-fit: contain;
    flex-shrink: 0;
}

.brand-text {
    display: flex;
    flex-direction: column;
    gap: 2px;
}

.brand-text strong {
    font-size: 15px;
    font-weight: 900;
    line-height: 1.4;
}

.brand-text span {
    color: var(--ink-soft);
    font-size: 11px;
}

.header-actions {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 8px;
    flex-wrap: wrap;
}

/* Buttons */

.pill-btn,
.theme-btn,
.primary-btn,
.ghost-action-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    min-height: 42px;
    padding: 0 13px;
    border: 1px solid var(--line);
    border-radius: 11px;
    background: var(--card);
    color: var(--ink);
    font-size: 13px;
    font-weight: 800;
    cursor: pointer;
    text-decoration: none;
    transition: background .18s, border-color .18s, color .18s, transform .18s;
}

.pill-btn:hover,
.theme-btn:hover {
    border-color: var(--indigo);
    color: var(--indigo);
}

.pill-btn.active-btn {
    background: rgba(0, 104, 155, .09);
    color: var(--indigo);
    border-color: var(--indigo);
}

.pill-btn.active-pro {
    background: linear-gradient(135deg, #00689B, #0088C7);
    color: #FFFFFF;
    border-color: transparent;
}

.theme-btn {
    width: 42px;
    padding: 0;
    flex-shrink: 0;
}

.primary-btn {
    min-height: 44px;
    padding: 10px 18px;
    border-color: transparent;
    background: var(--course-button);
    color: #FFFFFF;
    box-shadow: 0 4px 12px rgba(0, 104, 155, .15);
}

.primary-btn:hover {
    filter: brightness(1.08);
    transform: translateY(-1px);
}

.ghost-action-btn {
    min-height: 40px;
    background: transparent;
    color: var(--ink-soft);
    font-size: 12px;
}

.ghost-action-btn:hover {
    border-color: var(--indigo);
    color: var(--indigo);
    background: rgba(0, 104, 155, .06);
}

/* Typing stage */

.zen-stage {
    flex: 1;
    width: 100%;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    padding: 36px 24px 44px;
    min-height: 560px;
    min-height: max(560px, calc(100dvh - 145px));
    cursor: text;
}

.zen-top-meta {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex-wrap: wrap;
    gap: 9px;
    max-width: 100%;
    margin-bottom: clamp(28px, 5vh, 48px);
    padding: 8px 15px;
    border: 1px solid var(--line);
    border-radius: 999px;
    background: var(--card);
    color: var(--ink-soft);
    font-size: 13px;
    font-weight: 800;
    cursor: pointer;
}

.zen-top-meta:hover {
    border-color: var(--indigo);
}

.level-dot {
    padding: 3px 9px;
    border-radius: 999px;
    color: #FFFFFF;
    font: 800 11px "Quicksand", sans-serif;
}

.level-A1 { background: #2196F3; }
.level-A2 { background: #00A896; }
.level-B1 { background: #E58A00; }
.level-B2 { background: #7C3AED; }
.level-C1 { background: #E53935; }

.zen-sentence-wrapper {
    position: relative;
    width: 100%;
    max-width: 1100px;
    margin: 0 auto;
    padding: 8px 4px;
    text-align: center;
    transition: opacity .2s, transform .2s;
}

.zen-sentence-wrapper.sentence-transition {
    opacity: 0;
    transform: translateY(-5px);
}

.zen-sentence-wrapper.shake {
    animation: gentleShake .25s ease;
}

.giant-english-sentence {
    position: relative;
    display: inline-block;
    max-width: 100%;
    direction: ltr;
    text-align: center;
    font-family: "Inter", "Plus Jakarta Sans", sans-serif;
    font-size: clamp(30px, 4.5vw, 58px);
    font-weight: 700;
    line-height: 1.55;
    letter-spacing: -.025em;
    overflow-wrap: anywhere;
    user-select: none;
}

.story-word {
    position: relative;
    display: inline-block;
    margin: 0 1px;
    padding: 1px 2px;
    border-radius: 6px;
    cursor: pointer;
}

.story-word:hover {
    background: rgba(128, 128, 128, .1);
}

.char {
    position: relative;
    display: inline-block;
    border-radius: 3px;
    color: var(--ink-untyped);
    transition: color .1s;
}

.char.space-char {
    min-width: .34em;
}

.char.correct {
    color: var(--ink-typed);
}

.char.current {
    color: var(--ink-untyped);
}

.char.incorrect {
    color: var(--error) !important;
    background: var(--error-bg);
}

.gliding-caret {
    position: absolute;
    height: 3px;
    border-radius: 999px;
    background: var(--indigo);
    pointer-events: none;
    box-shadow: 0 2px 8px rgba(0, 136, 199, .3);
    transition: left .1s, top .1s, width .1s;
}

/* Professional mode */

.pro-mode-active .char.untyped,
.pro-mode-active .char.current {
    color: transparent !important;
}

.pro-mode-active .char.untyped:not(.space-char)::after,
.pro-mode-active .char.current:not(.space-char)::after {
    content: "•";
    position: absolute;
    inset: 0;
    display: grid;
    place-items: center;
    color: var(--ink-untyped);
    font-size: .65em;
}

/* Translation and controls */

.zen-divider {
    width: 76px;
    height: 2px;
    margin: 30px auto 20px;
    border-radius: 5px;
    background: var(--line);
}

.zen-arabic-translation {
    width: 100%;
    max-width: 740px;
    margin: 0 auto;
    color: var(--ink-soft);
    text-align: center;
    font-size: clamp(18px, 2.1vw, 24px);
    font-weight: 500;
    line-height: 1.8;
    overflow-wrap: anywhere;
}

.zen-quick-actions {
    display: flex;
    justify-content: center;
    align-items: center;
    flex-wrap: wrap;
    gap: 9px;
    margin-top: 28px;
}

.zen-quick-actions strong {
    color: var(--ink);
}

/* Virtual keyboard */

.keyboard-wrapper {
    width: 100%;
    max-width: 800px;
    margin: 26px auto 0;
    padding: 14px;
    border: 1px solid var(--line);
    border-radius: 18px;
    background: var(--card);
    box-shadow: var(--shadow);
    direction: ltr;
    overflow-x: auto;
}

.kb-rows {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.kb-row {
    display: flex;
    justify-content: center;
    gap: 5px;
    width: 100%;
}

.kb-key {
    display: flex;
    align-items: center;
    justify-content: center;
    min-width: 0;
    min-height: 38px;
    flex: 1 1 0;
    padding: 5px;
    border: 1px solid var(--line);
    border-radius: 7px;
    background: var(--paper);
    color: var(--ink-soft);
    font: 700 12px "JetBrains Mono", monospace;
}

.kb-key.kb-wide {
    flex: 1.8 1 0;
}

.kb-key.kb-space {
    flex: 5 1 0;
}

.kb-key.active-target {
    background: var(--course-button);
    color: #FFFFFF;
    border-color: var(--course-button);
}

/* Footer and statistics */

.bottom-bar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-wrap: wrap;
    gap: 14px 24px;
    padding: 15px clamp(16px, 3vw, 40px);
    border-top: 1px solid var(--line);
    background: var(--card);
    color: var(--ink-soft);
    font-size: 13px;
    font-weight: 700;
}

.telemetry-group {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px 22px;
}

.telemetry-item {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
}

.telemetry-item strong {
    color: var(--ink);
    font-size: 15px;
    font-weight: 900;
    font-variant-numeric: tabular-nums;
}

.hint-shortcuts {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 12px;
    font-size: 12px;
}

.kbd-tag {
    padding: 3px 7px;
    border: 1px solid var(--line);
    border-radius: 6px;
    background: var(--paper);
    color: var(--ink);
    font: 700 11px "JetBrains Mono", monospace;
}

/* Word translation popup */

.word-popover {
    position: fixed;
    z-index: 45;
    display: inline-flex;
    align-items: center;
    gap: 12px;
    max-width: calc(100vw - 24px);
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 13px;
    background: var(--card);
    color: var(--ink);
    box-shadow: var(--shadow);
    overflow-wrap: anywhere;
}

.popover-translation {
    font-size: 19px;
    font-weight: 800;
    line-height: 1.5;
}

.popover-close {
    min-width: 30px;
    min-height: 30px;
    padding: 0;
    border: 0;
    background: transparent;
    color: var(--ink-soft);
    font-size: 23px;
    cursor: pointer;
}

/* Modals */

.modal-backdrop {
    position: fixed;
    inset: 0;
    z-index: 50;
    display: grid;
    place-items: center;
    padding: 16px;
    background: rgba(0, 12, 20, .7);
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    overflow-y: auto;
}

#locked-modal {
    z-index: 60;
}

.stories-modal-card,
.rating-modal-card,
.locked-modal-card {
    width: 100%;
    max-height: 88vh;
    max-height: 88dvh;
    overflow-y: auto;
    overscroll-behavior: contain;
    border: 1px solid var(--line);
    border-radius: 20px;
    background: var(--card);
    box-shadow: var(--shadow);
}

.stories-modal-card {
    max-width: 960px;
    padding: clamp(16px, 3vw, 28px);
}

.stories-modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    margin-bottom: 20px;
    padding-bottom: 15px;
    border-bottom: 1px solid var(--line);
}

.stories-modal-header h2 {
    font-size: clamp(19px, 3vw, 24px) !important;
    line-height: 1.5;
}

.stories-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 16px;
}

.story-card {
    display: flex;
    flex-direction: column;
    min-width: 0;
    overflow: hidden;
    border: 1px solid var(--line);
    border-radius: 16px;
    background: var(--paper);
    cursor: pointer;
    transition: border-color .2s, transform .2s, opacity .2s;
}

.story-card:hover {
    border-color: var(--indigo);
    transform: translateY(-2px);
}

.story-card.selected-story {
    border: 2px solid var(--course-button);
}

/* Locked Story Card Styling */
.story-card.locked-story {
    opacity: 0.82;
}

.story-card.locked-story .story-cover {
    background: rgba(128, 128, 128, 0.08);
}

.cover-lock-badge {
    position: absolute;
    top: 10px;
    right: 10px;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 4px 9px;
    border-radius: 999px;
    background: rgba(0, 12, 20, 0.72);
    color: #FFFFFF;
    font-size: 11px;
    font-weight: 800;
}

.cover-lock-badge svg,
.cover-lock-badge i {
    width: 13px;
    height: 13px;
}

.story-status-locked {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    color: var(--ink-soft);
    font-weight: 800;
    font-size: 13px;
}

.story-status-locked svg,
.story-status-locked i {
    width: 14px;
    height: 14px;
}

.story-cover {
    position: relative;
    display: grid;
    place-items: center;
    min-height: 95px;
    border-bottom: 1px solid var(--line);
    background: rgba(0, 104, 155, .08);
}

.cover-emoji {
    font-size: 38px;
}

.cover-level {
    position: absolute;
    top: 10px;
    left: 10px;
    padding: 4px 9px;
    border-radius: 999px;
    color: #FFFFFF;
    font: 800 11px "Quicksand", sans-serif;
}

.story-card-body {
    display: flex;
    flex: 1;
    flex-direction: column;
    padding: 16px;
}

.story-title {
    margin: 0;
    color: var(--ink);
    font: 700 17px/1.5 "Quicksand", sans-serif;
    direction: ltr;
    text-align: right;
    overflow-wrap: anywhere;
}

.story-arabic-title {
    margin: 5px 0 8px;
    color: var(--indigo);
    font-size: 15px;
    font-weight: 800;
}

.story-description {
    margin: 0 0 12px;
    color: var(--ink-soft);
    font-size: 13px;
    line-height: 1.8;
}

/* Completion modal */

.rating-modal-card {
    max-width: 490px;
    padding: clamp(18px, 4vw, 28px);
    text-align: center;
}

.rating-modal-card h3 {
    font-size: clamp(22px, 5vw, 26px) !important;
}

.stars-row {
    display: flex;
    justify-content: center;
    gap: 8px;
    margin: 16px 0;
    direction: ltr;
}

.star-item {
    color: var(--line);
    font-size: clamp(28px, 7vw, 38px);
    line-height: 1;
}

.star-item.earned {
    color: var(--gold);
    filter: drop-shadow(0 3px 7px rgba(245, 158, 11, .3));
    animation: starPop .4s ease both;
}

.modal-metrics-grid {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
    margin: 16px 0 22px;
}

.modal-metric-box {
    min-width: 0;
    padding: 12px 8px;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: var(--paper);
    overflow-wrap: anywhere;
}

.modal-metric-box span {
    display: block;
    color: var(--ink-soft);
    font-size: 12px;
    font-weight: 700;
}

.modal-metric-box strong {
    display: block;
    margin-top: 5px;
    color: var(--indigo);
    font-size: 17px;
    font-weight: 900;
}

/* Locked Topic Popup Modal */

.locked-modal-card {
    position: relative;
    max-width: 430px;
    padding: clamp(24px, 4vw, 32px);
    text-align: center;
}

.locked-close-btn {
    position: absolute;
    top: 14px;
    left: 14px;
    width: 36px;
    height: 36px;
    min-height: 36px;
}

.locked-icon-circle {
    width: 64px;
    height: 64px;
    margin: 6px auto 18px;
    border-radius: 50%;
    background: rgba(0, 104, 155, 0.12);
    color: var(--indigo);
    display: grid;
    place-items: center;
}

.locked-icon-circle svg,
.locked-icon-circle i {
    width: 28px;
    height: 28px;
}

.locked-message {
    margin: 0 0 24px;
    color: var(--ink);
    font-size: 18px;
    font-weight: 800;
    line-height: 1.7;
}

.purchase-btn {
    width: 100%;
    min-height: 48px;
    font-size: 15px;
    font-weight: 900;
    border-radius: 13px;
}

/* Animations */

@keyframes starPop {
    0% { transform: scale(.5); }
    70% { transform: scale(1.15); }
    100% { transform: scale(1); }
}

@keyframes gentleShake {
    0%, 100% { transform: translateX(0); }
    25% { transform: translateX(-4px); }
    75% { transform: translateX(4px); }
}

/* Tablet */

@media (max-width: 900px) {
    .top-header {
        gap: 12px;
        padding-inline: 18px;
    }

    .header-actions {
        gap: 6px;
    }

    .pill-btn {
        padding-inline: 10px;
        font-size: 12px;
    }

    .brand-logo {
        width: 35px;
        height: 35px;
    }

    .zen-stage {
        padding-inline: 20px;
    }

    .giant-english-sentence {
        font-size: clamp(30px, 5vw, 46px);
    }

    .bottom-bar {
        justify-content: center;
    }
}

/* Phones */

@media (max-width: 600px) {
    .top-header {
        flex-wrap: wrap;
        padding: 12px 14px;
        gap: 12px;
    }

    .brand {
        flex: 1;
    }

    .brand-logo {
        width: 34px;
        height: 34px;
    }

    .header-actions {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(3, minmax(0, 1fr));
        gap: 7px;
    }

    .header-actions .pill-btn {
        min-width: 0;
        min-height: 42px;
        padding: 7px 5px;
        gap: 5px;
        font-size: 11px;
        white-space: normal;
        line-height: 1.3;
    }

    .header-actions .pill-btn span {
        min-width: 0;
    }

    .header-actions .theme-btn {
        position: absolute;
        top: 12px;
        width: 36px;
        height: 36px;
        min-height: 36px;
    }

    #toggle-sfx-btn {
        left: 54px;
    }

    #theme-toggle-btn {
        left: 12px;
    }

    .brand {
        padding-left: 82px;
    }

    .zen-stage {
        min-height: auto;
        padding: 30px 14px 34px;
    }

    .zen-top-meta {
        margin-bottom: 28px;
        font-size: 12px;
    }

    .giant-english-sentence {
        font-size: clamp(27px, 7vw, 40px);
        line-height: 1.65;
        letter-spacing: -.02em;
    }

    .zen-divider {
        margin: 24px auto 16px;
    }

    .zen-arabic-translation {
        font-size: clamp(17px, 4.5vw, 21px);
        line-height: 1.9;
    }

    .zen-quick-actions {
        gap: 7px;
        margin-top: 23px;
    }

    .ghost-action-btn {
        min-height: 42px;
        padding: 8px 10px;
        font-size: 12px;
    }

    .keyboard-wrapper {
        padding: 8px;
        border-radius: 12px;
    }

    .kb-row {
        gap: 3px;
    }

    .kb-key {
        min-height: 34px;
        padding: 4px 2px;
        font-size: 10px;
    }

    .bottom-bar {
        padding: 14px 12px;
    }

    .telemetry-group {
        justify-content: center;
        gap: 12px 18px;
    }

    .hint-shortcuts {
        display: none;
    }

    .stories-grid {
        grid-template-columns: 1fr;
        gap: 10px;
    }

    .story-card {
        display: grid;
        grid-template-columns: 76px minmax(0, 1fr);
    }

    .story-cover {
        min-height: 100%;
        border-bottom: 0;
        border-left: 1px solid var(--line);
    }

    .cover-emoji {
        font-size: 30px;
    }

    .cover-level {
        top: 5px;
        left: 4px;
        padding: 3px 5px;
        font-size: 9px;
    }

    .cover-lock-badge {
        top: auto;
        bottom: 5px;
        right: 4px;
        padding: 2px 5px;
        font-size: 9px;
    }

    .story-card-body {
        padding: 11px;
    }

    .story-title {
        font-size: 15px;
    }

    .story-description {
        font-size: 12px;
    }

    .stories-modal-card {
        padding: 15px;
        border-radius: 16px;
    }

    .modal-backdrop {
        padding: 12px;
    }

    .rating-modal-card {
        padding: 18px 14px;
    }

    .modal-metrics-grid {
        gap: 8px;
    }

    .rating-modal-card > div:last-child {
        gap: 8px !important;
    }

    .rating-modal-card > div:last-child .pill-btn,
    .rating-modal-card > div:last-child .primary-btn {
        min-width: 0;
        padding-inline: 8px;
        font-size: 12px;
    }
}

/* Small phones */

@media (max-width: 360px) {
    .top-header {
        padding-inline: 10px;
    }

    .brand {
        padding-left: 76px;
    }

    .header-actions {
        gap: 5px;
    }

    .header-actions .pill-btn {
        font-size: 10px;
    }

    #toggle-sfx-btn {
        left: 48px;
    }

    #theme-toggle-btn {
        left: 10px;
    }

    .giant-english-sentence {
        font-size: 26px;
    }

    .ghost-action-btn {
        padding-inline: 8px;
        font-size: 11px;
    }

    .story-card {
        grid-template-columns: 64px minmax(0, 1fr);
    }

    .telemetry-group {
        gap: 10px 13px;
    }
}

/* Short landscape screens */

@media (max-height: 500px) and (orientation: landscape) {
    .zen-stage {
        min-height: auto;
        padding-block: 20px;
    }

    .zen-top-meta {
        margin-bottom: 16px;
    }

    .zen-divider {
        margin-block: 14px 10px;
    }

    .zen-quick-actions {
        margin-top: 15px;
    }
}

/* Accessibility */

@media (prefers-reduced-motion: reduce) {
    *,
    *::before,
    *::after {
        animation-duration: .01ms !important;
        animation-iteration-count: 1 !important;
        transition-duration: .01ms !important;
        scroll-behavior: auto !important;
    }
}