/* ============================================================================================
 * autopsy.css — the Autopsy speciality tool (assets/js/autopsy.js)
 *
 * Designed for a gloved hand at arm's length: the record button is the largest thing on the screen,
 * the timer is readable from across a table, and nothing else needs touching while recording.
 * Every rule is scoped under .ap-* or #autopsy-view so nothing here reaches another surface (§30:
 * the phone layout is a first-class layout, not a desktop afterthought).
 * ==========================================================================================*/

.speciality-icon.autopsy { background: linear-gradient(135deg, #334155 0%, #64748b 100%); }

#autopsy-view .ap-wrap { max-width: 980px; margin: 0 auto; padding: 8px 16px 48px; display: flex; flex-direction: column; gap: 16px; }
.ap-card { background: #fff; border: 1px solid #e2e8f0; border-radius: 16px; padding: 18px; box-shadow: 0 1px 2px rgba(15, 23, 42, .04); }
.ap-card h3 { margin: 0 0 12px; font-size: 16px; display: flex; align-items: center; gap: 8px; color: #0f172a; }
.ap-muted { color: #64748b; font-size: 13px; }
.ap-err { color: #b91c1c; }
.ap-ok { color: #047857; font-weight: 600; }
.ap-warn { color: #b45309; font-weight: 600; }

/* ── Hands-free recording ─────────────────────────────────────────────────────────────── */
.ap-rec { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 12px; }
/* The shared HamdOrb IS the control (§7): the whole orb is the tap target. */
.ap-orb-wrap { width: min(100%, 430px); border-radius: 28px; padding: 6px; transition: box-shadow .2s, background .2s; }
.ap-orb-wrap .ap-mic { cursor: pointer; outline: none; }
.ap-orb-wrap .ap-mic:focus-visible { outline: 3px solid #93c5fd !important; outline-offset: 4px; }
.ap-orb-wrap.on { background: rgba(220, 38, 38, .06); box-shadow: 0 0 0 3px rgba(220, 38, 38, .35); }
.ap-orb-wrap.armed { background: rgba(127, 29, 29, .10); box-shadow: 0 0 0 5px #fca5a5; }
.ap-rec-state { font-size: 20px; font-weight: 800; color: #1b55e2; display: flex; align-items: center; gap: 8px; }
.ap-rec-state.on { color: #dc2626; }
.ap-rec-state.armed { color: #7f1d1d; }
.ap-timer { font-size: 56px; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: .02em; color: #0f172a; line-height: 1; }
.ap-rec-status { font-size: 14px; color: #334155; min-height: 1.2em; }
.ap-rec-status.err, .ap-gen-status.err { color: #b91c1c; font-weight: 600; }
.ap-rec-status.ok, .ap-gen-status.ok { color: #047857; }
.ap-gen-status.warn { color: #b45309; }
.ap-handsfree { font-size: 14px; color: #1e3a8a; background: #eff6ff; border: 1px solid #bfdbfe; border-radius: 12px; padding: 10px 14px; max-width: 640px; }
.ap-segments { width: 100%; display: flex; flex-direction: column; gap: 6px; text-align: left; }
.ap-seg { display: flex; flex-wrap: wrap; gap: 6px 12px; align-items: center; padding: 8px 12px; border-radius: 10px; background: #f8fafc; border: 1px solid #e2e8f0; font-size: 13px; }
.ap-seg-n { font-weight: 700; }
.ap-seg-time { font-variant-numeric: tabular-nums; color: #475569; }
.ap-seg-status { margin-left: auto; font-weight: 600; }
.ap-seg-done .ap-seg-status { color: #047857; }
.ap-seg-failed { background: #fef2f2; border-color: #fecaca; }
.ap-seg-failed .ap-seg-status, .ap-seg-err { color: #b91c1c; }
.ap-seg.excluded { opacity: .65; }
.ap-seg-note { flex-basis: 100%; color: #92400e; font-size: 12px; }
.ap-seg-err { flex-basis: 100%; font-size: 12px; }
/* Upload audio: large enough for a gloved tap, but visibly SECONDARY to the orb. */
.ap-upload { display: flex; flex-direction: column; align-items: center; gap: 6px; max-width: 640px; }
.ap-btn.ap-upload-btn { min-height: 52px; min-width: 220px; justify-content: center; font-size: 16px; padding: 12px 20px; }
.ap-upload-status { font-size: 14px; min-height: 0; }
.ap-upload-status.err { color: #b91c1c; font-weight: 600; }
.ap-seg-src { font-weight: 600; color: #334155; overflow-wrap: anywhere; min-width: 0; }

/* ── Forms ────────────────────────────────────────────────────────────────────────────── */
.ap-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 12px; }
.ap-grid label, .ap-search { display: flex; flex-direction: column; gap: 4px; font-size: 13px; font-weight: 600; color: #334155; }
.ap-grid .ap-wide { grid-column: 1 / -1; }
#autopsy-view input[type="text"], #autopsy-view input[type="search"], #autopsy-view input[type="tel"], #autopsy-view input[type="date"],
#autopsy-view input[type="datetime-local"], #autopsy-view input[type="number"], #autopsy-view textarea, #autopsy-view select {
  font: inherit; font-weight: 400; padding: 9px 11px; border: 1px solid #cbd5e1; border-radius: 10px; background: #fff; min-width: 0; width: 100%; }
.ap-pathologist { margin-top: 10px; font-size: 14px; }
.ap-place-results { display: flex; flex-direction: column; gap: 6px; margin: 8px 0 12px; }
.ap-place { text-align: left; border: 1px solid #e2e8f0; background: #f8fafc; border-radius: 10px; padding: 8px 12px; cursor: pointer; display: flex; flex-direction: column; gap: 2px; }
.ap-place span { color: #64748b; font-size: 12px; }
.ap-attrib { font-size: 11px; color: #64748b; }
.ap-map { margin-top: 12px; }
.ap-map iframe { width: 100%; height: 240px; border: 1px solid #e2e8f0; border-radius: 12px; }

.ap-image-list { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.ap-thumb { position: relative; width: 96px; height: 96px; border-radius: 10px; overflow: hidden; border: 1px solid #e2e8f0; }
.ap-thumb img { width: 100%; height: 100%; object-fit: cover; }
.ap-thumb .ap-x { position: absolute; top: 4px; right: 4px; }

/* ── Buttons ──────────────────────────────────────────────────────────────────────────── */
.ap-btn { font: inherit; font-size: 14px; font-weight: 600; display: inline-flex; align-items: center; gap: 6px; padding: 9px 14px; border-radius: 10px;
  border: 1px solid #cbd5e1; background: #fff; color: #0f172a; cursor: pointer; }
.ap-btn:disabled { opacity: .5; cursor: not-allowed; }
/* Generate stays tappable when not ready (a tap explains why, contract §10a) — it just reads as not ready. */
.ap-btn.ap-not-ready { opacity: .72; }
.ap-btn.ap-primary { background: #1b55e2; border-color: #1b55e2; color: #fff; }
.ap-btn.ap-ghost { background: transparent; }
.ap-btn.ap-sm { padding: 4px 10px; font-size: 12px; }
.ap-btn.ap-big { width: 100%; justify-content: center; min-height: 56px; font-size: 17px; }
.ap-actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 12px; }
.ap-x { border: 0; background: rgba(15, 23, 42, .08); width: 32px; height: 32px; border-radius: 50%; font-size: 20px; line-height: 1; cursor: pointer; }
.ap-x.ap-sm { width: 24px; height: 24px; font-size: 16px; background: rgba(255, 255, 255, .9); }
.ap-spin { animation: ap-spin 1s linear infinite; }
@keyframes ap-spin { to { transform: rotate(360deg); } }

/* ── Missing entries ──────────────────────────────────────────────────────────────────── */
.ap-missing { border-color: #fcd34d; background: #fffbeb; }
.ap-identity-warn { background: #fff7ed; border: 1px solid #fdba74; border-radius: 10px; padding: 10px 12px; margin: 10px 0; font-size: 14px; }
.ap-missing-list { display: flex; flex-direction: column; gap: 14px; margin-top: 10px; }
.ap-missing-group h4 { margin: 0 0 6px; font-size: 13px; letter-spacing: .04em; color: #78350f; }
.ap-missing-row { background: #fff; border: 1px solid #fde68a; border-radius: 10px; padding: 10px 12px; margin-bottom: 6px; display: flex; flex-direction: column; gap: 6px; }
.ap-missing-head { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ap-missing-label { font-weight: 700; }
.ap-missing-note { font-size: 12px; color: #92400e; }
.ap-reason { font-size: 11px; font-weight: 700; padding: 2px 8px; border-radius: 999px; background: #e2e8f0; color: #334155; }
.ap-reason-possibly_mistranscribed, .ap-reason-inaudible { background: #fee2e2; color: #991b1b; }
.ap-reason-conflicting { background: #ffedd5; color: #9a3412; }
.ap-ans-checks { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.ap-chk { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; flex-wrap: wrap; }
.ap-chk .ap-spec { width: 160px !important; padding: 5px 8px !important; }
.ap-ans-measure, .ap-ans-statement { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ap-ans-measure input, .ap-ans-statement input, .ap-ans-statement select { width: auto !important; flex: 1 1 120px; }
.ap-ans-measure .ap-unit { flex: 0 0 90px; }
.ap-ans-inline { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; }

/* ── Report preview & atlas overlays ─────────────────────────────────────────────────── */
.ap-overlay { position: fixed; inset: 0; z-index: 3000; background: rgba(15, 23, 42, .55); display: none; align-items: stretch; justify-content: center; padding: 16px; }
.ap-overlay.active { display: flex; }
.ap-sheet { background: #fff; border-radius: 16px; width: min(1000px, 100%); display: flex; flex-direction: column; overflow: hidden; }
.ap-sheet-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 12px; padding: 14px 16px 6px; }
.ap-sheet-head h3 { margin: 0 0 2px; font-size: 18px; }
.ap-sheet-tools { display: flex; flex-wrap: wrap; gap: 8px; padding: 6px 16px 12px; border-bottom: 1px solid #e2e8f0; }
#ap-report-frame, #ap-atlas-frame { flex: 1; width: 100%; border: 0; background: #f8fafc; min-height: 0; }
.ap-sheet-atlas { width: min(1280px, 100%); }

/* ── Previous Patient Records: an autopsy visit shows its own two tiles, not the encounter grid. */
.session-detail-container.is-autopsy-visit .vitals-display-section,
.session-detail-container.is-autopsy-visit #session-vitals-history-trigger,
.session-detail-container.is-autopsy-visit .prescription-summary-card,
.session-detail-container.is-autopsy-visit > .clinical-output-header,
.session-detail-container.is-autopsy-visit #session-output-cats,
.session-detail-container.is-autopsy-visit > .clinical-output-grid { display: none !important; }
#session-autopsy-panel .ap-tiles-head { margin: 8px 0 12px; }
#session-autopsy-panel .ap-tiles-head h3 { margin: 0 0 4px; font-size: 18px; color: #0f172a; }
#session-autopsy-panel .ap-tiles-head p { margin: 0; color: #64748b; font-size: 13px; }
#session-autopsy-panel .ap-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }

/* ── Output tiles in the workspace (same cards as the encounter's outputs) + background progress ── */
#autopsy-view .ap-tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 14px; }
.ap-ws-tile { min-height: 132px; }
.ap-ws-tile[data-state="none"], .ap-ws-tile[data-state="waiting"] { opacity: .85; }
.clinical-output-pill.ap-pill-busy { background: #fef3c7; color: #92400e; }
.clinical-output-pill.ap-pill-attn { background: #fee2e2; color: #991b1b; }
.ap-tiles-msg { margin-top: 12px; padding: 10px 14px; border-radius: 12px; background: #eff6ff; border: 1px solid #bfdbfe; color: #1e3a8a; font-size: 14px; }
/* Fixed bottom-LEFT, stacked ABOVE the Feedback button (feedback.js: left 20px, bottom 20px — 88px
   over the wizard footer); the toasts own bottom-right. Below the .ap-overlay sheets and the toasts. */
.ap-bg-pill { position: fixed; left: 20px; bottom: calc(72px + env(safe-area-inset-bottom, 0px)); z-index: 2900;
  display: inline-flex; align-items: center; gap: 6px; max-width: calc(100vw - 32px); padding: 8px 14px; border-radius: 999px; border: 1px solid #cbd5e1;
  background: #fff; color: #0f172a; font: inherit; font-size: 13px; font-weight: 700; cursor: pointer; white-space: nowrap; overflow: hidden;
  box-shadow: 0 8px 24px rgba(15, 23, 42, .18); }
body:has(#wiz.on) .ap-bg-pill { bottom: calc(140px + env(safe-area-inset-bottom, 0px)); }
.ap-bg-pill[hidden] { display: none !important; }
.ap-bg-pill .ap-bg-pill-state { overflow: hidden; text-overflow: ellipsis; }
.ap-bg-pill.busy { background: #fffbeb; border-color: #fcd34d; color: #92400e; }
.ap-bg-pill.rec { background: #fef2f2; border-color: #fca5a5; color: #b91c1c; }
.ap-bg-pill.ready { background: #ecfdf5; border-color: #6ee7b7; color: #047857; }
.ap-bg-pill.attn { background: #fef2f2; border-color: #fca5a5; color: #991b1b; }
.ap-bg-toast { cursor: pointer; }
.ap-bg-toast .ap-bg-toast-open { font: inherit; font-size: 13px; font-weight: 700; border: 0; background: none; color: #1b55e2; cursor: pointer; padding: 4px 0 4px 8px; white-space: nowrap; }
/* An autopsy finished in the BACKGROUND (autopsy.js showRunNotice): it stays until it is acted on. */
.ap-run-notice .ap-run-dismiss { font: inherit; font-size: 18px; line-height: 1; border: 0; background: none; color: #64748b; cursor: pointer; padding: 2px 0 2px 10px; }
/* Above the toast layer: the notice that opens it is a toast, and on a phone it would otherwise sit
   on top of the dialog's own buttons. */
#ap-run-identity { z-index: 10001; }
.ap-sheet.ap-sheet-small { width: min(520px, 100%); align-self: center; padding-bottom: 14px; }
.ap-sheet-small .ap-grid, .ap-sheet-small .ap-actions, .ap-sheet-small .ap-gen-status { margin: 8px 16px 0; }

@media (max-width: 600px) {
  .ap-bg-pill { padding: 7px 12px; font-size: 12px; max-width: calc(100vw - 40px); }
  .ap-card { padding: 14px; border-radius: 14px; }
  .ap-rec-state { font-size: 17px; }
  .ap-timer { font-size: 44px; }
  .ap-overlay { padding: 0; }
  .ap-sheet { border-radius: 0; }
  .ap-sheet-tools .ap-btn { flex: 1 1 calc(50% - 8px); justify-content: center; }
  .ap-chk .ap-spec { width: 100% !important; }
}

/* [hidden] must win over the display set on .ap-btn / .ap-card above ("Rebuild from structure"
   only exists once the report carries hand edits; the result and missing panels only once there is
   a report). */
#autopsy-view [hidden], .ap-overlay [hidden], #session-autopsy-panel[hidden], #session-autopsy-panel [hidden] { display: none !important; }

/* Clear session — the encounter's convention (a ghost button with the eraser), top-right of the workspace. */
.ap-head-actions { display: flex; justify-content: flex-end; margin-bottom: -6px; }
.ap-head-actions .ap-btn:disabled { opacity: .55; cursor: default; }

/* §93b — an autopsy record whose report the server is still producing: its progress, in percent. */
.ap-progress { padding: 16px; }
.ap-progress-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; margin-bottom: 8px; }
.ap-progress-label { font-weight: 700; color: #0f172a; }
.ap-progress-pct { font-size: 22px; font-weight: 800; color: #1b55e2; font-variant-numeric: tabular-nums; }
.ap-progress-track { height: 10px; border-radius: 99px; background: #e2e8f0; overflow: hidden; }
.ap-progress-fill { height: 100%; background: #1b55e2; border-radius: 99px; transition: width .4s ease; }
.ap-progress.failed .ap-progress-fill { background: #dc2626; }
.ap-progress-msg { margin: 10px 0 0; font-size: 13px; }
.ap-row-progress { display: inline-block; margin-top: 4px; padding: 2px 10px; border-radius: 99px; background: #fef3c7; color: #92400e; font-size: 12px; font-weight: 700; font-variant-numeric: tabular-nums; }
.ap-row-progress.failed { background: #fee2e2; color: #991b1b; }

/* §93c — the decedent's details, entered on the record (Previous Patient Records → Autopsy). */
.ap-record-details { margin-top: 14px; }
.ap-record-details.pending { border-color: #f59e0b; background: #fffbeb; }
.ap-record-details .ap-progress-label .ti { color: #b45309; }
.ap-record-details .ap-details-form { margin-top: 10px; }
.ap-record-details .ap-actions { margin-top: 10px; }

/* §91c — a record whose recording was low quality or only partly transcribed says so. */
.ap-quality-warn { margin: 0 0 12px; padding: 10px 12px; border-radius: 10px; background: #fffbeb; border: 1px solid #f59e0b; color: #92400e; font-size: 13px; line-height: 1.4; }
.ap-quality-warn .ti { color: #b45309; }
