:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #16191f;
  --muted: #6b7280;
  --accent: #e8a317;
  --accent-text: #1a1300;
  --up: #12805c;
  --down: #c8341f;
  --warn-bg: #fff4e0;
  --warn-text: #8a5300;
  --danger: #c8341f;
  --series-1: #2f6fdb;
  --series-2: #9aa3b2;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .05);
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #171a20;
    --surface-2: #1f232b;
    --border: #2a2f39;
    --text: #e8eaee;
    --muted: #9099a8;
    --accent: #f0b429;
    --up: #3ccf91;
    --down: #ff6b55;
    --warn-bg: #3a2a0c;
    --warn-text: #ffd27a;
    --danger: #ff6b55;
    --series-1: #6fa1ff;
    --series-2: #5d6676;
    --shadow: none;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-variant-numeric: tabular-nums;
}
.hidden { display: none !important; }
h1 { font-size: 22px; margin: 0 0 4px; }
h2 { font-size: 15px; margin: 0; font-weight: 600; }
code { background: var(--surface-2); padding: 1px 5px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.up { color: var(--up); }
.down { color: var(--down); }

/* layout */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 16px;
  padding: 10px 20px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; letter-spacing: .2px; }
.tabs { display: flex; gap: 2px; flex: 1; overflow-x: auto; }
.tabs button {
  background: none; border: 0; color: var(--muted); cursor: pointer;
  padding: 8px 12px; border-radius: 8px; font: inherit; white-space: nowrap;
}
.tabs button:hover { background: var(--surface-2); color: var(--text); }
.tabs button.active { color: var(--text); background: var(--surface-2); font-weight: 600; }
main { max-width: 1200px; margin: 0 auto; padding: 20px; }
.tab > * + * { margin-top: 16px; }
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 16px; box-shadow: var(--shadow);
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 12px; }
.grid2 { display: grid; grid-template-columns: repeat(auto-fit, minmax(420px, 1fr)); gap: 16px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); gap: 16px; }
.kpi-label { color: var(--muted); font-size: 12px; }
.kpi-value { font-size: 24px; font-weight: 600; margin-top: 2px; }
.kpi-value.small { font-size: 15px; }

/* buttons & inputs */
.btn {
  font: inherit; cursor: pointer; border-radius: 8px; padding: 7px 14px;
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
.btn.primary:hover { filter: brightness(1.05); }
.btn.ghost { background: transparent; }
.btn.small { padding: 4px 10px; font-size: 13px; }
.btn.danger-ghost { color: var(--danger); background: transparent; }
.icon-btn { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 16px; padding: 2px 6px; }
.icon-btn:hover { color: var(--danger); }
input, select {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: 8px; padding: 7px 10px; width: 100%;
}
input:focus, select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.field > span { font-size: 12px; color: var(--muted); }
.field .hint { margin: 0; }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.row > .field { flex: 1 1 220px; }
.fields-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); gap: 14px 16px; }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
.check { flex-direction: row; align-items: center; gap: 8px; }
.check input { width: auto; }

/* status */
.statusbar { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.status-left, .status-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.badge { font-size: 12px; font-weight: 700; padding: 3px 8px; border-radius: 6px; background: var(--surface-2); letter-spacing: .3px; }
.badge.live { background: var(--down); color: #fff; }
.badge.test { background: var(--series-1); color: #fff; }
.run-state::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); }
.run-state.on::before { background: var(--up); }
.banner { background: var(--warn-bg); color: var(--warn-text); border-radius: var(--radius); padding: 10px 14px; }

/* tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: right; padding: 7px 8px; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { font-weight: 500; color: var(--muted); font-size: 12px; }
th:first-child, td:first-child { text-align: left; }
td.left, th.left { text-align: left; }
tr:last-child td { border-bottom: 0; }
.tag { font-size: 11px; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--muted); }
.tag.ok { background: color-mix(in srgb, var(--up) 18%, transparent); color: var(--up); }

.cat-table { table-layout: fixed; min-width: 860px; }
.cat-table th:nth-child(1) { width: 8%; }
.cat-table th:nth-child(2) { width: 22%; }
.cat-table th:nth-child(10) { width: 64px; }

/* share bars */
.sharebar { position: relative; height: 6px; width: 90px; background: var(--surface-2); border-radius: 3px; display: inline-block; vertical-align: middle; margin-left: 6px; }
.sharebar i { position: absolute; left: 0; top: 0; bottom: 0; background: var(--series-1); border-radius: 3px; }
.sharebar b { position: absolute; top: -3px; bottom: -3px; width: 2px; background: var(--text); opacity: .5; }
.cat-progress { display: flex; align-items: center; gap: 10px; }
.cat-progress .sharebar { width: 160px; height: 8px; }

/* settings */
.cat-editor h3 { margin: 0 0 10px; font-size: 14px; text-transform: capitalize; }
.cat-editor table td { padding: 4px 6px; }
.cat-editor input { padding: 5px 8px; }
.add-row { display: flex; gap: 8px; margin-top: 10px; }
.savebar {
  position: sticky; bottom: 0; display: flex; justify-content: flex-end; align-items: center; gap: 10px;
  padding: 12px 16px; margin-top: 16px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.savebar > div { flex: 1; }
.error-text { color: var(--danger); min-height: 1em; }

/* charts */
.chart { width: 100%; min-height: 220px; }
.chart svg { display: block; width: 100%; height: 240px; }
.chart .axis { stroke: var(--border); }
.chart text { fill: var(--muted); font-size: 11px; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 10px; height: 3px; margin-right: 5px; vertical-align: middle; }
.empty { color: var(--muted); padding: 24px 0; text-align: center; }

.logs {
  margin: 0; max-height: 70vh; overflow: auto; font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap; word-break: break-word;
}

/* login */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 340px; display: flex; flex-direction: column; gap: 12px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 8px; z-index: 50;
  max-width: calc(100% - 32px);
}

@media (max-width: 640px) {
  main { padding: 12px 16px; }
  .topbar { padding: 8px 12px; gap: 8px; flex-wrap: wrap; }
  .tabs { order: 3; width: 100%; }
  .grid2 { grid-template-columns: 1fr; }
  .kpi-value { font-size: 20px; }
  .kpis { grid-template-columns: 1fr 1fr; gap: 10px; }
  .kpi { padding: 12px; }
}

#settings-form > .card + .card { margin-top: 16px; }

/* optimizer */
.progress { height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; margin: 12px 0 6px; }
.progress i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s; }
.heat { border-collapse: separate; border-spacing: 3px; }
.heat th, .heat td { border: 0; text-align: center; padding: 8px 6px; }
.heat td { border-radius: 6px; min-width: 76px; font-size: 13px; }
.heat td.best { outline: 2px solid var(--text); outline-offset: -2px; font-weight: 700; }
.ratio { position: relative; display: inline-block; width: 120px; height: 8px; background: var(--surface-2); border-radius: 4px; vertical-align: middle; }
.ratio i { position: absolute; left: 0; top: 0; bottom: 0; border-radius: 4px; background: var(--series-2); }
.ratio i.ok { background: var(--up); }
.ratio b { position: absolute; top: -3px; bottom: -3px; width: 2px; left: 50%; background: var(--text); opacity: .6; }
