/* ---------- tokens (light) ---------- */
:root {
  color-scheme: light;
  --bg: #f4f4f2;
  --surface-1: #fcfcfb;
  --surface-2: #efeeea;
  --border: #dddcd6;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #6d6c67;
  --grid: #e6e5e0;
  --series-1: #2a78d6;
  --good: #0a7d0a;       /* text-safe step of the status "good" */
  --critical: #c03030;
  --warning-bg: #fff4d6;
  --warning-ink: #6b4a00;
  --critical-bg: #fde8e8;
  --kill: #c62828;
  --kill-ink: #ffffff;
  --live: #c62828;
  --paper: #2a78d6;
  --focus: #2a78d6;
  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #111110;
    --surface-1: #1a1a19;
    --surface-2: #252523;
    --border: #34332f;
    --text-primary: #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted: #9a998f;
    --grid: #2c2c29;
    --series-1: #3987e5;
    --good: #2fbf2f;
    --critical: #ff6b6b;
    --warning-bg: #3a2e0a;
    --warning-ink: #ffd36b;
    --critical-bg: #3a1515;
    --kill: #e53935;
    --paper: #3987e5;
    --focus: #6aa8f0;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111110; --surface-1: #1a1a19; --surface-2: #252523; --border: #34332f;
  --text-primary: #ffffff; --text-secondary: #c3c2b7; --text-muted: #9a998f; --grid: #2c2c29;
  --series-1: #3987e5; --good: #2fbf2f; --critical: #ff6b6b; --warning-bg: #3a2e0a; --warning-ink: #ffd36b;
  --critical-bg: #3a1515; --kill: #e53935; --paper: #3987e5; --focus: #6aa8f0;
}

/* ---------- base ---------- */
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text-primary);
  font: 16px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  font-variant-numeric: tabular-nums;
}
h1 { font-size: 1.5rem; margin: 0 0 .25rem; }
h2 { font-size: 1.05rem; margin: 0 0 .75rem; }
.muted { color: var(--text-secondary); margin: 0 0 .75rem; font-size: .9rem; }
.error { color: var(--critical); min-height: 1.2em; margin: .5rem 0 0; }
[hidden] { display: none !important; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- buttons ---------- */
.btn {
  appearance: none; border: 1px solid var(--border); background: var(--surface-2); color: var(--text-primary);
  font: inherit; font-weight: 600; padding: .7rem 1rem; border-radius: 10px; cursor: pointer; min-height: 44px;
}
.btn.primary { background: var(--series-1); border-color: var(--series-1); color: #fff; }
.btn.kill { background: var(--kill); border-color: var(--kill); color: var(--kill-ink); letter-spacing: .04em; }
.btn.kill-outline { background: transparent; border: 2px solid var(--kill); color: var(--kill); }
.btn.wide { display: block; width: 100%; margin: 0 0 .6rem; text-align: left; }
.btn.wide span { display: block; font-weight: 400; font-size: .85rem; opacity: .9; margin-top: .15rem; }
.btn:disabled { opacity: .5; cursor: progress; }
.row { display: flex; gap: .5rem; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- login ---------- */
.login { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 380px; }
.login-card label { display: block; margin: .9rem 0; font-size: .9rem; color: var(--text-secondary); }
.login-card .btn { width: 100%; }

/* ---------- top bar (always visible) ---------- */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: .75rem;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  background: var(--surface-1); border-bottom: 1px solid var(--border);
}
.topbar-left { display: flex; align-items: center; gap: .5rem; min-width: 0; flex-wrap: wrap; }
.badge { font-weight: 800; font-size: .75rem; letter-spacing: .08em; padding: .25rem .5rem; border-radius: 6px;
         color: #fff; background: var(--paper); }
.badge.live { background: var(--live); }
.pill { font-size: .85rem; font-weight: 600; padding: .2rem .6rem; border-radius: 999px; background: var(--surface-2);
        color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55vw; }
.pill.running { color: var(--good); }
.pill.halted { color: var(--critical); }
.topbar .kill { padding: .6rem 1.2rem; font-size: 1rem; box-shadow: 0 0 0 3px color-mix(in srgb, var(--kill) 25%, transparent); }

/* ---------- layout ---------- */
.content { max-width: 860px; margin: 0 auto; padding: 12px 16px calc(84px + env(safe-area-inset-bottom)); }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; margin: 0 0 12px; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: .5rem; margin-bottom: .5rem; }
.card-head h2 { margin: 0; }

.banner { margin: 12px 16px 0; padding: 12px 14px; border-radius: 12px; font-size: .95rem; max-width: 828px; }
@media (min-width: 892px) { .banner { margin: 12px auto 0; } }
.banner.warn { background: var(--warning-bg); color: var(--warning-ink); }
.banner.crit { background: var(--critical-bg); color: var(--critical); }
.banner strong { display: block; margin-bottom: .2rem; }
.banner .btn { margin-top: .6rem; }

/* ---------- stat tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 12px; }
@media (min-width: 640px) { .tiles { grid-template-columns: repeat(3, minmax(0, 1fr)); } }
.tile { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; }
.tile .label { font-size: .8rem; color: var(--text-secondary); }
.tile .value { font-size: 1.35rem; font-weight: 700; margin-top: .15rem; overflow-wrap: anywhere; }
.tile .sub { font-size: .8rem; color: var(--text-muted); }
.up { color: var(--good); }
.down { color: var(--critical); }
.meter { height: 6px; background: var(--surface-2); border-radius: 3px; margin-top: .45rem; overflow: hidden; }
.meter > i { display: block; height: 100%; background: var(--text-secondary); border-radius: 3px; }
.meter.hot > i { background: var(--critical); }

/* ---------- segmented control ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 9px; padding: 2px; }
.seg button { appearance: none; border: 0; background: transparent; color: var(--text-secondary); font: inherit;
              font-size: .85rem; padding: .35rem .6rem; border-radius: 7px; min-height: 32px; cursor: pointer; }
.seg button.on { background: var(--surface-1); color: var(--text-primary); font-weight: 600; }

/* ---------- chart ---------- */
.chart { position: relative; height: 220px; touch-action: pan-y; }
.chart svg { display: block; width: 100%; height: 100%; overflow: visible; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .axis text { fill: var(--text-muted); font-size: 11px; }
.chart .line { fill: none; stroke: var(--series-1); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .cross { stroke: var(--text-muted); stroke-width: 1; }
.chart .dot { fill: var(--series-1); stroke: var(--surface-1); stroke-width: 2; }
.chart .empty { display: grid; place-items: center; height: 100%; color: var(--text-muted); font-size: .9rem; text-align: center; }
.tooltip { position: absolute; pointer-events: none; background: var(--surface-1); border: 1px solid var(--border);
           border-radius: 8px; padding: .35rem .55rem; font-size: .8rem; box-shadow: 0 4px 14px rgb(0 0 0 / .12);
           white-space: nowrap; transform: translate(-50%, -110%); }
.tooltip strong { display: block; font-size: .95rem; color: var(--text-primary); }
.tooltip span { color: var(--text-secondary); }
.table-view summary { cursor: pointer; color: var(--text-secondary); font-size: .85rem; margin-top: .5rem; }

/* ---------- tables & lists ---------- */
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th { text-align: left; color: var(--text-secondary); font-weight: 600; font-size: .78rem; padding: .35rem .4rem; border-bottom: 1px solid var(--border); }
.tbl td { padding: .5rem .4rem; border-bottom: 1px solid var(--grid); vertical-align: top; }
.tbl td.num, .tbl th.num { text-align: right; }
.scroll-x { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.empty-msg { color: var(--text-muted); font-size: .9rem; margin: 0; }

.pos { display: grid; grid-template-columns: 1fr auto; gap: .15rem .75rem; padding: .7rem 0; border-bottom: 1px solid var(--grid); }
.pos:last-child { border-bottom: 0; }
.pos .sym { font-weight: 700; }
.pos .pl { text-align: right; font-weight: 700; }
.pos .detail { grid-column: 1 / -1; font-size: .82rem; color: var(--text-secondary); }
.ok-tag { color: var(--good); font-weight: 600; }
.bad-tag { color: var(--critical); font-weight: 600; }

.log { list-style: none; margin: 0; padding: 0; }
.log li { padding: .55rem 0; border-bottom: 1px solid var(--grid); font-size: .85rem; overflow-wrap: anywhere; }
.log .meta { color: var(--text-muted); font-size: .75rem; }
.log .ev { font-weight: 600; }
.log .cat { display: inline-block; font-size: .7rem; font-weight: 700; text-transform: uppercase; letter-spacing: .05em;
            padding: .05rem .35rem; border-radius: 4px; background: var(--surface-2); color: var(--text-secondary); margin-right: .35rem; }
.log .cat.error, .log .cat.risk { background: var(--critical-bg); color: var(--critical); }
.log .details { color: var(--text-secondary); font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: .75rem; margin-top: .2rem; }

.warnings { list-style: none; padding: 0; margin: .75rem 0 0; }
.warnings li { padding: .5rem .65rem; border-radius: 8px; margin-bottom: .4rem; font-size: .88rem; background: var(--surface-2); }
.warnings li.critical { background: var(--critical-bg); color: var(--critical); }
.warnings li.warning { background: var(--warning-bg); color: var(--warning-ink); }

/* ---------- forms ---------- */
.fields { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: .75rem 1rem; margin-bottom: 1rem; }
label { font-size: .88rem; color: var(--text-secondary); }
input, textarea, select {
  display: block; width: 100%; margin-top: .3rem; font: inherit; font-size: 16px; color: var(--text-primary);
  background: var(--surface-2); border: 1px solid var(--border); border-radius: 9px; padding: .6rem .7rem; min-height: 44px;
}
select { width: auto; margin: 0; }
input[type="checkbox"] { width: auto; min-height: 0; display: inline-block; margin-right: .4rem; }
.hint { display: block; font-size: .75rem; color: var(--text-muted); margin-top: .2rem; }
.form-msg { min-height: 1.2em; margin: .6rem 0 0; font-size: .9rem; }
.form-msg.ok { color: var(--good); }
.form-msg.err { color: var(--critical); }
.danger-zone { border-color: color-mix(in srgb, var(--kill) 45%, var(--border)); }
.disclaimer { color: var(--text-muted); font-size: .8rem; text-align: center; margin: 1.5rem 0; }

/* ---------- bottom tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--surface-1); border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button { appearance: none; border: 0; background: transparent; color: var(--text-muted); font: inherit;
                 font-size: .85rem; padding: .85rem 0; min-height: 52px; cursor: pointer; }
.tabbar button.on { color: var(--series-1); font-weight: 700; }

/* ---------- dialogs ---------- */
dialog.sheet {
  border: 0; border-radius: 18px; padding: 20px; width: min(440px, calc(100vw - 32px));
  background: var(--surface-1); color: var(--text-primary); box-shadow: 0 20px 60px rgb(0 0 0 / .35);
}
dialog::backdrop { background: rgb(0 0 0 / .55); }
dialog .row { margin-top: 1rem; }
#kill-result { margin: .5rem 0 0; font-size: .9rem; }
