:root {
  --bg0: #0b1210;
  --bg1: #13201a;
  --ink: #e7f2ea;
  --muted: #93a89a;
  --accent: #3dd68c;
  --accent-ink: #062116;
  --warn: #f0b429;
  --line: rgba(231, 242, 234, 0.12);
  --card: rgba(19, 32, 26, 0.78);
  --font-display: "Syne", sans-serif;
  --font-body: "IBM Plex Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; min-height: 100%; }
body {
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--bg0);
  min-height: 100vh;
}

.bg {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(900px 500px at 12% -10%, rgba(61, 214, 140, 0.22), transparent 55%),
    radial-gradient(700px 420px at 95% 15%, rgba(47, 158, 120, 0.16), transparent 50%),
    linear-gradient(160deg, var(--bg0), var(--bg1) 55%, #0a100e);
  z-index: -1;
}
.bg::after {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23n)' opacity='0.04'/%3E%3C/svg%3E");
  opacity: 0.55;
  pointer-events: none;
}

.shell {
  width: min(1200px, calc(100% - 2.5rem));
  margin: 0 auto;
  padding: 2.75rem 0 4rem;
}

.brand {
  display: grid;
  gap: 0.35rem;
  max-width: 52rem;
}

.brand-mark {
  margin: 0 0 0.75rem;
  font-family: var(--font-display);
  font-weight: 800;
  font-size: clamp(2.6rem, 8vw, 4.4rem);
  letter-spacing: -0.04em;
  line-height: 0.95;
  background: linear-gradient(120deg, #fff 20%, var(--accent));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: rise 0.7s ease both;
}

.brand h1 {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.35rem, 3.5vw, 1.85rem);
  font-weight: 700;
  letter-spacing: -0.02em;
  animation: rise 0.7s 0.08s ease both;
}

.lead {
  margin: 0.85rem 0 0;
  max-width: 38rem;
  color: var(--muted);
  font-size: 1.05rem;
  line-height: 1.5;
  animation: rise 0.7s 0.14s ease both;
}

.tabs {
  display: flex;
  gap: 0.45rem;
  margin-top: 1.6rem;
  padding: 0.3rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.22);
  width: fit-content;
  max-width: 100%;
}
.tab {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.65rem 1.15rem;
  background: transparent;
  color: var(--muted);
  font: 600 0.95rem/1 var(--font-body);
  cursor: pointer;
}
.tab.active {
  background: var(--accent);
  color: var(--accent-ink);
}

.panel { margin-top: 1rem; }
.panel[hidden] { display: none !important; }

.stage {
  margin-top: 0.4rem;
  padding: 1.5rem 1.6rem;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--card);
  backdrop-filter: blur(10px);
  animation: rise 0.75s 0.2s ease both;
}

/* Desktop stream: controls left, result/history right */
@media (min-width: 980px) {
  #panel-stream .stage {
    display: grid;
    grid-template-columns: minmax(0, 1.2fr) minmax(280px, 0.9fr);
    gap: 1.25rem 1.5rem;
    align-items: start;
  }
  #panel-stream .viz,
  #panel-stream .device,
  #panel-stream .controls {
    grid-column: 1;
  }
  #panel-stream .result {
    grid-column: 2;
    grid-row: 1;
    margin-top: 0;
    min-height: 120px;
  }
  #panel-stream .history {
    grid-column: 2;
    grid-row: 2 / span 3;
    margin-top: 0;
    align-content: start;
    max-height: 420px;
    overflow: auto;
  }
  #panel-stream .device select {
    max-width: 28rem;
  }
}

.btn.ghost {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--line);
  border-radius: 999px;
}
.btn.ghost:hover { border-color: rgba(61, 214, 140, 0.45); }

.devices-toolbar {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: flex-start;
  margin-bottom: 1.1rem;
}

.period-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  margin-bottom: 1rem;
  padding: 0.85rem 0.9rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
}
.period-bar label {
  display: grid;
  gap: 0.3rem;
  color: var(--muted);
  font-size: 0.82rem;
}
.period-bar select,
.period-bar input {
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.28);
  color: var(--ink);
  padding: 0.55rem 0.7rem;
  font: 500 0.9rem/1.2 var(--font-body);
  min-width: 9rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 0.55rem;
  margin: 0.85rem 0 1rem;
}

.chart-wrap {
  margin: 0.4rem 0 0.6rem;
  padding: 0.7rem 0.75rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.16);
}
.chart-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem 1rem;
  align-items: center;
  margin: 0.25rem 0 0.65rem !important;
}
.chart-legend .dot {
  display: inline-block;
  width: 0.65rem;
  height: 0.65rem;
  border-radius: 50%;
  margin-right: 0.35rem;
  vertical-align: middle;
}
.chart-legend .dot.ok { background: #3dd68c; }
.chart-legend .dot.miss { background: #ff6b6b; }
.chart-legend .dot.dup { background: #f0b429; }
.chart-canvas-wrap {
  width: 100%;
  overflow: hidden;
  border-radius: 10px;
}
#recogChart {
  display: block;
  width: 100%;
  height: 260px;
  cursor: crosshair;
}
.stat-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(61, 214, 140, 0.07);
  padding: 0.75rem 0.8rem;
  display: grid;
  gap: 0.2rem;
}
.stat-value {
  font-family: var(--font-display);
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}
.stat-label {
  color: var(--muted);
  font-size: 0.78rem;
}

.subhead {
  margin: 1rem 0 0.55rem;
  font-family: var(--font-display);
  font-size: 1rem;
}
.periods-table-wrap { overflow-x: auto; }
.periods-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.88rem;
}
.periods-table th,
.periods-table td {
  text-align: left;
  padding: 0.45rem 0.4rem;
  border-bottom: 1px solid var(--line);
}
.periods-table th { color: var(--muted); font-weight: 600; }
.periods-table td:not(:first-child),
.periods-table th:not(:first-child) { text-align: right; }
.section-title {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}
.section-note {
  margin: 0.35rem 0 0;
  color: var(--muted);
  font-size: 0.92rem;
}

.devices-layout {
  display: grid;
  grid-template-columns: minmax(260px, 320px) minmax(0, 1fr);
  gap: 1.25rem;
  align-items: start;
}
.device-list {
  display: grid;
  gap: 0.55rem;
  position: sticky;
  top: 1rem;
  max-height: calc(100vh - 2rem);
  overflow: auto;
  padding-right: 0.15rem;
}
.device-card {
  appearance: none;
  text-align: left;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.22);
  color: var(--ink);
  padding: 0.85rem 0.9rem;
  cursor: pointer;
  display: grid;
  gap: 0.2rem;
}
.device-card:hover { border-color: rgba(61, 214, 140, 0.35); }
.device-card.active {
  border-color: rgba(61, 214, 140, 0.55);
  background: rgba(61, 214, 140, 0.1);
}
.device-card-name {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1.05rem;
}
.device-card-id,
.device-card-stats,
.device-card-last,
.device-card-time {
  color: var(--muted);
  font-size: 0.82rem;
}
.device-card-last {
  color: var(--ink);
  opacity: 0.85;
}

.playlist-wrap {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(0, 0, 0, 0.18);
  padding: 1.1rem 1.2rem 1.25rem;
  min-height: 280px;
}
.playlist-head { margin-bottom: 0.85rem; }
.playlist-head h3 {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.15rem;
}
.playlist {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.55rem;
}
.playlist-item {
  display: grid;
  grid-template-columns: 2rem 56px 1fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.55rem 0.45rem;
  border-radius: 12px;
  border: 1px solid transparent;
}
.playlist-item:hover {
  border-color: var(--line);
  background: rgba(255, 255, 255, 0.03);
}
.playlist-num {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.playlist-item img,
.cover-fallback {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  object-fit: cover;
  background: #1a2a22;
}
.cover-fallback {
  display: grid;
  place-items: center;
  color: var(--muted);
  font-weight: 600;
}
.playlist-body {
  display: grid;
  gap: 0.15rem;
  min-width: 0;
}
.playlist-body strong {
  font-size: 0.98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.playlist-body span {
  color: var(--muted);
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.playlist-time { font-size: 0.78rem !important; }
.playlist-item a {
  color: var(--accent);
  font-weight: 600;
  font-size: 0.85rem;
  text-decoration: none;
}
.empty {
  margin: 0.5rem 0;
  color: var(--muted);
}

.viz {
  border-radius: 12px;
  background: rgba(0, 0, 0, 0.28);
  overflow: hidden;
  border: 1px solid var(--line);
}
#wave {
  display: block;
  width: 100%;
  height: 120px;
  touch-action: none;
}

.level-row {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.75rem 0.85rem 0.95rem;
}
.level-track {
  flex: 1;
  height: 14px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}
.level-fill {
  height: 100%;
  width: 0%;
  border-radius: inherit;
  background: linear-gradient(90deg, #2f9e78, #7dffb5);
  transition: width 0.06s linear;
  will-change: width;
}
.level-fill.hot { background: linear-gradient(90deg, #f0b429, #ff6b6b); }
.level-text {
  min-width: 3.4rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 600;
}

.device {
  display: grid;
  gap: 0.4rem;
  margin-top: 1rem;
  color: var(--muted);
  font-size: 0.9rem;
}
.device select {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.28);
  color: var(--ink);
  padding: 0.7rem 0.8rem;
  font: 500 0.95rem/1.2 var(--font-body);
}

.controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  margin-top: 1.25rem;
}

.btn {
  appearance: none;
  border: 0;
  border-radius: 999px;
  padding: 0.9rem 1.45rem;
  font: 600 1rem/1 var(--font-body);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.2s ease, box-shadow 0.2s ease;
}
.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
  box-shadow: 0 0 0 0 rgba(61, 214, 140, 0.35);
}
.btn.primary:hover { transform: translateY(-1px); }
.btn.primary.live {
  background: #ff6b6b;
  color: #2a0808;
  animation: pulse 1.6s ease-in-out infinite;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none; }

.status {
  margin: 0;
  color: var(--muted);
  font-size: 0.95rem;
}
.status.live { color: var(--accent); }
.status.warn { color: var(--warn); }

.result {
  display: grid;
  grid-template-columns: 96px 1fr;
  gap: 1rem;
  align-items: center;
  margin-top: 1.4rem;
  padding: 1rem;
  border-radius: 14px;
  border: 1px solid rgba(61, 214, 140, 0.35);
  background: rgba(61, 214, 140, 0.08);
}
.result.stale {
  opacity: 0.62;
  border-color: var(--line);
  background: rgba(0, 0, 0, 0.18);
}
.result.stale .label { color: var(--muted); }
.result img {
  width: 96px;
  height: 96px;
  object-fit: cover;
  border-radius: 10px;
  background: #1a2a22;
}
.result .label {
  margin: 0;
  font-size: 0.75rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--accent);
}
.result h2 {
  margin: 0.2rem 0 0.15rem;
  font-family: var(--font-display);
  font-size: 1.35rem;
  letter-spacing: -0.02em;
}
.result p { margin: 0; color: var(--muted); }
.result a {
  display: inline-block;
  margin-top: 0.55rem;
  color: var(--ink);
  font-weight: 600;
}

.history {
  list-style: none;
  margin: 1.25rem 0 0;
  padding: 0;
  display: grid;
  gap: 0.55rem;
}
.history li {
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  padding: 0.7rem 0.85rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: rgba(0, 0, 0, 0.18);
  font-size: 0.92rem;
}
.history .meta { color: var(--muted); white-space: nowrap; }

@keyframes rise {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes pulse {
  0%, 100% { box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.35); }
  50% { box-shadow: 0 0 0 10px rgba(255, 107, 107, 0); }
}

@media (min-width: 1280px) {
  .shell {
    width: min(1360px, calc(100% - 3rem));
    padding-top: 3rem;
  }
  .brand-mark { font-size: 4.2rem; }
  .brand h1 { font-size: 2rem; }
  .lead { font-size: 1.1rem; max-width: 44rem; }
  .stage { padding: 1.75rem 1.85rem; }
  #wave { height: 140px; }
  #recogChart { height: 300px; }
  .stats-grid { gap: 0.75rem; }
  .stat-value { font-size: 1.55rem; }
  .devices-layout {
    grid-template-columns: 320px minmax(0, 1fr);
    gap: 1.4rem;
  }
  .period-bar {
    align-items: end;
  }
  .period-bar select,
  .period-bar input {
    min-width: 11rem;
  }
}

@media (max-width: 980px) {
  .shell { width: min(900px, calc(100% - 2rem)); }
  .devices-layout { grid-template-columns: 1fr; }
  .device-list {
    position: static;
    max-height: none;
    grid-auto-flow: column;
    grid-auto-columns: minmax(220px, 280px);
    overflow-x: auto;
    padding-bottom: 0.35rem;
  }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  #recogChart { height: 220px; }
}

@media (max-width: 560px) {
  .shell { padding-top: 2rem; width: min(760px, calc(100% - 1.25rem)); }
  .stage { padding: 1rem; }
  #wave { height: 96px; }
  .level-track { height: 16px; }
  .level-text { font-size: 1rem; }
  .controls { flex-direction: column; align-items: stretch; }
  .btn { width: 100%; text-align: center; padding: 1rem 1.2rem; }
  .result { grid-template-columns: 72px 1fr; }
  .result img { width: 72px; height: 72px; }
  .brand-mark { font-size: clamp(2.2rem, 12vw, 3.4rem); }
  .tabs { width: 100%; }
  .tab { flex: 1; text-align: center; }
  .device-list {
    grid-auto-flow: row;
    grid-auto-columns: unset;
    overflow-x: visible;
  }
  .playlist-item {
    grid-template-columns: 1.5rem 48px 1fr;
    grid-template-rows: auto auto;
  }
  .playlist-item a {
    grid-column: 3;
    justify-self: start;
  }
  .playlist-item img,
  .cover-fallback { width: 48px; height: 48px; }
}
