:root {
  color-scheme: light dark;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --border: #d7dee8;
  --text: #0f172a;
  --muted: #5b6779;
  --accent: #2563eb;
  --accent-text: #ffffff;
  --danger: #b91c1c;
  --ok: #15803d;
  --log-bg: #0f172a;
  --log-text: #e2e8f0;
  --radius: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1120;
    --surface: #131c30;
    --border: #26324a;
    --text: #e6ecf5;
    --muted: #94a3b8;
    --accent: #60a5fa;
    --accent-text: #07142b;
    --danger: #f87171;
    --ok: #4ade80;
    --log-bg: #060c18;
    --log-text: #cbd5e1;
  }
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 0 1rem env(safe-area-inset-bottom) 1rem;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.55 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

main {
  max-width: 46rem;
  margin: 0 auto;
  display: grid;
  gap: 1rem;
}

.topbar {
  max-width: 46rem;
  margin: 0 auto;
  padding: 1rem 0;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.topbar h1 {
  margin: 0;
  font-size: 1.25rem;
  letter-spacing: -0.01em;
}

footer {
  max-width: 46rem;
  margin: 1.5rem auto;
  text-align: center;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
}

.card + .card {
  margin-top: 1rem;
}

h2 {
  margin: 0 0 0.5rem;
  font-size: 1.05rem;
}

h3 {
  margin: 1rem 0 0.35rem;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

p {
  margin: 0 0 0.85rem;
}

.muted {
  color: var(--muted);
}

.small {
  font-size: 0.85rem;
}

.mono,
code,
pre {
  font-family: ui-monospace, "Cascadia Code", Consolas, "SF Mono", monospace;
  font-size: 0.85em;
}

.break {
  overflow-wrap: anywhere;
}

/* ------------------------------------------------------------- controls */

.field {
  display: block;
  margin-bottom: 0.85rem;
}

.field span {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 0.3rem;
}

input[type='text'],
input[type='file'] {
  width: 100%;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: inherit;
  font: inherit;
}

.button-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin: 0.5rem 0;
}

button {
  padding: 0.6rem 1rem;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: inherit;
  font: inherit;
  font-weight: 500;
  cursor: pointer;
  min-height: 44px; /* comfortable touch target */
}

button:hover:not(:disabled) {
  border-color: var(--accent);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

button.danger {
  color: var(--danger);
  border-color: var(--danger);
}

details {
  margin-top: 0.75rem;
  border-top: 1px solid var(--border);
  padding-top: 0.75rem;
}

summary {
  cursor: pointer;
  font-size: 0.9rem;
  color: var(--muted);
  margin-bottom: 0.5rem;
}

/* --------------------------------------------------------------- pieces */

.pill {
  margin-left: auto;
  padding: 0.2rem 0.7rem;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
  border: 1px solid currentColor;
}

.pill-muted {
  color: var(--muted);
}

.pill-ok {
  color: var(--ok);
}

.pill-warn {
  color: var(--danger);
}

.kv {
  display: grid;
  grid-template-columns: minmax(7rem, auto) 1fr;
  gap: 0.3rem 1rem;
  margin: 0 0 1rem;
}

.kv dt {
  color: var(--muted);
  font-size: 0.85rem;
}

.kv dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.log {
  background: var(--log-bg);
  color: var(--log-text);
  padding: 0.75rem;
  border-radius: 8px;
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0.5rem 0 0;
  max-height: 22rem;
}

.lines {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.35rem;
}

.lines li {
  display: flex;
  gap: 0.75rem;
  align-items: baseline;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0.45rem 0.6rem;
}

.lines .confidence {
  margin-left: auto;
  font-variant-numeric: tabular-nums;
  font-size: 0.8rem;
  color: var(--muted);
  flex: none;
}

#scanner {
  margin: 0.5rem 0;
}

#scanner-video {
  width: 100%;
  max-height: 60vh;
  border-radius: var(--radius);
  background: #000;
  object-fit: cover;
}

#image-preview {
  display: block;
  max-width: 100%;
  max-height: 20rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  margin: 0.5rem 0;
}

.error {
  color: var(--danger);
  border: 1px solid currentColor;
  border-radius: 8px;
  padding: 0.75rem;
  margin: 0;
}

a {
  color: var(--accent);
}

.qr {
  display: block;
  width: min(260px, 70vw);
  height: auto;
  image-rendering: pixelated;
  background: #fff;
  padding: 0.75rem;
  border-radius: var(--radius);
  margin: 0.75rem 0;
}

.lines li.device {
  align-items: center;
}

.lines li.device .confidence {
  color: inherit;
}

/* ------------------------------------------------------- live admin view */

.card-head {
  display: flex;
  align-items: baseline;
  gap: 1rem;
  flex-wrap: wrap;
}

.card-head h2 {
  margin-bottom: 0;
}

.live-toggle {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 0.4rem;
  color: var(--muted);
  cursor: pointer;
  font-variant-numeric: tabular-nums;
}

.live-toggle input {
  accent-color: var(--accent);
}

.live-dot {
  width: 0.55rem;
  height: 0.55rem;
  border-radius: 50%;
  background: var(--muted);
  flex: none;
}

.live-dot.live-ok {
  background: var(--ok);
  animation: heartbeat 3s ease-in-out infinite;
}

.live-dot.live-error {
  background: var(--danger);
}

@keyframes heartbeat {
  0%,
  90%,
  100% {
    opacity: 1;
  }
  95% {
    opacity: 0.25;
  }
}

.lines li.device {
  gap: 1rem;
}

.device-info {
  min-width: 0;
  flex: 1 1 14rem;
}

.device-stats {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  flex: none;
  margin-left: auto;
}

.device-action {
  flex: none;
}

.ping-count {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 1.05rem;
}

/* Fires only when a device's call count actually goes up. */
.ping-count.pulse {
  animation: ping-flash 1.2s ease-out;
}

@keyframes ping-flash {
  0% {
    color: var(--accent);
    transform: scale(1.35);
  }
  100% {
    color: inherit;
    transform: scale(1);
  }
}

@media (prefers-reduced-motion: reduce) {
  .live-dot.live-ok,
  .ping-count.pulse {
    animation: none;
  }
}

.device-action {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.35rem;
}

/* A revoked device stays in the list - its key is kept, so it can be unblocked. */
.lines li.device.revoked {
  opacity: 0.62;
}
