* { margin: 0; padding: 0; box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
  --bg: #09090b;
  --surface: #111113;
  --surface-hover: #1a1a1f;
  --surface-active: #222228;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --text: #f0f0f0;
  --text-muted: #71717a;
  --border: #27272a;
  --error: #ef4444;
  --radius: 8px;
  --sidebar-w: 220px;
  --topbar-h: 56px;
}

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

/* ===== VIEWS ===== */
.view { display: none; height: 100vh; width: 100vw; }
.view.active { display: flex; }

/* ===== LOGIN ===== */
#login-view {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}

.login-card {
  width: 100%;
  max-width: 400px;
  padding: 40px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 4px;
}

.logo h1 {
  font-size: 24px;
  font-weight: 700;
  letter-spacing: -0.5px;
}

.subtitle {
  color: var(--text-muted);
  font-size: 14px;
  margin-bottom: 28px;
}

.login-tabs {
  display: flex;
  gap: 0;
  margin-bottom: 24px;
  background: var(--bg);
  border-radius: var(--radius);
  padding: 3px;
}

.login-tab {
  flex: 1;
  padding: 8px 0;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

.login-tab.active {
  background: var(--surface-hover);
  color: var(--text);
}

.login-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

input[type="url"],
input[type="text"],
input[type="password"],
input[type="search"] {
  width: 100%;
  padding: 10px 14px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

input:focus {
  border-color: var(--primary);
}

input::placeholder { color: var(--text-muted); }

.btn-primary {
  padding: 10px 20px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover { background: var(--primary-hover); }
.btn-primary:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-sm { padding: 8px 16px; font-size: 13px; }

.progress-info {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.progress-bar {
  width: 100%;
  height: 4px;
  background: var(--bg);
  border-radius: 2px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0%;
  background: var(--primary);
  border-radius: 2px;
  transition: width 0.3s ease;
}

.progress-text {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.3;
}

.error-msg {
  margin-top: 12px;
  padding: 10px 14px;
  background: rgba(239, 68, 68, 0.1);
  border: 1px solid rgba(239, 68, 68, 0.3);
  border-radius: var(--radius);
  color: var(--error);
  font-size: 13px;
}

/* ===== TOPBAR ===== */
.topbar {
  display: flex;
  align-items: center;
  height: var(--topbar-h);
  padding: 0 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  gap: 16px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 160px;
}

.topbar-title {
  font-size: 16px;
  font-weight: 700;
  letter-spacing: -0.3px;
}

.btn-icon {
  width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.btn-icon:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.btn-logout {
  border-color: #dc2626 !important;
  color: #dc2626 !important;
}

.btn-logout:hover {
  background: #dc2626 !important;
  color: #fff !important;
}

.topbar-tabs {
  display: flex;
  gap: 4px;
  flex: 1;
  justify-content: center;
}

.tab {
  padding: 6px 16px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

.tab:not(.active):hover {
  background: var(--surface-hover);
  color: var(--text);
}

.topbar-right {
  min-width: 200px;
  display: flex;
  align-items: center;
  gap: 8px;
  justify-content: flex-end;
}

.sort-wrapper select {
  padding: 7px 28px 7px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 12px;
  cursor: pointer;
  outline: none;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='%2371717a'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  transition: border-color 0.2s;
}

.sort-wrapper select:focus {
  border-color: var(--primary);
}

.sort-wrapper select option {
  background: var(--surface);
  color: var(--text);
}

#search-input {
  width: 200px;
  padding: 7px 12px;
  font-size: 13px;
}

/* ===== DASHBOARD BODY ===== */
.dashboard-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

#dashboard-view {
  flex-direction: column;
}

/* ===== SIDEBAR ===== */
#sidebar {
  width: var(--sidebar-w);
  min-width: var(--sidebar-w);
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

#sidebar::-webkit-scrollbar { width: 4px; }
#sidebar::-webkit-scrollbar-track { background: transparent; }
#sidebar::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

.cat-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 8px 12px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13px;
  cursor: pointer;
  border-radius: 6px;
  text-align: left;
  transition: all 0.15s;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.cat-item:hover {
  background: var(--surface-hover);
  color: var(--text);
}

.cat-item.active {
  background: var(--surface-active);
  color: var(--text);
}

.cat-count {
  font-size: 11px;
  color: var(--text-muted);
  background: var(--bg);
  padding: 1px 6px;
  border-radius: 10px;
  flex-shrink: 0;
  margin-left: 8px;
}

/* ===== CONTENT AREA ===== */
#content-area {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
}

#content-area::-webkit-scrollbar { width: 6px; }
#content-area::-webkit-scrollbar-track { background: transparent; }
#content-area::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

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

.grid-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  cursor: pointer;
  transition: all 0.2s;
}

.grid-item:hover {
  border-color: var(--primary);
  transform: translateY(-2px);
}

.grid-item-poster {
  width: 100%;
  aspect-ratio: 2/3;
  background: var(--surface-hover);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  position: relative;
}

.grid-item-poster img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.grid-item-poster .placeholder-icon {
  color: var(--text-muted);
  opacity: 0.3;
}

.grid-item-info {
  padding: 8px 10px;
}

.grid-item-name {
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.grid-item-group {
  font-size: 11px;
  color: var(--text-muted);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Live TV - wider cards */
.grid.live-grid {
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
}

.live-grid .grid-item-poster {
  aspect-ratio: 16/9;
}

/* ===== EMPTY / LOADING ===== */
.empty, .loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 300px;
  color: var(--text-muted);
  font-size: 14px;
}

.spinner {
  width: 32px;
  height: 32px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin-bottom: 12px;
}

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

/* ===== PLAYER ===== */
#player-view {
  flex-direction: column;
  background: #000;
  position: relative;
}

.player-topbar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: rgba(0,0,0,0.8);
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 10;
  opacity: 1;
  transition: opacity 0.3s;
}

#player-view:not(:hover) .player-topbar {
  opacity: 0;
}

#player-view:not(:hover) .player-topbar:focus-within {
  opacity: 1;
}

.player-name {
  font-size: 14px;
  font-weight: 600;
}

#video-player {
  width: 100%;
  height: 100%;
  background: #000;
}

/* Plyr wrapper fills player view */
#player-view .plyr {
  flex: 1;
  width: 100%;
  height: 100%;
  --plyr-color-main: var(--primary);
  --plyr-video-background: #000;
}

#player-view .plyr video {
  width: 100%;
  height: 100%;
  object-fit: contain;
}

.player-error {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  color: var(--text-muted);
}

/* ===== SERIES VIEW ===== */
#series-view {
  flex-direction: column;
  background: var(--bg);
}

.series-body {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

.season-block {
  margin-bottom: 24px;
}

.season-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--border);
}

.episode-list {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.episode-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s;
}

.episode-item:hover {
  border-color: var(--primary);
  background: var(--surface-hover);
}

.episode-num {
  color: var(--primary);
  font-weight: 700;
  font-size: 14px;
  min-width: 32px;
}

.episode-name {
  font-size: 13px;
  flex: 1;
}

.episode-duration {
  font-size: 12px;
  color: var(--text-muted);
}

/* ===== EPISODE NAV BUTTONS ===== */
.ep-nav {
  display: flex;
  gap: 4px;
  margin-left: auto;
}

.btn-ep-nav {
  width: 36px;
  height: 36px;
  border-color: rgba(255,255,255,0.2);
  color: rgba(255,255,255,0.7);
}

.btn-ep-nav:hover:not(:disabled) {
  background: rgba(255,255,255,0.1);
  color: #fff;
  border-color: rgba(255,255,255,0.4);
}

.btn-ep-nav:disabled {
  opacity: 0.25;
  cursor: not-allowed;
}

/* ===== NEXT EPISODE OVERLAY ===== */
.next-ep-overlay {
  position: absolute;
  bottom: 80px;
  right: 24px;
  z-index: 20;
  animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(20px); }
  to { opacity: 1; transform: translateY(0); }
}

.next-ep-card {
  background: rgba(17, 17, 19, 0.95);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  min-width: 260px;
  backdrop-filter: blur(8px);
}

.next-ep-label {
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

#next-ep-countdown {
  color: var(--primary);
  font-weight: 700;
}

.next-ep-name {
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 300px;
}

.next-ep-actions {
  display: flex;
  gap: 8px;
  margin-top: 4px;
}

.btn-cancel {
  padding: 8px 16px;
  font-size: 13px;
  background: var(--surface-hover);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: all 0.2s;
}

.btn-cancel:hover {
  background: var(--surface-active);
  color: var(--text);
}

/* ===== SETTINGS VIEW ===== */
#settings-view {
  flex-direction: column;
  background: var(--bg);
}

.settings-body {
  flex: 1;
  overflow-y: auto;
  padding: 32px;
  display: flex;
  justify-content: center;
}

.settings-card {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

.settings-section-title {
  font-size: 18px;
  font-weight: 700;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.setting-item {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 16px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.setting-info {
  flex: 1;
}

.setting-info label {
  font-size: 14px;
  font-weight: 600;
  display: block;
  margin-bottom: 4px;
}

.setting-desc {
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1.4;
}

.setting-input {
  width: 80px;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 16px;
  font-weight: 600;
  text-align: center;
  outline: none;
  flex-shrink: 0;
}

.setting-input:focus {
  border-color: var(--primary);
}

/* Remove spinner arrows on number inputs */
.setting-input::-webkit-inner-spin-button,
.setting-input::-webkit-outer-spin-button {
  -webkit-appearance: none;
  margin: 0;
}

.setting-input[type=number] {
  -moz-appearance: textfield;
}

/* ===== RESPONSIVE ===== */
@media (max-width: 768px) {
  .topbar-tabs { gap: 2px; }
  .tab { padding: 6px 10px; font-size: 12px; }
  #search-input { width: 140px; }
  #sidebar { width: 160px; min-width: 160px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 8px; }
  .login-card { margin: 16px; padding: 24px; }
}

@media (max-width: 520px) {
  .topbar { flex-wrap: wrap; height: auto; padding: 8px; gap: 8px; }
  .topbar-left { min-width: auto; }
  .topbar-right { min-width: auto; width: 100%; }
  #search-input { width: 100%; }
  #sidebar { display: none; }
  #content-area { padding: 8px; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); }
}
