:root {
  --bg: #0d1117;
  --panel: #161b22;
  --panel-2: #1c2330;
  --border: #2a3340;
  --text: #e6edf3;
  --muted: #8b949e;
  --accent: #1f6feb;
  --accent-2: #58a6ff;
  --ok: #2ea043;
  --warn: #d29922;
  --bad: #f85149;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
.hidden { display: none !important; }
a { color: var(--accent-2); }
h1, h2, h3 { margin: 0; font-weight: 600; }
h2 { font-size: 18px; }

button, input, select, textarea {
  font: inherit;
  color: inherit;
}
button {
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--panel-2);
  border-radius: 8px;
  padding: 8px 14px;
}
button:hover { border-color: var(--accent-2); }
button:disabled { opacity: .55; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #2f7bf5; }
button.danger { color: var(--bad); }
button.ghost { background: transparent; }
button.small { padding: 5px 10px; font-size: 13px; }
.icon-btn { background: transparent; border: none; font-size: 20px; padding: 4px 10px; line-height: 1; }

input[type=text], input[type=password], input[type=search], input[type=url], select, textarea {
  width: 100%;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 9px 11px;
  outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent-2); }
textarea { font-family: ui-monospace, Consolas, monospace; font-size: 13px; resize: vertical; }
label { display: block; margin: 12px 0 5px; color: var(--muted); font-size: 13px; }
.check { display: flex; gap: 8px; align-items: flex-start; color: var(--text); font-size: 14px; }
.check input { margin-top: 3px; }
.error { color: var(--bad); min-height: 1.2em; margin: 10px 0 0; }
.muted { color: var(--muted); }
.hint { color: var(--muted); font-size: 13px; margin: 6px 0 0; }
.spacer { flex: 1; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }

/* ---------------------------------------------------------------- login */
#login { min-height: 100%; display: grid; place-items: center; padding: 20px; }
.login-card {
  width: min(360px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 28px;
}
.login-card .logo { font-size: 32px; font-weight: 800; text-align: center; color: var(--accent-2); letter-spacing: 2px; }
.login-card button { width: 100%; margin-top: 16px; }

/* ---------------------------------------------------------------- top bar */
#topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; gap: 10px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: rgba(13, 17, 23, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
#title { font-size: 18px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.badge {
  border-radius: 999px; padding: 4px 12px; font-size: 13px; white-space: nowrap;
  border: 1px solid var(--border); background: var(--panel);
}
.badge::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--muted); margin-right: 7px; vertical-align: 1px; }
.badge.ok::before { background: var(--ok); }
.badge.warn::before { background: var(--warn); }
.badge.bad::before { background: var(--bad); }

/* ---------------------------------------------------------------- home */
.view { padding: 18px 16px 40px; max-width: 1500px; margin: 0 auto; }
.section-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 16px; gap: 10px; }
.service-grid { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(280px, 1fr)); }
.service-card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 16px; display: flex; flex-direction: column; gap: 8px; cursor: pointer;
}
.service-card:hover { border-color: var(--accent-2); }
.service-card h3 { font-size: 17px; }
.service-card .meta { color: var(--muted); font-size: 13px; }
.service-card .actions { display: flex; gap: 8px; margin-top: 6px; }
.pill { display: inline-block; font-size: 12px; padding: 1px 8px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.pill.ok { color: var(--ok); border-color: rgba(46,160,67,.5); }
.pill.warn { color: var(--warn); border-color: rgba(210,153,34,.5); }
.pill.bad { color: var(--bad); border-color: rgba(248,81,73,.5); }
.empty { padding: 40px 20px; text-align: center; color: var(--muted); border: 1px dashed var(--border); border-radius: var(--radius); }

/* ---------------------------------------------------------------- browse */
#browse { padding-top: 12px; }
.browse-bar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.tabs { display: flex; background: var(--panel); border: 1px solid var(--border); border-radius: 9px; padding: 3px; }
.tab { border: none; background: transparent; padding: 6px 14px; border-radius: 7px; color: var(--muted); }
.tab.active { background: var(--accent); color: #fff; }
#search { flex: 1; min-width: 160px; width: auto; }
#category-select { display: none; width: auto; max-width: 100%; }
.browse-body { display: grid; grid-template-columns: 240px 1fr; gap: 16px; align-items: start; }
.categories {
  position: sticky; top: 70px; max-height: calc(100vh - 90px); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 6px;
}
.categories button {
  display: flex; width: 100%; text-align: left; border: none; background: transparent;
  padding: 7px 10px; border-radius: 6px; font-size: 14px; gap: 8px;
}
.categories button span:first-child { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.categories button .count { color: var(--muted); font-size: 12px; }
.categories button.active { background: var(--panel-2); color: var(--accent-2); }
.status { color: var(--muted); margin-bottom: 10px; min-height: 1.2em; }
.status.error { color: var(--bad); }

.items.live { display: grid; gap: 8px; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); }
.items.posters { display: grid; gap: 14px; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); }
.channel {
  display: flex; align-items: center; gap: 10px; padding: 8px 10px; text-align: left;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px; min-width: 0;
}
.channel img, .channel .ph { width: 52px; height: 36px; object-fit: contain; flex: none; border-radius: 4px; background: #0b0f14; }
.channel .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.poster { padding: 0; border: none; background: transparent; text-align: left; min-width: 0; }
.poster .art {
  aspect-ratio: 2 / 3; width: 100%; border-radius: 8px; overflow: hidden; background: var(--panel);
  border: 1px solid var(--border); display: grid; place-items: center; color: var(--muted); font-size: 12px; text-align: center; padding: 0;
}
.poster .art img { width: 100%; height: 100%; object-fit: cover; display: block; }
.poster:hover .art { border-color: var(--accent-2); }
.poster .name { margin-top: 6px; font-size: 13px; overflow: hidden; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
.poster .sub { color: var(--muted); font-size: 12px; }
#sentinel { height: 40px; }

/* ---------------------------------------------------------------- overlays */
.overlay { position: fixed; inset: 0; z-index: 50; }
#player { background: #000; display: flex; flex-direction: column; }
.player-top {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: #0b0f14; border-bottom: 1px solid #1b222c; flex-wrap: wrap;
}
.player-title { display: flex; align-items: center; gap: 10px; min-width: 0; }
.player-title img { height: 28px; max-width: 60px; object-fit: contain; }
.player-title img[src=""] { display: none; }
#player-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.video-wrap { position: relative; flex: 1; min-height: 0; display: grid; place-items: center; background: #000; overflow: hidden; }
#video { position: absolute; inset: 0; width: 100%; height: 100%; background: #000; }
.player-error {
  position: absolute; inset: auto 16px 50%; transform: translateY(50%);
  margin: 0 auto; max-width: 560px; background: rgba(22,27,34,.96); border: 1px solid var(--bad);
  border-radius: var(--radius); padding: 16px 18px; text-align: center;
}
.spinner {
  position: absolute; width: 46px; height: 46px; border-radius: 50%;
  border: 4px solid rgba(255,255,255,.15); border-top-color: var(--accent-2); animation: spin 0.9s linear infinite;
  pointer-events: none;
}
@keyframes spin { to { transform: rotate(360deg); } }
.epg { flex: none; background: #0b0f14; border-top: 1px solid #1b222c; padding: 8px 14px; padding-bottom: max(8px, env(safe-area-inset-bottom)); font-size: 14px; }
.epg:empty { display: none; }
.epg .now { font-weight: 600; }
.epg .time { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 8px; }
.epg .bar { height: 3px; background: #222; border-radius: 2px; margin: 6px 0; overflow: hidden; }
.epg .bar span { display: block; height: 100%; background: var(--accent-2); }

.modal-backdrop { background: rgba(0,0,0,.65); display: grid; place-items: center; padding: 16px; overflow-y: auto; }
.modal {
  position: relative; width: min(640px, 100%); max-height: calc(100vh - 32px); overflow-y: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 14px; padding: 22px;
}
.modal.wide { width: min(900px, 100%); }
.modal-x { position: absolute; top: 10px; right: 8px; }
.modal h2 { margin-bottom: 4px; padding-right: 30px; }
.modal .footer { display: flex; gap: 10px; margin-top: 20px; flex-wrap: wrap; }
.modal .footer .spacer { flex: 1; }
.modal ol { padding-left: 20px; margin: 8px 0; }
.modal ol li { margin: 4px 0; }
details summary { cursor: pointer; color: var(--accent-2); margin-top: 14px; }

.detail { display: grid; grid-template-columns: 160px 1fr; gap: 18px; }
.detail img.cover { width: 100%; border-radius: 8px; aspect-ratio: 2/3; object-fit: cover; background: var(--bg); }
.detail .facts { color: var(--muted); font-size: 13px; margin: 6px 0 10px; }
.detail .plot { margin: 8px 0; }
.episodes { margin-top: 16px; display: flex; flex-direction: column; gap: 6px; }
.episode {
  display: flex; gap: 12px; align-items: center; text-align: left; padding: 8px 10px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 8px;
}
.episode .num { color: var(--accent-2); font-weight: 700; min-width: 32px; }
.episode .t { flex: 1; min-width: 0; }
.episode .t div:last-child { color: var(--muted); font-size: 12px; }

.vpn-status { background: var(--bg); border: 1px solid var(--border); border-radius: var(--radius); padding: 14px; margin: 14px 0; }
.vpn-status .state { font-size: 17px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.dot { width: 10px; height: 10px; border-radius: 50%; display: inline-block; background: var(--muted); }
.dot.ok { background: var(--ok); box-shadow: 0 0 8px var(--ok); }
.dot.warn { background: var(--warn); }
.dot.bad { background: var(--bad); }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 4px 14px; margin-top: 10px; font-size: 14px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; overflow-wrap: anywhere; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 100;
  background: var(--panel-2); border: 1px solid var(--border); border-radius: 10px; padding: 10px 16px;
  box-shadow: 0 8px 30px rgba(0,0,0,.4); max-width: calc(100% - 32px);
}

@media (max-width: 760px) {
  .browse-body { grid-template-columns: 1fr; }
  .categories { display: none; }
  #category-select { display: block; flex: 1 1 0; min-width: 0; }
  #search { flex: 1 1 100%; order: 3; }
  .items.posters { grid-template-columns: repeat(auto-fill, minmax(105px, 1fr)); gap: 10px; }
  .items.live { grid-template-columns: 1fr; }
  .detail { grid-template-columns: 1fr; }
  .detail img.cover { max-width: 180px; }
  #title { font-size: 16px; }
  .player-top .ghost.small { padding: 4px 8px; }
}
