:root {
  --bg: #f4f6f9;
  --bg-1: #ffffff;
  --bg-2: #eef1f5;
  --bg-3: #e2e7ee;
  --line: #e3e7ec;
  --line-2: #cdd3dc;
  --txt: #1b1f26;
  --txt-dim: #5b636f;
  --txt-faint: #8b93a0;
  --accent: #22c55e;
  --accent-d: #16a34a;
  --blue: #2563eb;
  --red: #dc2626;
  --amber: #d97706;
  /* theme-dependent semantic tokens (light defaults) */
  --accent-text: #16a34a;
  --badge-on-bg: rgba(22,163,74,.12);
  --badge-off-bg: rgba(220,38,38,.10);
  --danger-bd: #f0c9c9;
  --danger-bg: #fbecec;
  --btn-hover-bd: #cdd3dc;
  --shadow: rgba(20,30,50,.14);
  --scheme: light;
  --mono: "IBM Plex Mono", ui-monospace, monospace;
  --sans: "IBM Plex Sans", system-ui, sans-serif;
  --radius: 8px;
}

/* ── Dark theme ─────────────────────────────────────── */
:root[data-theme="dark"] {
  --bg: #0c0e11;
  --bg-1: #14171c;
  --bg-2: #1b1f26;
  --bg-3: #232830;
  --line: #2a2f38;
  --line-2: #353b46;
  --txt: #e6e9ee;
  --txt-dim: #99a1ad;
  --txt-faint: #646c79;
  --accent: #4ade80;
  --accent-d: #22c55e;
  --blue: #60a5fa;
  --red: #f87171;
  --amber: #fbbf24;
  --accent-text: #4ade80;
  --badge-on-bg: rgba(74,222,128,.12);
  --badge-off-bg: rgba(248,113,113,.12);
  --danger-bd: #4a2a2a;
  --danger-bg: #2a1414;
  --btn-hover-bd: #45505f;
  --shadow: rgba(0,0,0,.4);
  --scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--txt);
  color-scheme: var(--scheme);
  font-family: var(--sans);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
code, .mono { font-family: var(--mono); }

/* ── Layout ─────────────────────────────────────────── */
.layout { display: grid; grid-template-columns: 232px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--bg-1);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  position: sticky; top: 0; height: 100vh;
}
.brand {
  padding: 20px 22px 18px;
  font-weight: 700; font-size: 18px; letter-spacing: -0.02em;
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 9px;
}
.brand .dot { width: 9px; height: 9px; border-radius: 50%; background: var(--accent); box-shadow: 0 0 10px var(--accent); }
.nav { padding: 12px 10px; display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav a {
  color: var(--txt-dim); padding: 9px 12px; border-radius: 6px;
  font-weight: 500; display: flex; align-items: center; gap: 10px;
}
.nav a:hover { background: var(--bg-2); color: var(--txt); text-decoration: none; }
.nav a.active { background: var(--bg-3); color: var(--txt); }
.nav .ico { width: 16px; text-align: center; opacity: .8; }
.sidebar-foot { padding: 14px 16px; border-top: 1px solid var(--line); font-size: 12.5px; color: var(--txt-faint); }
.sidebar-foot .email { color: var(--txt-dim); display: block; margin-bottom: 6px; word-break: break-all; }

.main { padding: 26px 30px 60px; max-width: 1280px; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 22px; gap: 16px; flex-wrap: wrap; }
.page-head h1 { font-size: 22px; margin: 0; letter-spacing: -0.02em; }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ── Buttons ────────────────────────────────────────── */
.btn {
  font-family: var(--sans); font-size: 13.5px; font-weight: 600;
  border: 1px solid var(--line-2); background: var(--bg-2); color: var(--txt);
  padding: 8px 14px; border-radius: 7px; cursor: pointer; display: inline-flex;
  align-items: center; gap: 7px; transition: .12s; white-space: nowrap;
}
.btn:hover { background: var(--bg-3); border-color: var(--btn-hover-bd); }
.btn.primary { background: var(--accent-d); border-color: var(--accent-d); color: #fff; }
.btn.primary:hover { background: var(--accent); border-color: var(--accent); }
.btn.danger { color: var(--red); border-color: var(--danger-bd); }
.btn.danger:hover { background: var(--danger-bg); }
.btn.sm { padding: 5px 10px; font-size: 12.5px; }
.btn.ghost { background: transparent; border-color: transparent; color: var(--txt-dim); }
.btn.ghost:hover { background: var(--bg-2); color: var(--txt); }

/* ── Inputs ─────────────────────────────────────────── */
input[type=text], input[type=password], input[type=email], input[type=number], select, textarea {
  font-family: var(--sans); font-size: 13.5px; color: var(--txt);
  background: var(--bg-1); border: 1px solid var(--line-2); border-radius: 7px;
  padding: 8px 11px; width: 100%; outline: none; transition: .12s;
}
input.mono { font-family: var(--mono); }
input:focus, select:focus, textarea:focus { border-color: var(--accent-d); background: var(--bg); }
input::placeholder { color: var(--txt-faint); }
select { cursor: pointer; }
label.field { display: block; margin-bottom: 16px; }
label.field > span { display: block; font-size: 12.5px; color: var(--txt-dim); margin-bottom: 6px; font-weight: 500; }
.hint { font-size: 12px; color: var(--txt-faint); margin-top: 5px; }
.row { display: flex; gap: 12px; }
.row > * { flex: 1; }

/* ── Tables ─────────────────────────────────────────── */
.card { background: var(--bg-1); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--line); }
th { font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--txt-faint); font-weight: 600; background: var(--bg-1); }
tbody tr:hover { background: var(--bg-2); }
tbody tr:last-child td { border-bottom: none; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; font-family: var(--mono); }
td .id { font-family: var(--mono); color: var(--txt-faint); font-size: 12.5px; }
.name-cell { font-weight: 500; }
.name-cell .slug { font-family: var(--mono); font-size: 12px; color: var(--txt-faint); display: block; }
.empty { padding: 50px; text-align: center; color: var(--txt-faint); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }

/* ── Badges / status ────────────────────────────────── */
.badge { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 2px 9px; border-radius: 20px; }
.badge.on { background: var(--badge-on-bg); color: var(--accent-text); }
.badge.off { background: var(--badge-off-bg); color: var(--red); }
.badge.muted { background: var(--bg-3); color: var(--txt-dim); }

/* status toggle dot */
.tgl { width: 36px; height: 20px; border-radius: 20px; background: var(--bg-3); border: 1px solid var(--line-2); position: relative; cursor: pointer; flex: none; transition: .15s; }
.tgl::after { content: ""; position: absolute; width: 14px; height: 14px; border-radius: 50%; background: var(--txt-faint); top: 2px; left: 2px; transition: .15s; }
.tgl.on { background: rgba(34,197,94,.25); border-color: var(--accent-d); }
.tgl.on::after { background: var(--accent); left: 18px; }

.cb { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; font-size: 13px; }

/* ── Tabs ───────────────────────────────────────────── */
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin-bottom: 18px; }
.tabs button {
  background: none; border: none; color: var(--txt-dim); font-family: var(--sans);
  font-size: 13.5px; font-weight: 600; padding: 9px 14px; cursor: pointer; border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs button:hover { color: var(--txt); }
.tabs button.active { color: var(--txt); border-bottom-color: var(--accent); }

/* ── Editor 2-column ────────────────────────────────── */
.editor { display: grid; grid-template-columns: 1fr 1fr; gap: 22px; align-items: start; }
.panel { background: var(--bg-1); border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; }
.panel h3 { margin: 0 0 16px; font-size: 14px; letter-spacing: -.01em; }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 16px; }
.panel-head h3 { margin: 0; }

/* params grid */
.pgrid { display: grid; grid-template-columns: 1fr 1fr 1fr 28px; gap: 8px; align-items: center; }
.pgrid .head { font-size: 11.5px; color: var(--txt-faint); text-transform: uppercase; letter-spacing: .03em; }
.pgrid input { padding: 6px 9px; }
.x-btn { background: none; border: none; color: var(--txt-faint); cursor: pointer; font-size: 16px; padding: 4px; }
.x-btn:hover { color: var(--red); }

/* stream block */
.stream { border: 1px solid var(--line-2); border-radius: 8px; padding: 14px; margin-bottom: 12px; background: var(--bg); }
.stream-head { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.stream-head .pos { font-family: var(--mono); color: var(--txt-faint); font-size: 12px; }
.filter-row { display: grid; grid-template-columns: 1fr 90px 1fr 28px; gap: 7px; margin-top: 7px; align-items: center; }

.linkbox { background: var(--bg); border: 1px solid var(--line-2); border-radius: 7px; padding: 10px 12px; font-family: var(--mono); font-size: 12.5px; color: var(--accent-text); word-break: break-all; display: flex; gap: 10px; align-items: flex-start; }
.linkbox .copy { margin-left: auto; }

/* ── Login ──────────────────────────────────────────── */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 360px; background: var(--bg-1); border: 1px solid var(--line); border-radius: 12px; padding: 30px; }
.login-card .brand { border: none; padding: 0 0 22px; }

/* ── Toast ──────────────────────────────────────────── */
#toast { position: fixed; right: 22px; bottom: 22px; display: flex; flex-direction: column; gap: 8px; z-index: 100; }
.toast { background: var(--bg-1); border: 1px solid var(--line-2); border-left: 3px solid var(--accent); padding: 11px 16px; border-radius: 7px; font-size: 13px; min-width: 220px; box-shadow: 0 8px 24px var(--shadow); animation: slidein .2s; }
.toast.err { border-left-color: var(--red); }
@keyframes slidein { from { transform: translateY(8px); opacity: 0; } }

/* ── Modal ──────────────────────────────────────────── */
.overlay { position: fixed; inset: 0; background: rgba(0,0,0,.6); display: grid; place-items: center; z-index: 90; padding: 20px; }
.modal { background: var(--bg-1); border: 1px solid var(--line-2); border-radius: 12px; padding: 24px; width: 100%; max-width: 440px; }
.modal h3 { margin: 0 0 16px; }
.modal-foot { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; }

/* ── Log tables ─────────────────────────────────────── */
#log-body { overflow-x: auto; }
.subid { font-family: var(--mono); font-size: 11.5px; color: var(--txt-faint); margin-top: 2px; }
.chip { display: inline-block; font-family: var(--mono); font-size: 11.5px; background: var(--bg-3); color: var(--txt-dim); border-radius: 5px; padding: 1px 7px; margin: 1px 0; white-space: nowrap; }
.chip b { color: var(--txt); font-weight: 600; }
td.chips { max-width: 280px; }
tfoot tr.totals td { background: var(--bg-2); border-top: 1px solid var(--line-2); font-size: 13px; color: var(--txt-dim); position: sticky; bottom: 0; }
tfoot tr.totals b { color: var(--txt); font-variant-numeric: tabular-nums; }
.urlcell { display: flex; align-items: center; gap: 8px; }
.urlcell .log-url { font-family: var(--mono); font-size: 12px; color: var(--accent-text); max-width: 360px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.urlcell .copy { flex: none; }
.pager { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-top: 14px; flex-wrap: wrap; }
.pager-btns { display: flex; align-items: center; gap: 8px; }
.pager .btn[disabled] { opacity: .4; cursor: default; pointer-events: none; }

/* ── Period (date range) ────────────────────────────── */
.period { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin-bottom: 16px; }
.period input[type=date] { width: auto; font-family: var(--mono); font-size: 12.5px; color-scheme: var(--scheme); }
.period-presets { display: flex; gap: 4px; margin-left: 4px; }
.period-presets .btn { padding: 5px 10px; }
.flag { font-size: 15px; line-height: 1; vertical-align: -1px; }

/* ── System status ──────────────────────────────────── */
.sys-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 18px; align-items: start; }
.kv { display: flex; justify-content: space-between; gap: 12px; padding: 5px 0; font-size: 13px; }
.kv > span { color: var(--txt-dim); }
.kv > b { font-variant-numeric: tabular-nums; }
.meter { height: 8px; background: var(--bg-3); border-radius: 6px; overflow: hidden; margin-top: 6px; }
.meter-fill { height: 100%; background: var(--accent-d); border-radius: 6px; transition: width .2s; }
.meter-fill.warn { background: var(--amber); }
.meter-fill.danger { background: var(--red); }

.section-sep { height: 1px; background: var(--line); margin: 24px 0; border: none; }
.muted { color: var(--txt-dim); }
.spinner { padding: 60px; text-align: center; color: var(--txt-faint); }

@media (max-width: 980px) {
  .layout { grid-template-columns: 1fr; }
  .sidebar { position: static; height: auto; flex-direction: row; flex-wrap: wrap; }
  .nav { flex-direction: row; flex-wrap: wrap; flex: 1; }
  .editor { grid-template-columns: 1fr; }
}
