:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1b1f24;
  --muted: #5f6b7a;
  --border: #d9dee5;
  --accent: #8c1515;
  --accent-text: #ffffff;
  --accent-soft: #f4e7e7;
  --viewer-bg: #eceff3;
  --error-bg: #fdecec;
  --error-text: #8a1c1c;
  --shadow: 0 1px 2px rgba(16, 24, 40, .06);
  --radius: 8px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111418;
    --panel: #181c21;
    --text: #e7eaee;
    --muted: #97a2b0;
    --border: #2c333c;
    --accent: #e06666;
    --accent-text: #111418;
    --accent-soft: #3a2224;
    --viewer-bg: #0b0d10;
    --error-bg: #3a1f22;
    --error-text: #ffb4b4;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); }
code { font: 12.5px ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; background: var(--panel); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
[hidden] { display: none !important; }

/* ---- Header */
.top {
  display: flex; align-items: center; gap: 16px;
  padding: 10px 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 650; font-size: 16px; white-space: nowrap; }
.brand svg { width: 24px; height: 24px; fill: none; stroke: var(--accent); stroke-width: 2; }
.source { display: flex; flex: 1; gap: 8px; min-width: 0; }
.source input {
  flex: 1; min-width: 0;
  padding: 9px 12px;
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.source input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
button, .btn-link {
  font: inherit; cursor: pointer;
  border-radius: var(--radius);
}
#source-go {
  padding: 9px 18px;
  font-weight: 600;
  color: var(--accent-text);
  background: var(--accent);
  border: 0;
}
#source-go:disabled { opacity: .6; cursor: progress; }

/* ---- Welcome */
.welcome { max-width: 880px; margin: 48px auto; padding: 0 16px; }
.welcome h1 { font-size: 24px; margin: 0 0 8px; }
.welcome > p { color: var(--muted); max-width: 70ch; }
.sources { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 12px; margin: 24px 0; }
.sources > div { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.sources h2 { font-size: 13px; margin: 0 0 4px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.sources p { margin: 0; }
.hint { color: var(--muted); line-height: 2; }

/* ---- Results bar */
.results { background: var(--panel); border-bottom: 1px solid var(--border); padding: 8px 16px 10px; }
.source-meta { display: flex; align-items: baseline; gap: 8px; min-width: 0; margin-bottom: 8px; }
.badge { flex: none; font-size: 11.5px; font-weight: 600; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); }
.source-title { font-weight: 600; color: var(--text); text-decoration: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.source-title:hover { text-decoration: underline; }
#source-stats { flex: none; font-size: 12.5px; }
.file-bar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
#file-select {
  flex: 1 1 320px; min-width: 0; max-width: 720px;
  padding: 7px 10px;
  font: inherit; color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.icon-btn { display: grid; place-items: center; width: 34px; height: 34px; padding: 0; background: var(--bg); border: 1px solid var(--border); color: var(--text); }
.icon-btn:disabled { opacity: .4; cursor: default; }
.icon-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; }
.count { font-size: 12.5px; padding: 0 4px; white-space: nowrap; }
.spacer { flex: 1; }
.btn-link { padding: 6px 10px; border: 1px solid var(--border); background: var(--bg); color: var(--text); text-decoration: none; font-size: 13px; white-space: nowrap; }
.btn-link:hover { border-color: var(--muted); }

/* ---- Status */
.status { margin: 12px 16px 0; padding: 10px 14px; border-radius: var(--radius); background: var(--panel); border: 1px solid var(--border); }
.status.error { background: var(--error-bg); color: var(--error-text); border-color: transparent; }
.progress { height: 4px; margin-top: 8px; background: var(--border); border-radius: 2px; overflow: hidden; }
.progress > div { height: 100%; width: 0; background: var(--accent); transition: width .15s; }

/* ---- Viewer */
.viewer { flex: 1; position: relative; min-height: 0; background: var(--viewer-bg); overflow: hidden; }
.viewer > iframe { position: absolute; inset: 0; width: 100%; height: 100%; border: 0; background: #fff; }
.viewer .message { position: absolute; inset: 0; display: grid; place-items: center; text-align: center; padding: 24px; color: var(--muted); }
.viewer .message.error { color: var(--error-text); }

.image-view { position: absolute; inset: 0; overflow: auto; display: grid; place-items: center; cursor: zoom-in;
  background-color: var(--viewer-bg);
  background-image: linear-gradient(45deg, rgba(128,128,128,.12) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.12) 75%),
                    linear-gradient(45deg, rgba(128,128,128,.12) 25%, transparent 25%, transparent 75%, rgba(128,128,128,.12) 75%);
  background-size: 20px 20px; background-position: 0 0, 10px 10px; }
.image-view img { max-width: 100%; max-height: 100%; object-fit: contain; }
.image-view.actual { place-items: start; cursor: zoom-out; }
.image-view.actual img { max-width: none; max-height: none; }

/* Shared overlay toolbars (3D + DICOM) */
.overlay-bar { position: absolute; top: 10px; left: 10px; display: flex; gap: 6px; flex-wrap: wrap; z-index: 2; }
.overlay-bar button, .overlay-bar label {
  font-size: 12.5px; padding: 5px 10px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); border-radius: 6px; box-shadow: var(--shadow);
}
.overlay-bar button[aria-pressed="true"] { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }
.overlay-info { position: absolute; bottom: 10px; left: 10px; font-size: 12px; color: var(--muted); background: var(--panel); padding: 4px 8px; border-radius: 6px; border: 1px solid var(--border); z-index: 2; }
.model-view, .dicom-view { position: absolute; inset: 0; }
.model-view canvas { display: block; width: 100%; height: 100%; outline: none; }

.dicom-view { display: flex; }
.dicom-stage { position: relative; flex: 1; min-width: 0; display: grid; place-items: center; background: #000; cursor: crosshair; }
.dicom-stage canvas { max-width: 100%; max-height: 100%; image-rendering: auto; }
.dicom-stage .overlay-info { background: rgba(0,0,0,.6); color: #ddd; border-color: transparent; }
.dicom-stage .frame-ctl { position: absolute; bottom: 10px; right: 10px; display: flex; align-items: center; gap: 8px; background: rgba(0,0,0,.6); color: #ddd; padding: 4px 10px; border-radius: 6px; font-size: 12px; }
.dicom-meta { width: 300px; flex: none; overflow: auto; background: var(--panel); border-left: 1px solid var(--border); padding: 12px 14px; font-size: 12.5px; }
.dicom-meta h3 { margin: 0 0 8px; font-size: 13px; }
.dicom-meta dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 10px; margin: 0; }
.dicom-meta dt { color: var(--muted); }
.dicom-meta dd { margin: 0; word-break: break-word; }
.dicom-meta .note { margin-top: 12px; padding: 8px 10px; border-radius: 6px; background: var(--error-bg); color: var(--error-text); }

@media (max-width: 720px) {
  .top { flex-wrap: wrap; }
  .source { flex-basis: 100%; }
  .dicom-view { flex-direction: column; }
  .dicom-meta { width: auto; max-height: 35%; border-left: 0; border-top: 1px solid var(--border); }
  .spacer { display: none; }
}
