  * { box-sizing: border-box; }

  :root {
    --navy: #09295b;
    --ink: #17233d;
    --muted: #65718a;
    --line: #dbe1ea;

    --break: #fff1a8;
    --lunch: #dff1d5;
    --registration: #f4f6fa;

    --bg: #eef1f5;
    --surface: #f8f9fb;
    --alert: #c71919;
    --accent: #2563eb;
    --slate: #33405a;
    --danger: #c0392b;
    --error-bg: #fde8e8;
    --error-line: #f3b4b4;
    --error-ink: #7a1a1a;
    --teacher-ink: #536079;

    --gutter: clamp(10px, 2.2vw, 24px);
    --ease-out: cubic-bezier(.32, .72, 0, 1);
    --ease-in: cubic-bezier(.5, 0, .75, 0);
    --press: 100ms ease-out;
  }

  html { -webkit-text-size-adjust: 100%; }

  body {
    margin: 0;
    overflow: hidden;
    background: var(--ui-bg);
    color: var(--ui-text);
    font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont,
                 "Segoe UI", sans-serif;
    line-height: 1.4;
  }

  html { -webkit-font-smoothing: antialiased; }

  :focus-visible {
    outline: 2px solid var(--ui-accent);
    outline-offset: 2px;
  }

  /* The timetable itself is paper: white in light and dark mode alike. */
  .page {
    max-width: 1600px;
    margin: 0 auto;
    background: white;
    color: var(--ink);
    line-height: normal;
    -webkit-font-smoothing: auto;
    padding: var(--gutter);
    border-radius: 20px;
    box-shadow: var(--ui-shadow);
  }

  .weeks {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(min(340px, 100%), 1fr));
    gap: 28px;
    align-items: start;
  }

  .week-title {
    display: inline-block;
    background: var(--navy);
    color: white;
    padding: 8px 18px;
    border-radius: 9px 9px 0 0;
    font-size: 18px;
    font-weight: 850;
    letter-spacing: .5px;
  }

  .days {
    display: grid;
    grid-template-columns: repeat(5, minmax(0, 1fr));
    gap: 5px;
    border: 1px solid var(--line);
    border-radius: 0 10px 10px 10px;
    padding: 5px;
    background: #f8f9fb;
  }

  .day {
    min-width: 0;
    background: white;
    border-radius: 7px;
    overflow: hidden;
    border: 1px solid var(--line);
  }

  .day-name {
    background: var(--navy);
    color: white;
    text-align: center;
    padding: 9px 4px;
    font-size: 13px;
    font-weight: 850;
    letter-spacing: .3px;
  }

  .stack {
    display: flex;
    flex-direction: column;
  }

  /* Every day is its own sequence.
     This is important because Friday has a different
     position for Break and Lunch. */

  .slot {
    border-bottom: 1px solid var(--line);
  }

  .slot:last-child {
    border-bottom: 0;
  }

  .registration {
    min-height: 62px;
    padding: 8px;
    background: var(--registration);
    display: grid;
    grid-template-columns: 25px 1fr auto;
    align-items: center;
    gap: 5px;
  }

  .am {
    color: #c71919;
    font-size: 10px;
    font-weight: 900;
    align-self: start;
    padding-top: 2px;
  }

  .reg-text {
    min-width: 0;
  }

  .subject {
    font-size: 12px;
    font-weight: 850;
    line-height: 1.1;
  }

  .teacher {
    margin-top: 3px;
    color: #536079;
    font-size: 10px;
    font-weight: 650;
    line-height: 1.1;
  }

  .room {
    align-self: end;
    padding: 3px 5px;
    border: 1px solid rgba(20,35,60,.18);
    border-radius: 5px;
    background: rgba(255,255,255,.72);
    font-size: 9px;
    line-height: 1;
    font-weight: 850;
    white-space: nowrap;
  }

  .lesson {
    position: relative;
    overflow: hidden;
    min-height: 76px;
    padding: 8px 10px;
    display: grid;
    grid-template-columns: minmax(0,1fr) auto;
    gap: 5px;
    align-items: center;
  }

  /* Keep the text/room above the watermark */
  .lesson > * {
    position: relative;
    z-index: 1;
  }

  /* Big translucent subject emoji behind the tile.
     Opacity is set per-render from the JSON (watermarkOpacity). */
  .icon {
    position: absolute;
    z-index: 0;
    right: -8px;
    bottom: -12px;
    font-size: 85px;
    line-height: 1;
    opacity: .17;
    pointer-events: none;
    -webkit-user-select: none;
    user-select: none;
  }

  .lesson .subject {
    font-size: 18px;
  }

  .lesson .teacher {
    font-size: 10px;
  }

  .break,
  .lunch {
    min-height: 34px;
    display: flex;
    justify-content: center;
    align-items: center;
    gap: 5px;
    font-size: 10px;
    font-weight: 900;
    letter-spacing: .3px;
  }

  .break { background: var(--break); }
  .lunch { background: var(--lunch); }

  .period {
    display: grid;
    grid-template-columns: 18px minmax(0,1fr);
    gap: 4px;
    align-items: center;
  }

  .period-number {
    font-size: 10px;
    font-weight: 900;
    color: var(--muted);
    text-align: center;
  }

  /* Free-text note — compact block under the grids, shown on whichever
     page(s) its "appearsOn" setting points at. */
  .note {
    grid-column: 1 / -1;
    margin-top: 10px;
    padding: 10px 14px;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #f8f9fb;
    font-size: 12px;
    line-height: 1.5;
  }

  .note .nt-title {
    display: block;
    margin-bottom: 3px;
    font-weight: 850;
    letter-spacing: .3px;
    color: var(--navy);
  }

  .note .nt-line {
    display: block;
  }

  .note .nt-line b {
    font-weight: 850;
  }

  .note .fayre {
    color: #c71919;
    font-weight: 800;
  }

  /* =========================================================
     A6 CARD — one printed page. Same on screen, in print,
     and in the "Download PDF" export so there are no surprises.
     ========================================================= */
  .sheet {
    width: 148mm;
    height: 105mm;
    padding: 4mm;
    background: #fff;
    /* The card is paper: its colours must never follow the app's light/dark theme. */
    color: var(--ink);
    color-scheme: light;
    line-height: normal;
    -webkit-font-smoothing: auto;
    overflow: hidden;
    position: relative;
    -webkit-print-color-adjust: exact;
    print-color-adjust: exact;
  }

  .sheet .week-title {
    padding: 3px 9px;
    font-size: 8.5px;
    border-radius: 4px 4px 0 0;
  }

  .sheet .days {
    gap: 2px;
    padding: 2px;
    border-radius: 0 5px 5px 5px;
  }

  .sheet .day { border-radius: 3px; }

  .sheet .day-name {
    padding: 2px 1px;
    font-size: 9px;
    letter-spacing: 0;
  }

  .sheet .registration {
    min-height: 21px;
    padding: 3px;
    grid-template-columns: 11px 1fr auto;
    gap: 3px;
    align-items: center;
  }

  .sheet .lesson {
    min-height: 41px;
    padding: 4px 6px;
    grid-template-columns: minmax(0, 1fr) auto;
    gap: 3px;
  }

  /* Long subject names are shrunk to fit on one line by fitLessonSubjects()
     right before the PDF snapshot; word-break stays "normal" so the rare
     name that still doesn't fit wraps at a space instead of mid-word. */

  /* Watermark emoji, sized for the small card */
  .sheet .icon {
    font-size: 45px;
    right: -5px;
    bottom: -8px;
  }

  .sheet .room {
    align-self: center;
    font-size: 9px;
    padding: 1px 3px;
    border-radius: 3px;
  }

  .sheet .subject { font-size: 11px; line-height: 1.1; }
  .sheet .lesson .subject { font-size: 11px; }
  .sheet .registration .subject { font-size: 7px; hyphens: none; }
  .sheet .registration .teacher { font-size: 6px; margin-top: 0; }
  .sheet .period .subject { font-size: 10px; hyphens: none; }
  .sheet .teacher,
  .sheet .lesson .teacher { font-size: 8px; margin-top: 1px; line-height: 1.1; }

  .sheet .am { font-size: 6px; padding-top: 0; align-self: center; }

  .sheet .break,
  .sheet .lunch {
    min-height: 13px;
    padding: 2px 0;
    font-size: 6.5px;
    letter-spacing: 0;
  }

  .sheet .period { grid-template-columns: 9px minmax(0, 1fr); gap: 2px; }
  .sheet .period-number { font-size: 7px; }

  .sheet .note {
    margin: 3px 0 0;
    padding: 3px 5px;
    border: 1px solid var(--line);
    border-radius: 4px;
    background: #f8f9fb;
    font-size: 6px;
    line-height: 1.3;
  }
  .sheet .note .nt-title {
    display: block;
    margin-bottom: 1px;
    font-size: 6.5px;
    font-weight: 850;
    color: var(--navy);
  }
  .sheet .note .nt-line { display: block; }


  /* ==========================================================================
     APP CHROME — everything around the timetable.
     The timetable ("paper") keeps its own tokens above and never changes with
     the theme; the chrome uses --ui-* tokens that follow the system light/dark
     setting, or the choice made in Settings → Appearance.
     ========================================================================== */

  :root {
    color-scheme: light;
    --ui-bg: #f4f7ff;
    --ui-glow-a: rgba(79,130,255,.34);
    --ui-glow-b: rgba(190,140,255,.26);
    --ui-pane: #fbfcff;
    --ui-chrome: rgba(255,255,255,.82);
    --ui-chrome-edge: rgba(255,255,255,.85);
    --ui-surface: #ffffff;
    --ui-surface-2: #eaf0ff;
    --ui-surface-3: #d8e3ff;
    --ui-input: #ffffff;
    --ui-text: #101a30;
    --ui-text-2: #52607f;
    --ui-line: rgba(30,60,140,.13);
    --ui-accent: #2563eb;
    --ui-accent-fill: #2563eb;
    --ui-accent-text: #1d4ed8;
    --ui-accent-soft: rgba(37,99,235,.11);
    --ui-danger: #c0392b;
    --ui-danger-soft: rgba(192,57,43,.10);
    --ui-ok: #1f9d55;
    --ui-warn: #d98a00;
    --ui-shadow: 0 1px 2px rgba(16,32,60,.06), 0 14px 44px -16px rgba(16,32,60,.30);
    --ui-scrim: rgba(10,18,35,.38);
    --cm-string: #0b7a4b;
    --cm-number: #b45309;
    --cm-prop: #1d4ed8;
    --cm-atom: #7c3aed;
  }

  @media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
      color-scheme: dark;
      --ui-bg: #0a1224;
      --ui-glow-a: rgba(59,110,255,.22);
      --ui-glow-b: rgba(139,92,246,.15);
      --ui-pane: #0e1832;
      --ui-chrome: rgba(14,23,44,.72);
      --ui-chrome-edge: rgba(255,255,255,.09);
      --ui-surface: #131d36;
      --ui-surface-2: #1b2745;
      --ui-surface-3: #26355d;
      --ui-input: #0d1630;
      --ui-text: #e9eefb;
      --ui-text-2: #9db0d3;
      --ui-line: rgba(255,255,255,.11);
      --ui-accent: #5b93ff;
      --ui-accent-text: #8fb4ff;
      --ui-accent-soft: rgba(91,147,255,.18);
      --ui-danger: #ff7f70;
      --ui-danger-soft: rgba(255,127,112,.13);
      --ui-ok: #3ddc84;
      --ui-warn: #ffb547;
      --ui-shadow: 0 1px 2px rgba(0,0,0,.45), 0 18px 54px -16px rgba(0,0,0,.7);
      --ui-scrim: rgba(2,6,16,.55);
      --cm-string: #7ee0a8;
      --cm-number: #ffb870;
      --cm-prop: #8fb4ff;
      --cm-atom: #c4a5ff;
    }
  }
  :root[data-theme="dark"] {
    color-scheme: dark;
    --ui-bg: #0a1224;
    --ui-glow-a: rgba(59,110,255,.22);
    --ui-glow-b: rgba(139,92,246,.15);
    --ui-pane: #0e1832;
    --ui-chrome: rgba(14,23,44,.72);
    --ui-chrome-edge: rgba(255,255,255,.09);
    --ui-surface: #131d36;
    --ui-surface-2: #1b2745;
    --ui-surface-3: #26355d;
    --ui-input: #0d1630;
    --ui-text: #e9eefb;
    --ui-text-2: #9db0d3;
    --ui-line: rgba(255,255,255,.11);
    --ui-accent: #5b93ff;
    --ui-accent-text: #8fb4ff;
    --ui-accent-soft: rgba(91,147,255,.18);
    --ui-danger: #ff7f70;
    --ui-danger-soft: rgba(255,127,112,.13);
    --ui-ok: #3ddc84;
    --ui-warn: #ffb547;
    --ui-shadow: 0 1px 2px rgba(0,0,0,.45), 0 18px 54px -16px rgba(0,0,0,.7);
    --ui-scrim: rgba(2,6,16,.55);
    --cm-string: #7ee0a8;
    --cm-number: #ffb870;
    --cm-prop: #8fb4ff;
    --cm-atom: #c4a5ff;
  }

  /* ---------- shell ---------- */
  .app {
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    background:
      radial-gradient(1100px 620px at 8% -12%, var(--ui-glow-a), transparent 62%),
      radial-gradient(900px 600px at 100% 0%, var(--ui-glow-b), transparent 58%),
      var(--ui-bg);
  }
  .workspace { flex: 1; min-height: 0; display: flex; }

  [data-icon] { display: inline-flex; align-items: center; justify-content: center; }
  .ico { width: 1.25em; height: 1.25em; flex: none; }

  /* One press language everywhere: instant feedback on pointer-down. */
  .icon-btn, .btn-primary, .f-mini-btn, .seg button,
  .zoom-label, .dialog-actions button, .toast button, .btn-quiet, .btn-danger {
    transition: transform var(--press), background-color 150ms ease, color 150ms ease,
                filter 150ms ease, opacity 150ms ease, box-shadow 150ms ease;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
  }
  .icon-btn:active:not(:disabled), .btn-primary:active:not(:disabled), .btn-ghost:active, .menu-item:active, .btn-add:active,
  .f-mini-btn:active, .zoom-label:active, .btn-quiet:active,
  .btn-danger:active, .dialog-actions button:active, .toast button:active {
    transform: scale(.97);
    transition-duration: 60ms;
  }

  /* ---------- top bar: a structural material ---------- */
  .topbar {
    flex: none;
    z-index: 70;            /* above the phone Edit sheet (60), so its menus are never hidden under it */
    display: flex;
    align-items: center;
    gap: 14px;
    min-height: 58px;
    padding: 8px max(16px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left));
    background: var(--ui-chrome);
    -webkit-backdrop-filter: blur(24px) saturate(150%);
    backdrop-filter: blur(24px) saturate(150%);
    box-shadow: inset 0 1px 0 var(--ui-chrome-edge), 0 1px 0 var(--ui-line);
  }
  .brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
  .brand-mark { width: 32px; height: 32px; flex: none; display: block; }
  .brand-name {
    font-size: 1rem;
    font-weight: 750;
    letter-spacing: -.015em;
    white-space: nowrap;
  }

  .save-state {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    margin-left: 6px;
    padding: 5px 11px;
    border-radius: 999px;
    background: var(--ui-surface-2);
    color: var(--ui-text-2);
    font-size: .75rem;
    font-weight: 600;
    letter-spacing: .01em;
    white-space: nowrap;
    min-width: 0;
  }
  .save-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ui-text-2); flex: none; opacity: .6; }
  .save-state[data-state="saved"] .save-dot { background: var(--ui-ok); opacity: 1; }
  .save-state[data-state="saving"] .save-dot { background: var(--ui-warn); opacity: 1; }
  .save-state[data-state="error"] { background: var(--ui-danger-soft); color: var(--ui-danger); }
  .save-state[data-state="error"] .save-dot { background: var(--ui-danger); opacity: 1; }

  .topbar-actions { margin-left: auto; display: flex; align-items: center; gap: 6px; }
  .lbl-short { display: none; }
  .save-short { display: none; }

  .icon-btn {
    width: 38px; height: 38px;
    display: inline-flex; align-items: center; justify-content: center;
    border: 0; border-radius: 11px;
    background: transparent;
    color: var(--ui-text);
    cursor: pointer;
  }
  .icon-btn.sm { width: 30px; height: 30px; border-radius: 9px; }
  .icon-btn:disabled { opacity: .35; cursor: default; }
  .icon-btn .ico { width: 19px; height: 19px; }
  .icon-btn.sm .ico { width: 16px; height: 16px; }
  @media (hover: hover) {
    .icon-btn:hover:not(:disabled) { background: var(--ui-surface-2); }
  }

  /* Secondary top-bar button: same shape as Download PDF, quieter colour. */
  .btn-ghost {
    display: inline-flex; align-items: center; gap: 7px;
    height: 38px; padding: 0 12px;
    border: 0; border-radius: 11px;
    background: var(--ui-surface-2); color: var(--ui-text);
    font: inherit; font-size: .875rem; font-weight: 650; letter-spacing: -.005em;
    cursor: pointer;
    transition: transform var(--press), background-color 150ms ease, filter 150ms ease;
    -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  }
  .btn-ghost .ico { width: 17px; height: 17px; }
  .btn-ghost:active { transform: scale(.97); transition-duration: 60ms; }
  .btn-ghost[aria-expanded="true"] .btn-caret { transform: rotate(180deg); }
  @media (hover: hover) { .btn-ghost:hover { background: var(--ui-surface-3); } }

  .btn-primary {
    display: inline-flex; align-items: center; gap: 8px;
    height: 38px;
    padding: 0 16px;
    margin-left: 8px;
    border: 0; border-radius: 11px;
    font: inherit; font-size: .875rem; font-weight: 700; letter-spacing: -.005em;
    color: #fff;
    background: linear-gradient(180deg, #4d86f7, var(--ui-accent-fill));
    box-shadow: inset 0 1px 0 rgba(255,255,255,.3), 0 6px 16px -6px rgba(37,99,235,.7);
    cursor: pointer;
  }
  .btn-primary .ico { width: 17px; height: 17px; }
  .btn-primary:disabled { opacity: .6; cursor: default; }
  @media (hover: hover) { .btn-primary:hover:not(:disabled) { filter: brightness(1.08); } }
  .btn-primary.busy [data-icon] { display: none; }
  .btn-primary.busy::before {
    content: "";
    width: .9em; height: .9em;
    border: 2px solid rgba(255,255,255,.4);
    border-top-color: #fff;
    border-radius: 50%;
    animation: spin .8s linear infinite;
  }
  @keyframes spin { to { transform: rotate(360deg); } }

  /* ---------- Download PDF menu: it grows out of the button ---------- */
  .menu-wrap { position: relative; }
  .btn-caret .ico { width: 15px; height: 15px; opacity: .85; }
  .btn-primary[aria-expanded="true"] .btn-caret { transform: rotate(180deg); }
  .btn-caret { transition: transform .3s var(--ease-out); }
  .menu {
    position: absolute; top: calc(100% + 10px); right: 0; z-index: 80;
    width: min(340px, calc(100vw - 24px));
    padding: 6px;
    border-radius: 18px;
    background: var(--ui-surface);
    box-shadow: 0 0 0 1px var(--ui-line), 0 22px 56px -12px rgba(0,0,0,.45);
    transform-origin: calc(100% - 28px) -8px;
    opacity: 0; visibility: hidden;
    transform: scale(.92) translateY(-6px);
    transition: opacity .14s ease, transform .16s var(--ease-in), visibility 0s linear .16s;
  }
  .menu.open {
    opacity: 1; visibility: visible; transform: none;
    transition: opacity .18s ease, transform .34s var(--ease-out), visibility 0s;
  }
  .menu-item {
    display: flex; align-items: center; gap: 12px;
    width: 100%;
    padding: 10px 12px;
    border: 0; border-radius: 13px; background: transparent;
    color: var(--ui-text);
    font: inherit; text-align: left;
    cursor: pointer;
    transition: background-color 120ms ease, transform var(--press);
    -webkit-tap-highlight-color: transparent;
  }
  .menu-item:active { transform: scale(.98); transition-duration: 60ms; }
  @media (hover: hover) { .menu-item:hover { background: var(--ui-surface-2); } }
  .menu-item:focus-visible { outline-offset: -2px; background: var(--ui-surface-2); }
  .menu-thumb { width: 40px; height: 50px; flex: none; color: var(--ui-accent); }
  .menu-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
  .menu-title { font-size: .9375rem; font-weight: 650; letter-spacing: -.01em; }
  .menu-blurb { font-size: .78rem; color: var(--ui-text); opacity: .85; line-height: 1.35; }
  .menu-meta { font-size: .72rem; color: var(--ui-text-2); line-height: 1.35; }
  .menu-go { color: var(--ui-accent-text); display: inline-flex; }

  .menu-ico {
    width: 40px; height: 40px; flex: none;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 12px;
    background: var(--ui-accent-soft); color: var(--ui-accent-text);
  }
  .menu-item + .menu-item { margin-top: 2px; }
  .menu.head-menu {
    position: absolute; top: calc(100% + 6px); left: auto; right: 0;
    width: min(270px, calc(100vw - 24px));
    transform-origin: calc(100% - 20px) -8px;
  }
  /* Right-click menu: at the pointer, compact, above everything. */
  .menu.ctx-menu {
    position: fixed; right: auto; bottom: auto; z-index: 450;
    width: 200px; padding: 5px; border-radius: 14px;
    transform-origin: 0 0;
  }
  .ctx-menu .menu-item { padding: 6px 8px; gap: 10px; border-radius: 10px; }
  .ctx-menu .menu-ico { width: 28px; height: 28px; border-radius: 8px; }
  .ctx-menu .menu-ico .ico { width: 16px; height: 16px; }
  .menu-item.danger .menu-title { color: var(--ui-danger); }
  .menu-item.danger .menu-ico { background: var(--ui-danger-soft); color: var(--ui-danger); }
  .menu-sep { height: 1px; margin: 5px 12px; background: var(--ui-line); }

  /* ---------- inspector (sidebar on desktop, bottom sheet on phones) ---------- */
  .inspector {
    position: relative;
    width: clamp(330px, 27vw, 410px);
    flex: none;
    display: flex;
    flex-direction: column;
    background: var(--ui-pane);
    box-shadow: inset -1px 0 0 var(--ui-line);
    z-index: 20;
  }

  .nav { position: relative; flex: 1; min-height: 0; overflow: hidden; }
  .nav-pane {
    position: absolute; inset: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    transition: opacity .2s ease, transform .42s var(--ease-out), visibility 0s;
  }
  /* Tabs swap in place: the newest fades in over whatever it replaces. */
  .nav-pane[data-state="after"],
  .nav-pane[data-state="under"] { opacity: 0; visibility: hidden; transition-delay: 0s, 0s, .42s; }
  .nav-pane[data-state="active"] { opacity: 1; visibility: visible; }
  /* A screen pushed from a tab slides in from the right and leaves the same way; the one
     underneath drifts the other way and fades. */
  .nav-pane.child[data-state="after"] { opacity: 1; transform: translateX(100%); }
  .nav-pane[data-state="under"] { transform: translateX(-26%); }

  /* ---------- tab bar: a row at the top of the sidebar; a bottom bar on phones (below) ---------- */
  .tabbar {
    flex: none; display: flex; gap: 2px;
    padding: 10px 10px 8px;
    box-shadow: 0 1px 0 var(--ui-line);
  }
  .tab {
    flex: 1; min-width: 0;
    display: flex; flex-direction: column; align-items: center; gap: 3px;
    padding: 6px 2px 5px;
    border: 0; border-radius: 10px;
    background: transparent;
    color: var(--ui-text-2);
    font: inherit;
    cursor: pointer;
    transition: transform var(--press), color 150ms ease, background-color 150ms ease;
    -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  }
  .tab:active { transform: scale(.95); transition-duration: 60ms; }
  .tab .ico { width: 24px; height: 24px; }
  .tab-label { font-size: .6875rem; font-weight: 600; letter-spacing: 0; white-space: nowrap; }
  .tab[aria-current="page"] { color: var(--ui-accent-text); }
  @media (hover: hover) { .tab:not([aria-current="page"]):hover { background: var(--ui-surface-2); color: var(--ui-text); } }
  /* The preview is always visible beside the sidebar, so it has no tab there. */
  @media (min-width: 701px) and (min-height: 501px) { .tab[data-tab="preview"] { display: none; } }

  .detail-head {
    position: sticky; top: 0; z-index: 2;
    padding: 10px 14px 8px;
    background: var(--ui-pane);
  }
  /* A soft fade where content slides under the header, not a hard divider. */
  .detail-head::after {
    content: ""; position: absolute; left: 0; right: 0; top: 100%; height: 14px;
    background: linear-gradient(var(--ui-pane), transparent);
    pointer-events: none;
  }
  .back-btn {
    display: inline-flex; align-items: center; gap: 2px;
    margin: 0 0 0 -10px;
    padding: 6px 10px 6px 4px;
    border: 0; border-radius: 9px;
    background: transparent;
    color: var(--ui-accent-text);
    font: inherit; font-size: .9375rem; font-weight: 600;
    cursor: pointer;
    transition: transform var(--press), background-color 150ms ease;
  }
  .back-btn:active { transform: scale(.97); transition-duration: 60ms; }
  .back-btn .ico { width: 22px; height: 22px; }
  @media (hover: hover) { .back-btn:hover { background: var(--ui-accent-soft); } }
  /* Title on the left, the screen's "+" / "…" on the right, tinted like an iOS nav bar. */
  .head-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
  .head-actions { display: flex; align-items: center; gap: 2px; margin-right: -8px; }
  .head-actions .icon-btn { color: var(--ui-accent-text); }
  .head-actions .icon-btn .ico { width: 24px; height: 24px; }
  .detail-head h2 {
    margin: 4px 0 0;
    font-size: 1.5rem; font-weight: 750; line-height: 1.15; letter-spacing: -.022em;
  }
  .detail-body { padding: 8px 14px 32px; }
  .head-extra { display: flex; flex-direction: column; gap: 8px; margin-top: 10px; }
  .head-extra:empty { display: none; }

  /* ---------- drag and drop ---------- */
  /* The subject tray: docked above the timetable, so what you drag from and what you
     drag onto are always on screen together. */
  .tray { flex: 1 1 0; min-width: 0; padding: 0; }
  .tray[hidden] { display: none; }
  .tray-hint { font-size: .75rem; line-height: 1.35; color: var(--ui-text-2); margin: 0 0 4px; }
  .chip-row {
    display: flex; gap: 8px;
    overflow-x: auto; overscroll-behavior-x: contain;
    margin: 0; padding: 2px 0 4px;
    scrollbar-width: none;
  }
  .chip-row::-webkit-scrollbar { display: none; }
  .chip-wrap { position: relative; }
  .chip-arrow {
    position: absolute; top: 50%; transform: translateY(-56%); z-index: 2;
    width: 32px; height: 32px;
    display: inline-flex; align-items: center; justify-content: center;
    border: 0; border-radius: 50%;
    background: var(--ui-surface); color: var(--ui-text);
    box-shadow: 0 0 0 1px var(--ui-line), 0 6px 16px -4px rgba(0,0,0,.4);
    cursor: pointer;
    transition: transform var(--press), background-color 150ms ease;
  }
  .chip-arrow[hidden] { display: none; }
  .chip-arrow.left { left: 4px; }
  .chip-arrow.right { right: 4px; }
  .chip-arrow:active { transform: translateY(-56%) scale(.94); transition-duration: 60ms; }
  .chip-arrow .ico { width: 18px; height: 18px; }
  @media (hover: hover) {
    .chip-arrow:hover { background: var(--ui-surface-2); }
    /* with a mouse there's room for a real, thin scrollbar as well */
    .chip-row { scrollbar-width: thin; scrollbar-color: var(--ui-text-2) transparent; padding-bottom: 8px; }
    .chip-row::-webkit-scrollbar { display: block; height: 5px; }
    .chip-row::-webkit-scrollbar-thumb { background: color-mix(in srgb, var(--ui-text-2) 55%, transparent); border-radius: 3px; }
  }
  @media (hover: none) { .chip-arrow { display: none; } }
  .subject-chip {
    flex: none;
    display: inline-flex; flex-direction: row; align-items: center; gap: 7px;
    height: 38px; padding: 3px 13px 3px 4px;
    border: 1px solid var(--ui-line); border-radius: 999px;
    background: var(--ui-surface); color: var(--ui-text);
    font: inherit; cursor: grab;
    -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
    touch-action: pan-x;
    transition: transform var(--press), background-color 150ms ease;
    -webkit-tap-highlight-color: transparent;
  }
  .subject-chip:active { transform: scale(.96); transition-duration: 60ms; }
  .subject-chip.armed { border-color: var(--ui-accent); box-shadow: 0 0 0 3px color-mix(in srgb, var(--ui-accent) 30%, transparent); background: var(--ui-accent-soft); }
  .chip-ico {
    width: 30px; height: 30px; border-radius: 50%;
    display: inline-flex; align-items: center; justify-content: center;
    font-size: 16px; box-shadow: inset 0 0 0 1px rgba(0,0,0,.08);
  }
  .chip-ico .ico { width: 16px; height: 16px; }
  .chip-name { max-width: 120px; font-size: .8125rem; font-weight: 650; line-height: 1.1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  @media (hover: hover) { .subject-chip:hover { background: var(--ui-surface-2); } }

  .chip-sep { flex: none; width: 1px; align-self: stretch; margin: 6px 2px; background: var(--ui-line); }
  .subject-chip.special .chip-ico { font-size: 14px; font-weight: 800; }
  .subject-chip.special:not(.armed) { background: var(--ui-surface-2); }
  .subject-chip.new { border-style: dashed; color: var(--ui-accent-text); cursor: pointer; touch-action: manipulation; }
  .subject-chip.new .chip-ico { background: color-mix(in srgb, var(--ui-accent) 18%, transparent); color: var(--ui-accent-text); box-shadow: none; }
  .trash {
    position: fixed; z-index: 900;
    display: flex; align-items: center; justify-content: center; gap: 10px;
    border-radius: 16px;
    background: color-mix(in srgb, var(--ui-danger) 14%, var(--ui-pane));
    border: 2px dashed color-mix(in srgb, var(--ui-danger) 60%, transparent);
    color: var(--ui-danger);
    font-size: .9375rem; font-weight: 700;
    pointer-events: auto;
  }
  .trash[hidden] { display: none; }
  .trash .ico { width: 22px; height: 22px; }
  .trash.dnd-over { outline: none; background: var(--ui-danger); color: #fff; border-style: solid; filter: none; }
  /* a bin beside each week's title (only when there's more than one week) */
  .page .week-del {
    display: inline-flex; align-items: center; justify-content: center; vertical-align: top;
    width: 38px; height: 38px; margin-left: 4px; padding: 0;
    border: 0; border-radius: 9px 9px 0 0;
    background: transparent; color: var(--muted);
    cursor: pointer;
    transition: background-color 150ms ease, color 150ms ease, transform var(--press);
  }
  .page .week-del .ico { width: 18px; height: 18px; }
  .page .week-del:hover, .page .week-del:focus-visible { background: color-mix(in srgb, var(--alert) 12%, #fff); color: var(--alert); }
  .page .week-del:active { transform: scale(.93); transition-duration: 60ms; }
  /* a tab beside the last week's title: add another week right here */
  .page .week-add {
    display: inline-block; vertical-align: top;
    margin-left: 6px; padding: 8px 16px;
    border: 2px dashed color-mix(in srgb, var(--accent) 55%, transparent); border-bottom: 0;
    border-radius: 9px 9px 0 0;
    background: transparent; color: var(--accent);
    font: inherit; font-size: 15px; font-weight: 800; letter-spacing: .02em;
    cursor: pointer;
    transition: background-color 150ms ease, transform var(--press);
  }
  .page .week-add span { font-size: 18px; line-height: 1; margin-right: 3px; vertical-align: -1px; }
  .page .week-add:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
  .page .week-add:active { transform: scale(.97); transition-duration: 60ms; }
  /* notes bar (screen only): switch a note on or off, add one; click a note to edit it */
  .page .notes-bar { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin: 8px 0 0; }
  .page .nb-label { font-size: 10.5px; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-right: 2px; }
  .page .note-pill, .page .note-add {
    display: inline-flex; align-items: center; gap: 6px;
    max-width: 100%; padding: 4px 11px;
    border: 1px solid var(--line); border-radius: 999px;
    background: #fff; color: var(--ink);
    font: inherit; font-size: 12px; font-weight: 700;
    cursor: pointer;
    transition: background-color 150ms ease, transform var(--press), border-color 150ms ease;
  }
  .page .note-pill:active, .page .note-add:active { transform: scale(.97); transition-duration: 60ms; }
  .page .np-dot { width: 9px; height: 9px; border-radius: 50%; border: 2px solid var(--muted); flex: none; }
  .page .note-pill.on { border-color: color-mix(in srgb, var(--accent) 55%, transparent); background: color-mix(in srgb, var(--accent) 9%, #fff); }
  .page .note-pill.on .np-dot { background: var(--accent); border-color: var(--accent); }
  .page .note-pill:not(.on) { color: var(--muted); border-style: dashed; }
  .page .note-add { border-style: dashed; color: var(--accent); }
  .page .note-pill:hover, .page .note-add:hover { background: color-mix(in srgb, var(--accent) 12%, #fff); }
  .page section.note[data-ni] { cursor: pointer; }
  .page section.note[data-ni]:hover { outline: 2px solid color-mix(in srgb, var(--accent) 45%, transparent); outline-offset: 1px; }
  /* period numbers: click to hide; the # in a day's header brings them back */
  .page .day-name { position: relative; }
  .page .period-toggle {
    position: absolute; left: 5px; top: 50%; transform: translateY(-50%);
    width: 24px; height: 24px; padding: 0;
    border: 0; border-radius: 7px;
    background: rgba(255,255,255,.16); color: #fff;
    font: 800 13px/1 inherit; letter-spacing: 0;
    cursor: pointer; opacity: 0;
    transition: opacity 150ms ease, background-color 150ms ease;
  }
  .page .days:hover .period-toggle { opacity: .6; }
  .page .period-toggle:hover, .page .period-toggle:focus-visible { opacity: 1; background: rgba(255,255,255,.32); }
  .page .period-toggle[aria-pressed="true"] { background: rgba(255,255,255,.3); }
  @media (hover: none) { .page .period-toggle { opacity: .55; } }
  .page .period-number { cursor: pointer; }
  .page .slot[data-si] { cursor: grab; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
  .dnd-active, .dnd-active * { cursor: grabbing !important; -webkit-user-select: none; user-select: none; }
  .dnd-ghost {
    position: fixed; left: 0; top: 0; z-index: 1000;
    margin: 0 !important;
    pointer-events: none;
    opacity: .96;
    border-radius: 12px; overflow: hidden;
    box-shadow: 0 18px 40px -8px rgba(0,0,0,.5), 0 0 0 2px var(--ui-accent);
    background: var(--ui-surface);
    will-change: transform;
  }
  .subject-chip.dnd-ghost { display: inline-flex; }
  .dnd-source { opacity: .35; }
  /* holding Shift / Option: copy, not move */
  .dnd-ghost.copying::after {
    content: "+"; position: absolute; top: 5px; right: 5px; z-index: 3;
    width: 24px; height: 24px; border-radius: 50%;
    background: #22a559; color: #fff;
    font: 800 18px/24px ui-sans-serif, system-ui, sans-serif; text-align: center;
    box-shadow: 0 2px 6px rgba(0,0,0,.35);
  }
  .dnd-copy, .dnd-copy * { cursor: copy !important; }
  .dnd-over.copy { outline: 3px dashed #22a559; background-image: none; }
  .row-actions { display: flex; flex-wrap: wrap; gap: 8px; }
  .dnd-over { outline: 3px solid var(--ui-accent); outline-offset: -3px; filter: brightness(1.06); }
  .lrow.dnd-over { outline-offset: -2px; }
  .dnd-line {
    display: none; position: fixed; z-index: 999; height: 4px; border-radius: 2px;
    background: var(--ui-accent);
    box-shadow: 0 0 0 3px color-mix(in srgb, var(--ui-accent) 30%, transparent);
    pointer-events: none;
  }
  .seg.full { display: grid; width: 100%; }
  .seg.compact button { padding: 0 4px; }

  /* ---------- preview canvas ---------- */
  .canvas { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  .canvas-bar {
    flex: none;
    display: flex; align-items: center; flex-wrap: nowrap; gap: 10px 14px;
    padding: 10px max(20px, env(safe-area-inset-right)) 6px 20px;
  }
  #pdfStatus { flex: none; max-width: 40%; font-size: .78rem; color: var(--ui-text-2); }
  #pdfStatus:empty { display: none; }
  .canvas-scroll {
    flex: 1; min-height: 0;
    overflow: auto;
    padding: 6px max(20px, env(safe-area-inset-right)) 28px 20px;
    overscroll-behavior: contain;
  }

  .zoom {
    display: inline-flex; align-items: center; gap: 2px;
    padding: 3px;
    border-radius: 12px;
    background: var(--ui-surface-2);
  }
  .zoom-label {
    min-width: 50px; height: 30px;
    border: 0; border-radius: 9px; background: transparent;
    color: var(--ui-text);
    font: inherit; font-size: .78rem; font-weight: 650; font-variant-numeric: tabular-nums;
    cursor: pointer;
  }
  @media (hover: hover) { .zoom-label:hover { background: var(--ui-surface-3); } }

  /* Segmented control with a sliding thumb */
  .seg {
    --n: 2; --i: 0;
    position: relative;
    display: inline-grid;
    grid-auto-flow: column;
    grid-auto-columns: 1fr;
    padding: 3px;
    border-radius: 12px;
    background: var(--ui-surface-2);
  }
  .seg-thumb {
    position: absolute; top: 3px; bottom: 3px; left: 3px;
    width: calc((100% - 6px) / var(--n));
    border-radius: 9px;
    background: var(--ui-surface);
    box-shadow: 0 1px 3px rgba(0,0,0,.2), 0 0 0 .5px var(--ui-line);
    transform: translateX(calc(var(--i) * 100%));
    transition: transform .38s var(--ease-out);
  }
  .seg button {
    position: relative; z-index: 1;
    height: 30px;
    padding: 0 14px;
    border: 0; background: transparent;
    border-radius: 9px;
    color: var(--ui-text-2);
    font: inherit; font-size: .8125rem; font-weight: 650; white-space: nowrap;
    overflow: hidden; text-overflow: ellipsis;
    cursor: pointer;
  }
  .seg button[aria-checked="true"] { color: var(--ui-text); }

  /* The preview scrolls sideways instead of squashing five day columns.
     Scoped to .page so the off-screen PDF card (.sheet) is never affected. */
  .page .days {
    grid-template-columns: repeat(5, minmax(118px, 1fr));
    overflow-x: auto;
    scroll-snap-type: x proximity;
    overscroll-behavior-x: contain;
    -webkit-overflow-scrolling: touch;
  }
  .page .day { scroll-snap-align: start; }
  /* A column is as wide as five days need, so a week shows whole; two weeks only sit
     side by side when there is room, otherwise Week B stacks below. */
  .page .weeks { grid-template-columns: repeat(auto-fit, minmax(min(650px, 100%), 1fr)); }
  /* An empty or nearly empty day still needs somewhere to drop things. */
  .page .day { min-height: 320px; }

  /* ---------- form language ---------- */
  .f-section { margin: 0 0 18px; }
  .f-section-title {
    margin: 0 0 8px;
    font-size: .6875rem; font-weight: 700;
    text-transform: uppercase; letter-spacing: .07em;
    color: var(--ui-text-2);
  }
  .f-hint { margin: 0 0 10px; font-size: .75rem; line-height: 1.45; color: var(--ui-text-2); }
  .f-row { margin-bottom: 10px; }
  .f-label {
    display: block;
    margin-bottom: 4px;
    font-size: .6875rem; font-weight: 650;
    text-transform: uppercase; letter-spacing: .06em;
    color: var(--ui-text-2);
  }
  .f-input, .f-select {
    width: 100%;
    padding: 8px 10px;
    border: 1px solid var(--ui-line);
    border-radius: 10px;
    background: var(--ui-input);
    color: var(--ui-text);
    font: inherit; font-size: .875rem;
    transition: border-color 150ms ease, box-shadow 150ms ease;
  }
  .f-input:focus, .f-select:focus {
    outline: none;
    border-color: var(--ui-accent);
    box-shadow: 0 0 0 3px var(--ui-accent-soft);
  }
  @media (pointer: coarse) { .f-input, .f-select { font-size: 16px; } }

  .f-inline { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
  .f-inline .f-input { width: auto; flex: 1; min-width: 0; }
  .f-inline input[type="color"] {
    width: 34px; height: 34px; padding: 2px; flex: none;
    border: 1px solid var(--ui-line); border-radius: 9px;
    background: var(--ui-input);
    cursor: pointer;
  }
  .f-check { display: inline-flex; align-items: center; gap: 6px; flex: none; font-size: .8125rem; color: var(--ui-text); }
  .f-check input, .f-range-row input { accent-color: var(--ui-accent); }
  .f-day-row { flex-wrap: wrap; row-gap: 6px; }
  .f-inline.f-day-row .f-input { min-width: 110px; }

  .f-card {
    margin-bottom: 8px;
    padding: 10px;
    border: 1px solid var(--ui-line);
    border-radius: 12px;
    background: var(--ui-surface);
  }
  .f-card-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 8px; }
  .f-card-head .f-select { flex: 1; width: auto; }
  .f-btn-row { display: flex; gap: 5px; flex-wrap: wrap; flex: none; }

  .f-mini-btn {
    min-height: 30px;
    padding: 4px 11px;
    border: 0; border-radius: 9px;
    background: var(--ui-surface-2);
    color: var(--ui-text);
    font: inherit; font-size: .75rem; font-weight: 650;
    cursor: pointer;
  }
  .f-mini-btn.danger { background: var(--ui-danger-soft); color: var(--ui-danger); }
  @media (hover: hover) { .f-mini-btn:hover { filter: brightness(.96); } }
  @media (pointer: coarse) { .f-mini-btn { min-height: 38px; padding: 6px 13px; } }

  .f-day {
    margin-bottom: 8px;
    padding: 0 12px;
    border: 1px solid var(--ui-line);
    border-radius: 12px;
    background: var(--ui-surface);
  }
  .f-day > summary {
    cursor: pointer;
    padding: 10px 0;
    font-size: .75rem; font-weight: 700; letter-spacing: .04em;
    color: var(--ui-text);
  }
  .f-day[open] > summary { margin-bottom: 2px; }
  .f-day-body { padding-bottom: 12px; }
  .f-slot-list { margin-bottom: 8px; }
  .f-grid { display: flex; flex-wrap: wrap; gap: 8px; }
  .f-grid > .f-row { margin-bottom: 8px; min-width: 0; }

  .f-range-row { display: flex; align-items: center; gap: 10px; }
  .f-range-row input[type="range"] { flex: 1; }
  .f-range-row .f-range-out {
    min-width: 42px; text-align: right;
    font-size: .78rem; font-weight: 700; font-variant-numeric: tabular-nums;
    color: var(--ui-text);
  }

  .btn-quiet, .btn-danger {
    display: inline-flex; align-items: center; justify-content: center; gap: 6px;
    padding: 9px 14px;
    border: 0; border-radius: 10px;
    background: var(--ui-surface-2);
    color: var(--ui-text);
    font: inherit; font-size: .8125rem; font-weight: 650;
    cursor: pointer;
  }
  .btn-danger { background: var(--ui-danger-soft); color: var(--ui-danger); }
  @media (hover: hover) { .btn-quiet:hover, .btn-danger:hover { filter: brightness(.96); } }
  .btn-row { display: flex; flex-wrap: wrap; gap: 8px; }
  a.btn-quiet { text-decoration: none; }

  /* One "add something" row for every screen: a dashed list row, not a filled button. */
  .btn-add {
    display: flex; align-items: center; justify-content: flex-start; gap: 10px;
    width: 100%; min-height: 44px; padding: 10px 14px;
    border: 1px dashed color-mix(in srgb, var(--ui-accent) 45%, var(--ui-line)); border-radius: 13px;
    background: transparent; color: var(--ui-accent-text);
    font: inherit; font-size: .9375rem; font-weight: 650; text-align: left;
    cursor: pointer;
    transition: transform var(--press), background-color 150ms ease;
    -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  }
  .btn-add:active { transform: scale(.98); transition-duration: 60ms; }
  @media (hover: hover) { .btn-add:hover { background: var(--ui-accent-soft); } }
  .f-colour {
    width: 100%; height: 36px; padding: 2px;
    border: 1px solid var(--ui-line); border-radius: 10px;
    background: var(--ui-input); cursor: pointer;
  }

  /* emoji picker: a small popover that grows out of its button */
  .f-emoji { position: relative; display: flex; align-items: center; gap: 4px; flex: none; }
  .f-emoji-input { width: 46px !important; flex: none !important; text-align: center; font-size: 15px; padding: 6px !important; }
  .f-emoji-toggle { padding: 4px 8px; font-size: 14px; }
  .f-emoji-backdrop {
    position: fixed; inset: 0; z-index: 250;
    background: transparent;
    visibility: hidden; pointer-events: none;
    transition: visibility 0s linear .16s;
  }
  .f-emoji-backdrop.open { visibility: visible; pointer-events: auto; transition: visibility 0s; }
  .f-emoji-modal {
    position: fixed;
    padding: 6px;
    border-radius: 14px;
    background: var(--ui-surface);
    box-shadow: 0 0 0 1px var(--ui-line), 0 18px 44px -8px rgba(0,0,0,.45);
    opacity: 0; transform: scale(.92);
    transition: opacity .14s ease, transform .16s var(--ease-in);
  }
  .f-emoji-backdrop.open .f-emoji-modal { opacity: 1; transform: none; transition: opacity .18s ease, transform .3s var(--ease-out); }
  emoji-picker {
    --num-columns: 8;
    --background: var(--ui-surface);
    --border-color: var(--ui-line);
    --input-border-color: var(--ui-line);
    --input-font-color: var(--ui-text);
    --button-hover-background: var(--ui-surface-2);
    --button-active-background: var(--ui-surface-3);
    --indicator-color: var(--ui-accent);
    --outline-color: var(--ui-accent);
    max-width: 88vw; height: 360px; max-height: 70vh;
  }

  /* ---------- lesson rows: tap to unfold, grip to reorder, swipe to delete ---------- */
  .lesson-list { position: relative; display: flex; flex-direction: column; }
  .lrow {
    position: relative;
    margin-bottom: 6px;
    border: 1px solid var(--ui-line);
    border-radius: 13px;
    background: var(--ui-surface);
    overflow: hidden;
  }
  .lrow-del {
    position: absolute; inset: 0;
    display: flex; align-items: center; justify-content: flex-end;
    padding-right: 20px;
    background: var(--ui-danger);
    color: #fff; font-size: .8125rem; font-weight: 700;
    opacity: 0;
    pointer-events: none;   /* purely visual: it must never sit over the editor */
  }
  .lrow-face {
    position: relative;
    display: flex; align-items: center;
    background: var(--ui-surface);
    touch-action: pan-y;
  }
  .lrow-grip {
    flex: none; width: 38px; height: 54px;
    display: flex; align-items: center; justify-content: center;
    border: 0; background: transparent;
    color: var(--ui-text-2);
    cursor: grab; touch-action: none;
  }
  .lrow-grip .ico { width: 18px; height: 18px; }
  .lrow-face.no-grip .lrow-toggle { padding-left: 14px; }
  /* a subject row's icon tile is what you pick up and drag onto the timetable (wide screens) */
  @media (min-width: 701px) and (min-height: 501px) {
    .lrow[data-drag-subject] .lrow-chip { cursor: grab; transition: transform var(--press), box-shadow 150ms ease; }
    .lrow[data-drag-subject] .lrow-chip:hover { transform: scale(1.08); box-shadow: inset 0 0 0 1px rgba(0,0,0,.08), 0 4px 10px -2px rgba(0,0,0,.35); }
    .lrow[data-drag-subject] .lrow-chip:active { cursor: grabbing; transform: scale(1.02); }
  }
  .lrow-toggle {
    flex: 1; min-width: 0; min-height: 54px;
    display: flex; align-items: center; gap: 10px;
    padding: 8px 12px 8px 0;
    border: 0; background: transparent;
    color: inherit; font: inherit; text-align: left;
    cursor: pointer;
  }
  .lrow-chip {
    width: 34px; height: 34px; flex: none;
    display: inline-flex; align-items: center; justify-content: center;
    border-radius: 10px; font-size: 17px; font-weight: 700; color: var(--ui-text);
    box-shadow: inset 0 0 0 1px rgba(0,0,0,.08);
  }
  .lrow-text { flex: 1; min-width: 0; display: flex; flex-direction: column; }
  .lrow-title { font-size: .875rem; font-weight: 650; letter-spacing: -.005em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .lrow-sub { font-size: .75rem; color: var(--ui-text-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  /* One-click delete for mouse users: on every row, shown on hover or keyboard focus. Touch has swipe. */
  .lrow-quick {
    display: none;
    flex: none; width: 40px; height: 40px; margin-right: 6px;
    align-items: center; justify-content: center;
    border: 0; border-radius: 11px; background: transparent;
    color: var(--ui-text-2); cursor: pointer;
    transition: opacity 150ms ease, transform var(--press), background-color 150ms ease, color 150ms ease;
  }
  .lrow-quick .ico { width: 18px; height: 18px; }
  .lrow-quick:active { transform: scale(.92); transition-duration: 60ms; }
  @media (hover: hover) and (pointer: fine) {
    .lrow-quick { display: inline-flex; opacity: 0; }
    .lrow:hover .lrow-quick, .lrow:focus-within .lrow-quick { opacity: 1; }
    .lrow-quick:hover { background: var(--ui-danger-soft); color: var(--ui-danger); }
  }
  .lrow-chev { color: var(--ui-text-2); display: inline-flex; transition: transform .3s var(--ease-out); }
  .lrow.open .lrow-chev { transform: rotate(90deg); }

  .lrow-edit { position: relative; background: var(--ui-surface); display: grid; grid-template-rows: 0fr; transition: grid-template-rows .32s var(--ease-out); }
  .lrow.open .lrow-edit { grid-template-rows: 1fr; box-shadow: inset 0 1px 0 var(--ui-line); }
  .lrow-edit-in { overflow: hidden; min-height: 0; visibility: hidden; transition: visibility 0s linear .32s; }
  .lrow.open .lrow-edit-in { visibility: visible; transition: visibility 0s; }
  .lrow-edit-pad { padding: 12px 12px 12px; }

  /* while one is being dragged */
  .lesson-list.reordering .lrow-edit { transition: none; grid-template-rows: 0fr; }
  .lesson-list.reordering .lrow { transition: transform .22s var(--ease-out); }
  .lesson-list.reordering .lrow.lifting {
    transition: none; z-index: 5;
    box-shadow: var(--ui-shadow);
  }
  .lesson-list.reordering .lrow.settling { transition: transform .26s var(--ease-out); z-index: 5; }
  .btn-clear {
    display: inline-flex; align-items: center; justify-content: center;
    min-height: 34px; padding: 6px 13px;
    border: 0; border-radius: 10px;
    background: var(--ui-danger-soft); color: var(--ui-danger);
    font: inherit; font-size: .8125rem; font-weight: 700;
    cursor: pointer;
    transition: transform var(--press), filter 150ms ease;
    -webkit-tap-highlight-color: transparent; touch-action: manipulation;
  }
  .btn-clear:active { transform: scale(.97); transition-duration: 60ms; }
  @media (hover: hover) { .btn-clear:hover { filter: brightness(.95); } }
  @media (pointer: coarse) { .btn-clear { min-height: 40px; } }
  .clear-row { margin-top: 8px; }
  .f-section.wipe { margin-top: 26px; }
  .apply-row { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
  .apply-row .apply-btn { justify-content: flex-start; text-align: left; color: var(--ui-accent-text); }
  .apply-row .apply-btn[hidden] { display: none; }
  /* Add buttons sit in a tidy grid: one fills the row, four make 2 × 2. */
  .add-row { margin-top: 6px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
  .add-row > .btn-add:only-child { grid-column: 1 / -1; }

  /* ---------- raw JSON ---------- */
  /* the raw-data editor takes the place of the timetable */
  .canvas { position: relative; }
  .canvas.data-open > .canvas-bar, .canvas.data-open > .canvas-scroll { display: none; }
  .dataview {
    position: absolute; inset: 0;
    display: flex; flex-direction: column; gap: 10px;
    padding: 14px max(20px, env(safe-area-inset-right)) 20px 20px;
    background: var(--ui-pane);
    z-index: 5;
  }
  .dataview[hidden] { display: none; }
  .dv-head { flex: none; display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
  .dv-title h2 { margin: 0; font-size: 1.25rem; font-weight: 750; letter-spacing: -.02em; }
  .dv-title p { margin: 2px 0 0; font-size: .8rem; color: var(--ui-text-2); }
  .dv-actions { display: flex; align-items: center; gap: 8px; }
  .dv-actions .btn-primary { margin-left: 4px; height: 36px; }
  .dv-editor { flex: 1; min-height: 0; }
  .dv-editor .CodeMirror { height: 100%; }
  .dataview .editor-error { flex: none; margin: 0; }
  .editor-error { display: flex; align-items: flex-start; gap: 10px; }
  .editor-error[hidden] { display: none; }
  .editor-error .err-actions { flex: none; margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
  .editor-error .err-fix, .editor-error .err-go {
    padding: 4px 11px; border: 0; border-radius: 8px; cursor: pointer;
    font: inherit; font-size: .75rem; font-weight: 700; white-space: nowrap;
  }
  .editor-error .err-fix { background: var(--ui-accent-fill); color: #fff; }
  .editor-error .err-go { background: transparent; color: var(--ui-danger); box-shadow: inset 0 0 0 1.5px currentColor; }
  /* the mistake, in the editor */
  .cm-json-error { background: color-mix(in srgb, var(--ui-danger) 35%, transparent); border-bottom: 2px solid var(--ui-danger); border-radius: 2px; }
  .cm-json-error-line { background: color-mix(in srgb, var(--ui-danger) 12%, transparent); }
  @media (max-width: 700px), (max-height: 500px) {
    .dataview { position: fixed; left: 0; right: 0; bottom: 0; top: 58px; z-index: 65; padding: 12px 14px calc(14px + env(safe-area-inset-bottom)); }
    .dv-actions .btn-primary { margin-left: 0; }
  }
  /* iOS zooms into any focused field under 16px */
  .CodeMirror textarea { font-size: 16px !important; }
  .CodeMirror {
    height: 100%;
    border: 1px solid var(--ui-line);
    border-radius: 12px;
    background: var(--ui-input);
    color: var(--ui-text);
    font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
    font-size: 12.5px; line-height: 1.55;
  }
  .CodeMirror-gutters { background: var(--ui-surface-2); border-right: 1px solid var(--ui-line); }
  .CodeMirror-linenumber { color: var(--ui-text-2); }
  .CodeMirror-cursor { border-left: 1.5px solid var(--ui-text); }
  .CodeMirror-focused .CodeMirror-selected, .CodeMirror-selected { background: var(--ui-accent-soft); }
  .cm-s-default .cm-string { color: var(--cm-string); }
  .cm-s-default .cm-number { color: var(--cm-number); }
  .cm-s-default .cm-property { color: var(--cm-prop); }
  .cm-s-default .cm-atom, .cm-s-default .cm-keyword { color: var(--cm-atom); }
  .cm-s-default .CodeMirror-matchingbracket { color: inherit; background: var(--ui-accent-soft); outline: 1px solid var(--ui-accent); }
  .CodeMirror-focused { border-color: var(--ui-accent); box-shadow: 0 0 0 3px var(--ui-accent-soft); }

  .editor-error {
    margin: 10px 0 0;
    padding: 10px 12px;
    border-radius: 10px;
    background: var(--ui-danger-soft);
    color: var(--ui-danger);
    font-size: .75rem; line-height: 1.45;
  }
  .data-error {
    margin: 24px auto 0;
    max-width: 640px;
    padding: 18px 22px;
    border-radius: 14px;
    background: var(--ui-danger-soft);
    color: var(--ui-text);
    box-shadow: inset 0 0 0 1px var(--ui-danger);
    font-size: .875rem; line-height: 1.5;
  }
  .data-error code { background: rgba(127,127,127,.18); padding: 1px 5px; border-radius: 4px; }

  /* ---------- dialog + toast: modal and transient surfaces ---------- */
  .dialog-backdrop {
    position: fixed; inset: 0; z-index: 300;
    display: flex; align-items: center; justify-content: center;
    padding: 16px;
    background: var(--ui-scrim);
    -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px);
    opacity: 0; visibility: hidden;
    transition: opacity .2s ease, visibility 0s linear .2s;
  }
  .dialog-backdrop.open { opacity: 1; visibility: visible; transition: opacity .2s ease, visibility 0s; }
  .dialog {
    width: min(380px, 100%);
    padding: 22px;
    border-radius: 18px;
    background: var(--ui-surface);
    color: var(--ui-text);
    box-shadow: 0 0 0 1px var(--ui-line), 0 28px 70px -14px rgba(0,0,0,.55);
    transform: scale(.94);
    transition: transform .2s var(--ease-in);
  }
  .dialog-backdrop.open .dialog { transform: none; transition: transform .35s var(--ease-out); }
  .dialog h3 { margin: 0 0 6px; font-size: 1.0625rem; letter-spacing: -.015em; }
  .dialog p { margin: 0 0 18px; font-size: .875rem; line-height: 1.5; color: var(--ui-text-2); }
  .dialog-actions { display: flex; gap: 8px; justify-content: flex-end; }
  .dialog-actions button {
    padding: 9px 16px;
    border: 0; border-radius: 10px;
    background: var(--ui-surface-2); color: var(--ui-text);
    font: inherit; font-size: .8125rem; font-weight: 650;
    cursor: pointer;
  }
  .dialog-actions button.primary { background: var(--ui-accent-fill); color: #fff; }
  .dialog-actions button.danger { background: var(--ui-danger); color: #fff; }

  .toast {
    position: fixed; left: 0; right: 0; margin: 0 auto; width: max-content;
    bottom: max(18px, env(safe-area-inset-bottom));
    z-index: 400;
    display: flex; align-items: center; gap: 12px;
    max-width: min(440px, calc(100vw - 32px));
    padding: 10px 12px 10px 16px;
    border-radius: 16px;
    background: rgba(18,26,48,.9);
    -webkit-backdrop-filter: blur(24px) saturate(150%); backdrop-filter: blur(24px) saturate(150%);
    box-shadow: inset 0 1px 0 rgba(255,255,255,.18), 0 14px 34px -8px rgba(0,0,0,.55);
    color: #fff;
    font-size: .8125rem; font-weight: 500;
    opacity: 0; visibility: hidden;
    transform: translateY(16px) scale(.97);
    transition: opacity .2s ease, transform .2s var(--ease-in), visibility 0s linear .2s;
  }
  .toast.open { opacity: 1; visibility: visible; transform: none; transition: opacity .2s ease, transform .4s var(--ease-out), visibility 0s; }
  .toast button {
    padding: 6px 12px;
    border: 0; border-radius: 9px;
    background: rgba(255,255,255,.18); color: #fff;
    font: inherit; font-weight: 700;
    cursor: pointer;
  }
  .toast :focus-visible { outline-color: #fff; }

  /* ---------- phones & narrow windows: full screens + a bottom tab bar ---------- */
  @media (max-width: 700px), (max-height: 500px) {
    .topbar { gap: 8px; padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
    .brand-name { display: none; }
    .save-state { margin-left: 0; }
    .lbl-long, .save-long { display: none; }
    .lbl-short, .save-short { display: inline; }
    .btn-primary { padding: 0 14px; margin-left: 4px; }

    /* One screen at a time: the preview, or the editor for the selected tab. */
    .workspace { position: relative; }
    .inspector { width: auto; flex: 1; min-width: 0; box-shadow: none; }
    .app[data-screen="preview"] .inspector { display: none; }
    .app[data-screen="edit"] .canvas { display: none; }

    /* A translucent layer floating over the content, like the top bar: screens scroll
       underneath it (they carry --tabbar-h of bottom padding so the end stays reachable). */
    .app { --tabbar-h: calc(58px + env(safe-area-inset-bottom)); }
    .tabbar {
      display: flex;
      position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
      padding: 6px max(6px, env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
      background: var(--ui-chrome);
      -webkit-backdrop-filter: blur(24px) saturate(150%);
      backdrop-filter: blur(24px) saturate(150%);
      box-shadow: inset 0 1px 0 var(--ui-chrome-edge), 0 -1px 0 var(--ui-line);
      gap: 2px;
    }
    .nav-pane { padding-bottom: calc(var(--tabbar-h) + 16px); }
    .tab { padding: 5px 2px 3px; }
    .tab .ico { width: 25px; height: 25px; }
    .detail-head { padding-top: 14px; }
    .detail-head h2 { margin-top: 0; font-size: 1.75rem; }

    .toast { bottom: calc(var(--tabbar-h) + 14px); }
    .canvas-bar { padding: 10px 14px 4px; }
    .canvas-scroll { padding: 4px 12px calc(var(--tabbar-h) + 16px); }
    .page .lesson .subject { font-size: 1rem; }
  }
  @media (max-width: 700px), (max-height: 500px) {
    .tray, .trash { display: none !important; }
    .page .slot[data-si] { cursor: pointer; }
  }
  @media (max-width: 600px) {
    .menu { position: fixed; left: 12px; right: 12px; top: 66px; width: auto; transform-origin: 85% -8px; }
  }
  @media (max-width: 420px) {
    .brand { display: none; }
    .lbl-file { display: none; }
    .zoom { display: none; }
  }

  /* ---------- accessibility preferences ---------- */
  @media (prefers-reduced-motion: reduce) {
    .nav-pane { transition-property: opacity, visibility; transition-duration: .2s, 0s; }
    .nav-pane[data-state], .nav-pane.child[data-state] { transform: none; }
    .nav-pane.child[data-state="after"] { opacity: 0; }
    .seg-thumb { transition: none; }
    .lrow-edit, .lrow-chev, .lesson-list.reordering .lrow, .lesson-list.reordering .lrow.settling { transition: none; }
    .menu, .menu.open { transform: none !important; }
    .toast, .toast.open, .dialog, .dialog-backdrop.open .dialog,
    .f-emoji-modal, .f-emoji-backdrop.open .f-emoji-modal { transform: none !important; }
    .icon-btn:active:not(:disabled), .btn-primary:active:not(:disabled), .tab:active,
    .f-mini-btn:active, .zoom-label:active, .btn-quiet:active,
    .btn-danger:active, .dialog-actions button:active, .toast button:active { transform: none; }
    .btn-primary.busy::before { animation-duration: 2.4s; }
    .page .days { scroll-behavior: auto; }
  }

  /* Frosted becomes solid */
  @media (prefers-reduced-transparency: reduce) {
    .menu { -webkit-backdrop-filter: none; backdrop-filter: none; }
    .topbar, .inspector, .detail-head { background: var(--ui-surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
    .tabbar { background: var(--ui-surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
    .toast { background: #121a30; -webkit-backdrop-filter: none; backdrop-filter: none; }
    .dialog-backdrop { -webkit-backdrop-filter: none; backdrop-filter: none; }
  }

  @media (prefers-contrast: more) {
    :root, :root[data-theme] { --ui-line: currentColor; }
    .menu { -webkit-backdrop-filter: none; backdrop-filter: none; }
    .topbar, .inspector, .detail-head { background: var(--ui-surface); -webkit-backdrop-filter: none; backdrop-filter: none; }
    .f-card, .f-day, .f-input, .f-select, .f-mini-btn, .btn-quiet, .btn-danger { border: 1px solid currentColor; }
    .toast { background: #000; border: 1px solid #fff; }
    .dialog { border: 1px solid currentColor; }
  }

  /* ---------- print: just the timetable ---------- */
  @media print {
    body { overflow: visible; background: #fff; }
    .topbar, .inspector, .canvas-bar, .toast, .dialog-backdrop, .dataview { display: none !important; }
    .app, .workspace, .canvas, .canvas-scroll { display: block; height: auto; overflow: visible; padding: 0; background: none; }
    .page { box-shadow: none; border-radius: 0; }
    .weeks { zoom: 1 !important; }
  }

  /* ---------- a tablet held upright: the sidebar is a drawer over the timetable ---------- */
  .scrim, .rotate-note { display: none; }
  @media (min-width: 701px) and (max-width: 900px) and (min-height: 501px) {
    .workspace { position: relative; }
    /* the same tab bar as a phone, always docked: a tab opens the drawer on that screen */
    .app { --tabbar-h: calc(58px + env(safe-area-inset-bottom)); }
    .tabbar {
      display: flex;
      position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
      padding: 6px max(6px, env(safe-area-inset-right)) calc(4px + env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left));
      background: var(--ui-chrome);
      -webkit-backdrop-filter: blur(24px) saturate(150%);
      backdrop-filter: blur(24px) saturate(150%);
      box-shadow: inset 0 1px 0 var(--ui-chrome-edge), 0 -1px 0 var(--ui-line);
      gap: 2px;
    }
    .tab { padding: 5px 2px 3px; }
    .tab .ico { width: 25px; height: 25px; }
    .tab[data-tab="preview"] { display: flex; }
    .canvas-scroll { padding-bottom: calc(var(--tabbar-h) + 16px); }
    .toast { bottom: calc(var(--tabbar-h) + 14px); }
    .brand-name, .lbl-long, .save-long { display: none; }
    .lbl-short, .save-short { display: inline; }
    .inspector {
      position: absolute; left: 0; top: 0; bottom: var(--tabbar-h); z-index: 40;
      width: min(380px, 88vw);
      transform: translateX(-102%);
      visibility: hidden;
      transition: transform .4s var(--ease-out), visibility 0s .4s;
      box-shadow: none;
    }
    .app[data-drawer="open"] .inspector {
      transform: none; visibility: visible;
      transition: transform .4s var(--ease-out), visibility 0s;
      box-shadow: inset 0 0 0 1px var(--ui-line), 0 0 60px -10px rgba(0,0,0,.45);
    }
    .scrim { position: absolute; inset: 0 0 var(--tabbar-h) 0; z-index: 35; background: rgba(0,0,0,.28); opacity: 0; pointer-events: none; transition: opacity .3s ease; }
    .app[data-drawer="open"] .scrim { display: block; opacity: 1; pointer-events: auto; }
    .scrim { display: block; }
  }
  @media (prefers-reduced-motion: reduce) { .inspector { transition: none !important; } }

  /* A phone on its side has no room for the editor: ask for portrait (it can be dismissed). */
  @media (pointer: coarse) and (orientation: landscape) and (max-height: 500px) {
    html:not(.rotate-ok) .rotate-note {
      position: fixed; inset: 0; z-index: 200;
      display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
      background: var(--ui-bg); color: var(--ui-text); text-align: center; padding: 20px;
    }
    .rotate-note p { margin: 0; }
  }

  /* Narrow windows: the PDF status floats under the bar instead of squeezing the subject tray. */
  @media (max-width: 900px) {
    .canvas-bar { position: relative; }
    #pdfStatus {
      position: absolute; left: 20px; top: calc(100% - 2px); z-index: 6; max-width: calc(100% - 40px);
      padding: 4px 12px; border-radius: 999px;
      background: var(--ui-surface); box-shadow: 0 0 0 1px var(--ui-line), 0 6px 18px -8px rgba(0,0,0,.35);
    }
  }
