:root {
  --bg: #f6f6f4;
  --card: #ffffff;
  --fg: #17191d;
  --muted: #646a74;
  --line: #e2e3e6;
  --accent: #ff5a36;
  --accent-fg: #ffffff;
  --ok: #1f9d6b;
  --warn: #d9822b;
  --chip-bg: rgba(255, 255, 255, 0.86);
  --sim-bg: #eceef1;
  --sim-floor: #e4e6ea;
  --sim-grid: #c3c7ce;
  --radius: 12px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1013;
    --card: #16191e;
    --fg: #e9ebef;
    --muted: #979ea9;
    --line: #262a31;
    --chip-bg: rgba(22, 25, 30, 0.85);
    --sim-bg: #1b1e24;
    --sim-floor: #23272e;
    --sim-grid: #3a3f48;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0e1013;
  --card: #16191e;
  --fg: #e9ebef;
  --muted: #979ea9;
  --line: #262a31;
  --chip-bg: rgba(22, 25, 30, 0.85);
  --sim-bg: #1b1e24;
  --sim-floor: #23272e;
  --sim-grid: #3a3f48;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--fg);
  font: 15px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}
a { color: inherit; text-decoration-color: color-mix(in srgb, currentColor 35%, transparent); text-underline-offset: 2px; }
a:hover { text-decoration-color: currentColor; }
code { font-family: var(--mono); font-size: 0.86em; background: color-mix(in srgb, var(--fg) 7%, transparent); padding: 0.05em 0.35em; border-radius: 4px; }
.mono { font-family: var(--mono); font-variant-numeric: tabular-nums; }

/* ── header ─────────────────────────────────────────────── */
.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 24px;
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; }
.brand-mark { width: 12px; height: 12px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 0 4px color-mix(in srgb, var(--accent) 18%, transparent); }
.tabs { display: flex; gap: 4px; padding: 4px; background: color-mix(in srgb, var(--fg) 5%, transparent); border-radius: 999px; }
.tab {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 7px 16px;
  border-radius: 999px;
  cursor: pointer;
  white-space: nowrap;
}
.tab:hover { color: var(--fg); }
.tab[aria-selected="true"] { background: var(--card); color: var(--fg); box-shadow: 0 1px 2px rgba(0,0,0,0.08), 0 0 0 1px var(--line); }
.tab:focus-visible, .btn:focus-visible, summary:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ── layout ─────────────────────────────────────────────── */
main { flex: 1; display: flex; padding: 20px 24px; min-height: 0; }
.demo {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 340px;
  gap: 20px;
  min-height: 0;
}
.demo[hidden] { display: none; }

.stage {
  position: relative;
  border-radius: var(--radius);
  overflow: hidden;
  background: var(--sim-bg);
  border: 1px solid var(--line);
  min-height: 460px;
  height: calc(100dvh - 160px);
  max-height: 900px;
}
.viewport { position: absolute; inset: 0; touch-action: none; }
.viewport.can-grab { cursor: grab; }
.viewport.grabbing { cursor: grabbing; }
.sim-canvas { display: block; width: 100%; height: 100%; }

.hud { position: absolute; top: 12px; left: 12px; display: flex; gap: 8px; flex-wrap: wrap; pointer-events: none; }
.chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px;
  border-radius: 999px;
  background: var(--chip-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
  border: 1px solid var(--line);
  font-size: 13px;
  font-weight: 500;
}
.chip[data-state]::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: var(--muted); }
.chip[data-state="ok"]::before { background: var(--ok); }
.chip[data-state="busy"]::before { background: var(--warn); }
.chip[data-state="bad"]::before { background: var(--accent); }
.hint {
  position: absolute;
  bottom: 12px;
  left: 50%;
  transform: translateX(-50%);
  margin: 0;
  max-width: calc(100% - 24px);
  width: max-content;
  text-align: center;
  font-size: 12.5px;
  color: var(--muted);
  pointer-events: none;
  padding: 4px 12px;
  border-radius: 999px;
  background: var(--chip-bg);
  backdrop-filter: blur(8px);
  -webkit-backdrop-filter: blur(8px);
}

.loading {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 10px;
  background: var(--sim-bg);
  color: var(--muted);
  font-size: 13px;
  transition: opacity 0.3s;
}
.loading.done { opacity: 0; pointer-events: none; }
.loading.error { color: var(--accent); }
.loading p { margin: 0; max-width: 34ch; text-align: center; }
.bar { width: 180px; height: 3px; border-radius: 2px; background: color-mix(in srgb, var(--fg) 10%, transparent); overflow: hidden; }
.bar span { display: block; height: 100%; width: 0; background: var(--accent); transition: width 0.2s; }

/* ── side panel ─────────────────────────────────────────── */
.panel { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.panel h1 { font-size: 19px; line-height: 1.25; letter-spacing: -0.015em; margin: 2px 0 0; }
.lede { margin: 0; color: var(--muted); }
.controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.btn {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--fg);
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  padding: 8px 14px;
  border-radius: 9px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.btn:hover { border-color: color-mix(in srgb, var(--fg) 30%, transparent); }
.btn.primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.btn.primary:hover { background: color-mix(in srgb, var(--fg) 85%, var(--bg)); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.file { position: relative; overflow: hidden; align-self: flex-start; font-size: 13px; padding: 6px 12px; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }

.switch { display: inline-flex; align-items: center; gap: 9px; font-size: 14px; cursor: pointer; user-select: none; margin-left: 4px; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track { width: 34px; height: 20px; border-radius: 999px; background: color-mix(in srgb, var(--fg) 18%, transparent); position: relative; transition: background 0.15s; flex: none; }
.switch .track::after { content: ""; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px; border-radius: 50%; background: #fff; box-shadow: 0 1px 2px rgba(0,0,0,0.25); transition: transform 0.15s; }
.switch input:checked + .track { background: var(--ok); }
.switch input:checked + .track::after { transform: translateX(14px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }

.stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 1px; margin: 0; background: var(--line); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.stats div { background: var(--card); padding: 9px 11px; min-width: 0; }
.stats dt { font-size: 11.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stats dd { margin: 2px 0 0; font-size: 14px; white-space: nowrap; }

.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12.5px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.sw { display: inline-block; width: 12px; height: 12px; border-radius: 50%; }
.sw-poly { border-radius: 2px; border: 2px solid #3b82f6; }
.sw-cp { border: 2.5px solid var(--ok); }
.sw-com { background: var(--muted); width: 8px; height: 8px; }

details { border-top: 1px solid var(--line); padding-top: 12px; }
summary { cursor: pointer; font-weight: 500; font-size: 14px; }
details p { color: var(--muted); font-size: 13.5px; margin: 10px 0 0; }

.policy-card { border: 1px solid var(--line); background: var(--card); border-radius: 10px; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.policy-head { display: flex; flex-direction: column; gap: 5px; }
.eyebrow { font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); }
.policy-select {
  appearance: none;
  font: inherit;
  font-weight: 600;
  font-size: 14px;
  color: var(--fg);
  background: var(--bg) url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%23888' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 10px center;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 6px 28px 6px 10px;
  width: 100%;
  min-width: 0;
  text-overflow: ellipsis;
  cursor: pointer;
}
.policy-select:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.policy-io { margin: 0; font-size: 11.5px; color: var(--muted); overflow-wrap: anywhere; }
.views { display: flex; gap: 10px; }
.views figure { margin: 0; display: flex; flex-direction: column; gap: 4px; }
.views canvas { width: 96px; height: 96px; image-rendering: pixelated; border-radius: 6px; background: #000; border: 1px solid var(--line); }
.views figcaption { font-family: var(--mono); font-size: 11px; color: var(--muted); }

.foot {
  display: flex;
  justify-content: space-between;
  gap: 8px 24px;
  flex-wrap: wrap;
  padding: 12px 24px 18px;
  font-size: 12.5px;
  color: var(--muted);
}
.noscript { text-align: center; padding: 24px; }

/* ── small screens ──────────────────────────────────────── */
@media (max-width: 900px) {
  .top { padding: 12px 16px; flex-wrap: wrap; }
  main { padding: 12px 16px; }
  .demo { grid-template-columns: minmax(0, 1fr); gap: 16px; }
  .stage { height: 62vh; height: 62dvh; min-height: 320px; }
  .foot { padding: 12px 16px 18px; }
}
.hint .short { display: none; }
@media (max-width: 520px) {
  .hint { font-size: 11.5px; bottom: 8px; padding: 3px 10px; }
  .hint .long { display: none; }
  .hint .short { display: inline; }
  .top { gap: 10px; }
  .brand { font-size: 14px; }
  .tabs { width: 100%; }
  .tab { flex: 1; padding: 7px 8px; font-size: 13px; }
  .stage { height: 58vh; height: 58dvh; }
  .panel h1 { font-size: 17px; }
  .stats dd { font-size: 13px; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
