/* tsctl SPA — Apple HIG (macOS/iOS Settings) inspired design.
   Pure CSS, system fonts, inline SVG only. No build step, no external assets.
   Hierarchy comes from type weight + spacing, not heavy borders. */

:root {
  color-scheme: light dark;

  /* ---- light theme (default) ---- */
  --bg: #f2f2f7;            /* iOS grouped background */
  --card: #ffffff;          /* grouped card surface */
  --card-nested: #f2f2f7;
  --fill: rgba(120, 120, 128, 0.12);       /* control fill */
  --fill-strong: rgba(120, 120, 128, 0.2);

  --text: #1c1c1e;
  --text-secondary: rgba(60, 60, 67, 0.6);
  --text-tertiary: rgba(60, 60, 67, 0.3);
  --separator: rgba(60, 60, 67, 0.16);     /* hairline */

  --accent: #007aff;
  --accent-tint: rgba(0, 122, 255, 0.12);
  --c-accent: #0067d6;     /* readable accent text */

  --green: #34c759;
  --green-tint: rgba(52, 199, 89, 0.16);
  --c-green: #248a3d;      /* readable on light tint */

  --amber: #ff9500;
  --amber-tint: rgba(255, 149, 0, 0.18);
  --c-amber: #8a5300;

  --red: #ff3b30;
  --red-tint: rgba(255, 59, 48, 0.14);
  --c-red: #c70d12;

  --indigo: #5856d6;
  --indigo-tint: rgba(88, 86, 214, 0.14);
  --c-indigo: #413fb0;

  --shadow: 0 1px 2px rgba(0, 0, 0, 0.05), 0 0 1px rgba(0, 0, 0, 0.04);
  --shadow-pop: 0 12px 40px rgba(0, 0, 0, 0.18), 0 2px 8px rgba(0, 0, 0, 0.1);
  --topbar-bg: rgba(249, 249, 251, 0.72);
  --scrim: rgba(0, 0, 0, 0.32);

  --radius: 12px;
  --radius-lg: 16px;
  --radius-pill: 999px;
  --col: 44rem;

  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue",
    system-ui, sans-serif;
  --t: 220ms ease-out;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #000000;
    --card: #1c1c1e;
    --card-nested: #2c2c2e;
    --fill: rgba(120, 120, 128, 0.24);
    --fill-strong: rgba(120, 120, 128, 0.4);

    --text: #ffffff;
    --text-secondary: rgba(235, 235, 245, 0.6);
    --text-tertiary: rgba(235, 235, 245, 0.3);
    --separator: rgba(84, 84, 88, 0.6);

    --accent: #0a84ff;
    --accent-tint: rgba(10, 132, 255, 0.24);
    --c-accent: #4da3ff;

    --green: #30d158;
    --green-tint: rgba(48, 209, 88, 0.22);
    --c-green: #41d367;

    --amber: #ff9f0a;
    --amber-tint: rgba(255, 159, 10, 0.22);
    --c-amber: #ffb340;

    --red: #ff453b;
    --red-tint: rgba(255, 69, 58, 0.22);
    --c-red: #ff6961;

    --indigo: #5e5ce6;
    --indigo-tint: rgba(94, 92, 230, 0.26);
    --c-indigo: #9b99ff;

    --shadow: none;
    --shadow-pop: 0 12px 40px rgba(0, 0, 0, 0.6);
    --topbar-bg: rgba(22, 22, 24, 0.72);
    --scrim: rgba(0, 0, 0, 0.6);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  font-family: var(--font);
  margin: 0;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

/* ------------------------------------------------------------ top bar --- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: var(--topbar-bg);
  -webkit-backdrop-filter: saturate(180%) blur(20px);
  backdrop-filter: saturate(180%) blur(20px);
  border-bottom: 1px solid var(--separator);
}
.topbar-inner {
  max-width: var(--col);
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  min-height: 52px;
  padding: 0.5rem max(1rem, env(safe-area-inset-right))
    0.5rem max(1rem, env(safe-area-inset-left));
}
.brand { min-width: 0; }
.topbar-title {
  margin: 0;
  font-size: 1.125rem;
  font-weight: 700;
  letter-spacing: -0.01em;
  line-height: 1.1;
}
.topbar-subtitle {
  margin: 0.05rem 0 0;
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.topbar-right {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex: none;
}
.updated {
  font-size: 0.75rem;
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

/* connection pill */
.conn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 0.25rem 0.6rem;
  border-radius: var(--radius-pill);
  transition: color var(--t), background var(--t);
  white-space: nowrap;
}
.conn-live { color: var(--c-green); background: var(--green-tint); }
.conn-wait { color: var(--c-amber); background: var(--amber-tint); }
.conn-off { color: var(--c-red); background: var(--red-tint); }
.conn .dot,
.conn .spinner { width: 0.7rem; height: 0.7rem; }

/* round icon button (help) */
.icon-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--fill);
  color: var(--text-secondary);
  font: inherit;
  font-weight: 700;
  font-size: 0.95rem;
  cursor: pointer;
  transition: background var(--t), color var(--t);
}
.icon-btn:hover { background: var(--fill-strong); color: var(--text); }
.icon-btn[aria-expanded="true"] { background: var(--accent-tint); color: var(--c-accent); }

/* text button (e.g. Sign out) */
.text-btn {
  flex: none;
  display: inline-flex;
  align-items: center;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--fill);
  color: var(--c-accent);
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 0.3rem 0.7rem;
  min-height: 30px;
  cursor: pointer;
  white-space: nowrap;
  transition: background var(--t), color var(--t);
}
.text-btn:hover { background: var(--fill-strong); }

/* ------------------------------------------------------------ layout ---- */
.container {
  max-width: var(--col);
  margin: 0 auto;
  padding: 1.25rem max(1rem, env(safe-area-inset-right))
    3rem max(1rem, env(safe-area-inset-left));
}
.section { margin-top: 1.75rem; }
.section[hidden] { display: none; }
.section:first-of-type { margin-top: 0.5rem; }
.section-head {
  display: flex;
  align-items: baseline;
  gap: 0.5rem;
  margin: 0 0 0.5rem;
  padding: 0 1rem;
}
.section-title {
  margin: 0 0 0.5rem;
  padding: 0 1rem;
  font-size: 0.8125rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-secondary);
}
.section-head .section-title { margin: 0; padding: 0; }
.section-count {
  font-size: 0.8125rem;
  color: var(--text-tertiary);
  font-variant-numeric: tabular-nums;
}

/* ----------------------------------------------------------- legend ----- */
.legend {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 0.9rem 1rem;
  margin-bottom: 0.75rem;
}
.legend-title {
  margin: 0 0 0.6rem;
  font-size: 0.9375rem;
  font-weight: 600;
}
.legend-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 0.55rem; }
.legend-list li {
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  font-size: 0.875rem;
  color: var(--text-secondary);
  line-height: 1.35;
}
.legend-list strong { color: var(--text); font-weight: 600; }
.legend-icon {
  flex: none;
  width: 5.2rem;
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
}
.legend-dot { display: inline-block; width: 0.62rem; height: 0.62rem; border-radius: 50%; background: var(--green); }

/* ----------------------------------------------------------- banners ---- */
#banners:not(:empty) { margin-bottom: 0.5rem; }
.banner {
  padding: 0.7rem 0.9rem;
  border-radius: var(--radius);
  margin-bottom: 0.5rem;
  font-size: 0.875rem;
  line-height: 1.35;
  white-space: pre-wrap;
  word-break: break-word;
}
.banner-error { background: var(--red-tint); color: var(--c-red); }
.banner-warn { background: var(--amber-tint); color: var(--c-amber); }

/* ----------------------------------------------------------- loading ---- */
.loading { display: grid; gap: 0.85rem; }
.loading[hidden], .loading.hidden { display: none; }
.skeleton {
  border-radius: var(--radius-lg);
  background: linear-gradient(100deg, var(--fill) 30%, var(--fill-strong) 50%, var(--fill) 70%);
  background-size: 200% 100%;
  animation: shimmer 1.4s linear infinite;
}
.skeleton-section { height: 1rem; width: 6rem; margin: 0 0 0.25rem 1rem; border-radius: var(--radius); }
.skeleton-card { height: 5.5rem; box-shadow: var(--shadow); }
@keyframes shimmer { to { background-position: -200% 0; } }

/* ----------------------------------------------- device search/filter --- */
.tools {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0 0 0.6rem;
  align-items: center;
}
.search-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  flex: 1 1 14rem;
  min-width: 11rem;
}
.search-icon {
  position: absolute;
  left: 0.7rem;
  color: var(--text-tertiary);
  pointer-events: none;
}
.search-input {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--fill);
  border: none;
  border-radius: 10px;
  width: 100%;
  min-height: 38px;
  padding: 0 0.85rem 0 2.1rem;
  -webkit-appearance: none;
  appearance: none;
  transition: background var(--t);
}
.search-input::placeholder { color: var(--text-tertiary); }
.search-input:hover { background: var(--fill-strong); }
.chips { display: inline-flex; flex-wrap: wrap; gap: 0.35rem; }
.chip {
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  padding: 0.3rem 0.7rem;
  min-height: 32px;
  border: none;
  border-radius: var(--radius-pill);
  background: var(--fill);
  color: var(--text-secondary);
  cursor: pointer;
  transition: background var(--t), color var(--t);
}
.chip:hover { background: var(--fill-strong); }
.chip[aria-pressed="true"] { background: var(--accent); color: #fff; }

/* ----------------------------------------------- grouped list (nodes) --- */
.list-group {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  overflow: hidden;
}
.list-group:empty { display: none; }

.node {
  position: relative;
  padding: 0.75rem 1rem;
}
.node.is-filtered { display: none; }
.router.is-filtered { display: none; }
.list-group > .node:not(.is-filtered) ~ .node:not(.is-filtered)::before {
  content: "";
  position: absolute;
  left: 1rem;
  right: 0;
  top: 0;
  height: 1px;
  background: var(--separator);
}
.node-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.node-head .dot { order: 0; }
.node-name {
  order: 1;
  font-weight: 600;
  font-size: 1rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.node-head .badge { order: 2; margin-left: auto; flex: none; }

.node-ips {
  margin-top: 0.2rem;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.node-meta {
  margin-top: 0.1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem;
  color: var(--text-secondary);
  font-size: 0.8125rem;
}

/* ----------------------------------------------- router cards ----------- */
.router-list { display: flex; flex-direction: column; gap: 0.85rem; }
.router {
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 0.9rem 1rem 1rem;
}
.router-head {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  min-width: 0;
}
.router-name {
  font-weight: 600;
  font-size: 1rem;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.router-head .state { margin-left: auto; flex: none; }

.router-ips {
  margin-top: 0.25rem;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.current {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.6rem;
  font-size: 0.9375rem;
  min-width: 0;
}
.current-text { font-variant-numeric: tabular-nums; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.current.is-stale .current-text { color: var(--text-tertiary); }
.arrow-icon { flex: none; color: var(--text-secondary); }

/* inline warn line (e.g. current exit node is offline) */
.router-warn {
  display: flex;
  align-items: flex-start;
  gap: 0.4rem;
  margin-top: 0.55rem;
  padding: 0.5rem 0.65rem;
  border-radius: var(--radius);
  background: var(--amber-tint);
  color: var(--c-amber);
  font-size: 0.8125rem;
  line-height: 1.35;
}
.router-warn .warn-icon { flex: none; margin-top: 0.05rem; }

/* offline note */
.offline-note {
  margin-top: 0.55rem;
  font-size: 0.8125rem;
  color: var(--text-secondary);
}

/* internet-egress indicator (stage 1, docs/design/keep-egress.md): shown on the
   card only when the backend probed egress after a confirmed set. ✓ green = the
   router reaches the internet through its exit node; ✗ amber = it can't. */
.egress-line {
  margin-top: 0.5rem;
  font-size: 0.8125rem;
  line-height: 1.4;
  min-width: 0;
}
.egress-line.hidden { display: none; }
.egress-status { font-weight: 600; }
.egress-ok .egress-status { color: var(--c-green); }
.egress-fail .egress-status { color: var(--c-amber); }
.egress-detail {
  margin-top: 0.25rem;
  color: var(--text-secondary);
  word-break: break-word;
  white-space: pre-wrap;
  max-height: 7rem;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}
.egress-when { margin-top: 0.25rem; color: var(--text-tertiary); font-size: 0.75rem; }

/* explicit-Keep gate (stage 2, docs/design/keep-egress.md): shown only while the
   router is "awaiting-keep" (a confirmed-but-not-kept selection in the revert
   window). Amber/attention block with a live countdown + a prominent Keep button —
   explicitly NOT a success treatment; not keeping in time auto-reverts. */
.keep-gate {
  margin-top: 0.6rem;
  padding: 0.7rem 0.8rem;
  border-radius: var(--radius);
  background: var(--amber-tint);
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}
.keep-gate.hidden { display: none; }
.keep-note {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--c-amber);
  line-height: 1.35;
  font-variant-numeric: tabular-nums;   /* steady width as the countdown ticks */
}
.keep-btn {
  font: inherit;
  font-size: 0.9375rem;
  font-weight: 600;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  align-self: flex-start;
  min-height: 38px;
  padding: 0 1.4rem;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--t), opacity var(--t);
}
.keep-btn:hover { opacity: 0.9; }
.keep-btn:disabled { opacity: 0.55; cursor: not-allowed; }
.keep-btn .spinner { width: 0.85rem; height: 0.85rem; }
.keep-err { font-size: 0.8125rem; color: var(--c-red); line-height: 1.35; word-break: break-word; }
.keep-err.hidden { display: none; }

/* ----------------------------------------------- dots & icons ---------- */
.dot {
  display: inline-block;
  flex: none;
  width: 0.62rem;
  height: 0.62rem;
}
.dot circle { fill: currentColor; }
.dot-on { color: var(--green); }
.dot-off { color: var(--text-tertiary); }

.spinner {
  display: inline-block;
  flex: none;
  width: 0.9rem;
  height: 0.9rem;
  color: currentColor;
  animation: spin 0.8s linear infinite;
  transform-origin: center;
}

/* ----------------------------------------------- badges & state -------- */
.badge,
.state {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 0.15rem 0.5rem;
  border-radius: var(--radius-pill);
  white-space: nowrap;
}
.badge { background: var(--fill); color: var(--text-secondary); }
.badge-exit-node { background: var(--accent-tint); color: var(--c-accent); }
.badge-router { background: var(--indigo-tint); color: var(--c-indigo); }
.badge-generic { background: var(--fill); color: var(--text-secondary); }

.state-ok { background: var(--green-tint); color: var(--c-green); }
.state-pending { background: var(--amber-tint); color: var(--c-amber); }
.state-unconfirmed { background: var(--amber-tint); color: var(--c-amber); }
/* awaiting-keep (stage 2): a CONFIRMED-but-not-yet-kept selection inside the revert
   window. Amber = attention/decision-needed — explicitly NOT the green success state. */
.state-awaiting-keep { background: var(--amber-tint); color: var(--c-amber); }
.state-unreachable { background: var(--red-tint); color: var(--c-red); }
.state-unknown { background: var(--fill); color: var(--text-secondary); }
/* Online in the netmap but tsctl can't control it: amber (a problem that needs
   attention), distinct from red genuine-offline/unreachable. */
.state-control-error { background: var(--amber-tint); color: var(--c-amber); }
/* Listed by the fallback but never auto-probed: neutral grey — NOT a problem,
   just unknown until a manual Test SSH. */
.state-unprobed { background: var(--fill); color: var(--text-secondary); }

/* ----------------------------------------------- picker / select ------- */
.picker { margin-top: 0.85rem; }
.picker-label {
  display: block;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text);
  margin-bottom: 0.35rem;
}
.select-wrap {
  position: relative;
  display: flex;
  align-items: center;
  width: 100%;
}
.exit-select {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--fill);
  border: none;
  border-radius: 10px;
  width: 100%;
  min-height: 44px;            /* 44px hit target */
  padding: 0 2.25rem 0 0.85rem;
  margin: 0;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
  text-overflow: ellipsis;
  transition: background var(--t);
}
.exit-select:hover { background: var(--fill-strong); }
.exit-select:disabled {
  color: var(--text-secondary);
  opacity: 0.65;
  cursor: not-allowed;
}
.select-chevron {
  position: absolute;
  right: 0.7rem;
  color: var(--text-secondary);
  pointer-events: none;
}
.exit-select:disabled + .select-chevron { opacity: 0.5; }

/* ----------------------------------------------- pending indicator ----- */
.pending {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.5rem;
  font-size: 0.8125rem;
  width: 100%;
}
.pending-spin { color: var(--c-amber); }
.pending-amber { color: var(--c-amber); }

.picker-hint {
  margin-top: 0.5rem;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  line-height: 1.35;
}

/* ----------------------------------------------- stats & errors -------- */
.stats {
  margin-top: 0.7rem;
  color: var(--text-secondary);
  font-size: 0.8125rem;
  font-variant-numeric: tabular-nums;
}
.error-box {
  margin-top: 0.7rem;
  padding: 0.6rem 0.75rem;
  border-radius: var(--radius);
  background: var(--red-tint);
  color: var(--c-red);
  font-size: 0.8125rem;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ----------------------------------------------- SSH probe ------------- */
.probe { margin-top: 0.7rem; }
.probe-btn {
  font: inherit;
  font-size: 0.8125rem;
  font-weight: 500;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--c-accent);
  background: var(--fill);
  border: none;
  border-radius: var(--radius-pill);
  padding: 0.3rem 0.85rem;
  min-height: 32px;
  cursor: pointer;
  transition: background var(--t), opacity var(--t);
}
.probe-btn:hover { background: var(--fill-strong); }
.probe-btn:disabled { opacity: 0.7; cursor: progress; }
.probe-btn .spinner { width: 0.85rem; height: 0.85rem; }

.probe-result {
  margin-top: 0.55rem;
  padding: 0.55rem 0.7rem;
  border-radius: var(--radius);
  font-size: 0.8125rem;
  line-height: 1.4;
}
.probe-result.hidden { display: none; }
.probe-result.ok { background: var(--green-tint); color: var(--c-green); }
.probe-result.is-fail { background: var(--red-tint); color: var(--c-red); }
.probe-status { font-weight: 600; }
.probe-detail { margin-top: 0.3rem; word-break: break-word; white-space: pre-wrap; }
.probe-output {
  margin: 0.45rem 0 0;
  padding: 0.5rem 0.6rem;
  max-height: 9rem;
  overflow: auto;
  background: var(--card-nested);
  color: var(--text);
  border-radius: 8px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.75rem;
  line-height: 1.45;
  white-space: pre;
  -webkit-overflow-scrolling: touch;
}
.probe-when { margin-top: 0.35rem; font-size: 0.75rem; opacity: 0.8; }

/* ----------------------------------------------- empty / footer -------- */
.empty {
  color: var(--text-secondary);
  font-size: 0.9375rem;
  text-align: center;
  padding: 1.5rem 1rem;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
}
.empty strong { color: var(--text); font-weight: 600; }
.empty code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: var(--fill);
  padding: 0.05rem 0.35rem;
  border-radius: 6px;
}

.hidden { display: none !important; }

/* ----------------------------------------------- login overlay --------- */
.login-overlay {
  position: fixed;
  inset: 0;
  z-index: 55;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: var(--bg);
}
.login-overlay[hidden] { display: none; }
.login-card {
  width: 100%;
  max-width: 22rem;
  text-align: center;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 2rem 1.5rem 1.5rem;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}
.login-icon { color: var(--accent); align-self: center; }
.login-card h2 { margin: 0.75rem 0 0.4rem; font-size: 1.25rem; }
.login-desc {
  margin: 0 0 1.1rem;
  color: var(--text-secondary);
  font-size: 0.9375rem;
  line-height: 1.45;
}
.login-label {
  text-align: left;
  font-size: 0.8125rem;
  font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 0.3rem;
}
.login-input {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--fill);
  border: none;
  border-radius: 10px;
  width: 100%;
  min-height: 44px;
  padding: 0 0.85rem;
  -webkit-appearance: none;
  appearance: none;
  transition: background var(--t);
}
.login-input:hover { background: var(--fill-strong); }
.login-input:disabled { opacity: 0.6; cursor: not-allowed; }
.login-error {
  min-height: 1.1rem;
  margin: 0.5rem 0 0;
  text-align: left;
  color: var(--c-red);
  font-size: 0.8125rem;
  line-height: 1.35;
}
.login-error:empty { margin: 0; min-height: 0; }
.login-submit { width: 100%; margin-top: 0.9rem; justify-content: center; display: inline-flex; align-items: center; }

/* ----------------------------------------------- not-authorized view --- */
.auth-error {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  background: var(--bg);
}
.auth-error[hidden] { display: none; }
.auth-error-card {
  max-width: 26rem;
  text-align: center;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow);
  padding: 2rem 1.5rem;
}
.auth-error-icon { color: var(--red); }
.auth-error-card h2 { margin: 0.75rem 0 0.5rem; font-size: 1.25rem; }
.auth-error-card p { margin: 0; color: var(--text-secondary); font-size: 0.9375rem; line-height: 1.45; }
.auth-error-card code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85em;
  background: var(--fill);
  padding: 0.05rem 0.35rem;
  border-radius: 6px;
}

/* ----------------------------------------------- confirm modal --------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  background: var(--scrim);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
  animation: fade-in 160ms ease-out;
}
.modal {
  width: 100%;
  max-width: 24rem;
  background: var(--card);
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-pop);
  padding: 1.25rem;
  animation: pop-in 200ms ease-out;
}
.modal-title { margin: 0 0 0.5rem; font-size: 1.0625rem; font-weight: 600; }
.modal-body { margin: 0 0 1.1rem; font-size: 0.9375rem; color: var(--text-secondary); line-height: 1.45; }
.modal-body strong { color: var(--text); font-weight: 600; }
.modal-note { margin: 0.6rem 0 0; font-size: 0.8125rem; color: var(--text-tertiary); }
.modal-actions { display: flex; gap: 0.6rem; justify-content: flex-end; }
.btn {
  font: inherit;
  font-size: 1rem;
  font-weight: 500;
  min-height: 44px;
  padding: 0 1.1rem;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  transition: background var(--t), opacity var(--t);
}
.btn-secondary { background: var(--fill); color: var(--text); }
.btn-secondary:hover { background: var(--fill-strong); }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { opacity: 0.9; }

/* ----------------------------------------------- toasts ---------------- */
.toasts {
  position: fixed;
  left: 50%;
  bottom: max(1rem, env(safe-area-inset-bottom));
  transform: translateX(-50%);
  z-index: 70;
  display: flex;
  flex-direction: column-reverse;
  align-items: center;
  gap: 0.5rem;
  width: min(28rem, calc(100vw - 2rem));
  pointer-events: none;
}
.toast {
  pointer-events: auto;
  width: 100%;
  display: flex;
  align-items: flex-start;
  gap: 0.6rem;
  padding: 0.7rem 0.85rem;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: var(--shadow-pop);
  font-size: 0.875rem;
  line-height: 1.35;
  animation: toast-in 200ms ease-out;
}
.toast.toast-out { animation: toast-out 200ms ease-in forwards; }
.toast-icon { flex: none; margin-top: 0.05rem; }
.toast-ok .toast-icon { color: var(--green); }
.toast-err .toast-icon { color: var(--red); }
.toast-warn .toast-icon { color: var(--amber); }
.toast-body { flex: 1 1 auto; min-width: 0; word-break: break-word; white-space: pre-wrap; }
.toast-title { font-weight: 600; }
.toast-close {
  flex: none;
  border: none;
  background: transparent;
  color: var(--text-tertiary);
  font-size: 1.1rem;
  line-height: 1;
  cursor: pointer;
  padding: 0 0.15rem;
}
.toast-close:hover { color: var(--text); }

/* ----------------------------------------------- focus & motion -------- */
:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 8px;
}
.exit-select:focus-visible { outline-offset: 1px; }
/* Pill-shaped controls keep their rounded outline on focus — the generic 8px
   radius above would otherwise square off their corners while focused. */
.chip:focus-visible,
.seg:focus-visible,
.zone-tab:focus-visible,
.text-btn:focus-visible,
.icon-btn:focus-visible,
.probe-btn:focus-visible { border-radius: var(--radius-pill); }

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
@keyframes pop-in { from { opacity: 0; transform: scale(0.96); } to { opacity: 1; transform: scale(1); } }
@keyframes toast-in { from { opacity: 0; transform: translateY(0.5rem); } to { opacity: 1; transform: translateY(0); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(0.5rem); } }

/* ========================================== zone graph (default view) === */
/* segmented view switch (Zones | Devices) */
.viewbar { display: flex; justify-content: center; margin: 0 0 1.25rem; }
.viewbar.hidden, .viewbar[hidden] { display: none; }
.segmented {
  display: inline-flex;
  background: var(--fill);
  border-radius: var(--radius-pill);
  padding: 2px;
  gap: 2px;
}
.seg {
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-secondary);
  background: transparent;
  border: none;
  border-radius: var(--radius-pill);
  padding: 0.35rem 1.1rem;
  min-height: 32px;
  cursor: pointer;
  transition: color var(--t), background var(--t);
}
.seg.is-selected { background: var(--card); color: var(--text); box-shadow: var(--shadow); }

/* zone selector + actions */
.zone-bar {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  margin: 0 0 0.9rem;
  flex-wrap: wrap;
}
.zone-tabs {
  display: flex;
  gap: 0.3rem;
  overflow-x: auto;
  flex: 1 1 12rem;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.zone-tabs::-webkit-scrollbar { display: none; }
.zone-tab {
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  color: var(--text-secondary);
  background: var(--fill);
  border: none;
  border-radius: var(--radius-pill);
  padding: 0.35rem 0.9rem;
  min-height: 34px;
  cursor: pointer;
  transition: background var(--t), color var(--t);
}
.zone-tab:hover { background: var(--fill-strong); }
.zone-tab.is-selected { background: var(--accent); color: #fff; }
.zone-actions { display: inline-flex; gap: 0.4rem; flex: none; }
.zone-delete { color: var(--c-red); }

/* bipartite grid: two columns + an absolutely-positioned SVG wire overlay */
.graph-grid {
  position: relative;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4rem; /* the wires live in this gap -- give them room so curves aren't cramped */
  align-items: start;
}
.graph-grid[hidden] { display: none; }
.gcol { min-width: 0; }
.gcol-title {
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  margin: 0 0 0.5rem;
  padding: 0 0.25rem;
}
.gcol-body { display: flex; flex-direction: column; gap: 0.55rem; }
.gcol-empty { color: var(--text-secondary); font-size: 0.8125rem; padding: 0.5rem 0.25rem; }
.gcol-empty.hidden { display: none; }
#graph-empty { margin-top: 0.5rem; }

.wires {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;     /* events pass through to the column nodes */
  overflow: visible;
  z-index: 2;
}
.wire { stroke-width: 2.5; fill: none; stroke-linecap: round; }
.wire-ok { stroke: var(--green); }
.wire-unconfirmed { stroke: var(--amber); }
.wire-off { stroke: var(--text-tertiary); stroke-dasharray: 2 5; }
.wire-pending {
  stroke: var(--amber);
  stroke-dasharray: 7 5;
  animation: wire-march 0.7s linear infinite; /* disabled by reduced-motion below */
}
.wire-ghost { stroke: var(--accent); stroke-dasharray: 7 5; opacity: 0.85; }
@keyframes wire-march { to { stroke-dashoffset: -24; } }

/* graph node cards */
.gnode {
  position: relative;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 0.6rem 0.75rem;
  min-width: 0;
}
/* Touch input scrolls the page natively — drag-to-rewire is a mouse/pen
   affordance only (see onConsumerPointerDown), so don't trap the touch gesture. */
.gnode-consumer { touch-action: auto; }
.gnode-consumer.is-actionable { cursor: grab; }
.gnode-consumer.is-actionable:hover { background: var(--fill); }
.gnode.is-dragging { cursor: grabbing; box-shadow: var(--shadow-pop); opacity: 0.92; }
.gnode.is-missing { opacity: 0.55; }
.gnode.is-off .gnode-name { color: var(--text-secondary); }
/* Control error: device online but uncontrollable — amber edge, NOT greyed-out
   like genuine-offline (the device is up; the control path is the problem). */
.gnode.is-control-error { border: 1px solid var(--amber); }
.gnode.is-control-error .gnode-name { color: var(--text); }
/* Unconfirmed: a set that never confirmed and stuck (survives polls + reload). Amber
   edge like control-error, but the card stays ACTIONABLE — the user retries / switches
   / clears to escape (the only exit; there is no armed timer to clear it). */
.gnode.is-unconfirmed { border: 1px solid var(--amber); }
.gnode.is-unconfirmed .gnode-name { color: var(--text); }
/* Unprobed (fallback-listed, not yet contacted): dashed edge = "unknown, not a
   failure". The device is interactive (set on demand) so the name stays readable. */
.gnode.is-unprobed { border: 1px dashed var(--separator); }
.gnode.is-unprobed .gnode-name { color: var(--text); }
.gnode.is-drop-hover { outline: 2px solid var(--accent); outline-offset: 2px; background: var(--accent-tint); }
.gnode-head { display: flex; align-items: center; gap: 0.45rem; min-width: 0; }
.gnode-name {
  font-weight: 600;
  font-size: 0.9375rem;
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.gnode-grip { flex: none; color: var(--text-tertiary); font-size: 0.8rem; letter-spacing: -2px; }
.gnode-sub {
  margin-top: 0.2rem;
  font-size: 0.8125rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.gnode-state {
  display: inline-block;
  margin-top: 0.35rem;
  font-size: 0.625rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.02em;
  padding: 0.1rem 0.45rem;
  border-radius: var(--radius-pill);
}
/* Graph Keep button (awaiting-keep): a compact accent pill beside the state badge.
   Shown only while the Keep action is live; hidden (never greyed) otherwise. */
.gnode-keep {
  display: inline-block;
  margin-top: 0.35rem;
  margin-left: 0.4rem;
  vertical-align: middle;
  font: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  line-height: 1;
  padding: 0.28rem 0.7rem;
  color: #fff;
  background: var(--accent);
  border: none;
  border-radius: var(--radius-pill);
  cursor: pointer;
  transition: opacity var(--t);
}
.gnode-keep:hover { opacity: 0.9; }
.gnode-keep.hidden { display: none; }
.gnode-direct {
  border: 1px dashed var(--separator);
  box-shadow: none;
  background: transparent;
}
.gnode-direct .gnode-name { color: var(--text-secondary); }
.gnode.is-outofzone { border: 1px dashed var(--amber); }

.graph-hint { margin: 1rem 0 0; font-size: 0.8125rem; color: var(--text-tertiary); text-align: center; }
.graph-hint[hidden] { display: none; }
/* The hint swaps copy by input model: drag instructions on desktop, tap on touch. */
.graph-hint .hint-mobile { display: none; }

body.graph-dragging { cursor: grabbing; user-select: none; -webkit-user-select: none; }

/* keyboard/click rewire menu (accessible alternative to drag) */
.zone-menu {
  position: fixed;
  z-index: 65;
  min-width: 13rem;
  max-width: min(20rem, calc(100vw - 1.5rem));
  max-height: 60vh;
  overflow-y: auto;
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-pop);
  padding: 0.3rem;
  display: flex;
  flex-direction: column;
}
/* Picker header — hidden for the desktop popover (anchored to the card), shown
   when the picker becomes a bottom sheet on touch/narrow screens. */
.zone-menu-title {
  display: none;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-secondary);
  padding: 0.55rem 0.65rem 0.4rem;
}
.zone-menu-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
  width: 100%;
  font: inherit;
  font-size: 0.9375rem;
  text-align: left;
  color: var(--text);
  background: transparent;
  border: none;
  border-radius: 8px;
  padding: 0.55rem 0.6rem;
  min-height: 40px;
  cursor: pointer;
}
.zone-menu-item:hover, .zone-menu-item:focus-visible { background: var(--fill); }
.zone-menu-item:disabled { color: var(--text-tertiary); cursor: not-allowed; }
.zone-menu-item.is-current { color: var(--c-accent); font-weight: 600; }
.zone-menu-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.zone-menu-check { flex: none; color: var(--c-accent); }

/* zone editor modal */
.editor { max-width: 30rem; }
.editor-body { max-height: 70vh; overflow-y: auto; color: var(--text); }
.editor-field { margin-bottom: 1rem; }
.editor-label {
  display: block;
  font-size: 0.8125rem;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.03em;
  margin-bottom: 0.4rem;
}
.editor-input {
  font: inherit;
  font-size: 1rem;
  color: var(--text);
  background: var(--fill);
  border: none;
  border-radius: 10px;
  width: 100%;
  min-height: 44px;
  padding: 0 0.85rem;
  -webkit-appearance: none;
  appearance: none;
  transition: background var(--t);
}
.editor-input:hover { background: var(--fill-strong); }
.member-list {
  display: flex;
  flex-direction: column;
  background: var(--card-nested);
  border-radius: var(--radius);
  overflow: hidden;
  max-height: 12rem;
  overflow-y: auto;
}
.member-item { display: flex; align-items: center; gap: 0.6rem; padding: 0.5rem 0.7rem; cursor: pointer; }
.member-item + .member-item { border-top: 1px solid var(--separator); }
.member-item.is-missing { opacity: 0.6; }
.member-cb { width: 1.1rem; height: 1.1rem; flex: none; accent-color: var(--accent); }
.member-text { display: flex; flex-direction: column; min-width: 0; }
.member-name { font-size: 0.9375rem; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.member-sub { font-size: 0.75rem; color: var(--text-secondary); font-variant-numeric: tabular-nums; }
.member-empty { color: var(--text-secondary); font-size: 0.875rem; padding: 0.6rem 0.7rem; margin: 0; }
/* Guests panel rows reuse .member-item but, unlike the zone-editor checkbox list,
   carry trailing actions (Disable/Delete) and aren't row-clickable. Let the text grow
   so the actions hug the right edge, drop the misleading pointer cursor, give the row
   a touch more breathing room, and separate the list from the create form above. */
#guests-list { margin-top: 0.85rem; }
#guests-list .member-item { cursor: default; padding: 0.6rem 0.85rem; gap: 0.85rem; }
#guests-list .member-text { flex: 1 1 auto; }
.editor-error { color: var(--c-red); font-size: 0.8125rem; margin-top: 0.25rem; line-height: 1.35; }
.editor-error.hidden { display: none; }
.editor-actions { align-items: center; }
.editor-spacer { flex: 1 1 auto; }
.btn-danger { background: var(--red-tint); color: var(--c-red); }
.btn-danger:hover { opacity: 0.85; }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

/* Touch / narrow: a tap-to-select model. Drag-to-rewire is off (the page must
   scroll), so the exit-node column — a pure drag target — is hidden and each
   router becomes a tappable row. Wires hide; the picker opens as a bottom sheet.
   `(pointer: coarse)` is the PRIMARY pointer, so a mouse-driven desktop with a
   touchscreen keeps the full two-column drag UI. */
@media (max-width: 34rem), (pointer: coarse) {
  .graph-grid { grid-template-columns: 1fr; gap: 0; }
  .wires { display: none; }
  .gcol-exits { display: none; }
  .gcol-title { display: none; }       /* the lone router list needs no column header */
  .gnode-grip { display: none; }       /* drag is off on touch — no drag handle */
  /* A router row reads as a tappable disclosure: pointer cursor + a chevron, with
     room reserved on the right so long names ellipsize before it. */
  .gnode-consumer.is-actionable { cursor: pointer; padding-right: 2rem; }
  .gnode-consumer.is-actionable::after {
    content: "›";
    position: absolute;
    right: 0.9rem;
    top: 50%;
    transform: translateY(-50%);
    color: var(--text-tertiary);
    font-size: 1.35rem;
    line-height: 1;
  }

  /* exit-node picker as a bottom sheet: full width, large tap targets, a header,
     and safe-area padding. The fixed positioning from positionMenu is overridden. */
  .zone-menu {
    left: 0 !important;
    right: 0;
    top: auto !important;
    bottom: 0;
    width: 100%;
    max-width: none;
    max-height: 72vh;
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 0.4rem 0.5rem calc(0.5rem + env(safe-area-inset-bottom));
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.28);
  }
  /* sticky so the "Route X through" header stays put while a long list scrolls */
  .zone-menu-title { display: block; position: sticky; top: 0; background: var(--card); }
  .zone-menu-item { min-height: 48px; font-size: 1rem; padding: 0.7rem 0.75rem; }

  .graph-hint .hint-desktop { display: none; }
  .graph-hint .hint-mobile { display: inline; }
}

/* ----------------------------------------------- responsive ------------ */
@media (max-width: 30rem) {
  .topbar-subtitle { display: none; }
  .updated { display: none; }
  .topbar-title { font-size: 1.0625rem; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
