/* FractoVue XR — one deliberate look: a darkened radiology reading room.
   Bone ivory for the generic skeleton, cyan for patient-specific data, coral-red for fractures. */
:root {
  color-scheme: dark;
  --ground: #0b1112;
  --ground-2: #13201f;
  --panel: rgba(15, 24, 25, 0.86);
  --panel-solid: #111b1c;
  --raise: rgba(232, 228, 217, 0.06);
  --ink: #e8e4d9;
  --muted: #8e9d9a;
  --faint: #5d6b69;
  --bone: #d6c6a2;
  --accent: #7fd3de;
  --accent-ink: #06272b;
  --red: #ff6a5c;
  --amber: #f5a843;
  --line: rgba(232, 228, 217, 0.12);
  --line-2: rgba(232, 228, 217, 0.22);
  --display: "Archivo", "Arial Narrow", system-ui, sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "Cascadia Mono", Consolas, monospace;
  --panel-w: 384px;
  --r: 8px;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; }
body {
  margin: 0; overflow: hidden;
  background: radial-gradient(120% 90% at 60% 45%, var(--ground-2), var(--ground) 70%);
  color: var(--ink); font-family: var(--display); font-size: 14px; line-height: 1.4;
}
button { font: inherit; color: inherit; }
#stage { position: fixed; inset: 0; }
#stage canvas { display: block; width: 100%; height: 100%; touch-action: none; cursor: grab; }
#stage canvas:active { cursor: grabbing; }
body.pointing #stage canvas { cursor: pointer; }
.grid-bg {
  position: fixed; inset: 0; pointer-events: none; opacity: 0.3;
  background-image: linear-gradient(var(--line) 1px, transparent 1px), linear-gradient(90deg, var(--line) 1px, transparent 1px);
  background-size: 64px 64px;
  mask-image: radial-gradient(70% 70% at 60% 50%, #000 20%, transparent 80%);
}
.loading { position: fixed; inset: 0; display: grid; place-items: center; font-family: var(--mono); color: var(--muted); letter-spacing: .08em; z-index: 1; pointer-events: none; }
.loading span { animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

.hud { position: fixed; z-index: 3; }
.tooltip { position: fixed; z-index: 6; pointer-events: none; padding: 5px 9px; border-radius: 4px; background: var(--bone); color: #1b1712; font-size: 12.5px; font-weight: 600; white-space: nowrap; transform: translate(12px, -130%); }
.tooltip.patient { background: var(--accent); color: var(--accent-ink); }
.tooltip.fracture { background: var(--red); color: #2a0703; }

/* ---------- top bar ---------- */
.topbar { top: 0; left: 0; right: 0; height: 60px; padding: 0 16px; display: flex; align-items: center; justify-content: space-between; gap: 12px; background: linear-gradient(var(--ground) 30%, transparent); pointer-events: none; }
.topbar > * { pointer-events: auto; }
.brand { display: flex; align-items: center; gap: 12px; }
.logo { width: 34px; height: 34px; flex: none; }
.logo rect { fill: none; stroke: var(--accent); stroke-width: 1.6; }
.logo path { fill: none; stroke: var(--bone); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
h1 { margin: 0; font-weight: 800; font-stretch: 72%; font-size: 26px; line-height: 1; letter-spacing: 0.01em; text-transform: uppercase; }
h1 span { color: var(--accent); font-weight: 500; font-stretch: 100%; }
.eyebrow { margin: 3px 0 0; font-family: var(--mono); font-size: 10.5px; letter-spacing: 0.06em; color: var(--muted); text-transform: uppercase; }
.disclaimer { font-family: var(--mono); font-size: 10.5px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--amber); border: 1px solid rgba(245, 168, 67, 0.4); border-radius: 999px; padding: 5px 10px; white-space: nowrap; }

/* ---------- workflow panel ---------- */
.panel {
  top: 68px; left: 16px; bottom: 16px; width: var(--panel-w);
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  backdrop-filter: blur(14px); -webkit-backdrop-filter: blur(14px);
  overflow-y: auto; overscroll-behavior: contain; scrollbar-width: thin; scrollbar-color: var(--line-2) transparent;
  padding: 6px 0 18px;
}
.sheet-handle { display: none; }
.step { padding: 14px 18px 16px; border-bottom: 1px solid var(--line); }
.step:last-child { border-bottom: 0; }
.step h2 { margin: 0 0 12px; font-size: 13px; font-weight: 700; letter-spacing: 0.08em; text-transform: uppercase; display: flex; align-items: center; gap: 10px; }
.step h2 b { width: 22px; height: 22px; border-radius: 50%; display: grid; place-items: center; font-family: var(--mono); font-size: 11px; border: 1px solid var(--line-2); color: var(--muted); }
.step.done h2 b { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.step.active h2 b { border-color: var(--accent); color: var(--accent); }

.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip { padding: 6px 11px; border-radius: 999px; border: 1px solid var(--line-2); background: var(--raise); cursor: pointer; font-size: 13px; transition: background .15s, border-color .15s, color .15s; }
.chip:hover { border-color: rgba(127, 211, 222, 0.6); }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }

.bone-list .empty { margin: 0; color: var(--muted); }
.bone-group { margin-bottom: 10px; }
.bone-group:last-child { margin-bottom: 0; }
.bone-group .gl { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.bone-group .items { display: grid; grid-template-columns: repeat(auto-fill, minmax(104px, 1fr)); gap: 6px; }
.bone-btn { text-align: left; padding: 8px 10px; border-radius: var(--r); border: 1px solid var(--line); background: var(--raise); cursor: pointer; font-size: 13px; line-height: 1.2; }
.bone-btn small { display: block; color: var(--muted); font-family: var(--mono); font-size: 10px; margin-top: 2px; }
.bone-btn:hover { border-color: rgba(127, 211, 222, 0.6); }
.bone-btn[aria-pressed="true"] { border-color: var(--accent); background: rgba(127, 211, 222, 0.14); }
.bone-btn[aria-pressed="true"] small { color: var(--accent); }

.drop { display: grid; justify-items: center; gap: 4px; text-align: center; padding: 18px 12px; border: 1.5px dashed var(--line-2); border-radius: 10px; cursor: pointer; transition: border-color .15s, background .15s; }
.drop:hover, .drop.over { border-color: var(--accent); background: rgba(127, 211, 222, 0.06); }
.drop input { position: absolute; opacity: 0; pointer-events: none; width: 1px; }
.drop svg { width: 22px; height: 22px; stroke: var(--accent); fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.drop u { color: var(--accent); text-underline-offset: 3px; }
.drop small { color: var(--muted); font-family: var(--mono); font-size: 10.5px; }
.drop:focus-within { outline: 2px solid var(--accent); outline-offset: 2px; }

.wide { width: 100%; margin-top: 8px; }
.demos { display: grid; gap: 2px; margin-top: 10px; font-size: 12.5px; color: var(--muted); }
.demos .link { margin: 0; }
.link { display: block; margin: 8px 0 0; padding: 0; border: 0; background: none; color: var(--accent); font-size: 12.5px; text-align: left; cursor: pointer; text-decoration: underline; text-underline-offset: 3px; }
.ghost, .mini, .primary, .seg button, .tog, .icon-btn, .modes button, .xr-btn {
  border: 1px solid var(--line-2); background: var(--raise); border-radius: var(--r); cursor: pointer;
  transition: border-color .15s, background .15s, color .15s, opacity .15s;
}
.ghost { padding: 9px 12px; font-size: 13px; color: var(--ink); }
.ghost:hover:not(:disabled), .mini:hover:not(:disabled), .seg button:hover, .tog:hover:not(:disabled), .icon-btn:hover:not(:disabled), .modes button:hover:not(:disabled) { border-color: rgba(127, 211, 222, 0.6); }
.primary { padding: 12px; font-weight: 700; font-size: 14px; letter-spacing: 0.02em; background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.primary:hover:not(:disabled) { filter: brightness(1.08); }
button:disabled { opacity: 0.42; cursor: not-allowed; }
button:focus-visible, .chip:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.preview { position: relative; margin-top: 10px; border-radius: var(--r); overflow: hidden; background: #000; border: 1px solid var(--line); user-select: none; touch-action: none; }
.preview canvas { display: block; width: 100%; height: auto; max-height: 300px; object-fit: contain; }
.preview.crop, .preview.mark { cursor: crosshair; }
.roi { position: absolute; border: 2px solid var(--accent); background: rgba(127, 211, 222, 0.1); pointer-events: none; }
.pin { position: absolute; width: 18px; height: 18px; margin: -9px 0 0 -9px; border-radius: 50%; border: 2px solid var(--red); box-shadow: 0 0 0 4px rgba(255, 106, 92, 0.25); pointer-events: none; }
.preview-mode { position: absolute; left: 8px; top: 8px; font-family: var(--mono); font-size: 10.5px; background: rgba(0,0,0,.7); padding: 3px 7px; border-radius: 4px; color: var(--accent); }

.opts { display: grid; gap: 10px; margin-top: 10px; }
.lab { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-right: 4px; }
.seg { display: flex; align-items: center; gap: 4px; flex-wrap: wrap; }
.seg button { padding: 6px 12px; font-size: 12.5px; }
.seg button[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.seg.full { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; margin-bottom: 12px; }
.tool-row { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.mini { padding: 5px 9px; font-size: 12px; }
.mini[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.check { display: inline-flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); cursor: pointer; }
.check input { accent-color: var(--accent); }
.engine { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 8px; font-size: 12.5px; }
#engine-name { color: var(--ink); }
.note { margin: 10px 0 0; font-size: 12px; color: var(--muted); line-height: 1.45; }
.note.warn { color: #d8b27b; border-left: 2px solid var(--amber); padding-left: 10px; }

/* pipeline stages */
.stages { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.stage { display: grid; grid-template-columns: 22px 1fr 52px; gap: 10px; align-items: center; padding: 7px 8px; border-radius: var(--r); border: 1px solid transparent; cursor: default; }
.stage.done, .stage.warn { cursor: pointer; background: var(--raise); }
.stage.done:hover, .stage.warn:hover { border-color: var(--line-2); }
.stage .st { width: 20px; height: 20px; border-radius: 50%; border: 1.5px solid var(--line-2); display: grid; place-items: center; font-size: 11px; font-family: var(--mono); color: var(--muted); }
.stage.run .st { border-color: var(--accent); border-top-color: transparent; animation: spin .8s linear infinite; }
.stage.done .st { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.stage.warn .st { background: var(--amber); border-color: var(--amber); color: #2b1a04; }
.stage.err .st { background: var(--red); border-color: var(--red); color: #2a0703; }
@keyframes spin { to { transform: rotate(360deg); } }
.stage .tt { font-weight: 600; font-size: 13px; display: flex; gap: 6px; align-items: baseline; flex-wrap: wrap; }
.stage .sm { font-size: 11.5px; color: var(--muted); line-height: 1.35; margin-top: 2px; }
.stage .thumb { width: 52px; height: 52px; border-radius: 6px; background: #000 center/contain no-repeat; border: 1px solid var(--line); }
.basis { font-family: var(--mono); font-size: 9.5px; letter-spacing: .06em; text-transform: uppercase; padding: 1px 6px; border-radius: 999px; border: 1px solid; font-weight: 500; }
.basis.observed { color: var(--accent); border-color: rgba(127, 211, 222, .5); }
.basis.estimated { color: var(--amber); border-color: rgba(245, 168, 67, .5); }
.basis.mixed { color: var(--bone); border-color: rgba(214, 198, 162, .5); }

/* result */
.result { display: grid; gap: 10px; margin-bottom: 14px; }
.verdict { padding: 12px 14px; border-radius: 10px; border: 1px solid var(--line-2); background: var(--raise); display: grid; gap: 4px; }
.verdict.detected { border-color: rgba(255, 106, 92, .55); background: rgba(255, 106, 92, .08); }
.verdict .k { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); }
.verdict .v { font-size: 22px; font-weight: 800; font-stretch: 85%; }
.verdict.detected .v { color: var(--red); }
.verdict .conf { font-family: var(--mono); font-size: 12px; color: var(--ink); }
.meter { height: 5px; border-radius: 3px; background: var(--line); overflow: hidden; }
.meter i { display: block; height: 100%; background: var(--red); }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; margin: 0; font-size: 12.5px; }
.kv dt { color: var(--muted); font-family: var(--mono); font-size: 11px; padding-top: 1px; }
.kv dd { margin: 0; font-variant-numeric: tabular-nums; }
.group-label { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--muted); margin-bottom: 6px; }
.modes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; margin-bottom: 12px; }
.modes button { padding: 8px 10px; font-size: 12.5px; text-align: left; }
.modes button[aria-pressed="true"] { border-color: var(--accent); background: rgba(127, 211, 222, 0.16); color: var(--ink); }
.legend { display: grid; gap: 4px; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 2px; margin-right: 8px; vertical-align: -1px; }

/* fracture list */
.frlist { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.fr { width: 100%; display: grid; grid-template-columns: 26px 1fr 56px; gap: 10px; align-items: center; text-align: left; padding: 9px 10px; border-radius: var(--r); border: 1px solid rgba(255, 106, 92, .35); background: rgba(255, 106, 92, .06); cursor: pointer; }
.fr:hover, .fr.on { border-color: var(--red); background: rgba(255, 106, 92, .14); }
.fr .n { width: 24px; height: 24px; border-radius: 50%; display: grid; place-items: center; background: var(--red); color: #2a0703; font-family: var(--mono); font-size: 12px; font-weight: 600; }
.fr .b { font-size: 12.5px; line-height: 1.35; }
.fr .b small { color: var(--muted); font-size: 11.5px; }
.fr .c { font-family: var(--mono); font-size: 12px; text-align: right; font-variant-numeric: tabular-nums; }
.fr .c i { display: block; height: 3px; margin-top: 4px; border-radius: 2px; background: var(--red); margin-left: auto; }

/* ---------- right tools ---------- */
.tools { top: 68px; right: 16px; display: flex; flex-direction: column; gap: 6px; align-items: flex-end; }
.icon-btn { width: 40px; height: 40px; display: grid; place-items: center; padding: 0; background: var(--panel); backdrop-filter: blur(10px); }
.icon-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.icon-btn.accent:not(:disabled) { border-color: rgba(255, 106, 92, .7); color: var(--red); }
.icon-btn.accent[aria-pressed="true"] { background: var(--red); color: #2a0703; }
.tools .sep { height: 6px; }
.tog { height: 32px; padding: 0 12px; font-family: var(--mono); font-size: 11px; letter-spacing: .05em; text-transform: uppercase; background: var(--panel); backdrop-filter: blur(10px); min-width: 112px; text-align: right; }
.tog[aria-pressed="true"] { border-color: rgba(127, 211, 222, .7); color: var(--accent); }
.tog[aria-pressed="false"]:not(:disabled) { color: var(--muted); }

/* ---------- info card ---------- */
.info { right: 16px; bottom: 96px; width: 300px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 14px 16px; backdrop-filter: blur(12px); display: grid; gap: 6px; }
.info .tag { font-family: var(--mono); font-size: 10.5px; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.info .tag.red { color: var(--red); }
.info .name { font-size: 19px; font-weight: 700; font-stretch: 88%; line-height: 1.15; }
.info dl { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 12.5px; }
.info dt { color: var(--muted); font-family: var(--mono); font-size: 11px; padding-top: 1px; }
.info dd { margin: 0; }
.info .fact { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

/* ---------- XR bar ---------- */
.xrbar { right: 16px; bottom: 16px; display: flex; align-items: center; gap: 12px; flex-direction: row-reverse; max-width: calc(100vw - var(--panel-w) - 64px); }
.xr-btns { display: flex; gap: 8px; }
.xr-btn { height: 44px; padding: 0 16px; font-weight: 700; font-stretch: 90%; display: inline-flex; align-items: center; gap: 8px; background: var(--panel); backdrop-filter: blur(10px); }
.xr-btn svg { width: 18px; height: 18px; stroke: currentColor; fill: none; stroke-width: 1.7; stroke-linejoin: round; }
.xr-btn.ready { background: var(--bone); color: #1b1712; border-color: var(--bone); }
.xr-note { font-family: var(--mono); font-size: 10.5px; color: var(--muted); max-width: 360px; line-height: 1.45; text-align: right; }

/* ---------- stage modal ---------- */
.stage-modal { border: 1px solid var(--line-2); border-radius: 14px; background: var(--panel-solid); color: var(--ink); padding: 0; width: min(920px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
.stage-modal::backdrop { background: rgba(3, 6, 7, 0.7); backdrop-filter: blur(3px); }
.stage-modal .close { position: absolute; top: 8px; right: 10px; width: 34px; height: 34px; border-radius: 50%; border: 1px solid var(--line-2); background: var(--raise); cursor: pointer; font-size: 20px; line-height: 1; z-index: 1; }
.modal-body { display: grid; grid-template-columns: minmax(0, 1.3fr) minmax(0, 1fr); }
.modal-img { background: #000; display: grid; place-items: center; min-height: 320px; max-height: calc(100vh - 34px); }
.modal-img > * { max-width: 100%; max-height: calc(100vh - 40px); display: block; }
.modal-meta { padding: 22px 22px 20px; overflow-y: auto; }
.modal-meta h3 { margin: 10px 0 6px; font-size: 22px; font-stretch: 85%; }
.modal-meta p { color: var(--muted); margin: 0 0 14px; }
.modal-meta dl { display: grid; grid-template-columns: auto 1fr; gap: 6px 14px; margin: 0; font-size: 13px; }
.modal-meta dt { color: var(--muted); font-family: var(--mono); font-size: 11px; padding-top: 2px; white-space: pre; }
.modal-meta dd { margin: 0; }

/* ---------- AR DOM overlay ---------- */
#ar-overlay { position: fixed; inset: 0; z-index: 10; display: flex; flex-direction: column; justify-content: space-between; pointer-events: auto; font-family: var(--display); color: var(--ink); touch-action: none; }
#ar-overlay[hidden] { display: none; }
.ar-top { padding: calc(12px + env(safe-area-inset-top, 0px)) 12px 0; display: grid; gap: 8px; }
.ar-card { background: rgba(10, 16, 17, 0.82); border: 1px solid var(--line-2); border-radius: 12px; padding: 10px 14px; display: grid; gap: 2px; max-width: 360px; }
.ar-card .tag { font-family: var(--mono); font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--accent); }
.ar-card .tag.red { color: var(--red); }
.ar-card b { font-size: 18px; font-stretch: 88%; }
.ar-card span:not(.tag) { font-size: 12.5px; }
.ar-card i { font-style: normal; color: var(--muted); font-family: var(--mono); font-size: 10.5px; margin-right: 6px; }
.ar-status { margin: 0; font-size: 12.5px; background: rgba(10, 16, 17, 0.7); padding: 6px 10px; border-radius: 8px; justify-self: start; max-width: 100%; }
.ar-bottom { padding: 0 10px calc(12px + env(safe-area-inset-bottom, 0px)); display: grid; gap: 8px; }
.ar-row { display: flex; gap: 6px; overflow-x: auto; scrollbar-width: none; }
.ar-row button { flex: none; height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line-2); background: rgba(10, 16, 17, 0.84); font-weight: 600; font-size: 14px; }
.ar-row button[aria-pressed="true"] { border-color: var(--accent); color: var(--accent); }
.ar-row button.accent { background: var(--red); border-color: var(--red); color: #2a0703; }
.ar-row button.exit { background: var(--bone); color: #1b1712; border-color: var(--bone); }
body.ar-dom .hud, body.ar-dom #stage { visibility: hidden; }

/* ---------- small screens: panel becomes a bottom sheet ---------- */
@media (max-width: 820px) {
  :root { --panel-w: 0px; }
  .topbar { height: 54px; }
  h1 { font-size: 22px; }
  .eyebrow { display: none; }
  .disclaimer { font-size: 9.5px; padding: 4px 8px; white-space: normal; text-align: center; max-width: 150px; }
  .panel { top: auto; left: 0; right: 0; bottom: 0; width: auto; max-height: 56vh; border-radius: 16px 16px 0 0; padding-top: 0; transition: max-height .25s; }
  .panel.collapsed { max-height: 118px; overflow: hidden; }
  .sheet-handle { display: grid; place-items: center; width: 100%; height: 22px; border: 0; background: transparent; cursor: pointer; position: sticky; top: 0; z-index: 1; background: var(--panel-solid); }
  .sheet-handle span { width: 40px; height: 4px; border-radius: 2px; background: var(--line-2); }
  .tools { top: 58px; left: 10px; right: 10px; flex-direction: row; align-items: center; overflow-x: auto; scrollbar-width: none; padding-bottom: 2px; }
  .tools .sep { width: 4px; height: auto; flex: none; }
  .tools > * { flex: none; }
  .tog { min-width: 0; height: 34px; padding: 0 9px; font-size: 10px; }
  .icon-btn { width: 34px; height: 34px; }
  .info { left: 10px; right: auto; top: 100px; bottom: auto; width: min(250px, calc(100vw - 20px)); padding: 10px 12px; }
  .info .fact { display: none; }
  .info dt:nth-of-type(n+3), .info dd:nth-of-type(n+3) { display: none; }
  .info .name { font-size: 16px; }
  .xrbar { display: none; bottom: auto; top: 100px; right: 10px; max-width: none; }
  .xr-btns { flex-direction: column; }
  .xr-btn { height: 38px; padding: 0 12px; font-size: 13px; }
  body.has-xr .xrbar { display: flex; }
  .xr-note { display: none; }
  .modal-body { grid-template-columns: 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
