/* Mobile-first. Header pinned, list scrolls, tab bar docked above the home bar. */

:root {
  /* dark (default) */
  --bg: #0b0b0e;
  --panel: #15161b;
  --panel-2: #1d1f26;
  --line: #2a2d36;
  --text: #eceef2;
  --muted: #8b90a0;
  --accent: #6ea8fe;
  --ok: #3ddc97;
  --warn: #ffb454;
  --bad: #ff6b6b;
  --on-accent: #08121f;
  --scrim: rgba(0, 0, 0, 0.5);
  --sheet-shadow: rgba(0, 0, 0, 0.45);
  --switch-knob: #ffffff;
  color-scheme: dark;

  --tabs-h: 60px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

/* ---- light theme ---- */
[data-theme="light"] {
  --bg: #f4f5f8;
  --panel: #ffffff;
  --panel-2: #eef0f4;
  --line: #d9dce3;
  --text: #15171c;
  --muted: #6a7080;
  --accent: #2f6fed;
  --ok: #14a86b;
  --warn: #d98a12;
  --bad: #e0433d;
  --on-accent: #ffffff;
  --scrim: rgba(20, 22, 28, 0.35);
  --sheet-shadow: rgba(20, 22, 28, 0.18);
  --switch-knob: #ffffff;
  color-scheme: light;
}

/* follow the OS unless the user overrode it manually */
@media (prefers-color-scheme: light) {
  :root:not([data-theme]) {
    --bg: #f4f5f8;
    --panel: #ffffff;
    --panel-2: #eef0f4;
    --line: #d9dce3;
    --text: #15171c;
    --muted: #6a7080;
    --accent: #2f6fed;
    --ok: #14a86b;
    --warn: #d98a12;
    --bad: #e0433d;
    --on-accent: #ffffff;
    --scrim: rgba(20, 22, 28, 0.35);
    --sheet-shadow: rgba(20, 22, 28, 0.18);
    --switch-knob: #ffffff;
    color-scheme: light;
  }
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB",
    system-ui, sans-serif;
  overscroll-behavior-y: none;
  transition: background 0.25s ease, color 0.25s ease;
}

/* ------------------------------------------------------------------ header */

.top {
  position: sticky;
  top: 0;
  z-index: 10;
  padding: calc(var(--safe-t) + 10px) 14px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
  border-bottom: 1px solid var(--line);
}

.top-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  letter-spacing: 0.2px;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  transition: background 0.2s;
}
.dot.live {
  background: var(--ok);
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--ok) 22%, transparent);
}
.dot.err {
  background: var(--bad);
}

.icon-btn {
  min-width: 40px;
  min-height: 40px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 17px;
}
.icon-btn:active {
  background: var(--panel-2);
}
.icon-btn.spin {
  animation: spin 0.7s linear infinite;
}
@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

.current {
  margin-top: 10px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
}
.current-label {
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.current-name {
  margin-top: 2px;
  font-size: 16px;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.current-host {
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.filter-row {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.search {
  flex: 1;
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
  font-size: 15px; /* >=16px avoids iOS zoom; 15 + maximum-scale=1 is fine */
}
.search::placeholder {
  color: var(--muted);
}
.search:focus {
  outline: none;
  border-color: var(--accent);
}

.chip-toggle {
  min-height: 40px;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--panel);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}
.chip-toggle[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, var(--panel));
}

/* -------------------------------------------------------------------- list */

.list {
  padding: 10px 12px calc(var(--tabs-h) + var(--safe-b) + 18px);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.empty {
  padding: 32px 12px;
  text-align: center;
  color: var(--muted);
}

.node {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--panel);
  transition: border-color 0.15s, transform 0.06s;
}
.node:active {
  transform: scale(0.995);
}
.node.on {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 10%, var(--panel));
}
.node.busy {
  opacity: 0.55;
}

.node-main {
  min-width: 0;
}
.node-name {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.node-host {
  font-size: 12px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.node-meta {
  grid-column: 1 / -1;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 2px;
}
.tag {
  padding: 2px 7px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.tag.ok {
  color: var(--ok);
  border-color: color-mix(in srgb, var(--ok) 40%, var(--line));
}
.tag.warn {
  color: var(--warn);
  border-color: color-mix(in srgb, var(--warn) 40%, var(--line));
}
.tag.bad {
  color: var(--bad);
  border-color: color-mix(in srgb, var(--bad) 40%, var(--line));
}
.tag.accent {
  color: var(--accent);
  border-color: color-mix(in srgb, var(--accent) 40%, var(--line));
}

.node-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}
.btn {
  min-height: 38px;
  min-width: 54px;
  padding: 0 12px;
  border-radius: 11px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}
.btn:active {
  background: var(--line);
}
.btn.primary {
  border-color: var(--accent);
  color: var(--on-accent);
  background: var(--accent);
}
.btn[disabled] {
  opacity: 0.5;
}

/* -------------------------------------------------------------------- tabs */

.tabs {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 20;
  height: calc(var(--tabs-h) + var(--safe-b));
  padding-bottom: var(--safe-b);
  display: flex;
  border-top: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 92%, transparent);
  backdrop-filter: saturate(160%) blur(12px);
}

.tab {
  flex: 1;
  border: 0;
  background: transparent;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 11px;
  font-weight: 600;
}
.tab .tab-label {
  font-size: 12px;
}
.tab .tab-count {
  font-size: 10px;
  opacity: 0.75;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
}
.tab[aria-selected="true"] {
  color: var(--accent);
}
.tab[aria-selected="true"] .tab-label::after {
  content: "";
  display: block;
  height: 2px;
  margin-top: 3px;
  border-radius: 2px;
  background: var(--accent);
}

/* ------------------------------------------------------------------- toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(var(--tabs-h) + var(--safe-b) + 16px);
  transform: translate(-50%, 12px);
  z-index: 30;
  max-width: 88vw;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}
.toast.show {
  opacity: 1;
  transform: translate(-50%, 0);
}
.toast.bad {
  border-color: color-mix(in srgb, var(--bad) 50%, var(--line));
}

/* ------------------------------------------------------- routing + failover */

.chip-toggle.active {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 14%, var(--panel));
}

/* Routing sheet: hidden by default; floats above the list only while open,
   so the bottom tab row is always visible. */
.sheet-scrim {
  position: fixed;
  inset: 0;
  background: var(--scrim);
  backdrop-filter: blur(2px);
  z-index: 60;
}
body.sheet-open { overflow: hidden; }

.routing.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 61;
  margin: 0;
  border-top: 1px solid var(--line);
  border-radius: 18px 18px 0 0;
  background: var(--panel);
  padding: 6px 16px calc(14px + var(--safe-b));
  max-height: 76vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  box-shadow: 0 -12px 40px var(--sheet-shadow);
  animation: sheet-up 0.22s ease;
}
@keyframes sheet-up {
  from { transform: translateY(24px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}

/* Queue detail (expanded) */
.sheet-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 2px 0 6px;
}
.sheet-title { font-size: 16px; font-weight: 700; color: var(--text); }
.sheet-close {
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--muted);
  width: 30px;
  height: 30px;
  border-radius: 9px;
  font-size: 14px;
  cursor: pointer;
}

/* ⚙ routing tab, sitting in the same bottom row as the app tabs */
.tab.tab-routing[aria-selected="true"] {
  color: var(--accent);
}
.tab.tab-routing .tab-label { font-size: 18px; line-height: 1; }

/* 📋 logs tab */
.tab.tab-logs[aria-selected="true"] {
  color: var(--accent);
}
.tab.tab-logs .tab-label { font-size: 18px; line-height: 1; }
.logs-badge {
  min-width: 18px;
  padding: 0 5px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--accent) 18%, var(--panel));
  color: var(--accent);
  font-weight: 700;
}

/* logs sheet */
.logs-window { font-size: 12px; color: var(--muted); font-weight: 400; }
.logs-filters {
  display: flex;
  gap: 8px;
  padding: 4px 0 10px;
}
.logs-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.log-item {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  padding: 9px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
}
.log-main { min-width: 0; flex: 1; }
.log-top {
  display: flex;
  align-items: baseline;
  gap: 8px;
  min-width: 0;
}
.log-node {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.log-model {
  font-size: 11px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.log-meta {
  margin-top: 3px;
  font-size: 11px;
  color: var(--muted);
}
.log-side { flex: none; }
.log-error {
  grid-column: 1 / -1;
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px dashed var(--line);
  font-size: 11px;
  color: var(--bad);
  word-break: break-all;
}

.switch-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.switch-text { min-width: 0; }
.switch-title { font-size: 15px; font-weight: 600; color: var(--text); }
.switch-sub { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* iOS-style switch */
.switch {
  flex: none;
  width: 50px;
  height: 30px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  position: relative;
  cursor: pointer;
  transition: background 0.18s ease, border-color 0.18s ease;
  -webkit-tap-highlight-color: transparent;
}
.switch::after {
  content: "";
  position: absolute;
  top: 50%;
  left: 3px;
  transform: translateY(-50%);
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--muted);
  transition: left 0.18s ease, background 0.18s ease;
}
.switch.on {
  background: color-mix(in srgb, var(--ok) 55%, var(--panel-2));
  border-color: color-mix(in srgb, var(--ok) 60%, var(--line));
}
.switch.on::after {
  left: calc(100% - 27px);
  background: var(--switch-knob);
}
.switch[disabled] { opacity: 0.4; cursor: not-allowed; }

.retries-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  border-bottom: 1px solid color-mix(in srgb, var(--line) 60%, transparent);
}
.retries-row.dim { opacity: 0.4; }
.retries-label { font-size: 14px; color: var(--text); }
.stepper { display: flex; align-items: center; gap: 4px; }
.step-btn {
  width: 32px;
  height: 32px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
}
.step-btn:active { background: var(--line); }
.step-val {
  min-width: 34px;
  text-align: center;
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.queue { padding-top: 12px; }
.queue-head {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  display: flex;
  align-items: center;
  gap: 6px;
}
.queue-count { color: var(--muted); font-weight: 400; }
.queue-list { margin-top: 8px; display: flex; flex-direction: column; gap: 6px; }

.queue-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--panel-2);
}
.queue-rank {
  flex: none;
  width: 22px;
  height: 22px;
  border-radius: 7px;
  background: color-mix(in srgb, var(--accent) 18%, var(--panel));
  color: var(--accent);
  font-size: 12px;
  font-weight: 700;
  display: grid;
  place-items: center;
}
.queue-text { flex: 1; min-width: 0; }
.queue-name {
  font-size: 13px;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.queue-host {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.queue-item .tag { flex: none; }
.queue-remove {
  flex: none;
  border: 1px solid var(--line);
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 5px 9px;
  border-radius: 8px;
  cursor: pointer;
}
.queue-remove:active { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 50%, var(--line)); }

.queue-hint {
  margin-top: 8px;
  font-size: 11px;
  color: var(--muted);
}

/* node card queue toggle */
.btn.queue.on {
  border-color: color-mix(in srgb, var(--warn) 55%, var(--line));
  color: var(--warn);
  background: color-mix(in srgb, var(--warn) 12%, var(--panel));
}

/* compact node card: primary action + "⋯", secondary actions expand inline */
.btn.icon {
  min-width: 40px;
  padding-left: 0;
  padding-right: 0;
  font-size: 18px;
  letter-spacing: 1px;
}
.node-more {
  grid-column: 1 / -1;
  display: flex;
  gap: 8px;
  margin-top: 4px;
}
.node-more[hidden] { display: none; }
.node-more .btn { flex: 1; }

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
