:root {
  --bg: #f4f6fa;
  --surface: #ffffff;
  --text: #111827;
  --muted: #5b6474;
  --border: #e2e6ee;
  --accent: #2563eb;
  --on-accent: #ffffff;
  --p2p: #15803d;      --p2p-bg: #dcfce7;
  --lan: #0e7490;      --lan-bg: #cffafe;
  --relay: #c2410c;    --relay-bg: #ffedd5;
  --wait: #6d28d9;     --wait-bg: #ede9fe;
  --error: #b91c1c;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #121b2f;
    --text: #e6e9ef;
    --muted: #97a3b6;
    --border: #22304d;
    --accent: #60a5fa;
    --on-accent: #0b1120;
    --p2p: #4ade80;    --p2p-bg: #133a22;
    --lan: #22d3ee;    --lan-bg: #0b3540;
    --relay: #fb923c;  --relay-bg: #3d2411;
    --wait: #c4b5fd;   --wait-bg: #2b2254;
    --error: #f87171;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
}

.bar {
  position: sticky;
  top: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: max(12px, env(safe-area-inset-top)) 16px 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; }
.brand img { width: 28px; height: 28px; border-radius: 7px; }

main { max-width: 720px; margin: 0 auto; padding: 16px 16px max(24px, env(safe-area-inset-bottom)); }

h1 { font-size: 20px; margin: 0; }
h2 {
  margin: 22px 0 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: .07em;
  text-transform: uppercase;
}

.card {
  display: grid;
  gap: 14px;
  max-width: 420px;
  margin: 32px auto;
  padding: 22px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
}
label { display: grid; gap: 6px; color: var(--muted); font-size: 14px; }

input {
  min-width: 0;
  padding: 11px 12px;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
}
button {
  padding: 10px 14px;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  font: inherit;
  cursor: pointer;
}
button:disabled { opacity: .6; cursor: default; }
button.primary { color: var(--on-accent); background: var(--accent); border-color: var(--accent); font-weight: 650; }
button.link { padding: 6px; color: var(--accent); background: none; border: 0; }
input:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.tabs button { flex: 1; color: var(--muted); background: none; border: 0; }
.tabs button[aria-selected="true"] { color: var(--text); background: var(--bg); font-weight: 650; }

.list { display: grid; gap: 8px; }
.session, .device, .empty {
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.empty { color: var(--muted); }
.session { display: flex; align-items: flex-start; gap: 12px; }
.session.live { border-left: 4px solid currentColor; }
.session.live.relay { color: var(--relay); }
.session.live.p2p { color: var(--p2p); }
.session.live.lan { color: var(--lan); }
.session.live.connecting { color: var(--wait); }

.badge {
  flex: none;
  min-width: 78px;
  padding: 4px 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 700;
  text-align: center;
}
.badge.p2p, .badge.direct { color: var(--p2p); background: var(--p2p-bg); }
.session.live.direct { color: var(--p2p); }
.session.direct .dot { background: var(--p2p); }
.dev-tools { display: flex; flex-wrap: wrap; gap: 8px; }
.section-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 22px 0 8px; }
.section-head h2 { margin: 0; }
button.small { padding: 6px 12px; font-size: 13px; }
button.danger { color: var(--error); }
button.danger.armed { color: #fff; background: var(--error); border-color: var(--error); font-weight: 650; }
.settings {
  display: grid;
  gap: 10px;
  padding: 12px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
}
.settings input:not([type="checkbox"]) { width: 100%; background: var(--surface); }
label.check { display: flex; align-items: center; gap: 8px; color: var(--text); }
.status.done { color: var(--p2p); }
.status.failed { color: var(--error); }
.status.waiting, .status.sent { color: var(--wait); }
.dev-title { display: flex; flex-wrap: wrap; align-items: center; gap: 4px 10px; min-width: 0; }
.dev-types { font-weight: 650; }
.dev-type.relay { color: var(--relay); }
.dev-type.p2p, .dev-type.direct { color: var(--p2p); }
.dev-type.lan { color: var(--lan); }
.dev-type.connecting { color: var(--wait); }
.in-use { color: var(--p2p); font-size: 13px; font-weight: 650; }
.in-use .dot { background: var(--p2p); }
.badge.lan { color: var(--lan); background: var(--lan-bg); }
.badge.relay { color: var(--relay); background: var(--relay-bg); }
.badge.connecting { color: var(--wait); background: var(--wait-bg); }

.who { flex: 1; min-width: 0; color: var(--text); }
.names { display: flex; flex-wrap: wrap; gap: 6px; font-weight: 650; overflow-wrap: anywhere; }
.arrow { color: var(--muted); font-weight: 400; }
.meta { color: var(--muted); font-size: 13px; overflow-wrap: anywhere; }

.dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  margin-right: 6px;
  border-radius: 50%;
  background: currentColor;
  vertical-align: 1px;
  animation: pulse 1.6s ease-in-out infinite;
}
.session.relay .dot { background: var(--relay); }
.session.connecting .dot { background: var(--wait); }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .dot { animation: none; } }

.meta.data { color: var(--text); font-variant-numeric: tabular-nums; }

.usage {
  display: grid;
  gap: 8px;
  margin-top: 16px;
  padding: 14px 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.usage-label { color: var(--muted); font-size: 13px; }
.usage-figure { font-variant-numeric: tabular-nums; }
.usage-figure strong { font-size: 24px; }
.meter { height: 8px; overflow: hidden; background: var(--bg); border-radius: 999px; }
.meter-fill { height: 100%; background: var(--p2p); border-radius: 999px; }
.meter-fill.warn { background: var(--relay); }

.device { display: grid; gap: 6px; }
.dev-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.dev-name { min-width: 0; font-size: 16px; overflow-wrap: anywhere; }
.dev-main { display: flex; gap: 8px; }
.dev-main input { flex: 1; }
a.button {
  display: inline-flex;
  align-items: center;
  padding: 10px 14px;
  border-radius: 10px;
  font-weight: 650;
  text-decoration: none;
}
a.button.connect { color: var(--on-accent); background: var(--accent); }
a.button.direct { color: var(--p2p); background: var(--p2p-bg); }
.dev-actions { display: flex; flex: none; gap: 6px; }
a.button:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.banner {
  margin-bottom: 12px;
  padding: 10px 14px;
  color: var(--relay);
  background: var(--relay-bg);
  border-radius: 12px;
  font-size: 14px;
}
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { min-height: 1.2em; margin: 0; color: var(--error); }

/* ---- compact layout ---- */
body { font-size: 14px; line-height: 1.4; }
.bar { padding: max(8px, env(safe-area-inset-top)) 12px 8px; }
.brand img { width: 24px; height: 24px; }
main { padding: 10px 12px max(16px, env(safe-area-inset-bottom)); }
h2 { margin: 14px 0 6px; font-size: 11px; }
.section-head { margin: 14px 0 6px; }
.tabs button { padding: 7px 10px; }
.list { gap: 6px; }
.session, .device, .empty { padding: 8px 10px; border-radius: 10px; }
.session { align-items: center; gap: 10px; }
.session.live { border-left-width: 3px; }
.badge { min-width: 54px; padding: 2px 6px; font-size: 11px; }
.names { gap: 4px; font-size: 14px; }
.meta { font-size: 12px; }
.when { flex: none; color: var(--muted); font-size: 12px; text-align: right; white-space: nowrap; }
.session.live .when { color: inherit; font-weight: 650; }
.usage { gap: 4px; margin-top: 10px; padding: 8px 10px; }
.usage-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.usage-figure strong { font-size: 15px; }
.meter { height: 6px; }
.device { gap: 6px; }
.dev-name { font-size: 15px; }
a.button { padding: 6px 12px; font-size: 13px; border-radius: 8px; }
button.small { padding: 4px 10px; font-size: 12px; border-radius: 8px; }
.rename { display: flex; flex: 1; gap: 6px; min-width: 0; }
.rename input { flex: 1; padding: 5px 8px; font-size: 14px; }
.settings { gap: 8px; padding: 10px; }
.settings input:not([type="checkbox"]) { padding: 7px 9px; }

.version { margin-left: 2px; padding: 1px 6px; color: var(--muted); border: 1px solid var(--border); border-radius: 999px; font-size: 11px; font-weight: 600; }
.ver { padding: 1px 6px; border-radius: 999px; font-size: 11px; font-weight: 650; }
.ver.ok { color: var(--p2p); background: var(--p2p-bg); }
.ver.old { color: var(--relay); background: var(--relay-bg); }
.badge.failed { color: var(--muted); background: var(--bg); border: 1px solid var(--border); }
