@font-face {
  font-family: "Archivo";
  src: url("/fonts/archivo-wdth.woff2") format("woff2");
  font-weight: 100 900;
  font-stretch: 62% 125%;
  font-display: swap;
}

:root {
  color-scheme: light dark;
  --enamel: #e8eae4;
  --panel: #f7f8f4;
  --raised: #ffffff;
  --ink: #1c2327;
  --ink-2: #56615f;
  --line: #c9cec6;
  --lamp: #f0a500;
  --lamp-glow: rgba(240, 165, 0, 0.4);
  --fault: #b83a2d;
  --ok: #2b7a53;
  --focus: #1d5fd6;
  --hatch: rgba(28, 35, 39, 0.08);
  --rocker-body: #1c2327;
  --rocker-lamp-off: #4d5659;
  --paddle: #f7f8f4;
  --radius: 12px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --tabbar: 64px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --enamel: #14181a;
    --panel: #1d2326;
    --raised: #252c30;
    --ink: #e7eae5;
    --ink-2: #9aa5a1;
    --line: #333d40;
    --lamp: #ffb81f;
    --lamp-glow: rgba(255, 184, 31, 0.35);
    --fault: #f27a6a;
    --ok: #62c493;
    --focus: #7aa7ff;
    --hatch: rgba(231, 234, 229, 0.07);
    --rocker-body: #0c0f10;
    --rocker-lamp-off: #3a4245;
    --paddle: #d6dbd5;
  }
}

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

html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--enamel);
  color: var(--ink);
  font-family: "Archivo", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-stretch: 100%;
  font-size: 1rem;
  line-height: 1.4;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
.btn:active, .tab:active, .tile-info:active, .rocker-btn:active, .seg button:active { background: var(--enamel); }
.btn.primary:active { background: var(--ink); opacity: 0.85; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

h1, h2, h3 { margin: 0; line-height: 1.2; }

/* ---------- shell ---------- */

.shell { min-height: 100dvh; display: flex; flex-direction: column; }

.appbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: calc(10px + env(safe-area-inset-top, 0px)) 16px 10px;
  background: var(--enamel);
  border-bottom: 1px solid var(--line);
}
.brand { font-size: 1.125rem; font-weight: 700; font-stretch: 114%; letter-spacing: 0.005em; }
.conn { margin-left: auto; display: flex; align-items: center; gap: 8px; font-size: 0.8125rem; color: var(--ink-2); }
.account-btn {
  width: 38px; height: 38px; border-radius: 50%;
  border: 1px solid var(--line); background: var(--raised);
  font-weight: 700; font-stretch: 110%; text-transform: uppercase;
}

.lamp-dot {
  width: 10px; height: 10px; border-radius: 50%; flex: none;
  background: var(--rocker-lamp-off);
}
.lamp-dot.ok { background: var(--ok); }
.lamp-dot.warn { background: var(--lamp); }
.lamp-dot.fault { background: var(--fault); }

.tabs {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 6;
  display: flex;
  height: calc(var(--tabbar) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--panel);
  border-top: 1px solid var(--line);
}
.tab {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  border: 0; background: none; color: var(--ink-2); font-size: 0.75rem; font-weight: 600;
}
.tab svg { width: 22px; height: 22px; }
.tab[aria-current="page"] { color: var(--ink); box-shadow: inset 0 3px 0 var(--lamp); }

main { width: 100%; max-width: 1120px; margin: 0 auto; padding: 16px 16px 32px; }
.shell.has-tabs main { padding-bottom: calc(var(--tabbar) + var(--safe-bottom) + 28px); }

@media (min-width: 720px) {
  .tabs {
    position: static; height: auto; padding: 0 16px; background: none; border-top: 0;
    border-bottom: 1px solid var(--line); gap: 4px;
  }
  .tab { flex: none; flex-direction: row; gap: 8px; padding: 12px 14px; font-size: 0.9375rem; }
  .tab[aria-current="page"] { box-shadow: inset 0 -3px 0 var(--lamp); }
  .shell.has-tabs main { padding-bottom: 32px; }
  main { padding-top: 24px; }
}

/* ---------- buttons and forms ---------- */

.btn {
  min-height: 44px; padding: 0 16px; border-radius: 10px;
  border: 1px solid var(--line); background: var(--raised); color: var(--ink);
  font-weight: 600; font-size: 0.9375rem;
}
.btn:hover { border-color: var(--ink-2); }
.btn.primary { background: var(--ink); border-color: var(--ink); color: var(--enamel); }
.btn.danger { color: var(--fault); border-color: currentColor; }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 0.875rem; }
.btn:disabled { opacity: 0.5; cursor: default; }

.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field > label, .field > .label { font-weight: 600; font-size: 0.875rem; }
input[type="text"], input[type="password"], input[type="number"], input[type="search"], select, textarea {
  width: 100%; min-height: 44px; padding: 8px 12px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--raised);
  font-size: 1rem; /* 16px stops iOS zooming into fields */
}
textarea { min-height: 88px; resize: vertical; }
.hint { font-size: 0.8125rem; color: var(--ink-2); }
.err { color: var(--fault); font-weight: 600; font-size: 0.9375rem; margin: 0 0 12px; }
.check { display: flex; align-items: center; gap: 10px; min-height: 36px; font-weight: 500; }
.check input { width: 20px; height: 20px; accent-color: var(--ink); flex: none; }
.check-list { display: grid; gap: 2px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; background: var(--raised); max-height: 220px; overflow: auto; }

/* ---------- sign in ---------- */

.login { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.login-card { width: 100%; max-width: 380px; }
.login-card img { width: 72px; height: 72px; border-radius: 16px; display: block; margin-bottom: 20px; }
.login-card h1 { font-size: 1.75rem; font-weight: 700; font-stretch: 118%; margin-bottom: 6px; }
.login-card p.lead { margin: 0 0 24px; color: var(--ink-2); }
.login-card .btn { width: 100%; }

/* ---------- board ---------- */

.summary { display: flex; flex-wrap: wrap; gap: 6px 28px; margin-bottom: 14px; color: var(--ink-2); }
.summary strong { color: var(--ink); font-weight: 700; font-stretch: 108%; font-size: 1.125rem; }

.tools { display: flex; flex-wrap: wrap; gap: 10px; margin-bottom: 18px; }
.tools input[type="search"] { flex: 1 1 220px; }
.seg { display: flex; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; background: var(--raised); }
.seg button { min-height: 44px; padding: 0 14px; border: 0; background: none; font-weight: 600; font-size: 0.875rem; color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button[aria-pressed="true"] { background: var(--ink); color: var(--enamel); }

.banner {
  margin-bottom: 14px; padding: 10px 14px; border: 1px solid var(--fault); border-radius: 10px;
  color: var(--fault); font-weight: 600;
}

.site { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 18px; overflow: hidden; }
.site-head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px; padding: 14px 16px 12px; }
.site-name { font-size: 1.1875rem; font-weight: 650; font-stretch: 108%; }
.site-where { color: var(--ink-2); font-size: 0.9375rem; }
.site-count { margin-left: auto; color: var(--ink-2); font-size: 0.875rem; }
.site-note { padding: 0 16px 12px; color: var(--ink-2); font-size: 0.875rem; margin: 0; }

.device { border-top: 1px solid var(--line); padding: 12px 16px 16px; }
.device-head { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; margin-bottom: 12px; }
.device-name { font-weight: 650; font-stretch: 104%; }
.device-status { display: flex; align-items: center; gap: 8px; font-size: 0.875rem; color: var(--ink-2); min-width: 0; }
.device-actions { margin-left: auto; display: flex; gap: 8px; }

.channels { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 280px), 1fr)); gap: 10px; }

.tile {
  display: flex; min-height: 76px; position: relative; overflow: hidden;
  background: var(--raised); border: 1px solid var(--line); border-radius: 10px;
}
.tile.on { border-color: var(--lamp); box-shadow: inset 5px 0 0 var(--lamp); }
.tile.nocontact {
  color: var(--ink-2);
  background-image: repeating-linear-gradient(135deg, transparent 0 9px, var(--hatch) 9px 10px);
}
.tile-info {
  flex: 1; min-width: 0; display: flex; flex-direction: column; justify-content: center; gap: 3px;
  padding: 10px 12px 10px 18px; text-align: left; background: none; border: 0;
}
.tile-label { font-weight: 600; font-size: 1.0625rem; overflow-wrap: anywhere; line-height: 1.25; }
.tile-meta { display: flex; flex-wrap: wrap; gap: 0 12px; font-size: 0.8125rem; color: var(--ink-2); }
.tile-state { font-weight: 700; color: var(--ink); }
.tile.nocontact .tile-state { color: var(--ink-2); }
.tile-lock { display: inline-block; font-weight: 600; }

.rocker-btn {
  width: 80px; flex: none; display: grid; place-items: center;
  background: none; border: 0; border-left: 1px solid var(--line);
}
.rocker-btn:disabled { cursor: default; }
.rocker { position: relative; width: 42px; height: 60px; border-radius: 9px; background: var(--rocker-body); }
.rocker .lamp { position: absolute; top: 7px; left: 50%; width: 9px; height: 9px; margin-left: -4.5px; border-radius: 50%; background: var(--rocker-lamp-off); }
.rocker .paddle {
  position: absolute; left: 5px; right: 5px; bottom: 5px; height: 29px; border-radius: 5px;
  background: var(--paddle); box-shadow: inset 0 -3px 0 rgba(0, 0, 0, 0.16);
  transition: transform 0.14s ease-out;
}
.rocker-btn[aria-checked="true"] .lamp { background: var(--lamp); box-shadow: 0 0 9px 3px var(--lamp-glow); }
.rocker-btn[aria-checked="true"] .paddle { transform: translateY(-8px); }
.tile.nocontact .rocker { opacity: 0.4; }
.rocker-btn[aria-busy="true"] .paddle { animation: nudge 0.5s ease-in-out infinite alternate; }
@keyframes nudge { to { transform: translateY(-4px); } }

.empty { padding: 40px 8px; text-align: center; color: var(--ink-2); }
.empty strong { display: block; color: var(--ink); font-size: 1.0625rem; margin-bottom: 6px; }

/* ---------- manage, people, log ---------- */

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 16px; }
.page-head h2 { font-size: 1.25rem; font-weight: 700; font-stretch: 110%; }

.rows { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; margin-bottom: 18px; }
.row { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 14px; padding: 12px 16px; }
.row + .row { border-top: 1px solid var(--line); }
.row .grow { flex: 1 1 200px; min-width: 0; }
.row .title { font-weight: 600; overflow-wrap: anywhere; }
.row .sub { font-size: 0.875rem; color: var(--ink-2); overflow-wrap: anywhere; }
.row .btns { display: flex; flex-wrap: wrap; gap: 8px; }
.row.head { background: var(--enamel); }
.row.head .title { font-size: 1.0625rem; font-stretch: 106%; font-weight: 650; }
.tag { display: inline-block; padding: 1px 8px; border: 1px solid var(--line); border-radius: 999px; font-size: 0.8125rem; font-weight: 600; color: var(--ink-2); }
.tag.fault { color: var(--fault); border-color: currentColor; }

.log-row { display: grid; gap: 2px; padding: 11px 16px; }
.log-row + .log-row { border-top: 1px solid var(--line); }
.log-top { display: flex; flex-wrap: wrap; gap: 2px 12px; align-items: baseline; }
.log-what { font-weight: 650; }
.log-who { color: var(--ink-2); font-size: 0.875rem; }
.log-when { margin-left: auto; color: var(--ink-2); font-size: 0.8125rem; font-variant-numeric: tabular-nums; }
.log-detail { color: var(--ink-2); font-size: 0.875rem; overflow-wrap: anywhere; }
.log-row.failed .log-what { color: var(--fault); }

/* ---------- sheets (dialogs) and toasts ---------- */

dialog.sheet {
  width: 100%; max-width: 520px; max-height: 92dvh; margin: auto auto 0; padding: 0; overflow: auto;
  border: 0; border-radius: 18px 18px 0 0; background: var(--panel); color: var(--ink);
}
dialog.sheet::backdrop { background: rgba(8, 12, 14, 0.6); }
@media (min-width: 720px) { dialog.sheet { margin: auto; border-radius: 16px; } }
.sheet-in { padding: 20px 20px calc(20px + var(--safe-bottom)); }
.sheet h3 { font-size: 1.1875rem; font-weight: 650; font-stretch: 108%; margin-bottom: 4px; }
.sheet .lead { color: var(--ink-2); margin: 0 0 18px; font-size: 0.9375rem; }
.sheet .actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: flex-end; margin-top: 6px; }
@media (max-width: 719px) { .sheet .actions .btn { flex: 1 1 120px; } }
.sheet .section { margin-top: 18px; padding-top: 16px; border-top: 1px solid var(--line); }
.sheet .section h4 { margin: 0 0 10px; font-size: 0.9375rem; font-weight: 650; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; }
.inline { display: flex; gap: 10px; }
.inline input { flex: 1; min-width: 0; }
.test-result { margin: 0 0 14px; font-weight: 600; font-size: 0.9375rem; }
.test-result.ok { color: var(--ok); }
.test-result.bad { color: var(--fault); }
.chan-edit { display: grid; grid-template-columns: 1fr auto; gap: 8px 12px; align-items: center; padding: 8px 0; }
.chan-edit + .chan-edit { border-top: 1px solid var(--line); }
.chan-edit .num { grid-column: 1 / -1; font-size: 0.8125rem; color: var(--ink-2); font-weight: 600; }
.chan-edit .check { font-size: 0.875rem; white-space: nowrap; }

#toasts {
  position: fixed; left: 12px; right: 12px; z-index: 30; display: grid; gap: 8px; justify-items: center;
  bottom: calc(var(--tabbar) + var(--safe-bottom) + 12px); pointer-events: none;
}
body.no-tabs #toasts { bottom: calc(var(--safe-bottom) + 16px); }
.toast {
  max-width: 440px; padding: 12px 16px; border-radius: 10px; pointer-events: auto;
  background: var(--ink); color: var(--enamel); font-weight: 600; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25);
}
.toast.error { background: var(--fault); color: #fff; }
@media (min-width: 720px) { #toasts, body.no-tabs #toasts { bottom: 20px; } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* two-step sign-in */
.login-card.wide { max-width: 440px; }
.login-card .btn.quiet { margin-top: 10px; background: transparent; border-color: transparent; color: var(--ink-2); }
.code-input { font-size: 1.5rem; letter-spacing: 0.3em; text-align: center; font-variant-numeric: tabular-nums; }
.steps { margin: 0 0 14px; padding-left: 1.4em; color: var(--ink-2); }
.steps li { margin-bottom: 6px; }
.enroll-key { display: grid; justify-items: center; gap: 8px; margin: 4px 0 18px; text-align: center; }
.qr { background: #fff; border-radius: 10px; padding: 6px; border: 1px solid var(--line); }
.secret { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 1rem; letter-spacing: 0.08em; word-break: normal; padding: 8px 12px; background: var(--raised); border: 1px solid var(--line); border-radius: 8px; user-select: all; }
.recovery { list-style: none; margin: 0 0 20px; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.recovery li { padding: 10px 12px; background: var(--raised); border: 1px solid var(--line); border-radius: 8px; text-align: center; }
.recovery code { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace; font-size: 1.0625rem; user-select: all; }
.login-card .actions { display: grid; gap: 10px; }
.login-card img.qr { width: 208px; height: 208px; margin: 0; border-radius: 10px; }
.secret { font-size: 0.9375rem; letter-spacing: 0.04em; text-align: center; }
