/* The Theater Papers: chapter sidebar.
   One element, the page's #chapterDrawer, in two models, split at 1024px
   (the Inventory SaaS sidebar's model; see assets/DESIGN.md):
   - 1024px and up: a persistent sidebar that reserves its width beside the
     text, in one of three modes picked from its bottom-left control menu
     (Supabase's): Expanded, Collapsed (a rail of chapter numbers, names as
     tooltips), or Expand on hover (a rail that peeks open over the text,
     without moving it, while hovered or tabbed into).
   - Below: the off-canvas drawer the pages always had (hamburger,
     backdrop), plus a grab handle on the left edge you can drag or tap.
   sidebar.js sets data-tp-sb="expanded|collapsed|hover" on <html> before
   first paint and keeps the classes below in step. */

/* ---- Both models ---- */
.sb-icon { flex: none; display: inline-flex; align-items: center; justify-content: center; }
.sb-icon svg { display: block; }
.sb-mark {
  flex: none; display: inline-flex; align-items: center; justify-content: center;
  width: 2.25rem; height: 2.25rem; border-radius: var(--r-md);
  background: color-mix(in srgb, var(--accent) 13%, transparent); color: var(--accent);
}
.drawer-header { justify-content: flex-start; gap: 0.75rem; }
.drawer-header-title { flex: 1 1 auto; min-width: 0; }
.drawer-home-link a { display: inline-flex; align-items: center; gap: 0.45rem; }
.drawer-toc a[aria-current] { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
.drawer-toc a[aria-current] .toc-num { color: var(--accent); }
.drawer-toc a:focus-visible, .drawer-home-link a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: -2px;
}
.sb-control, .sb-handle { display: none; }

/* ---- Below 1024px: off-canvas drawer + edge handle ---- */
@media (max-width: 1023.98px) {
  /* A tab-height grab bar at the left edge, centered vertically rather than
     full height, so a scroll gesture that starts near the edge still
     scrolls the page. Pointer only: the hamburger is the keyboard path. */
  .sb-handle {
    display: block; position: fixed; top: 50%; left: 0; z-index: 121;
    width: 1.1rem; height: 7rem; margin-top: -3.5rem;
    touch-action: none; cursor: grab;
    transition: left 0.25s ease;
  }
  .sb-handle::before {
    content: ""; position: absolute; top: 50%; left: 5px;
    width: 4px; height: 2.75rem; margin-top: -1.375rem; border-radius: 999px;
    background: var(--text-muted); opacity: 0.35; transition: opacity 0.2s;
  }
  .sb-handle:hover::before, html.sb-dragging .sb-handle::before { opacity: 0.7; }
  html.drawer-open .sb-handle { left: min(20rem, 84vw); }
  html.sb-dragging .sb-handle,
  html.sb-dragging .chapter-drawer,
  html.sb-dragging .drawer-backdrop { transition: none; }
}
@media (max-width: 480px) {
  html.drawer-open .sb-handle { left: min(18rem, 88vw); }
  .sb-mark { width: 2rem; height: 2rem; }
  .drawer-header { gap: 0.5rem; }
}

/* ---- 1024px and up: persistent sidebar / rail ----
   Every row puts its icon (or chapter number) in a 2.5rem box whose
   centre sits 2rem from the sidebar's left edge, in every state, so
   nothing slides sideways while the width animates (the rail is 4rem, so
   that's also the rail's centre). Rows are nowrap and labels only fade,
   so nothing reflows mid-animation either. The header title is the one
   wrapping text; it's laid out at the full width at all times and simply
   clipped in the rail, for the same reason. */
@media (min-width: 1024px) {
  :root { --sb-w: 18rem; --sb-rail: 4rem; --sb-offset: var(--sb-w); }
  /* Three modes, picked from the sidebar-control menu (Supabase's model):
     expanded = full width; collapsed = rail, names as tooltips;
     hover = rail that peeks open over the text while hovered. */
  :root:is([data-tp-sb="collapsed"], [data-tp-sb="hover"]) { --sb-offset: var(--sb-rail); }

  :root body { padding-left: var(--sb-offset); transition: padding-left 0.2s ease; }
  :root .hamburger-btn, :root .drawer-backdrop, :root .drawer-close, :root .drawer-footer { display: none; }

  :root .chapter-drawer {
    width: var(--sb-offset); transform: none; z-index: 99;
    padding: 0; overflow: hidden;
    border-right: 1px solid var(--rule); box-shadow: none;
    transition: width 0.2s ease, box-shadow 0.2s ease, opacity 0.3s ease;
  }
  :root .chapter-drawer.sb-peek { width: var(--sb-w); box-shadow: 0 14px 44px rgba(0,0,0,0.2); }

  :root .drawer-header {
    flex: none; align-items: center; gap: 0; margin: 0;
    height: 4.5rem; padding: 0 0 0 0.75rem; border-bottom: 1px solid var(--rule);
  }
  :root .sb-mark { width: 2.5rem; height: 2.5rem; }
  :root .drawer-header-title {
    flex: none; width: calc(var(--sb-w) - 4.25rem); padding-left: 0.7rem;
    font-size: var(--fs-small); line-height: 1.35;
  }

  :root .drawer-home-link { flex: none; margin: 0.5rem 0 0; }
  :root .drawer-home-link a,
  :root .drawer-toc a {
    display: flex; align-items: center; gap: 0.25rem;
    height: 2.5rem; margin: 0 0.5rem; padding: 0 0.75rem 0 0.25rem;
    border-radius: var(--r-md); white-space: nowrap; overflow: hidden;
    font-size: var(--fs-small); color: var(--text); text-decoration: none;
    transition: background-color 0.15s, color 0.15s;
  }
  :root .drawer-home-link a { color: var(--text-muted); }
  :root .drawer-home-link a:hover, :root .drawer-toc a:hover { background: var(--bg); color: var(--link); }
  :root .drawer-toc a[aria-current] { background: color-mix(in srgb, var(--accent) 12%, transparent); color: var(--accent); }
  :root .drawer-home-link .sb-icon,
  :root .drawer-toc .toc-num { flex: none; width: 2.5rem; min-width: 0; display: inline-flex; justify-content: center; }
  /* Georgia's numerals are old-style (3, 4, 5, 7, 9 drop below the line),
     which reads as a ragged column in the rail; lining figures in the UI
     face line up like the icons they stand in for. */
  :root .drawer-toc .toc-num {
    font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
    font-size: var(--fs-label); font-variant-numeric: lining-nums tabular-nums;
  }
  :root .sb-label,
  :root .drawer-toc a > [data-seg] { flex: 1 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

  :root .drawer-toc {
    flex: 1 1 auto; min-height: 0; overflow-x: hidden; overflow-y: auto;
    padding-bottom: 0.75rem; scrollbar-width: thin; scrollbar-color: var(--rule) transparent;
  }
  :root .drawer-toc h2 {
    height: 2.5rem; display: flex; align-items: flex-end; margin: 0 0 0.25rem;
    padding: 0 0 0.5rem 1rem; white-space: nowrap;
  }
  :root .drawer-toc li { margin-bottom: 0.125rem; }

  /* Sidebar control: one icon button pinned at the bottom (2.25rem square,
     0.875rem in, so its centre is on the 2rem icon column too). */
  :root .sb-control {
    display: flex; flex: none; align-items: center;
    height: 4.5rem; padding-left: 0.875rem; border-top: 1px solid var(--rule);
  }
  .sb-control-btn {
    display: inline-flex; align-items: center; justify-content: center; flex: none;
    width: 2.25rem; height: 2.25rem; padding: 0; border-radius: var(--r-md);
    border: 1px solid var(--rule); background: var(--bg-elevated); color: var(--text-muted);
    cursor: pointer; transition: background-color 0.15s, color 0.15s, border-color 0.15s;
  }
  .sb-control-btn:hover { background: var(--bg); color: var(--text); }
  .sb-control-btn[aria-expanded="true"] { border-color: var(--accent); color: var(--accent); }
  .sb-control-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
  .sb-control-btn svg { display: block; }

  /* The rail: labels fade out but keep their space; numbers stay put. */
  :root .drawer-header-title, :root .sb-label,
  :root .drawer-toc a > [data-seg], :root .drawer-toc h2 > span { transition: opacity 0.15s ease; }
  :root:is([data-tp-sb="collapsed"], [data-tp-sb="hover"]) .chapter-drawer:not(.sb-peek) :is(.drawer-header-title, .sb-label, .drawer-toc a > [data-seg], .drawer-toc h2 > span) { opacity: 0; }
  :root:is([data-tp-sb="collapsed"], [data-tp-sb="hover"]) .chapter-drawer:not(.sb-peek) .drawer-toc { scrollbar-width: none; }

  /* Fixed things centred on the page re-centre on the text column. */
  :root #resumeToast { left: calc(var(--sb-offset) + 1.2rem); }
  :root .jump-pill { left: calc(50% + var(--sb-offset) / 2); }

  /* Focus mode (reader.js): the sidebar recedes while you read down. */
  :root[data-tp-focus="on"].tp-chrome-away .chapter-drawer:not(:hover):not(:focus-within):not(.sb-peek) { opacity: 0.35; }
}

/* ---- Sidebar-control menu and rail tooltips ----
   Both live on <body>, not in the sidebar: the sidebar clips its overflow,
   and in the rail both reach well past its 4rem edge. sidebar.js positions
   them (fixed) next to whatever they belong to. */
.sb-menu {
  position: fixed; z-index: 131; min-width: 12.5rem;
  padding: 0 0 0.25rem; border: 1px solid var(--rule); border-radius: var(--r-lg);
  background: var(--bg-elevated); color: var(--text);
  box-shadow: 0 12px 36px rgba(0,0,0,0.18), var(--shadow);
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif; font-size: var(--fs-small); line-height: 1.3;
  -webkit-user-select: none; user-select: none;
  opacity: 0; visibility: hidden; transform: translateY(4px);
  transition: opacity 0.14s ease, transform 0.14s ease, visibility 0s linear 0.14s;
}
.sb-menu.is-open { opacity: 1; visibility: visible; transform: none; transition: opacity 0.14s ease, transform 0.14s ease, visibility 0s; }
.sb-menu-title {
  margin: 0 0 0.25rem; padding: 0.5rem 0.875rem; border-bottom: 1px solid var(--rule);
  font-size: var(--fs-label); font-weight: 400; color: var(--text-muted);
}
.sb-menu-item {
  display: flex; align-items: center; gap: 0.5rem; width: 100%;
  padding: 0.5rem 0.875rem 0.5rem 0.5rem; border: 0; border-radius: 0; background: none;
  font: inherit; color: var(--text); text-align: left; white-space: nowrap; cursor: pointer;
}
.sb-menu-item:hover, .sb-menu-item:focus-visible { background: var(--bg); outline: none; }
.sb-menu-item:focus-visible { box-shadow: inset 2px 0 0 var(--accent); }
.sb-menu-dot { flex: none; display: inline-flex; align-items: center; justify-content: center; width: 1rem; height: 1rem; }
.sb-menu-item[aria-checked="true"] .sb-menu-dot::before {
  content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor;
}

.sb-tip {
  position: fixed; z-index: 131; max-width: 18rem;
  padding: 0.25rem 0.625rem; border-radius: var(--r-sm);
  background: var(--text); color: var(--bg); box-shadow: var(--shadow);
  font-family: Georgia, "Iowan Old Style", "Times New Roman", serif; font-size: var(--fs-small); line-height: 1.3;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis; pointer-events: none;
  opacity: 0; transform: translate(-3px, -50%); transition: opacity 0.12s ease, transform 0.12s ease;
}
.sb-tip.is-on { opacity: 1; transform: translate(0, -50%); }

@media (prefers-reduced-motion: reduce) {
  :root body, :root .chapter-drawer, .sb-handle, .sb-handle::before,
  :root .drawer-header-title, :root .sb-label,
  :root .drawer-toc a > [data-seg], :root .drawer-toc h2 > span,
  .sb-control-btn, .sb-menu, .sb-menu.is-open, .sb-tip { transition: none; }
}
