:root {
  --bg: #000000;
  --bg-elevated: #1c1c1e;
  --bg-card: #1c1c1e;
  --bg-input: #2c2c2e;
  --border: #38383a;
  --text: #ffffff;
  --text-secondary: #8e8e93;
  --text-tertiary: #636366;
  --accent: #0a84ff;
  --favorite: #ff375f;
  --danger: #ff453a;
  --success: #30d158;
  --tab-height: calc(56px + env(safe-area-inset-bottom));
  --header-height: calc(44px + env(safe-area-inset-top));
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Helvetica Neue", sans-serif;
  font-size: 16px;
  overscroll-behavior: none;
  -webkit-font-smoothing: antialiased;
  height: 100%;
  overflow: hidden;
}
body { display: flex; flex-direction: column; }

.hidden { display: none !important; }

/* HEADER */
#header {
  position: fixed; top: 0; left: 0; right: 0;
  height: var(--header-height);
  padding-top: env(safe-area-inset-top);
  background: rgba(0,0,0,0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  display: flex; align-items: center; justify-content: space-between;
  padding-left: 16px; padding-right: 12px;
  z-index: 100;
  border-bottom: 0.5px solid var(--border);
}
#viewTitle {
  margin: 0; font-size: 20px; font-weight: 700; letter-spacing: -0.4px;
}
.icon-btn {
  background: none; border: none; color: var(--text);
  padding: 8px; border-radius: 8px; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}
.icon-btn:active { background: var(--bg-input); }

/* SEARCH BAR */
.search-bar {
  position: fixed; top: 0; left: 0; right: 0;
  padding-top: env(safe-area-inset-top);
  height: var(--header-height);
  background: rgba(0,0,0,0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  display: flex; align-items: center; gap: 8px;
  padding-left: 12px; padding-right: 12px;
  z-index: 101;
}
#searchInput {
  flex: 1; height: 36px; border-radius: 10px; border: none;
  background: var(--bg-input); color: var(--text); font-size: 16px;
  padding: 0 12px; outline: none;
}
#searchCancel {
  background: none; border: none; color: var(--accent);
  font-size: 16px; padding: 8px; cursor: pointer;
}

/* MAIN */
#main {
  flex: 1;
  padding-top: var(--header-height);
  padding-bottom: var(--tab-height);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.view { display: none; padding: 12px; }
.view.active { display: block; }

/* CHANNEL GRID */
.channel-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}
@media (min-width: 500px) { .channel-grid { grid-template-columns: repeat(4, 1fr); } }
@media (min-width: 700px) { .channel-grid { grid-template-columns: repeat(6, 1fr); } }

.channel {
  background: var(--bg-card);
  border-radius: 12px;
  padding: 12px 8px;
  display: flex; flex-direction: column; align-items: center;
  cursor: pointer;
  transition: transform 0.1s, background 0.15s;
  aspect-ratio: 1 / 1.15;
  content-visibility: auto;
  contain-intrinsic-size: 0 140px;
}
.channel:active { transform: scale(0.96); background: var(--bg-input); }
.channel-logo {
  width: 100%; aspect-ratio: 1; flex-shrink: 0;
  border-radius: 10px; background: #000;
  display: flex; align-items: center; justify-content: center;
  overflow: hidden;
  margin-bottom: 6px;
}
.channel-logo img { max-width: 90%; max-height: 90%; object-fit: contain; }
.channel-logo .initial {
  font-size: 24px; font-weight: 600; color: var(--text-secondary);
}
.channel-name {
  font-size: 12px; font-weight: 500;
  text-align: center; line-height: 1.2;
  overflow: hidden; text-overflow: ellipsis;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  width: 100%;
}

/* LIST (categories) */
.list { display: flex; flex-direction: column; gap: 1px; background: var(--border); border-radius: 12px; overflow: hidden; }
.list-item {
  background: var(--bg-card); padding: 14px 16px;
  display: flex; align-items: center; justify-content: space-between;
  cursor: pointer; font-size: 16px;
  touch-action: manipulation;
  -webkit-user-select: none; user-select: none;
}
.list-item > * { pointer-events: none; }
.list-item:active { background: var(--bg-input); }
.list-item .badge {
  color: var(--text-secondary); font-size: 14px;
}
.list-item .chevron {
  color: var(--text-tertiary); margin-left: 8px;
}

.back-btn {
  background: none; border: none; color: var(--accent);
  font-size: 17px; padding: 8px 0; cursor: pointer; margin-bottom: 8px;
}
#categoryChannelsTitle { margin: 0 0 12px 0; font-size: 20px; font-weight: 700; }

/* EMPTY / LOADING */
.load-more {
  grid-column: 1 / -1;
  padding: 14px; text-align: center;
  background: var(--bg-card); border-radius: 12px;
  color: var(--accent); font-weight: 600; cursor: pointer;
  margin-top: 8px;
}
.load-more:active { opacity: 0.6; }
.big-hint {
  grid-column: 1 / -1;
  padding: 40px 20px; text-align: center;
  background: var(--bg-card); border-radius: 12px;
  color: var(--text-secondary); line-height: 1.6;
}
.big-hint strong { color: var(--text); }

.empty, .loading {
  padding: 60px 20px; text-align: center; color: var(--text-secondary);
}
.empty p { margin: 8px 0; }
.empty .hint { color: var(--text-tertiary); font-size: 14px; }
.empty button { margin-top: 20px; }
.spinner {
  width: 32px; height: 32px; border: 3px solid var(--bg-input);
  border-top-color: var(--accent); border-radius: 50%;
  animation: spin 0.8s linear infinite; margin: 0 auto 12px;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* SETTINGS */
.settings-group {
  background: var(--bg-card); border-radius: 12px;
  padding: 14px; margin-bottom: 16px;
}
.settings-label {
  display: block; font-size: 13px; text-transform: uppercase;
  color: var(--text-secondary); margin-bottom: 8px; letter-spacing: 0.3px;
}
.settings-group input[type="url"], .settings-group input[type="text"] {
  width: 100%; height: 40px; padding: 0 12px;
  background: var(--bg-input); border: none; border-radius: 8px;
  color: var(--text); font-size: 15px; outline: none;
}
.settings-group textarea {
  width: 100%; padding: 10px 12px;
  background: var(--bg-input); border: none; border-radius: 8px;
  color: var(--text); font-size: 13px; outline: none;
  font-family: ui-monospace, Menlo, monospace;
  resize: vertical; min-height: 100px;
}
.row-btns { display: flex; gap: 8px; margin-top: 8px; flex-wrap: wrap; }
.row-btns > * { flex: 1; min-width: 100px; }
button.small, .file-btn.small {
  height: 36px; padding: 0 12px; font-size: 14px; margin: 0;
  display: flex; align-items: center; justify-content: center;
  border-radius: 8px; cursor: pointer;
}
.file-btn { position: relative; overflow: hidden; }
.file-btn input[type="file"] {
  position: absolute; inset: 0; opacity: 0; cursor: pointer;
}
.settings-hint { color: var(--text-tertiary); font-size: 13px; margin: 8px 0 0 0; line-height: 1.4; }

button.primary, button.secondary, button.danger {
  width: 100%; height: 44px; border: none; border-radius: 10px;
  font-size: 16px; font-weight: 600; cursor: pointer;
  margin-top: 8px;
}
button.primary { background: var(--accent); color: white; }
button.primary:active { opacity: 0.7; }
button.secondary { background: var(--bg-input); color: var(--text); }
button.secondary:active { opacity: 0.7; }
button.danger { background: transparent; color: var(--danger); border: 1px solid var(--danger); }
button.danger:active { background: rgba(255,69,58,0.15); }

.test-output {
  background: #000; color: #30d158; font-size: 12px;
  padding: 10px; border-radius: 8px; margin-top: 10px;
  max-height: 240px; overflow: auto; white-space: pre-wrap; word-break: break-all;
  font-family: ui-monospace, Menlo, monospace;
}
.test-output.error { color: #ff453a; }

/* TAB BAR */
#tabBar {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--tab-height);
  padding-bottom: env(safe-area-inset-bottom);
  background: rgba(0,0,0,0.85);
  backdrop-filter: blur(20px);
  -webkit-backdrop-filter: blur(20px);
  border-top: 0.5px solid var(--border);
  display: flex; z-index: 100;
}
.tab {
  flex: 1; background: none; border: none; color: var(--text-tertiary);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; padding: 6px 0; cursor: pointer;
  font-size: 10px; font-weight: 500;
}
.tab.active { color: var(--accent); }
.tab svg { opacity: 0.85; }

/* PLAYER */
.player {
  position: fixed; inset: 0; background: #000;
  z-index: 200; display: flex; flex-direction: column;
}
.player-topbar {
  position: absolute; top: 0; left: 0; right: 0;
  padding-top: env(safe-area-inset-top);
  height: calc(52px + env(safe-area-inset-top));
  display: flex; align-items: center; justify-content: space-between;
  padding-left: 8px; padding-right: 8px;
  background: linear-gradient(to bottom, rgba(0,0,0,0.85), transparent);
  z-index: 10;
}
.player-title {
  flex: 1; text-align: center; font-size: 16px; font-weight: 600;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  padding: 0 12px;
}
#playerFav.active { color: var(--favorite); }
#video {
  width: 100%; height: 100%; background: #000;
  object-fit: contain;
}
.player-error {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center; text-align: center;
  padding: 20px; background: #000;
}
.player-error p { color: var(--text-secondary); margin-bottom: 20px; }
.player-error button {
  background: var(--accent); color: white; border: none;
  padding: 10px 24px; border-radius: 10px; font-size: 16px; cursor: pointer;
}
