/* TV — PWA. Un solo tema scuro: si guarda la TV con la luce bassa, su OLED. */
:root {
  --ground: #0c0a14;          /* nero violaceo, dal fondo dell'icona */
  --raise: #17142a;
  --raise-2: #221e3a;
  --line: rgb(255 255 255 / .08);
  --ink: #f3f1fb;
  --ink-2: #b9b4cf;
  --ink-3: #8a84a6;
  --accent: #5ef2e6;          /* ciano dell'icona */
  --accent-ink: #062b29;
  --plate: #3a3650;           /* scheda per loghi con parti bianche */
  --plate-light: #f6f5fa;
  --danger: #ff8a8a;
  --r-tile: 12px;
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --gutter: 16px;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--ground); color: var(--ink); }
body {
  font: 400 15px/1.4 -apple-system, BlinkMacSystemFont, "SF Pro Text", system-ui, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
  overflow: hidden;
}
::selection { background: rgb(94 242 230 / .35); color: var(--ink); }
button { font: inherit; color: inherit; background: none; border: 0; padding: 0; cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
[hidden] { display: none !important; }
.ico { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- Struttura ---------- */
#app {
  position: fixed; inset: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  grid-template-rows: auto minmax(0, 1fr) auto;
  grid-template-areas: "top" "grid" "dock";
}
.top { grid-area: top; }
.grid, .empty { grid-area: grid; }
.dock { grid-area: dock; }

/* ---------- Barra in alto ---------- */
.top {
  container-type: inline-size;
  display: flex; align-items: center; gap: 12px;
  padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + var(--gutter)) 10px calc(var(--safe-l) + var(--gutter));
}
.search {
  flex: 1; min-width: 0;
  display: flex; align-items: center; gap: 8px;
  height: 44px; padding: 0 14px;
  background: var(--raise); border-radius: 14px;
  color: var(--ink-3);
  transition: background .2s;
}
.search:focus-within { background: var(--raise-2); color: var(--ink-2); box-shadow: 0 0 0 1.5px rgb(94 242 230 / .55); }
.search input {
  flex: 1; min-width: 0; height: 100%;
  background: none; border: 0; outline: 0;
  color: var(--ink); font: inherit; font-size: 17px; /* 17px: iOS non zooma il campo */
  caret-color: var(--accent);
}
.search input::placeholder { color: var(--ink-3); }
.search input::-webkit-search-cancel-button { filter: grayscale(1) brightness(1.6); }
.clock {
  flex: none; color: var(--ink-2); font-size: 15px; font-weight: 500;
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.clock b { color: var(--ink); font-weight: 600; }
@container (max-width: 420px) { .clock .day { display: none; } }

/* ---------- Griglia canali ---------- */
.grid {
  overflow-y: auto; overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  align-content: start;
  gap: 18px 12px;
  padding: 8px calc(var(--safe-r) + var(--gutter)) 24px calc(var(--safe-l) + var(--gutter));
  scrollbar-width: thin; scrollbar-color: var(--raise-2) transparent;
}
.tile {
  display: flex; flex-direction: column; gap: 7px;
  text-align: center; min-width: 0;
  -webkit-user-select: none; user-select: none;
}
.tile .plate {
  position: relative; aspect-ratio: 16 / 9; border-radius: var(--r-tile);
  background: var(--plate); overflow: hidden;
  display: grid; place-items: center;
  transition: transform .18s var(--ease-out);
}
.tile .plate::after { /* anello di selezione esterno, come nell'app Mac */
  content: ""; position: absolute; inset: 0; border-radius: inherit;
  box-shadow: inset 0 0 0 1px rgb(255 255 255 / .06);
  pointer-events: none;
}
.tile.light .plate { background: var(--plate-light); }
.tile.full .plate img { width: 100%; height: 100%; object-fit: cover; padding: 0; }
.tile img { width: 100%; height: 100%; object-fit: contain; padding: 10% 12%; display: block; }
.tile .fallback { font-weight: 650; font-size: 15px; color: var(--ink); padding: 0 6px; line-height: 1.15; }
.tile .name {
  font-size: 12.5px; font-weight: 500; color: var(--ink-2);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tile:active .plate { transform: scale(.96); }
.tile:focus-visible { outline: 0; }
.tile:focus-visible .plate, .tile.kb .plate { box-shadow: 0 0 0 3px var(--ground), 0 0 0 5px var(--ink-2); }
.tile[aria-selected="true"] .plate { box-shadow: 0 0 0 3px var(--ground), 0 0 0 5.5px var(--accent); }
.tile .plate { overflow: visible; }
.tile .plate img { border-radius: inherit; }
.tile[aria-selected="true"] .name { color: var(--accent); }
.empty { align-self: start; text-align: center; color: var(--ink-3); padding-top: 48px; margin: 0; }

/* ---------- Dock ---------- */
.dock {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  padding: 10px calc(var(--safe-r) + var(--gutter)) calc(var(--safe-b) + 10px) calc(var(--safe-l) + var(--gutter));
  background: linear-gradient(to top, var(--ground) 60%, rgb(12 10 20 / 0));
}
.seg { display: inline-flex; background: var(--raise); border-radius: 12px; padding: 3px; }
.seg button {
  min-width: 84px; height: 38px; border-radius: 9px;
  font-weight: 600; font-size: 15px; color: var(--ink-2);
  transition: background .2s, color .2s;
}
.seg button[aria-selected="true"] { background: var(--raise-2); color: var(--ink); box-shadow: 0 1px 3px rgb(0 0 0 / .4); }
.chip {
  display: inline-flex; align-items: center; gap: 6px; height: 44px; padding: 0 14px;
  border-radius: 12px; color: var(--ink-3); font-size: 14px; font-weight: 550;
}
.chip .ico { width: 18px; height: 18px; }
.chip[aria-pressed="true"] { color: var(--accent); }

/* ---------- Lettore ---------- */
.stage { grid-area: stage; display: none; min-width: 0; min-height: 0; background: #000; }
.screen {
  position: relative; overflow: hidden; background: #000;
  touch-action: none; -webkit-user-select: none; user-select: none;
}
.screen video {
  position: absolute; inset: 0; width: 100%; height: 100%;
  object-fit: contain; background: transparent;
  opacity: 0; pointer-events: none;
}
.screen video.front { opacity: 1; }
.poster { position: absolute; inset: 0; display: grid; place-items: center; }
.poster img { max-width: 38%; max-height: 38%; object-fit: contain; border-radius: 16px; }
.poster.light img { background: var(--plate-light); padding: 4%; }

.loader { position: absolute; inset: 0; display: grid; place-items: center; pointer-events: none; opacity: 0; transition: opacity .2s; }
.loader span {
  width: 34px; height: 34px; border-radius: 50%;
  border: 3px solid rgb(255 255 255 / .18); border-top-color: var(--ink);
  animation: spin .8s linear infinite;
}
.screen.loading .loader { opacity: 1; transition-delay: .15s; } /* niente lampeggio se parte subito */
@keyframes spin { to { transform: rotate(360deg); } }

/* Chrome: pulsanti sopra al video */
.chrome {
  position: absolute; inset: 0; pointer-events: none;
  display: flex; justify-content: space-between; align-items: flex-start;
  padding: calc(var(--safe-t) + 10px) calc(var(--safe-r) + 12px) 0 calc(var(--safe-l) + 12px);
  background: linear-gradient(to bottom, rgb(0 0 0 / .55), rgb(0 0 0 / 0) 120px);
  opacity: 0; transition: opacity .25s;
}
.screen.chrome-on .chrome { opacity: 1; }
.screen.chrome-on .chrome .round { pointer-events: auto; }
.tools { display: flex; gap: 8px; }
.round {
  width: 44px; height: 44px; border-radius: 50%;
  display: grid; place-items: center;
  background: rgb(20 18 32 / .62);
  -webkit-backdrop-filter: blur(14px) saturate(1.4); backdrop-filter: blur(14px) saturate(1.4);
  color: var(--ink);
}
.round[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); }
.round:active { transform: scale(.92); }

/* HUD di zapping: logo + nome + numero, il momento firmato dell'app */
.hud {
  position: absolute; left: 50%; top: calc(var(--safe-t) + 16px);
  display: flex; align-items: center; gap: 12px;
  padding: 8px 16px 8px 8px; border-radius: 16px;
  background: rgb(20 18 32 / .72);
  -webkit-backdrop-filter: blur(18px) saturate(1.4); backdrop-filter: blur(18px) saturate(1.4);
  box-shadow: 0 8px 28px rgb(0 0 0 / .35);
  transform: translate(-50%, 0); max-width: calc(100% - 140px);
  animation: hud-in .35s var(--ease-out);
}
.hud .logo { width: 64px; aspect-ratio: 16/9; border-radius: 8px; background: var(--plate); display: grid; place-items: center; overflow: hidden; flex: none; }
.hud .logo.light { background: var(--plate-light); }
.hud .logo img { width: 100%; height: 100%; object-fit: contain; padding: 8%; }
.hud .logo.full img { object-fit: cover; padding: 0; }
.hud .txt { min-width: 0; }
.hud .n { font-size: 16px; font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud .p { font-size: 13px; color: var(--ink-2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hud .num { font-variant-numeric: tabular-nums; color: var(--ink-3); font-weight: 500; margin-right: 6px; }
@keyframes hud-in { from { opacity: 0; transform: translate(-50%, -8px) scale(.97); filter: blur(4px); } }
.hud.out { opacity: 0; transition: opacity .4s; }

/* Feedback durante lo swipe */
.zap { position: absolute; inset: 0; pointer-events: none; }
.zap::before, .zap::after {
  content: ""; position: absolute; top: 0; bottom: 0; width: 34%;
  opacity: 0; transition: opacity .2s;
}
.zap::before { left: 0; background: linear-gradient(to right, rgb(94 242 230 / .22), transparent); }
.zap::after { right: 0; background: linear-gradient(to left, rgb(94 242 230 / .22), transparent); }
.zap[data-dir="prev"]::before, .zap[data-dir="next"]::after { opacity: var(--pull, 0); }

.alert {
  position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 14px;
  background: rgb(0 0 0 / .7); text-align: center; padding: 24px;
}
.alert p { margin: 0; font-size: 16px; color: var(--ink); max-width: 32ch; }
.pill {
  height: 40px; padding: 0 18px; border-radius: 20px;
  background: var(--ink); color: var(--ground); font-weight: 600; font-size: 15px;
  display: inline-flex; align-items: center; gap: 8px;
}
.unmute { position: absolute; left: 50%; bottom: calc(var(--safe-b) + 28px); transform: translateX(-50%); box-shadow: 0 8px 24px rgb(0 0 0 / .4); }
.unmute .ico { width: 18px; height: 18px; }

/* ---------- Pannello programma ---------- */
.info { color: var(--ink); }
.info .title { font-size: 22px; font-weight: 650; line-height: 1.2; margin: 0; text-wrap: balance; }
.info .meta { display: flex; align-items: center; gap: 10px; margin-top: 6px; font-size: 14px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.info .bar { flex: 1; max-width: 220px; height: 3px; border-radius: 2px; background: rgb(255 255 255 / .2); overflow: hidden; }
.info .bar i { display: block; height: 100%; background: var(--accent); transform-origin: left; }
.info .syn { margin: 10px 0 0; font-size: 15px; color: rgb(243 241 251 / .86); display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; max-width: 70ch; }
.info .next { margin-top: 10px; font-size: 13.5px; color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-variant-numeric: tabular-nums; }
.info .next b { color: var(--ink-2); font-weight: 550; }
.info .details { font-size: 13.5px; color: var(--accent); font-weight: 550; margin-bottom: 6px; }
.info .chan { font-size: 13.5px; color: var(--ink-3); font-weight: 550; margin-bottom: 4px; }
.info .muted { color: var(--ink-3); font-size: 15px; margin: 0; }
.info[data-state="expanded"] .syn { -webkit-line-clamp: unset; display: block; }
.info[data-state="expanded"] .title { font-size: clamp(24px, 4.2vw, 40px); }

/* =====================================================================
   Layout compatto (iPhone, display esterno del pieghevole, iPhone in
   orizzontale): lista a tutto schermo, lettore sopra a tutto.
   ===================================================================== */
@media (max-width: 699.98px), (max-height: 499.98px) {
  .stage {
    display: block; position: fixed; inset: 0; z-index: 10;
    visibility: hidden; opacity: 0; transform: scale(.985);
    transition: opacity .28s var(--ease-out), transform .38s var(--ease-out), visibility 0s .38s;
  }
  #app[data-view="player"] .stage { visibility: visible; opacity: 1; transform: none; transition-delay: 0s; }
  .screen { position: absolute; inset: 0; }

  .info {
    position: absolute; left: 0; right: 0; bottom: 0; z-index: 3;
    padding: 56px calc(var(--safe-r) + 20px) calc(var(--safe-b) + 22px) calc(var(--safe-l) + 20px);
    background: linear-gradient(to top, rgb(0 0 0 / .86), rgb(0 0 0 / .6) 55%, rgb(0 0 0 / 0));
    text-align: center;
    opacity: 0; transform: translateY(16px); pointer-events: none;
    transition: opacity .3s var(--ease-out), transform .45s var(--ease-out), background .45s;
  }
  .info .meta { justify-content: center; }
  .info .syn { margin-inline: auto; }
  .info[data-state="open"], .info[data-state="expanded"] { opacity: 1; transform: none; pointer-events: auto; }
  .info[data-state="expanded"] {
    top: 0; display: flex; flex-direction: column; justify-content: center; align-items: center;
    background: rgb(4 3 8 / .88); padding-top: calc(var(--safe-t) + 60px);
    overflow-y: auto;
  }
  .info[data-state="expanded"] .syn { max-width: 56ch; font-size: 16px; line-height: 1.5; }
}
/* iPhone in verticale: il video sta in alto, sotto resta sempre il programma in onda */
@media (max-width: 699.98px) and (max-aspect-ratio: 1/1) {
  .screen video { object-position: 50% 34%; }
  .poster { place-items: start center; padding-top: 22vh; }
  .info[data-state="closed"] { opacity: 1; transform: none; pointer-events: auto; }
  .info:not([data-state="expanded"]) {
    top: calc(0.34 * (100% - 56.25vw) + 56.25vw); bottom: auto;
    background: none; padding: 22px calc(var(--safe-r) + 24px) 0 calc(var(--safe-l) + 24px);
  }
  .info:not([data-state="expanded"]) .syn { -webkit-line-clamp: 4; }
  .unmute { top: calc(0.34 * (100% - 56.25vw) + 28.125vw); bottom: auto; transform: translate(-50%, -50%); }
  .theaterBtn, #theaterBtn { display: none; }
  .hud { top: calc(var(--safe-t) + 66px); max-width: calc(100% - 32px); }
}
@media (max-width: 699.98px), (max-height: 499.98px) { #theaterBtn { display: none; } }
/* iPhone in orizzontale: pannello più basso */
@media (max-height: 500px) {
  .info { padding-top: 32px !important; }
  .info .syn { -webkit-line-clamp: 1; }
}

/* =====================================================================
   Layout ampio: display interno del pieghevole, iPad.
   Il lettore è sempre visibile: cambiare canale non copre mai la lista.
   ===================================================================== */
@media (min-width: 700px) and (min-height: 500px) {
  :root { --gutter: 20px; }
  .stage { display: flex; flex-direction: column; overflow: hidden; }
  .screen { aspect-ratio: 16 / 9; width: 100%; max-height: 100%; flex: none; }
  .screen .back { display: none; }
  .chrome { padding-top: 12px; }
  .hud { top: 14px; }
  .info {
    flex: 1; min-height: 0; overflow-y: auto;
    padding: 18px 24px calc(var(--safe-b) + 18px);
    background: var(--ground);
  }
  .info[data-state="closed"] { /* nel layout ampio il pannello resta aperto */ }
  .info .syn { -webkit-line-clamp: 3; }
  .info .bar { max-width: 280px; }
  .stage .unmute { bottom: 20px; }

  /* Senza canale scelto il lettore mostra un invito */
  #app:not([data-has-channel]) .screen::after {
    content: "Scegli un canale"; position: absolute; inset: 0; display: grid; place-items: center;
    color: var(--ink-3); font-size: 17px; font-weight: 500;
  }

  /* Modalità cinema: il video prende tutto lo schermo */
  #app[data-theater] .stage { position: fixed; inset: 0; z-index: 10; }
  #app[data-theater] .screen { aspect-ratio: auto; height: 100%; }
  #app[data-theater] .info { position: absolute; inset: auto 0 0 0; background: linear-gradient(to top, rgb(0 0 0 / .86), rgb(0 0 0 / 0)); opacity: 0; pointer-events: none; transition: opacity .3s; padding-top: 60px; text-align: center; }
  #app[data-theater] .info .meta { justify-content: center; }
  #app[data-theater] .info .syn { margin-inline: auto; }
  #app[data-theater] .info[data-state="open"], #app[data-theater] .info[data-state="expanded"] { opacity: 1; pointer-events: auto; }
  #app[data-theater] .chrome { padding-top: calc(var(--safe-t) + 12px); }
}

/* Orizzontale ampio (pieghevole aperto ~4:3, iPad orizzontale): lista a sinistra, lettore a destra */
@media (min-width: 700px) and (min-height: 500px) and (min-aspect-ratio: 1/1) {
  #app {
    grid-template-columns: minmax(300px, 36%) minmax(0, 1fr);
    grid-template-rows: auto 1fr auto;
    grid-template-areas: "top stage" "grid stage" "dock stage";
  }
  .top, .grid, .dock { padding-right: var(--gutter); }
  .stage { padding-right: var(--safe-r); background: var(--ground); border-left: 1px solid var(--line); }
  .screen { padding-top: 0; }
  .stage .screen { margin-top: var(--safe-t); }
  .grid { grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); }
  .chip span { display: none; }
}
@media (min-width: 1100px) and (min-height: 500px) and (min-aspect-ratio: 1/1) {
  #app { grid-template-columns: minmax(340px, 32%) minmax(0, 1fr); }
  .chip span { display: inline; }
}

/* Verticale ampio (pieghevole aperto in verticale, iPad verticale): lettore sopra, lista sotto */
@media (min-width: 600px) and (min-height: 700px) and (max-aspect-ratio: 1/1) {
  .stage {
    display: flex; flex-direction: column; overflow: hidden;
    position: static; visibility: visible; opacity: 1; transform: none;
  }
  #app {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto;
    grid-template-areas: "stage" "top" "grid" "dock";
  }
  .stage { padding-top: var(--safe-t); max-height: 58vh; }
  .screen { position: relative; inset: auto; aspect-ratio: 16 / 9; width: 100%; flex: none; }
  .screen .back { display: none; }
  .info {
    position: static; opacity: 1; transform: none; pointer-events: auto; text-align: left;
    background: var(--ground); padding: 14px var(--gutter) 4px; flex: 1; min-height: 0; overflow-y: auto;
  }
  .info .meta { justify-content: flex-start; }
  .info .syn { margin-inline: 0; -webkit-line-clamp: 2; }
  .top { padding-top: 10px; }
  #app:not([data-has-channel]) .screen::after {
    content: "Scegli un canale"; position: absolute; inset: 0; display: grid; place-items: center;
    color: var(--ink-3); font-size: 17px; font-weight: 500;
  }
  #app[data-theater] .stage { position: fixed; inset: 0; z-index: 10; max-height: none; background: #000; }
  #app[data-theater] .screen { aspect-ratio: auto; flex: 1; }
  #app[data-theater] .info { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}
