:root,
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0a0d12;
  --surface: rgba(20, 25, 33, 0.78);
  --surface-solid: #141922;
  --surface-2: #1b222c;
  --surface-3: #242c38;
  --border: rgba(255, 255, 255, 0.07);
  --border-strong: rgba(255, 255, 255, 0.14);
  --text: #e7ecf3;
  --text-muted: #8a95a6;
  --accent: #52aaac;
  --accent-ink: #062021;
  --accent-soft: rgba(82, 170, 172, 0.15);
  --accent-ring: rgba(82, 170, 172, 0.45);
  --danger: #f87171;
  --ok: #34d399;
  --warn: #fbbf24;
  --switch-off: #3a4452;
  --mark: rgba(82, 170, 172, 0.26);
  --shadow: 0 12px 32px rgba(0, 0, 0, 0.45);
  --shadow-soft: 0 6px 18px rgba(0, 0, 0, 0.35);
  --behind-filter: grayscale(1) brightness(0.72);
  --behind-filter-hover: grayscale(0.4) brightness(0.92);
  --stage-bg: #030508;
  --thumb-bg: #05070a;
}

:root[data-theme="light"] {
  color-scheme: light;
  --bg: #eef2f7;
  --surface: rgba(255, 255, 255, 0.8);
  --surface-solid: #ffffff;
  --surface-2: #f1f4f8;
  --surface-3: #e5eaf1;
  --border: rgba(15, 23, 42, 0.08);
  --border-strong: rgba(15, 23, 42, 0.16);
  --text: #0f172a;
  --text-muted: #5b6678;
  --accent: #2e7d7f;
  --accent-ink: #ffffff;
  --accent-soft: rgba(46, 125, 127, 0.13);
  --accent-ring: rgba(46, 125, 127, 0.4);
  --danger: #dc2626;
  --ok: #059669;
  --warn: #d97706;
  --switch-off: #c4ccd8;
  --mark: rgba(82, 170, 172, 0.24);
  --shadow: 0 12px 32px rgba(15, 23, 42, 0.14);
  --shadow-soft: 0 6px 18px rgba(15, 23, 42, 0.1);
  --behind-filter: grayscale(1) contrast(0.75) brightness(1.06);
  --behind-filter-hover: grayscale(0.4) contrast(0.9);
  --stage-bg: #0b0f15;
  --thumb-bg: #0b0f15;
}

:root {
  --accent-stage: #52aaac;
  --accent-stage-ink: #062021;
  --switch-knob-on: #ffffff;
}

:root[data-accent="silver"] { --accent-stage: #cbd5e1; --accent-stage-ink: #0f172a; }
:root[data-theme="dark"][data-accent="silver"] {
  --accent: #cbd5e1;
  --accent-ink: #0f172a;
  --accent-soft: rgba(203, 213, 225, 0.13);
  --accent-ring: rgba(203, 213, 225, 0.4);
  --mark: rgba(203, 213, 225, 0.24);
  --switch-knob-on: #1b222c;
}
:root[data-theme="light"][data-accent="silver"] {
  --accent: #475569;
  --accent-ink: #ffffff;
  --accent-soft: rgba(71, 85, 105, 0.16);
  --accent-ring: rgba(71, 85, 105, 0.5);
  --mark: rgba(100, 116, 139, 0.22);
}

* { box-sizing: border-box; }

.i18n-pending body { visibility: hidden; }

[hidden] { display: none !important; }

html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  -webkit-font-smoothing: antialiased;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-columns: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

button, input { font: inherit; color: inherit; }

:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: 40px; height: 40px; padding: 0;
  border-radius: 12px; border: 1px solid transparent;
  background: transparent; color: var(--text); cursor: pointer;
  transition: background 0.15s, border-color 0.15s;
}
.icon-btn:hover { background: var(--surface-2); border-color: var(--border); }
.icon-btn.small { width: 30px; height: 30px; border-radius: 8px; }

.btn {
  height: 38px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--border-strong); background: var(--surface-2);
  font-weight: 600; cursor: pointer; transition: filter 0.15s, background 0.15s;
}
.btn:hover { background: var(--surface-3); }
.btn.primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }

.link-btn {
  padding: 0; border: 0; background: none; color: var(--accent);
  font-weight: 600; cursor: pointer; text-decoration: underline; text-underline-offset: 2px;
}

.chip {
  display: inline-block; padding: 1px 8px; border-radius: 999px;
  font-size: 11px; letter-spacing: 0.02em; white-space: nowrap;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--text-muted);
}

.spinner {
  display: inline-block; flex: none; width: 16px; height: 16px; border-radius: 50%;
  border: 2px solid currentColor; border-right-color: transparent;
  animation: spin 0.7s linear infinite;
}
.spinner.small { width: 12px; height: 12px; }
@keyframes spin { to { transform: rotate(360deg); } }

.switch {
  appearance: none; -webkit-appearance: none; flex: none; position: relative;
  width: 42px; height: 24px; margin: 0; border-radius: 999px;
  background: var(--switch-off); cursor: pointer; transition: background 0.2s;
}
.switch::after {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px;
  border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.2s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.switch:checked { background: var(--accent); }
.switch:checked::after { transform: translateX(18px); background: var(--switch-knob-on); }
.switch:disabled { opacity: 0.45; cursor: not-allowed; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; cursor: pointer; }
.settings-section .switch-row + .switch-row { margin-top: 14px; }
.switch-text { display: flex; flex-direction: column; gap: 2px; }
.switch-text .hint { margin: 0; }

.topbar {
  position: relative; z-index: 30;
  display: grid; grid-template-columns: 1fr minmax(0, 560px) 1fr; align-items: center; gap: 16px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  background: var(--surface);
  -webkit-backdrop-filter: blur(16px);
  backdrop-filter: blur(16px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 10px; min-width: 0; color: var(--accent); }
.brand-mark { flex: none; color: var(--accent-stage); }
.brand-mark .brand-film { fill: var(--stage-bg); }
.brand-name { color: var(--text); font-weight: 650; letter-spacing: 0.02em; white-space: nowrap; }
.brand-name .brand-x { color: var(--accent); font-weight: 750; }

.topbar-actions { justify-self: end; display: flex; align-items: center; gap: 8px; }

.connection-dot {
  width: 10px; height: 10px; border-radius: 50%; flex: none;
  background: var(--text-muted); transition: background 0.2s, box-shadow 0.2s;
}
.connection-dot[data-state="ok"] { background: var(--ok); box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 20%, transparent); }
.connection-dot[data-state="error"] { background: var(--danger); box-shadow: 0 0 0 4px color-mix(in srgb, var(--danger) 20%, transparent); }
.connection-dot[data-state="no-key"] { background: var(--warn); box-shadow: 0 0 0 4px color-mix(in srgb, var(--warn) 20%, transparent); }
.connection-dot[data-state="connecting"] { background: var(--accent); animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }

.search { position: relative; min-width: 0; }

.search-field {
  display: flex; align-items: center; gap: 6px; height: 42px; padding: 0 6px 0 13px;
  border-radius: 12px; border: 1px solid var(--border-strong); background: var(--surface-2);
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.search-field:focus-within { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); background: var(--surface-solid); }

.search input {
  flex: 1; min-width: 0; height: 100%; padding: 0 4px;
  border: 0; outline: none; background: transparent; font-size: 15px;
  text-overflow: ellipsis;
}
.search input::placeholder { color: var(--text-muted); }

.search-icon { flex: none; color: var(--text-muted); pointer-events: none; }

.search-clear {
  flex: none; width: 30px; height: 30px; padding: 0;
  display: grid; place-items: center; border: 0; border-radius: 8px;
  background: transparent; color: var(--text-muted); cursor: pointer;
}
.search-clear:hover { background: var(--surface-3); color: var(--text); }

.patient-chip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; height: 30px; padding: 0 8px 0 10px;
  border: 0; border-radius: 8px; background: var(--surface-3);
  font-size: 12.5px; color: var(--text-muted); white-space: nowrap; font-variant-numeric: tabular-nums;
  cursor: pointer; transition: background 0.15s, color 0.15s;
}
.patient-chip > span:empty { display: none; }
.patient-chip > span + span::before { content: "·"; margin-right: 6px; opacity: 0.6; }
.patient-chip .chip-born:empty + .chip-images::before,
.patient-chip .chip-born:empty + .chip-images:empty + .chip-dates::before { display: none; }
.patient-chip .chip-dates { color: var(--text); font-weight: 600; }
.patient-chip:hover:not(:disabled), .patient-chip[aria-expanded="true"] { background: var(--accent-soft); color: var(--text); }
.patient-chip:disabled { cursor: default; }
.patient-chip:disabled svg { display: none; }
.patient-chip.dates-elsewhere .chip-dates,
.patient-chip.dates-elsewhere .chip-images { display: none; }
.patient-chip.dates-elsewhere:has(.chip-born:empty) { display: none; }
.search-field:has(input:focus) .patient-chip { display: none; }

.search-popover {
  position: absolute; top: calc(100% + 8px); left: 0; right: 0;
  display: flex; flex-direction: column; max-height: min(62vh, 520px); overflow: hidden;
  background: var(--surface-solid); border: 1px solid var(--border-strong); border-radius: 14px;
  box-shadow: var(--shadow);
  animation: pop-in 0.14s ease-out;
}
@keyframes pop-in { from { opacity: 0; transform: translateY(-4px); } }

.patient-list { list-style: none; margin: 0; padding: 6px; overflow-y: auto; min-height: 0; overscroll-behavior: contain; }
.patient-list:empty { display: none; }

.patient-option {
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; align-items: center; gap: 12px;
  padding: 8px 10px; border-radius: 10px; cursor: pointer;
}
.patient-option.active { background: var(--accent-soft); }

.patient-avatar {
  display: grid; place-items: center; width: 36px; height: 36px; border-radius: 50%;
  background: var(--surface-2); border: 1px solid var(--border);
  color: var(--accent); font-size: 13px; font-weight: 650; letter-spacing: 0.02em;
}
.patient-option.active .patient-avatar { background: var(--surface-solid); }

.patient-main { min-width: 0; }
.patient-name { display: block; overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.patient-name strong { font-weight: 650; }
.patient-meta { display: block; color: var(--text-muted); font-size: 12.5px; font-variant-numeric: tabular-nums; }
.patient-sources { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; max-width: 160px; }

mark { background: var(--mark); color: inherit; border-radius: 3px; }

.search-footer {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px;
  padding: 9px 14px; font-size: 12.5px; color: var(--text-muted);
  border-top: 1px solid var(--border);
}
.patient-list:empty + .search-footer { border-top: 0; }
.search-footer.error-status { color: var(--danger); }

.preview {
  position: relative; display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr);
  gap: 12px; min-height: 0;
  padding: 12px 16px 0;
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.preview.has-image { grid-template-columns: minmax(0, 1fr) var(--adjust-w, 232px); }
.preview { --dates-w: 264px; }
.preview:has(> .date-list:not([hidden])) { grid-template-columns: var(--dates-w) minmax(0, 1fr); }
.preview.has-image:has(> .date-list:not([hidden])) { grid-template-columns: var(--dates-w) minmax(0, 1fr) var(--adjust-w, 232px); }

.stages { display: grid; grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr); gap: 8px; min-width: 0; min-height: 0; }
.stages.comparing { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.stages.comparing .preview-stage.active-pane { border-color: var(--accent-stage); box-shadow: 0 0 0 1px var(--accent-stage); }

.compare-stage.empty { cursor: default; }
.compare-stage.empty .stage-badge { display: none; }
.compare-stage:not(.empty) .compare-empty { display: none; }
.compare-empty h2 { font-size: 17px; }

.adjust {
  display: flex; flex-direction: column; gap: 16px; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 10px 12px 14px; border-radius: 18px;
  background: var(--surface-solid); border: 1px solid var(--border); box-shadow: var(--shadow-soft);
  scrollbar-width: thin;
  animation: slide-in 0.2s ease-out;
}
@keyframes slide-in { from { opacity: 0; transform: translateX(12px); } }

.adjust-head { display: flex; align-items: center; justify-content: space-between; gap: 4px; min-height: 36px; }
.adjust-close { margin-left: auto; }
.compare-toggle[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent-ring); color: var(--accent); }

.adjust-sliders { display: flex; flex-direction: column; gap: 14px; }

.tool-slider { display: flex; flex-direction: column; gap: 6px; color: var(--text-muted); }
.tool-label { display: flex; align-items: center; gap: 8px; font-size: 12.5px; }
.tool-label svg { flex: none; }
.tool-name { color: var(--text); }
.tool-value { margin-left: auto; font-variant-numeric: tabular-nums; }
.tool-slider input[type="range"] { width: 100%; margin: 0; accent-color: var(--accent); cursor: pointer; }

.adjust-toggles { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }

.adjust:not(:has(#previewFlashlight:checked)) .flashlight-size { display: none; }

.tool-btn, .tool-toggle {
  position: relative; display: inline-grid; place-items: center; flex: none;
  width: 36px; height: 36px; padding: 0; border-radius: 10px;
  border: 1px solid transparent; background: transparent; color: var(--text); cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.tool-btn:hover, .tool-toggle:hover { background: var(--surface-2); }
.tool-btn:disabled { opacity: 0.3; cursor: default; background: transparent; }

.adjust-toggles .tool-btn, .adjust-toggles .tool-toggle {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  width: auto; height: 58px; padding: 4px;
  background: var(--surface-2); border-color: var(--border);
  font-size: 11px; line-height: 1.2; text-align: center;
}
.adjust-toggles .tool-btn:hover, .adjust-toggles .tool-toggle:hover { background: var(--surface-3); }
.adjust-toggles .tool-name { color: inherit; }

.tool-toggle input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.adjust-toggles .tool-toggle:has(input:checked) { background: var(--accent-soft); border-color: var(--accent-ring); color: var(--accent); }
.tool-toggle:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }

.preview-nav { display: flex; align-items: center; gap: 2px; }
.preview-nav-position { min-width: 4.5em; text-align: center; font-size: 13px; color: var(--text-muted); font-variant-numeric: tabular-nums; }

.preview-stage {
  position: relative; flex: 1 1 auto; min-height: 0; overflow: hidden;
  display: flex; align-items: center; justify-content: center;
  border-radius: 18px; border: 1px solid var(--border);
  background: radial-gradient(ellipse at 50% 45%, rgba(255, 255, 255, 0.035), transparent 70%), var(--stage-bg);
  touch-action: none;
  cursor: grab;
}
.preview-stage.panning { cursor: grabbing; }
.preview:not(.has-image) .preview-stage { cursor: default; }

.stage-image {
  max-width: 100%; max-height: 100%;
  user-select: none; -webkit-user-select: none; -webkit-user-drag: none; -webkit-touch-callout: none;
}

.flashlight-lens {
  position: absolute; inset: 0; display: none; align-items: center; justify-content: center;
  pointer-events: none;
}
.flashlight-lens.active { display: flex; }
.preview:not(.has-image) .flashlight-lens { display: none; }
.preview-stage.flashlight-active { cursor: none; }
.preview:not(.has-image) .preview-stage.flashlight-active { cursor: default; }

.stage-loading {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-radius: 999px;
  font-size: 13px; color: #e7ecf3; background: rgba(0, 0, 0, 0.6); pointer-events: none;
}

.stage-error {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  max-width: 80%; padding: 8px 14px; border-radius: 10px; text-align: center;
  font-size: 13px; color: #fff; background: #b3261e; border: 1px solid #ef9a9a; pointer-events: none;
}

.stage-badge { position: absolute; top: 12px; left: 12px; display: none; flex-wrap: wrap; gap: 6px; max-width: 45%; pointer-events: none; }
.stage-badge.active { display: flex; }
.preview:not(.has-image) .stage-badge { display: none; }
.stage-badge span {
  padding: 2px 8px; border-radius: 6px; font-size: 11px; font-weight: 700; letter-spacing: 0.06em;
  color: #fff; background: rgba(0, 0, 0, 0.65); border: 1px solid rgba(255, 255, 255, 0.35);
}
.stage-badge span.hazard { background: #b3261e; border-color: #ef9a9a; }

.stage-info {
  position: absolute; top: 12px; right: 12px; display: none; max-width: 50%;
  padding: 6px 10px; border-radius: 10px; text-align: right;
  font-size: 12px; line-height: 1.5; color: #dfe6ee;
  background: rgba(0, 0, 0, 0.6); border: 1px solid rgba(255, 255, 255, 0.18);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  pointer-events: none;
}
.stage-info.active { display: block; }
.stage-info .primary { font-size: 13.5px; font-weight: 650; color: #fff; }

.mount-canvas {
  position: relative; flex: none;
  width: min(100cqw, 100cqh * var(--mount-ratio, 1.5));
}
.mount-slot { position: absolute; overflow: hidden; }
.mount-slot img {
  display: block; width: 100%; height: 100%; object-fit: contain;
  opacity: 0; transition: opacity 0.3s; user-select: none; -webkit-user-drag: none;
}
.mount-slot.loaded img { opacity: 1; }
.mount-slot.placeholder img { opacity: 0.55; }

.mount-view {
  position: absolute; inset: 0; z-index: 2; display: flex; flex-direction: column;
  padding: 72px 16px 16px; cursor: default;
}
.mount-view[hidden] { display: none; }
.mount-view-fit {
  flex: 1 1 auto; min-height: 0; container-type: size;
  display: flex; align-items: center; justify-content: center;
}
.mount-view .mount-slot {
  padding: 0; border-radius: 6px; border: 1px solid rgba(255, 255, 255, 0.16);
  background: var(--thumb-bg); cursor: zoom-in; transition: border-color 0.15s, box-shadow 0.15s;
}
.mount-view .mount-slot:hover,
.mount-view .mount-slot:focus-visible { border-color: var(--accent-stage); box-shadow: 0 0 0 1px var(--accent-stage); outline: none; }
.mount-close {
  position: absolute; top: 12px; left: 12px; color: #e7ecf3;
  background: rgba(0, 0, 0, 0.6); border-color: rgba(255, 255, 255, 0.18);
}
.mount-close:hover { background: rgba(0, 0, 0, 0.8); }
.mount-back[hidden] { display: none; }

.preview-empty {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 24px; text-align: center; color: #c5cfdb; cursor: default;
}
.empty-art { margin-bottom: 4px; color: var(--accent-stage); opacity: 0.85; }
.preview-empty h2 { margin: 0; font-size: 20px; font-weight: 650; color: #eef3f9; }
.preview-empty p { margin: 0; max-width: 44ch; color: #8a95a6; }
.preview-empty .btn { margin-top: 10px; }
.preview-empty .btn.primary { background: var(--accent-stage); color: var(--accent-stage-ink); }
.preview-empty[data-state="connecting"] .empty-art,
.preview-empty[data-state="loading"] .empty-art { animation: pulse 1.2s ease-in-out infinite; }

.timeline {
  --cell: 96px;
  --positions: 10;
  --behind: 0;
  --gap: 8px;
  --arrow: 32px;
  --label: 96px;
  --thumb-h: calc(var(--cell) * 0.75);
  --row-pad: 8px;
  --row-h: calc(var(--thumb-h) + 2 * var(--row-pad) + 2px);
  --step: 46px;
  --recede: 70px;

  position: relative; z-index: 10;
  padding: 10px 16px 12px;
  padding-bottom: max(12px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
}
.timeline[data-state="empty"] { display: none; }

.timeline-status { margin: 0 auto 8px; text-align: center; font-size: 12.5px; color: var(--text-muted); }
.timeline-status:empty { display: none; }
.timeline-status.error-status { color: var(--danger); }

.dates-menu {
  position: absolute; right: 0; top: calc(100% + 8px); z-index: 40;
  width: max-content; min-width: 280px; max-width: calc(100vw - 16px); max-height: 60vh; overflow-y: auto; margin: 0; padding: 0 6px 6px; list-style: none;
  background: var(--surface-solid); border: 1px solid var(--border-strong); border-radius: 12px;
  box-shadow: var(--shadow); animation: pop-in 0.14s ease-out;
}

.stack {
  position: relative;
  height: calc(var(--row-h) + var(--behind) * var(--step));
  perspective: 1000px;
  perspective-origin: 50% 0%;
  transition: height 0.3s;
}
.timeline[data-state="message"] .stack { display: none; }

.trow {
  --depth: 0;
  position: absolute; left: 0; right: 0; bottom: 0;
  display: flex; gap: var(--gap);
  width: max-content; margin-inline: auto;
  padding: var(--row-pad) 10px;
  border-radius: 16px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-soft);
  transform: translateY(calc(var(--depth) * var(--step) * -1)) translateZ(calc(var(--depth) * var(--recede) * -1));
  z-index: calc(20 - var(--depth));
  transition: transform 0.45s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.35s, filter 0.3s, border-color 0.3s, visibility 0s;
}
.trow.active { border-color: var(--accent-ring); box-shadow: var(--shadow), 0 0 0 3px var(--accent-soft); }

.trow.behind { filter: var(--behind-filter); cursor: pointer; }
.trow.behind.intent, .trow.behind:focus-visible { filter: var(--behind-filter-hover); }
.trow.behind:focus-visible { outline-offset: -2px; }
.trow.behind .trow-body { pointer-events: none; }

.trow.stowed {
  opacity: 0; visibility: hidden; pointer-events: none;
  transition: transform 0.45s cubic-bezier(0.2, 0.8, 0.2, 1), opacity 0.3s, filter 0.3s, visibility 0s 0.45s;
}

.trow-label {
  flex: none; display: flex; flex-direction: column; gap: 2px; width: var(--label);
  padding-top: 2px; font-size: 12.5px; line-height: 1.3; white-space: nowrap;
}
.trow-date { font-weight: 650; font-variant-numeric: tabular-nums; }
.trow-meta { color: var(--text-muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.trow-meta > span + span::before { content: "·"; margin: 0 5px; opacity: 0.7; }
.weekday { font-weight: 500; color: var(--text-muted); }
.trow.active .trow-date { color: var(--accent); }

.trow-body { display: flex; align-items: center; gap: var(--gap); }

.timeline.compact-labels .trow-label {
  position: absolute; top: 3px; left: 12px; z-index: 3; width: auto;
  flex-direction: row; align-items: baseline; gap: 6px; padding: 1px 7px;
  border-radius: 6px; font-size: 11px; color: #e7ecf3; background: rgba(0, 0, 0, 0.62);
  pointer-events: none;
}
.timeline.compact-labels .trow-label .weekday { display: none; }
.timeline.compact-labels .trow-label .trow-meta { font-size: 11px; color: #b5c0cc; }
.timeline.compact-labels .trow.active .trow-date { color: var(--accent-stage); }

.trow-viewport {
  width: calc(var(--positions) * var(--cell) + (var(--positions) - 1) * var(--gap));
  overflow-x: auto; overflow-y: hidden;
  scroll-snap-type: x mandatory; overscroll-behavior-x: contain;
  scrollbar-width: none;
  border-radius: 10px;
}
.trow-viewport::-webkit-scrollbar { display: none; }

.trow-track { display: flex; gap: var(--gap); width: max-content; margin: 0 auto; }

.thumb {
  position: relative; flex: none; display: block;
  width: var(--cell); height: var(--thumb-h); padding: 0; overflow: hidden;
  border-radius: 10px; border: 1px solid var(--border-strong);
  background: var(--thumb-bg); cursor: pointer;
  scroll-snap-align: start;
  transition: border-color 0.15s, box-shadow 0.15s;
}
.thumb img {
  display: block; width: 100%; height: 100%; object-fit: contain;
  opacity: 0; transition: opacity 0.3s;
  user-select: none; -webkit-user-drag: none;
}
.thumb.loaded img { opacity: 1; }
.thumb.placeholder img { opacity: 0.55; }

.thumb.mount {
  width: clamp(var(--cell), var(--thumb-h) * var(--mount-ratio, 1), 3 * var(--cell) + 2 * var(--gap));
  display: flex; align-items: center; justify-content: center;
  container-type: size;
}
.thumb.mount .mount-slot { background: rgba(255, 255, 255, 0.04); outline: 1px solid rgba(255, 255, 255, 0.08); outline-offset: -1px; }
.thumb.mount .mount-slot.current { outline: 2px solid var(--accent); outline-offset: -2px; }
.mount-slot:not(.loaded):not(.failed)::before {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 20%, rgba(255, 255, 255, 0.07) 50%, transparent 80%) 0 0 / 200% 100%;
  animation: shimmer 1.3s linear infinite;
}

.thumb:not(.loaded):not(.failed):not(.mount)::before {
  content: ""; position: absolute; inset: 0;
  background: linear-gradient(100deg, transparent 20%, rgba(255, 255, 255, 0.07) 50%, transparent 80%) 0 0 / 200% 100%;
  animation: shimmer 1.3s linear infinite;
}
@keyframes shimmer { from { background-position: 150% 0; } to { background-position: -50% 0; } }

.thumb.failed::before {
  content: ""; position: absolute; inset: 0; margin: auto; width: 20px; height: 20px;
  border-radius: 50%; border: 2px solid rgba(255, 255, 255, 0.25);
  background: linear-gradient(45deg, transparent 45%, rgba(255, 255, 255, 0.25) 45% 55%, transparent 55%);
}

.trow.active .thumb:hover { border-color: var(--accent); }
.thumb.current { border-color: var(--accent); box-shadow: inset 0 0 0 2px var(--accent); }
.timeline.comparing .thumb[data-pane]::after {
  content: attr(data-pane); position: absolute; top: 4px; left: 4px;
  padding: 0 5px; border-radius: 5px; font-size: 10.5px; font-weight: 700; line-height: 16px;
  color: var(--accent-stage-ink); background: var(--accent-stage);
}
.thumb:focus-visible { outline-offset: -2px; }

.trow-arrow {
  display: grid; place-items: center; flex: none;
  width: var(--arrow); height: var(--arrow); padding: 0; border-radius: 50%;
  border: 1px solid var(--border-strong); background: var(--surface-2);
  cursor: pointer; transition: opacity 0.2s, background 0.15s;
}
.trow-arrow:hover:not(:disabled) { background: var(--surface-3); }
.trow-arrow:disabled { opacity: 0.25; cursor: default; }
.trow:not(.overflowing) .trow-arrow, .trow.behind .trow-arrow, .trow.stowed .trow-arrow { visibility: hidden; }

.timeline.overlay-arrows .trow-body { position: relative; }
.timeline.overlay-arrows .trow-arrow { position: absolute; top: 50%; z-index: 2; transform: translateY(-50%); box-shadow: var(--shadow-soft); }
.timeline.overlay-arrows .trow-arrow.prev { left: 4px; }
.timeline.overlay-arrows .trow-arrow.next { right: 4px; }
.timeline.overlay-arrows .trow-arrow:disabled { visibility: hidden; }

.date-entries, .dates-menu, .date-days { list-style: none; margin: 0; }
.date-days { padding: 0; }
.date-days > li, .date-year { position: relative; }
.date-days > li::before, .date-year::before {
  content: ""; position: absolute; left: 25px; top: 0; bottom: 0; width: 2px; background: var(--border);
}

.date-year {
  position: sticky; top: 0; z-index: 2;
  padding: 10px 8px 4px 46px; background: var(--surface-solid);
  font-size: 11.5px; font-weight: 700; letter-spacing: 0.08em; color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.date-entry {
  position: relative; z-index: 1;
  display: grid; grid-template-columns: 36px minmax(0, 1fr) auto; align-items: center; gap: 10px;
  width: 100%; padding: 6px 8px; border: 0; border-radius: 10px;
  background: transparent; text-align: left; cursor: pointer;
}
.date-entry:hover { background: var(--surface-2); }
.date-entry.active { background: var(--accent-soft); }
.date-entry:focus-visible { outline-offset: -2px; }

.date-badge {
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  width: 36px; height: 38px; border-radius: 9px; line-height: 1.05;
  background: var(--surface-solid); border: 1px solid var(--border-strong);
}
.date-badge b { font-size: 14px; font-weight: 700; font-variant-numeric: tabular-nums; }
.date-badge small { font-size: 9.5px; font-weight: 600; letter-spacing: 0.06em; text-transform: uppercase; color: var(--text-muted); }
.date-entry.active .date-badge { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.date-entry.active .date-badge small { color: inherit; opacity: 0.8; }

.date-main { display: flex; flex-direction: column; min-width: 0; }
.date-iso { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.date-entry.active .date-iso { color: var(--accent); }
.date-meta { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.date-sources { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; max-width: 110px; }

.date-list {
  display: flex; flex-direction: column; min-height: 0; overflow: hidden;
  border-radius: 18px; background: var(--surface-solid); border: 1px solid var(--border); box-shadow: var(--shadow-soft);
  animation: slide-in-left 0.2s ease-out;
}
@keyframes slide-in-left { from { opacity: 0; transform: translateX(-12px); } }

.date-list-head {
  display: flex; align-items: center; justify-content: space-between; flex: none;
  padding: 12px 14px 10px; border-bottom: 1px solid var(--border);
  font-size: 11.5px; font-weight: 700; letter-spacing: 0.09em; text-transform: uppercase; color: var(--text-muted);
}
.date-list-count { letter-spacing: 0; text-transform: none; font-weight: 600; font-variant-numeric: tabular-nums; }

.date-entries {
  position: relative; flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain;
  padding: 0 6px 6px; scrollbar-width: thin;
}

.trow.skeleton .trow-track { margin: 0; }
.trow.skeleton .thumb { cursor: default; }

.thumb-zoom {
  position: fixed; z-index: 50; pointer-events: none;
  border-radius: 12px; border: 1px solid var(--accent);
  background: var(--thumb-bg); box-shadow: 0 18px 44px rgba(0, 0, 0, 0.55);
  opacity: 0; transform: scale(0.5); transform-origin: 50% 100%;
  transition: opacity 0.12s, transform 0.16s cubic-bezier(0.2, 0.8, 0.2, 1);
}
.thumb-zoom.visible { opacity: 1; transform: none; }
.thumb-zoom img { display: block; width: 100%; height: 100%; object-fit: contain; border-radius: 11px; }
.thumb-zoom.placeholder img { opacity: 0.55; }
.thumb-zoom img[hidden] { display: none; }
.zoom-mount {
  position: absolute; inset: 4px; container-type: size;
  display: flex; align-items: center; justify-content: center;
}
.zoom-mount .mount-slot { outline: 1px solid rgba(255, 255, 255, 0.1); outline-offset: -1px; }

.thumb-tip {
  position: absolute; left: 50%; bottom: calc(100% + 8px); transform: translateX(-50%);
  padding: 6px 10px; border-radius: 10px; text-align: center; white-space: nowrap;
  font-size: 12px; background: var(--surface-solid); border: 1px solid var(--border-strong); box-shadow: var(--shadow-soft);
}
.tip-date { font-weight: 650; font-variant-numeric: tabular-nums; }
.tip-source { color: var(--text-muted); }

.scrim {
  position: fixed; inset: 0; z-index: 60;
  background: rgba(3, 6, 10, 0.5);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: fade-in 0.2s ease-out;
}
@keyframes fade-in { from { opacity: 0; } }

.settings {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 70;
  display: flex; flex-direction: column; width: min(380px, 100vw);
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-right: env(safe-area-inset-right);
  background: var(--surface-solid); border-left: 1px solid var(--border);
  box-shadow: -24px 0 48px rgba(0, 0, 0, 0.3);
  transform: translateX(105%); visibility: hidden;
  transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s 0.28s;
}
.settings.open { transform: none; visibility: visible; transition: transform 0.28s cubic-bezier(0.2, 0.8, 0.2, 1); }

.settings-head { display: flex; align-items: center; justify-content: space-between; padding: 12px 12px 12px 20px; border-bottom: 1px solid var(--border); }
.settings-head h2 { margin: 0; font-size: 17px; font-weight: 650; }
.settings-body { flex: 1; overflow-y: auto; padding: 4px 20px 24px; overscroll-behavior: contain; }

.settings-section { padding: 18px 0; border-bottom: 1px solid var(--border); }
.settings-section:last-child { border-bottom: 0; }
.settings-section h3 { margin: 0 0 12px; font-size: 11.5px; font-weight: 700; letter-spacing: 0.09em; text-transform: uppercase; color: var(--text-muted); }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.section-head .hint { margin: 0; }

.collapsible > summary { display: flex; align-items: center; justify-content: space-between; list-style: none; cursor: pointer; }
.collapsible > summary::-webkit-details-marker { display: none; }
.collapsible > summary h3 { margin: 0; }
.collapsible > summary::after {
  content: ""; width: 7px; height: 7px; margin: 0 4px 4px 0;
  border-right: 2px solid var(--text-muted); border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg); transition: transform 0.15s;
}
.collapsible[open] > summary { margin-bottom: 12px; }
.collapsible[open] > summary::after { transform: rotate(-135deg); margin-bottom: -4px; }
.collapsible > summary:hover h3 { color: var(--text); }
.collapsible > summary:hover::after { border-color: var(--text); }

.field { display: block; margin: 0 0 6px; font-size: 13px; font-weight: 600; }
.settings input[type="text"], .settings input[type="password"], .settings input:not([type]) {
  width: 100%; min-width: 0; height: 40px; padding: 0 12px;
  border-radius: 10px; border: 1px solid var(--border-strong); background: var(--surface-2);
}
.settings input[type="text"]:focus, .settings input[type="password"]:focus, .settings input:not([type]):focus {
  outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft);
}

.check-row { display: flex; align-items: center; gap: 8px; margin-top: 12px; font-size: 13.5px; cursor: pointer; }
.check-row input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); }

.hint { margin: 6px 0 0; font-size: 12.5px; color: var(--text-muted); }

.form-actions { display: flex; flex-wrap: wrap; align-items: center; gap: 12px; margin-top: 18px; }
.status-line { font-size: 12.5px; color: var(--text-muted); }
.status-line.error-status { color: var(--danger); }
.status-line.ok-status { color: var(--ok); }

.source-list { display: grid; gap: 6px; margin: 12px 0 0; padding: 0; list-style: none; }
.source-item .switch-row { padding: 10px 12px; border-radius: 12px; background: var(--surface-2); border: 1px solid var(--border); }
.source-item.disabled .switch-row { cursor: not-allowed; }
.source-item.disabled .source-text { opacity: 0.55; }
.source-text { display: flex; flex-direction: column; min-width: 0; }
.source-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; }
.source-meta { font-size: 12px; color: var(--text-muted); }
.source-meta:empty { display: none; }
.source-health[data-state="live"] { color: var(--ok); }
.source-health[data-state="down"],
.source-health[data-state="unavailable"] { color: var(--danger); }
.source-empty { padding: 4px 0; font-size: 13px; color: var(--text-muted); }

.swatches { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 8px; margin: 14px 0 0; padding: 0; border: 0; }
.swatches legend { padding: 0; margin-bottom: 8px; }
.swatch {
  position: relative; display: flex; flex-direction: column; align-items: center; gap: 6px;
  padding: 10px 6px 8px; border-radius: 12px; cursor: pointer;
  background: var(--surface-2); border: 1px solid var(--border);
  font-size: 12.5px; transition: border-color 0.15s, background 0.15s;
}
.swatch:hover { background: var(--surface-3); }
.swatch input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.swatch:has(input:checked) { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.swatch:has(input:focus-visible) { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatch-dot { width: 28px; height: 28px; border-radius: 50%; box-shadow: inset 0 0 0 1px rgba(0, 0, 0, 0.15); }
.swatch-dot.silver { background: linear-gradient(135deg, #cbd5e1 50%, #475569 50%); }
.swatch-dot.turquoise { background: linear-gradient(135deg, #52aaac 50%, #2e7d7f 50%); }

.segmented {
  display: flex; gap: 4px; margin: 0; padding: 4px; border: 0; border-radius: 12px;
  background: var(--surface-2);
}
.segment { position: relative; flex: 1; }
.segment input { position: absolute; inset: 0; margin: 0; opacity: 0; cursor: pointer; }
.segment span {
  display: block; padding: 8px 10px; border-radius: 9px; text-align: center;
  font-size: 13.5px; font-weight: 600; color: var(--text-muted); transition: background 0.15s, color 0.15s;
}
.segment:hover span { color: var(--text); }
.segment input:checked + span { background: var(--surface-solid); color: var(--text); box-shadow: var(--shadow-soft); }
.segment input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

@media (max-width: 1000px) {
  .brand-name { display: none; }
  .patient-chip .chip-images { display: none; }
}

@media (max-width: 760px) {
  .topbar { grid-template-columns: auto minmax(0, 1fr) auto; gap: 10px; padding-top: max(8px, env(safe-area-inset-top)); padding-bottom: 8px; }
  .search { position: static; }
  .search-popover { left: 8px; right: 8px; top: calc(100% + 4px); max-height: 70dvh; }
  .dates-menu { right: 8px; top: calc(100% + 4px); }
  .patient-sources { display: none; }
  .patient-chip .chip-born { display: none; }
  .patient-chip .chip-dates::before { display: none; }
  .patient-chip.dates-elsewhere { display: none; }

  .preview { padding: 8px 8px 0; gap: 8px; }
  .preview-stage { border-radius: 14px; }
  .stage-info { max-width: 62%; font-size: 11px; }
  .stage-info .primary { font-size: 12.5px; }

  .timeline { padding: 8px 10px 10px; padding-bottom: max(10px, env(safe-area-inset-bottom)); --step: 36px; --recede: 60px; }
}

@media (max-width: 599px), (max-width: 899px) and (orientation: portrait) {
  .preview.has-image { grid-template-columns: minmax(0, 1fr); grid-template-rows: minmax(0, 1fr) auto; }
  .preview { --dates-w: 220px; }
  .preview.has-image:has(> .date-list:not([hidden])) { grid-template-columns: var(--dates-w) minmax(0, 1fr); }
  .preview.has-image .date-list { grid-row: 1 / span 2; }
  .stages.comparing { grid-template-columns: minmax(0, 1fr); grid-template-rows: repeat(2, minmax(0, 1fr)); }

  .adjust {
    flex-direction: row; flex-wrap: wrap; justify-content: center; align-items: center;
    gap: 4px 10px; padding: 6px; border-radius: 14px; overflow: visible;
    animation: pop-in 0.18s ease-out;
  }
  .adjust .tool-name, .adjust .tool-value { display: none; }
  .adjust-head { min-height: 0; }
  .adjust-sliders { flex-direction: row; flex-wrap: wrap; justify-content: center; gap: 0 4px; }
  .tool-slider { flex-direction: row; align-items: center; gap: 6px; height: 40px; padding: 0 4px; }
  .tool-slider input[type="range"] { width: 80px; }
  .adjust-toggles { display: flex; gap: 2px; }
  .adjust-toggles .tool-btn, .adjust-toggles .tool-toggle { width: 40px; height: 40px; padding: 0; background: transparent; border-color: transparent; }
}

@media (max-width: 599px) {
  .preview > .date-list { display: none; }
  .preview:has(> .date-list:not([hidden])) { grid-template-columns: minmax(0, 1fr); }
  .preview.has-image:has(> .date-list:not([hidden])) { grid-template-columns: minmax(0, 1fr); }

  .adjust { flex-wrap: nowrap; justify-content: flex-start; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
  .adjust::-webkit-scrollbar { display: none; }
  .adjust > * { flex: none; }
  .tool-slider input[type="range"] { width: 72px; }
}

@media (max-height: 520px) {
  .topbar { padding-top: 6px; padding-bottom: 6px; }
  .search-field { height: 38px; }
  .preview { padding-top: 6px; }
  .timeline { padding-top: 6px; --step: 30px; }
}
@media (max-height: 520px) and (orientation: landscape) {
  .preview.has-image { --adjust-w: 200px; }
  .preview { --dates-w: 200px; }
  .date-list-head { padding: 6px 10px; }
  .date-badge { height: 32px; }
  .adjust { gap: 8px; padding: 6px 8px; }
  .adjust .tool-name, .adjust .tool-value { display: none; }
  .adjust-sliders { gap: 4px; }
  .tool-slider { flex-direction: row; align-items: center; gap: 8px; }
  .adjust-toggles { grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 3px; }
  .adjust-toggles .tool-btn, .adjust-toggles .tool-toggle { height: 38px; }
}

@media (pointer: coarse) {
  .tool-btn, .tool-toggle { width: 42px; height: 42px; }
  .icon-btn.small { width: 38px; height: 38px; }
  .switch { width: 48px; height: 28px; }
  .switch::after { width: 22px; height: 22px; }
  .switch:checked::after { transform: translateX(20px); }
}

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