/* Design tokens. Both themes are defined up front so the page follows the
   viewer's system setting without a flash of the wrong one. */
:root {
  color-scheme: light dark;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f0f2f5;
  --border: #e2e5ea;
  --text: #16181d;
  --text-dim: #666e7a;
  --accent: #1d4ed8;
  --accent-soft: #e8efff;
  --good: #15803d;
  --good-soft: #e6f5ec;
  --warn: #b45309;
  --warn-soft: #fdf1e0;
  --bad: #b91c1c;
  --bad-soft: #fdecec;
  --shadow: 0 1px 2px rgb(16 24 40 / 6%), 0 8px 24px rgb(16 24 40 / 8%);
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0e1116;
    --surface: #161a21;
    --surface-2: #1d222b;
    --border: #272d38;
    --text: #e8eaee;
    --text-dim: #99a1ad;
    --accent: #6f9bff;
    --accent-soft: #1a2439;
    --good: #4ade80;
    --good-soft: #14281d;
    --warn: #fbbf24;
    --warn-soft: #2a2213;
    --bad: #f87171;
    --bad-soft: #2d1717;
    --shadow: 0 1px 2px rgb(0 0 0 / 40%), 0 8px 24px rgb(0 0 0 / 35%);
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 ui-sans-serif, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}

svg { width: 18px; height: 18px; fill: none; stroke: currentColor;
      stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* --- Top bar --------------------------------------------------------------- */

.bar {
  position: sticky; top: 0; z-index: 20;
  display: flex; flex-wrap: wrap; gap: 14px;
  align-items: center; justify-content: space-between;
  padding: 14px 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
}

.bar-title { display: flex; align-items: center; gap: 14px; }
h1 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }

.status {
  display: inline-flex; align-items: center; gap: 7px;
  font-size: 12px; font-weight: 600;
  padding: 4px 10px; border-radius: 999px;
  background: var(--surface-2); color: var(--text-dim);
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: currentColor; }
.status[data-state="live"] { background: var(--good-soft); color: var(--good); }
.status[data-state="live"] .dot { animation: pulse 2s ease-in-out infinite; }
.status[data-state="lost"] { background: var(--bad-soft); color: var(--bad); }

@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.45; transform: scale(0.82); }
}

.bar-actions { display: flex; gap: 10px; align-items: center; flex: 1 1 340px;
               justify-content: flex-end; }

.search { position: relative; flex: 1 1 260px; max-width: 420px; }
.search svg { position: absolute; left: 11px; top: 50%; transform: translateY(-50%);
              color: var(--text-dim); }
.search input {
  width: 100%; padding: 9px 12px 9px 36px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 9px;
  font: inherit; font-size: 14px;
}
.search input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

button {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 9px 14px; border-radius: 9px;
  border: 1px solid var(--border); background: var(--surface-2);
  color: var(--text); font: inherit; font-size: 14px; font-weight: 600;
  cursor: pointer;
}
button:hover { border-color: var(--accent); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { filter: brightness(1.08); }
button.danger { background: var(--bad-soft); color: var(--bad); border-color: transparent; }
button.icon { padding: 7px; border: 0; background: transparent; color: var(--text-dim); }

/* --- Stats ----------------------------------------------------------------- */

.stats { display: flex; flex-wrap: wrap; gap: 10px; padding: 16px 20px 0; }
.stat {
  flex: 1 1 150px; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius);
}
.stat b { display: block; font-size: 22px; letter-spacing: -0.02em; }
.stat span { font-size: 12px; color: var(--text-dim); }
.stat[data-tone="warn"] b { color: var(--warn); }
.stat[data-tone="bad"] b { color: var(--bad); }

/* --- Table ----------------------------------------------------------------- */

main { padding: 16px 20px 48px; }

.table-wrap {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow);
}

table { width: 100%; border-collapse: collapse; }

th {
  position: sticky; top: 63px;
  text-align: left; font-size: 11px; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--text-dim);
  padding: 11px 14px; background: var(--surface-2);
  border-bottom: 1px solid var(--border); white-space: nowrap;
}

td { padding: 12px 14px; border-bottom: 1px solid var(--border); font-size: 14px; }
tbody tr { cursor: pointer; }
tbody tr:hover { background: var(--surface-2); }
tbody tr:last-child td { border-bottom: 0; }

.name { font-weight: 600; }
.mono { font-variant-numeric: tabular-nums;
        font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 13px; }
.muted { color: var(--text-dim); }

.pill {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 3px 9px; border-radius: 999px;
  font-size: 11px; font-weight: 700; white-space: nowrap;
}
.pill[data-tone="good"] { background: var(--good-soft); color: var(--good); }
.pill[data-tone="warn"] { background: var(--warn-soft); color: var(--warn); }
.pill[data-tone="bad"]  { background: var(--bad-soft);  color: var(--bad); }
.pill[data-tone="flat"] { background: var(--surface-2); color: var(--text-dim); }

/* A row that just arrived, so the eye catches it without needing to re-read
   the whole list. */
@keyframes arrive {
  from { background: var(--accent-soft); }
  to   { background: transparent; }
}
tr.new { animation: arrive 2.4s ease-out; }

.empty { padding: 72px 24px; text-align: center; }
.empty-icon { font-size: 44px; }
.empty h2 { margin: 14px 0 6px; font-size: 17px; }
.empty p { margin: 0 auto; max-width: 42ch; color: var(--text-dim); font-size: 14px; }

/* --- Detail drawer --------------------------------------------------------- */

.drawer {
  position: fixed; inset: 0 0 0 auto; z-index: 40;
  width: min(440px, 100%);
  display: flex; flex-direction: column;
  background: var(--surface); border-left: 1px solid var(--border);
  box-shadow: var(--shadow); overflow-y: auto;
  animation: slide 0.18s ease-out;
}
@keyframes slide { from { transform: translateX(100%); } }

.drawer-head {
  position: sticky; top: 0;
  display: flex; align-items: flex-start; justify-content: space-between; gap: 12px;
  padding: 18px 20px; background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.drawer-head h2 { margin: 0; font-size: 17px; }
.drawer-head p { margin: 4px 0 0; font-size: 13px; }

.drawer dl { margin: 0; padding: 8px 20px 20px; }
.drawer dt {
  font-size: 11px; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-dim); margin-top: 16px;
}
.drawer dd {
  margin: 3px 0 0; font-size: 15px; font-weight: 600;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.drawer dd.missing { font-weight: 400; color: var(--text-dim); }

.drawer-foot { margin-top: auto; padding: 16px 20px; border-top: 1px solid var(--border); }

.scrim { position: fixed; inset: 0; z-index: 30; background: rgb(0 0 0 / 35%); }

/* --- Toasts ---------------------------------------------------------------- */

.toasts { position: fixed; bottom: 20px; left: 20px; z-index: 60;
          display: flex; flex-direction: column; gap: 8px; }
.toast {
  padding: 11px 15px; border-radius: 10px;
  background: var(--surface); border: 1px solid var(--border);
  box-shadow: var(--shadow); font-size: 14px; font-weight: 600;
  animation: rise 0.2s ease-out;
}
.toast[data-tone="good"] { border-color: var(--good); color: var(--good); }
.toast[data-tone="bad"] { border-color: var(--bad); color: var(--bad); }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } }

@media (max-width: 720px) {
  th:nth-child(4), td:nth-child(4),
  th:nth-child(8), td:nth-child(8) { display: none; }
  .bar { padding: 12px 14px; }
  main { padding: 12px 14px 40px; }
  .stats { padding: 12px 14px 0; }
}

/* --- Pairing panel --------------------------------------------------------- */

.pairing {
  margin: 16px 20px 0;
  padding: 18px 20px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

/* Before a phone joins, the code is the point of the page. Once one has, it
   becomes reference material and yields space to the list. */
.pairing[data-paired="true"] { padding: 12px 16px; }
.pairing[data-paired="true"] .pairing-copy p { display: none; }
.pairing[data-paired="true"] .pairing-main { align-items: center; }
.pairing[data-paired="true"] .code span {
  font-size: 20px; padding: 4px 9px; letter-spacing: 0;
}
.pairing[data-paired="true"] h2 { font-size: 14px; }

.pairing-main {
  display: flex; flex-wrap: wrap; gap: 20px;
  align-items: flex-start; justify-content: space-between;
}
.pairing-copy h2 { margin: 0 0 6px; font-size: 17px; }
.pairing-copy p { margin: 0; max-width: 46ch; font-size: 14px; }

.code { display: flex; gap: 8px; cursor: pointer; user-select: all; }
.code span {
  display: grid; place-items: center;
  min-width: 46px; padding: 10px 12px;
  background: var(--accent-soft); color: var(--accent);
  border-radius: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 30px; font-weight: 700; line-height: 1;
  font-variant-numeric: tabular-nums;
}
.code:hover span { filter: brightness(0.97); }

.pairing-actions {
  display: flex; flex-wrap: wrap; gap: 8px;
  align-items: center; margin-top: 14px;
}
.pairing-actions .spacer { flex: 1; }
.devices { display: flex; flex-wrap: wrap; gap: 6px; }

button.ghost {
  background: transparent; border-color: var(--border);
  color: var(--text-dim); font-size: 13px; padding: 7px 12px;
}
button.ghost:hover { color: var(--accent); border-color: var(--accent); }

/* --- Dialog ---------------------------------------------------------------- */

.dialog {
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  padding: 26px;
  width: min(360px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgb(0 0 0 / 45%); }
.dialog h2 { margin: 0 0 6px; font-size: 18px; }
.dialog p { margin: 0 0 16px; font-size: 14px; }

.dialog input {
  width: 100%; padding: 12px 14px;
  background: var(--surface-2); color: var(--text);
  border: 1px solid var(--border); border-radius: 10px;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 26px; font-weight: 700; text-align: center;
  letter-spacing: 0.3em; text-indent: 0.3em;
}
.dialog input:focus { outline: 2px solid var(--accent); outline-offset: -1px; }

.dialog-actions {
  display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px;
}

.alert {
  margin: 14px 0 0; padding: 10px 12px;
  background: var(--bad-soft); color: var(--bad);
  border-radius: 9px; font-size: 13px; font-weight: 600;
}

@media (max-width: 720px) {
  .pairing { margin: 12px 14px 0; padding: 14px; }
  .code span { min-width: 38px; font-size: 24px; padding: 8px 9px; }
}
