:root {
  --bg: #0b0e12;
  --bg-elev: #12161c;
  --panel: #161b22;
  --panel-2: #1d242d;
  --panel-3: #262f3a;
  --line: #262e38;
  --text: #e9eef4;
  --muted: #8a95a3;
  --faint: #5c6673;
  --accent: #5aa9ff;
  --accent-2: #8cc4ff;
  --ok: #3ddc84;
  --warn: #ffb547;
  --err: #ff5d5d;
  --live: #ff4d4d;
  --radius: 12px;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  min-height: 100%;
  background:
    radial-gradient(1200px 600px at 20% -10%, rgba(90, 169, 255, 0.10), transparent 60%),
    radial-gradient(900px 500px at 100% 0%, rgba(255, 77, 77, 0.06), transparent 60%),
    var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Inter, "Helvetica Neue", Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-font-smoothing: antialiased;
}

button { font: inherit; }
button:focus-visible, input:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---- Top bar ------------------------------------------------------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 10px 18px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(11, 14, 18, 0.82);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  border-bottom: 1px solid var(--line);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  font-weight: 700;
  font-size: 17px;
  letter-spacing: 0.01em;
  color: var(--text);
}
.brand-mark { width: 26px; height: 26px; color: var(--accent); }

.now-playing {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.np-number { font-weight: 700; color: var(--text); }
.np-name { overflow: hidden; text-overflow: ellipsis; }

.live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--live);
  box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.6);
  animation: pulse 1.8s ease-out infinite;
  flex: none;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(255, 77, 77, 0.55); }
  100% { box-shadow: 0 0 0 9px rgba(255, 77, 77, 0); }
}

.pill {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  padding: 4px 11px;
  border-radius: 999px;
  background: var(--panel-2);
  color: var(--muted);
  white-space: nowrap;
}
.pill.live { background: rgba(61, 220, 132, 0.14); color: var(--ok); }
.pill.warn { background: rgba(255, 181, 71, 0.14); color: var(--warn); }
.pill.err { background: rgba(255, 93, 93, 0.14); color: var(--err); }

/* ---- Layout -------------------------------------------------------------- */
.layout {
  display: grid;
  grid-template-columns: 1fr;
  gap: 16px;
  padding: 16px;
  padding-bottom: max(16px, env(safe-area-inset-bottom));
  max-width: 1480px;
  margin: 0 auto;
}

@media (min-width: 960px) {
  .layout { grid-template-columns: minmax(0, 1fr) 320px; padding: 20px; }
  .sidebar { position: sticky; top: 68px; max-height: calc(100vh - 88px); }
}

/* ---- Player -------------------------------------------------------------- */
.video-wrap {
  position: relative;
  background: #000;
  border-radius: var(--radius);
  overflow: hidden;
  aspect-ratio: 16 / 9;
  box-shadow: var(--shadow);
  border: 1px solid rgba(255, 255, 255, 0.05);
}

video {
  display: block;
  width: 100%;
  height: 100%;
  background: #000;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background:
    radial-gradient(600px 300px at 50% 40%, rgba(90, 169, 255, 0.10), transparent 70%),
    rgba(8, 10, 14, 0.86);
  transition: opacity 0.25s ease;
}
.overlay[hidden] { display: none; }
.overlay-inner { text-align: center; padding: 24px; max-width: 420px; }
.overlay-icon { color: var(--accent); margin-bottom: 6px; }
.overlay-icon svg { width: 56px; height: 56px; }
.overlay-title { font-size: 20px; font-weight: 700; margin-bottom: 6px; }
.overlay-text { color: var(--muted); font-size: 14px; }
.overlay.error .overlay-icon { color: var(--err); }
.overlay.error .overlay-title { color: var(--err); }

.spinner {
  width: 44px;
  height: 44px;
  margin: 0 auto 14px;
  border-radius: 50%;
  border: 3px solid rgba(255, 255, 255, 0.12);
  border-top-color: var(--accent);
  animation: spin 0.9s linear infinite;
}
.spinner[hidden] { display: none; }
@keyframes spin { to { transform: rotate(360deg); } }

.tap {
  margin-top: 18px;
  padding: 12px 26px;
  border: 0;
  border-radius: 999px;
  background: var(--accent);
  color: #06111f;
  font-weight: 700;
  font-size: 16px;
  cursor: pointer;
  box-shadow: 0 6px 24px rgba(90, 169, 255, 0.35);
}
.tap:hover { background: var(--accent-2); }

.toast {
  position: absolute;
  left: 50%;
  bottom: 64px;
  transform: translateX(-50%);
  padding: 8px 14px;
  border-radius: 8px;
  background: rgba(20, 24, 30, 0.92);
  border: 1px solid var(--line);
  color: var(--text);
  font-size: 13px;
  max-width: 90%;
  pointer-events: none;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  min-height: 36px;
}
.toolbar-spacer { flex: 1; }
.ch-nav { display: flex; gap: 6px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 36px;
  padding: 0 14px;
  border-radius: 9px;
  border: 1px solid var(--line);
  background: var(--panel);
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}
.btn:hover { background: var(--panel-2); border-color: var(--panel-3); }
.btn:disabled { opacity: 0.45; cursor: default; }
.btn.icon { width: 36px; padding: 0; }
.btn.icon svg { width: 20px; height: 20px; }
.btn.danger:hover { border-color: rgba(255, 93, 93, 0.5); color: var(--err); }
.btn.text { height: auto; padding: 0; border: 0; background: none; color: var(--accent); font-size: 13px; }
.btn.text:hover { color: var(--accent-2); background: none; }

.sync-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  height: 36px;
  padding: 0 12px;
  border-radius: 9px;
  background: var(--panel);
  border: 1px solid var(--line);
  font-size: 13px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  min-width: 0;
}
.sync-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--faint); flex: none; }
.sync-chip.ok .sync-dot { background: var(--ok); box-shadow: 0 0 8px rgba(61, 220, 132, 0.6); }
.sync-chip.ok .sync-text { color: var(--ok); }
.sync-chip.warn .sync-dot { background: var(--warn); }
.sync-chip.warn .sync-text { color: var(--warn); }
.sync-text { font-weight: 600; white-space: nowrap; }
.sync-detail { color: var(--faint); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
@media (max-width: 480px) { .sync-detail { display: none; } }

/* ---- Sidebar ------------------------------------------------------------- */
.sidebar {
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 14px;
  min-height: 0;
}
.sidebar-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 10px; }
.sidebar h2 { margin: 0; font-size: 15px; font-weight: 700; }
.hint { color: var(--muted); font-size: 12px; margin: 0 0 10px; }

.search {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 38px;
  padding: 0 12px;
  margin-bottom: 10px;
  border-radius: 9px;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  color: var(--faint);
}
.search:focus-within { border-color: var(--accent); color: var(--accent); }
.search svg { width: 18px; height: 18px; flex: none; }
.search input {
  flex: 1;
  min-width: 0;
  border: 0;
  background: none;
  color: var(--text);
  font-size: 14px;
  outline: none;
}
.search input::placeholder { color: var(--faint); }
.search input::-webkit-search-cancel-button { filter: invert(0.7); }

.channels {
  flex: 1;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  padding-right: 2px;
  scrollbar-width: thin;
  scrollbar-color: var(--panel-3) transparent;
}

.group { margin-bottom: 10px; }
.group-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--faint);
  margin: 6px 2px 6px;
}
.group-title::after { content: ""; flex: 1; height: 1px; background: var(--line); }

.group-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(132px, 1fr));
  gap: 8px;
}

.channel {
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  padding: 9px 10px 8px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: var(--panel-2);
  color: var(--text);
  text-align: left;
  cursor: pointer;
  min-height: 56px;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
}
.channel:hover { background: var(--panel-3); border-color: var(--panel-3); }
.channel:active { transform: scale(0.98); }
.channel.active {
  border-color: var(--accent);
  background: linear-gradient(180deg, rgba(90, 169, 255, 0.18), rgba(90, 169, 255, 0.08));
}
.channel .num { display: flex; align-items: center; gap: 6px; font-weight: 800; font-size: 16px; letter-spacing: -0.01em; }
.channel .name {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}
.channel.active .name { color: var(--accent-2); }
.channel .hd {
  font-size: 9px;
  font-weight: 800;
  letter-spacing: 0.06em;
  padding: 1px 4px;
  border-radius: 4px;
  background: rgba(90, 169, 255, 0.18);
  color: var(--accent-2);
}

.channel .indicator {
  position: absolute;
  right: 9px;
  top: 10px;
  display: none;
  align-items: flex-end;
  gap: 2px;
  height: 12px;
}
.channel.active .indicator { display: flex; }
.indicator i {
  display: block;
  width: 3px;
  height: 100%;
  border-radius: 1px;
  background: var(--accent);
  transform-origin: bottom;
  animation: eq 1s ease-in-out infinite;
}
.indicator i:nth-child(2) { animation-delay: 0.2s; }
.indicator i:nth-child(3) { animation-delay: 0.4s; }
.channel.tuning .indicator i { animation: none; opacity: 0.35; transform: scaleY(0.5); }
@keyframes eq {
  0%, 100% { transform: scaleY(0.35); }
  50% { transform: scaleY(1); }
}

.empty { color: var(--muted); font-size: 13px; padding: 12px 4px; }
