:root {
  --bg: #f6f7f9; --panel: #fff; --ink: #0f172a; --ink2: #334155; --muted: #64748b; --line: #e5e7eb; --line2: #f1f5f9;
  --brand: #2563eb; --brand-soft: #eff6ff;
  --ok: #047857; --ok-bg: #ecfdf5; --warn: #b45309; --warn-bg: #fffbeb; --bad: #b91c1c; --bad-bg: #fef2f2; --info: #1d4ed8; --info-bg: #eff6ff; --gray-bg: #f1f5f9;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  --shadow: 0 1px 2px rgba(15,23,42,.04), 0 4px 16px rgba(15,23,42,.06);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0c0e12; --panel: #14171d; --ink: #f1f5f9; --ink2: #cbd5e1; --muted: #8b95a7; --line: #262b35; --line2: #1b1f27;
    --brand: #60a5fa; --brand-soft: #172036;
    --ok: #34d399; --ok-bg: #0d2a20; --warn: #fbbf24; --warn-bg: #2b220b; --bad: #f87171; --bad-bg: #2e1414; --info: #93c5fd; --info-bg: #172036; --gray-bg: #1d222b;
    --shadow: 0 1px 2px rgba(0,0,0,.3), 0 4px 16px rgba(0,0,0,.25);
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font: 14px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; -webkit-font-smoothing: antialiased; }
a { color: var(--brand); text-decoration: none; } a:hover { text-decoration: underline; }
h1 { font-size: 22px; margin: 0; letter-spacing: -.015em; }
h2 { font-size: 15px; margin: 0 0 12px; }
h3 { font-size: 13px; margin: 0 0 8px; color: var(--ink2); text-transform: uppercase; letter-spacing: .04em; }
code, pre, .mono { font-family: var(--mono); font-size: 12.5px; }
pre { margin: 0; white-space: pre-wrap; word-break: break-word; }
[hidden] { display: none !important; }

/* layout */
.shell { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 18px 12px; display: flex; flex-direction: column; gap: 2px; position: sticky; top: 0; height: 100vh; }
.logo { display: flex; align-items: center; gap: 9px; font-weight: 800; font-size: 17px; padding: 2px 10px 18px; letter-spacing: -.02em; }
.logo i { width: 26px; height: 26px; border-radius: 7px; background: var(--brand); display: grid; place-items: center; }
.side a.nav { display: flex; align-items: center; justify-content: space-between; color: var(--ink2); padding: 8px 10px; border-radius: 8px; font-weight: 500; }
.side a.nav:hover { background: var(--line2); text-decoration: none; }
.side a.nav.on { background: var(--brand-soft); color: var(--brand); }
.side .count { font-size: 11px; font-weight: 700; background: var(--bad); color: #fff; border-radius: 999px; padding: 0 7px; line-height: 18px; }
.side .grow { flex: 1; }
.side .who { font-size: 12px; color: var(--muted); padding: 10px; border-top: 1px solid var(--line); }
.main { padding: 28px 32px 60px; max-width: 1240px; width: 100%; min-width: 0; }
.head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.head p { margin: 4px 0 0; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.grow { flex: 1; }
.grid { display: grid; gap: 16px; }
.g2 { grid-template-columns: 1fr 1fr; }
.g-editor { grid-template-columns: minmax(0, 1fr) 420px; align-items: start; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 18px 20px; box-shadow: var(--shadow); min-width: 0; }
.card.flush { padding: 0; overflow: hidden; }
.muted { color: var(--muted); } .small { font-size: 12.5px; } .right { text-align: right; }
.sticky { position: sticky; top: 20px; }

/* controls */
.btn { font: inherit; font-weight: 600; display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 1px solid var(--line); background: var(--panel); color: var(--ink); border-radius: 8px; padding: 7px 13px; cursor: pointer; white-space: nowrap; }
.btn:hover { border-color: var(--muted); text-decoration: none; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 4px 9px; font-size: 12.5px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; width: 100%; min-width: 0; }
input:focus, select:focus, textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
textarea { font-family: var(--mono); font-size: 12.5px; resize: vertical; }
input[type=checkbox] { width: auto; }
input[type=color] { padding: 2px; height: 36px; }
label.l { display: block; font-size: 12px; font-weight: 600; color: var(--ink2); margin-bottom: 4px; }
.fg { margin-bottom: 12px; }
.fgrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(120px, 1fr)); gap: 10px; }
.hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
.err-text { color: var(--bad); font-size: 12.5px; }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 16px; overflow-x: auto; }
.tabs a { padding: 9px 14px; color: var(--muted); font-weight: 600; border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs a.on { color: var(--ink); border-color: var(--brand); }
.tabs a:hover { text-decoration: none; color: var(--ink); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--panel); }
.seg button { font: inherit; border: 0; background: none; color: var(--muted); padding: 6px 12px; cursor: pointer; font-weight: 600; }
.seg button.on { background: var(--brand-soft); color: var(--brand); }

/* tables */
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--line2); vertical-align: middle; }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; background: var(--panel); }
tbody tr.click { cursor: pointer; }
tbody tr.click:hover { background: var(--line2); }
tr:last-child td { border-bottom: 0; }
td.num { font-variant-numeric: tabular-nums; }
.tw { overflow-x: auto; }
.trunc { max-width: 320px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* badges */
.badge { display: inline-flex; align-items: center; gap: 5px; font-size: 11.5px; font-weight: 650; padding: 2px 8px; border-radius: 999px; white-space: nowrap; }
.badge::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; }
.b-delivered, .b-ok, .b-success { background: var(--ok-bg); color: var(--ok); }
.b-retrying, .b-pending, .b-sending, .b-in_flight, .b-retryable { background: var(--warn-bg); color: var(--warn); }
.b-needs_review, .b-failing, .b-fatal { background: var(--bad-bg); color: var(--bad); }
.b-held { background: var(--info-bg); color: var(--info); }
.b-paused, .b-stored, .b-cancelled, .b-test { background: var(--gray-bg); color: var(--muted); }
.chips { display: flex; gap: 4px; flex-wrap: wrap; }

/* ledger */
.ledger { display: flex; align-items: stretch; gap: 10px; flex-wrap: wrap; }
.ledger .term { flex: 1; min-width: 120px; border-radius: 10px; padding: 12px 14px; background: var(--line2); }
.ledger .term b { display: block; font-size: 28px; font-variant-numeric: tabular-nums; letter-spacing: -.02em; line-height: 1.15; }
.ledger .term span { font-size: 12px; color: var(--muted); font-weight: 600; }
.ledger .op { align-self: center; font-size: 20px; color: var(--muted); font-weight: 300; }
.ledger .t-cap { background: var(--brand-soft); } .ledger .t-cap b { color: var(--brand); }
.ledger .t-ok b { color: var(--ok); } .ledger .t-fl b { color: var(--warn); } .ledger .t-nr b { color: var(--bad); }
.bar { display: flex; height: 10px; border-radius: 6px; overflow: hidden; background: var(--line2); margin-top: 14px; }
.bar i { display: block; height: 100%; }
.balanced { display: inline-flex; gap: 6px; align-items: center; font-size: 12.5px; font-weight: 600; color: var(--ok); margin-top: 10px; }
.callout { display: flex; gap: 12px; align-items: center; justify-content: space-between; border-radius: 10px; padding: 12px 16px; margin-top: 16px; flex-wrap: wrap; }
.callout.bad { background: var(--bad-bg); color: var(--bad); }
.spark { display: flex; align-items: flex-end; gap: 4px; height: 70px; }
.spark i { flex: 1; background: var(--brand); opacity: .8; border-radius: 3px 3px 0 0; min-height: 2px; }

/* builder */
.item { border: 1px solid var(--line); border-radius: 10px; padding: 12px; margin-bottom: 10px; background: var(--panel); }
.item-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.item-head .grow { font-weight: 650; }
.formula-ok { color: var(--ok); font-weight: 650; font-variant-numeric: tabular-nums; }
.vars { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.vars code { background: var(--line2); border-radius: 5px; padding: 1px 6px; cursor: pointer; }
.preview-frame { width: 100%; border: 0; height: 640px; border-radius: 12px; background: transparent; }
.gate-opt { display: flex; gap: 10px; align-items: flex-start; border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 8px; cursor: pointer; }
.gate-opt.on { border-color: var(--brand); background: var(--brand-soft); }
.gate-opt input { width: auto; margin-top: 3px; }
.kv { display: grid; grid-template-columns: 1fr 1.6fr auto; gap: 6px; margin-bottom: 6px; }

/* drawer / modal */
.drawer, .modal { position: fixed; inset: 0; z-index: 50; }
.drawer-bg, .modal-bg { position: absolute; inset: 0; background: rgba(15,23,42,.35); }
.drawer-panel { position: absolute; right: 0; top: 0; bottom: 0; width: min(720px, 100%); background: var(--bg); overflow-y: auto; padding: 22px 24px 40px; box-shadow: -8px 0 30px rgba(0,0,0,.15); }
.modal-panel { position: relative; margin: 6vh auto; width: min(760px, calc(100% - 24px)); max-height: 88vh; overflow-y: auto; background: var(--bg); border-radius: 14px; padding: 22px 24px; box-shadow: 0 20px 60px rgba(0,0,0,.25); }
.x { float: right; border: 0; background: none; font-size: 22px; line-height: 1; color: var(--muted); cursor: pointer; }
.attempt { border-left: 3px solid var(--line); padding: 2px 0 14px 14px; margin-left: 4px; position: relative; }
.attempt.success { border-color: var(--ok); } .attempt.retryable { border-color: var(--warn); } .attempt.fatal { border-color: var(--bad); }
.attempt details { margin-top: 6px; }
.attempt summary { cursor: pointer; color: var(--muted); font-size: 12.5px; }
.codebox { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-top: 6px; max-height: 320px; overflow: auto; }
.hintbox { background: var(--warn-bg); color: var(--warn); border-radius: 8px; padding: 10px 12px; font-size: 13px; margin: 10px 0; }
.hintbox.bad { background: var(--bad-bg); color: var(--bad); }
.hintbox.ok { background: var(--ok-bg); color: var(--ok); }
.kvlist { display: grid; grid-template-columns: max-content 1fr; gap: 4px 16px; font-size: 13px; }
.kvlist dt { color: var(--muted); } .kvlist dd { margin: 0; font-variant-numeric: tabular-nums; word-break: break-word; }

.empty { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty b { display: block; color: var(--ink); font-size: 15px; margin-bottom: 4px; }
#toast { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; z-index: 99; pointer-events: none; font-weight: 500; max-width: calc(100% - 32px); }
#toast.show { opacity: 1; transform: translateX(-50%); }
.tpl { border: 1px solid var(--line); border-radius: 10px; padding: 14px; cursor: pointer; background: var(--panel); }
.tpl:hover, .tpl.on { border-color: var(--brand); background: var(--brand-soft); }
.calc-card { display: block; color: inherit; }
.calc-card:hover { text-decoration: none; border-color: var(--brand); }

/* auth */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth .logo { padding: 0 0 6px; }
.auth .tag { color: var(--muted); margin: 0 0 20px; }

@media (max-width: 1100px) { .g-editor { grid-template-columns: 1fr; } .sticky { position: static; } }
@media (max-width: 820px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; flex-direction: row; overflow-x: auto; padding: 8px 10px; border-right: 0; border-bottom: 1px solid var(--line); align-items: center; }
  .logo { padding: 0 8px 0 0; }
  .side .who, .side .grow { display: none; }
  .main { padding: 18px 16px 40px; }
  .g2 { grid-template-columns: 1fr; }
  .ledger .op { display: none; }
  .kv { grid-template-columns: 1fr; }
}
