:root {
  --bg: #f3f2ef;
  --panel: #ffffff;
  --panel-2: #f6f5f2;
  --line: #dedbd4;
  --text: #1f2125;
  --muted: #6a6d74;
  --accent: #2563eb;
  --accent-ink: #ffffff;
  --warn: #c2410c;
  --danger: #b91c1c;
  --ok: #15803d;
  --viewport: #20232a;
  --shadow: 0 1px 2px rgba(0,0,0,.06), 0 2px 8px rgba(0,0,0,.05);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111317; --panel: #181b20; --panel-2: #1f2329; --line: #2c3139; --text: #e7e8ea; --muted: #9aa0a9;
    --accent: #5b8cff; --accent-ink: #0b0d10; --warn: #fb923c; --danger: #f87171; --ok: #4ade80; --viewport: #0d0f12;
    --shadow: none; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #111317; --panel: #181b20; --panel-2: #1f2329; --line: #2c3139; --text: #e7e8ea; --muted: #9aa0a9;
  --accent: #5b8cff; --accent-ink: #0b0d10; --warn: #fb923c; --danger: #f87171; --ok: #4ade80; --viewport: #0d0f12;
  --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column; overflow: hidden;
}
button, select, input { font: inherit; color: inherit; }
a { color: var(--accent); }
kbd { font: 12px ui-monospace, monospace; border: 1px solid var(--line); border-radius: 4px; padding: 0 4px; background: var(--panel-2); }

.topbar {
  display: flex; align-items: center; gap: 12px; padding: 8px 12px;
  background: var(--panel); border-bottom: 1px solid var(--line); flex: 0 0 auto;
}
.brand { font-weight: 650; white-space: nowrap; }
.brand .sub { font-weight: 400; color: var(--muted); margin-left: 6px; }
.tabs { display: flex; gap: 4px; margin-left: auto; background: var(--panel-2); padding: 3px; border-radius: 10px; }
.tabs button { border: 0; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; color: var(--muted); }
.tabs button.active { background: var(--panel); color: var(--text); box-shadow: var(--shadow); font-weight: 600; }
.icon-btn { border: 1px solid var(--line); background: var(--panel); width: 32px; height: 32px; border-radius: 8px; cursor: pointer; flex: 0 0 auto; }
#panelToggle { display: none; }

.layout { flex: 1 1 auto; display: flex; min-height: 0; }
.panel {
  width: 340px; flex: 0 0 340px; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px;
  background: var(--bg); border-right: 1px solid var(--line);
}
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px; box-shadow: var(--shadow); }
.card h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0 0 8px; font-weight: 650; }
.card h3 { font-size: 14px; margin: 12px 0 4px; }
.card h3:first-child { margin-top: 0; }
.card p, .card ul { margin: 4px 0 8px; }
.card ul { padding-left: 18px; }
.muted { color: var(--muted); }
.notes summary { list-style: none; cursor: pointer; }
.notes summary::-webkit-details-marker { display: none; }
.notes summary h2 { display: inline; }
.notes summary::after { content: "▾"; float: right; color: var(--muted); }
.notes details:not([open]) summary::after { content: "▸"; }

.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.card button, .quiz button, dialog button {
  border: 1px solid var(--line); background: var(--panel-2); border-radius: 8px; padding: 6px 10px; cursor: pointer;
}
.card button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.row { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
.checks { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 8px; }
.checks label, .check-inline { display: flex; align-items: center; gap: 6px; cursor: pointer; }
.check-inline { margin-top: 8px; }
.range { display: block; margin-top: 8px; }
.range output { float: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.range input { width: 100%; accent-color: var(--accent); }
select { width: 100%; padding: 5px 6px; border-radius: 8px; border: 1px solid var(--line); background: var(--panel-2); margin-top: 6px; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.two-col label { font-size: 12px; color: var(--muted); }
.sub-ctl { padding-left: 22px; }

.seg { display: flex; flex-wrap: wrap; gap: 4px; }
.seg button { flex: 1 1 auto; }
.seg button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
.stage-info { margin-top: 10px; font-size: 13px; }
.stage-info .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin-top: 8px; }
.stage-info ul { margin: 2px 0; }

.safety { margin-top: 10px; font-size: 13px; display: grid; gap: 3px; }
.safety .d { display: flex; justify-content: space-between; }
.safety .d b { font-variant-numeric: tabular-nums; }
.lvl-ok { color: var(--ok); } .lvl-warn { color: var(--warn); } .lvl-bad { color: var(--danger); }

.tasks { list-style: none; padding: 0; margin: 0; display: grid; gap: 6px; }
.tasks li { display: grid; grid-template-columns: 20px 1fr; gap: 6px; align-items: start; }
.tasks li .tick { width: 16px; height: 16px; border-radius: 50%; border: 2px solid var(--line); margin-top: 2px; }
.tasks li.done .tick { background: var(--ok); border-color: var(--ok); }
.tasks li.na { opacity: .45; }
.tasks .prog { display: block; font-size: 12px; color: var(--muted); }

.viewport { flex: 1 1 auto; position: relative; background: var(--viewport); min-width: 0; touch-action: none; }
#c { display: block; width: 100%; height: 100%; }
.inset-frame {
  position: absolute; pointer-events: none; border: 2px solid rgba(255,255,255,.55); border-radius: 10px;
}
.inset-frame span { position: absolute; left: 8px; top: 6px; font-size: 11px; color: #fff; text-shadow: 0 1px 2px #000; }
.hud {
  position: absolute; left: 12px; top: 12px; color: #fff; font-size: 13px; background: rgba(0,0,0,.55);
  padding: 8px 10px; border-radius: 8px; max-width: min(360px, 60%); pointer-events: none;
}
.hud .warn { color: #fdba74; } .hud .bad { color: #fca5a5; font-weight: 600; }
.aim { position: absolute; width: 26px; height: 26px; margin: -13px 0 0 -13px; border: 2px solid rgba(255,255,255,.85); border-radius: 50%; pointer-events: none; box-shadow: 0 0 0 1px rgba(0,0,0,.5); }
.aim::after { content: ""; position: absolute; left: 50%; top: 50%; width: 4px; height: 4px; margin: -2px 0 0 -2px; border-radius: 50%; background: #fff; }
.aim.touching { border-color: #4ade80; }
.aim.touching::after { background: #4ade80; }
.hud .hint { margin-top: 4px; font-size: 11px; color: rgba(255,255,255,.7); }
.scope-ctl { position: absolute; left: 10px; bottom: 10px; display: grid; gap: 6px; justify-items: start; z-index: 2; }
.scope-ctl .row { display: flex; align-items: center; gap: 4px; background: rgba(0,0,0,.6); padding: 4px; border-radius: 12px; }
.scope-ctl button {
  min-width: 44px; height: 40px; padding: 0 6px; border: 0; border-radius: 9px; background: rgba(255,255,255,.14); color: #fff;
  font-size: 12px; cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.scope-ctl button:hover { background: rgba(255,255,255,.24); }
.scope-ctl button.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }
.scope-ctl button.big { font-size: 20px; line-height: 1; }
.scope-ctl .lbl { font-size: 10px; color: rgba(255,255,255,.7); text-align: center; width: 30px; line-height: 1.1; }
.orient { position: absolute; right: 12px; top: 12px; font-size: 11px; color: rgba(255,255,255,.7); text-align: right; pointer-events: none; line-height: 1.5; }
.info {
  position: absolute; left: 12px; bottom: 12px; max-width: min(420px, calc(100% - 24px)); background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; box-shadow: 0 4px 18px rgba(0,0,0,.25);
}
.info .t { font-weight: 650; } .info .g { font-size: 11px; text-transform: uppercase; color: var(--muted); letter-spacing: .05em; }
.info .x { float: right; border: 0; background: none; cursor: pointer; color: var(--muted); font-size: 16px; line-height: 1; }
.quiz {
  position: absolute; right: 12px; bottom: 12px; width: min(320px, calc(100% - 24px)); background: var(--panel);
  border: 1px solid var(--line); border-radius: 12px; padding: 12px; box-shadow: 0 4px 18px rgba(0,0,0,.25);
}
.quiz .opts { display: grid; gap: 6px; margin: 8px 0; }
.quiz .opts button { text-align: left; }
.quiz .opts button.right { background: var(--ok); color: #fff; border-color: var(--ok); }
.quiz .opts button.wrong { background: var(--danger); color: #fff; border-color: var(--danger); }
.toast {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%); background: rgba(0,0,0,.75); color: #fff;
  padding: 6px 12px; border-radius: 999px; font-size: 13px; pointer-events: none; white-space: nowrap; max-width: 90%; overflow: hidden; text-overflow: ellipsis;
}
.loading { position: absolute; inset: 0; display: grid; place-items: center; color: #cfd3da; font-size: 15px; z-index: 5; }
.loading .err { max-width: 520px; margin: 16px; padding: 14px 16px; background: var(--panel); color: var(--text); border: 1px solid var(--danger); border-radius: 12px; font-size: 14px; overflow-wrap: anywhere; }
.loading .err .muted { font-size: 12px; }

dialog { border: 1px solid var(--line); border-radius: 14px; background: var(--panel); color: var(--text); max-width: 560px; width: calc(100% - 32px); }
dialog::backdrop { background: rgba(0,0,0,.45); }
dialog h2 { margin-top: 0; }

[hidden] { display: none !important; }

@media (max-width: 820px) {
  .brand .sub { display: none; }
  #panelToggle { display: block; }
  .topbar { gap: 8px; padding: 8px; }
  .tabs button { padding: 6px 8px; }
  .layout { flex-direction: column-reverse; }
  .panel {
    width: 100%; flex: 0 0 auto; max-height: 45vh; border-right: 0; border-top: 1px solid var(--line); padding: 10px 16px;
  }
  body.panel-collapsed .panel { display: none; }
  .viewport { flex: 1 1 0; }
  .brand { overflow: hidden; text-overflow: ellipsis; }
}
@media (max-height: 500px) {
  body.panel-collapsed .panel { display: none; }
  #panelToggle { display: block; }
}
@media (max-width: 480px) {
  .scope-ctl { left: 6px; bottom: 6px; }
  .scope-ctl button { min-width: 40px; padding: 0 4px; font-size: 11px; }
  .scope-ctl .lbl { display: none; }
  .hud { max-width: calc(100% - 24px); font-size: 12px; }
  .brand { display: none; }
  .tabs { margin-left: 0; flex: 1; }
  .tabs button { flex: 1; font-size: 13px; padding: 6px 4px; }
}

.viva-score { display: block; font-size: 14px; color: var(--text); font-weight: 600; padding-top: 4px; }
.viva-body { margin-top: 10px; font-size: 13px; }
.viva-body .lbl { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--accent); font-weight: 650; }
.viva-q { font-size: 15px; font-weight: 600; margin: 4px 0 6px; }
.viva-a { background: var(--panel-2); border-left: 3px solid var(--accent); border-radius: 6px; padding: 8px 10px; }
.card button.ok { background: var(--ok); border-color: var(--ok); color: #fff; }
