:root {
  color-scheme: light dark;
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --surface-3: #e6e9ee;
  --border: #d9dde4;
  --border-strong: #c2c8d2;
  --text: #1b1f27;
  --text-muted: #626b7a;
  --accent: #2563eb;
  --accent-hover: #1d4fd0;
  --accent-soft: #e3ecfd;
  --accent-text: #ffffff;
  --good: #15803d;
  --good-soft: #e3f4e8;
  --warn: #b45309;
  --warn-soft: #fdf1df;
  --bad: #c02626;
  --bad-soft: #fde6e6;
  --info-soft: #e6effd;
  --focus: #2563eb;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-pop: 0 10px 30px rgba(16, 24, 40, 0.18);
  --log-bg: #fbfbfc;
  --chart-down: #2563eb;
  --chart-up: #db7a12;
  --chart-direct: #8a94a6;
  --toggle-off: #c3c9d3;
  --radius: 10px;
  --radius-sm: 7px;
  --topbar-h: 56px;
  --nav-h: 60px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", "Liberation Sans", sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Noto Color Emoji";
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", "DejaVu Sans Mono", monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --surface: #1c1f25;
    --surface-2: #23272e;
    --surface-3: #2c313a;
    --border: #30353e;
    --border-strong: #434a56;
    --text: #e6e9ef;
    --text-muted: #98a1b0;
    --accent: #5b8def;
    --accent-hover: #74a0f3;
    --accent-soft: #1f2d4a;
    --accent-text: #0d1220;
    --good: #4ec27a;
    --good-soft: #17301f;
    --warn: #e9a23b;
    --warn-soft: #36281a;
    --bad: #f06a6a;
    --bad-soft: #3a1d1f;
    --info-soft: #1d2a40;
    --focus: #7aa7ff;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-pop: 0 12px 32px rgba(0, 0, 0, 0.55);
    --log-bg: #16181d;
    --chart-down: #6b9bf5;
    --chart-up: #f0a54a;
    --chart-direct: #7c8597;
    --toggle-off: #4a515d;
  }
}

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

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); }
a:hover { color: var(--accent-hover); }
h1, h2, h3 { margin: 0; line-height: 1.25; }
p { margin: 0 0 10px; }
code, .mono { font-family: var(--mono); font-size: 0.92em; }
img { display: block; }
fieldset { border: 0; margin: 0; padding: 0; min-width: 0; }
legend { padding: 0; font-weight: 600; margin-bottom: 6px; }
progress { width: 100%; height: 8px; accent-color: var(--accent); }

:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.sr-only {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link { position: absolute; left: 8px; top: -48px; z-index: 100; background: var(--surface); padding: 8px 12px; border-radius: var(--radius-sm); box-shadow: var(--shadow-pop); }
.skip-link:focus { top: 8px; }
.muted { color: var(--text-muted); }
.small { font-size: 12.5px; }
.ellipsis { display: block; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ok { color: var(--good); font-weight: 600; }
.bad { color: var(--bad); font-weight: 600; }
.warn { color: var(--warn); font-weight: 600; }
.noscript { padding: 24px; text-align: center; }
.empty { padding: 28px 12px; text-align: center; color: var(--text-muted); }

/* icons, spinner */
.icon { flex: none; display: inline-block; vertical-align: middle; }
.icon-space { display: inline-block; width: 16px; flex: none; }
.spinner {
  display: inline-block; width: 20px; height: 20px; flex: none; vertical-align: middle;
  border: 2px solid var(--border-strong); border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
.spinner.sm { width: 14px; height: 14px; }
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) { .spinner { animation-duration: 2.4s; } }
.splash, .loading { display: flex; align-items: center; justify-content: center; gap: 10px; color: var(--text-muted); }
.splash { min-height: 100vh; }
.loading { padding: 28px 12px; }

/* buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 34px; padding: 6px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text);
  font: inherit; font-weight: 500; line-height: 1.2; cursor: pointer; white-space: nowrap;
  text-decoration: none;
}
.btn:hover:not(:disabled) { background: var(--surface-2); border-color: var(--border-strong); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover:not(:disabled) { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--border)); }
.btn.danger:hover:not(:disabled) { background: var(--bad-soft); }
.dialog-foot .btn.danger { background: var(--bad); border-color: var(--bad); color: #fff; }
.btn.small { min-height: 28px; padding: 3px 9px; font-size: 13px; }
.btn.block { display: flex; width: 100%; }
.btn.more { margin-top: 10px; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center; flex: none;
  width: 34px; height: 34px; padding: 0; border-radius: var(--radius-sm);
  border: 1px solid transparent; background: transparent; color: var(--text-muted); cursor: pointer;
}
.icon-btn:hover:not(:disabled) { background: var(--surface-2); color: var(--text); }
.icon-btn[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.icon-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.icon-btn.small { width: 28px; height: 28px; }

/* forms */
.input {
  width: 100%; min-height: 34px; padding: 6px 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit;
}
.input:hover:not(:disabled) { border-color: var(--border-strong); }
.input:focus-visible { outline: 2px solid var(--focus); outline-offset: 0; border-color: var(--focus); }
.input:disabled { opacity: 0.6; }
textarea.input { resize: vertical; min-height: 80px; line-height: 1.4; }
select.input { padding-right: 28px; }
input[type="file"].input { padding: 5px; }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 14px; }
.field > label { font-weight: 600; }
.field-help { color: var(--text-muted); font-size: 12.5px; }
.field-error, .form-error { color: var(--bad); font-size: 12.5px; }
.form-error { font-size: 13.5px; margin: 0 0 10px; }
.field.invalid .input { border-color: var(--bad); }
.field.changed > label, .field.changed .switch-row > label > span:first-child { color: var(--accent); }
.restart-tag { display: inline-block; margin-top: 2px; color: var(--warn); }
.input-unit { display: flex; align-items: center; gap: 8px; }
.input-unit .input { max-width: 200px; }
.input-row { display: flex; align-items: center; gap: 6px; }
.input-row .input { flex: 1; min-width: 0; }
.field-label { font-weight: 600; }
.unit { color: var(--text-muted); }
.check { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; min-height: 30px; }
.check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); flex: none; }
.parts { display: flex; flex-direction: column; gap: 2px; margin-bottom: 12px; }

.switch-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; padding: 6px 0; }
.switch-row > label { display: flex; flex-direction: column; gap: 2px; cursor: pointer; min-width: 0; }
.switch-row > label > span { font-weight: 600; }
.switch-row > label > small { color: var(--text-muted); font-size: 12.5px; }
.card > .switch-row + .switch-row { border-top: 1px solid var(--border); }

.toggle {
  appearance: none; -webkit-appearance: none; flex: none; margin: 0; cursor: pointer;
  position: relative; width: 40px; height: 24px; border-radius: 12px;
  background: var(--toggle-off); transition: background 0.15s;
}
.toggle::before {
  content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 2px rgba(0, 0, 0, 0.3); transition: transform 0.15s;
}
.toggle:checked { background: var(--accent); }
.toggle:checked::before { transform: translateX(16px); }
.toggle:disabled { opacity: 0.5; cursor: not-allowed; }
.toggle:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }

.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; background: var(--surface); }
.seg { position: relative; display: inline-flex; align-items: center; padding: 5px 12px; cursor: pointer; font-weight: 500; color: var(--text-muted); }
.seg + .seg { border-left: 1px solid var(--border); }
.seg input { position: absolute; opacity: 0; width: 1px; height: 1px; pointer-events: none; }
.seg.active { background: var(--accent-soft); color: var(--accent); }
.seg:focus-within { outline: 2px solid var(--focus); outline-offset: -2px; }

/* notices, banners */
.notice, .banner {
  display: flex; align-items: flex-start; gap: 10px; padding: 10px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--border); background: var(--surface); margin-bottom: 12px;
}
.notice > .icon, .banner > .icon { margin-top: 1px; }
.notice-body, .banner-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; overflow-wrap: anywhere; }
.notice.bad, .banner.bad { background: var(--bad-soft); border-color: color-mix(in srgb, var(--bad) 35%, transparent); }
.notice.bad > .icon, .banner.bad > .icon { color: var(--bad); }
.notice.warn, .banner.warn { background: var(--warn-soft); border-color: color-mix(in srgb, var(--warn) 35%, transparent); }
.notice.warn > .icon, .banner.warn > .icon { color: var(--warn); }
.notice.info, .banner.info { background: var(--info-soft); border-color: color-mix(in srgb, var(--accent) 30%, transparent); }
.notice.info > .icon, .banner.info > .icon { color: var(--accent); }
.notice.compact { padding: 7px 10px; margin: 8px 0; font-size: 13px; }
.banners { display: flex; flex-direction: column; gap: 8px; margin-bottom: 14px; }
.banners .banner { margin: 0; align-items: center; }
.problems { margin: 4px 0 0; padding-left: 18px; font-size: 13px; }
.note { padding: 8px 10px; border-radius: var(--radius-sm); font-size: 13px; }
.note.warn { background: var(--warn-soft); color: var(--text); font-weight: 400; }
.admin-hint { display: flex; align-items: center; gap: 6px; }
.prose p { margin: 0 0 8px; }

/* layout */
.shell { min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px;
  height: var(--topbar-h); padding: 0 16px; padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; color: var(--text); text-decoration: none; font-weight: 700; font-size: 16px; flex: none; }
.brand:hover { color: var(--text); }
.conn { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.conn-text { display: flex; flex-direction: column; min-width: 0; line-height: 1.25; }
.conn-state { font-weight: 600; white-space: nowrap; }
.conn-profile { color: var(--text-muted); font-size: 12.5px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.state-dot { width: 10px; height: 10px; border-radius: 50%; flex: none; background: var(--toggle-off); }
.state-dot.running { background: var(--good); box-shadow: 0 0 0 3px color-mix(in srgb, var(--good) 25%, transparent); }
.state-dot.connecting, .state-dot.stopping { background: var(--warn); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.35; } }
.tag { display: inline-block; padding: 0 5px; border-radius: 4px; font-size: 10.5px; font-weight: 700; letter-spacing: 0.03em; background: var(--accent-soft); color: var(--accent); vertical-align: 1px; }
.tag.ks-armed { background: var(--good-soft); color: var(--good); }
.tag.ks-arming { background: var(--warn-soft); color: var(--warn); }
.tag.ks-failed { background: var(--bad-soft); color: var(--bad); }
.rates { display: flex; gap: 12px; font-variant-numeric: tabular-nums; font-size: 12.5px; color: var(--text-muted); white-space: nowrap; }
.rates .arrow { margin-right: 3px; }

.nav ul { list-style: none; margin: 0; padding: 0; }
.nav-link { display: flex; align-items: center; gap: 10px; color: var(--text-muted); text-decoration: none; font-weight: 500; border-radius: var(--radius-sm); }
.nav-link:hover { color: var(--text); background: var(--surface-2); }
.nav-link[aria-current="page"] { color: var(--accent); background: var(--accent-soft); }

.main { padding: 16px; outline: none; min-width: 0; }
.view { max-width: 1280px; margin: 0 auto; }
.view-title { font-size: 20px; margin-bottom: 14px; }
.view-title[tabindex]:focus { outline: none; }
.grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(320px, 1fr)); align-items: start; }
.grid.two { grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px 16px; box-shadow: var(--shadow); min-width: 0; margin-bottom: 14px; }
.grid > .card { margin-bottom: 0; }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; min-height: 28px; }
.card-head h2 { font-size: 15px; }
.actions { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; }
.pill { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 12.5px; font-weight: 600; background: var(--surface-3); color: var(--text-muted); }
.pill.running { background: var(--good-soft); color: var(--good); }
.pill.connecting, .pill.stopping { background: var(--warn-soft); color: var(--warn); }
.facts { display: grid; grid-template-columns: minmax(110px, auto) 1fr; gap: 6px 14px; margin: 0; }
.facts dt { color: var(--text-muted); }
.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.ks-armed { color: var(--good); font-weight: 600; }
.ks-arming { color: var(--warn); font-weight: 600; }
.ks-failed { color: var(--bad); font-weight: 600; }

/* phone: bottom navigation */
@media (max-width: 899px) {
  .main { padding: 12px; padding-bottom: calc(var(--nav-h) + 16px + env(safe-area-inset-bottom)); }
  .nav {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .nav ul { display: flex; height: var(--nav-h); overflow-x: auto; scrollbar-width: none; }
  .nav ul::-webkit-scrollbar { display: none; }
  .nav li { flex: 1 0 68px; }
  .nav-link { flex-direction: column; justify-content: center; gap: 2px; height: 100%; border-radius: 0; font-size: 10.5px; padding: 0 2px; }
  .nav-link span { max-width: 100%; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .nav-link[aria-current="page"] { background: transparent; }
  .grid, .grid.two { grid-template-columns: minmax(0, 1fr); }
}
@media (max-width: 559px) {
  .topbar { gap: 8px; padding: 0 10px; }
  .brand-name, .hide-xs { display: none; }
  .rates { flex-direction: column; gap: 0; font-size: 11.5px; line-height: 1.3; }
  .view-title { font-size: 18px; }
  .card { padding: 12px; }
}
@media (min-width: 900px) {
  .shell { display: grid; grid-template-columns: 200px minmax(0, 1fr); grid-template-rows: var(--topbar-h) 1fr; }
  .topbar { grid-column: 1 / -1; }
  .nav { position: sticky; top: var(--topbar-h); align-self: start; height: calc(100vh - var(--topbar-h)); padding: 14px 10px; border-right: 1px solid var(--border); background: var(--surface); }
  .nav ul { display: flex; flex-direction: column; gap: 2px; }
  .nav-link { padding: 8px 12px; }
  .main { padding: 20px 24px; }
}

/* dialogs */
.dialog {
  padding: 0; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text);
  width: min(520px, calc(100vw - 24px)); max-height: calc(100vh - 32px); box-shadow: var(--shadow-pop);
}
.dialog.wide { width: min(720px, calc(100vw - 24px)); }
.dialog::backdrop { background: rgba(10, 14, 22, 0.5); }
.dialog-inner { display: flex; flex-direction: column; max-height: calc(100vh - 34px); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 12px 12px 8px 18px; }
.dialog-head h2 { font-size: 16px; }
.dialog-body { padding: 4px 18px 12px; overflow: auto; }
.dialog-foot { display: flex; justify-content: flex-end; flex-wrap: wrap; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }

/* toasts */
.toasts { position: fixed; z-index: 60; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; width: min(400px, calc(100vw - 24px)); pointer-events: none; }
.toast {
  pointer-events: auto; display: flex; align-items: flex-start; gap: 10px; padding: 10px 8px 10px 12px;
  background: var(--surface); border: 1px solid var(--border); border-left: 4px solid var(--accent);
  border-radius: var(--radius-sm); box-shadow: var(--shadow-pop);
}
.toast > .icon { margin-top: 1px; color: var(--accent); }
.toast.success { border-left-color: var(--good); }
.toast.success > .icon { color: var(--good); }
.toast.error { border-left-color: var(--bad); }
.toast.error > .icon { color: var(--bad); }
.toast-body { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; overflow-wrap: anywhere; }
.toast-body span { color: var(--text-muted); font-size: 12.5px; white-space: pre-line; }
@media (max-width: 899px) {
  .toasts { left: 12px; right: 12px; width: auto; bottom: calc(var(--nav-h) + 12px + env(safe-area-inset-bottom)); }
}

/* restart overlay */
.overlay { position: fixed; inset: 0; z-index: 50; display: flex; align-items: center; justify-content: center; padding: 16px; background: color-mix(in srgb, var(--bg) 82%, transparent); backdrop-filter: blur(2px); }
.overlay-card { max-width: 420px; text-align: center; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 24px; box-shadow: var(--shadow-pop); display: flex; flex-direction: column; align-items: center; gap: 10px; }
.overlay-card h2 { font-size: 17px; }

/* menu */
.menu { position: relative; display: inline-flex; }
.menu-list {
  position: absolute; top: calc(100% + 4px); z-index: 30; min-width: 220px; max-width: min(320px, calc(100vw - 24px));
  padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius-sm); box-shadow: var(--shadow-pop);
}
.menu-list.end { right: 0; }
.menu-list.start { left: 0; }
.menu-item {
  display: flex; align-items: flex-start; gap: 10px; width: 100%; padding: 8px 10px; border: 0; border-radius: 5px;
  background: transparent; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.menu-item .icon { margin-top: 1px; color: var(--text-muted); }
.menu-item:hover:not(:disabled), .menu-item:focus-visible { background: var(--surface-2); outline: none; }
.menu-item:disabled { opacity: 0.45; cursor: not-allowed; }
.menu-item.danger, .menu-item.danger .icon { color: var(--bad); }
.menu-label { display: flex; flex-direction: column; min-width: 0; }
.menu-label small { color: var(--text-muted); font-size: 12px; }
.menu-sep { height: 1px; margin: 4px 6px; background: var(--border); }

/* badges and profile bits */
.badge { display: inline-block; padding: 1px 7px; border-radius: 5px; font-size: 11.5px; font-weight: 600; background: var(--surface-3); color: var(--text-muted); white-space: nowrap; }
.badge.accent { background: var(--accent-soft); color: var(--accent); }
.lat { font-variant-numeric: tabular-nums; font-weight: 600; white-space: nowrap; }
.lat.good { color: var(--good); }
.lat.warn { color: var(--warn); }
.lat.bad { color: var(--bad); }
.lat.none { color: var(--text-muted); font-weight: 400; }
.country { white-space: nowrap; }
.speed { display: inline-flex; gap: 8px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.traffic { font-variant-numeric: tabular-nums; color: var(--text-muted); white-space: nowrap; }
.profile-summary { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.ps-name { font-size: 16px; font-weight: 600; overflow-wrap: anywhere; }
.ps-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 12px; min-width: 0; color: var(--text-muted); }

/* profiles view */
.group-bar { display: flex; gap: 6px; overflow-x: auto; padding: 2px 2px 8px; margin: 0 -2px 6px; scrollbar-width: thin; }
.group-tab {
  display: inline-flex; align-items: center; gap: 6px; flex: none; max-width: 240px;
  padding: 6px 10px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font: inherit; cursor: pointer;
}
.group-tab:hover { border-color: var(--border-strong); }
.group-tab.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.group-tab.archived:not(.active) { opacity: 0.6; }
.gname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.gcount { font-size: 11.5px; padding: 0 6px; border-radius: 999px; background: var(--surface-3); color: var(--text-muted); }
.group-tab.active .gcount { background: rgba(255, 255, 255, 0.25); color: inherit; }
.group-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 10px; }
.group-info { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; min-width: 0; }
.group-info h2 { font-size: 18px; overflow-wrap: anywhere; }
.group-actions { display: flex; align-items: center; gap: 6px; }
.toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-bottom: 10px; }
.search { position: relative; flex: 1 1 220px; min-width: 0; }
.search .icon { position: absolute; left: 10px; top: 50%; transform: translateY(-50%); color: var(--text-muted); pointer-events: none; }
.search .input { padding-left: 32px; }
.sort { width: auto; flex: none; }
.selbar, .jobbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 8px; padding: 8px 10px; margin-bottom: 10px;
  border-radius: var(--radius-sm); border: 1px solid color-mix(in srgb, var(--accent) 35%, var(--border)); background: var(--accent-soft);
}
.selbar > span:first-child { margin-right: 4px; }
.jobbar-text { flex: 1 1 200px; min-width: 0; }
.jobbar progress { flex: 1 1 120px; }
.list-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 0 4px 6px; }
.table-wrap { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.ptable, .ttable, .ctable { width: 100%; border-collapse: collapse; table-layout: fixed; }
.ptable th, .ttable th, .ctable th { text-align: left; font-size: 12px; font-weight: 600; color: var(--text-muted); padding: 8px; background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap; }
.ptable td, .ttable td, .ctable td { padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; min-width: 0; }
.ptable tbody tr:last-child td, .ttable tbody tr:last-child td, .ctable tbody tr:last-child td { border-bottom: 0; }
.prow { cursor: pointer; }
.prow:hover { background: var(--surface-2); }
.prow.checked { background: var(--accent-soft); }
.prow.running { box-shadow: inset 3px 0 0 var(--good); }
.prow.running .pname { color: var(--good); }
.ptable .c-sel { width: 38px; padding: 0; }
.ptable .c-type { width: 100px; }
.ptable .c-addr { width: 18%; }
.ptable .c-lat { width: 84px; }
.ptable .c-speed { width: 120px; }
.ptable .c-geo { width: 160px; }
.ptable .c-traffic { width: 100px; }
.ptable .c-act { width: 40px; padding: 0 4px; }
.ptable td.c-geo, .ptable td.c-traffic, .ptable td.c-speed, .ptable td.c-type { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cell-check { display: flex; align-items: center; justify-content: center; width: 100%; min-height: 38px; cursor: pointer; }
.cell-check input { width: 16px; height: 16px; margin: 0; accent-color: var(--accent); cursor: pointer; }
.pname {
  display: flex; align-items: center; gap: 6px; width: 100%; min-width: 0; padding: 4px 0; border: 0; background: none;
  color: var(--text); font: inherit; font-weight: 600; text-align: left; cursor: pointer;
}
.pname:hover .ellipsis { text-decoration: underline; }
.run-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--good); flex: none; }
@media (max-width: 1099px) {
  .ptable .c-traffic, .ptable .c-type { display: none; }
}
@media (max-width: 999px) {
  .ptable .c-geo { width: 72px; }
  .ptable .c-geo .ip { display: none; }
}
@media (max-width: 759px) {
  .ptable thead { display: none; }
  .ptable, .ptable tbody { display: block; }
  .ptable tr {
    display: grid; grid-template-columns: 36px minmax(0, 1fr) auto 30px;
    grid-template-areas: "sel name lat act" "sel meta meta act" "sel geo speed act" "sel traffic traffic act";
    column-gap: 6px; padding: 6px 10px 6px 4px; border-bottom: 1px solid var(--border);
  }
  .ptable tbody tr:last-child { border-bottom: 0; }
  .ptable td { display: block; padding: 0; border: 0; width: auto !important; }
  .ptable td:empty { display: none; }
  .ptable .c-sel { grid-area: sel; }
  .ptable .c-name { grid-area: name; }
  .ptable .c-lat { grid-area: lat; text-align: right; align-self: center; }
  .ptable .c-type { grid-area: meta; display: none; }
  .ptable .c-addr { grid-area: meta; padding-left: 0; color: var(--text-muted); font-size: 12.5px; }
  .ptable .c-geo { grid-area: geo; font-size: 12.5px; }
  .ptable .c-geo .ip { display: inline; }
  .ptable .c-speed { grid-area: speed; font-size: 12.5px; text-align: right; }
  .ptable .c-traffic { grid-area: traffic; display: block; font-size: 12px; }
  .ptable .c-act { grid-area: act; align-self: center; padding: 0; }
  .cell-check { min-height: 32px; align-items: flex-start; padding-top: 6px; }
}

/* import dialog */
.import-result p { margin-bottom: 10px; }
.issue-list { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.issue { padding: 8px 10px; border-radius: var(--radius-sm); background: var(--surface-2); overflow-wrap: anywhere; }
.issue.error { border-left: 3px solid var(--bad); }
.issue.warning { border-left: 3px solid var(--warn); }
.examples { margin: 6px 0 0; padding-left: 18px; font-size: 12px; color: var(--text-muted); }
.segmented + .field, form > .segmented { margin-bottom: 12px; }

/* status view */
.job-list { list-style: none; margin: 0; padding: 0; }
.job { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.job:last-child { border-bottom: 0; }
.job.running strong { color: var(--accent); }

/* charts */
.chart { position: relative; }
.chart-svg { display: block; width: 100%; height: auto; max-height: 200px; border-radius: var(--radius-sm); background: var(--surface-2); }
.chart-grid { stroke: var(--border); stroke-width: 1; vector-effect: non-scaling-stroke; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-area { stroke: none; opacity: 0.14; }
.chart-series.s-down .chart-line { stroke: var(--chart-down); }
.chart-series.s-down .chart-area { fill: var(--chart-down); }
.chart-series.s-up .chart-line { stroke: var(--chart-up); }
.chart-series.s-direct .chart-line { stroke: var(--chart-direct); stroke-dasharray: 4 3; }
.chart-series.s-direct-up .chart-line { stroke: var(--chart-direct); stroke-dasharray: 1 3; }
.chart-scale { display: flex; justify-content: space-between; font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
.chart-legend { list-style: none; margin: 8px 0 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12.5px; }
.chart-legend li { display: inline-flex; align-items: center; gap: 6px; }
.chart-legend strong { font-variant-numeric: tabular-nums; }
.swatch { display: inline-block; width: 12px; height: 3px; border-radius: 2px; background: var(--chart-direct); }
.chart-legend .s-down .swatch { background: var(--chart-down); }
.chart-legend .s-up .swatch { background: var(--chart-up); }

/* traffic totals */
.ttable th.num, .ttable td.num { text-align: right; width: 110px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.bar { height: 4px; margin-top: 4px; border-radius: 2px; background: var(--surface-3); overflow: hidden; }
.bar span { display: block; height: 100%; background: var(--chart-down); border-radius: 2px; }
@media (max-width: 559px) {
  .ttable th.num, .ttable td.num { width: 82px; font-size: 12.5px; }
  .ttable th:nth-child(2), .ttable td:nth-child(2) { display: none; }
}

/* logs */
.view-logs .toolbar { margin-bottom: 8px; }
.log-wrap { position: relative; }
.log-view {
  height: calc(100vh - var(--topbar-h) - 190px); min-height: 260px; overflow: auto;
  padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--log-bg);
  font-family: var(--mono); font-size: 12px; line-height: 1.5;
}
.log-line { white-space: pre-wrap; overflow-wrap: anywhere; }
.log-time { color: var(--text-muted); }
.lv-fatal { color: var(--bad); font-weight: 700; }
.lv-error { color: var(--bad); }
.lv-warn { color: var(--warn); }
.lv-debug { color: var(--text-muted); }
.lv-meta { color: var(--accent); font-style: italic; }
.log-empty { padding: 12px 0; font-family: var(--font); }
.jump { position: absolute; right: 16px; bottom: 14px; box-shadow: var(--shadow-pop); }
@media (max-width: 899px) {
  .log-view { height: calc(100vh - var(--topbar-h) - var(--nav-h) - 200px); }
}

/* connections */
.th-btn { display: inline-flex; align-items: center; gap: 3px; max-width: 100%; padding: 0; border: 0; background: none; color: inherit; font: inherit; cursor: pointer; }
.th-btn:hover { color: var(--text); }
.ctable th.num, .ctable td.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
.ctable td.num span { display: block; }
.ctable .c-source, .ctable .c-rule { width: 16%; }
.ctable .c-outbound { width: 13%; }
.ctable .c-network { width: 96px; }
.ctable .c-speed { width: 108px; }
.ctable .c-total { width: 96px; }
.ctable .c-age { width: 72px; }
.ctable .c-act { width: 40px; padding: 0 4px; }
.ctable td.c-network { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.crow:hover { background: var(--surface-2); }
.crow.closed { color: var(--text-muted); }
.conn-sort { display: none; align-items: center; gap: 4px; }
@media (max-width: 1099px) {
  .ctable .c-total { display: none; }
}
@media (max-width: 999px) {
  .ctable .c-network { display: none; }
}
@media (max-width: 759px) {
  .conn-sort { display: flex; }
  .ctable thead { display: none; }
  .ctable, .ctable tbody { display: block; }
  .ctable tr {
    display: grid; grid-template-columns: minmax(0, 1fr) auto 30px;
    grid-template-areas: "dst age act" "src net act" "out speed act" "rule total act";
    column-gap: 10px; padding: 8px 4px 8px 10px; border-bottom: 1px solid var(--border);
  }
  .ctable tbody tr:last-child { border-bottom: 0; }
  .ctable td { display: block; padding: 0; border: 0; width: auto !important; }
  .ctable .c-destination { grid-area: dst; font-weight: 600; }
  .ctable .c-source { grid-area: src; }
  .ctable .c-outbound { grid-area: out; }
  .ctable .c-rule { grid-area: rule; }
  .ctable .c-network { grid-area: net; display: block; text-align: right; font-size: 12px; }
  .ctable .c-speed { grid-area: speed; }
  .ctable .c-total { grid-area: total; display: block; }
  .ctable .c-age { grid-area: age; }
  .ctable .c-act { grid-area: act; align-self: center; padding: 0; }
  .ctable td.num { font-size: 12px; }
  .ctable td.num span { display: inline; }
  .ctable td.num span + span { margin-left: 6px; }
  .ctable .sub { display: none; }
}

/* routing */
.route-list, .rule-list { list-style: none; margin: 0; padding: 0; }
.route-item, .rule-item { display: flex; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.route-item:last-child, .rule-item:last-child { border-bottom: 0; }
.route-item { align-items: center; justify-content: space-between; }
.route-info { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.route-name { padding: 0; border: 0; background: none; color: var(--text); font: inherit; font-weight: 600; text-align: left; cursor: pointer; overflow-wrap: anywhere; }
.route-name:hover { text-decoration: underline; }
.route-item.active .route-name { color: var(--accent); }
.route-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 6px; }
.route-actions, .rule-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.editor-head { display: flex; align-items: center; flex-wrap: wrap; gap: 8px; margin-bottom: 14px; }
.editor-head .view-title { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.rule-item { align-items: flex-start; }
.rule-item.disabled .rule-main { opacity: 0.6; }
.rule-num { flex: none; min-width: 22px; color: var(--text-muted); font-variant-numeric: tabular-nums; text-align: right; }
.rule-main { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 2px; }
.rule-name { display: flex; align-items: center; gap: 6px; min-width: 0; font-weight: 600; }
.rule-sum { overflow-wrap: anywhere; }
.rule-actions .toggle { margin-right: 6px; }
.rule-section { margin-bottom: 6px; }
.attr-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.attr-head label { font-weight: 600; }
.attr-bool { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
.add-cond { margin-bottom: 14px; }
@media (max-width: 559px) {
  .route-item { flex-wrap: wrap; }
  .route-actions { margin-left: auto; }
  .rule-item { flex-wrap: wrap; }
  .rule-actions { width: 100%; justify-content: flex-end; }
}

/* settings */
.settings .card { margin-bottom: 14px; }
.fields { display: grid; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); gap: 0 24px; }
.fields .field { margin-bottom: 12px; }
.fields .switch-row { padding: 0; min-height: 34px; }
.has-savebar { padding-bottom: 72px; }
.savebar {
  position: sticky; bottom: 12px; z-index: 15; display: flex; align-items: center; justify-content: space-between; flex-wrap: wrap; gap: 10px;
  padding: 10px 12px; border-radius: var(--radius); border: 1px solid var(--border-strong); background: var(--surface); box-shadow: var(--shadow-pop);
}
.savebar .notice { margin: 0; flex: 1 1 260px; }
.savebar-actions { display: flex; gap: 8px; margin-left: auto; }
@media (max-width: 899px) {
  .savebar { bottom: calc(var(--nav-h) + 10px + env(safe-area-inset-bottom)); }
}
.file-list { list-style: none; margin: 0; padding: 0; }
.file-list li { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; border-bottom: 1px solid var(--border); }
.file-list li:last-child { border-bottom: 0; }
.file-info { display: flex; flex-direction: column; min-width: 0; }

/* system */
.fingerprint { display: flex; align-items: flex-start; gap: 6px; }
.fingerprint code { overflow-wrap: anywhere; font-size: 11.5px; }

/* login */
.login { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--surface); border: 1px solid var(--border); border-radius: 14px; padding: 24px; box-shadow: var(--shadow-pop); }
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 20px; }
.login-brand h1 { font-size: 22px; }
.login-card .btn.block { margin: 4px 0 14px; }

/* phone overrides that must follow the base rules above */
@media (max-width: 559px) {
  .sort { max-width: 150px; }
  .group-bar { scrollbar-width: none; }
  .group-bar::-webkit-scrollbar { display: none; }
  .group-head { gap: 6px; }
}
