:root {
  --bg: #0b0b0c;
  --panel: #121214;
  --panel-2: #17171a;
  --line: #222226;
  --line-2: #2c2c31;
  --text: #ececef;
  --muted: #8b8b94;
  --faint: #5a5a62;
  --accent: #3ee6a8;
  --accent-ink: #04140e;
  --accent-soft: rgba(62, 230, 168, .12);
  --danger: #ff6b6b;
  --radius: 10px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, monospace;
  color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 13px/1.45 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  -webkit-font-smoothing: antialiased;
}
button, input, select, textarea { font: inherit; color: inherit; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.spacer { flex: 1; }

/* shell */
.shell { display: grid; grid-template-columns: 200px 1fr; min-height: 100vh; }
.rail {
  position: sticky; top: 0; height: 100vh; display: flex; flex-direction: column;
  border-right: 1px solid var(--line); padding: 16px 10px; background: var(--bg);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 600; letter-spacing: .01em; padding: 2px 8px 18px; font-size: 14px; }
.brand .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
nav { display: flex; flex-direction: column; gap: 1px; }
.nav-item {
  display: flex; align-items: center; gap: 9px; padding: 6px 8px; border-radius: 7px;
  color: var(--muted); text-decoration: none; cursor: default;
}
.nav-item svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linecap: round; stroke-linejoin: round; }
a.nav-item { cursor: pointer; }
a.nav-item:hover { background: var(--panel); color: var(--text); }
.nav-item.active { background: var(--panel-2); color: var(--text); }
.nav-item.soon { color: var(--faint); }
.nav-group { font-size: 11px; color: var(--faint); padding: 16px 8px 6px; text-transform: uppercase; letter-spacing: .08em; }
.rail-foot { margin-top: auto; padding: 8px; font-size: 11.5px; color: var(--muted); line-height: 1.5; }
.rail-foot b { color: var(--text); font-weight: 500; }
.rail-foot code { display: block; font-family: var(--mono); font-size: 10.5px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; direction: rtl; text-align: left; }

main { padding: 26px 32px 60px; max-width: 1480px; width: 100%; }
.page-head { display: flex; align-items: flex-end; gap: 16px; margin-bottom: 18px; }
.page-head h1 { font-size: 20px; font-weight: 600; margin: 0 0 2px; letter-spacing: -.01em; }
.sub { margin: 0; color: var(--muted); }
.head-actions { margin-left: auto; display: flex; align-items: center; gap: 12px; }
.save-state { font-size: 12px; color: var(--muted); }
.save-state.err { color: var(--danger); }
.save-state.ok::before { content: ""; display: inline-block; width: 6px; height: 6px; border-radius: 50%; background: var(--accent); margin-right: 6px; vertical-align: 1px; }

/* cards */
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 14px; }
.card-head { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-bottom: 1px solid var(--line); min-height: 50px; }
.card-head h2 { font-size: 13px; font-weight: 600; margin: 0; }

/* controls */
.btn {
  border: 1px solid var(--line-2); background: var(--panel-2); border-radius: 7px;
  padding: 5px 11px; cursor: pointer; white-space: nowrap;
}
.btn:hover { border-color: #3a3a40; background: #1c1c20; }
.btn.ghost { background: transparent; }
.btn.primary { background: var(--text); color: var(--bg); border-color: var(--text); font-weight: 500; }
.btn.primary:hover { background: #fff; }
.btn:disabled { opacity: .4; cursor: default; }
.btn.danger:hover { color: var(--danger); border-color: rgba(255,107,107,.4); }
.icon-btn { background: none; border: 0; color: var(--faint); cursor: pointer; padding: 4px; border-radius: 6px; display: inline-flex; }
.icon-btn:hover { color: var(--text); background: var(--panel-2); }
.icon-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.input, .select {
  background: var(--bg); border: 1px solid var(--line-2); border-radius: 7px; padding: 5px 8px; outline: none; min-width: 0;
}
.input:focus, .select:focus, textarea:focus { border-color: #4a4a52; }
.select { appearance: none; padding-right: 24px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%238b8b94' fill='none' stroke-width='1.3'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 8px center; background-size: 9px; }
.select.sm, .input.sm { padding: 3px 8px; font-size: 12px; }
.select.sm { padding-right: 22px; }
.input.num { width: 58px; font-variant-numeric: tabular-nums; }

.switch { position: relative; width: 32px; height: 18px; flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch span { position: absolute; inset: 0; border-radius: 99px; background: #2a2a2f; transition: background .15s; }
.switch span::after { content: ""; position: absolute; left: 2px; top: 2px; width: 14px; height: 14px; border-radius: 50%; background: #9a9aa2; transition: transform .15s, background .15s; }
.switch input:checked + span { background: var(--accent); }
.switch input:checked + span::after { transform: translateX(14px); background: var(--accent-ink); }
.switch input:focus-visible + span { outline: 2px solid #4a4a52; outline-offset: 2px; }
.switch.lg { width: 46px; height: 26px; }
.switch.lg span::after { width: 20px; height: 20px; left: 3px; top: 3px; }
.switch.lg input:checked + span::after { transform: translateX(20px); }

/* master */
.master { display: flex; align-items: center; gap: 18px; padding: 16px 18px; }
.master .state h3 { margin: 0; font-size: 15px; font-weight: 600; }
.master .state p { margin: 2px 0 0; color: var(--muted); }
.master.off .state h3 { color: var(--muted); }
.master .facts { margin-left: auto; display: flex; gap: 26px; align-items: center; }
.fact { display: flex; flex-direction: column; gap: 3px; }
.fact label { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .07em; }
.fact .val { display: flex; align-items: center; gap: 6px; height: 26px; }
.fact .when { color: var(--muted); }
.fact code { font-family: var(--mono); font-size: 12px; color: var(--muted); }

/* rules */
.rules { display: flex; flex-direction: column; }
.rule-head, .rule-row {
  display: grid; align-items: center; gap: 10px; padding: 8px 14px;
  grid-template-columns: 34px minmax(130px, 1.1fr) 150px 92px minmax(120px, 1fr) 130px minmax(170px, 1.6fr) 56px;
}
.rule-head > :nth-child(2) { padding-left: 9px; }
.rule-head { font-size: 11px; color: var(--faint); text-transform: uppercase; letter-spacing: .07em; padding-top: 10px; padding-bottom: 6px; }
.rule { border-top: 1px solid var(--line); }
.rule:first-of-type { border-top: 0; }
.rule.disabled .rule-row > :not(:first-child) { opacity: .45; }
.rule-row .input, .rule-row .select { width: 100%; }
.rule-row .id { font-family: var(--mono); font-size: 12px; border-color: transparent; background: transparent; }
.rule-row .id:hover { border-color: var(--line-2); }
.rule-row .id:focus { border-color: #4a4a52; background: var(--bg); }
.every { display: flex; align-items: center; gap: 6px; color: var(--muted); }
.every .input { width: 52px; }
.params-inline { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; color: var(--muted); }
.params-inline .p { display: inline-flex; align-items: center; gap: 6px; }
.params-inline .summary { font-size: 12px; color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 200px; }
.row-actions { display: flex; justify-content: flex-end; gap: 2px; }
.rule-detail { padding: 4px 14px 16px 58px; display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.rule-detail .field.wide { grid-column: 1 / -1; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field > label { font-size: 12px; color: var(--muted); }
.field .hint { font-size: 11.5px; color: var(--faint); }
.field textarea { background: var(--bg); border: 1px solid var(--line-2); border-radius: 7px; padding: 8px 10px; min-height: 70px; resize: vertical; outline: none; font-size: 12.5px; }
.empty { padding: 26px 14px; text-align: center; color: var(--muted); }
.chev { transition: transform .15s; }
.open .chev { transform: rotate(90deg); }

/* chips / multiselect */
.ms { position: relative; }
.ms-btn { width: 100%; text-align: left; display: flex; align-items: center; gap: 4px; overflow: hidden; flex-wrap: nowrap; cursor: pointer; min-height: 28px; }
.chip { display: inline-flex; align-items: center; padding: 0 7px; height: 20px; border-radius: 5px; background: var(--panel-2); border: 1px solid var(--line-2); font-size: 11.5px; white-space: nowrap; }
.chip.all { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.ms-pop {
  position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; min-width: 190px; max-height: 300px; overflow: auto;
  background: var(--panel-2); border: 1px solid var(--line-2); border-radius: 9px; padding: 5px; box-shadow: 0 12px 30px rgba(0,0,0,.5);
}
.ms-pop label { display: flex; align-items: center; gap: 8px; padding: 5px 8px; border-radius: 6px; cursor: pointer; white-space: nowrap; }
.ms-pop label:hover { background: #202024; }
.ms-pop input { accent-color: var(--accent); margin: 0; }
.ms-pop hr { border: 0; border-top: 1px solid var(--line-2); margin: 4px 2px; }
.ms-pop .sub { font-size: 11px; color: var(--faint); margin-left: auto; padding-left: 10px; }

/* split */
.split { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px; align-items: start; }
.split .card { margin-bottom: 0; }

/* live log */
.live { display: inline-flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--muted); cursor: pointer; user-select: none; }
.live i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: pulse 2s infinite; }
.live.paused i { background: var(--faint); animation: none; }
.live.err i { background: var(--danger); animation: none; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: .35; } }
.log { height: 620px; overflow: auto; }
.ev { border-bottom: 1px solid var(--line); }
.ev-row { display: grid; grid-template-columns: 62px 70px 1fr auto; gap: 10px; align-items: center; padding: 7px 14px; cursor: pointer; }
.ev-row:hover { background: var(--panel-2); }
.ev .t { font-family: var(--mono); font-size: 11.5px; color: var(--faint); font-variant-numeric: tabular-nums; }
.ev .a { font-weight: 500; overflow: hidden; text-overflow: ellipsis; }
.ev .what { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ev .what b { color: var(--text); font-weight: 400; font-family: var(--mono); font-size: 12px; }
.ev .n { font-family: var(--mono); font-size: 11.5px; color: var(--faint); }
.ev.fresh .ev-row { animation: fresh 2.4s ease-out; }
@keyframes fresh { from { background: var(--accent-soft); } to { background: transparent; } }
.ev pre {
  margin: 0; padding: 4px 14px 14px 86px; font-family: var(--mono); font-size: 12px; line-height: 1.55;
  white-space: pre-wrap; word-break: break-word; color: #c9c9cf;
}
.ev .meta { padding: 0 14px 6px 86px; font-size: 11.5px; color: var(--faint); font-family: var(--mono); }

/* voice */
.tabs { display: flex; gap: 2px; flex-wrap: wrap; justify-content: flex-end; }
.tab { border: 0; background: none; padding: 4px 9px; border-radius: 6px; color: var(--muted); cursor: pointer; }
.tab:hover { color: var(--text); background: var(--panel-2); }
.tab.active { color: var(--text); background: var(--panel-2); box-shadow: inset 0 0 0 1px var(--line-2); }
.tab.dirty::after { content: "•"; color: var(--accent); margin-left: 4px; }
.voice { display: flex; flex-direction: column; }
.voice textarea {
  height: 572px; border: 0; resize: none; background: transparent; padding: 12px 14px; outline: none;
  font-family: var(--mono); font-size: 12px; line-height: 1.6; tab-size: 2; color: #d4d4da;
}
.voice-foot { display: flex; align-items: center; gap: 10px; padding: 8px 14px; border-top: 1px solid var(--line); min-height: 48px; }
.voice-foot .muted { font-size: 12px; }
.voice-foot .hint { font-size: 12px; color: var(--faint); }

/* drawer + toast */
.drawer { position: fixed; inset: 0; background: rgba(0,0,0,.5); display: flex; justify-content: flex-end; z-index: 50; }
.drawer[hidden] { display: none; }
.drawer-panel { width: 420px; max-width: 100%; height: 100%; background: var(--panel); border-left: 1px solid var(--line); overflow: auto; }
.drawer-panel > p { padding: 10px 14px 0; margin: 0; }
.ver { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-bottom: 1px solid var(--line); }
.ver code { font-family: var(--mono); font-size: 12px; }
.ver .muted { font-size: 12px; }
.toast {
  position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-weight: 500; box-shadow: 0 10px 30px rgba(0,0,0,.5);
}
.toast.err { background: var(--danger); color: #1a0000; }
.toast[hidden] { display: none; }

.gate { max-width: 420px; margin: 18vh auto; text-align: center; color: var(--muted); }
.gate h2 { color: var(--text); font-size: 16px; }

@media (max-width: 1180px) {
  .split { grid-template-columns: 1fr; }
  .rule-head { display: none; }
  .rule-row { grid-template-columns: 34px 1fr 1fr 56px; grid-auto-rows: auto; }
  .rule-row > .c-event, .rule-row > .c-every, .rule-row > .c-agents, .rule-row > .c-module, .rule-row > .c-params { grid-column: 2 / 4; }
  .rule-row > .row-actions { grid-row: 1; grid-column: 4; }
  .rule-detail { padding-left: 14px; }
  .rule-row > .id { grid-column: 2 / 4; }
}
@media (max-width: 760px) {
  .shell { grid-template-columns: 1fr; }
  .rail { position: static; height: auto; flex-direction: row; align-items: center; padding: 10px 14px; border-right: 0; border-bottom: 1px solid var(--line); overflow-x: auto; }
  .brand { padding: 0 12px 0 0; }
  nav { flex-direction: row; }
  .nav-group, .nav-item.soon, .rail-foot { display: none; }
  main { padding: 18px 14px 40px; }
  .master { flex-wrap: wrap; }
  .master .facts { margin-left: 0; width: 100%; }
  .page-head { flex-wrap: wrap; }
  .voice-card .card-head { flex-wrap: wrap; }
  .tabs { flex-wrap: nowrap; overflow-x: auto; justify-content: flex-start; width: 100%; }
  .voice-foot .hint { display: none; }
  .voice textarea { height: 60vh; }
  .log { height: 60vh; }
}
