:root {
    --bg: #0f0f10;
    --fg: #e8e8ea;
    --muted: #a9adb7;
    --card: #15161a;
    --border: #2b2f36;
    --accent: #4c8df6;
    --good: #42b883;
    --danger: #e66;
    --warn: #f4b400;
    --header-h: 50px;
}

* {
    box-sizing: border-box
}

html, body {
    height: 100%
}

body {
    margin: 0;
    background: var(--bg);
    color: var(--fg);
    font: 14px/1.4 system-ui, -apple-system, Segoe UI, Roboto, Inter, Arial, sans-serif;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

header {
    display: flex;
    gap: 8px;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    background: var(--bg);
    z-index: 5;
}

input, select, button, textarea {
    background: var(--card);
    color: var(--fg);
    border: 1px solid var(--border);
    border-radius: 8px;
    padding: 8px 10px;
    outline: none;
}

button {
    cursor: pointer
}

button.primary {
    background: rgba(76, 141, 246, .16);
    border-color: rgba(76, 141, 246, .45)
}

button.primary:hover {
    background: rgba(76, 141, 246, .24)
}

button.good {
    background: rgba(66, 184, 131, .14);
    border-color: rgba(66, 184, 131, .38)
}

button.danger {
    background: rgba(238, 102, 102, .14);
    border-color: rgba(238, 102, 102, .40)
}

button:disabled {
    opacity: .6;
    cursor: not-allowed
}

.pill {
    padding: 2px 8px;
    border: 1px solid var(--border);
    border-radius: 999px;
    font-size: 12px
}

.hint {
    font-size: 12px;
    color: var(--muted)
}

.badge {
    font-size: 12px;
    color: #ccc
}

.hr {
    height: 1px;
    background: var(--border);
    margin: 8px 0
}

#wrap {
    --side-w: 320px;
    height: calc(100dvh - var(--header-h));
    display: grid;
    grid-template-columns:var(--side-w) 1fr;
    min-height: 0;
}

#side {
    position: relative;
    border-right: 1px solid var(--border);
    padding: 12px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    width: var(--side-w);
    overflow-y: auto;
}

.panel {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 10px
}

#main {
    display: flex;
    flex-direction: column;
    min-height: 0
}

#main-header {
    flex: 0 0 auto;
    position: sticky;
    top: 0;
    z-index: 1;
    background: var(--bg);
    border-bottom: 1px solid var(--border);
    padding: 12px
}

#main-scroll {
    flex: 1 1 auto;
    overflow: auto;
    padding: 12px
}

/* Meta grid */
.meta-grid {
    display: flex;
    flex-direction: column;
    gap: 10px;
    margin-top: 10px
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px
}

/* Lessons */
.toolrow {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
    margin-bottom: 10px
}

.lessons {
    display: block;
    margin-top: 8px
}

.lesson {
    display: grid;
    grid-template-columns: 44px 56px 1fr auto;
    gap: 8px;
    align-items: center;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 10px;
    margin-bottom: 8px;
    background: rgba(255, 255, 255, .02);
}

.lesson.dragging {
    opacity: .7;
    outline: 2px dashed var(--accent)
}

.handle {
    cursor: grab;
    user-select: none;
    font-weight: 700;
    text-align: center;
}

.ltitle {
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
}

.ltools {
    display: flex;
    gap: 6px;
}

.lsub {
    color: var(--muted);
    font-size: 12px;
}

.thumb {
    width: 48px;
    height: 48px;
    object-fit: cover;
    border-radius: 8px;
    background: #000;
    border: 1px solid var(--border);
}

/* Mobile */
@media (max-width: 900px) {
    #wrap {
        grid-template-columns:1fr
    }

    #side {
        position: fixed;
        left: 0;
        top: var(--header-h);
        bottom: 0;
        width: min(86vw, 360px);
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: 0 10px 24px rgba(0, 0, 0, .4);
        z-index: 4;
    }

    body.sidebar-open #side {
        transform: translateX(0)
    }

    #backdrop {
        position: fixed;
        inset: var(--header-h) 0 0 0;
        background: rgba(0, 0, 0, .4);
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease;
        z-index: 3
    }

    body.sidebar-open #backdrop {
        opacity: 1;
        pointer-events: auto
    }

    #open-side {
        display: inline-flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        margin-right: 6px;
        border-radius: 999px;
        background: var(--card);
        border: 1px solid var(--border);
    }

    header {
        flex-wrap: wrap;
        gap: 6px 8px
    }

    header input, header select, header button {
        flex: 1 1 auto;
        min-width: 0;
        width: auto !important
    }

    #in-course {
        flex-basis: 100%
    }
}

@media (max-width: 900px) {
    /* make sidebar overlay header */
    #side {
        z-index: 6;
    }

    #backdrop {
        z-index: 5;
    }

    /* prevent lesson row overflow */
    .lesson {
        grid-template-columns: 36px 44px 1fr;
    }

    .ltools {
        grid-column: 1 / -1;
        justify-content: flex-end;
        flex-wrap: wrap;
        gap: 6px 8px;
    }
}


/* Hide app until a course is loaded */
body[data-has-course="false"] #wrap {
    display: none;
}

.iconbtn {
    width: 28px;
    height: 28px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: 1px solid var(--border);
    border-radius: 8px;
    background: var(--card);
    color: var(--fg);
    padding: 0;
}

.iconbtn:hover {
    border-color: var(--accent);
}

.iconbtn svg {
    width: 16px;
    height: 16px;
    display: block
}

.iconbtn.danger {
    background: rgba(238, 102, 102, .12);
    border-color: rgba(238, 102, 102, .40);
}

.iconbtn.danger:hover {
    background: rgba(238, 102, 102, .18);
}

/* Course Gate (overlay shown when no course is loaded) */
#course-gate {
    position: fixed;
    inset: 0;
    display: none; /* toggled via .show */
    align-items: center;
    justify-content: center;
    background: rgba(0, 0, 0, .55);
    z-index: 999; /* above header and sidebar */
}

#course-gate.show {
    display: flex;
}

.gate-card {
    width: min(680px, 92vw);
    max-height: min(86vh, 800px);
    overflow: auto;
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 14px;
    box-shadow: 0 12px 36px rgba(0, 0, 0, .5);
}

.gate-title {
    display: flex;
    align-items: center;
    gap: 10px;
}

.gate-title strong {
    font-size: 16px
}

.gate-hr {
    height: 1px;
    background: var(--border);
    margin: 10px 0
}

.gate-grid {
    display: grid;
    grid-template-columns:1fr;
    gap: 10px;
}

.gate-row {
    display: flex;
    gap: 8px;
    align-items: center;
    flex-wrap: wrap;
}

.gate-row > * {
    flex: 1 1 auto;
    min-width: 0
}

.gate-actions {
    display: flex;
    gap: 8px;
    justify-content: flex-end;
    flex-wrap: wrap;
}

.gate-small {
    font-size: 12px;
    color: var(--muted)
}

/* prevent the long list from blowing up on mobile */
#gate-select {
    width: 100%;
    max-width: 100%
}

#gate-search {
    width: 100%
}

/* --- sidebar toggle (base) --- */
#side-toggle {
    position: absolute;
    top: 8px;
    right: 8px;
    width: 28px;
    height: 28px;
    border-radius: 999px;
    background: var(--card);
    border: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 2;
}

/* --- mobile (drawer) --- */
@media (max-width: 900px) {
    /* hide desktop collapse button */
    #side-toggle {
        display: none !important;
    }

    #wrap {
        grid-template-columns:1fr;
    }

    #side {
        position: fixed;
        left: 0;
        top: var(--header-h);
        bottom: 0;
        width: min(86vw, 360px);
        transform: translateX(-100%);
        transition: transform .2s ease;
        box-shadow: 0 10px 24px rgba(0, 0, 0, .4);
        z-index: 6;
    }

    body.sidebar-open #side {
        transform: translateX(0);
    }

    #backdrop {
        position: fixed;
        inset: var(--header-h) 0 0 0;
        background: rgba(0, 0, 0, .4);
        opacity: 0;
        pointer-events: none;
        transition: opacity .2s ease;
        z-index: 5;
    }

    body.sidebar-open #backdrop {
        opacity: 1;
        pointer-events: auto;
    }

    /* show hamburger in header on mobile */
    #open-side {
        display: inline-flex !important; /* <— beats the inline display:none */
        align-items: center;
        justify-content: center;
    }
}

/* --- desktop (collapsible rail) --- */
@media (min-width: 901px) {
    /* hide mobile hamburger on desktop */
    #open-side {
        display: none !important;
    }

    /* layout */
    #wrap {
        display: flex;
        min-height: calc(100vh - var(--header-h, 50px));
    }

    #side {
        width: 320px;
        flex: 0 0 auto;
        transition: width .18s ease;
    }

    #main {
        flex: 1 1 auto;
        min-width: 0;
    }

    /* collapsed state */
    #wrap.collapsed {
        --side-w: 48px;
    }

    #wrap.collapsed #side {
        width: 56px;
        overflow: hidden;
    }

    #wrap.collapsed #side > *:not(#side-toggle) {
        display: none;
    }

    /* keep only the toggle visible */
    #wrap.collapsed #side .panel {
        display: none;
    }

    /* IMPORTANT: keep toggle small; don't stretch full width */
    #side #side-toggle {
        width: 28px;
        height: 28px;
    }
}

    .ca-backdrop {
    position: fixed; inset: 0; background: rgba(0,0,0,.35);
    display:flex; align-items:center; justify-content:center; z-index: 99999;
    }
    .ca-modal {
    min-width: 320px; max-width: 560px; background:#fff; color:#111;
    border-radius: 14px; box-shadow: 0 10px 30px rgba(0,0,0,.25);
    overflow:hidden; font: 14px/1.45 system-ui, -apple-system, Segoe UI, Roboto, Helvetica, Arial;
    transform: translateY(6px); opacity: 0; transition: opacity .12s ease, transform .12s ease;
    }
    .ca-modal.ca-in { transform: translateY(0); opacity: 1; }
    .ca-header {
    display:flex; gap:.6rem; align-items:center; padding: 14px 16px; border-bottom: 1px solid rgba(0,0,0,.06);
    }
    .ca-badge {
    flex: 0 0 auto; width: 22px; height: 22px; border-radius: 999px; display:grid; place-items:center;
    font-size: 12px; font-weight: 700; color: #fff;
    }
    .ca-title { font-weight: 700; font-size: 15px; }
    .ca-body { padding: 14px 16px; white-space: pre-wrap; }
    .ca-footer { padding: 12px 16px; display:flex; justify-content:flex-end; gap:8px; background: rgba(0,0,0,.02); }
    .ca-btn {
    appearance: none; border: 1px solid rgba(0,0,0,.15); background:#fff; padding: 8px 12px; border-radius: 8px; cursor:pointer;
    }
    .ca-btn:focus { outline: 2px solid rgba(0,0,0,.2); outline-offset: 1px; }
    .ca-btn.primary {
    background: #111; color:#fff; border-color:#111;
    }
    /* Variants */
    .ca-err  .ca-badge { background:#d92d20; }
    .ca-info .ca-badge { background:#2563eb; }
    .ca-ok   .ca-badge { background:#16a34a; }
    @media (prefers-color-scheme: dark) {
    .ca-modal { background:#1f1f1f; color:#e9e9e9; box-shadow: 0 10px 30px rgba(0,0,0,.45); }
    .ca-footer { background: rgba(255,255,255,.03); }
    .ca-btn { background:#2a2a2a; color:#eee; border-color: rgba(255,255,255,.18); }
    .ca-btn.primary { background:#e9e9e9; color:#111; border-color:#e9e9e9; }
    .ca-header { border-bottom-color: rgba(255,255,255,.08); }
    }

    /* 🔔 1-second glow ping for the settings button */
    #settings-button.error-glow {
    position: relative;
    animation: settings-glow 1s ease-out;
    }

    @keyframes settings-glow {
    0%   { box-shadow: 0 0 0px 0 rgba(255, 86, 86, 0.0); color: inherit; }
    10%  { box-shadow: 0 0 12px 6px rgba(255, 86, 86, 0.55); color: #ff5757; }
    60%  { box-shadow: 0 0 8px 3px rgba(255, 86, 86, 0.35); }
    100% { box-shadow: 0 0 0px 0 rgba(255, 86, 86, 0.0); color: inherit; }
    }

    /* Respect reduced-motion users */
    @media (prefers-reduced-motion: reduce) {
    #settings-button.error-glow { animation: none; box-shadow: 0 0 10px 4px rgba(255, 86, 86, .5); }
    }

    /* Flip flow where it helps when learning language is RTL */
    .lesson-problem.rtl-ll .token-bank,
    .lesson-problem.rtl-ll .answer-queue {
    direction: rtl;
    }

    /* Keep numbers/shortcut badges readable inside rtl containers */
    .lesson-problem.rtl-ll .button-key { direction: ltr; }
