: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:1200px){
      #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:1200px){
      /* 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:1200px){
      /* 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:1201px){
      /* 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; }
    }

     /* Tiny additions on top of your shared styles */
    .toolrow .seg {display:inline-flex;border:1px solid var(--border);border-radius:10px;overflow:hidden}
    .toolrow .seg button{border:0;background:transparent;padding:6px 10px}
    .toolrow .seg button[aria-pressed="true"]{background:rgba(255,255,255,.06)}

    table.lm {width:100%;border-collapse:collapse;background:var(--card);color:var(--fg)}
    table.lm thead th{position:sticky;top:0;background:var(--bg);z-index:1;text-align:left;padding:8px;border-bottom:1px solid var(--border);font-weight:600}
    table.lm tbody td{padding:8px;border-bottom:1px solid var(--border);vertical-align:middle}

    .status-item{display:inline-flex;align-items:center;gap:6px;margin-right:10px}
    .status-dot{width:8px;height:8px;border-radius:50%;background:#666;display:inline-block}
    .status-new .status-dot{background:#8ab4f8}
    .status-cards .status-dot{background:#a78bfa}
    .status-known .status-dot{background:#34d399}
    .status-ignored .status-dot{background:#f9ab00}

    .row-tools{display:flex;gap:6px;flex-wrap:wrap}
    .row-tools .iconbtn{width:28px;height:28px}
    .row-tools .mini{padding:4px 8px;border-radius:8px}

    .thumb50{width:50px;height:36px;object-fit:cover;border-radius:6px;border:1px solid var(--border);background:#000}

    .pill-muted{background:rgba(255,255,255,.06);border:1px solid var(--border);border-radius:999px;padding:2px 8px;color:var(--muted);font-size:12px}

    .selectable td:first-child{width:32px}
    .sticky-footer{position:sticky;bottom:0;background:var(--bg);border-top:1px solid var(--border);padding:8px;display:flex;gap:8px;align-items:center}

    .empty{padding:24px;text-align:center;color:var(--muted)}

    /* Make checkboxes larger for touch */
    .chk{width:18px;height:18px}

    #loading{
      position:fixed; left:0; right:0; top:var(--header-h); bottom:0;
      display:none; align-items:center; justify-content:center;
      background:rgba(15,15,16,.45); z-index:6;
    }
    #loading .spinner{
      width:22px; height:22px; border-radius:50%;
      border:2px solid var(--border); border-top-color:var(--fg);
      animation:spin .8s linear infinite;
    }
    #loading .label{ margin-top:8px; font-size:12px; color:var(--muted); text-align:center; }
    body[data-loading="true"] #loading{ display:flex; }
    @keyframes spin{ to{ transform:rotate(360deg) } }

    .lm td[dir="rtl"] { text-align: right; }

    /* grid rows: keep everything inside the viewport */
    .lesson       { max-width:100%; overflow:hidden; }
    .lesson > *   { min-width:0; }                   /* <-- critical */
    .ltitle,
    .lsub         { min-width:0; max-width:100%; overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
    .ltools       { flex-wrap:wrap; gap:6px 8px; }   /* already there but keep */
    @supports (overflow: clip) {
      .lesson { overflow: clip; }                    /* iOS/Safari safe clip */
    }

    /* mobile grid: allow image column to shrink a bit */
    @media (max-width:1200px){
      .lesson{ grid-template-columns: 36px minmax(36px, 48px) 1fr; }
    }

    /* break unbreakables (e.g., long channel/title tokens) */
    .ltitle, .lsub { overflow-wrap:anywhere; word-break:break-word; }


    /* make the table host scroll horizontally if needed (not the page) */
    #tbl-pending, #tbl-complete { display:block; max-width:100%; overflow-x:auto; -webkit-overflow-scrolling:touch; }

    /* keep table cells from demanding more width than available */
    table.lm { table-layout:fixed; }
    table.lm th, table.lm td { overflow:hidden; text-overflow:ellipsis; white-space:nowrap; }
    table.lm td { overflow-wrap:anywhere; word-break:break-word; }

    /* actions/status can wrap to avoid pushing width */
    .row-tools { flex-wrap:wrap; }

    /* hide low-value columns on small screens to prevent X overflow */
    @media (max-width:640px){
      /* 1:chk 2:lang 3:thumb 4:title 5:channel 6:status 7:actions 8:created */
      table.lm thead th:nth-child(2),
      table.lm tbody td:nth-child(2) { display:none; } /* Language */

      table.lm thead th:nth-child(5),
      table.lm tbody td:nth-child(5) { display:none; } /* Channel */

      table.lm thead th:nth-child(8),
      table.lm tbody td:nth-child(8) { display:none; } /* Created At */
    }

    /* (optional) if you still see squeeze: let Status wrap tighter on mobile */
    @media (max-width:640px){
      .status-item { margin-right:6px; }
      .toolrow .seg button{ padding: 6px 8px; font-size: 12px; }
    }

    /* the main scroller should never create an X-scroll on the viewport */
    #main-scroll { overflow-y:auto; overflow-x:hidden; }

    /* Turn .lm tables into cards on narrow screens */
    @media (max-width:640px){
      /* hide header row, we’ll print labels per cell */
      table.lm thead{ display:none !important; }

      /* make rows/cards */
      table.lm, table.lm tbody, table.lm tr, table.lm td{ display:block; width:100%; }
      table.lm tbody tr{
        background:var(--card);
        border:1px solid var(--border);
        border-radius:12px;
        padding:10px;
        margin:10px 0;
      }

      /* each cell becomes a 2-col row: [label][value] */
      table.lm tbody td{
        display:grid !important;
        gap:6px 10px;
        padding:6px 0;
        border:0;               /* kill row separators */
        white-space:normal;     /* allow wrapping inside cards */
        overflow:visible;       /* ignore table ellipsis on mobile cards */
        text-overflow:clip;
      }
      table.lm tbody td::before{
        content: attr(data-label);
        color: var(--muted);
        font-size:12px;
        font-weight:600;
      }

      /* polish */
      table.lm tbody td:first-child{ padding-top:0; }
      table.lm tbody td:first-child::before{ content:''; display:none; } /* e.g. checkbox cell */
      table.lm tbody td:last-child{ padding-bottom:0; }

      /* common bits in your rows */
      .thumb50{ width:96px; height:64px; border-radius:10px; }
      .row-tools{ justify-content:flex-end; gap:8px; margin-top:6px; flex-wrap:wrap; }
    }

    @media (max-width:640px){
      table.lm tbody td > * { grid-column: 2; }
      td.status-cell .status-item{ margin-right:10px; }
      td.status-cell .status-item:last-child{ margin-right:0; }
    }

    /* ===== Standardize library subsections to match Lessons ===== */
    .content-section { margin: 0; }
    .content-section .section-head { display: none !important; } /* kill h2s if present */

    /* Use same surface/colors as Lessons */
    .content-section table.lm{
      background: var(--card);
      border: 1px solid var(--border);
      border-radius: 12px;
      overflow: hidden;
    }
    .content-section table.lm thead{ display:none }               /* remove table headers */
    .content-section table.lm tbody td{
      padding: 8px;
      border-bottom: 1px solid var(--border);
      vertical-align: middle;
    }

    /* Top row with prev/next/refresh (looks like Lessons toolrow) */
    .content-section .toolrow{
      display:flex; gap:8px; align-items:center; margin: 0 0 8px 0;
    }
    .content-section .toolrow .spacer{ flex:1 }
    .content-section .toolrow .ghost{ min-width: 72px }

    /* Make links inside pill groups look/behave like the tab buttons */
    .toolrow .seg a,
    .toolrow .seg button{
      display:inline-flex;
      align-items:center;
      gap:6px;
      border:0;
      background:transparent;
      padding:6px 10px;
      color:var(--fg);
      text-decoration:none;
    }

    .toolrow .seg a:hover,
    .toolrow .seg button:hover{
      background:rgba(255,255,255,.06);
    }

    .toolrow .seg a:focus-visible,
    .toolrow .seg button:focus-visible{
      outline:2px solid var(--accent);
      outline-offset:-2px; /* sits nicely inside the group border */
    }

    /* optional: move the inline SVG margin into CSS */
    #create-btn svg{ margin-right:6px; }

    /* LESSONS-ONLY: checkbox (col 1) */
    #tbl-pending table.lm th:first-child,
    #tbl-pending table.lm td:first-child,
    #tbl-complete table.lm th:first-child,
    #tbl-complete table.lm td:first-child{
      width:24px;
      padding-left:6px;
      padding-right:6px;
      text-align:center;
    }
    #tbl-pending table.lm .chk,
    #tbl-complete table.lm .chk{
      width:14px; height:14px; margin:0 auto; display:block;
    }

    /* LESSONS-ONLY: Language col (col 2) */
    #tbl-pending table.lm th:nth-child(2),
    #tbl-pending table.lm td:nth-child(2),
    #tbl-complete table.lm th:nth-child(2),
    #tbl-complete table.lm td:nth-child(2){
      width:90px;
      max-width:100px;
    }

    /* LESSONS-ONLY: Thumb col (col 3) */
    #tbl-pending table.lm th:nth-child(3),
    #tbl-pending table.lm td:nth-child(3),
    #tbl-complete table.lm th:nth-child(3),
    #tbl-complete table.lm td:nth-child(3){
      width:56px;
      max-width:60px;
      padding-left:4px;
      padding-right:4px;
      text-align:center;
    }

    /* LESSONS-ONLY: Smaller image inside col 3 */
    #tbl-pending table.lm td:nth-child(3) .thumb50,
    #tbl-complete table.lm td:nth-child(3) .thumb50{
      width:44px;
      aspect-ratio:16 / 9;
      height:auto;
      display:block;
      object-fit:cover;
      border-radius:6px;
      margin:0 auto;
    }

    /* LESSONS-ONLY: slightly tighter row padding (optional) */
    #tbl-pending table.lm tbody td,
    #tbl-complete table.lm tbody td{
      padding-top:6px;
      padding-bottom:6px;
    }

    #btn-apply { display:none !important; }
    #btn-reset { width:100%; }

    body.embedding #main-header .seg,
    body.embedding #page-nav,
    body.embedding #count-badge,
    body.embedding #page-indicator { display:none !important; }

