/* ==========================================================================
   server4 control panel — stylesheet

   Colour comes from the validated reference data-viz palette: categorical
   slots 1–3 (blue / orange / aqua), the fixed status palette, and the chart
   chrome + ink roles. Slots 1–3 are the documented all-pairs-safe subset, so
   every chart here caps at three series by construction.

   Dark mode is *selected*, not an inversion: each role has its own step
   chosen for the dark surface. Values are declared in three scopes so the
   toggle wins in both directions over the OS preference.
   ========================================================================== */

:root {
  color-scheme: light;

  /* surfaces & ink */
  --plane:          #f9f9f7;
  --surface:        #fcfcfb;
  --surface-2:      #f2f1ed;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #898781;
  --gridline:       #e1e0d9;
  --baseline:       #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);
  --border-strong:  rgba(11, 11, 11, 0.18);
  --wash:           rgba(11, 11, 11, 0.04);

  /* categorical series (slots 1–3 of the reference theme) */
  --series-1: #2a78d6;   /* blue   */
  --series-2: #eb6834;   /* orange */
  --series-3: #1baf7a;   /* aqua   */

  /* status — fixed, never themed */
  --good:     #0ca30c;
  --warning:  #fab219;
  --serious:  #ec835a;
  --critical: #d03b3b;
  --good-text: #006300;

  --radius: 10px;
  --radius-sm: 6px;
  --shadow: 0 1px 2px rgba(11,11,11,.05), 0 4px 12px rgba(11,11,11,.04);
  --font: system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --plane:          #0d0d0d;
    --surface:        #1a1a19;
    --surface-2:      #232322;
    --text-primary:   #ffffff;
    --text-secondary: #c3c2b7;
    --text-muted:     #898781;
    --gridline:       #2c2c2a;
    --baseline:       #383835;
    --border:         rgba(255, 255, 255, 0.10);
    --border-strong:  rgba(255, 255, 255, 0.18);
    --wash:           rgba(255, 255, 255, 0.05);
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --good-text: #0ca30c;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 12px rgba(0,0,0,.3);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --plane:          #0d0d0d;
  --surface:        #1a1a19;
  --surface-2:      #232322;
  --text-primary:   #ffffff;
  --text-secondary: #c3c2b7;
  --text-muted:     #898781;
  --gridline:       #2c2c2a;
  --baseline:       #383835;
  --border:         rgba(255, 255, 255, 0.10);
  --border-strong:  rgba(255, 255, 255, 0.18);
  --wash:           rgba(255, 255, 255, 0.05);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --good-text: #0ca30c;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 4px 12px rgba(0,0,0,.3);
}

/* -------------------------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  color: var(--text-primary);
  background: var(--plane);
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3 { margin: 0; font-weight: 600; letter-spacing: -0.01em; }
h2 { font-size: 14px; }

/* ── top bar ─────────────────────────────────────────────────────────────── */

.topbar {
  position: sticky; top: 0; z-index: 40;
  display: flex; align-items: center; gap: 20px;
  flex-wrap: wrap;
  padding: 12px clamp(14px, 3vw, 28px);
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.brand-mark {
  width: 30px; height: 30px; border-radius: 8px; flex: none;
  background: linear-gradient(140deg, var(--series-1), var(--series-3));
  position: relative;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 8px 7px;
  border-radius: 2px;
  background: repeating-linear-gradient(
    to bottom, #fff 0 3px, transparent 3px 6px);
  opacity: .85;
}

.brand-text { display: flex; flex-direction: column; line-height: 1.25; min-width: 0; }
.brand-text strong { font-size: 15px; letter-spacing: -0.02em; }
.brand-sub {
  font-size: 11.5px; color: var(--text-muted);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.topbar-stats {
  display: flex; gap: 18px; flex: 1 1 auto; flex-wrap: wrap;
  min-width: 0;
}
.mini { display: flex; flex-direction: column; line-height: 1.2; }
.mini-label {
  font-size: 10px; text-transform: uppercase; letter-spacing: .07em;
  color: var(--text-muted); font-weight: 600;
}
.mini-value { font-size: 14px; font-weight: 600; color: var(--text-primary); }

.topbar-actions { display: flex; align-items: center; gap: 10px; margin-left: auto; }

.conn {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--text-secondary);
  padding: 4px 9px; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2);
}
.conn-dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--good);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 22%, transparent);
}
.conn.is-stale .conn-dot { background: var(--warning); box-shadow: 0 0 0 3px color-mix(in srgb, var(--warning) 22%, transparent); }
.conn.is-down .conn-dot { background: var(--critical); box-shadow: 0 0 0 3px color-mix(in srgb, var(--critical) 22%, transparent); }

.btn-icon {
  width: 30px; height: 30px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text-secondary); font-size: 14px; line-height: 1;
}
.btn-icon:hover { background: var(--wash); color: var(--text-primary); }

/* ── tabs ────────────────────────────────────────────────────────────────── */

.tabs {
  display: flex; gap: 2px; overflow-x: auto;
  padding: 0 clamp(14px, 3vw, 28px);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 55px; z-index: 30;
  scrollbar-width: none;
}
.tabs::-webkit-scrollbar { display: none; }

.tab {
  appearance: none; border: 0; background: none; cursor: pointer;
  padding: 11px 14px; font: inherit; font-size: 13px; font-weight: 500;
  color: var(--text-secondary); white-space: nowrap;
  border-bottom: 2px solid transparent;
  display: inline-flex; align-items: center; gap: 7px;
}
.tab:hover { color: var(--text-primary); }
.tab.is-active { color: var(--text-primary); border-bottom-color: var(--series-1); font-weight: 600; }

.tab-badge {
  font-size: 10.5px; font-weight: 600; padding: 1px 6px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-secondary);
  border: 1px solid var(--border);
  font-variant-numeric: tabular-nums;
}
.tab-badge:empty { display: none; }
.tab-badge.is-alert { background: color-mix(in srgb, var(--critical) 15%, transparent);
  color: var(--critical); border-color: color-mix(in srgb, var(--critical) 35%, transparent); }

/* ── layout ──────────────────────────────────────────────────────────────── */

main { padding: clamp(14px, 3vw, 24px); max-width: 1600px; margin: 0 auto; }

.view { display: none; }
.view.is-active { display: block; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }

.tile-grid {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
}
.chart-grid {
  display: grid; gap: 12px; margin-bottom: 16px;
  grid-template-columns: repeat(auto-fit, minmax(340px, 1fr));
}
.split-grid {
  display: grid; gap: 12px;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
}

/* ── cards ───────────────────────────────────────────────────────────────── */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  margin: 0;
}

.card-head {
  display: flex; align-items: flex-start; justify-content: space-between;
  gap: 14px; flex-wrap: wrap;
  padding: 13px 15px 11px;
  border-bottom: 1px solid var(--border);
}
.card-sub { margin: 2px 0 0; font-size: 11.5px; color: var(--text-muted); }
.card-body { padding: 13px 15px; }
.head-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

/* ── stat tiles ──────────────────────────────────────────────────────────── */

.tile {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 13px 15px 14px;
  box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 7px;
  position: relative; overflow: hidden;
}
.tile-label {
  font-size: 10.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .07em; color: var(--text-muted);
  display: flex; align-items: center; gap: 6px;
}
.tile-value {
  font-size: 27px; font-weight: 600; line-height: 1;
  letter-spacing: -0.03em; color: var(--text-primary);
}
.tile-value .unit { font-size: 14px; font-weight: 500; color: var(--text-secondary); margin-left: 2px; }
.tile-note { font-size: 11.5px; color: var(--text-secondary); }

/* meter: a thin magnitude bar with a 4px rounded data-end on the baseline */
.meter {
  height: 5px; border-radius: 3px; background: var(--surface-2);
  overflow: hidden; border: 1px solid var(--border);
}
.meter-fill {
  height: 100%; border-radius: 0 3px 3px 0;
  background: var(--series-1);
  transition: width .45s cubic-bezier(.4,0,.2,1), background-color .3s;
}
.meter-fill[data-status="good"]     { background: var(--good); }
.meter-fill[data-status="warning"]  { background: var(--warning); }
.meter-fill[data-status="serious"]  { background: var(--serious); }
.meter-fill[data-status="critical"] { background: var(--critical); }

.status-chip {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 11px; font-weight: 600; padding: 2px 8px; border-radius: 999px;
  border: 1px solid var(--border);
}
.status-chip::before { content: ""; width: 6px; height: 6px; border-radius: 50%; background: currentColor; flex: none; }
.status-chip[data-status="good"]     { color: var(--good-text);  background: color-mix(in srgb, var(--good) 12%, transparent); }
.status-chip[data-status="warning"]  { color: #7a5200;           background: color-mix(in srgb, var(--warning) 20%, transparent); }
.status-chip[data-status="serious"]  { color: #8c3d18;           background: color-mix(in srgb, var(--serious) 20%, transparent); }
.status-chip[data-status="critical"] { color: var(--critical);   background: color-mix(in srgb, var(--critical) 13%, transparent); }
.status-chip[data-status="neutral"]  { color: var(--text-secondary); background: var(--surface-2); }
:root[data-theme="dark"] .status-chip[data-status="warning"] { color: var(--warning); }
:root[data-theme="dark"] .status-chip[data-status="serious"] { color: var(--serious); }
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .status-chip[data-status="warning"] { color: var(--warning); }
  :root:where(:not([data-theme="light"])) .status-chip[data-status="serious"] { color: var(--serious); }
}

/* ── charts ──────────────────────────────────────────────────────────────── */

.chart-card { display: flex; flex-direction: column; }
.chart-host { position: relative; padding: 10px 8px 4px; height: 190px; }
.chart-host canvas { width: 100%; height: 100%; display: block; }

.legend { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; }
.legend-item {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 11.5px; color: var(--text-secondary); white-space: nowrap;
}
.legend-swatch { width: 9px; height: 9px; border-radius: 2px; flex: none; }
.legend-value {
  font-weight: 600; color: var(--text-primary);
  font-variant-numeric: tabular-nums;
}

.chart-tip {
  position: absolute; pointer-events: none; z-index: 5;
  background: var(--surface); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
  padding: 7px 9px; font-size: 11.5px; min-width: 128px;
  opacity: 0; transition: opacity .12s;
}
.chart-tip.is-on { opacity: 1; }
.chart-tip-time { color: var(--text-muted); font-size: 10.5px; margin-bottom: 4px; }
.chart-tip-row { display: flex; align-items: center; gap: 6px; justify-content: space-between; }
.chart-tip-row span:first-child { display: inline-flex; align-items: center; gap: 5px; color: var(--text-secondary); }
.chart-tip-row b { font-variant-numeric: tabular-nums; font-weight: 600; }

/* ── tables ──────────────────────────────────────────────────────────────── */

.table-scroll { overflow-x: auto; padding: 0; }
table { width: 100%; border-collapse: collapse; font-size: 12.5px; }
thead th {
  text-align: left; font-size: 10.5px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-muted);
  padding: 9px 13px; border-bottom: 1px solid var(--border);
  white-space: nowrap; position: sticky; top: 0; background: var(--surface); z-index: 1;
}
tbody td { padding: 9px 13px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--wash); }
td.num { font-variant-numeric: tabular-nums; text-align: right; white-space: nowrap; }
td.mono, .mono { font-family: var(--mono); font-size: 11.5px; }
.muted { color: var(--text-muted); }
.nowrap { white-space: nowrap; }

.kv { display: grid; grid-template-columns: auto 1fr; gap: 7px 14px; align-items: center; }
.kv dt { color: var(--text-secondary); font-size: 12px; }
.kv dd { margin: 0; font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }

.bar-row { display: flex; flex-direction: column; gap: 5px; margin-bottom: 13px; }
.bar-row:last-child { margin-bottom: 0; }
.bar-head { display: flex; justify-content: space-between; align-items: baseline; gap: 10px; font-size: 12px; }
.bar-head b { font-variant-numeric: tabular-nums; }

/* stacked composition bar — 2px surface gap between segments */
.stack { display: flex; height: 9px; border-radius: 4px; overflow: hidden; background: var(--surface-2);
  border: 1px solid var(--border); gap: 2px; }
.stack-seg { height: 100%; }
.stack-legend { display: flex; gap: 12px; flex-wrap: wrap; margin-top: 9px; }

/* ── controls ────────────────────────────────────────────────────────────── */

.btn {
  appearance: none; cursor: pointer; font: inherit; font-size: 12px; font-weight: 500;
  padding: 5px 11px; border-radius: var(--radius-sm);
  border: 1px solid var(--border-strong); background: var(--surface);
  color: var(--text-primary);
}
.btn:hover { background: var(--wash); }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; cursor: not-allowed; }
.btn-sm { padding: 3px 8px; font-size: 11px; }
.btn-danger { color: var(--critical); border-color: color-mix(in srgb, var(--critical) 40%, transparent); }
.btn-danger:hover { background: color-mix(in srgb, var(--critical) 10%, transparent); }

.input {
  font: inherit; font-size: 12px; padding: 5px 9px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--surface); color: var(--text-primary); min-width: 150px;
}
.input:focus { outline: 2px solid color-mix(in srgb, var(--series-1) 45%, transparent); outline-offset: -1px; }

.switch { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; color: var(--text-secondary); cursor: pointer; }
.switch input { accent-color: var(--series-1); }

.actions { display: flex; gap: 5px; justify-content: flex-end; }

/* ── logs ────────────────────────────────────────────────────────────────── */

.logbox {
  margin: 0; padding: 13px 15px; font-family: var(--mono); font-size: 11.5px;
  line-height: 1.55; white-space: pre-wrap; word-break: break-word;
  max-height: 62vh; overflow: auto; color: var(--text-secondary);
  background: var(--surface);
}

/* ── toasts ──────────────────────────────────────────────────────────────── */

.toast-host {
  position: fixed; bottom: 16px; right: 16px; z-index: 90;
  display: flex; flex-direction: column; gap: 8px; align-items: flex-end;
}
.toast {
  background: var(--surface); border: 1px solid var(--border-strong);
  border-left: 3px solid var(--series-1);
  border-radius: var(--radius-sm); box-shadow: var(--shadow);
  padding: 9px 13px; font-size: 12.5px; max-width: 340px;
  animation: slide .2s ease;
}
.toast[data-kind="error"] { border-left-color: var(--critical); }
.toast[data-kind="ok"]    { border-left-color: var(--good); }
@keyframes slide { from { opacity: 0; transform: translateX(12px); } to { opacity: 1; transform: none; } }

.empty { padding: 26px 15px; text-align: center; color: var(--text-muted); font-size: 12.5px; }

@media (max-width: 720px) {
  .tabs { top: 0; position: static; }
  .topbar { position: static; }
  .topbar-stats { order: 3; width: 100%; gap: 14px; }
  .chart-host { height: 165px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

/* ── login page ──────────────────────────────────────────────────────────
   Served to unauthenticated visitors, so it deliberately reveals nothing
   about the host: no hostname, no version, no service list. */

.login-body { min-height: 100vh; display: grid; place-items: center; padding: 20px; }

.login-shell { width: 100%; max-width: 380px; }

.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 26px 24px 22px;
  display: flex; flex-direction: column; gap: 16px;
}

.login-brand { display: flex; align-items: center; gap: 12px; }
.login-brand h1 { font-size: 19px; letter-spacing: -0.02em; }
.login-sub { margin: 1px 0 0; font-size: 12px; color: var(--text-muted); }

.login-field { display: flex; flex-direction: column; gap: 6px; }
.login-field span {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .07em; color: var(--text-secondary);
}
.login-field input {
  font: inherit; font-size: 15px; padding: 10px 12px;
  border-radius: var(--radius-sm); border: 1px solid var(--border-strong);
  background: var(--plane); color: var(--text-primary); width: 100%;
}
.login-field input:focus {
  outline: 2px solid color-mix(in srgb, var(--series-1) 50%, transparent);
  outline-offset: 1px; border-color: var(--series-1);
}

.login-btn {
  appearance: none; cursor: pointer; font: inherit; font-size: 14px; font-weight: 600;
  padding: 10px 14px; border-radius: var(--radius-sm); border: 1px solid transparent;
  background: var(--series-1); color: #fff; width: 100%;
}
.login-btn:hover { filter: brightness(1.06); }
.login-btn:active { transform: translateY(1px); }
.login-btn[disabled] { opacity: .6; cursor: progress; }

.login-error {
  margin: 0; font-size: 12.5px; color: var(--critical);
  min-height: 0; opacity: 0; transition: opacity .15s;
  padding: 8px 10px; border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--critical) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--critical) 30%, transparent);
}
.login-error.is-on { opacity: 1; }
.login-error:not(.is-on) { padding: 0; border-color: transparent; background: none; }

.login-foot {
  margin: 0; font-size: 11px; line-height: 1.5; color: var(--text-muted);
  border-top: 1px solid var(--border); padding-top: 13px;
}

/* ── forms (site creation) ───────────────────────────────────────────────── */

.form-grid {
  display: grid; gap: 14px;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  align-items: start;
}
.field { display: flex; flex-direction: column; gap: 5px; min-width: 0; }
.field > span {
  font-size: 11px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .06em; color: var(--text-secondary);
}
.field small { font-size: 11px; color: var(--text-muted); line-height: 1.45; }
.field select.input { cursor: pointer; }

.form-actions {
  grid-column: 1 / -1;
  display: flex; align-items: center; gap: 14px; flex-wrap: wrap;
  border-top: 1px solid var(--border); padding-top: 13px;
}
.form-actions .btn { margin-left: auto; }

.btn-primary {
  background: var(--series-1); color: #fff; border-color: transparent; font-weight: 600;
}
.btn-primary:hover { filter: brightness(1.07); background: var(--series-1); }

.form-error {
  margin: 12px 0 0; font-size: 12.5px; color: var(--critical);
  padding: 9px 11px; border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--critical) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--critical) 30%, transparent);
  display: none;
}
.form-error.is-on { display: block; }
.form-error.is-ok {
  display: block; color: var(--good-text);
  background: color-mix(in srgb, var(--good) 12%, transparent);
  border-color: color-mix(in srgb, var(--good) 30%, transparent);
}

.note-box {
  margin: 14px 0 0; font-size: 12px; line-height: 1.55;
  color: var(--text-secondary);
  padding: 11px 13px; border-radius: var(--radius-sm);
  background: var(--surface-2); border: 1px solid var(--border);
  border-left: 3px solid var(--warning);
}
.note-box strong { color: var(--text-primary); }

/* ── backup scope picker ─────────────────────────────────────────────────── */

.scope-list { display: grid; gap: 10px; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); }

.scope {
  display: flex; flex-direction: column; gap: 7px;
  padding: 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface-2);
}
.scope h3 { font-size: 13px; }
.scope p { margin: 0; font-size: 11.5px; color: var(--text-muted); line-height: 1.5; flex: 1; }
.scope .btn { align-self: flex-start; }

.backup-run { margin-top: 16px; display: flex; flex-direction: column; gap: 7px; }
.meter.indeterminate { position: relative; overflow: hidden; }
.meter.indeterminate .meter-fill {
  background: linear-gradient(90deg,
    color-mix(in srgb, var(--series-1) 25%, transparent) 0%,
    var(--series-1) 50%,
    color-mix(in srgb, var(--series-1) 25%, transparent) 100%);
  background-size: 200% 100%;
  animation: sweep 1.1s linear infinite;
}
@keyframes sweep { from { background-position: 200% 0; } to { background-position: -200% 0; } }

.site-domains { display: flex; flex-wrap: wrap; gap: 4px; }
.domain-chip {
  font-size: 11px; padding: 1px 7px; border-radius: 999px;
  background: color-mix(in srgb, var(--series-1) 12%, transparent);
  color: var(--series-1); border: 1px solid color-mix(in srgb, var(--series-1) 30%, transparent);
  font-family: var(--mono);
}

/* The sign-out control is an <a> so it works without JavaScript; these
   rules make an anchor sit correctly in a button-shaped box. */
.btn-logout {
  display: inline-flex; align-items: center; justify-content: center;
  text-decoration: none;
}
.btn-logout:hover { color: var(--critical); }
