/* Visit record — one stylesheet, design tokens first.
   World: a clinical referral card. Cool white ground, one cobalt field for the visit identity,
   heat colours (amber → coral) reserved for pain and "discuss first", green only for "discussed".
   System fonts: the CSP allows no web fonts, and a phone held up in a clinic should read instantly. */

:root {
  /* Colour */
  --ground: #eef1f6;
  --surface: #ffffff;
  --surface-2: #f6f8fb;
  --ink: #0e1726;
  --ink-2: #445066;
  --ink-3: #626d82;
  --line: #dbe1ea;
  --line-strong: #c3ccd9;
  --cobalt: #1d3bc6;
  --cobalt-deep: #152c9a;
  --cobalt-wash: #e9edff;
  --cobalt-tint: #c9d3ff;
  --on-cobalt: #ffffff;
  --on-cobalt-2: #d3dbff;
  --amber: #ffcf85;
  --coral: #ff7d5c;
  --flag: #b0400e;
  --flag-wash: #fff3ea;
  --flag-line: #f6d3bd;
  --done: #0b7a55;
  --done-wash: #e8f6ef;
  --error: #b42318;
  --error-wash: #fdeceb;
  --night: #07090e;
  --night-2: #121722;
  --night-3: #1c2331;
  --night-line: #2a3345;
  --night-ink: #e7ebf3;
  --night-ink-2: #a3adc2;
  --night-accent: #8ea2ff;

  /* Type: a 1.2 scale on a 16px base */
  --sans: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --arabic: 'SF Arabic', 'Geeza Pro', 'Noto Naskh Arabic', 'Segoe UI', Tahoma, sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;
  --fs-xs: .75rem;
  --fs-sm: .8125rem;
  --fs-md: .9375rem;
  --fs-base: 1rem;
  --fs-lg: 1.125rem;
  --fs-xl: 1.375rem;
  --fs-2xl: 1.75rem;
  --fs-3xl: 2.25rem;

  /* Space and shape */
  --s1: .25rem; --s2: .5rem; --s3: .75rem; --s4: 1rem; --s5: 1.5rem; --s6: 2rem; --s7: 3rem;
  --radius-sm: .5rem;
  --radius: .75rem;
  --radius-lg: 1.25rem;
  --page: min(100% - 2rem, 74rem);
  --bar-h: 3.5rem;
  --jump-h: 3.25rem;
  --shadow: 0 1px 2px rgb(14 23 38 / 6%), 0 10px 28px -16px rgb(14 23 38 / 28%);
  --ease: cubic-bezier(.2, .8, .2, 1);
  --fast: 160ms;

  font: 100%/1.55 var(--sans);
  color: var(--ink);
  background: var(--ground);
  font-synthesis: none;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
  accent-color: var(--cobalt);
  caret-color: var(--cobalt);
}

/* ——— Base ——— */
*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; min-height: 100vh; min-height: 100dvh; }
body[data-view='study'] { overflow: hidden; background: var(--night); }
h1, h2, h3, h4, p, ol, ul, dl, dd, figure, blockquote { margin: 0; padding: 0; }
ol, ul { list-style: none; }
h1, h2, h3, h4 { text-wrap: balance; letter-spacing: -.012em; }
p { overflow-wrap: break-word; text-wrap: pretty; }
img { display: block; max-width: 100%; }
button, input, select { font: inherit; color: inherit; }
a { color: var(--cobalt); text-underline-offset: .2em; }
::selection { background: var(--cobalt-tint); color: var(--ink); }
:focus-visible { outline: 2px solid var(--cobalt); outline-offset: 2px; border-radius: var(--radius-sm); }
h1:focus, h1:focus-visible { outline: none; }
.icon { width: 1.25rem; height: 1.25rem; flex: none; fill: none; stroke: currentColor; stroke-width: 1.75; stroke-linecap: round; stroke-linejoin: round; }
.icon.icon-sm { width: 1rem; height: 1rem; }

/* ——— Buttons ——— */
.primary, .secondary, .ghost, .icon-only, .text-button {
  display: inline-flex; align-items: center; justify-content: center; gap: var(--s2);
  min-height: 2.75rem; padding: 0 var(--s4);
  border: 1px solid transparent; border-radius: var(--radius-sm);
  font-weight: 600; font-size: var(--fs-md); line-height: 1.2; text-decoration: none;
  cursor: pointer; white-space: nowrap; background: none;
  transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease), color var(--fast) var(--ease), transform var(--fast) var(--ease);
}
.primary { background: var(--cobalt); color: var(--on-cobalt); }
.primary:hover { background: var(--cobalt-deep); }
.secondary { background: var(--surface); color: var(--ink); border-color: var(--line-strong); }
.secondary:hover { border-color: var(--cobalt); color: var(--cobalt); }
.ghost { color: var(--ink-2); }
.ghost:hover { background: var(--surface-2); color: var(--ink); }
.primary:active, .secondary:active, .ghost:active, .icon-only:active { transform: translateY(1px); }
.small { min-height: 2.25rem; padding: 0 var(--s3); font-size: var(--fs-sm); }
.wide { width: 100%; }
.icon-only { width: 2.75rem; padding: 0; color: var(--ink-2); }
.icon-only:hover { background: var(--surface-2); color: var(--ink); }
.text-button { min-height: 2rem; padding: 0; color: var(--cobalt); font-size: var(--fs-sm); }
.text-button:hover { color: var(--cobalt-deep); text-decoration: underline; }
button:disabled { opacity: .4; cursor: default; transform: none; }

/* ——— Top bar ——— */
.topbar { position: sticky; top: 0; z-index: 20; background: var(--surface); border-bottom: 1px solid var(--line); padding-top: env(safe-area-inset-top); }
.topbar-inner { width: var(--page); margin: auto; height: var(--bar-h); display: flex; align-items: center; justify-content: space-between; gap: var(--s3); }
.brand { display: flex; align-items: center; gap: var(--s2); font-weight: 700; letter-spacing: -.01em; }
.brand-mark { display: grid; place-items: center; width: 2rem; height: 2rem; border-radius: var(--radius-sm); background: var(--cobalt); color: var(--on-cobalt); }
.brand-mark .icon { width: 1.125rem; height: 1.125rem; }
.topbar .back { margin-left: calc(var(--s3) * -1); }
.topbar .lock { margin-right: calc(var(--s3) * -1); }

/* ——— Code gate ——— */
.gate { width: var(--page); margin: auto; min-height: calc(100dvh - var(--bar-h)); display: grid; align-content: center; justify-items: center; gap: var(--s5); padding: var(--s6) 0; }
.gate-panel { width: min(100%, 28rem); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); padding: clamp(1.5rem, 6vw, 2.5rem); box-shadow: var(--shadow); }
.gate-mark { display: grid; place-items: center; width: 3rem; height: 3rem; border-radius: var(--radius); background: var(--cobalt); color: var(--on-cobalt); margin-bottom: var(--s5); }
.gate-mark .icon { width: 1.5rem; height: 1.5rem; }
.gate h1 { font-size: var(--fs-2xl); line-height: 1.15; }
.gate .lead { color: var(--ink-2); margin: var(--s2) 0 var(--s5); }
.gate form { display: grid; gap: var(--s2); }
.gate label { font-weight: 600; font-size: var(--fs-sm); }
.code-field { display: flex; align-items: center; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); background: var(--surface-2); transition: border-color var(--fast) var(--ease), box-shadow var(--fast) var(--ease); }
.code-field:focus-within { border-color: var(--cobalt); box-shadow: 0 0 0 3px var(--cobalt-wash); background: var(--surface); }
.code-field.invalid { border-color: var(--error); box-shadow: 0 0 0 3px var(--error-wash); }
.code-field input { flex: 1; min-width: 0; border: 0; background: none; padding: var(--s3) var(--s4); font: 500 var(--fs-lg)/1.3 var(--mono); letter-spacing: .04em; }
.code-field input:focus { outline: none; }
.code-field .reveal { width: 2.75rem; min-height: 2.75rem; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; }
.gate .primary { margin-top: var(--s2); min-height: 3rem; font-size: var(--fs-base); }
.feedback { color: var(--error); font-size: var(--fs-sm); }
.feedback:empty { display: none; }
.gate-note { display: flex; align-items: center; gap: var(--s2); color: var(--ink-3); font-size: var(--fs-sm); text-align: center; }

/* ——— Visit band (signature: the patient's words + a shared 0–10 pain scale) ——— */
.visit-band { background: var(--cobalt); color: var(--on-cobalt); }
.band-inner { width: var(--page); margin: auto; padding: var(--s6) 0 var(--s6); display: grid; gap: var(--s5); }
.visit-band h1 { font-size: clamp(1.75rem, 1.2rem + 2.6vw, 2.75rem); line-height: 1.08; letter-spacing: -.025em; font-weight: 750; }
.band-meta { margin-top: var(--s2); color: var(--on-cobalt-2); font-size: var(--fs-md); font-variant-numeric: tabular-nums; white-space: pre-wrap; }
.statement { position: relative; margin-top: var(--s5); padding-left: 2rem; max-width: 40rem; }
.statement p { font-size: var(--fs-lg); line-height: 1.5; }
.quote-mark { position: absolute; left: 0; top: .1rem; width: 1.5rem; height: 1.5rem; color: var(--amber); stroke-width: 2.25; }
.profile { display: grid; gap: var(--s4); align-content: start; background: var(--cobalt-deep); border-radius: var(--radius); padding: var(--s4) var(--s4) var(--s5); }
.facts div { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s3); }
.facts dt { color: var(--on-cobalt-2); font-size: var(--fs-sm); }
.facts dd { font-size: var(--fs-xl); font-weight: 700; font-variant-numeric: tabular-nums; letter-spacing: -.01em; }
.scale { display: grid; gap: var(--s2); padding-top: var(--s4); border-top: 1px solid rgb(255 255 255 / 16%); }
.scale-track { position: relative; height: .625rem; border-radius: 999px; background: rgb(255 255 255 / 14%); margin-top: var(--s3); }
.scale-ticks { position: absolute; inset: -.4rem 0 auto; display: flex; justify-content: space-between; }
.scale-ticks i { width: 1px; height: .3rem; background: rgb(255 255 255 / 40%); }
.scale-band { position: absolute; top: 0; bottom: 0; left: calc(var(--from) * 100%); width: calc((var(--to) - var(--from)) * 100%); border-radius: 999px; }
.scale-band.level-0 { background: var(--amber); }
.scale-band.level-1, .scale-band.level-2 { background: var(--coral); }
.scale-ends { display: flex; justify-content: space-between; color: var(--on-cobalt-2); font-size: var(--fs-xs); font-variant-numeric: tabular-nums; }
.scale-legend { display: grid; grid-template-columns: 1fr 1fr; gap: var(--s3); margin-top: var(--s1); }
.scale-legend div { display: grid; gap: .1rem; padding-left: 1.125rem; position: relative; }
/* A round swatch keys each legend entry to its band on the scale above. */
.scale-legend div::before { content: ''; position: absolute; left: 0; top: .4rem; width: .625rem; height: .625rem; border-radius: 50%; background: var(--amber); }
.scale-legend .level-1::before, .scale-legend .level-2::before { background: var(--coral); }
.scale-legend dt { color: var(--on-cobalt-2); font-size: var(--fs-sm); }
.scale-legend dd { font-size: var(--fs-xl); font-weight: 700; font-variant-numeric: tabular-nums; }

@media (max-width: 39.99rem) {
  /* Phones: a tighter band so the imaging strip starts inside the first screen. */
  .band-inner { padding: var(--s5) 0; gap: var(--s4); }
  .statement { margin-top: var(--s4); padding-left: 1.75rem; }
  .statement p { font-size: var(--fs-base); }
  .quote-mark { width: 1.25rem; height: 1.25rem; }
  .profile { padding: var(--s3) var(--s4) var(--s4); gap: var(--s3); }
  .scale { padding-top: var(--s3); }
  .facts dd, .scale-legend dd { font-size: var(--fs-lg); }
}

/* ——— Jump bar ——— */
.jump { position: sticky; top: calc(var(--bar-h) + env(safe-area-inset-top)); z-index: 15; background: var(--ground); border-bottom: 1px solid var(--line); }
.jump-inner { width: var(--page); margin: auto; height: var(--jump-h); display: flex; align-items: center; gap: var(--s1); overflow-x: auto; scrollbar-width: none; }
.jump-inner::-webkit-scrollbar { display: none; }
.jump-link { display: inline-flex; align-items: center; gap: var(--s2); height: 2.25rem; padding: 0 var(--s3); border-radius: 999px; color: var(--ink-2); font-weight: 600; font-size: var(--fs-md); text-decoration: none; white-space: nowrap; transition: background-color var(--fast) var(--ease), color var(--fast) var(--ease); }
.jump-link:hover { color: var(--ink); background: var(--surface); }
.jump-link[aria-current='true'] { background: var(--ink); color: var(--surface); }
.jump-count { font-size: var(--fs-xs); font-variant-numeric: tabular-nums; padding: .1rem .45rem; border-radius: 999px; background: var(--surface); color: var(--ink-2); border: 1px solid var(--line); }
.jump-link[aria-current='true'] .jump-count { background: rgb(255 255 255 / 16%); color: var(--surface); border-color: transparent; }
@media (max-width: 39.99rem) {
  /* All four links must fit a 320–390px screen without sideways scrolling. */
  .jump-inner { gap: 0; justify-content: space-between; }
  .jump-link { padding: 0 .55rem; gap: .3rem; font-size: var(--fs-sm); }
  .jump-count { padding: .05rem .35rem; }
}

/* ——— Home layout: one column on phones, checklist + side column on wide screens ——— */
.home { padding-bottom: var(--s7); }
.home-grid { width: var(--page); margin: auto; display: grid; gap: var(--s7) var(--s6); padding-top: var(--s6); }
.home-grid > section { scroll-margin-top: calc(var(--bar-h) + var(--jump-h) + var(--s4)); min-width: 0; }
@media (min-width: 64rem) {
  .home-grid { grid-template-columns: minmax(0, 1fr) 22.5rem; grid-template-areas: 'discuss imaging' 'discuss sources' 'history sources'; grid-template-rows: auto auto 1fr; align-items: start; }
  .imaging { grid-area: imaging; }
  .discuss { grid-area: discuss; }
  .history { grid-area: history; }
  .sources { grid-area: sources; }
  .band-inner { grid-template-columns: minmax(0, 1fr) 22.5rem; align-items: end; gap: var(--s6); padding: var(--s7) 0 var(--s6); }
}
.section-head { display: flex; align-items: baseline; justify-content: space-between; gap: var(--s3) var(--s4); flex-wrap: wrap; margin-bottom: var(--s3); }
.section-head h2 { font-size: var(--fs-xl); line-height: 1.2; font-weight: 750; }
.hint { color: var(--ink-3); font-size: var(--fs-sm); margin-bottom: var(--s4); }
.group-title { display: flex; align-items: center; gap: var(--s2); font-size: var(--fs-base); font-weight: 700; color: var(--ink); margin: var(--s6) 0 var(--s3); }
.group-title.flagged { color: var(--flag); margin-top: var(--s2); }
.page-note { width: var(--page); margin: var(--s7) auto 0; padding-top: var(--s4); border-top: 1px solid var(--line); color: var(--ink-3); font-size: var(--fs-sm); }

/* ——— Discussion checklist ——— */
.progress { display: grid; gap: var(--s1); justify-items: end; min-width: 10rem; }
.progress-text { font-size: var(--fs-sm); font-weight: 600; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.meter { width: 10rem; height: .375rem; border-radius: 999px; background: var(--line); overflow: hidden; }
.meter span { display: block; height: 100%; width: calc(var(--done, 0) * 100%); background: var(--done); border-radius: inherit; transition: width 240ms var(--ease); }
.discuss.complete .progress-text { color: var(--done); }
@media (max-width: 39.99rem) {
  /* Under the heading on phones, the counter and meter span the full width. */
  .progress { width: 100%; justify-items: stretch; }
  .meter { width: 100%; }
}
.topic-list { display: grid; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.topic-list.priority { gap: var(--s3); background: none; border: 0; overflow: visible; }
.topic { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 0 var(--s1); padding: var(--s4) var(--s4) var(--s4) var(--s1); transition: background-color var(--fast) var(--ease); }
.topic + .topic { border-top: 1px solid var(--line); }
.topic.is-priority { background: var(--flag-wash); border: 1px solid var(--flag-line); border-radius: var(--radius); }
.topic.is-priority + .topic.is-priority { border-top: 1px solid var(--flag-line); }
.topic.done { background: var(--done-wash); }
.topic.is-priority.done { border-color: #bfe5d2; }
.topic.done .topic-title { color: var(--ink-2); }
.check-hit { display: grid; place-items: start center; width: 2.75rem; padding-top: .05rem; cursor: pointer; align-self: stretch; }
.check { appearance: none; -webkit-appearance: none; margin: 0; width: 1.5rem; height: 1.5rem; border: 2px solid var(--ink-3); border-radius: .4rem; background: var(--surface); display: grid; place-items: center; cursor: pointer; transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease); }
.check::after { content: ''; width: .4rem; height: .75rem; border: solid var(--surface); border-width: 0 2.5px 2.5px 0; transform: translateY(-1px) rotate(45deg) scale(0); transition: transform var(--fast) var(--ease); }
.check:checked { background: var(--done); border-color: var(--done); }
.check:checked::after { transform: translateY(-1px) rotate(45deg) scale(1); }
.check:hover { border-color: var(--done); }
.topic-title { font-size: var(--fs-base); font-weight: 650; line-height: 1.35; }
.topic-note { margin-top: var(--s1); color: var(--ink-2); font-size: var(--fs-md); max-width: 68ch; }
.topic-note.clamped { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; overflow: hidden; }
.is-priority .topic-note { color: var(--ink); }
/* One-line summary: the line you scan. The patient's full wording sits below it in a quiet panel. */
.summary { margin-top: var(--s1); color: var(--ink); font-size: var(--fs-md); line-height: 1.5; max-width: 68ch; }
.full-note { margin-top: var(--s2); padding: var(--s3) var(--s3); border-radius: var(--radius-sm); background: var(--surface-2); color: var(--ink-2); font-size: var(--fs-sm); line-height: 1.6; max-width: 68ch; }
.is-priority .full-note { background: rgb(255 255 255 / 65%); color: var(--ink-2); }
.topic.done .full-note { background: rgb(255 255 255 / 60%); }
.entry .summary { color: var(--ink); }
.entry .full-note { color: var(--ink-2); font-size: var(--fs-sm); }
.more { margin-top: var(--s1); gap: var(--s1); }
.more .icon { width: 1rem; height: 1rem; transition: transform var(--fast) var(--ease); }
.more[aria-expanded='true'] .icon { transform: rotate(180deg); }
.source-links { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s3); }
.source-chip { display: inline-flex; align-items: center; gap: .375rem; min-height: 2rem; padding: .25rem .7rem; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--cobalt); font-size: var(--fs-sm); font-weight: 550; text-decoration: none; line-height: 1.25; transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease); }
.source-chip:hover { background: var(--cobalt-wash); border-color: var(--cobalt-tint); }

/* ——— Imaging & documents ——— */
.study-strip { display: grid; gap: var(--s4); }
@media (max-width: 39.99rem) {
  /* Phones: a horizontal strip so all studies are visible without scrolling the page far. */
  .study-strip { grid-auto-flow: column; grid-auto-columns: min(80%, 19rem); overflow-x: auto; scroll-snap-type: x mandatory; scroll-padding: 0 1rem; margin: 0 -1rem; padding: 0 1rem var(--s2); scrollbar-width: none; }
  .study-strip::-webkit-scrollbar { display: none; }
  .study-card { scroll-snap-align: start; }
}
@media (min-width: 40rem) and (max-width: 63.99rem) { .study-strip { grid-template-columns: repeat(auto-fill, minmax(16rem, 1fr)); } }
.study-card { display: grid; grid-template-rows: auto 1fr; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow); }
.study-preview { position: relative; display: block; width: 100%; aspect-ratio: 16 / 10; padding: 0; border: 0; background: var(--night); cursor: zoom-in; overflow: hidden; }
.study-preview img { width: 100%; height: 100%; object-fit: cover; opacity: 1; transition: opacity 280ms var(--ease), transform 400ms var(--ease); }
.study-preview:hover img { transform: scale(1.03); }
.study-preview.loading img { opacity: 0; }
.study-preview.loading::before { content: ''; position: absolute; inset: 0; background: linear-gradient(100deg, transparent 30%, rgb(255 255 255 / 7%) 50%, transparent 70%) 0 0 / 200% 100%; animation: shimmer 1.4s linear infinite; }
.preview-badge { position: absolute; left: var(--s3); bottom: var(--s3); display: inline-flex; align-items: center; gap: .35rem; padding: .25rem .6rem; border-radius: 999px; background: rgb(7 9 14 / 72%); color: var(--night-ink); font-size: var(--fs-xs); font-weight: 600; }
.preview-failed { position: absolute; inset: 0; display: grid; place-items: center; color: var(--night-ink-2); font-size: var(--fs-sm); }
.study-info { display: grid; align-content: start; gap: var(--s1); padding: var(--s4); }
.study-info h3 { font-size: var(--fs-lg); line-height: 1.25; font-weight: 700; }
.study-meta { color: var(--ink-3); font-size: var(--fs-sm); }
.study-actions { display: flex; flex-wrap: wrap; gap: var(--s2); margin-top: var(--s3); }
.doc-list { display: grid; margin-top: var(--s4); background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
.doc-list li + li { border-top: 1px solid var(--line); }
.doc-row { width: 100%; display: flex; align-items: center; gap: var(--s3); padding: var(--s3) var(--s4); border: 0; background: none; text-align: left; cursor: pointer; transition: background-color var(--fast) var(--ease); }
.doc-row:hover { background: var(--surface-2); }
.doc-row:focus-visible { outline-offset: -2px; }
.doc-icon { display: grid; place-items: center; width: 2.5rem; height: 2.5rem; flex: none; border-radius: var(--radius-sm); background: var(--cobalt-wash); color: var(--cobalt); }
.doc-text { display: grid; flex: 1; min-width: 0; gap: .1rem; }
.doc-text strong { font-weight: 600; font-size: var(--fs-md); line-height: 1.3; }
.doc-text span { color: var(--ink-3); font-size: var(--fs-sm); }
.chevron-right { color: var(--ink-3); }
.sources .hint { margin-bottom: 0; }

/* ——— History ——— */
.history .group-title { margin-top: var(--s6); }
.history .timeline .group-title { margin-top: var(--s2); }
.entries { display: grid; }
.entry h4 { font-size: var(--fs-base); font-weight: 650; line-height: 1.35; }
.entry p { margin-top: var(--s1); color: var(--ink-2); font-size: var(--fs-md); max-width: 68ch; }
.entry .source-links { margin-top: var(--s2); }
.timeline .entries { position: relative; padding-left: var(--s5); }
.timeline .entries::before { content: ''; position: absolute; left: .3rem; top: .6rem; bottom: .6rem; width: 2px; border-radius: 1px; background: var(--cobalt-tint); }
.timeline .entry { position: relative; padding-bottom: var(--s5); }
.timeline .entry:last-child { padding-bottom: 0; }
.timeline .entry::before { content: ''; position: absolute; left: calc(var(--s5) * -1); top: .35rem; width: .75rem; height: .75rem; border-radius: 50%; background: var(--ground); border: 2px solid var(--cobalt); }
.fact-group .entries { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.fact-group .entry { padding: var(--s4); }
.fact-group .entry + .entry { border-top: 1px solid var(--line); }

/* ——— Reader ——— */
.reader { width: var(--page); margin: auto; padding: var(--s5) 0 var(--s7); }
.sheet { max-width: 48rem; margin: auto; background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-lg); box-shadow: var(--shadow); padding: clamp(1.25rem, 5vw, 2.75rem); }
.sheet-head { padding-bottom: var(--s4); margin-bottom: var(--s5); border-bottom: 1px solid var(--line); }
.sheet h1 { font-size: var(--fs-2xl); line-height: 1.2; font-weight: 750; }
.sheet-kind { display: flex; align-items: center; gap: var(--s2); margin-top: var(--s2); color: var(--ink-3); font-size: var(--fs-sm); }
.sheet-loading { color: var(--ink-3); }
.original { white-space: pre-wrap; overflow-wrap: anywhere; font-size: 1.0625rem; line-height: 1.75; max-width: 70ch; }
.original[lang='ar'] { font-family: var(--arabic); font-size: 1.125rem; line-height: 1.9; }
.pdf-link { margin: var(--s3) 0 var(--s5); }
.pdf-frame { display: none; width: 100%; height: 75vh; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface-2); }
@media (min-width: 48rem) { .pdf-frame { display: block; } }

/* ——— Viewer (full screen, dark: images read best on black) ——— */
.viewer { position: fixed; inset: 0; z-index: 30; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: auto minmax(0, 1fr) auto; background: var(--night); color: var(--night-ink); }
.viewer :focus-visible { outline-color: var(--night-accent); }
.viewer ::selection { background: var(--night-3); color: var(--night-ink); }
.viewer-bar { display: flex; align-items: center; gap: var(--s3); padding: calc(var(--s2) + env(safe-area-inset-top)) var(--s3) var(--s2); background: var(--night-2); border-bottom: 1px solid var(--night-line); }
.viewer-title { flex: 1; min-width: 0; }
.viewer-title h1 { font-size: var(--fs-base); font-weight: 700; line-height: 1.25; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.viewer-sub { color: var(--night-ink-2); font-size: var(--fs-sm); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.on-dark { color: var(--night-ink); }
.ghost.on-dark:hover, .icon-only.on-dark:hover { background: var(--night-3); color: #fff; }
.stage { position: relative; min-height: 0; touch-action: pan-y pinch-zoom; user-select: none; -webkit-user-select: none; }
.frame { position: absolute; inset: 0; overflow: hidden; }
.frame.zoomed { overflow: auto; touch-action: auto; cursor: grab; }
.frame img { width: calc(var(--zoom, 1) * 100%); height: calc(var(--zoom, 1) * 100%); max-width: none; object-fit: contain; }
.stage-nav { position: absolute; top: 50%; translate: 0 -50%; width: 3rem; height: 3rem; border-radius: 50%; background: rgb(18 23 34 / 78%); color: var(--night-ink); border: 1px solid var(--night-line); }
.stage-nav:hover { background: var(--night-3); color: #fff; }
.stage-nav:disabled { opacity: 0; pointer-events: none; }
.stage-nav.prev { left: var(--s4); }
.stage-nav.next { right: var(--s4); }
@media (max-width: 39.99rem), (pointer: coarse) { .stage-nav { display: none; } }
.stage.busy::after { content: ''; position: absolute; top: var(--s4); left: var(--s4); width: 1.25rem; height: 1.25rem; border-radius: 50%; border: 2px solid var(--night-line); border-top-color: var(--night-accent); animation: spin .8s linear infinite; }
.stage-status { position: absolute; inset: auto 0 var(--s5); display: flex; justify-content: center; align-items: center; gap: var(--s3); color: var(--night-ink); font-size: var(--fs-sm); pointer-events: none; }
.stage-status button { pointer-events: auto; }
.viewer-controls { display: grid; gap: var(--s3); padding: var(--s3) var(--s3) calc(var(--s3) + env(safe-area-inset-bottom)); background: var(--night-2); border-top: 1px solid var(--night-line); }
.series-tabs { display: flex; gap: var(--s2); overflow-x: auto; scrollbar-width: none; }
.series-tabs::-webkit-scrollbar { display: none; }
.series-chip { display: inline-flex; align-items: center; gap: var(--s2); flex: none; min-height: 2.25rem; padding: 0 var(--s3); border-radius: 999px; border: 1px solid var(--night-line); background: var(--night-3); color: var(--night-ink); font-size: var(--fs-sm); font-weight: 600; cursor: pointer; transition: background-color var(--fast) var(--ease), border-color var(--fast) var(--ease); }
.series-chip:hover { border-color: var(--night-accent); }
.series-chip[aria-pressed='true'] { background: var(--night-ink); color: var(--night); border-color: var(--night-ink); }
.chip-count { font-variant-numeric: tabular-nums; font-size: var(--fs-xs); opacity: .7; }
.scrub-row { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; gap: var(--s2); }
.scrub { width: 100%; accent-color: var(--night-accent); height: 2.75rem; margin: 0; cursor: pointer; }
.counter { min-width: 5ch; text-align: center; font-variant-numeric: tabular-nums; font-size: var(--fs-sm); font-weight: 600; color: var(--night-ink); }
/* Floating zoom control in the picture's top-right corner. */
.zoom-group { position: absolute; top: var(--s3); right: var(--s3); display: flex; padding: 2px; border-radius: var(--radius); background: rgb(18 23 34 / 82%); border: 1px solid var(--night-line); }
.zoom-group .icon-only { width: 2.5rem; min-height: 2.5rem; }
.filmstrip { display: flex; gap: .375rem; overflow-x: auto; padding: 2px; scrollbar-width: thin; scrollbar-color: var(--night-line) transparent; }
.thumb { position: relative; flex: none; width: 3.25rem; height: 3.25rem; padding: 0; border: 1px solid var(--night-line); border-radius: .375rem; background: var(--night-3); overflow: hidden; cursor: pointer; opacity: .62; transition: opacity var(--fast) var(--ease), border-color var(--fast) var(--ease); }
.thumb img { width: 100%; height: 100%; object-fit: cover; }
.thumb:hover { opacity: .9; }
.thumb[aria-current='true'] { opacity: 1; border-color: var(--night-accent); box-shadow: 0 0 0 1px var(--night-accent); }
.thumb-number { position: absolute; right: 2px; bottom: 1px; padding: 0 .2rem; border-radius: 3px; background: rgb(7 9 14 / 75%); font-size: .625rem; font-variant-numeric: tabular-nums; color: var(--night-ink); }
.viewer-help { color: var(--night-ink-2); font-size: var(--fs-xs); }
@media (max-width: 39.99rem) {
  /* Phones: Back and Report become icon buttons so the study title has room; labels stay for screen readers. */
  .viewer-bar .ghost { width: 2.75rem; padding: 0; }
  .viewer-bar .ghost span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
  .viewer-help { display: none; }
}
@media (min-width: 64rem) {
  /* Wide screens: image on the left, controls as a side panel on the right. */
  .viewer { grid-template-columns: minmax(0, 1fr) 22rem; grid-template-rows: auto minmax(0, 1fr); }
  .viewer-bar { grid-column: 1 / -1; }
  .viewer-controls { grid-row: 2; grid-column: 2; align-content: start; border-top: 0; border-left: 1px solid var(--night-line); padding: var(--s4); overflow-y: auto; }
  .series-tabs { flex-wrap: wrap; overflow: visible; }
  .filmstrip { flex-wrap: wrap; overflow: visible; }
  .thumb { width: calc((100% - 4 * .375rem) / 5); height: auto; aspect-ratio: 1; }
}

/* ——— Motion ——— */
@keyframes shimmer { to { background-position: -200% 0; } }
@keyframes spin { to { rotate: 360deg; } }
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition-duration: 0ms !important; animation-duration: 0ms !important; animation-iteration-count: 1 !important; }
  .study-preview:hover img { transform: none; }
}
