:root {
  /* Text sizes are rem so the phone's own text size setting applies (<meta name="text-scale">). */
  color-scheme: dark;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Apple SD Gothic Neo", "Segoe UI", sans-serif;
  --bg: #09111f;
  --fg: #f8fafc;
  --text: #e2e8f0;
  --dim: #9fb1c8;
  --faint: #6f849f;
  --line: rgb(148 163 184 / .16);
  --accent: #7dd3fc;
  --accent-strong: #38bdf8;
  --live: #ef4444;
  --chip: rgb(148 163 184 / .14);
  /* jeokja's motion vocabulary: a soft spring for things arriving, a long ease for settling. */
  --spring: cubic-bezier(.32, 1.28, .5, 1);
  --ease: cubic-bezier(.16, 1, .3, 1);
  background: var(--bg);
  color: var(--fg);
  -webkit-font-smoothing: antialiased;
}

* { box-sizing: border-box; }
body { min-width: 0; margin: 0; min-height: 100dvh; background: var(--bg); }
button { font: inherit; color: inherit; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

.viewer-shell {
  display: grid;
  grid-template-rows: auto auto auto minmax(0, 1fr);
  width: min(100%, 760px);
  height: 100dvh;
  margin: 0 auto;
  padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) 0 max(16px, env(safe-area-inset-left));
}
.viewer-visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* ── Header: state badge, sermon, text size ── */
.viewer-header { display: flex; align-items: center; gap: 10px; min-width: 0; animation: viewer-piece-in .6s var(--spring) backwards; }
@keyframes viewer-piece-in { from { opacity: 0; transform: translateY(12px) scale(.96); } }
.viewer-live {
  flex: none; display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 5px 10px;
  color: var(--dim); background: var(--chip); font-size: .75rem; font-weight: 750; letter-spacing: .04em; white-space: nowrap;
  transition: color .3s var(--ease), background-color .3s var(--ease);
}
.viewer-live[data-state="live"] { color: #fecaca; background: rgb(220 38 38 / .2); }
.viewer-live[data-state="live"]::before { content: ""; width: 8px; height: 8px; border-radius: 50%; background: var(--live); animation: viewer-pulse 1.6s ease-in-out infinite; }
.viewer-live[data-state="paused"] { color: #fde68a; background: rgb(217 119 6 / .18); }
@keyframes viewer-pulse { 50% { transform: scale(.7); opacity: .5; } }
.viewer-session { display: grid; flex: 1; min-width: 0; margin: 0; line-height: 1.3; }
.viewer-session span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.viewer-session-title { color: #fff; font-size: 1rem; font-weight: 650; }
.viewer-session-scripture { color: var(--dim); font-size: .8125rem; }
.viewer-text-size {
  flex: none; min-width: 44px; min-height: 44px; border: 1px solid #35516e; border-radius: 12px; padding: 0 9px;
  background: transparent; color: #dbeafe; font-size: .9375rem; font-weight: 700; transition: transform .3s var(--spring), background-color .15s;
}
/* Past services (/services): a list icon, with its name once the header has room. */
.viewer-archive {
  flex: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-width: 44px; min-height: 44px;
  border: 1px solid #35516e; border-radius: 12px; padding: 0 10px; color: #dbeafe; font-size: .875rem; font-weight: 700; text-decoration: none; white-space: nowrap;
}
.viewer-archive svg { width: 18px; height: 18px; }
.viewer-archive .label { display: none; }
@media (min-width: 480px) { .viewer-archive .label { display: inline; } }
.viewer-archive:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }
.viewer-text-size:active { transform: scale(.9); background: rgb(125 211 252 / .1); }

/* ── Language switch: a sliding pill behind the chosen option ── */
.viewer-modes {
  position: relative; display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 0; margin: 10px 0 0; padding: 4px;
  border: 1px solid #1e344f; border-radius: 14px; animation: viewer-piece-in .6s .05s var(--spring) backwards;
}
.viewer-modes::before {
  content: ""; position: absolute; top: 4px; bottom: 4px; left: 4px; width: calc((100% - 8px) / 3); border-radius: 10px;
  background: #12314a; box-shadow: inset 0 0 0 1px var(--accent-strong); transition: transform .45s var(--spring);
}
[data-viewer-mode="vi"] .viewer-modes::before { transform: translateX(100%); }
[data-viewer-mode="ko"] .viewer-modes::before { transform: translateX(200%); }
.viewer-modes button {
  position: relative; min-height: 44px; min-width: 0; border: 0; border-radius: 10px; padding: 6px 4px; background: transparent;
  color: var(--dim); font-size: .875rem; font-weight: 600; transition: color .25s var(--ease), transform .3s var(--spring);
}
.viewer-modes button[aria-pressed="true"] { color: #e0f2fe; }
.viewer-modes button:active { transform: scale(.94); }
.viewer-modes button:focus-visible, .viewer-text-size:focus-visible, .viewer-new-captions:focus-visible, .viewer-caption-feed:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.viewer-replay { display: inline-block; margin-top: 4px; color: var(--accent); font-weight: 700; text-decoration: none; white-space: nowrap; }
.viewer-replay:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.viewer-status { margin: 10px 0 0; color: var(--dim); font-size: .875rem; line-height: 1.45; white-space: pre-line; animation: viewer-hint-in .5s var(--ease); }
@keyframes viewer-hint-in { from { opacity: 0; transform: translateY(4px); filter: blur(3px); } }

/* ── Feed ── */
.viewer-caption-feed {
  min-height: 0; margin-top: 6px; overflow-y: auto; overscroll-behavior: contain; overflow-anchor: none;
  padding: 18px 2px 14vh; -webkit-overflow-scrolling: touch;
  /* Older lines dissolve into the header instead of being cut off. */
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 56px);
  mask-image: linear-gradient(to bottom, transparent 0, #000 56px);
}
.viewer-caption-feed.is-switching { animation: viewer-switch .35s var(--ease); }
@keyframes viewer-switch { from { opacity: .2; filter: blur(2px); } }
.viewer-empty { margin: 18vh 0 0; color: #8aa0bb; text-align: center; animation: viewer-hint-in .6s var(--ease); }

.viewer-caption-line {
  margin: 0; padding: 16px 4px; border-bottom: 1px solid var(--line); transform-origin: left center;
  animation: viewer-line-in .5s var(--spring) backwards;
  transition: opacity .55s var(--ease);
}
@keyframes viewer-line-in { from { opacity: 0; transform: translateY(14px); } }
.viewer-caption-line:last-of-type { border-bottom: 0; }
/* Depth while following: the newest line is crisp, older ones fade with distance. */
.viewer-caption-feed:not(.is-reading) .viewer-caption-line[data-d="1"] { opacity: .74; }
.viewer-caption-feed:not(.is-reading) .viewer-caption-line[data-d="2"] { opacity: .56; }
.viewer-caption-feed:not(.is-reading) .viewer-caption-line[data-d="3"] { opacity: .42; }

.viewer-vietnamese, .viewer-korean { margin: 0; overflow-wrap: anywhere; padding-right: .62em; }
.viewer-vietnamese { color: var(--text); font-size: calc(clamp(1.4375rem, 0.4rem + 4.4vw, 2.0625rem) * var(--caption-scale, 1)); line-height: 1.5; letter-spacing: -.005em; }
/* Both languages: Korean sits above the Vietnamese it was translated from. */
.viewer-korean { margin-bottom: 8px; color: #a9bad0; font-size: calc(clamp(1.0625rem, 0.3rem + 3.4vw, 1.375rem) * var(--caption-scale, 1)); line-height: 1.55; word-break: keep-all; }
[data-viewer-mode="ko"] .viewer-korean { margin: 0; color: var(--text); font-size: calc(clamp(1.375rem, 0.4rem + 4.2vw, 1.9375rem) * var(--caption-scale, 1)); line-height: 1.55; }
.viewer-caption-line.is-latest .viewer-vietnamese,
[data-viewer-mode="ko"] .viewer-caption-line.is-latest .viewer-korean { color: #fff; font-weight: 650; }

/* Words: each new word rises out of a blur (jeokja's w-in); words already shown never move. */
.w { display: inline-block; animation: viewer-word-in .55s var(--ease) backwards; }
.w-still { animation: none; }
@keyframes viewer-word-in {
  from { opacity: 0; filter: blur(6px); transform: translateY(.18em); }
  60% { filter: blur(0); }
  to { transform: none; }
}
/* Still being written: a little lighter, then brightened in place when final. */
.viewer-caption-line[data-status="draft"] .txt { opacity: .6; transition: opacity .5s var(--ease); }
.viewer-caption-line[data-status="draft"] .viewer-korean .txt { font-style: normal; }
.viewer-caption-line.is-settling .txt { animation: viewer-settle .6s var(--ease) both; }
@keyframes viewer-settle { from { opacity: .6; } to { opacity: 1; } }

/* The caret is a breathing dot, as in jeokja, not a blinking bar. */
.viewer-caret {
  /* Glued to the last word so it never wraps onto a line of its own. */
  display: inline-block; margin-right: -.62em; width: .3em; height: .3em; margin-left: .32em; vertical-align: .1em; border-radius: 50%;
  background: var(--accent); opacity: .8; animation: viewer-caret 1.4s ease-in-out infinite;
}
@keyframes viewer-caret { 50% { transform: scale(.6); opacity: .3; } }

/* Translation not started yet: three breathing dots where the Vietnamese will appear. */
.viewer-typing, .viewer-dots { display: inline-flex; gap: .32em; align-items: center; }
.viewer-typing { height: 1.4em; margin: 2px 0 0; font-size: calc(clamp(1.4375rem, 0.4rem + 4.4vw, 2.0625rem) * var(--caption-scale, 1)); }
.viewer-typing i, .viewer-dots i { width: .3em; height: .3em; border-radius: 50%; background: var(--fg); opacity: .35; animation: viewer-dots 1.8s ease-in-out infinite; }
.viewer-typing i:nth-child(2), .viewer-dots i:nth-child(2) { animation-delay: .25s; }
.viewer-typing i:nth-child(3), .viewer-dots i:nth-child(3) { animation-delay: .5s; }
@keyframes viewer-dots { 30% { opacity: .9; transform: scale(1.2); } }
.viewer-caption-status { display: block; margin-top: 6px; color: var(--faint); font-size: .75rem; }
.viewer-caption-status:empty { display: none; }

/* ── Live end: the sentence being spoken, or breathing dots between sentences ── */
.viewer-listening { padding: 16px 4px 0; }
.viewer-preview { margin: 0; padding-right: .62em; color: #93a8c1; font-size: calc(clamp(1.0625rem, 0.3rem + 3.4vw, 1.375rem) * var(--caption-scale, 1)); line-height: 1.55; word-break: keep-all; overflow-wrap: anywhere; }
.viewer-preview .txt { opacity: .78; }
[data-viewer-mode="ko"] .viewer-preview { color: #b8c6d8; font-size: calc(clamp(1.375rem, 0.4rem + 4.2vw, 1.9375rem) * var(--caption-scale, 1)); }
.viewer-breath { display: flex; align-items: center; gap: 12px; margin: 0; color: var(--faint); font-size: .875rem; }
.viewer-breath .viewer-dots { font-size: 1.375rem; }

/* ── New captions while reading back ── */
.viewer-new-captions {
  position: fixed; z-index: 2; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); display: inline-flex; align-items: center; gap: 8px;
  border: 0; border-radius: 999px; padding: 12px 18px; color: #06243a; background: var(--accent); box-shadow: 0 10px 28px rgb(0 0 0 / .4);
  font-size: .9375rem; font-weight: 750; white-space: nowrap; transform: translateX(-50%); animation: viewer-pill-in .5s var(--spring);
}
.viewer-new-captions svg { width: 16px; height: 16px; }
.viewer-new-captions .count:not(:empty) { min-width: 22px; padding: 1px 7px; border-radius: 999px; background: #06243a; color: var(--accent); font-size: .75rem; text-align: center; }
.viewer-new-captions:active { transform: translateX(-50%) scale(.94); }
@keyframes viewer-pill-in { from { opacity: 0; transform: translate(-50%, 18px) scale(.9); } }

@media (min-width: 761px) { .viewer-shell { border-inline: 1px solid #1e344f; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}
