:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --text: #0f172a;
  --muted: #5b6475;
  --line: #e3e7ee;
  --accent: #0d9488;
  --accent-text: #ffffff;
  --accent-soft: #ccfbf1;
  --danger: #dc2626;
  --danger-text: #ffffff;
  --zone-0: #cbd5e1;
  --zone-1: #22c55e;
  --zone-2: #f59e0b;
  --zone-3: #ef4444;
  --shadow: 0 1px 2px rgb(15 23 42 / 6%), 0 4px 16px rgb(15 23 42 / 5%);
  --radius: 16px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1220;
    --card: #131c2e;
    --text: #e6ebf3;
    --muted: #97a3b8;
    --line: #243049;
    --accent: #2dd4bf;
    --accent-text: #04201d;
    --accent-soft: #11423d;
    --danger: #f87171;
    --danger-text: #2a0707;
    --zone-0: #334155;
    --shadow: none;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }

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

main {
  max-width: 920px;
  margin: 0 auto;
  padding: 8px 16px 48px;
  display: grid;
  gap: 16px;
}

h1, h2, h3 { margin: 0; line-height: 1.2; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.15rem; }

.muted { color: var(--muted); }
.note { margin: 12px 0 0; color: var(--muted); font-size: 0.92rem; }
.note[data-kind="error"] { color: var(--danger); }

/* Top bar */
.topbar {
  max-width: 920px;
  margin: 0 auto;
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 1.2rem; }
.brand-mark { width: 30px; height: 30px; }
.brand-mark rect { fill: var(--accent); }
.brand-mark path { fill: var(--accent-text); }

.pill {
  padding: 4px 12px;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 600;
  background: var(--card);
  border: 1px solid var(--line);
  color: var(--muted);
  white-space: nowrap;
}
.pill[data-state="connected"] { background: var(--accent-soft); border-color: transparent; color: var(--text); }
.pill[data-state="demo"] { background: var(--accent-soft); border-color: transparent; color: var(--text); }
.pill[data-state="connecting"],
.pill[data-state="reconnecting"] { color: var(--text); }

/* Cards */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px;
}

.connect { text-align: center; padding: 36px 20px; }
.connect h1 { margin-bottom: 6px; }

/* Buttons */
.actions { display: flex; flex-wrap: wrap; gap: 10px; justify-content: center; margin-top: 18px; }
.actions.end { justify-content: flex-end; margin-top: 0; }
.history-head .actions { margin: 0; }

.btn {
  font: inherit;
  font-weight: 600;
  padding: 10px 18px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  cursor: pointer;
  transition: transform 0.08s ease, background 0.15s ease;
}
.btn:hover { border-color: var(--muted); }
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.btn-primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn-primary:hover { border-color: var(--accent); filter: brightness(1.05); }
.btn-ghost { background: transparent; }
.btn-small { padding: 6px 12px; font-size: 0.9rem; border-radius: 10px; }
.btn-big { padding: 14px 26px; font-size: 1.05rem; border-radius: 14px; min-width: 170px; }
.btn-stop { background: var(--danger); border-color: var(--danger); color: var(--danger-text); }
.btn-stop:hover { border-color: var(--danger); }

/* Hero */
.hero {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  margin-bottom: 16px;
}
.rec-state { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--muted); }
.rec-state .dot { width: 10px; height: 10px; border-radius: 50%; background: var(--zone-0); }
.rec-state[data-rec="1"] { color: var(--danger); }
.rec-state[data-rec="1"] .dot { background: var(--danger); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }

.timer {
  font-size: clamp(2.6rem, 9vw, 3.6rem);
  font-weight: 750;
  letter-spacing: -0.02em;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  margin: 4px 0;
}

/* Stat grid */
.grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 12px;
  margin-bottom: 16px;
}
.stat h3 { font-size: 0.85rem; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.stat .value {
  font-size: 2rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  margin: 6px 0 2px;
  display: flex;
  align-items: baseline;
  gap: 6px;
  flex-wrap: wrap;
}
.stat .value small { font-size: 0.85rem; font-weight: 600; color: var(--muted); }
.stat .sub { font-size: 0.88rem; color: var(--muted); min-height: 1.4em; }
.stat-off .value { color: var(--muted); }

.meter { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; margin: 8px 0 6px; }
.meter i { height: 8px; border-radius: 4px; background: var(--line); transition: background 0.3s ease; }
.meter[data-zone="0"] i:nth-child(1) { background: var(--zone-0); }
.meter[data-zone="1"] i:nth-child(-n+2) { background: var(--zone-1); }
.meter[data-zone="2"] i:nth-child(-n+3) { background: var(--zone-2); }
.meter[data-zone="3"] i { background: var(--zone-3); }

/* History */
.history-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; margin-bottom: 8px; }
.history-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 10px; }
.history-list li { border: 1px solid var(--line); border-radius: 12px; }
.history-list details > summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  display: grid;
  gap: 6px;
}
.history-list details > summary::-webkit-details-marker { display: none; }
.history-list details[open] > summary { border-bottom: 1px solid var(--line); }
.session-title { display: flex; justify-content: space-between; gap: 8px; flex-wrap: wrap; font-weight: 650; }
.session-title time { font-weight: 500; color: var(--muted); font-size: 0.9rem; }
.badge { font-size: 0.72rem; font-weight: 700; padding: 1px 8px; border-radius: 999px; background: var(--accent-soft); margin-left: 6px; vertical-align: 2px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px 14px; color: var(--muted); font-size: 0.9rem; font-variant-numeric: tabular-nums; }
.chips b { color: var(--text); font-weight: 650; }

.detail { padding: 14px; display: grid; gap: 14px; }
.detail dl { margin: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(125px, 1fr)); gap: 10px 16px; }
.detail dt { font-size: 0.8rem; color: var(--muted); }
.detail dd { margin: 0; font-weight: 650; font-variant-numeric: tabular-nums; }

.split { display: flex; height: 12px; border-radius: 6px; overflow: hidden; background: var(--line); }
.split span { display: block; height: 100%; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 0.82rem; color: var(--muted); margin-top: 6px; }
.legend i { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 5px; }

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

@media (max-width: 640px) {
  .grid { grid-template-columns: 1fr 1fr; }
}

@media (max-width: 520px) {
  .card { padding: 16px; }
  .hero .btn-big { width: 100%; }
  .stat .value { font-size: 1.6rem; }
}
