:root {
  --bg: #0f1420;
  --surface: #171e2e;
  --surface-2: #202a3f;
  --line: #2a3550;
  --text: #e8ecf5;
  --muted: #94a0ba;
  --accent: #5b9dff;
  --accent-ink: #06142b;
  --good: #3ecf8e;
  --warn: #f5b942;
  --bad: #ff6b6b;
  --bad-ink: #2b0707;
  --radius: 14px;
  --tap: 48px;
  --bar: 64px;
  color-scheme: dark;
}
@media (prefers-color-scheme: light) {
  :root {
    --bg: #f3f5fa;
    --surface: #ffffff;
    --surface-2: #e9edf6;
    --line: #d5dbe9;
    --text: #172033;
    --muted: #5d6a85;
    --accent: #1f6feb;
    --accent-ink: #ffffff;
    --good: #12935e;
    --warn: #a86a00;
    --bad: #d03a3a;
    --bad-ink: #ffffff;
    color-scheme: light;
  }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior-y: none;
}
[hidden] { display: none !important; }
h1, h2, h3, p { margin: 0; }
button, input, select, textarea { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Shell */
.app { display: flex; flex-direction: column; height: 100%; max-width: 760px; margin: 0 auto; }
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: calc(env(safe-area-inset-top) + 10px) 16px 10px;
  border-bottom: 1px solid var(--line);
}
.topbar h1 { font-size: 20px; font-weight: 700; }
.view { flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 14px 16px 24px; outline: none; }
.tabbar {
  display: grid; grid-template-columns: 1fr 1fr;
  border-top: 1px solid var(--line); background: var(--surface);
  padding-bottom: env(safe-area-inset-bottom);
}
.tab {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px;
  min-height: var(--bar); background: none; border: 0; color: var(--muted); font-size: 12px; cursor: pointer;
}
.tab svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.tab[aria-current="page"] { color: var(--accent); }

/* Controls */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 0 18px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface-2); cursor: pointer; font-weight: 600;
}
.btn:disabled { opacity: .45; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.danger { background: var(--bad); border-color: var(--bad); color: var(--bad-ink); }
.btn.outline-danger { color: var(--bad); border-color: var(--bad); background: transparent; }
.btn.ghost { background: transparent; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn.block { width: 100%; }

.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 14px; }
.field > span { font-size: 13px; color: var(--muted); }
.field input, .field select, .field textarea, .search {
  width: 100%; min-height: var(--tap); padding: 10px 14px; border-radius: 12px;
  border: 1px solid var(--line); background: var(--surface); font-size: 16px; /* 16px stops iOS zooming on focus */
}
.field .row { display: flex; gap: 8px; }
.field .row input { flex: 1; min-width: 0; }
.hint { font-size: 13px; color: var(--muted); }
.error { color: var(--bad); font-size: 14px; margin-bottom: 12px; }

/* Login */
.login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card { width: 100%; max-width: 380px; display: flex; flex-direction: column; }
.login-card h1 { font-size: 24px; margin: 8px 0 20px; }
.login-icon { border-radius: 16px; }

/* Lists */
.toolbar { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  min-height: 36px; padding: 0 14px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted); font-size: 14px; cursor: pointer;
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); font-weight: 600; }
.count { color: var(--muted); font-size: 13px; margin: 4px 2px 10px; }
.list { display: flex; flex-direction: column; gap: 8px; }
.row-card {
  display: flex; align-items: center; gap: 12px; width: 100%; text-align: left; min-height: 64px;
  padding: 10px 14px; border-radius: var(--radius); border: 1px solid var(--line); background: var(--surface); cursor: pointer;
}
.row-card .main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.row-card .name, .row-card .sub { display: block; }
.row-card .name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-card .sub { color: var(--muted); font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chev { color: var(--muted); font-size: 20px; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; background: var(--muted); }
.dot.online { background: var(--good); }
.dot.hub { background: var(--accent); }
.dot.mission { background: var(--warn); }
.dot.banned { background: var(--bad); }
.empty { text-align: center; color: var(--muted); padding: 40px 8px; }
.fab {
  position: fixed; right: max(16px, calc((100vw - 760px) / 2 + 16px)); bottom: calc(var(--bar) + env(safe-area-inset-bottom) + 16px);
  width: 56px; height: 56px; border-radius: 50%; border: 0; background: var(--accent); color: var(--accent-ink);
  font-size: 30px; line-height: 1; cursor: pointer; box-shadow: 0 6px 18px rgba(0,0,0,.35);
}

/* Badges */
.badges { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { padding: 2px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; background: var(--surface-2); color: var(--muted); }
.badge.good { background: color-mix(in srgb, var(--good) 22%, transparent); color: var(--good); }
.badge.warn { background: color-mix(in srgb, var(--warn) 22%, transparent); color: var(--warn); }
.badge.bad { background: color-mix(in srgb, var(--bad) 22%, transparent); color: var(--bad); }
.badge.accent { background: color-mix(in srgb, var(--accent) 22%, transparent); color: var(--accent); }

/* Sheets (full-screen dialogs) and modal dialogs */
dialog { color: var(--text); background: var(--bg); border: 0; padding: 0; max-width: none; max-height: none; }
dialog::backdrop { background: rgba(5, 8, 16, .66); }
dialog.sheet { width: 100%; height: 100%; margin: 0; }
dialog.sheet[open] { display: flex; flex-direction: column; }
.sheet-inner { max-width: 760px; width: 100%; margin: 0 auto; display: flex; flex-direction: column; height: 100%; }
.sheet-head {
  display: flex; align-items: center; gap: 8px; padding: calc(env(safe-area-inset-top) + 8px) 8px 8px;
  border-bottom: 1px solid var(--line);
}
.sheet-head h2 { flex: 1; font-size: 18px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet-body { flex: 1; overflow-y: auto; padding: 16px 16px calc(env(safe-area-inset-bottom) + 24px); }
dialog.modal {
  width: min(92vw, 440px); margin: auto; border-radius: 18px; border: 1px solid var(--line); background: var(--surface);
}
dialog.modal form { padding: 20px 18px 18px; display: flex; flex-direction: column; }
dialog.modal h2 { font-size: 18px; margin-bottom: 8px; }
dialog.modal p.msg { color: var(--muted); margin-bottom: 14px; white-space: pre-line; }
.actions { display: flex; gap: 10px; margin-top: 4px; }
.actions .btn { flex: 1; }

/* Detail */
.section { margin-top: 20px; }
.section > h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin-bottom: 8px; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); }
.kv { display: grid; grid-template-columns: auto 1fr; margin: 0; }
.kv dt, .kv dd { padding: 11px 14px; margin: 0; border-bottom: 1px solid var(--line); }
.kv dt { color: var(--muted); font-size: 14px; }
.kv dd { text-align: right; overflow-wrap: anywhere; }
.kv dt:nth-last-of-type(1), .kv dd:last-child { border-bottom: 0; }
.kv .copy { background: none; border: 0; color: var(--accent); padding: 0; cursor: pointer; text-decoration: underline; }
.stats { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.stat { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 10px 14px; }
.stat b { display: block; font-size: 20px; }
.stat span { color: var(--muted); font-size: 12px; }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { padding: 11px 14px; border-bottom: 1px solid var(--line); }
.timeline li:last-child { border-bottom: 0; }
.timeline .meta { color: var(--muted); font-size: 13px; }
.btn-stack { display: flex; flex-direction: column; gap: 10px; }

/* Servers */
.server-card { padding: 14px; margin-bottom: 12px; }
.server-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 6px; }
.server-head h2 { font-size: 18px; }
.server-meta { color: var(--muted); font-size: 14px; display: flex; flex-wrap: wrap; gap: 4px 14px; margin-bottom: 10px; }
.server-meta b { color: var(--text); font-weight: 600; }
.roster { margin: 10px 0 0; padding: 0; list-style: none; border-top: 1px solid var(--line); }
.roster li { display: flex; justify-content: space-between; gap: 8px; padding: 8px 2px; border-bottom: 1px solid var(--line); font-size: 14px; }
.roster .muted, .muted { color: var(--muted); }
.server-actions { display: grid; grid-template-columns: repeat(6, 1fr); gap: 8px; margin-top: 12px; }
.server-actions .btn { padding: 0 8px; white-space: nowrap; grid-column: span 2; }
.server-actions .half { grid-column: span 3; }
.alert { padding: 10px 12px; border-radius: 10px; font-size: 14px; background: color-mix(in srgb, var(--bad) 18%, transparent); color: var(--bad); margin-top: 8px; }

/* Toasts */
.toasts {
  position: fixed; left: 0; right: 0; z-index: 1000; display: flex; flex-direction: column; align-items: center; gap: 8px; pointer-events: none;
  bottom: calc(var(--bar) + env(safe-area-inset-bottom) + 12px);
}
.toast { pointer-events: auto; max-width: min(92vw, 420px); padding: 12px 16px; border-radius: 12px; background: var(--text); color: var(--bg); font-size: 15px; box-shadow: 0 6px 20px rgba(0,0,0,.35); }
.toast.error { background: var(--bad); color: var(--bad-ink); }
.toast.ok { background: var(--good); color: #04210f; }
.spinner { text-align: center; color: var(--muted); padding: 24px; }

@media (prefers-reduced-motion: no-preference) {
  .toast { animation: rise .18s ease-out; }
  @keyframes rise { from { transform: translateY(8px); opacity: 0; } }
}

/* Server logs */
.sheet-body.logs-body { display: flex; flex-direction: column; gap: 10px; padding-bottom: calc(env(safe-area-inset-bottom) + 12px); overflow: hidden; }
.logs-tools { display: flex; flex-direction: column; gap: 8px; }
.logs-row { display: flex; gap: 8px; }
.logs-row .search { flex: 1; width: auto; min-width: 0; }
.logs-row .btn { min-height: var(--tap); }
.logs-status { font-size: 13px; color: var(--muted); }
.logs-status.live::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--good); margin-right: 6px; }
.logs-status.bad { color: var(--bad); }
.logs-frame { position: relative; flex: 1; min-height: 0; display: flex; }
.logs-view {
  flex: 1; min-width: 0; overflow: auto; -webkit-overflow-scrolling: touch; border: 1px solid var(--line); border-radius: 10px;
  background: var(--surface); padding: 6px 0; font: 12px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.ll { display: flex; gap: 8px; padding: 1px 10px; border-left: 3px solid transparent; }
.ll .lt { flex: none; color: var(--muted); }
.ll .lx { white-space: pre-wrap; overflow-wrap: anywhere; min-width: 0; }
.ll.lv-error { border-left-color: var(--bad); color: var(--bad); }
.ll.lv-warn { border-left-color: var(--warn); color: var(--warn); }
.ll.supervisor { border-left-color: var(--accent); color: var(--accent); }
.ll.marker { justify-content: center; color: var(--muted); font-style: italic; padding: 6px 10px; }
.logs-view .empty { padding: 24px 12px; font: 14px/1.4 system-ui, sans-serif; }
.logs-jump { position: absolute; right: 12px; bottom: 12px; box-shadow: 0 2px 10px rgba(0, 0, 0, .35); }
