:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f7f8fa;
  --text: #0f172a;
  --muted: #64748b;
  --border: #e3e8ef;
  --accent: #0f766e;
  --accent-hover: #115e59;
  --accent-contrast: #ffffff;
  --accent-soft: #ccfbf1;
  --ok: #15803d;
  --ok-soft: #dcfce7;
  --warn: #b45309;
  --warn-soft: #fef3c7;
  --danger: #b91c1c;
  --danger-soft: #fee2e2;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .05), 0 2px 8px rgba(15, 23, 42, .05);
  --font: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, "SF Mono", SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light dark;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1016;
    --surface: #121a23;
    --surface-2: #17212c;
    --text: #e6edf5;
    --muted: #93a3b8;
    --border: #223040;
    --accent: #2dd4bf;
    --accent-hover: #5eead4;
    --accent-contrast: #042f2e;
    --accent-soft: rgba(45, 212, 191, .14);
    --ok: #4ade80;
    --ok-soft: rgba(74, 222, 128, .14);
    --warn: #fbbf24;
    --warn-soft: rgba(251, 191, 36, .14);
    --danger: #f87171;
    --danger-soft: rgba(248, 113, 113, .14);
    --shadow: none;
  }
}

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

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

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

h1, h2 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 650; }
p { margin: 0; }
.muted { color: var(--muted); }
.mono { font-family: var(--mono); font-size: .92em; }
strong { font-weight: 600; }

/* ---------------------------------------------------------------- buttons */

.btn {
  appearance: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 550;
  text-decoration: none;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
  -webkit-tap-highlight-color: transparent;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .55; cursor: default; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.btn.primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); }
.btn.ghost { background: transparent; border-color: transparent; color: var(--muted); }
.btn.ghost:hover { color: var(--text); background: var(--surface-2); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); border-color: transparent; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 14px; }
.btn.block { width: 100%; }

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

/* ----------------------------------------------------------------- fields */

.field { display: grid; gap: 6px; }
.field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
input[type="text"], input[type="password"], input:not([type]) {
  width: 100%;
  min-height: 44px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 16px; /* stops iOS zooming on focus */
}
input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; }
legend { padding: 0; margin-bottom: 8px; font-size: 13px; font-weight: 600; color: var(--muted); }
.form-error { color: var(--danger); font-size: 14px; min-height: 1em; }
.form-error:empty { display: none; }

/* ------------------------------------------------------------------ login */

.login {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.login-card {
  width: 100%;
  max-width: 360px;
  display: grid;
  gap: 16px;
  padding: 28px 24px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-card .field { text-align: left; }
.login-icon { margin: 0 auto; border-radius: 14px; }

/* ----------------------------------------------------------------- layout */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px max(16px, env(safe-area-inset-left)) 10px max(16px, env(safe-area-inset-right));
  padding-top: max(10px, env(safe-area-inset-top));
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(1.4) blur(12px);
  -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 650; }
.brand img { border-radius: 7px; }

.wrap {
  max-width: 860px;
  margin: 0 auto;
  padding: 16px 16px calc(32px + env(safe-area-inset-bottom));
  display: grid;
  gap: 16px;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 18px;
  display: grid;
  gap: 14px;
  min-width: 0;
}
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.foot { text-align: center; font-size: 13px; padding: 4px 0; }

/* ------------------------------------------------------------------- hero */

.hero-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; }
.eyebrow { font-size: 12px; font-weight: 650; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.hero-state { display: flex; align-items: center; gap: 10px; font-size: 26px; font-weight: 700; margin: 2px 0 2px; }
.dot { width: 12px; height: 12px; border-radius: 50%; background: var(--muted); flex: none; }
.dot.ok { background: var(--ok); box-shadow: 0 0 0 5px var(--ok-soft); }
.dot.warn { background: var(--warn); box-shadow: 0 0 0 5px var(--warn-soft); }
.dot.bad { background: var(--danger); box-shadow: 0 0 0 5px var(--danger-soft); }

.stats {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 1px;
  margin: 0;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.stats > div { background: var(--surface-2); padding: 10px 12px; min-width: 0; }
.stats dt { font-size: 12px; color: var(--muted); }
.stats dd { margin: 2px 0 0; font-weight: 650; font-variant-numeric: tabular-nums; overflow-wrap: anywhere; }
.stats dd.mono { font-size: 13px; line-height: 1.7; overflow-wrap: normal; }

/* switch */
.switch { position: relative; flex: none; cursor: pointer; }
.switch input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.switch .track {
  display: block;
  width: 56px;
  height: 32px;
  border-radius: 999px;
  background: var(--border);
  transition: background .2s;
  position: relative;
}
.switch .thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 26px;
  height: 26px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .2s;
}
.switch input:checked + .track { background: var(--ok); }
.switch input:checked + .track .thumb { transform: translateX(24px); }
.switch input:focus-visible + .track { outline: 2px solid var(--accent); outline-offset: 2px; }
.switch input:disabled + .track { opacity: .5; }

/* ------------------------------------------------------------ this device */

.you { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 10px; }
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  background: var(--surface-2);
  color: var(--muted);
}
.badge.ok { background: var(--ok-soft); color: var(--ok); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.speed { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 12px; }

/* --------------------------------------------------------------- devices */

.devices { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.device {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 4px 12px;
  align-items: center;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-2);
}
.device.off { opacity: .6; }
.device-icon {
  grid-row: span 2;
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  color: var(--accent);
}
.device-icon svg { width: 22px; height: 22px; }
.device-name { font-weight: 650; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.device-meta { font-size: 13px; color: var(--muted); overflow-wrap: anywhere; }
.device-state { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 6px; justify-self: end; white-space: nowrap; }
.device-state .dot { width: 8px; height: 8px; box-shadow: none; }
.device-actions { grid-column: 2 / -1; display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
.empty { color: var(--muted); text-align: center; padding: 16px 0 6px; }

/* ------------------------------------------------------------------ guide */

.tabs { display: flex; gap: 6px; flex-wrap: wrap; }
.tabs button {
  appearance: none;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--muted);
  border-radius: 999px;
  padding: 6px 14px;
  font: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}
.tabs button[aria-selected="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-contrast); }
.steps { margin: 0; padding-left: 22px; display: grid; gap: 8px; }
.steps li::marker { color: var(--muted); font-weight: 600; }
.steps .tip { list-style: none; margin-left: -22px; font-size: 14px; color: var(--muted); padding: 10px 12px; background: var(--surface-2); border-radius: 10px; }

.grid-form { display: grid; gap: 12px; max-width: 420px; }

/* ---------------------------------------------------------------- dialogs */

dialog {
  width: min(560px, calc(100vw - 24px));
  max-height: calc(100dvh - 24px);
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 18px;
  background: var(--surface);
  color: var(--text);
  box-shadow: 0 20px 60px rgba(0, 0, 0, .25);
}
dialog::backdrop { background: rgba(8, 12, 18, .5); backdrop-filter: blur(2px); }
.sheet { display: grid; gap: 16px; padding: 18px; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }

.chips { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 8px; }
.chips label { position: relative; cursor: pointer; }
.chips input { position: absolute; opacity: 0; }
.chips span {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  border: 1px solid var(--border);
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
}
.chips svg { width: 22px; height: 22px; }
.chips input:checked + span { border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.chips input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.choices { display: grid; gap: 8px; }
.choice {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  cursor: pointer;
}
.choice:has(input:checked) { border-color: var(--accent); background: var(--accent-soft); }
.choice input { margin: 3px 0 0; accent-color: var(--accent); }
.choice span { display: grid; gap: 2px; }
.choice small { color: var(--muted); font-size: 13px; }

.config-body { display: grid; grid-template-columns: auto minmax(0, 1fr); gap: 16px; align-items: start; }
.qr-wrap { background: #fff; padding: 10px; border-radius: 12px; border: 1px solid var(--border); line-height: 0; }
.qr-wrap img { width: 220px; height: 220px; image-rendering: pixelated; }
.config-side { display: grid; gap: 12px; }
.fine { font-size: 13px; color: var(--muted); }

.segmented { display: grid; grid-template-columns: 1fr 1fr; padding: 3px; gap: 3px; background: var(--surface-2); border: 1px solid var(--border); border-radius: 10px; }
.segmented label { position: relative; cursor: pointer; }
.segmented input { position: absolute; opacity: 0; }
.segmented span { display: block; text-align: center; padding: 6px 8px; border-radius: 8px; font-size: 14px; font-weight: 600; color: var(--muted); }
.segmented input:checked + span { background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); }

/* ------------------------------------------------------------------ toast */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  max-width: calc(100vw - 32px);
  padding: 10px 16px;
  border-radius: 12px;
  background: var(--text);
  color: var(--bg);
  font-size: 14px;
  font-weight: 550;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s, transform .2s;
  z-index: 20;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.error { background: var(--danger); color: #fff; }

/* ----------------------------------------------------------------- mobile */

@media (max-width: 640px) {
  .stats { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .hero-state { font-size: 22px; }
  .config-body { grid-template-columns: 1fr; justify-items: center; }
  .config-side { width: 100%; }
  .device-actions { grid-column: 1 / -1; }
  dialog { width: calc(100vw - 16px); }
}

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