: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;
    }
}