/*
 * Shared brand palette, sampled straight from the "IPTV Player by AboHsayn"
 * logo (the cyan play-mark that fades cyan -> electric-blue -> magenta on a
 * near-black navy field). The Flutter app's AppTheme.dark() uses the same
 * values (see ../../../lib/core/theme/app_theme.dart) so every platform —
 * web, Android, Tizen, webOS — reads as the same product.
 *
 * Source colors: bg #060D1B · cyan #00C2FF · blue #0096FF/#006BF9 ·
 * indigo #1D08BE · magenta #C80BF5 · silver #73A4C2.
 */
:root {
  --bg: #070E1C;
  --surface: #0F1728;
  --surface-2: #0B1220;
  --tile: #131C2E;
  --tile-2: #1D2740;
  --primary: #35C6FF;         /* cyan — focus rings, accents, active links */
  --primary-strong: #2C7BFF;  /* electric blue — primary buttons */
  --accent: #C64BF0;          /* magenta — secondary accent */
  --on-primary: #04244F;      /* dark ink on cyan/blue fills */
  --text: #E8EDF7;
  --text-muted: #94A2BC;
  --border: #212C46;
  --heart: #FF6B6B;
  --focus: #35C6FF;

  /* The logo's play-mark gradient, reused for brand accents (splash glow,
     the app-bar hairline, primary button sheen). */
  --brand-gradient: linear-gradient(120deg, #00C2FF 0%, #1E7BFF 52%, #C64BF0 100%);

  /* 10-foot UI: keep interactive content inside the TV-safe area to avoid
     overscan cropping on older displays. */
  --safe-margin: 5vw;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  overflow: hidden; /* the app manages its own scroll regions */
}
#app { width: 100%; height: 100%; display: flex; flex-direction: column; }

button { font-family: inherit; }

/* Focus highlight — the TV-web equivalent of FocusableCard. Every
   [data-focusable] element gets this automatically via :focus-visible
   (and plain :focus as a fallback, since some TV WebKit builds don't
   apply :focus-visible to programmatic .focus() calls). */
[data-focusable] {
  outline: none;
  border: 3px solid transparent;
  transition: transform 0.12s ease, box-shadow 0.12s ease, border-color 0.12s ease;
  cursor: pointer;
}
[data-focusable]:focus,
[data-focusable]:focus-visible {
  transform: scale(1.045);
  border-color: var(--focus);
  box-shadow: 0 0 0 1px var(--focus), 0 8px 22px -4px rgba(138, 180, 255, 0.45);
  z-index: 2;
  position: relative;
}

.appbar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 28px var(--safe-margin) 20px;
  background: var(--bg);
}
.appbar h1 { font-size: 2rem; margin: 0; font-weight: 600; }
.appbar .back {
  background: var(--tile);
  border: none;
  color: var(--text);
  border-radius: 50%;
  width: 52px;
  height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
}

.shell { flex: 1; display: flex; min-height: 0; }

.rail {
  width: 220px;
  flex: none;
  background: var(--surface-2);
  padding: 32px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
.rail-item {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 16px 28px;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1.15rem;
  text-align: left;
  border-radius: 0 30px 30px 0;
  margin-right: 24px;
}
.rail-item.active { color: var(--on-primary); background: var(--primary); font-weight: 600; }
.rail-item[data-focusable]:focus,
.rail-item[data-focusable]:focus-visible {
  transform: none;
  border-color: transparent;
  box-shadow: inset 0 0 0 3px var(--focus);
}

.content { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 0 var(--safe-margin) var(--safe-margin); overflow-y: auto; overflow-x: hidden; }

/* Slim themed scrollbars everywhere, instead of the bulky OS default. */
.content, .grid, .cat-rail, .form-wrap {
  scrollbar-width: thin;
  scrollbar-color: var(--tile-2) transparent;
}
.content::-webkit-scrollbar, .grid::-webkit-scrollbar, .form-wrap::-webkit-scrollbar { width: 8px; }
.cat-rail::-webkit-scrollbar { height: 8px; }
.content::-webkit-scrollbar-track, .grid::-webkit-scrollbar-track,
.cat-rail::-webkit-scrollbar-track, .form-wrap::-webkit-scrollbar-track { background: transparent; }
.content::-webkit-scrollbar-thumb, .grid::-webkit-scrollbar-thumb,
.cat-rail::-webkit-scrollbar-thumb, .form-wrap::-webkit-scrollbar-thumb {
  background: var(--tile-2); border-radius: 8px;
}
.content::-webkit-scrollbar-thumb:hover, .grid::-webkit-scrollbar-thumb:hover,
.cat-rail::-webkit-scrollbar-thumb:hover, .form-wrap::-webkit-scrollbar-thumb:hover {
  background: var(--primary);
}

.cat-rail { display: flex; gap: 14px; padding: 20px 4px; overflow-x: auto; overflow-y: hidden; flex: none; }
.chip {
  flex: none;
  padding: 10px 22px;
  border-radius: 24px;
  border: 2px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  font-size: 1.05rem;
}
.chip.active { background: var(--primary); color: var(--on-primary); border-color: var(--primary); font-weight: 600; }

/* Category selector, styled as small cards rather than plain pills. */
.cat-card {
  flex: none;
  min-width: 96px;
  padding: 14px 20px;
  border-radius: 14px;
  border: 2px solid var(--border);
  background: var(--tile);
  color: var(--text-muted);
  font-size: 1.02rem;
  font-weight: 600;
  text-align: center;
}
.cat-card.active { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
/* "Back to categories" chip that leads the rail in the channel view. */
.cat-card.cat-back { background: var(--tile-2); color: var(--text); border-color: var(--tile-2); position: sticky; left: 0; }

/* Search box for filtering the current section's categories/items live. */
.section-search {
  flex: none;
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 20px 4px 0;
  padding: 12px 16px;
  max-width: 480px;
  background: var(--tile);
  border: 2px solid var(--border);
  border-radius: 10px;
}
.section-search:focus-within { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(138, 180, 255, 0.25); }

/* Live TV toolbar: search + Arabic/Favorites toggles on one row. */
.livetv-toolbar { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
.livetv-toolbar .section-search { flex: 1 1 260px; }
.toggle-btn {
  flex: none; margin: 20px 4px 0; padding: 12px 18px; border-radius: 10px;
  border: 2px solid var(--border); background: var(--tile); color: var(--text-muted);
  font-size: 1rem; font-weight: 600;
}
.toggle-btn.active { background: var(--primary); color: var(--on-primary); border-color: var(--primary); }
.section-search-icon { color: var(--text-muted); font-size: 1.05rem; flex: none; }
.section-search-input {
  flex: 1; min-width: 0; background: none; border: none; outline: none !important;
  color: var(--text); font-size: 1.02rem;
}
/* The input also matches the generic [data-focusable] rule (for TV remote
   nav on buttons/cards), whose focus box-shadow + scale was showing up as
   a mismatched rectangular ring layered inside the rounded search box
   above — the container's own focus-within glow is the only one it needs. */
.section-search-input[data-focusable]:focus,
.section-search-input[data-focusable]:focus-visible {
  transform: none;
  box-shadow: none;
  border-color: transparent;
}
.section-search-input::placeholder { color: var(--text-faint, var(--text-muted)); }

.grid {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  display: grid;
  gap: 24px;
  padding: 8px 4px 40px;
  grid-template-columns: repeat(auto-fill, minmax(220px, 240px));
  /* With the full unfiltered channel list (2000+ tiles), Chromium's
     content-based `auto` row sizing collapses every implicit row to a few
     px regardless of what the tiles inside them actually measure at —
     reproduced live (computed grid-template-rows read back as "6px 6px
     6px…" for all rows). A fixed floor sidesteps that measurement instead
     of depending on it. */
  /* 230px, not 190px: room for the 2-line caption below (see .tile
     .caption) — this floor is load-bearing, not just a fallback (see the
     comment above), so it needs to already fit the tallest normal case
     rather than rely on auto growing into it. */
  grid-auto-rows: minmax(230px, auto);
  justify-content: start;
  align-content: start;
}
.grid.poster { grid-template-columns: repeat(auto-fill, minmax(180px, 200px)); grid-auto-rows: minmax(270px, auto); }

.tile {
  background: var(--tile);
  border-radius: 12px;
  padding: 0;
  display: flex;
  flex-direction: column;
  color: var(--text);
  overflow: hidden;
  position: relative;
}
/* padding-top percentage box instead of aspect-ratio: with hundreds/
   thousands of grid tiles on screen (the full unfiltered channel list),
   aspect-ratio on a flex child collapses to 0 height in Chromium's grid+flex
   sizing at that scale — reproduced live, confirmed fine at a handful of
   items but 0px at 800+. The padding-top box model has no such ceiling. */
.tile .art { width: 100%; padding-top: 68.75%; background: var(--tile-2); position: relative; overflow: hidden; }
.tile.poster .art { padding-top: 150%; }
/* Up to 2 lines instead of single-line ellipsis truncation — most channel
   names in real playlists are long enough that one line was cutting off
   most of the name. display:-webkit-box + -webkit-line-clamp (the usual
   line-clamp technique) doesn't actually engage in this app's rendering
   engine — computed display reads back as flow-root, not -webkit-box, so
   the clamp silently never applied and text stayed stuck at 1 line. Plain
   wrapping + a fixed max-height has no such dependency. */
.tile .caption {
  padding: 12px 14px; font-size: 1.02rem; line-height: 1.3;
  max-height: 66px; overflow: hidden;
}
.tile .monogram {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  width: 64px; height: 64px; border-radius: 12px;
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 1.4rem; color: #fff;
}
/* Logo/poster image sits over the monogram fallback and fades in once
   loaded; if it 404s/is blocked, onerror removes it and the monogram
   underneath just stays visible — no layout jump either way. */
.tile .art-img {
  position: absolute; inset: 0; width: 100%; height: 100%;
  opacity: 0; transition: opacity 0.15s ease;
}
.tile .art-img.loaded { opacity: 1; }
.tile:not(.poster) .art-img { object-fit: contain; }
.tile.poster .art-img { object-fit: cover; }
.tile .poster-scrim {
  position: absolute; inset: 0;
  background: linear-gradient(180deg, transparent 45%, rgba(0,0,0,0.8) 100%);
  display: flex; align-items: flex-end; padding: 14px;
}
.tile .poster-title { font-size: 0.98rem; font-weight: 600; line-height: 1.3; }
.tile .heart {
  position: absolute; top: 10px; right: 10px;
  background: rgba(0,0,0,0.45); border-radius: 50%;
  width: 40px; height: 40px; display: flex; align-items: center; justify-content: center;
}

.empty-state, .loading-state, .error-state {
  flex: 1; display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--text-muted); text-align: center; gap: 16px; font-size: 1.1rem;
}
.error-state button { margin-top: 8px; }

.btn-primary {
  background: var(--brand-gradient); color: #fff; border: none; border-radius: 8px;
  padding: 16px 24px; font-size: 1.1rem; font-weight: 600;
  box-shadow: 0 8px 22px -8px rgba(44, 123, 255, 0.6);
}

/* ---- Branding: app-bar logo + animated splash ---- */
.appbar .brand-logo { height: 42px; width: auto; flex: none; display: block; }
.appbar { gap: 16px; }
/* Brand lockup at the top of the onboarding form. */
.form-brand {
  display: block; height: 78px; width: auto; max-width: 100%;
  margin: 0 auto 28px; object-fit: contain;
  filter: drop-shadow(0 0 20px rgba(53, 198, 255, 0.28));
}

/* Full-screen preloader shown on launch, faded out once the app mounts.
   Lives in index.html so it paints before app.js parses/runs. */
#splash {
  position: fixed; inset: 0; z-index: 100;
  background:
    radial-gradient(120% 90% at 50% 40%, #12203D 0%, transparent 60%),
    var(--bg);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 30px;
  transition: opacity 0.45s ease;
}
#splash.hide { opacity: 0; pointer-events: none; }
#splash .splash-logo {
  width: min(62vw, 340px); height: auto;
  filter: drop-shadow(0 0 34px rgba(53, 198, 255, 0.45));
  animation: splash-pop 0.9s cubic-bezier(0.16, 1, 0.3, 1) both,
             splash-glow 2.4s ease-in-out 0.9s infinite;
}
#splash .splash-bar {
  width: min(46vw, 220px); height: 4px; border-radius: 4px;
  background: rgba(255, 255, 255, 0.1); overflow: hidden; position: relative;
}
#splash .splash-bar::before {
  content: ''; position: absolute; top: 0; bottom: 0; left: -40%; width: 40%;
  border-radius: 4px; background: var(--brand-gradient);
  animation: splash-slide 1.15s ease-in-out infinite;
}
@keyframes splash-pop {
  0%   { opacity: 0; transform: scale(0.82); }
  100% { opacity: 1; transform: scale(1); }
}
@keyframes splash-glow {
  0%, 100% { filter: drop-shadow(0 0 24px rgba(53, 198, 255, 0.35)); }
  50%      { filter: drop-shadow(0 0 44px rgba(198, 75, 240, 0.55)); }
}
@keyframes splash-slide {
  0%   { left: -40%; }
  100% { left: 100%; }
}
@media (prefers-reduced-motion: reduce) {
  #splash .splash-logo { animation: splash-pop 0.4s ease both; }
  #splash .splash-bar::before { animation-duration: 2s; }
}

/* ---- Onboarding / forms ---- */
.form-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  overflow-y: auto;
  overflow-x: hidden;
  /* Horizontal padding so the form never touches the screen edges on a phone. */
  padding: 24px 20px;
}
/* margin:auto centres the form but, unlike align/justify-center, never clips
   the top (the segmented tabs) when the form is taller than the viewport. */
.form { margin: auto; width: 100%; max-width: 920px; flex: none; }

/* Login fields on the left, QR sign-in beside them on wide screens; they stack
   on a phone. */
.login-row { display: flex; gap: 36px; align-items: flex-start; }
.login-col { flex: 1; min-width: 0; }
.qr-col { flex: none; width: 264px; }
.qr-signin {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 20px 18px;
  text-align: center;
}
#qr-box {
  display: flex; align-items: center; justify-content: center;
  background: #fff; border-radius: 16px; padding: 14px; margin: 16px auto 0;
  width: 210px; height: 210px; box-sizing: border-box;
}
/* Below the form's full width, stack the fields and QR and let each stretch to
   the full width (align-items:stretch), so the fields aren't squished into a
   narrow left-aligned block on tablets/phones. */
@media (max-width: 940px) {
  .login-row { flex-direction: column; align-items: stretch; }
  .login-col, .qr-col { width: 100%; }
}
.segmented { display: flex; border: 2px solid var(--border); border-radius: 10px; overflow: hidden; margin-bottom: 28px; }
.segmented button { flex: 1; background: transparent; border: none; color: var(--text-muted); padding: 16px; font-size: 1.05rem; }
.segmented button.active { background: var(--primary-strong); color: #fff; }
.field-label { display: block; font-size: 0.95rem; color: var(--text-muted); margin: 20px 0 8px; }
.field {
  width: 100%; background: var(--surface); border: 2px solid var(--border); border-radius: 8px;
  color: var(--text); padding: 16px; font-size: 1.1rem;
}

/* ---- Player ---- */
.player {
  position: fixed; inset: 0; background: #000; z-index: 50;
  display: flex; flex-direction: column;
}
.player video { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: contain; background: #000; }
.player-overlay {
  position: relative; z-index: 2; flex: 1; display: flex; flex-direction: column;
  transition: opacity 0.2s ease;
}
.player-overlay.hidden { opacity: 0; pointer-events: none; }
.player-top {
  display: flex; align-items: flex-start; gap: 16px; padding: 24px var(--safe-margin);
  background: linear-gradient(180deg, rgba(0,0,0,0.7), transparent);
}
.player-titles .ch-name { font-size: 1.5rem; font-weight: 600; }
.player-titles .epg { font-size: 1rem; color: rgba(255,255,255,0.7); margin-top: 4px; }
.player-top .fullscreen-btn {
  flex: none;
  background: rgba(255,255,255,0.12); border: none; color: #fff; border-radius: 50%;
  width: 52px; height: 52px; display: flex; align-items: center; justify-content: center;
  font-size: 1.3rem;
}
.player-top .fullscreen-btn:hover { background: rgba(255,255,255,0.22); }
.player-bottom {
  margin-top: auto; padding: 24px var(--safe-margin) 40px;
  background: linear-gradient(0deg, rgba(0,0,0,0.8), transparent);
}
.live-controls { display: flex; align-items: center; justify-content: center; gap: 48px; }
.live-controls button { background: none; border: none; color: #fff; font-size: 2.4rem; }
.live-controls button.play { font-size: 3.4rem; }
.vod-controls .scrub-track { height: 6px; background: rgba(255,255,255,0.25); border-radius: 3px; position: relative; margin-bottom: 10px; }
.vod-controls .scrub-fill { position: absolute; left: 0; top: 0; bottom: 0; background: var(--primary); border-radius: 3px; }
.time-row { display: flex; align-items: center; justify-content: space-between; color: rgba(255,255,255,0.8); font-size: 1rem; }
.buffering-spinner {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center; z-index: 1;
}
.spinner {
  width: 56px; height: 56px; border-radius: 50%;
  border: 5px solid rgba(255,255,255,0.2); border-top-color: #fff;
  animation: spin 0.9s linear infinite;
}
@media (prefers-reduced-motion: reduce) { .spinner { animation: none; } }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------------------------------------------------------------------
 * Phone / small-tablet layout.
 *
 * The base stylesheet is tuned for a 10-foot TV view (fixed 220px side
 * rail, 220–240px grid tiles, 2rem headings). On a phone the old
 * `width=1920` viewport made all of that render at desktop size and then
 * shrink to fit — the "zoomed out" look. With `width=device-width` the
 * page now measures in real phone pixels, and these overrides reflow it:
 * the side rail becomes a horizontal scroller on top, tiles pack ~2 per
 * row, and headings/controls scale down to a hand-held size.
 * ------------------------------------------------------------------- */
@media (max-width: 768px) {
  :root { --safe-margin: 16px; }

  .appbar { padding: 16px var(--safe-margin) 10px; gap: 12px; }
  .appbar h1 { font-size: 1.4rem; }
  .appbar .back { width: 44px; height: 44px; font-size: 1.2rem; }

  /* Side rail → horizontal, scrollable strip above the content. */
  .shell { flex-direction: column; }
  .rail {
    width: 100%;
    flex: none;
    flex-direction: row;
    align-items: center;
    gap: 8px;
    padding: 8px var(--safe-margin);
    overflow-x: auto;
    overflow-y: hidden;
    scrollbar-width: none;
  }
  .rail::-webkit-scrollbar { display: none; }
  .rail-item {
    flex: none;
    padding: 10px 18px;
    margin-right: 0;
    border-radius: 22px;
    font-size: 1rem;
  }

  .content { padding: 0 var(--safe-margin) var(--safe-margin); }

  /* Pack tiles ~2 per row instead of one giant TV-sized tile. Keep a
     fixed row-height floor (see the .grid comment above — it's what
     stops Chromium collapsing rows to a few px at large item counts). */
  .grid {
    gap: 12px;
    grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
    grid-auto-rows: minmax(180px, auto);
  }
  .grid.poster {
    grid-template-columns: repeat(auto-fill, minmax(115px, 1fr));
    grid-auto-rows: minmax(240px, auto);
  }
  .tile .caption { padding: 10px 12px; font-size: 0.95rem; max-height: 60px; }

  .cat-card { min-width: 84px; padding: 12px 16px; font-size: 0.98rem; }

  /* Player controls sized for a thumb, not a remote across the room. */
  .player-titles .ch-name { font-size: 1.2rem; }
  .player-top { padding: 16px var(--safe-margin); }
  .player-top .fullscreen-btn { width: 44px; height: 44px; font-size: 1.1rem; }
  .player-bottom { padding: 16px var(--safe-margin) 28px; }
  .live-controls { gap: 28px; }
  .live-controls button { font-size: 2rem; }
  .live-controls button.play { font-size: 2.8rem; }

  /* Turn off the focus zoom on touch devices — it's for D-pad TVs and
     just causes tap jitter on a phone. */
  [data-focusable]:focus,
  [data-focusable]:focus-visible { transform: none; }
}
