:root {
  --bg: #0A0C10;
  --surface: #12161C;
  --surface-2: #1A1F27;
  --surface-3: #232935;
  --border: #262C36;
  --text: #ECEFF3;
  --text-dim: #8B94A3;
  --text-faint: #565E6C;
  --accent: #E8A33D;
  --accent-dim: #6B5023;
  --live: #E5484D;

  --font-display: 'Oswald', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'IBM Plex Mono', monospace;

  --header-h: 56px;
  --sidebar-w: 260px;
  --radius: 10px;
}

* { box-sizing: border-box; }

/* The hidden attribute must always win, even on elements whose class sets
   its own display value (e.g. .modal-overlay { display:flex }) — without
   this, author CSS specificity ties beat the UA stylesheet and the
   element stays visible despite [hidden] being set. */
[hidden] { display: none !important; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.001ms !important; animation-iteration-count: 1 !important; transition-duration: 0.001ms !important; }
}

html, body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

#app {
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

button { font-family: inherit; cursor: pointer; }
input, textarea { font-family: inherit; }

/* ---------- Header ---------- */

.tuner-bar {
  height: var(--header-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 0 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  position: relative;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.brand-mark {
  display: inline-flex;
  align-items: flex-end;
  gap: 2px;
  height: 16px;
}
.brand-mark i {
  display: block;
  width: 3px;
  background: var(--accent);
  border-radius: 1px;
  animation: bars 1.6s ease-in-out infinite;
}
.brand-mark i:nth-child(1) { height: 40%; animation-delay: 0s; }
.brand-mark i:nth-child(2) { height: 70%; animation-delay: .15s; }
.brand-mark i:nth-child(3) { height: 100%; animation-delay: .3s; }
.brand-mark i:nth-child(4) { height: 65%; animation-delay: .45s; }
.brand-mark i:nth-child(5) { height: 45%; animation-delay: .6s; }
@keyframes bars {
  0%, 100% { transform: scaleY(0.55); }
  50% { transform: scaleY(1); }
}

.brand-name {
  font-family: var(--font-display);
  font-weight: 600;
  letter-spacing: 0.14em;
  font-size: 15px;
  color: var(--text);
}

.now-tuned {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
  color: var(--text-dim);
  font-size: 13px;
}
.now-tuned .chan-num {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 2px 7px;
  font-size: 12px;
  flex: 0 0 auto;
}
.now-tuned .chan-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
}
.live-dot {
  flex: 0 0 auto;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--live);
  box-shadow: 0 0 0 0 rgba(229,72,77,.6);
  animation: pulse 1.8s ease-out infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(229,72,77,.5); }
  70% { box-shadow: 0 0 0 6px rgba(229,72,77,0); }
  100% { box-shadow: 0 0 0 0 rgba(229,72,77,0); }
}

.btn-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: transparent;
  border: 1px solid transparent;
  color: var(--text-dim);
}
.btn-icon:hover { background: var(--surface-2); color: var(--text); border-color: var(--border); }

#menuToggle { display: none; }

/* ---------- Layout ---------- */

.layout {
  flex: 1 1 auto;
  display: flex;
  min-height: 0;
}

.sidebar {
  width: var(--sidebar-w);
  flex: 0 0 auto;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 14px 10px;
  overflow-y: auto;
}

.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 6px 12px;
}
.sidebar-head-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}
.sidebar-head-actions .btn-icon {
  width: 28px;
  height: 28px;
}
.sidebar-head h2 {
  font-family: var(--font-display);
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--text-faint);
  margin: 0;
}
.btn-add {
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--accent);
  font-size: 16px;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn-add:hover { border-color: var(--accent-dim); }

.playlist-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.playlist-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 9px 10px;
  border-radius: 8px;
  border: 1px solid transparent;
  color: var(--text-dim);
}
.playlist-item:hover { background: var(--surface-2); }
.playlist-item.active {
  background: var(--surface-2);
  border-color: var(--accent-dim);
  color: var(--text);
}
.playlist-item .pl-btn {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
  background: none;
  border: none;
  color: inherit;
  text-align: left;
  padding: 0;
}
.playlist-item .pl-name {
  font-size: 13.5px;
  font-weight: 500;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.playlist-item.active .pl-name { color: var(--accent); }
.playlist-item .pl-meta {
  font-size: 11px;
  color: var(--text-faint);
}
.playlist-item .pl-remove {
  flex: 0 0 auto;
  opacity: 0;
  width: 22px;
  height: 22px;
  border-radius: 6px;
  border: none;
  background: transparent;
  color: var(--text-faint);
  display: flex;
  align-items: center;
  justify-content: center;
}
.playlist-item:hover .pl-remove { opacity: 1; }
.playlist-item .pl-remove:hover { color: var(--live); background: var(--surface-3); }

.sidebar-empty {
  font-size: 12.5px;
  color: var(--text-faint);
  padding: 10px 8px;
  line-height: 1.5;
}

.sidebar-scrim {
  display: none;
}

/* ---------- Main ---------- */

.main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.toolbar {
  flex: 0 0 auto;
  padding: 14px 18px 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  border-bottom: 1px solid var(--border);
}

.toolbar-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.btn-manage {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 12.5px;
  font-weight: 500;
}
.btn-manage:hover:not(:disabled) { border-color: var(--text-faint); color: var(--text); }
.btn-manage:disabled { opacity: .45; cursor: not-allowed; }

.search-wrap {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  color: var(--text-faint);
  max-width: 340px;
  flex: 1 1 auto;
}
.search-wrap input {
  border: none;
  background: none;
  outline: none;
  color: var(--text);
  font-size: 13.5px;
  width: 100%;
}
.search-wrap input::placeholder { color: var(--text-faint); }
.search-wrap input:disabled { cursor: not-allowed; }

.group-tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding-bottom: 2px;
  scrollbar-width: thin;
}
.group-tab {
  flex: 0 0 auto;
  padding: 6px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 12.5px;
  white-space: nowrap;
}
.group-tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: var(--accent);
}
.group-tab.fav-tab {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.group-tab.fav-tab svg { flex: 0 0 auto; }
.group-tab.fav-tab.active svg path { fill: currentColor; }
.group-tab.fav-tab:not(.active) svg { color: var(--text-faint); }

.grid {
  flex: 1 1 auto;
  overflow-y: auto;
  padding: 16px 18px 28px;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(148px, 1fr));
  gap: 12px;
  align-content: start;
}

.channel-card {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  min-height: 128px;
}
.channel-card:hover {
  border-color: var(--accent-dim);
  background: var(--surface-2);
}
.channel-card.playing {
  border-color: var(--accent);
  box-shadow: 0 0 0 1px var(--accent) inset;
}

.card-hit {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 14px 10px 12px;
  background: none;
  border: none;
  color: var(--text);
  text-align: center;
}
.card-hit:focus-visible { outline: 2px solid var(--accent-dim); outline-offset: -2px; }

.card-star {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 26px;
  height: 26px;
  border-radius: 7px;
  border: none;
  background: rgba(10,12,16,.55);
  color: rgba(236,239,243,.55);
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity .12s ease;
}
.card-star svg path { fill: none; stroke: currentColor; stroke-width: 1.6; }
.channel-card:hover .card-star,
.card-star.is-fav {
  opacity: 1;
}
.card-star.is-fav {
  color: var(--accent);
}
.card-star.is-fav svg path { fill: currentColor; stroke: currentColor; }
.card-star:hover { background: rgba(10,12,16,.8); }

.channel-card .logo-wrap {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  background: var(--surface-2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex: 0 0 auto;
}
.channel-card img {
  width: 100%;
  height: 100%;
  object-fit: contain;
}
.channel-card .logo-fallback {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 15px;
  color: var(--text-faint);
}
.channel-card .c-num {
  font-family: var(--font-mono);
  font-size: 10.5px;
  color: var(--text-faint);
}
.channel-card .c-name {
  font-size: 12.5px;
  font-weight: 500;
  line-height: 1.3;
  overflow: hidden;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
}

.empty-state {
  grid-column: 1 / -1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding: 60px 20px;
  color: var(--text-faint);
  text-align: center;
}
.empty-glyph {
  display: flex;
  align-items: flex-end;
  gap: 4px;
  height: 30px;
  opacity: .5;
  margin-bottom: 6px;
}
.empty-glyph i {
  width: 5px;
  border-radius: 2px;
  background: var(--text-faint);
}
.empty-glyph i:nth-child(1) { height: 35%; }
.empty-glyph i:nth-child(2) { height: 60%; }
.empty-glyph i:nth-child(3) { height: 100%; }
.empty-glyph i:nth-child(4) { height: 55%; }
.empty-glyph i:nth-child(5) { height: 40%; }
.empty-state h3 {
  font-family: var(--font-display);
  color: var(--text-dim);
  margin: 0;
  font-weight: 500;
  letter-spacing: .02em;
}
.empty-state p { margin: 0; font-size: 13px; max-width: 280px; }

/* ---------- Buttons ---------- */

.btn-primary {
  background: var(--accent);
  color: #1A1204;
  border: none;
  padding: 10px 18px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 13.5px;
}
.btn-primary:hover { filter: brightness(1.08); }
.btn-primary:disabled { opacity: .55; cursor: not-allowed; }

.btn-ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13.5px;
}
.btn-ghost:hover { color: var(--text); border-color: var(--text-faint); }

/* ---------- Player overlay ---------- */

.player-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5,6,8,.88);
  backdrop-filter: blur(6px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}

.player-shell {
  width: 100%;
  max-width: 980px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.player-topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
}
.player-info {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}
.chan-num.mono {
  font-family: var(--font-mono);
  font-weight: 600;
  color: var(--accent);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 3px 8px;
  font-size: 12px;
  flex: 0 0 auto;
}
.player-title {
  font-size: 14px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.player-group {
  font-size: 11.5px;
  color: var(--text-faint);
}
.player-actions { display: flex; gap: 4px; flex: 0 0 auto; }

.video-wrap {
  position: relative;
  background: #000;
  aspect-ratio: 16/9;
  width: 100%;
}
#video {
  width: 100%;
  height: 100%;
  display: block;
  background: #000;
}
.player-status {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 10px;
  color: var(--text-dim);
  font-size: 13px;
  background: rgba(0,0,0,.55);
}
.scan {
  width: 60px;
  height: 3px;
  border-radius: 2px;
  background: linear-gradient(90deg, transparent, var(--accent), transparent);
  animation: scan 1.2s linear infinite;
}
@keyframes scan {
  0% { transform: translateX(-40px); opacity: .3; }
  50% { opacity: 1; }
  100% { transform: translateX(40px); opacity: .3; }
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(5,6,8,.8);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 18px;
}
.modal {
  width: 100%;
  max-width: 460px;
  max-height: 90vh;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}
.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.modal-head h2 {
  font-family: var(--font-display);
  font-size: 15px;
  letter-spacing: .03em;
  margin: 0;
  font-weight: 600;
}
.modal-body {
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field span {
  font-size: 12px;
  color: var(--text-dim);
  font-weight: 500;
}
.field input, .field textarea {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  color: var(--text);
  font-size: 13.5px;
  outline: none;
  resize: vertical;
}
.field textarea { font-family: var(--font-mono); font-size: 12px; }
.field input:focus, .field textarea:focus { border-color: var(--accent-dim); }

.tabs {
  display: flex;
  gap: 4px;
  background: var(--surface-2);
  padding: 3px;
  border-radius: 8px;
}
.tab-btn {
  flex: 1 1 0;
  background: none;
  border: none;
  color: var(--text-dim);
  padding: 7px 8px;
  border-radius: 6px;
  font-size: 12.5px;
  font-weight: 500;
}
.tab-btn.active { background: var(--surface-3); color: var(--accent); }

.hint {
  font-size: 11.5px;
  color: var(--text-faint);
  line-height: 1.5;
  margin: -4px 0 0;
}

.group-check-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  max-height: 46vh;
  overflow-y: auto;
}
.group-check-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 8px 6px;
  border-radius: 7px;
  cursor: pointer;
}
.group-check-row:hover { background: var(--surface-2); }
.group-check-row input[type="checkbox"] {
  width: 16px;
  height: 16px;
  accent-color: var(--accent);
  flex: 0 0 auto;
}
.group-check-row .g-name {
  flex: 1 1 auto;
  font-size: 13.5px;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.group-check-row .g-count {
  flex: 0 0 auto;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-faint);
}

#favBtn.is-fav { color: var(--accent); }
#favBtn.is-fav svg path { fill: currentColor; }
.modal-error {
  font-size: 12.5px;
  color: var(--live);
  background: rgba(229,72,77,.1);
  border: 1px solid rgba(229,72,77,.3);
  border-radius: 8px;
  padding: 9px 11px;
  margin: 0;
}
.modal-foot {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding: 14px 16px;
  border-top: 1px solid var(--border);
  flex: 0 0 auto;
}

/* ---------- Toasts ---------- */

.toast-wrap {
  position: fixed;
  bottom: 18px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  gap: 8px;
  z-index: 300;
  align-items: center;
}
.toast {
  background: var(--surface-3);
  border: 1px solid var(--border);
  color: var(--text);
  padding: 10px 16px;
  border-radius: 8px;
  font-size: 13px;
  box-shadow: 0 6px 20px rgba(0,0,0,.4);
  animation: toast-in .18s ease-out;
}
.toast.error { border-color: rgba(229,72,77,.4); color: #FFD7D8; }
.toast.success { border-color: var(--accent-dim); color: var(--accent); }
@keyframes toast-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ---------- Responsive ---------- */

@media (max-width: 860px) {
  #menuToggle { display: inline-flex; }

  .sidebar {
    position: fixed;
    top: var(--header-h);
    bottom: 0;
    left: 0;
    z-index: 60;
    width: 82vw;
    max-width: 300px;
    transform: translateX(-100%);
    transition: transform .22s ease;
    box-shadow: 8px 0 24px rgba(0,0,0,.4);
  }
  .sidebar.open { transform: translateX(0); }

  .sidebar-scrim {
    display: none;
    position: fixed;
    inset: var(--header-h) 0 0 0;
    background: rgba(0,0,0,.5);
    z-index: 50;
  }
  .sidebar-scrim.open { display: block; }

  .now-tuned .chan-name { display: none; }

  .grid {
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
    gap: 10px;
    padding: 12px;
  }
  .channel-card { padding: 10px 6px; }
  .channel-card .logo-wrap { width: 46px; height: 46px; }

  .player-overlay { padding: 0; }
  .player-shell { max-width: none; height: 100%; border-radius: 0; }
  .video-wrap { aspect-ratio: auto; flex: 1 1 auto; }
}
