/* Threads & Subplots — production CSS (Direction A: Manuscript)
   Ported from apps/web/pagesNew/threads.css.
   Uses pb-* token system (premise.css). No Tailwind utilities.
   New section classes (th-sep-*, th-mech-*, th-stake-*, th-res-*)
   are additions to the wireframe reference, per Phase 10.1 contract.
   Registered in app.html during Phase 10.3. */

/* ============================================================
   A — OUTER SHELL + APP GRID
   ============================================================ */
.th-app {
  height: 100vh;
  overflow: hidden;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  background: var(--paper);
}
.th-app .pb-main {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.th-app #state-container {
  flex: 1;
  min-height: 0;
  /* Same clipping disease as characters: the pregeneration .th-empty has no
     inner scroller, so a phone viewport clipped the hero with no scroll. */
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  display: flex;
  flex-direction: column;
  /* scroll works, but no standalone scrollbar pill floating beside the hero */
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.th-app #state-container::-webkit-scrollbar { display: none; width: 0; }

/* Three-pane manuscript stage */
.th-stage {
  display: grid;
  grid-template-columns: 240px 1fr 320px;
  flex: 1;
  min-height: 0;
  background: var(--paper);
}

/* ============================================================
   B — RAIL (left, 240px)
   ============================================================ */
.th-rail {
  border-right: 1px solid var(--line);
  background: var(--paper);
  padding: 18px 12px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
}
.th-rail-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 6px 12px;
}
.th-rail-group { margin-top: 14px; }
.th-rail-group:first-of-type { margin-top: 0; }
.th-rail-group-label {
  font-size: 9.5px; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 600;
  padding: 0 6px 6px;
}
.th-rail-item {
  display: grid;
  grid-template-columns: 3px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 8px 8px 0;
  border-radius: 6px;
  background: transparent;
  border: none;
  width: 100%;
  text-align: left;
  cursor: pointer;
  margin-bottom: 2px;
}
.th-rail-item:hover { background: var(--paper-2); }
.th-rail-item.active { background: var(--surface); box-shadow: inset 0 0 0 1px var(--line); }

/* Kind → accent color; JS also sets style="--accent:…" on .th-stage */

.th-rail-stripe {
  width: 3px;
  height: 28px;
  border-radius: 2px;
  background: var(--accent, var(--line-2));
  margin-left: 6px;
}
.th-rail-item.active .th-rail-stripe { background: var(--accent, var(--ink)); }
.th-rail-body { min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.th-rail-name {
  font-family: var(--serif);
  font-size: 14px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--ink);
  line-height: 1.2;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.th-rail-meta {
  font-size: 11px; color: var(--ink-4);
  display: flex; gap: 6px; align-items: center;
}
.th-rail-meta .dot { color: var(--ink-5); }

/* ============================================================
   C — CENTER PANE (flex-1)
   ============================================================ */
.th-page-wrap {
  overflow-y: auto;
  background: var(--paper);
  padding: 28px 36px 40px;
}
.th-page {
  max-width: 1080px;
  margin: 0 auto;
  padding: 36px 44px 44px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 2px 0 rgba(0,0,0,0.04), 0 14px 40px rgba(20,18,12,0.15);
}
.th-page-eyebrow {
  display: flex; align-items: center; gap: 10px;
  margin-bottom: 14px;
}
.th-page-eyebrow .eyebrow { color: var(--accent, var(--ink-4)); }
.th-page-title {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 44px;
  line-height: 1.05;
  letter-spacing: -0.025em;
  color: var(--ink);
  margin: 0 0 12px;
  text-wrap: pretty;
}
.th-page-logline {
  font-family: var(--serif);
  font-style: italic;
  font-size: 18px;
  line-height: 1.55;
  color: var(--ink-3);
  margin: 0 0 28px;
  max-width: 64ch;
  text-wrap: pretty;
}

/* Health dot */
.th-health-dot { width: 8px; height: 8px; border-radius: 999px; flex: none; }
.th-health-dot.on-track { background: var(--ok, #10B981); }
.th-health-dot.watch    { background: var(--warn, #F59E0B); }
.th-health-dot.thin     { background: var(--ink-4, #9CA3AF); }

/* ============================================================
   D — SECTION CHROME
   ============================================================ */
.th-sec { margin-top: 36px; }
.th-sec-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 14px;
}
.th-sec-head h3 {
  font-family: var(--serif);
  font-size: 20px;
  font-weight: 600;
  margin: 0;
  letter-spacing: -0.01em;
  color: var(--ink);
}
.th-sec-head .meta { font-size: 11.5px; color: var(--ink-4); }

/* ============================================================
   E — ARC SECTION
   ============================================================ */
.th-arc {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--rad);
  overflow: hidden;
}
.th-arc-step {
  background: var(--surface);
  padding: 20px 22px;
}
.th-arc-step .label {
  font-size: 10.5px; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 600;
  margin-bottom: 8px;
  display: flex; align-items: center; gap: 8px;
}
.th-arc-step .label::before {
  content: "";
  width: 6px;
  height: 6px;
  border-radius: 999px;
  display: inline-block;
}
.th-arc-step:nth-child(1) .label::before { background: var(--ok, #4F7A6A); }
.th-arc-step:nth-child(2) .label::before { background: var(--warn, #B8693B); }
.th-arc-step:nth-child(3) .label::before { background: #5B6FA6; }
.th-arc-step .body {
  font-family: var(--serif);
  font-size: 15px;
  line-height: 1.55;
  color: var(--ink-2);
  text-wrap: pretty;
}

/* Single-block arc fallback (Phase 10.1 decision: display as one cell) */
.th-arc.single-block {
  grid-template-columns: 1fr;
}

/* ============================================================
   F — INLINE LIST BLOCKS
   ============================================================ */
.th-list-block { display: flex; flex-direction: column; gap: 6px; }
.th-list-head {
  display: flex; align-items: baseline; justify-content: space-between;
  margin-bottom: 2px;
}
.th-list-label {
  font-size: 10.5px; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 600;
}
.th-list-body {
  font-family: var(--serif);
  font-size: 14.5px;
  line-height: 1.6;
  color: var(--ink-2);
  white-space: pre-wrap;
  outline: none;
  min-height: 1.6em;
  border-radius: 4px;
  padding: 4px 6px;
  margin: 0 -6px;
}
.th-list-body:hover { background: var(--paper-2); }
.th-list-body:focus { background: var(--paper-2); box-shadow: inset 0 0 0 1.5px var(--line); }

/* ============================================================
   G — INSPECTOR (right, 320px)
   ============================================================ */
.th-insp {
  border-left: 1px solid var(--line);
  background: var(--paper);
  padding: 24px 22px;
  overflow-y: auto;
  display: flex; flex-direction: column; gap: 24px;
}
.th-insp-block { display: flex; flex-direction: column; gap: 10px; }
.th-insp-label {
  font-size: 10.5px; letter-spacing: 0.16em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 600;
}

/* ============================================================
   H — AUDIT CELLS (Foreshadowing / Planning summary)
   ============================================================ */
.th-audit {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 1px;
  background: var(--line);
  border: 1px solid var(--line);
  border-radius: var(--rad);
  overflow: hidden;
}
.th-audit-cell {
  background: var(--surface);
  padding: 12px 10px;
  text-align: center;
  display: flex; flex-direction: column; gap: 4px;
}
.th-audit-cell .num {
  font-family: var(--serif);
  font-size: 22px; font-weight: 600;
  color: var(--ink);
  letter-spacing: -0.01em;
  line-height: 1;
}
.th-audit-cell .num .total { color: var(--ink-4); font-size: 13px; }
.th-audit-cell .lbl {
  font-size: 10.5px; letter-spacing: 0.12em; text-transform: uppercase;
  color: var(--ink-4); font-weight: 600;
}
.th-audit-cell.warn .num { color: var(--warn, #F59E0B); }

/* ============================================================
   I — CAST / CHARACTERS (inspector block)
   ============================================================ */
.th-chars { display: flex; flex-direction: column; gap: 6px; }
.th-char {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 6px 8px;
  border-radius: 6px;
}
.th-char:hover { background: var(--paper-2); }
.th-char-mono {
  width: 28px; height: 28px; border-radius: 999px;
  background: var(--accent, var(--paper-2));
  color: white;
  font-family: var(--serif);
  font-weight: 600; font-size: 13px;
  display: grid; place-items: center;
  letter-spacing: -0.01em;
}
.th-char-name {
  font-size: 13px; font-weight: 500; color: var(--ink);
  line-height: 1.2;
}
.th-char-role {
  font-size: 10.5px; color: var(--ink-4);
  text-transform: uppercase; letter-spacing: 0.08em; font-weight: 600;
  margin-top: 2px;
}
.th-char-count {
  font-family: var(--mono); font-size: 11px; color: var(--ink-4);
}

/* ============================================================
   J — ECHOES / LINKS (inspector block)
   ============================================================ */
.th-echo {
  display: grid;
  grid-template-columns: 4px 1fr auto;
  gap: 10px;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px dotted var(--line);
}
.th-echo:last-child { border-bottom: none; }
.th-echo-bar {
  width: 4px; height: 22px; border-radius: 2px;
  background: var(--accent, var(--line-2));
}
.th-echo-name {
  font-family: var(--serif); font-weight: 500;
  font-size: 13.5px; color: var(--ink);
}
.th-echo-count {
  font-family: var(--mono);
  font-size: 11px; color: var(--ink-4);
}

/* ============================================================
   K — SEEDS · ESCALATIONS · PAYOFFS section (NEW)
   3-column planning grid per Phase 10.1 contract
   ============================================================ */
.th-sep-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

/* ============================================================
   L — MECHANICS section (NEW)
   Setups · Twists · Reversals · Reveals · Misdirects
   ============================================================ */
.th-mech {
  display: flex;
  flex-direction: column;
  gap: 20px;
}


/* ============================================================
   N — RESOLUTION inspector block (NEW)
   Status badge + payoff chapter line
   ============================================================ */
.th-resolution {
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.th-res-badge {
  display: inline-flex; align-items: center; gap: 6px;
  height: 24px; padding: 0 10px;
  border-radius: 999px;
  font-size: 11px; font-weight: 600;
  letter-spacing: 0.04em;
  align-self: flex-start;
  border: 1px solid transparent;
}
.th-res-badge::before {
  content: "";
  width: 6px; height: 6px; border-radius: 999px;
  background: currentColor;
  opacity: 0.8;
  flex-shrink: 0;
}
.th-res-badge.open {
  background: #F0FDF4;
  color: #15803D;
  border-color: #BBF7D0;
}
.th-res-badge.partially-paid {
  background: #EFF6FF;
  color: #2563EB;
  border-color: #BFDBFE;
}
.th-res-badge.stalled {
  background: #FFFBEB;
  color: #B45309;
  border-color: #FDE68A;
}
.th-res-badge.closed {
  background: var(--surface);
  color: var(--ink-3);
  border-color: var(--line);
}
.th-res-chapter {
  font-family: var(--mono);
  font-size: 11.5px;
  color: var(--ink-4);
  letter-spacing: 0.04em;
}

/* ============================================================
   O — EMPTY STATE
   Matches .ch-empty / .wb-empty pattern exactly
   ============================================================ */
.th-empty {
  max-width: 950px;
  margin: 0 auto;
  padding: 80px 64px;
  display: flex;
  flex-direction: column;
  gap: 22px;
}
.th-empty .wb-eyebrow { margin-bottom: 0; }
.th-empty h1 {
  font-family: var(--serif);
  font-weight: 600;
  font-size: 44px;
  letter-spacing: -0.025em;
  line-height: 1.05;
  margin: 0;
  color: var(--ink);
}
.th-empty .lede {
  font-family: var(--serif);
  font-style: italic;
  font-size: 18px;
  line-height: 1.55;
  color: var(--ink-3);
  text-wrap: pretty;
  max-width: 56ch;
  margin: 0;
}
.th-empty .actions {
  display: flex;
  gap: 10px;
  align-items: center;
}
.th-empty .actions .pb-btn {
  height: 38px;
  padding: 0 18px;
  font-size: 13.5px;
}
/* Context card — "FROM CHARACTERS" (mirrors .ch-empty .world-card) */
.th-empty .characters-card {
  background: var(--paper-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 18px 22px 16px;
  margin: 20px 0 28px;
}
.th-empty .characters-card .row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: var(--mono);
  font-size: 11px;
  color: var(--ink-4);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 10px;
}
.th-empty .characters-card .ll {
  font-family: var(--serif);
  font-size: 15.5px;
  line-height: 1.55;
  color: var(--ink);
  margin: 0 0 12px;
}
.th-empty .characters-card .open {
  display: inline-block;
  font-size: 10.5px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  font-weight: 600;
  color: var(--ink-4);
  text-decoration: none;
  border-bottom: 1px solid var(--line);
  padding-bottom: 2px;
}
.th-empty .characters-card .open:hover {
  color: var(--ink);
  border-bottom-color: var(--ink-3);
}

/* Inner scrollable empty container */
.th-empty {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  scrollbar-width: none;
  -ms-overflow-style: none;
}
.th-empty::-webkit-scrollbar { display: none; }

/* ============================================================
   P — SCROLLBAR HIDING + CHROME LOCK
   Follows 9.7n pattern
   ============================================================ */
.th-rail::-webkit-scrollbar,
.th-page-wrap::-webkit-scrollbar,
.th-insp::-webkit-scrollbar { display: none; }
.th-rail,
.th-page-wrap,
.th-insp {
  scrollbar-width: none;
  -ms-overflow-style: none;
}

/* ============================================================
   Q — SHARED / MISC
   ============================================================ */

/* View tab toggle (Direction A / B switcher if ever surfaced) */
.th-view-tabs {
  display: inline-flex;
  padding: 3px;
  background: var(--paper-2);
  border-radius: 8px;
  border: 1px solid var(--line-soft);
}
.th-view-tab {
  display: inline-flex; align-items: center; gap: 6px;
  height: 26px; padding: 0 12px;
  border: none; background: transparent;
  border-radius: 6px;
  font-size: 12px; font-weight: 500;
  color: var(--ink-3);
  cursor: pointer;
}
.th-view-tab:hover { color: var(--ink); }
.th-view-tab.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: 0 1px 2px rgba(0,0,0,0.04);
}

/* Loading spinner container */
.th-loading {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  color: var(--ink-3);
  font-family: var(--mono);
  font-size: 12px;
  letter-spacing: 0.08em;
}


/* ============================================================
   RESPONSIVE — fluid reflow with side panels that retract to
   their own edges (left rail -> left, inspector -> right).
   Desktop (>= 1241px) unchanged.
   ============================================================ */

.pb-main { min-width: 0; }
.pb-topbar { min-width: 0; }
.crumbs { min-width: 0; }

.topbar-right { min-width: 0; flex-wrap: wrap; }
.cast-view-tabs {
  min-width: 0; max-width: 100%;
  overflow-x: auto; flex-wrap: nowrap; scrollbar-width: thin;
}
.cast-view-tab { flex: 0 0 auto; white-space: nowrap; }

.th-rail, .th-page-wrap, .th-insp { min-width: 0; }

.th-rail-toggle, .th-insp-toggle { display: none; }  /* hidden on desktop */

/* <=1240: middle is the main column; left rail retracts to the LEFT edge,
   right inspector retracts to the RIGHT edge. */
@media (max-width: 1240px) {
  .th-stage {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    overflow-x: hidden;
    overflow-y: auto;
    position: relative;
  }

  .th-rail, .th-insp {
    position: absolute;
    top: 0;
    height: 100%;
    background: var(--paper);
    z-index: 20;
    transition: transform 0.22s ease;
  }

  /* LEFT rail: pinned left, slides off to the left */
  .th-rail {
    left: 0;
    width: min(280px, 86%);
    border-right: 1px solid var(--line);
    box-shadow: 8px 0 24px rgba(20,18,12,0.12);
    transform: translateX(-100%);
  }
  .th-stage.rail-open .th-rail { transform: translateX(0); }

  /* RIGHT inspector: pinned right, slides off to the right */
  .th-insp {
    right: 0;
    width: min(340px, 86%);
    border-left: 1px solid var(--line);
    box-shadow: -8px 0 24px rgba(20,18,12,0.12);
    transform: translateX(100%);
  }
  .th-stage.insp-open .th-insp { transform: translateX(0); }

  /* scrim */
  .th-scrim {
    position: absolute; inset: 0;
    background: rgba(20,18,12,0.28);
    opacity: 0; pointer-events: none;
    transition: opacity 0.22s ease;
    z-index: 15;
  }
  .th-stage.rail-open .th-scrim,
  .th-stage.insp-open .th-scrim { opacity: 1; pointer-events: auto; }

  /* edge handles */
  .th-rail-toggle, .th-insp-toggle {
    display: flex; align-items: center; justify-content: center;
    position: absolute;
    top: 14px;
    width: 30px; height: 46px;
    border: 1px solid var(--line);
    background: var(--paper); color: var(--ink-4);
    cursor: pointer; padding: 0; z-index: 25;
    transition: transform 0.22s ease;
  }
  .th-rail-toggle svg, .th-insp-toggle svg { width: 16px; height: 16px; transition: transform 0.22s ease; }

  .th-rail-toggle {
    left: 0; border-left: 0;
    border-radius: 0 8px 8px 0;
    box-shadow: 3px 0 10px rgba(20,18,12,0.08);
  }
  .th-stage.rail-open .th-rail-toggle { transform: translateX(min(280px, 86%)); }
  .th-stage.rail-open .th-rail-toggle svg { transform: rotate(180deg); }

  .th-insp-toggle {
    right: 0; border-right: 0;
    border-radius: 8px 0 0 8px;
    box-shadow: -3px 0 10px rgba(20,18,12,0.08);
  }
  .th-stage.insp-open .th-insp-toggle { transform: translateX(calc(-1 * min(340px, 86%))); }
  .th-stage.insp-open .th-insp-toggle svg { transform: rotate(180deg); }
}

/* <=899: sidebar collapses to the 60px icon-rail */
@media (max-width: 899px) {
  .th-app { grid-template-columns: 60px minmax(0, 1fr); }
}

/* Phone: the detail-view sections are desktop-first 3-track grids
   (.th-arc and .th-sep-grid both repeat(3,1fr)) — too cramped at phone
   width, and the third column's label ("Payo…") clipped off the right edge.
   Stack them into full-width single-column blocks. Desktop >768 unchanged. */
@media (max-width: 768px) {
  .th-arc,
  .th-sep-grid {
    grid-template-columns: minmax(0, 1fr);
  }
  /* the arc's 1px inter-cell gap now reads as horizontal dividers between
     the stacked Setup / Midpoint / Resolution cells */
  .th-arc-step { padding: 16px 18px; }
  /* labels and section headers never clip in the wider column, but guard
     against long single words regardless */
  .th-list-label,
  .th-sec-head h3 { overflow-wrap: anywhere; }
  .th-sec-head h3 { font-size: clamp(1.0625rem, 4.6vw, 1.25rem); }
  .th-sep-grid { gap: 18px; }
}

/* Phone */
@media (max-width: 560px) {
  .th-page-wrap { padding: 22px 18px 32px; }
}
