/* Bardo OS — shell and layer rendering (Phase 1).
   The reading surface proper (modes, controls, auto-scroll) is Phase 2;
   this file styles the thin shell and the five layer treatments. */

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--font-body);
  font-size: var(--size-base);
  line-height: var(--lh-body);
  font-feature-settings: "kern", "liga", "onum" 1;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

/* Tibetan rendering hints (from the reference app) */
.bo {
  font-family: var(--font-tibetan);
  line-height: var(--lh-tibetan);
  text-rendering: optimizeLegibility;
  font-feature-settings: "ccmp", "rclt";
}

/* ── Shell ──────────────────────────────────────────────── */
/* A thin, constant bar; the catalogue lives behind the corner
   toggle and drops down over the text instead of beside it. */
.app-header {
  position: sticky;
  top: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-3);
  min-height: 44px;
  padding: 0 var(--sp-4);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  border-bottom: 1px solid var(--border);
}
.app-header h1 {
  font-family: var(--font-ui);
  font-size: 0.85rem;
  font-weight: 600;
  letter-spacing: var(--ls-caps);
  text-transform: uppercase;
  color: var(--accent);
}

.menu-toggle {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 44px;
  padding: 0 var(--sp-2);
  margin-left: calc(-1 * var(--sp-2));
  font-family: var(--font-ui);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--ls-caps);
  color: var(--text-muted);
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color var(--dur-2);
}
.menu-toggle:hover { color: var(--text); }
.menu-toggle:focus { outline: none; }
.menu-toggle:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.menu-caret { transition: transform var(--dur-2); }
body.nav-open .menu-toggle { color: var(--accent); }
body.nav-open .menu-caret { transform: rotate(180deg); }

.header-actions {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  flex: none;
}
/* The title yields before the controls do on a narrow screen. */
.app-header h1 { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* Donate: replicated from the Ngondro reference header — small,
   bordered, discreet; a link out, never a pattern. */
.donate-link {
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: var(--accent);
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 var(--sp-2);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, transparent);
  border-radius: var(--radius-sm);
  transition: background var(--dur-2), color var(--dur-2), border-color var(--dur-2);
}
.donate-link:hover,
.donate-link:focus-visible {
  background: var(--accent);
  color: var(--bg);
  border-color: var(--accent);
}
.donate-link:focus { outline: none; }
.donate-link:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

/* Interface-language selector (Yontendzo pattern, this app's tokens):
   a small button in the corner, a plain dropdown beneath it. */
.lang-select { position: relative; }
.lang-select-btn {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-1);
  min-height: 30px;
  padding: 0 var(--sp-2);
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: color var(--dur-2), border-color var(--dur-2);
}
.lang-select-btn:hover { color: var(--text); }
.lang-select-btn[aria-expanded="true"] { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 35%, var(--border)); }
.lang-select-btn:focus { outline: none; }
.lang-select-btn:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

.lang-menu {
  position: absolute;
  right: 0;
  top: calc(100% + 4px);
  z-index: 70;
  min-width: 11rem;
  margin: 0;
  padding: var(--sp-1) 0;
  list-style: none;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  box-shadow: 0 16px 32px color-mix(in srgb, var(--text) 12%, transparent);
}
.lang-menu li {
  display: flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 40px;
  padding: var(--sp-1) var(--sp-3);
  font-family: var(--font-ui);
  font-size: 0.8rem;
  color: var(--text);
  cursor: pointer;
}
.lang-menu li:hover { background: var(--bg-secondary); }
.lang-menu li.active { color: var(--accent); font-weight: 600; background: var(--bg-secondary); }
.lang-menu li:focus { outline: none; }
.lang-menu li:focus-visible { outline: none; box-shadow: inset 0 0 0 2px var(--accent); }
.lang-menu .menu-pending {
  margin-left: auto;
  padding-left: var(--sp-3);
  font-size: 0.62rem;
  font-style: italic;
  color: var(--text-muted);
}

/* The header disappears when the screen is given wholly to the text. */
body.fs .app-header { display: none; }
@media (display-mode: fullscreen) {
  .app-header { display: none; }
}

.shell {
  max-width: var(--column);
  margin: 0 auto;
  padding: var(--sp-5) var(--sp-4) var(--sp-9);
}

/* ── Navigation: the catalogue as a collapsible sidebar ───
   Docked beside the text on a wide screen, an overlay above it on a
   narrow one; hidden entirely in Voice mode (rules with the other
   voice styles below). Nothing slides — the sidebar is either there
   or not (no animation, BRIEF §9). */
.nav {
  display: none;
  position: fixed;
  top: var(--header-h);
  bottom: 0;
  left: 0;
  z-index: 55;
  width: min(var(--nav-w), 100vw);
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: var(--sp-3) var(--sp-3) var(--sp-7);
  background: var(--bg);
  border-right: 1px solid var(--border);
}
body.nav-open .nav { display: block; }

/* Overlay (narrow): a shadow separates it from the text beneath. */
@media (max-width: 63.99rem) {
  .nav { box-shadow: 0 16px 32px color-mix(in srgb, var(--text) 12%, transparent); }
}
/* Docked (wide): the shell moves over and the reader re-centers
   in the space that remains. */
@media (min-width: 64rem) {
  body.nav-open .shell { margin-left: var(--nav-w); max-width: none; }
  body.nav-open .reader { max-width: var(--column); margin: 0 auto; }
}

.nav-cat { margin-bottom: var(--sp-3); }
.nav-cat summary {
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--ls-caps-sm);
  color: var(--text-muted);
  padding: var(--sp-2) var(--sp-1);
  cursor: pointer;
  border-radius: var(--radius-sm);
  -webkit-user-select: none;
  user-select: none;
}
.nav-cat summary::marker { color: var(--text-light, var(--text-muted)); }
.nav-cat summary:hover { color: var(--text); }
.nav-cat summary:focus { outline: none; }
.nav-cat summary:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

/* Prayers & Liturgies: a collection within the collection — a framed,
   gently washed panel with its heading in the accent. Never color
   alone: the frame, the space, and the larger heading carry it too. */
.nav-cat-collection {
  margin: var(--sp-5) 0;
  padding: var(--sp-3) var(--sp-2);
  background: color-mix(in srgb, var(--accent) 6%, transparent);
  border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border));
  border-radius: var(--radius-md);
}
.nav-cat-collection summary {
  color: var(--accent);
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: var(--ls-caps);
}
.nav-cat-collection summary:hover { color: var(--accent); }
.nav-cat-collection button.nav-text { font-weight: 600; }

.nav button.nav-text {
  display: block;
  width: 100%;
  text-align: left;
  font-family: var(--font-body);
  font-size: 0.95rem;
  color: var(--text);
  background: none;
  border: none;
  border-radius: var(--radius-sm);
  padding: var(--sp-2) var(--sp-3);
  cursor: pointer;
}
.nav button.nav-text:hover { background: var(--bg-secondary); }
.nav button.nav-text.active { color: var(--accent); background: var(--bg-secondary); }
.nav button.nav-text:focus { outline: none; }
.nav button.nav-text:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }

/* Forthcoming: present but locked — never color alone (icon + note) */
.nav-text.locked {
  display: flex;
  align-items: flex-start;
  gap: var(--sp-2);
  padding: var(--sp-2) var(--sp-3);
  color: var(--text-muted);
  opacity: 0.65;
  cursor: default;
}
.nav-lock { width: 12px; height: 12px; flex: none; margin-top: 0.25em; }
.nav-locked-body { display: block; min-width: 0; }
.nav-locked-title { display: block; font-family: var(--font-body); font-size: 0.95rem; }
.nav-note {
  display: block;
  font-family: var(--font-ui);
  font-size: 0.62rem;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  margin-top: 2px;
}

.reader { min-width: 0; }

/* ── Text chrome ────────────────────────────────────────── */
.text-title { text-align: center; margin: var(--sp-5) 0 var(--sp-8); }
.text-title .bo {
  font-size: calc(var(--size-base) * 1.6);
  color: var(--accent);
  margin-bottom: var(--sp-3);
  text-wrap: balance;
}
.text-title .en {
  font-style: italic;
  color: var(--text-muted);
  text-wrap: balance;
}

.section { margin-bottom: var(--sp-7); }
.section-heading {
  font-family: var(--font-ui);
  font-size: 0.7rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--ls-caps);
  color: var(--accent);
  margin-bottom: var(--sp-5);
  padding-bottom: var(--sp-2);
  border-bottom: 1px solid color-mix(in srgb, var(--accent) 18%, var(--border));
}

/* ── Blocks: shared bones ───────────────────────────────────
   --size-block lets a layer raise its own floor (L1 never drops
   below 19px however small the global setting goes). */
.block { --size-block: var(--size-base); margin-bottom: var(--sp-4); text-align: center; font-size: var(--size-block); }
.block .bo   { font-size: calc(var(--size-block) * var(--size-tib)); margin-bottom: var(--sp-2); white-space: pre-wrap; }
.block .phon {
  font-family: var(--font-ui);
  font-weight: 600;
  font-size: calc(var(--size-block) * var(--size-phon));
  letter-spacing: 0.04em;
  color: var(--phonetics);
  margin-bottom: var(--sp-1);
  white-space: pre-wrap;
}
.block .en   { max-width: var(--measure); margin: 0 auto; white-space: pre-wrap; }

/* Run labels (◆ READ ALOUD, BARDO RECITATION, LITURGY) */
.run-label {
  font-family: var(--font-ui);
  font-size: 0.62rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: var(--ls-caps-sm);
  color: var(--label);
}

/* ── L0 Rubric: quiet, recessive, never spoken ──────────── */
/* Upright, not italic: the reader needs the explanatory text legible at
   a glance at the bedside, and italics cost too much legibility. It sits
   a touch larger than the recited layers so it reads as the reader's own
   guidance, still set apart by its muted colour and narrow measure. */
.block.layer-L0 {
  color: var(--l0-text);
  font-size: calc(var(--size-block) * var(--size-l0));
}
.block.layer-L0 .bo {
  font-size: calc(var(--size-block) * var(--size-l0-tib));
  color: var(--l0-text);
}
.block.layer-L0 .en { max-width: var(--measure-l0); color: var(--l0-text); }

/* ── L1 Address: left-aligned, gold rule, amber wash ────── */
.run-L1 {
  border-left: 3px solid var(--l1-rule);
  background: var(--l1-wash);
  padding: var(--sp-4) var(--sp-4) var(--sp-4) var(--sp-5);
  margin: var(--sp-5) 0;
  text-align: left;
}
.run-L1 .run-label { display: block; margin-bottom: var(--sp-3); }
.run-L1 .run-label::before { content: "◆  "; }
.run-L1 .block { --size-block: max(19px, var(--size-base)); text-align: left; }
.run-L1 .block .en { margin: 0; max-width: var(--measure); color: var(--text); }
.run-L1 .block:last-child { margin-bottom: 0; }

/* ── L2 Bardo recitation: framed panel, verse ───────────── */
.run-L2 {
  max-width: 44rem;
  margin: var(--sp-6) auto;
  padding: var(--sp-4) var(--sp-5) var(--sp-5);
  background: var(--l2-wash);
  border: 1px solid var(--l2-border);
  border-radius: var(--radius-lg);
}
.run-L2 .run-label { display: block; text-align: center; margin-bottom: var(--sp-4); }
.run-L2 .block { text-align: left; color: var(--l2-text); }
.run-L2 .block .bo, .run-L2 .block .en { color: var(--l2-text); }
.run-L2 .block .en {
  margin: 0;
  padding-left: 1.5rem;
  text-indent: -0.75rem; /* hanging indent for verse lines */
}
.run-L2 .block:last-child { margin-bottom: 0; }

/* ── L3 Living liturgy: the reference presentation ──────── */
.run-L3 .run-label { display: block; text-align: center; margin-bottom: var(--sp-3); }
.block.layer-L3 .en { color: var(--text-muted); }

/* ── L4 Apparatus: silent; titles and colophons ─────────── */
.block.layer-L4 { color: var(--l4-text); }
.block.layer-L4.form-title .en {
  font-family: var(--font-ui);
  font-weight: 700;
  font-size: calc(var(--size-base) * 0.85);
  text-transform: uppercase;
  letter-spacing: var(--ls-caps-sm);
  color: var(--text);
}
.block.layer-L4.form-colophon,
.block.layer-L4.form-prose {
  font-style: italic;
  font-size: calc(var(--size-base) * 0.85);
  border-top: 1px solid var(--border);
  padding-top: var(--sp-4);
  max-width: var(--measure-l0);
  margin-left: auto;
  margin-right: auto;
}

/* Repeated formulas share one framed panel; the auto-scroll engine
   holds on it (no pulse, no glow — nothing animates but the scroll). */
.refrain-group {
  max-width: 44rem;
  margin: var(--sp-5) auto;
  padding: var(--sp-4) var(--sp-5);
  background: var(--l2-wash);
  border: 1px solid var(--l2-border);
  border-radius: var(--radius-lg);
}
.refrain-group .block:last-child { margin-bottom: 0; }
/* Inside the L2 panel a nested frame would be noise — the panel already frames. */
.run-L2 .refrain-group { border: none; background: none; padding: 0; margin: 0 0 var(--sp-4); }

/* ── Prayer cross-links: a mentioned prayer, one tap away ──
   Never color alone: border, icon, and type treatment carry it. */
.prayer-ref { margin-top: var(--sp-3); }
.prayer-link {
  display: inline-flex;
  align-items: center;
  gap: var(--sp-2);
  min-height: 44px;
  padding: var(--sp-1) var(--sp-3);
  font-family: var(--font-body);
  font-size: calc(var(--size-block, var(--size-base)) * 0.85);
  text-align: left;
  color: var(--accent);
  background: none;
  border: 1px solid color-mix(in srgb, var(--accent) 25%, transparent);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--dur-2);
}
@media (hover: hover) {
  .prayer-link:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
}
.prayer-link:focus { outline: none; }
.prayer-link:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.prayer-link-icon { width: 14px; height: 14px; flex: none; }
.prayer-link.locked {
  color: var(--text-muted);
  border-color: var(--border);
  opacity: 0.75;
  cursor: default;
}
.prayer-link-body { display: block; min-width: 0; }
.prayer-link-note {
  display: block;
  font-family: var(--font-ui);
  font-size: 0.62rem;
  letter-spacing: 0.02em;
  color: var(--text-muted);
  margin-top: 2px;
}

/* Verse lines render as real lines */
.line { display: block; }

/* Inline emphasis (**…** in content): bold, leaning toward the accent —
   different enough to be found at a glance, quiet enough not to shout. */
.em {
  font-weight: 700;
  color: color-mix(in srgb, var(--accent) 45%, var(--text));
}

/* Declared content gaps stay visible and honest */
.todo {
  font-family: var(--font-ui);
  font-size: 0.7em;
  font-style: normal;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 0 var(--sp-2);
}

/* ── Bottom bar: every control, one-handed, bottom third ── */
.bottom-bar {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 50;
  padding: var(--sp-3) var(--sp-4);
  padding-bottom: max(var(--sp-3), env(safe-area-inset-bottom));
  pointer-events: none;
}
.bar-inner {
  max-width: 40rem;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--sp-2);
  pointer-events: auto;
  flex-wrap: wrap;
}
.bar-group {
  display: flex;
  align-items: center;
  padding: var(--sp-1);
  background: color-mix(in srgb, var(--bg) 78%, transparent);
  backdrop-filter: blur(12px) saturate(140%);
  -webkit-backdrop-filter: blur(12px) saturate(140%);
  border: 1px solid color-mix(in srgb, var(--text) 8%, transparent);
  border-radius: var(--radius-md);
}
.bar-btn {
  min-width: 40px;
  height: 40px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background var(--dur-2), color var(--dur-2);
}
@media (hover: hover) {
  .bar-btn:hover { background: color-mix(in srgb, var(--accent) 10%, transparent); color: var(--text); }
}
.bar-btn:focus { outline: none; }
.bar-btn:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.bar-btn.active {
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  color: var(--accent);
  box-shadow: inset 0 0 0 1px color-mix(in srgb, var(--accent) 25%, transparent);
}
.bar-btn-text {
  font-family: var(--font-ui);
  font-size: 0.65rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 0 var(--sp-2);
}
/* Narrow screens: the whole bar stays on one row. Groups and buttons
   give up their fixed minimums and share the width; the 40px height
   keeps the touch targets honest. */
@media (max-width: 40rem) {
  .bottom-bar { padding-left: var(--sp-2); padding-right: var(--sp-2); }
  .bar-inner { flex-wrap: nowrap; gap: var(--sp-1); }
  .bar-group { flex: 1 1 auto; min-width: 0; justify-content: space-evenly; padding: 2px; }
  .bar-btn { min-width: 0; flex: 1 1 auto; }
  .bar-btn-text { padding: 0; }
}

/* Content never hides behind the bar */
.shell { padding-bottom: 7rem; }

/* ── Voice mode: the mode you use when someone is dying ─── */
body.voice .app-header,
body.voice .lang-group { display: none; }
/* The sidebar leaves with the header; the shell re-centers. These sit
   after the nav rules on purpose — at equal specificity, Voice wins. */
body.voice .nav { display: none; }
body.voice .shell { margin: 0 auto; max-width: var(--column); }
body.voice .reader { max-width: none; margin: 0; }
body.voice .shell { padding-top: var(--sp-4); }
body.voice .reader { --measure: 40rem; }
body.voice .section-heading { opacity: 0.6; }

/* A collapsed rubric: one thin, identical, peekable marker */
.voice-collapsed { margin: var(--sp-4) 0; font-size: var(--size-block); }
.l0-marker {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: var(--sp-3);
  width: 100%;
  min-height: 44px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}
.l0-marker:focus { outline: none; }
.l0-marker:focus-visible { outline: none; box-shadow: 0 0 0 2px var(--bg), 0 0 0 4px var(--accent); }
.l0-marker-line { width: 2rem; height: 1px; background: var(--border); }
.l0-marker-dot { font-style: normal; line-height: 1; }
.l0-peek { display: none; padding-top: var(--sp-2); }
.voice-collapsed.open .l0-peek { display: block; }
.voice-collapsed.open .l0-marker-dot { color: var(--accent); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
