:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --text: #1c1f24;
  --muted: #6b7280;
  --line: #e2e5ea;
  --accent: #0b74de;
  --accent-text: #ffffff;
  --rec: #d92d3a;
  --btn: #eef0f3;
  --btn-hover: #e2e6eb;
  --screen: #0b0f14;
  --screen-grid: rgba(255, 255, 255, 0.10);
  --screen-axis: rgba(255, 255, 255, 0.28);
  --screen-text: #c9d1d9;
  --shadow: 0 1px 2px rgba(0, 0, 0, .06), 0 2px 8px rgba(0, 0, 0, .04);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1216;
    --panel: #171b21;
    --text: #e6e8eb;
    --muted: #8b949e;
    --line: #262c35;
    --accent: #3b93f0;
    --btn: #222831;
    --btn-hover: #2c333d;
    --shadow: none;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1216; --panel: #171b21; --text: #e6e8eb; --muted: #8b949e; --line: #262c35;
  --accent: #3b93f0; --btn: #222831; --btn-hover: #2c333d; --shadow: none; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100vh;
}
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.link { color: var(--accent); cursor: pointer; }
h1, h2, h3 { margin: 0; }

/* ---- top bar ---- */
.topbar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-size: 17px; white-space: nowrap; }
.device-picker { display: flex; align-items: center; gap: 8px; flex: 1 1 260px; min-width: 0; }
.device-picker select { flex: 1; min-width: 0; max-width: 420px; }
.top-actions { display: flex; gap: 6px; flex-wrap: wrap; }

.pill { font-size: 12px; padding: 2px 8px; border-radius: 99px; background: var(--btn); color: var(--muted); white-space: nowrap; }
.pill.ok { background: #1f9d55; color: #fff; }
.pill.live { background: var(--accent); color: #fff; }
.pill.rec { background: var(--rec); color: #fff; }
.pill.err { background: #b45309; color: #fff; }

/* ---- controls ---- */
.btn, select, input[type=number] {
  font: inherit; color: var(--text); background: var(--btn); border: 1px solid var(--line);
  border-radius: 8px; padding: 6px 12px; min-height: 34px;
}
.btn { cursor: pointer; display: inline-flex; align-items: center; justify-content: center; gap: 4px; text-decoration: none; white-space: nowrap; user-select: none; }
.btn:hover:not(:disabled) { background: var(--btn-hover); }
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.ghost { background: transparent; }
.btn.coffee { background: #ffdd00; border-color: #f0cf00; color: #1c1f24; }
.btn.coffee:hover { background: #ffe433; }
.btn.rec { color: var(--rec); }
.btn.rec.on { background: var(--rec); color: #fff; border-color: var(--rec); animation: blink 1.2s infinite; }
.btn.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.small { padding: 4px 10px; min-height: 30px; font-size: 13px; }
.btn.big { padding: 10px 18px; font-size: 15px; }
@keyframes blink { 50% { opacity: .7; } }
select { padding-right: 8px; }

/* ---- layout ---- */
.layout {
  display: grid; grid-template-columns: minmax(0, 1fr) 320px; gap: 16px;
  padding: 16px; max-width: 1600px; margin: 0 auto;
}
@media (max-width: 980px) { .layout { grid-template-columns: minmax(0, 1fr); } }
.scope-col { display: flex; flex-direction: column; gap: 12px; min-width: 0; }

.screen-wrap { background: var(--screen); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.screen-wrap:fullscreen { border-radius: 0; display: flex; flex-direction: column; }
.screen-wrap:fullscreen .screen { flex: 1; aspect-ratio: auto; }
.screen-toolbar { display: flex; align-items: center; gap: 6px; padding: 6px 8px; background: #121820; border-bottom: 1px solid #1f2731; flex-wrap: wrap; }
.screen-toolbar .spacer { flex: 1; }
.tbtn, .seg button {
  font: inherit; font-size: 12px; color: var(--screen-text); background: #1b232d; border: 1px solid #263140;
  border-radius: 6px; padding: 4px 9px; cursor: pointer;
}
.tbtn:hover, .seg button:hover { background: #24303d; }
.tbtn.on, .seg button.on { background: #2d6fb8; border-color: #2d6fb8; color: #fff; }
.seg { display: inline-flex; }
.seg button { border-radius: 0; }
.seg button:first-child { border-radius: 6px 0 0 6px; }
.seg button:last-child { border-radius: 0 6px 6px 0; }

.screen { position: relative; aspect-ratio: 5 / 3; min-height: 240px; }
.screen canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; touch-action: none; cursor: crosshair; }
.welcome {
  position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 10px; text-align: center; padding: 24px; color: #e6edf3;
  background: radial-gradient(ellipse at center, rgba(11, 15, 20, .55), rgba(11, 15, 20, .92));
}
.welcome h1 { font-size: clamp(20px, 3vw, 30px); }
.welcome p { margin: 0; max-width: 560px; color: #b6c2cf; }
.welcome .muted { color: #8b9aab; }
.welcome-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; margin: 8px 0; }
.welcome code { background: #1b232d; padding: 1px 6px; border-radius: 4px; }
.welcome a { color: #79b8ff; }

.cursor-box, .frame-badge {
  position: absolute; background: rgba(13, 18, 24, .88); color: #e6edf3; border: 1px solid #2b3644;
  border-radius: 8px; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; padding: 6px 9px; pointer-events: none;
}
.cursor-box { top: 10px; right: 10px; white-space: pre; }
.frame-badge { bottom: 40px; left: 50%; transform: translateX(-50%); white-space: nowrap; }

/* ---- transport ---- */
.transport {
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 8px 10px; box-shadow: var(--shadow);
}
.transport .sep { width: 1px; align-self: stretch; background: var(--line); margin: 0 4px; }
.transport input[type=range] { flex: 1 1 160px; min-width: 120px; accent-color: var(--accent); }

/* ---- measurements ---- */
.measure-wrap { overflow-x: auto; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; box-shadow: var(--shadow); }
.measure { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.measure th, .measure td { padding: 7px 10px; text-align: right; white-space: nowrap; border-bottom: 1px solid var(--line); }
.measure th:first-child, .measure td:first-child { text-align: left; }
.measure th { font-weight: 600; color: var(--muted); font-size: 12px; }
.measure tr:last-child td { border-bottom: 0; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 6px; vertical-align: -1px; }

/* ---- side ---- */
.side { display: flex; flex-direction: column; gap: 12px; min-width: 0; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 12px 14px; box-shadow: var(--shadow); display: flex; flex-direction: column; gap: 8px; }
.card h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.card h3 { font-size: 12px; color: var(--muted); margin-top: 6px; }
.row { display: flex; gap: 6px; align-items: center; }
.row.wrap { flex-wrap: wrap; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; margin: 0; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }
.note { margin: 0; font-size: 12px; color: var(--muted); background: var(--btn); border-radius: 8px; padding: 8px 10px; }
.field { display: flex; justify-content: space-between; align-items: center; gap: 8px; font-size: 13px; }
.field select, .field input { width: 140px; }
.chan-ctl { border-top: 1px solid var(--line); padding-top: 8px; display: flex; flex-direction: column; gap: 6px; }
.chan-ctl .head { display: flex; justify-content: space-between; align-items: center; font-weight: 600; }

.channels { display: flex; flex-direction: column; gap: 6px; }
.chan { display: flex; align-items: flex-start; gap: 8px; font-size: 13px; }
.chan input { margin-top: 3px; }
.chan .meta { color: var(--muted); font-size: 12px; }

.library { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 320px; overflow: auto; }
.library li { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.library .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.library button { font-size: 12px; padding: 2px 7px; min-height: 26px; }

.foot { padding: 0 4px; }

.toast {
  position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%) translateY(20px); opacity: 0;
  background: #1c2128; color: #fff; padding: 10px 16px; border-radius: 10px; max-width: min(92vw, 640px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .25); transition: all .2s; pointer-events: none; z-index: 20; font-size: 13px;
}
.toast.show { opacity: 1; transform: translateX(-50%); }
.toast.error { background: #9b1c1c; }
.toast.has-action { pointer-events: auto; display: flex; align-items: center; gap: 12px; }
.toast button { font: inherit; font-weight: 600; color: #fff; background: rgba(255,255,255,.15); border: 1px solid rgba(255,255,255,.35); border-radius: 6px; padding: 3px 10px; cursor: pointer; white-space: nowrap; }

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); max-width: min(92vw, 900px); }
dialog::backdrop { background: rgba(0, 0, 0, .5); }
dialog form { display: flex; flex-direction: column; gap: 12px; }
#shotImg { max-width: 100%; image-rendering: pixelated; }

@media (max-width: 600px) {
  .topbar { padding: 8px 16px; }
  .top-actions .btn { padding: 5px 9px; }
  .layout { padding: 12px 16px; }
  .screen { aspect-ratio: 4 / 3; }
}

/* ---- setup guide ---- */
.guide { width: min(92vw, 760px); }
.guide form { gap: 10px; }
.guide-head { display: flex; justify-content: space-between; align-items: center; }
.guide-head h2 { font-size: 18px; }
.guide .steps { margin: 0; padding-left: 20px; display: flex; flex-direction: column; gap: 6px; }
.guide p { margin: 0; }
.guide ol { margin: 0; }
.guide section { display: flex; flex-direction: column; gap: 8px; border: 1px solid var(--line); border-radius: 10px; padding: 12px; }
.guide h3 { font-size: 14px; color: var(--text); margin-top: 4px; }
.guide code { background: var(--btn); padding: 1px 5px; border-radius: 4px; font-size: 12.5px; }
.guide-tabs { flex-wrap: wrap; }
.guide-tabs button { font: inherit; font-size: 13px; color: var(--text); background: var(--btn); border: 1px solid var(--line); padding: 5px 11px; cursor: pointer; }
.guide-tabs button:hover { background: var(--btn-hover); }
.guide-tabs button.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.code { position: relative; background: #0d1117; color: #e6edf3; border-radius: 8px; }
.code pre { margin: 0; padding: 10px 12px; padding-right: 70px; overflow-x: auto; font: 12.5px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; }
.code .btn { position: absolute; top: 6px; right: 6px; }
.faq { margin: 0; display: flex; flex-direction: column; gap: 2px; font-size: 13px; }
.faq dt { font-weight: 600; margin-top: 6px; }
.faq dd { margin: 0; color: var(--muted); }
