:root {
  --bg: #0a0a14;
  --surface: #15151f;
  --surface-2: #22222f;
  --text: #f2f2f7;
  --text-dim: #9b9ba8;
  --accent: #ff3b5c;
  --radius: 14px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0; padding: 0; height: 100%;
  background: var(--bg); color: var(--text);
  font-family: -apple-system, "Roboto", "Segoe UI", system-ui, sans-serif;
  overscroll-behavior: none;
  user-select: none; -webkit-user-select: none;
}

.screen {
  position: fixed; inset: 0;
  display: flex; flex-direction: column; overflow: hidden;
  padding-left: var(--safe-left); padding-right: var(--safe-right);
}
.hidden { display: none !important; }

/* ============ HEADER ============ */
.app-header {
  padding: calc(var(--safe-top) + 14px) 14px 10px;
  background: var(--bg);
  border-bottom: 1px solid #1e1e2a;
  flex-shrink: 0;
}
.brand { display: flex; align-items: center; gap: 10px; margin-bottom: 12px; }
.brand-logo { width: 32px; height: 32px; border-radius: 8px; }
.brand h1 { margin: 0; font-size: 20px; font-weight: 800; letter-spacing: -0.5px; }
.brand h1 span {
  background: linear-gradient(90deg, #3aa6ff, #a24bff, #ff8a3a);
  -webkit-background-clip: text; background-clip: text;
  -webkit-text-fill-color: transparent;
}
#search {
  width: 100%; padding: 11px 14px;
  background: var(--surface); border: none; border-radius: var(--radius);
  color: var(--text); font-size: 16px; outline: none;
}
#search::placeholder { color: var(--text-dim); }

.tabs { display: flex; gap: 8px; margin-top: 12px; }
.tab {
  padding: 7px 16px; background: var(--surface); border: none;
  border-radius: 20px; color: var(--text-dim);
  font-size: 14px; font-weight: 600;
}
.tab.active { background: var(--accent); color: #fff; }

.categories {
  display: flex; gap: 8px; margin-top: 10px;
  overflow-x: auto; padding-bottom: 4px; scrollbar-width: none;
}
.categories::-webkit-scrollbar { display: none; }
.cat-chip {
  padding: 5px 12px; background: var(--surface-2); border: none;
  border-radius: 14px; color: var(--text-dim); font-size: 12px;
  white-space: nowrap; flex-shrink: 0;
}
.cat-chip.active { background: #34344a; color: var(--text); }

/* ============ GRID ============ */
.channel-grid {
  flex: 1; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 12px 12px calc(var(--safe-bottom) + 20px);
  display: grid; grid-template-columns: repeat(3, 1fr);
  gap: 10px; align-content: start;
}
@media (min-width: 500px)  { .channel-grid { grid-template-columns: repeat(4, 1fr); } }
@media (min-width: 900px)  { .channel-grid { grid-template-columns: repeat(6, 1fr); } }
@media (min-width: 1400px) { .channel-grid { grid-template-columns: repeat(8, 1fr); } }

.channel-card {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 8px; padding: 12px 6px;
  background: var(--surface); border-radius: var(--radius); border: none;
  color: var(--text); text-align: center; overflow: hidden;
  transition: transform 0.1s ease, background 0.15s ease;
}
.channel-card:active { transform: scale(0.95); background: var(--surface-2); }
.channel-card img,
.channel-card .fallback-logo {
  width: 44px; height: 44px; object-fit: contain;
  border-radius: 8px; background: #fff2;
  display: flex; align-items: center; justify-content: center;
  font-size: 20px; font-weight: 700; color: var(--text-dim);
}
.channel-card .name {
  font-size: 11px; line-height: 1.2;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical;
  overflow: hidden; max-width: 100%; word-break: break-word;
}
.channel-card .fav-star {
  position: absolute; top: 4px; right: 6px;
  font-size: 14px; color: var(--accent);
}

/* ============ PLAYER ============ */
#player-screen { background: #000; z-index: 100; }
.player-wrap {
  flex: 1; position: relative;
  display: flex; align-items: center; justify-content: center;
  background: #000; min-height: 0;
}
#video {
  width: 100%; height: 100%;
  object-fit: contain; background: #000;
}
.player-overlay {
  position: absolute; inset: 0;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px; color: var(--text); background: #000a;
  pointer-events: none; text-align: center; padding: 20px;
}
.player-overlay small { color: var(--text-dim); font-size: 12px; max-width: 80%; }
.btn-retry {
  margin-top: 14px; padding: 10px 24px;
  background: var(--accent); border: none; border-radius: 22px;
  color: #fff; font-size: 14px; font-weight: 600;
  pointer-events: auto;
}
.btn-retry:active { opacity: 0.8; }

.player-bar {
  display: flex; align-items: center; gap: 8px;
  padding: 10px calc(var(--safe-bottom) + 12px) 14px 12px;
  background: var(--bg); flex-shrink: 0;
}
.player-title { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.player-title span {
  font-size: 15px; font-weight: 600;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.player-title small { font-size: 11px; color: var(--text-dim); }

.icon-btn {
  width: 42px; height: 42px; border: none; border-radius: 50%;
  background: var(--surface); color: var(--text); font-size: 20px;
  display: flex; align-items: center; justify-content: center;
  flex-shrink: 0;
}
.icon-btn.small { width: 38px; height: 38px; font-size: 22px; }
.icon-btn:active { background: var(--surface-2); }
.icon-btn.faved { color: var(--accent); }

/* ============ UTILS ============ */
.spinner {
  width: 34px; height: 34px;
  border: 3px solid #ffffff30; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.grid-status {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 12px; color: var(--text-dim); font-size: 14px;
}

/* ============ LOAD SCREEN / TEST MODE ============ */
#load-screen { align-items: center; justify-content: center; padding: 20px; overflow-y: auto; }
.load-card { width: 100%; max-width: 440px; text-align: center; }
.load-card h1 { margin: 0 0 4px; font-size: 30px; font-weight: 800; letter-spacing: -0.5px; }
.load-card h1 span { background: linear-gradient(90deg, #3aa6ff, #a24bff, #ff8a3a); -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; }
.load-sub { color: var(--text-dim); margin: 0 0 24px; }
.drop { display: block; padding: 26px 16px; border: 2px dashed #34344a; border-radius: var(--radius); background: var(--surface); cursor: pointer; }
.drop strong { display: block; font-size: 17px; } .drop small { color: var(--text-dim); }
.drop:active, .drop:hover { border-color: var(--accent); }
.or { margin: 18px 0 12px; color: var(--text-dim); font-size: 13px; }
.url-form { display: flex; gap: 8px; }
.url-form input { flex: 1; min-width: 0; padding: 12px 14px; border-radius: 12px; border: 1px solid #2a2a3a; background: var(--surface); color: var(--text); font-size: 15px; user-select: text; -webkit-user-select: text; }
.btn-primary { padding: 12px 20px; border: 0; border-radius: 12px; background: var(--accent); color: #fff; font-weight: 700; font-size: 15px; cursor: pointer; }
.btn-primary:disabled, .link-btn:disabled { opacity: .5; }
.link-btn { background: none; border: 0; color: #7db8ff; font-size: 14px; cursor: pointer; padding: 6px 8px; }
.load-card .link-btn { margin-top: 18px; }
.load-error { margin: 14px 0 0; padding: 10px 12px; border-radius: 10px; background: rgba(255,59,92,.15); color: #ff8da1; font-size: 14px; }
.privacy { margin: 22px 0 0; color: var(--text-dim); font-size: 13px; }
.badge-test { font-size: 10px; font-weight: 800; letter-spacing: .08em; padding: 3px 7px; border-radius: 6px; background: rgba(255,138,58,.18); color: #ffb06e; }
.brand .link-btn { margin-left: auto; }
