:root {
  --bg: #0b0f17;
  --panel: #111827;
  --panel-2: #172033;
  --line: #263248;
  --line-2: #33415c;
  --text: #e5e7eb;
  --muted: #94a3b8;
  --faint: #64748b;
  --accent: #facc15;
  --accent-ink: #1a1400;
  --blue: #3b82f6;
  --red: #ef4444;
  --ok: #22c55e;
  --warn: #f59e0b;
  --err: #f43f5e;
  --radius: 8px;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button, input, select, textarea {
  font: inherit;
  color: inherit;
}

button {
  background: var(--panel-2);
  border: 1px solid var(--line-2);
  border-radius: 6px;
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: var(--muted); }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.danger { color: #fecdd3; border-color: #7f1d1d; }
button.ghost { background: transparent; border-color: transparent; color: var(--muted); padding: 3px 6px; }
button.ghost:hover { color: var(--text); border-color: var(--line-2); }
button.small { padding: 2px 8px; font-size: 12px; }

input, select, textarea {
  background: #0d1422;
  border: 1px solid var(--line-2);
  border-radius: 6px;
  padding: 4px 7px;
  min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible, canvas:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}
input.expr { font-family: var(--mono); }
input.invalid { border-color: var(--err); }
textarea { font-family: var(--mono); font-size: 12px; width: 100%; resize: vertical; }
input[type="checkbox"] { accent-color: var(--accent); }
input[type="color"] { padding: 0; width: 28px; height: 24px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 6px; }
.brand .logo { color: var(--accent); }
.brand .tag { font-weight: 400; color: var(--faint); font-size: 12px; }
.project-name { width: 220px; font-weight: 600; }
.spacer { flex: 1; }
.seg { display: inline-flex; border: 1px solid var(--line-2); border-radius: 6px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: transparent; color: var(--muted); }
.seg button.on.blue { background: var(--blue); color: white; }
.seg button.on.red { background: var(--red); color: white; }
.seg button.on { background: var(--line-2); color: var(--text); }

/* ---------- layout ---------- */
.layout {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  min-height: 0;
}
.field-wrap { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.field-area { position: relative; flex: 1; min-height: 0; display: flex; align-items: center; justify-content: center; padding: 10px; }
#field { display: block; border-radius: 4px; box-shadow: 0 0 0 1px var(--line); cursor: default; touch-action: none; }
.field-overlay { position: absolute; font-size: 12px; pointer-events: none; }
.field-overlay > * { pointer-events: auto; }
.field-overlay.top-left { top: 16px; left: 16px; font-family: var(--mono); background: rgba(11, 15, 23, 0.8); padding: 3px 7px; border-radius: 5px; }
.field-overlay.top-left:empty { display: none; }
.field-overlay.top-right { top: 16px; right: 16px; display: flex; gap: 4px; }
.field-overlay.bottom-left { bottom: 16px; left: 16px; color: var(--muted); background: rgba(11, 15, 23, 0.85); padding: 4px 8px; border-radius: 5px; max-width: 70%; }
.field-overlay.bottom-left:empty { display: none; }
.field-overlay.bottom-right { bottom: 16px; right: 16px; display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 4px; max-width: 60%; pointer-events: none; }
.field-overlay.bottom-right:empty { display: none; }
.now .what { background: rgba(11, 15, 23, 0.85); padding: 3px 8px; border-radius: 5px; color: var(--text); }
.now .state { padding: 3px 8px; border-radius: 5px; color: #0b0f17; font-weight: 600; border: 1px solid rgba(0, 0, 0, 0.35); }
.view-toggles button { font-size: 11px; padding: 2px 7px; background: rgba(17, 24, 39, 0.85); color: var(--muted); }
.view-toggles button.on { color: var(--accent); border-color: var(--accent); }

/* ---------- playback / timeline ---------- */
.playback { border-top: 1px solid var(--line); background: var(--panel); padding: 8px 12px 10px; }
.pb-controls { display: flex; align-items: center; gap: 6px; margin-bottom: 6px; flex-wrap: wrap; }
.pb-controls .time { font-family: var(--mono); min-width: 150px; }
.pb-controls .budget { margin-left: auto; color: var(--muted); font-size: 12px; }
.pb-controls .budget b.over { color: var(--err); }
.strip { position: relative; height: 58px; background: #0d1422; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; cursor: pointer; user-select: none; }
.strip .row { position: absolute; left: 0; right: 0; }
.strip .row.a { top: 4px; height: 26px; }
.strip .row.b { top: 34px; height: 18px; }
.strip .blk {
  position: absolute; top: 0; bottom: 0; border-radius: 4px; overflow: hidden;
  font-size: 11px; line-height: 26px; padding: 0 5px; white-space: nowrap; text-overflow: ellipsis;
  color: #0b0f17; font-weight: 600; border: 1px solid rgba(0, 0, 0, 0.35);
}
.strip .row.b .blk { line-height: 16px; font-weight: 500; opacity: 0.9; }
.strip .blk.sel { box-shadow: 0 0 0 2px var(--accent) inset; }
.strip .blk.cut { background-image: repeating-linear-gradient(45deg, rgba(0,0,0,.45) 0 6px, transparent 6px 12px) !important; opacity: 0.55; }
.strip .blk.zero { min-width: 3px; padding: 0; }
.strip .line { position: absolute; top: 0; bottom: 0; width: 2px; }
.strip .line.timeout { background: var(--err); }
.strip .line.auto { background: var(--muted); }
.strip .line .lbl { position: absolute; bottom: 1px; left: 4px; font-size: 10px; color: var(--muted); white-space: nowrap; }
.strip .playhead { position: absolute; top: 0; bottom: 0; width: 2px; background: white; box-shadow: 0 0 4px black; pointer-events: none; }
.strip .tick { position: absolute; top: 0; width: 1px; height: 4px; background: var(--faint); }

/* ---------- sidebar ---------- */
.sidebar { border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }
.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button { flex: 1; border: 0; border-radius: 0; background: transparent; color: var(--muted); padding: 9px; border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--text); border-bottom-color: var(--accent); }
.badge { display: inline-block; min-width: 18px; padding: 0 5px; border-radius: 9px; font-size: 11px; background: var(--line-2); }
.badge.err { background: var(--err); color: white; }
.badge.warn { background: var(--warn); color: #1a1100; }
.badge:empty { display: none; }
.tab-body { flex: 1; overflow-y: auto; padding: 10px; display: flex; flex-direction: column; gap: 8px; }

.card { background: var(--panel-2); border: 1px solid var(--line); border-radius: var(--radius); }
.card > .hd { display: flex; align-items: center; gap: 6px; padding: 7px 9px; cursor: pointer; }
.card > .bd { padding: 4px 10px 10px; display: flex; flex-direction: column; gap: 8px; border-top: 1px solid var(--line); }
.card.sel { border-color: var(--accent); }
.card .hd .title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card .hd .num { color: var(--faint); font-family: var(--mono); font-size: 11px; width: 18px; }
.card .hd .dur { color: var(--muted); font-family: var(--mono); font-size: 11px; white-space: nowrap; }
.card .hd .dur.over { color: var(--err); }
.chip { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; }
.dot.err { background: var(--err); }
.dot.warn { background: var(--warn); }

.section-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); margin-top: 4px; }
.row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.row > label { color: var(--muted); font-size: 12px; }
.grid3 { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.grid2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 6px; }
.fld { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.fld > span { font-size: 11px; color: var(--muted); }
.fld input, .fld select { width: 100%; }
.sub { background: #0f1729; border: 1px solid var(--line); border-radius: 6px; padding: 7px; display: flex; flex-direction: column; gap: 6px; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.mono { font-family: var(--mono); }
.hint { font-size: 12px; color: var(--muted); }
.add-row { display: flex; gap: 6px; flex-wrap: wrap; }
.add-row button { flex: 1; }
.issue { display: flex; gap: 8px; align-items: flex-start; padding: 7px 9px; border-radius: 6px; background: var(--panel-2); border: 1px solid var(--line); cursor: pointer; }
.issue .dot { margin-top: 5px; }
.issue.info .dot { background: var(--muted); }
.stat { font-family: var(--mono); font-size: 12px; color: var(--muted); }
.cmdref { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px; align-items: end; }
.cmdref .params { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; }
.cmdref .params .fld { width: 120px; }
.piece { display: grid; grid-template-columns: 64px minmax(0, 1fr) auto; gap: 6px; align-items: end; }
.piece .extra { grid-column: 1 / -1; display: flex; gap: 6px; flex-wrap: wrap; }
.piece .extra .fld { width: 110px; }

/* ---------- overlays ---------- */
.overlay { position: fixed; inset: 0; background: rgba(3, 6, 12, 0.72); z-index: 20; display: flex; align-items: center; justify-content: center; padding: 24px; }
.overlay[hidden] { display: none; }
.modal { background: var(--panel); border: 1px solid var(--line-2); border-radius: 12px; width: min(1100px, 100%); max-height: 100%; display: flex; flex-direction: column; overflow: hidden; }
.modal > header { display: flex; align-items: center; gap: 10px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.modal > header h2 { margin: 0; font-size: 16px; flex: 1; }
.modal .body { display: flex; min-height: 0; flex: 1; }
.modal nav.side { width: 170px; border-right: 1px solid var(--line); padding: 8px; display: flex; flex-direction: column; gap: 2px; overflow-y: auto; }
.modal nav.side button { text-align: left; background: transparent; border-color: transparent; color: var(--muted); }
.modal nav.side button.on { background: var(--panel-2); color: var(--text); border-color: var(--line-2); }
.modal .content { flex: 1; overflow-y: auto; padding: 16px; display: flex; flex-direction: column; gap: 10px; min-width: 0; }
.modal .content h3 { margin: 4px 0 0; font-size: 14px; }
.modal .content p { margin: 0; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; }
.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-weight: 500; color: var(--muted); font-size: 12px; padding: 4px; }
.tbl td { padding: 3px 4px; vertical-align: top; }
.tbl input { width: 100%; }
.libcard { border: 1px solid var(--line); border-radius: 8px; padding: 10px; display: flex; flex-direction: column; gap: 8px; background: var(--panel-2); }
pre.code {
  margin: 0; padding: 14px; background: #070b12; border: 1px solid var(--line); border-radius: 8px;
  font: 12px/1.5 var(--mono); overflow: auto; white-space: pre; tab-size: 4; flex: 1; min-height: 200px;
}
.code-tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.code-tabs button.on { border-color: var(--accent); color: var(--accent); }
.code-tabs .more-label { color: var(--faint); font-size: 12px; align-self: center; margin-left: 8px; }
.code-tabs button.more { font-size: 12px; padding: 3px 8px; color: var(--muted); }
.points-opts { align-items: flex-end; gap: 10px; }
.points-opts[hidden] { display: none; }
.points-opts .fld { width: auto; }
.warnbox { border: 1px solid #78350f; background: #1c1407; color: #fde68a; border-radius: 8px; padding: 8px 10px; font-size: 12px; }
.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); background: #0f172a; border: 1px solid var(--line-2); padding: 8px 14px; border-radius: 8px; z-index: 40; box-shadow: 0 6px 24px rgba(0,0,0,.5); }

/* ---------- menus + help ---------- */
.menu {
  position: fixed; z-index: 30; min-width: 280px; max-width: min(420px, calc(100vw - 16px)); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--line-2); border-radius: 10px; padding: 6px;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.55); display: flex; flex-direction: column; gap: 2px;
}
.menu-title { font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--faint); padding: 6px 8px 2px; }
.menu-sep { height: 1px; background: var(--line); margin: 4px 0; }
.menu-row { display: flex; align-items: center; gap: 2px; border-radius: 6px; }
.menu-row.current { background: var(--panel-2); }
.menu-item { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 8px; text-align: left; background: transparent; border-color: transparent; padding: 6px 8px; }
.menu-item:hover, .menu-item:focus-visible { background: var(--panel-2); border-color: var(--line-2); }
.menu-item.danger { color: #fecdd3; }
.menu-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.menu-row.current .menu-label { font-weight: 600; }
.menu-row.current .menu-label::before { content: "● "; color: var(--accent); }
.menu-hint { color: var(--faint); font-size: 11px; white-space: nowrap; }
.modal.help { width: min(860px, 100%); }
.modal.help .content { display: grid; grid-template-columns: repeat(auto-fit, minmax(340px, 1fr)); gap: 6px 28px; }
.modal.help section { min-width: 0; }
dl.keys { display: grid; grid-template-columns: minmax(120px, 42%) 1fr; gap: 6px 12px; margin: 6px 0 12px; }
dl.keys dt { font-family: var(--mono); font-size: 12px; color: var(--accent); }
dl.keys dd { margin: 0; color: var(--muted); }

@media (max-width: 960px) {
  html, body { height: auto; }
  body { overflow: auto; display: block; }
  .layout { display: block; }
  .field-wrap { display: block; }
  .field-area { height: min(100vw, 78vh); padding: 6px; }
  .field-overlay.top-right { top: 8px; right: 8px; }
  .field-overlay.top-left { top: 8px; left: 8px; }
  .field-overlay.bottom-right { bottom: 8px; right: 8px; }
  .sidebar { border-left: 0; border-top: 1px solid var(--line); }
  .tab-body { overflow: visible; }
  .project-name { width: 140px; }
  .modal nav.side { width: 120px; }
  .overlay { padding: 8px; }
  .pb-controls .budget { margin-left: 0; width: 100%; }
  .modal .body { flex-direction: column; }
  .modal nav.side { width: auto; flex-direction: row; flex-wrap: wrap; border-right: 0; border-bottom: 1px solid var(--line); }
}
