/* PTA Study Lab — reusable components (OWNER: design lead).
 * Buttons, chips, rings, bars, segmented controls, switches, sheets/dialogs, notes, empty states.
 * Teammates: prefer these classes over new ones (btn, btn-primary, btn-ghost, btn-sm, btn-lg, btn-block,
 * icon-btn, chip, card, panel, note note-tip|warning|safety|exam|info, bar, segmented, switch, sheet).
 */

/* ── Buttons ──────────────────────────────────────────────────────────── */
.btn {
  --btn-bg: var(--surface); --btn-fg: var(--text); --btn-bd: var(--border-strong);
  display: inline-flex; align-items: center; justify-content: center; gap: var(--space-2);
  min-height: var(--tap); padding: 0 var(--space-4);
  border: 1.5px solid var(--btn-bd); border-radius: var(--radius-pill);
  background: var(--btn-bg); color: var(--btn-fg);
  font-weight: 700; font-size: .98rem; line-height: 1.1; letter-spacing: .005em;
  text-decoration: none; white-space: nowrap; user-select: none;
  transition: background var(--dur-fast) var(--ease), border-color var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease), box-shadow var(--dur-fast) var(--ease);
}
.btn:hover { text-decoration: none; background: color-mix(in srgb, var(--btn-bg) 92%, var(--text)); }
.btn:active { transform: scale(.97); }
.btn[disabled], .btn[aria-disabled="true"] { opacity: .45; pointer-events: none; }
.btn .icon { width: 20px; height: 20px; }
.btn-primary { --btn-bg: var(--primary); --btn-fg: var(--primary-contrast); --btn-bd: var(--primary); box-shadow: 0 1px 0 rgba(0,0,0,.08), 0 6px 16px -8px color-mix(in srgb, var(--primary) 70%, transparent); }
.btn-primary:hover { background: var(--primary-strong); border-color: var(--primary-strong); }
.btn-accent { --btn-bg: var(--accent); --btn-fg: var(--accent-contrast); --btn-bd: var(--accent); }
.btn-soft { --btn-bg: var(--primary-soft); --btn-fg: var(--primary-strong); --btn-bd: transparent; }
:root[data-theme="dark"] .btn-soft { --btn-fg: var(--primary); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .btn-soft { --btn-fg: var(--primary); } }
.btn-ghost { --btn-bg: transparent; --btn-bd: transparent; --btn-fg: var(--primary); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-danger { --btn-bg: var(--danger); --btn-fg: #fff; --btn-bd: var(--danger); }
.btn-danger-ghost { --btn-bg: transparent; --btn-fg: var(--danger); --btn-bd: color-mix(in srgb, var(--danger) 45%, transparent); }
.btn-sm { min-height: 36px; padding: 0 var(--space-3); font-size: .9rem; }
@media (pointer: coarse) { .btn-sm, .segmented label, .segmented button { min-height: var(--tap); } }   /* ≥ 44 px tap targets on touch screens */
.btn-lg { min-height: 54px; padding: 0 var(--space-5); font-size: 1.06rem; }
.btn-block { width: 100%; }
.btn.is-on { --btn-bg: var(--primary-soft); --btn-bd: var(--primary); --btn-fg: var(--primary-strong); }

.icon-btn {
  display: inline-grid; place-items: center; flex: none;
  width: var(--tap); height: var(--tap); border-radius: 50%;
  color: var(--text); background: transparent;
  transition: background var(--dur-fast) var(--ease), transform var(--dur-fast) var(--ease);
}
.icon-btn:hover { background: var(--surface-2); }
.icon-btn:active { transform: scale(.92); }
.icon-btn[disabled] { opacity: .35; pointer-events: none; }
.icon-btn.is-filled { background: var(--surface); border: 1.5px solid var(--border-strong); }
.icon-btn.is-primary { background: var(--primary); color: var(--primary-contrast); }
.icon-btn.is-primary:hover { background: var(--primary-strong); }
.icon-btn.is-lg { width: 56px; height: 56px; }

/* ── Chips & tags ─────────────────────────────────────────────────────── */
.chip {
  display: inline-flex; align-items: center; gap: 6px;
  min-height: 28px; padding: 2px 10px; border-radius: var(--radius-pill);
  background: var(--surface-2); color: var(--text-muted);
  font-size: .82rem; font-weight: 700; line-height: 1.2; white-space: nowrap;
}
.chip .icon { width: 15px; height: 15px; }
.chip-hands { background: var(--accent-soft); color: var(--accent-text); }
.chip-done { background: var(--success-soft); color: var(--success); }
.chip-primary { background: var(--primary-soft); color: var(--primary-strong); }
:root[data-theme="dark"] .chip-primary, :root[data-theme="dark"] .chip-hands { color: var(--text); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .chip-primary, :root:not([data-theme="light"]) .chip-hands { color: var(--text); } }

/* ── Surfaces ─────────────────────────────────────────────────────────── */
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-lg);
  box-shadow: var(--shadow); padding: var(--space-5);
}
.panel { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: var(--space-4); }
.well { background: var(--surface-2); border-radius: var(--radius); padding: var(--space-4); }

/* ── Progress ─────────────────────────────────────────────────────────── */
.ring .ring-track { stroke: color-mix(in srgb, var(--text) 12%, transparent); }
.ring .ring-value { stroke: var(--primary); transition: stroke-dashoffset var(--dur-slow) var(--ease); }
.ring.is-complete .ring-value { stroke: var(--success); }
.bar { position: relative; height: 8px; border-radius: 4px; background: color-mix(in srgb, var(--text) 10%, transparent); overflow: hidden; }
.bar > span { position: absolute; inset: 0 auto 0 0; border-radius: inherit; background: var(--primary); transition: width var(--dur-slow) var(--ease); }
.bar.is-complete > span { background: var(--success); }
.segbar { display: flex; gap: 3px; height: 6px; }
.segbar > span { flex: 1; border-radius: 3px; background: color-mix(in srgb, var(--text) 12%, transparent); }
.segbar > span.is-done { background: var(--success); }
.segbar > span.is-current { background: var(--primary); }
.segbar > span.is-current.is-done { background: linear-gradient(90deg, var(--success), var(--primary)); }

/* ── Segmented control (radio group) ──────────────────────────────────── */
.segmented {
  display: flex; padding: 4px; gap: 4px; border-radius: var(--radius-pill);
  background: var(--surface-2); border: 1px solid var(--border);
}
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label, .segmented button {
  flex: 1; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: var(--tap); padding: 0 var(--space-3); border-radius: var(--radius-pill);
  color: var(--text-muted); font-weight: 700; font-size: .92rem; cursor: pointer; white-space: nowrap;
  transition: background var(--dur) var(--ease), color var(--dur) var(--ease), box-shadow var(--dur) var(--ease);
}
.segmented input:checked + label, .segmented button[aria-pressed="true"], .segmented button[aria-selected="true"] {
  background: var(--surface); color: var(--text); box-shadow: var(--shadow);
}
.segmented input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: 1px; }

/* ── Switch ───────────────────────────────────────────────────────────── */
.switch {
  appearance: none; -webkit-appearance: none; flex: none; position: relative;
  width: 52px; height: 32px; border-radius: 16px; margin: 0; cursor: pointer;
  background: var(--surface-3); border: 1.5px solid var(--border-strong);
  transition: background var(--dur) var(--ease), border-color var(--dur) var(--ease);
}
.switch::after {
  content: ''; position: absolute; top: 3px; left: 3px; width: 23px; height: 23px; border-radius: 50%;
  background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.3);
  transition: transform var(--dur) var(--ease);
}
.switch:checked { background: var(--primary); border-color: var(--primary); }
.switch:checked::after { transform: translateX(20px); }
.switch:disabled { opacity: .6; cursor: not-allowed; }

/* ── Form bits ────────────────────────────────────────────────────────── */
.select, input[type="text"], input[type="search"] {
  width: 100%; min-height: var(--tap); padding: 0 var(--space-3);
  border: 1.5px solid var(--border-strong); border-radius: var(--radius-sm); background: var(--surface);
}
input[type="range"] { width: 100%; accent-color: var(--primary); min-height: var(--tap); }
.field { margin-bottom: var(--space-5); }
.field-label { display: block; font-weight: 700; margin-bottom: var(--space-2); }
.field-label-row { display: flex; justify-content: space-between; align-items: baseline; }
.field-label-row output { color: var(--text-muted); font-weight: 600; font-size: .9rem; }
.field-hint { color: var(--text-muted); font-size: .88rem; margin: var(--space-2) 0 0; }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: var(--space-4); }
.field-label-inline { display: flex; flex-direction: column; cursor: pointer; }
.field-label-inline .field-label { margin: 0; }
.field-label-inline .field-hint { margin-top: 2px; }
.field .btn { margin-top: var(--space-2); }

/* ── Sheet / dialog (bottom sheet on phones, centred on desktop) ──────── */
dialog.sheet {
  width: 100%; max-width: 560px; max-height: min(88dvh, 760px);
  margin: auto auto 0; padding: 0; border: 0;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  background: var(--surface); color: var(--text); box-shadow: var(--shadow-lg);
  overflow: auto; overscroll-behavior: contain;
}
dialog.sheet[open] { animation: sheet-up var(--dur-slow) var(--ease) both; }
dialog.sheet::backdrop { background: rgba(10, 18, 22, .42); -webkit-backdrop-filter: blur(2px); backdrop-filter: blur(2px); animation: fade-in var(--dur) var(--ease) both; }
@keyframes sheet-up { from { transform: translateY(40px); opacity: 0; } to { transform: none; opacity: 1; } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
.sheet-body { padding: var(--space-3) var(--space-5) calc(var(--space-5) + env(safe-area-inset-bottom)); }
.sheet-body::before { content: ''; display: block; width: 40px; height: 5px; border-radius: 3px; background: var(--border-strong); margin: 0 auto var(--space-3); }
.sheet-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-4); }
.sheet-head h2 { margin: 0; font-size: var(--step-2); }
.sheet-head .icon-btn { margin-right: -10px; }
.sheet-foot { position: sticky; bottom: calc(-1 * var(--space-5)); padding-top: var(--space-3); background: linear-gradient(transparent, var(--surface) 30%); }
@media (min-width: 640px) {
  dialog.sheet { margin: auto; border-radius: var(--radius-lg); }
  .sheet-body::before { display: none; }
  .sheet-body { padding-top: var(--space-5); }
}

/* ── Notes (tip / warning / safety / exam / info) ─────────────────────── */
.note {
  --note: var(--info); --note-bg: var(--info-soft);
  position: relative; display: grid; grid-template-columns: auto 1fr; gap: var(--space-1) var(--space-3);
  padding: var(--space-3) var(--space-4); border-radius: var(--radius);
  background: var(--note-bg); border: 1px solid color-mix(in srgb, var(--note) 25%, transparent);
}
.note > .note-icon { grid-row: span 2; color: var(--note); padding-top: 2px; }
.note > .note-label { font-size: .78rem; font-weight: 800; letter-spacing: .08em; text-transform: uppercase; color: var(--note); }
.note > .note-body { grid-column: 2; }
.note > .note-body > :last-child { margin-bottom: 0; }
.note-tip { --note: var(--success); --note-bg: var(--success-soft); }
.note-warning { --note: var(--warning); --note-bg: var(--warning-soft); }
.note-exam { --note: var(--info); --note-bg: var(--info-soft); }
.note-info { --note: var(--primary); --note-bg: var(--primary-soft); }
.note-safety {
  --note: var(--danger); --note-bg: var(--danger-soft);
  border: 2px solid var(--danger);
  box-shadow: inset 6px 0 0 var(--danger);
  padding-left: calc(var(--space-4) + 6px);
}
.note-safety > .note-label::after { content: ' · critical'; font-weight: 700; opacity: .8; }

/* ── Empty / error states ─────────────────────────────────────────────── */
.page-empty { display: flex; flex-direction: column; align-items: center; text-align: center; padding: var(--space-7) var(--space-3); }
.page-empty p { margin-inline: auto; }
.empty-art {
  display: grid; place-items: center; width: 84px; height: 84px; border-radius: 28px; margin-bottom: var(--space-4);
  background: var(--primary-soft); color: var(--primary);
}
.empty-art.is-danger { background: var(--danger-soft); color: var(--danger); }
.error-details { margin: var(--space-3) 0 var(--space-4); max-width: 100%; text-align: left; }
.error-details pre { white-space: pre-wrap; font-size: .78rem; background: var(--surface-2); padding: var(--space-3); border-radius: var(--radius-sm); max-height: 240px; overflow: auto; }
.section-error {
  border: 1.5px dashed var(--danger); border-radius: var(--radius); padding: var(--space-4);
  background: var(--danger-soft); color: var(--text);
}
.section-error h3 { font-family: var(--font-body); font-size: var(--step-0); font-weight: 800; color: var(--danger); margin-bottom: var(--space-1); }

/* ── Disclosure ───────────────────────────────────────────────────────── */
details.disclosure { border-top: 1px solid var(--border); }
details.disclosure:last-of-type { border-bottom: 1px solid var(--border); }
details.disclosure > summary {
  list-style: none; display: flex; align-items: center; justify-content: space-between; gap: var(--space-3);
  min-height: 52px; cursor: pointer; font-weight: 700;
}
details.disclosure > summary::-webkit-details-marker { display: none; }
details.disclosure > summary::after {
  content: ''; width: 10px; height: 10px; border-right: 2px solid var(--text-muted); border-bottom: 2px solid var(--text-muted);
  transform: rotate(45deg) translateY(-3px); transition: transform var(--dur) var(--ease); flex: none; margin-right: 6px;
}
details.disclosure[open] > summary::after { transform: rotate(-135deg) translateY(-1px); }
details.disclosure > :not(summary) { padding-bottom: var(--space-4); }

/* ── Toast ────────────────────────────────────────────────────────────── */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--tabbar-h) + var(--space-4) + env(safe-area-inset-bottom)); z-index: 60;
  transform: translateX(-50%); max-width: calc(100vw - 32px);
  display: flex; align-items: center; gap: var(--space-2);
  padding: var(--space-3) var(--space-4); border-radius: var(--radius-pill);
  background: var(--text); color: var(--bg); font-weight: 700; box-shadow: var(--shadow-lg);
  animation: toast-in var(--dur-slow) var(--ease) both;
}
.toast .icon { color: var(--success); }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 12px); } to { opacity: 1; transform: translate(-50%, 0); } }
@media (min-width: 960px) { .toast { bottom: var(--space-6); left: calc(50% + var(--sidebar-w) / 2); } }
/* Section pages have their own sticky bottom bars (step player, drills): the toast goes under the top bar instead. */
@media (max-width: 959px) { body[data-route="section"] .toast { bottom: auto; top: calc(var(--topbar-h) + env(safe-area-inset-top) + var(--space-3)); } }
/* UI names in running text ("tap Read aloud") look like the button, not like a link (design QA). */
.prose .ui, .ui { display: inline-block; padding: 0 .45em; border-radius: 6px; border: 1px solid var(--border-strong); background: var(--surface); font-weight: 700; font-size: .92em; line-height: 1.5; white-space: nowrap; color: var(--text); }

/* ── Visual (figure) — shared by every section ────────────────────────── */
.visual { margin: 0; }
.visual-stage { position: relative; }
.visual.is-svg .visual-stage > svg { width: 100%; height: auto; }
.visual.is-img .visual-stage > img { width: 100%; height: auto; border-radius: inherit; }
.visual figcaption { font-size: .88rem; color: var(--text-muted); margin-top: var(--space-2); line-height: 1.4; }
.visual-credit { display: inline-block; font-size: .78rem; opacity: .85; }
/* hotspots on img/svg visuals (js/core/visual.js) — the stage takes the picture's aspect ratio */
.visual.has-hotspots .visual-stage { aspect-ratio: var(--ar, auto); }
.visual.has-hotspots .visual-stage > img, .visual.has-hotspots .visual-stage > svg { display: block; width: 100%; height: 100%; }
.visual-hotspots { position: absolute; inset: 0; pointer-events: none; }
.visual-hotspot { position: absolute; transform: translate(-50%, -50%); display: flex; align-items: center; }
.visual-hotspot-dot {
  display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%;
  background: var(--accent-text); color: #fff; border: 2px solid #fff; box-shadow: var(--shadow);   /* 6:1 (coral + white was 3.9:1) */
  font: 800 .78rem/1 var(--font-body); font-variant-numeric: tabular-nums;
}
.visual-hotspot-label {
  position: absolute; left: calc(100% + 4px); white-space: nowrap;
  padding: 2px 8px; border-radius: var(--radius-pill); background: var(--surface); color: var(--text);
  font-size: .78rem; font-weight: 700; box-shadow: var(--shadow);
}
.visual-hotspot-label.is-left { left: auto; right: calc(100% + 4px); }
.visual-missing { display: grid; place-items: center; min-height: 160px; padding: var(--space-4); color: var(--text-muted); background: var(--surface-2); border-radius: var(--radius); text-align: center; font-size: .9rem; }
.reduce-motion .visual svg *, .reduce-motion .visual svg { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) { .visual svg * { animation: none !important; } }
