/* --- GLOBAL RESET & VARIABLES --- */
:root {
    --bg: #0b1020;
    --card: #12192d;
    --muted: #a0aec0;
    --text: #e7ecff;
    --ok: #36c689;
    --bad: #ff6470;
    --der: #4b8bff;
    --die: #ff6cae;
    --das: #ffd84b;
    --border: #24335c;
    --accent: #a3a9ff;

    --col-w: 520px;
    --stage-w: 380px;

    /* slot Y positions (overridden by JS for perfect clipping safety) */
    --y-next: 20px;
    --y-active: 170px;
    --y-prev: 330px;
    --y-exit: 520px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body{
    margin:0;
    font-family: system-ui, -apple-system, sans-serif;
    background: radial-gradient(circle at top, #161d33 0%, #0b1020 100%);
    color: var(--text);

    min-height: 100svh;
    height: 100dvh;

    width: 100vw;
    display: flex;
    flex-direction: column;

    overflow-x: hidden;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
}

/* --- HEADER --- */
header{
    width:100%;
    padding:8px 15px 10px;
    padding-top:max(8px, env(safe-area-inset-top));
    display:flex;
    justify-content:space-between;
    align-items:center;
    background: rgba(11,16,32,0.95);
    border-bottom: 1px solid var(--border);
    z-index:10;
}

.header-row{
    width:100%;
    display:flex;
    justify-content:space-between;
    align-items:center;
    gap:12px;
}

.controls-top{
    display:flex;
    align-items:center;
    gap:10px;
}

.credits-top{
    font-size:11px;
    color: rgba(231,236,255,0.65);
    display:flex;
    align-items:center;
    gap:8px;
    white-space:nowrap;
    line-height:1;
}
.credits-top a{
    color: rgba(163,169,255,0.9);
    text-decoration:none;
}
.credits-top a:hover{ text-decoration:underline; }

.pill{
    background: transparent;
    border: 1px solid var(--border);
    color: var(--text);
    padding: 6px 12px;
    border-radius: 8px;
    font-size: 12px;
    cursor: pointer;
    display:inline-flex;
    align-items:center;
    gap:6px;
}
.pill.active{
    background: rgba(163,169,255,0.18);
    border-color: rgba(163,169,255,0.75);
    color: var(--text);
}

.review-chip{
    border-width: 2px;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    padding: 6px 12px;
    border-color: var(--review-color, rgba(54,198,137,0.8));
    color: var(--review-color, var(--text));
    background: var(--review-bg, rgba(0,0,0,0));
}
.review-chip .pill-count{
    color: inherit;
}
.review-chip.review-hot{
    animation: review-pulse 1.2s ease-in-out infinite;
}
.review-chip.review-hot.active{
    animation: none;
}
@keyframes review-pulse{
    0%, 100% { box-shadow: 0 0 0 rgba(255,100,112,0); opacity: 1; }
    50% { box-shadow: 0 0 18px rgba(255,100,112,0.35); opacity: 0.7; }
}

/* --- LEVEL CHIPS --- */
.level-chips{ display:inline-flex; gap:6px; align-items:center; flex-wrap:nowrap; }
.level-chips.is-disabled{ opacity: 0.55; }
.level-chips.is-disabled .chip{ pointer-events: none; }
.level-chips.is-disabled .lang-toggle{ pointer-events: auto; opacity: 1; }
.pill-count{
    color: #ff5f5f;
    font-size: 12px;
    font-weight: 500;
    min-width: 2ch;
    text-align: right;
}
.chip{
    background: rgba(26,35,64,0.55);
    border: 1px solid rgba(36,51,92,0.95);
    color: rgba(231,236,255,0.85);
    padding:6px 10px;
    border-radius:999px;
    font-size:12px;
    font-weight:800;
    letter-spacing:0.02em;
    cursor:pointer;
    user-select:none;
    -webkit-tap-highlight-color: transparent;
    transition: transform 120ms ease, background 180ms ease, border-color 180ms ease, opacity 180ms ease;
}
.chip:active{ transform: scale(0.98); }
.chip:hover{ border-color: rgba(163,169,255,0.55); }
.chip.active{
    background: rgba(163,169,255,0.18);
    border-color: rgba(163,169,255,0.75);
    color: var(--text);
}

/* --- MAIN --- */
main{
    flex:1;
    width:100%;
    max-width:600px;
    margin:0 auto;
    padding:6px;
    position:relative;

    display:flex;
    flex-direction:column;
    gap:6px;

    padding-bottom: max(14px, env(safe-area-inset-bottom));
}

/* --- HUD --- */
.game-hud{
    display:flex;
    justify-content:space-between;
    align-items:center;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0 12px;
    gap: 8px;
    height: 50px;
    width: min(520px, 96vw);
    margin-left: auto;
    margin-right: auto;
    overflow: hidden;
}

.stat-item{ text-align:center; min-width:0; flex:1; }
.stat-item .label{
    display:block;
    font-size:9px;
    color: var(--muted);
    text-transform: uppercase;
    letter-spacing: 0.5px;
}
.stat-item .value{ font-size:18px; font-weight:800; white-space: nowrap; }
.words-trigger{
    background: transparent;
    border: none;
    padding: 0;
    cursor: pointer;
}
.words-trigger:active{ transform: translateY(1px); }

/* --- SEARCH --- */
.search-panel{
    position: relative;
    width: min(520px, 96vw);
    margin-left: auto;
    margin-right: auto;
    z-index: 100;
    margin-bottom: -2px;
}
.search-input{
    width: 100%;
    background: rgba(18,25,45,0.65);
    border: 1px solid var(--border);
    color: var(--text);
    padding: 10px 12px;
    border-radius: 10px;
    font-size: 14px;
    outline: none;
}
.search-input::placeholder{ color: rgba(160,174,192,0.8); }
.search-results{
    position: absolute;
    top: calc(100% + 6px);
    left: 0;
    right: 0;
    background: rgba(18,25,45,0.98);
    border: 1px solid var(--border);
    border-radius: 10px;
    max-height: 260px;
    overflow-y: auto;
    z-index: 110;
    display: none;
}
.search-results.is-visible{ display: block; }
.search-item{
    width: 100%;
    text-align: left;
    background: transparent;
    border: none;
    color: var(--text);
    padding: 10px 12px;
    cursor: pointer;
    font-size: 13px;
}
.search-item:hover{ background: rgba(163,169,255,0.12); }
.search-item .hint{ color: var(--muted); font-size: 11px; margin-left: 8px; }

/* --- NOTICE --- */
.notice{
    min-height: 0;
    text-align: center;
    font-size: 12px;
    color: var(--muted);
    opacity: 0;
    transform: translateY(-2px);
    transition: opacity 160ms ease, transform 160ms ease;
}
.notice.is-visible{
    opacity: 1;
    transform: translateY(0);
    min-height: 18px;
}

/* --- WORD LIST MODAL --- */
.modal{
    position: fixed;
    inset: 0;
    background: rgba(8,12,24,0.78);
    display: none;
    align-items: center;
    justify-content: center;
    padding: 14px;
    z-index: 30;
}
.modal.is-visible{ display: flex; }
.modal-card{
    width: min(540px, 96vw);
    max-height: min(78vh, 520px);
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 14px;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.modal-header{
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 10px 12px;
    border-bottom: 1px solid var(--border);
}
.modal-title{
    margin: 0;
    font-size: 14px;
    letter-spacing: 0.04em;
    text-transform: uppercase;
    color: var(--muted);
}
.modal-close{
    border: none;
    background: transparent;
    color: var(--text);
    font-size: 18px;
    cursor: pointer;
}
.modal-list{
    padding: 10px 12px;
    overflow-y: auto;
    display: grid;
    gap: 8px;
}
.modal-body{
    padding: 14px 12px;
    display: grid;
    gap: 6px;
}
.modal-footer{
    padding: 10px 12px 14px;
    display: flex;
    justify-content: flex-end;
}
.modal-info{
    width: min(420px, 92vw);
    max-height: 70vh;
}
.modal-mastered{
    width: min(420px, 92vw);
    max-height: 60vh;
}
.mastered-title{
    font-size: 16px;
    font-weight: 800;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    color: var(--accent);
}
.mastered-text{
    font-size: 14px;
    color: var(--text);
}
.info-word{
    font-size: 18px;
    font-weight: 800;
}
.info-plural{
    font-size: 14px;
    color: var(--muted);
}
.modal-row{
    display: flex;
    flex-direction: column;
    gap: 4px;
    padding: 8px 10px;
    border-radius: 10px;
    background: rgba(26,35,64,0.5);
    border: 1px solid rgba(36,51,92,0.6);
}
.modal-word{
    font-size: 16px;
    font-weight: 800;
}
.modal-article{
    margin-right: 8px;
    color: var(--accent);
}
.modal-meta{
    display: flex;
    gap: 10px;
    font-size: 12px;
    color: var(--muted);
}
.modal-translation{ color: rgba(231,236,255,0.72); }
.modal-empty{
    color: var(--muted);
    text-align: center;
    padding: 16px 0;
}

/* --- STAGE --- */
.stage-wrapper{ display:flex; align-items:center; justify-content:center; }

.stage{
    position:relative;
    width: min(520px, 96vw);
    height: 480px;
    background: #080c18;
    border-radius: 24px;
    border: 3px solid var(--border);
    box-shadow: inset 0 0 60px rgba(0,0,0,0.9);
    overflow:hidden;
    z-index:1;
}

.stage::before, .stage::after{
    content:"";
    position:absolute;
    left:0; right:0;
    height: 70px;
    z-index:10;
    pointer-events:none;
}
.stage::before{ top:0; background: linear-gradient(to bottom, #080c18 0%, transparent 100%); }
.stage::after{ bottom:0; background: linear-gradient(to top, #080c18 0%, transparent 100%); }

/* language button */
.lang-toggle{
    width:34px; height:34px;
    border-radius:10px;
    border: 1px solid rgba(36,51,92,0.9);
    background: rgba(18,25,45,0.35);
    color: var(--text);
    font-family: "Segoe UI Emoji","Segoe UI Symbol","Apple Color Emoji","Noto Color Emoji",system-ui,-apple-system,sans-serif;
    display:inline-flex;
    align-items:center;
    justify-content:center;
    font-size:0;
    cursor:pointer;
    user-select:none;
    -webkit-tap-highlight-color: transparent;
    transition: opacity 220ms ease, transform 120ms ease, background 220ms ease, border-color 220ms ease;
    line-height:1;
}
.level-chips .lang-toggle{
    margin-right: 2px;
}
.lang-toggle::before{
    content:"";
    width:20px;
    height:14px;
    border-radius:2px;
    background-size: cover;
    background-position: center;
}
.lang-toggle.lang-de::before{
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 36 24'><rect width='36' height='8' fill='%23000'/><rect y='8' width='36' height='8' fill='%23dd0000'/><rect y='16' width='36' height='8' fill='%23ffce00'/></svg>");
}
.lang-toggle.lang-en::before{
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 36 24'><rect width='36' height='24' fill='%2300247d'/><path d='M0 0 L36 24 M36 0 L0 24' stroke='%23fff' stroke-width='6'/><path d='M0 0 L36 24 M36 0 L0 24' stroke='%23cf142b' stroke-width='3'/><rect x='14' width='8' height='24' fill='%23fff'/><rect y='8' width='36' height='8' fill='%23fff'/><rect x='16' width='4' height='24' fill='%23cf142b'/><rect y='10' width='36' height='4' fill='%23cf142b'/></svg>");
}
.lang-toggle.lang-uk::before{
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 36 24'><rect width='36' height='12' fill='%230055bb'/><rect y='12' width='36' height='12' fill='%23ffd500'/></svg>");
}
.lang-toggle:active{ transform: scale(0.96); }
.lang-toggle.is-dim{ opacity: 0.18; }
.lang-toggle:hover{
    background: rgba(18,25,45,0.55);
    border-color: rgba(163,169,255,0.55);
}

/* --- CONTROLS --- */
.controls-area{
    display:flex;
    gap:8px;
    width:100%;
    height: 90px;
    align-items: stretch;
}

.btn{
    flex:1;
    border-radius:14px;
    border:none;
    font-size:20px;
    font-weight:800;
    color:#fff;
    cursor:pointer;
    box-shadow: 0 4px 0 rgba(0,0,0,0.3);
    transition: transform 0.1s;
    touch-action: manipulation;
}
.btn:active{ transform: translateY(4px); box-shadow:none; }
.btn.der{ background: var(--der); }
.btn.die{ background: var(--die); }
.btn.das{ background: var(--das); }

/* --- SLOT --- */
.reel-container{ position:relative; width:100%; height:100%; }

.slot-unit{
    position:absolute;
    left:0; right:0;
    height:140px;
    display:flex;
    align-items:center;
    justify-content:center;
    transition: all 0.6s cubic-bezier(0.25,1,0.5,1);
    font-weight:800;
    padding: 0 18px;
    text-align:center;
    --word-scale: 1;
}

/* INNER LAYOUT: center correctly (word-only vs article+word) */
.slot-content{
    display:flex;
    flex-direction:column;
    align-items:center;
    justify-content:center;
    width:100%;
}

.caption-row{
    display:flex;
    align-items:baseline;
    justify-content:center;
    width:100%;
    flex-wrap: nowrap;
}

/* word + article typography (bigger, still clamps) */
.word-span, .article-reveal{
    display:inline-block;
    white-space:nowrap;
    line-height:1.05;
}

/* bigger max for active */
.slot-unit.pos-active .word-span,
.slot-unit.pos-active .article-reveal{
    font-size: calc(clamp(22px, 6.4vw, 48px) * var(--word-scale));
}

/* previous readable */
.slot-unit.pos-prev .word-span,
.slot-unit.pos-prev .article-reveal{
    font-size: calc(clamp(16px, 3.8vw, 28px) * var(--word-scale));
}

/* next */
.slot-unit.pos-next .word-span,
.slot-unit.pos-next .article-reveal{
    font-size: calc(clamp(14px, 3.2vw, 22px) * var(--word-scale));
}

/* article should NOT affect centering until revealed */
.article-reveal{
    opacity:0;
    width: 0;
    text-align: right;
    transition: opacity 220ms ease;
    margin-right: 0;
}

/* when resolved, article appears and adds spacing */
.resolved .article-reveal{
    opacity:1;
    width: 3.6ch;
    margin-right: 10px;
}

/* translation: not caps, more spacing, aligned with caption */
.sub-translation{
    margin-top: 12px;              /* more space from the word */
    font-weight: 650;
    letter-spacing: 0.02em;        /* no “shouting” */
    text-transform: none;          /* NO CAPS */
    color: rgba(231,236,255,0.68);
    font-size: 13px;
    line-height: 1.2;
    pointer-events:none;
}

/* redo attempt history */
.attempt-bar{
    display:flex;
    align-items:center;
    justify-content:center;
    gap:6px;
    margin-bottom: 10px;
    opacity: 1;
    transform: translateY(0);
}
.attempt-seg{
    width: 22px;
    height: 6px;
    border-radius: 999px;
    background: rgba(160,174,192,0.25);
    border: 1px solid rgba(36,51,92,0.7);
}
.attempt-seg.is-correct{
    background: var(--ok);
    border-color: rgba(54,198,137,0.8);
}
.attempt-seg.is-wrong{
    background: var(--bad);
    border-color: rgba(255,100,112,0.8);
}

@media (max-width: 768px){
    .attempt-bar{
        margin-bottom: 6px;
    }
    .attempt-seg{
        width: 18px;
        height: 5px;
    }
}

/* show on active + previous */
.slot-unit.pos-next .sub-translation{ display:none !important; }
.resolved .sub-translation{ display:none !important; }
.slot-unit.pos-active.resolved .sub-translation{ display:block !important; opacity: 0.72; }
.slot-unit.pos-prev.resolved .sub-translation{ display:block !important; opacity: 0.55; }
.slot-unit.pos-next .attempt-bar{ opacity: 0.4; }
.slot-unit.pos-prev .attempt-bar{ opacity: 0.65; }

/* readability tweaks */
.slot-unit.pos-active{ text-shadow: 0 10px 24px rgba(0,0,0,0.65); }
.slot-unit.pos-prev{ opacity:0.82; filter: blur(0.35px); }
.slot-unit.pos-next{ opacity:0.45; filter: blur(1.2px); }

/* positions use vars (JS guarantees prev fits inside stage) */
.slot-unit.pos-next  { transform: translateY(var(--y-next)) scale(0.82); }
.slot-unit.pos-active{ transform: translateY(var(--y-active)) scale(1); filter: blur(0); }
.slot-unit.pos-prev  { transform: translateY(var(--y-prev)) scale(0.92); }
.slot-unit.pos-exit  { transform: translateY(var(--y-exit)) scale(0.6); opacity:0; filter: blur(0); }

/* shake uses active Y var */
.shake-active{ animation: shake 0.3s; }
@keyframes shake{
    0%, 100% { transform: translateY(var(--y-active)) translateX(0); }
    25%      { transform: translateY(var(--y-active)) translateX(-10px); }
    75%      { transform: translateY(var(--y-active)) translateX(10px); }
}

/* ===================================================================== */
/* MOBILE */
/* ===================================================================== */
@media (max-width: 768px){
    header{
        flex-direction:column;
        align-items:stretch;
        gap:8px;
        padding:8px 15px 10px;
        padding-top:max(8px, env(safe-area-inset-top));
    }

    .credits-top{ justify-content:flex-start; font-size:10px; }
    .header-row{ align-items:flex-start; }

    .app-title{
        font-size:18px;
        line-height:1.05;
        margin:0;
    }

    main{
        padding:6px;
        gap:6px;
    }

    .game-hud{
        height: 44px;
        padding: 0 8px;
        border-radius: 10px;
    }
    .stat-item{ min-width:72px; }
    .stat-item .value{ font-size:16px; }

    .search-input{
        padding: 8px 10px;
        font-size: 13px;
    }

    /* shorter stage (you asked), but still readable */
    .stage{
        width: 100%;
        height: clamp(220px, 42dvh, 320px);
        border-radius:18px;
        border-width: 2px;
    }

    .stage::before, .stage::after{ height: 55px; }

    .lang-toggle{
        width:32px; height:32px;
        border-radius:10px;
        font-size:0;
    }

    .controls-top{
        display:grid;
        grid-template-columns: auto 1fr;
        align-items:center;
        gap:8px 10px;
    }

    .level-chips{
        flex-wrap: wrap;
        row-gap:6px;
    }

    .chip{ padding:5px 9px; font-size:12px; }

    .controls-area{ height: 70px; }
    .btn{ font-size:17px; }

    .btn.der{
        box-shadow: 0 0 12px rgba(75,139,255,0.55),
                    0 0 26px rgba(75,139,255,0.35),
                    0 4px 0 rgba(0,0,0,0.3);
    }
    .btn.die{
        box-shadow: 0 0 12px rgba(255,108,174,0.55),
                    0 0 26px rgba(255,108,174,0.35),
                    0 4px 0 rgba(0,0,0,0.3);
    }
    .btn.das{
        box-shadow: 0 0 12px rgba(255,216,75,0.55),
                    0 0 26px rgba(255,216,75,0.35),
                    0 4px 0 rgba(0,0,0,0.3);
    }
}

/* ===================================================================== */
/* DESKTOP ONLY */
/* ===================================================================== */
@media (min-width: 769px){
    header{ padding-left:0; padding-right:0; }
    header > *{
        max-width:1100px;
        margin:0 auto;
        padding-left:18px;
        padding-right:18px;
    }

    main{ max-width:1100px; padding-top:8px; }

    .game-hud, .controls-area, .stage-wrapper{
        width: var(--stage-w);
        margin-left:auto;
        margin-right:auto;
    }
    .search-panel{
        width: var(--stage-w);
    }

    .game-hud{
        max-width:720px;
        height:56px;
        padding:0 18px;
        border-radius:14px;
    }

    .stat-item{ min-width:0; }
    .stat-item .label{ font-size:10px; letter-spacing:0.6px; }
    .stat-item .value{ font-size:20px; }

    .stage{ width: var(--stage-w); height:480px; }

    .controls-area{ gap:14px; height:90px; }
    .btn{ font-size:22px; border-radius:16px; padding:18px 0; }
}
