/* ===== Reset & Base ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

:root {
  --bg-primary: #0a0a0f;
  --bg-secondary: #12121a;
  --bg-card: #1a1a25;
  --bg-card-hover: #222233;
  --bg-input: #1e1e2e;
  --border: #2a2a3a;
  --border-focus: #6366f1;
  --text-primary: #e8e8f0;
  --text-secondary: #8888a0;
  --text-muted: #555570;
  --accent: #6366f1;
  --accent-hover: #818cf8;
  --accent-glow: rgba(99, 102, 241, 0.25);
  --success: #22c55e;
  --error: #ef4444;
  --warning: #f59e0b;
  --sidebar-width: 220px;
  --player-height: 0px;
  --radius: 10px;
  --radius-sm: 6px;
}

html, body {
  height: 100%;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-primary);
  color: var(--text-primary);
  overflow: hidden;
}

.hidden {
  display: none !important;
}

/* ===== App Layout ===== */
#app {
  display: flex;
  height: 100vh;
}

/* ===== Sidebar ===== */
#sidebar {
  width: var(--sidebar-width);
  background: var(--bg-secondary);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  z-index: 100;
}

.sidebar-header {
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
}

.logo {
  font-size: 22px;
  font-weight: 700;
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
}

.sidebar-nav {
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  flex: 1;
}

.nav-btn {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  border: none;
  background: none;
  color: var(--text-secondary);
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.15s ease;
}

.nav-btn svg {
  width: 20px;
  height: 20px;
  flex-shrink: 0;
}

.nav-btn:hover {
  background: var(--bg-card);
  color: var(--text-primary);
}

.nav-btn.active {
  background: var(--accent);
  color: white;
}

.sidebar-footer {
  padding: 12px 16px;
  border-top: 1px solid var(--border);
}

.source-status {
  font-size: 12px;
  color: var(--text-muted);
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.source-status .status-item {
  display: flex;
  align-items: center;
  gap: 6px;
}

.status-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  flex-shrink: 0;
}

.status-dot.connected { background: var(--success); }
.status-dot.disconnected { background: var(--error); }
.status-dot.disabled { background: var(--text-muted); }

/* ===== Main Content ===== */
#mainContent {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}

/* ===== Player ===== */
#playerContainer {
  background: #000;
  position: relative;
  flex-shrink: 0;
  transition: height 0.3s ease;
}

#playerContainer.hidden {
  display: none;
}

#playerContainer.active {
  --player-height: 50vh;
}

#playerContainer.fullscreen {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  z-index: 9999;
}

#playerWrapper {
  position: relative;
  width: 100%;
  height: 50vh;
  max-height: 70vh;
}

#playerContainer.fullscreen #playerWrapper {
  height: 100vh;
  max-height: 100vh;
}

#videoPlayer {
  width: 100%;
  height: 100%;
  object-fit: contain;
  background: #000;
}

#playerOverlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(transparent 60%, rgba(0,0,0,0.8));
  opacity: 0;
  transition: opacity 0.3s ease;
  display: flex;
  align-items: flex-end;
  cursor: pointer;
}

#playerWrapper:hover #playerOverlay,
#playerOverlay:focus-within {
  opacity: 1;
}

#playerControls {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 16px 24px;
}

.player-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.player-info .channel-num {
  background: var(--accent);
  color: white;
  padding: 4px 10px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 14px;
}

.player-info .channel-name {
  font-weight: 600;
  font-size: 16px;
}

.player-info .program-name {
  color: var(--text-secondary);
  font-size: 14px;
}

.player-actions {
  display: flex;
  gap: 8px;
}

.ctrl-btn {
  width: 40px;
  height: 40px;
  border: none;
  background: rgba(255,255,255,0.1);
  color: white;
  border-radius: var(--radius-sm);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
  backdrop-filter: blur(8px);
}

.ctrl-btn svg {
  width: 20px;
  height: 20px;
}

.ctrl-btn:hover {
  background: rgba(255,255,255,0.2);
}

.ctrl-btn.is-favorite svg {
  fill: var(--warning);
  stroke: var(--warning);
}

#playerLoading, #playerError {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  background: rgba(0,0,0,0.7);
  color: white;
  font-size: 16px;
  z-index: 5;
}

#playerLoading.hidden, #playerError.hidden {
  display: none;
}

.spinner {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255,255,255,0.2);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Views ===== */
.view {
  display: none;
  flex: 1;
  overflow-y: auto;
  padding: 24px 28px;
}

.view.active {
  display: flex;
  flex-direction: column;
}

.view-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.view-header h2 {
  font-size: 24px;
  font-weight: 700;
}

.view-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
}

/* ===== Search Box ===== */
.search-box {
  position: relative;
  display: flex;
  align-items: center;
}

.search-box svg {
  position: absolute;
  left: 12px;
  width: 16px;
  height: 16px;
  color: var(--text-muted);
  pointer-events: none;
}

.search-box input {
  padding: 8px 12px 8px 36px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  width: 240px;
  transition: border-color 0.15s ease;
}

.search-box input:focus {
  outline: none;
  border-color: var(--border-focus);
}

select {
  padding: 8px 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}

select:focus {
  outline: none;
  border-color: var(--border-focus);
}

/* ===== Channel Grid ===== */
.channel-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 14px;
  flex: 1;
}

.channel-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  cursor: pointer;
  transition: all 0.2s ease;
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
}

.channel-card:hover {
  background: var(--bg-card-hover);
  border-color: var(--accent);
  transform: translateY(-2px);
  box-shadow: 0 4px 20px var(--accent-glow);
}

.channel-card.playing {
  border-color: var(--accent);
  box-shadow: 0 0 20px var(--accent-glow);
}

.channel-card-top {
  display: flex;
  align-items: center;
  gap: 12px;
}

.channel-logo {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  object-fit: contain;
  background: var(--bg-input);
  flex-shrink: 0;
}

.channel-logo-placeholder {
  width: 48px;
  height: 48px;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), #a78bfa);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 16px;
  color: white;
  flex-shrink: 0;
}

.channel-card-info {
  flex: 1;
  min-width: 0;
}

.channel-card-info .ch-number {
  font-size: 12px;
  color: var(--text-muted);
  font-weight: 600;
}

.channel-card-info .ch-name {
  font-size: 14px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.channel-card-info .ch-program {
  font-size: 12px;
  color: var(--text-secondary);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-top: 2px;
}

.channel-card .fav-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: all 0.15s ease;
  opacity: 0;
}

.channel-card:hover .fav-btn {
  opacity: 1;
}

.channel-card .fav-btn:hover {
  background: rgba(255,255,255,0.1);
}

.channel-card .fav-btn.is-favorite {
  opacity: 1;
  color: var(--warning);
}

.channel-card .fav-btn svg {
  width: 16px;
  height: 16px;
}

.channel-card .source-badge {
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 4px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.source-badge.dispatcharr { background: rgba(99,102,241,0.2); color: #818cf8; }
.source-badge.hdhr { background: rgba(34,197,94,0.2); color: #4ade80; }
.source-badge.channels { background: rgba(245,158,11,0.2); color: #fbbf24; }

.channel-card-bottom {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

/* Playing indicator */
.now-playing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
}

.now-playing-indicator span {
  display: inline-block;
  width: 3px;
  height: 12px;
  background: var(--accent);
  border-radius: 2px;
  animation: bars 0.8s ease-in-out infinite;
}

.now-playing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.now-playing-indicator span:nth-child(3) { animation-delay: 0.4s; }

@keyframes bars {
  0%, 100% { height: 4px; }
  50% { height: 14px; }
}

/* ===== Guide ===== */
.guide-container {
  flex: 1;
  overflow: auto;
}

.guide-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 800px;
}

.guide-table th {
  position: sticky;
  top: 0;
  background: var(--bg-secondary);
  padding: 10px 14px;
  text-align: left;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  border-bottom: 1px solid var(--border);
  z-index: 10;
}

.guide-table td {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  vertical-align: middle;
}

.guide-row {
  cursor: pointer;
  transition: background 0.1s ease;
}

.guide-row:hover {
  background: var(--bg-card-hover);
}

.guide-channel-cell {
  display: flex;
  align-items: center;
  gap: 10px;
  white-space: nowrap;
}

.guide-channel-cell img {
  width: 32px;
  height: 32px;
  border-radius: 4px;
  object-fit: contain;
  background: var(--bg-input);
}

/* ===== Empty State ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px;
  padding: 60px 20px;
  text-align: center;
  color: var(--text-muted);
  grid-column: 1 / -1;
}

.empty-state h3 {
  color: var(--text-secondary);
  font-size: 18px;
}

.empty-state p {
  font-size: 14px;
  max-width: 320px;
}

/* ===== Settings ===== */
.settings-content {
  display: flex;
  flex-direction: column;
  gap: 20px;
  max-width: 680px;
}

.settings-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.settings-card-header {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
}

.settings-card-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.settings-card-header p {
  font-size: 13px;
  color: var(--text-secondary);
}

.settings-card-header > div:nth-child(2) {
  flex: 1;
}

.source-icon {
  width: 40px;
  height: 40px;
  border-radius: var(--radius-sm);
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 18px;
  color: white;
  flex-shrink: 0;
}

.source-icon.dispatcharr { background: linear-gradient(135deg, #6366f1, #818cf8); }
.source-icon.hdhr { background: linear-gradient(135deg, #16a34a, #22c55e); }
.source-icon.channels { background: linear-gradient(135deg, #d97706, #f59e0b); }
.source-icon.playback { background: linear-gradient(135deg, #8b5cf6, #a78bfa); }

.settings-card-body {
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group label {
  font-size: 13px;
  font-weight: 500;
  color: var(--text-secondary);
}

.form-group input[type="url"],
.form-group input[type="text"],
.form-group select {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  transition: border-color 0.15s ease;
}

.form-group input:focus,
.form-group select:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: 0 0 0 3px var(--accent-glow);
}

.form-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.connection-status {
  font-size: 13px;
  font-weight: 500;
}

.connection-status.success { color: var(--success); }
.connection-status.error { color: var(--error); }
.connection-status.testing { color: var(--text-secondary); }

.admin-status-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
}

/* Toggle Switch */
.toggle {
  position: relative;
  display: inline-block;
  width: 44px;
  height: 24px;
  flex-shrink: 0;
}

.toggle input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  inset: 0;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 24px;
  cursor: pointer;
  transition: all 0.2s ease;
}

.toggle-slider::before {
  content: '';
  position: absolute;
  width: 18px;
  height: 18px;
  left: 2px;
  top: 2px;
  background: var(--text-secondary);
  border-radius: 50%;
  transition: all 0.2s ease;
}

.toggle input:checked + .toggle-slider {
  background: var(--accent);
  border-color: var(--accent);
}

.toggle input:checked + .toggle-slider::before {
  transform: translateX(20px);
  background: white;
}

/* Buttons */
.btn-primary {
  padding: 10px 20px;
  background: var(--accent);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease;
}

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-secondary {
  padding: 10px 20px;
  background: var(--bg-input);
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-secondary:hover {
  background: var(--bg-card-hover);
  border-color: var(--text-muted);
}

.btn-danger {
  padding: 10px 20px;
  background: transparent;
  color: var(--error);
  border: 1px solid var(--error);
  border-radius: var(--radius-sm);
  font-family: inherit;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
}

.btn-danger:hover {
  background: rgba(239,68,68,0.1);
}

/* Source Entries */
.source-entry {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.source-entry-header {
  display: flex;
  align-items: center;
  gap: 10px;
}

.source-name-input {
  flex: 1;
  padding: 8px 12px;
  background: var(--bg-secondary);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text-primary);
  font-family: inherit;
  font-size: 14px;
  font-weight: 600;
}

.source-name-input:focus {
  outline: none;
  border-color: var(--border-focus);
}

.btn-icon {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: var(--radius-sm);
  transition: all 0.15s ease;
}

.btn-icon:hover {
  background: rgba(239,68,68,0.1);
  color: var(--error);
}

.settings-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

#settingsProtected.settings-locked {
  opacity: 0.55;
  pointer-events: none;
  user-select: none;
}

/* ===== Scrollbar ===== */
::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

::-webkit-scrollbar-track {
  background: transparent;
}

::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 4px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--text-muted);
}

/* ===== Toast Notifications ===== */
.toast-container {
  position: fixed;
  bottom: 20px;
  right: 20px;
  z-index: 10000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 20px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 14px;
  color: var(--text-primary);
  box-shadow: 0 8px 30px rgba(0,0,0,0.4);
  animation: slideIn 0.3s ease;
  max-width: 360px;
}

.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--error); }
.toast.info { border-left: 3px solid var(--accent); }

@keyframes slideIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: translateX(0); opacity: 1; }
}

/* ===== Responsive ===== */
@media (max-width: 768px) {
  #sidebar {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    height: auto;
    flex-direction: row;
    border-right: none;
    border-top: 1px solid var(--border);
    z-index: 1000;
  }

  .sidebar-header, .sidebar-footer {
    display: none;
  }

  .sidebar-nav {
    flex-direction: row;
    justify-content: space-around;
    padding: 6px;
    width: 100%;
  }

  .nav-btn {
    flex-direction: column;
    gap: 4px;
    font-size: 11px;
    padding: 8px;
  }

  .nav-btn svg {
    width: 18px;
    height: 18px;
  }

  #mainContent {
    margin-left: 0;
    padding-bottom: 70px;
  }

  .view {
    padding: 16px;
  }

  .channel-grid {
    grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
    gap: 10px;
  }

  .view-header {
    flex-direction: column;
    align-items: flex-start;
  }

  .search-box input {
    width: 100%;
  }

  #playerWrapper {
    height: 30vh;
  }
}
