/* ============================================================
   TV-optimized slideshow
   - Vanilla CSS, no preprocessors
   - Pixel-perfect on 1920x1080 TVs — no downscale, no white flash
   - Fallbacks for 2017-2019 Tizen / WebOS / AOSP TV browsers
   - Big focus rings for D-pad / TV remote navigation
   ============================================================ */

:root {
  --bg: #050505;
  --fg: #f5f5f5;
  --muted: #9aa0a6;
  --accent: #ffffff;
  --accent-2: #ffcc00;
  --panel: rgba(15, 15, 15, .82);
  --panel-strong: rgba(10, 10, 10, .94);
  --border: rgba(255, 255, 255, .14);
  --focus: #ffcc00;
  --shadow: 0 24px 60px rgba(0, 0, 0, .55);

  /* Overscan-safe gutter. On 1920+ screens we drop this to 0
     (see @media below) so slides render at native resolution. */
  --safe-x: 2.5vw;
  --safe-y: 2.5vh;

  /* Simple fallback first, then clamp() for modern TVs */
  --hud-h: 56px;
  --hud-h: clamp(40px, 4.5vh, 64px);

  --ease: cubic-bezier(.22, .61, .36, 1);
}

/* Respect device safe-area if available (notch / TV overscan hints) */
@supports (padding: env(safe-area-inset-top)) {
  :root {
    --safe-x: max(env(safe-area-inset-left, 0px), 2.5vw);
    --safe-y: max(env(safe-area-inset-top, 0px), 2.5vh);
  }
}

@media (prefers-reduced-motion: reduce) {
  :root { --ease: linear; }
  *, *::before, *::after {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--fg);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
  text-rendering: optimizeLegibility;
  overflow: hidden;
  /* Disable double-tap zoom on TV WebViews */
  touch-action: manipulation;
}

img {
  display: block;
  max-width: 100%;
  user-select: none;
  -webkit-user-drag: none;
}
button { font: inherit; color: inherit; }

/* Accessible visually-hidden helper */
.sr-only {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

/* Skip link */
.skip-link {
  position: absolute;
  left: 1rem; top: -10rem;
  background: var(--focus);
  color: #000;
  padding: .75rem 1rem;
  border-radius: .5rem;
  font-weight: 700;
  z-index: 1000;
  transition: top .15s var(--ease);
}
.skip-link:focus { top: 1rem; outline: none; }

/* ====== Stage / slide canvas ====== */
.stage {
  position: relative;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  background: var(--bg);
  outline: none;
  overflow: hidden;
}

/* Frame occupies the whole safe area. Toolbar floats on top
   (auto-hides after idle) so slides use 100% of the canvas. */
.stage__frame {
  position: absolute;
  top: var(--safe-y);
  right: var(--safe-x);
  bottom: var(--safe-y);
  left: var(--safe-x);
}

/* Each slide fills the frame. The img inside is centered with
   object-fit:contain so aspect ratio is preserved at native pixels —
   no aspect-ratio CSS hack required (older TVs don't support it). */
.slide {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  opacity: 0;
  -webkit-transform: translateX(2%) scale(.99);
      -ms-transform: translateX(2%) scale(.99);
          transform: translateX(2%) scale(.99);
  -webkit-transition: opacity .42s var(--ease), -webkit-transform .42s var(--ease);
          transition: opacity .42s var(--ease), transform .42s var(--ease);
  will-change: opacity, transform;
  pointer-events: none;
}
.slide.is-active {
  opacity: 1;
  -webkit-transform: none;
      -ms-transform: none;
          transform: none;
  pointer-events: auto;
  z-index: 2;
}
.slide.is-prev {
  opacity: 0;
  -webkit-transform: translateX(-2%) scale(.99);
      -ms-transform: translateX(-2%) scale(.99);
          transform: translateX(-2%) scale(.99);
}
.slide img {
  display: block;
  max-width: 100%;
  max-height: 100%;
  width: auto;
  height: auto;
  -o-object-fit: contain;
     object-fit: contain;
  background: transparent; /* no white flash on dark TVs */
  -webkit-box-shadow: var(--shadow);
          box-shadow: var(--shadow);
  border-radius: 4px;
  /* Let TVs use their native resampler for photos — crisp-edges
     would harden blur and ruin JPEG content. */
  image-rendering: auto;
}

/* <picture> wraps each slide image so we can serve AVIF / WebP /
   JPG per browser capability. It must not influence layout.

   Base rule (works on every TV WebKit, old and new): make the
   <picture> a zero-overhead flex container that fills .slide, so
   the inner <img> still centers via its own max-width/height +
   object-fit: contain.

   The second rule then promotes <picture> to `display: contents`
   on modern browsers — the box vanishes from the layout tree and
   the <img> becomes the direct flex child, matching the original
   pre-<picture> DOM exactly. Legacy WebKits drop the unknown
   `display: contents` declaration and silently fall back to the
   base rule. Either way, slides render identically. */
.slide picture {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  width: 100%;
  height: 100%;
  max-width: 100%;
  max-height: 100%;
}
.slide picture,
.grid__item picture {
  display: contents;
}

/* Edge tap zones (for touch / mouse). Not focusable. */
.edge {
  position: absolute;
  top: 0;
  width: 18vw;
  max-width: 240px;
  height: 100%;
  background: transparent;
  border: 0;
  cursor: pointer;
  opacity: 0;
  z-index: 5;
}
.edge--prev { left: 0; }
.edge--next { right: 0; }
@media (hover: hover) and (pointer: fine) {
  .edge:hover { opacity: 1; background: -webkit-gradient(linear, left top, right top, from(rgba(0,0,0,.25)), to(rgba(0,0,0,0))); background: linear-gradient(90deg, rgba(0,0,0,.25), rgba(0,0,0,0)); }
  .edge--next:hover { background: linear-gradient(-90deg, rgba(0,0,0,.25), rgba(0,0,0,0)); }
}

/* ====== HUD (counter + progress) ====== */
.hud {
  position: absolute;
  left: var(--safe-x);
  right: var(--safe-x);
  bottom: calc(var(--safe-y) + var(--hud-h) + 1.4vh);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  gap: 1.2rem;
  pointer-events: none;
  z-index: 4;
  opacity: .9;
}
.hud__counter {
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 18px;
  font-size: clamp(14px, 1.6vh, 22px);
  background: var(--panel);
  border: 1px solid var(--border);
  padding: .35em .8em;
  border-radius: 999px;
  -webkit-backdrop-filter: blur(8px);
          backdrop-filter: blur(8px);
}
.hud__sep { opacity: .5; margin: 0 .25em; }
.hud__progress {
  -webkit-box-flex: 1;
      -ms-flex: 1;
          flex: 1;
  height: 4px;
  background: rgba(255,255,255,.12);
  border-radius: 999px;
  overflow: hidden;
}
.hud__bar {
  height: 100%;
  width: 0%;
  background: var(--fg);
  border-radius: inherit;
  -webkit-transition: width .35s var(--ease);
          transition: width .35s var(--ease);
}

/* ====== Toolbar ====== */
.toolbar {
  position: absolute;
  left: 50%;
  -webkit-transform: translateX(-50%);
      -ms-transform: translateX(-50%);
          transform: translateX(-50%);
  bottom: var(--safe-y);
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  gap: .5rem;
  padding: .5rem;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 999px;
  -webkit-backdrop-filter: blur(10px);
          backdrop-filter: blur(10px);
  z-index: 6;
  max-width: calc(100vw - 2 * var(--safe-x));
  overflow-x: auto;
  scrollbar-width: none;
}
.toolbar::-webkit-scrollbar { display: none; }

.btn {
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  gap: .55em;
  padding: .65em 1.1em;
  background: transparent;
  color: var(--fg);
  border: 1px solid transparent;
  border-radius: 999px;
  cursor: pointer;
  font-size: 16px;
  font-size: clamp(13px, 1.55vh, 18px);
  font-weight: 600;
  line-height: 1;
  -webkit-transition: background .18s var(--ease), border-color .18s var(--ease), -webkit-transform .15s var(--ease);
          transition: background .18s var(--ease), border-color .18s var(--ease), transform .15s var(--ease);
  white-space: nowrap;
}
.btn svg { width: 1.25em; height: 1.25em; -ms-flex: none; flex: none; }
.btn:hover { background: rgba(255,255,255,.08); }
.btn:active { -webkit-transform: scale(.97); -ms-transform: scale(.97); transform: scale(.97); }

/* High-visibility focus for D-pad / keyboard / TV remotes */
.btn:focus,
.btn:focus-visible,
.grid__item:focus,
.grid__item:focus-visible {
  outline: none;
  border-color: var(--focus);
  -webkit-box-shadow: 0 0 0 4px rgba(255, 204, 0, .28);
          box-shadow: 0 0 0 4px rgba(255, 204, 0, .28);
  background: rgba(255, 204, 0, .14);
}

.btn--ghost {
  background: rgba(255,255,255,.06);
}

/* Hide button text labels on very small screens to keep toolbar tight */
@media (max-width: 540px) {
  .btn__label { display: none; }
  .btn { padding: .8em; }
}

/* ====== Auto-hiding chrome ====== */
.is-idle .hud,
.is-idle .toolbar,
.is-idle .edge {
  opacity: 0;
  -webkit-transition: opacity .35s var(--ease);
          transition: opacity .35s var(--ease);
  pointer-events: none;
}
.is-idle { cursor: none; }

/* ====== Grid overlay ====== */
.grid-overlay {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  background: var(--panel-strong);
  -webkit-backdrop-filter: blur(14px);
          backdrop-filter: blur(14px);
  z-index: 50;
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-orient: vertical;
  -webkit-box-direction: normal;
      -ms-flex-direction: column;
          flex-direction: column;
  padding: var(--safe-y) var(--safe-x);
  -webkit-animation: fade .3s var(--ease);
          animation: fade .3s var(--ease);
}
.grid-overlay[hidden] { display: none; }

.grid-overlay__head {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: justify;
      -ms-flex-pack: justify;
          justify-content: space-between;
  margin-bottom: 1.2rem;
}
.grid-overlay__title {
  margin: 0;
  font-size: 26px;
  font-size: clamp(20px, 3vh, 32px);
  font-weight: 700;
  letter-spacing: -.01em;
}

.grid {
  -webkit-box-flex: 1;
      -ms-flex: 1;
          flex: 1;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
  gap: clamp(12px, 1.6vh, 22px);
  overflow-y: auto;
  padding: 4px;
  scroll-padding: 80px;
  scrollbar-width: thin;
  /* Align-content so partially-filled last row stays at the top */
  align-content: start;
}

/* Tile uses padding-bottom aspect hack for older TVs, then
   overrides with modern aspect-ratio where supported. */
.grid__item {
  position: relative;
  background: #111;
  border: 2px solid transparent;
  border-radius: 10px;
  overflow: hidden;
  cursor: pointer;
  padding: 0;
  /* Fallback: 16:9 via padding (9/16 = 56.25%) */
  padding-bottom: 56.25%;
  height: 0;
  -webkit-transition: -webkit-transform .2s var(--ease), border-color .2s var(--ease);
          transition: transform .2s var(--ease), border-color .2s var(--ease);
}
@supports (aspect-ratio: 16 / 9) {
  .grid__item {
    aspect-ratio: 16 / 9;
    padding-bottom: 0;
    height: auto;
  }
}
.grid__item img {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  width: 100%;
  height: 100%;
  -o-object-fit: cover;
     object-fit: cover;
  background: #111; /* match tile bg — no white flash on load */
}
.grid__item.is-current { border-color: var(--focus); }
.grid__item:hover { -webkit-transform: translateY(-2px); transform: translateY(-2px); }
.grid__item__num {
  position: absolute;
  left: .5rem; top: .5rem;
  background: rgba(0,0,0,.7);
  color: #fff;
  padding: .2em .55em;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
  z-index: 2;
}

/* ====== Help overlay ====== */
.help-overlay {
  position: fixed;
  top: 0; right: 0; bottom: 0; left: 0;
  background: rgba(0,0,0,.7);
  -webkit-backdrop-filter: blur(8px);
          backdrop-filter: blur(8px);
  display: grid;
  place-items: center;
  z-index: 60;
  padding: var(--safe-y) var(--safe-x);
  -webkit-animation: fade .25s var(--ease);
          animation: fade .25s var(--ease);
}
.help-overlay[hidden] { display: none; }
.help-overlay__panel {
  background: #111;
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 28px 36px;
  padding: clamp(20px, 3vh, 36px) clamp(24px, 4vw, 48px);
  max-width: 540px;
  width: 100%;
  -webkit-box-shadow: var(--shadow);
          box-shadow: var(--shadow);
}
.help-overlay__title {
  margin: 0 0 1rem;
  font-size: 26px;
  font-size: clamp(22px, 3vh, 30px);
  font-weight: 700;
}
.help-list {
  list-style: none;
  margin: 0 0 1.4rem;
  padding: 0;
  display: grid;
  gap: .6rem;
  font-size: 16px;
  font-size: clamp(14px, 1.7vh, 18px);
}
.help-list li {
  display: -webkit-box;
  display: -ms-flexbox;
  display: flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  gap: .6rem;
  -ms-flex-wrap: wrap;
      flex-wrap: wrap;
}
.help-list span { color: var(--muted); margin-left: auto; }
kbd {
  display: -webkit-inline-box;
  display: -ms-inline-flexbox;
  display: inline-flex;
  -webkit-box-align: center;
      -ms-flex-align: center;
          align-items: center;
  -webkit-box-pack: center;
      -ms-flex-pack: center;
          justify-content: center;
  min-width: 2em;
  padding: .15em .55em;
  background: #1d1d1d;
  border: 1px solid #333;
  border-bottom-width: 2px;
  border-radius: 6px;
  font: 600 .9em ui-monospace, SFMono-Regular, Menlo, monospace;
  color: #f0f0f0;
}

/* Samsung / LG remote color button swatches shown in the help
   overlay so the user can match them visually to their physical
   remote. */
.kbd-col {
  width: 1.4em;
  height: 1.4em;
  min-width: 0;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,.25);
  -webkit-box-shadow: 0 0 0 2px rgba(0,0,0,.4) inset;
          box-shadow: 0 0 0 2px rgba(0,0,0,.4) inset;
}
.kbd-col--red    { background: #e53935; }
.kbd-col--green  { background: #43a047; }
.kbd-col--yellow { background: #fdd835; }
.kbd-col--blue   { background: #1e88e5; }

@-webkit-keyframes fade { from { opacity: 0; -webkit-transform: scale(.98); transform: scale(.98); } to { opacity: 1; -webkit-transform: none; transform: none; } }
@keyframes fade { from { opacity: 0; -webkit-transform: scale(.98); transform: scale(.98); } to { opacity: 1; -webkit-transform: none; transform: none; } }

/* ====== High-contrast & forced-colors ====== */
@media (prefers-contrast: more), (forced-colors: active) {
  :root {
    --panel: #000;
    --panel-strong: #000;
    --border: #fff;
    --focus: Highlight;
  }
  .btn { border-color: #fff; }
}

/* ====== Fullscreen presentation mode ======
   When the document enters native fullscreen (F key, toolbar
   button, or :fullscreen pseudo), we drop the overscan gutter
   to zero and let the slide fill the entire physical display,
   even on small monitors. Chrome auto-hides aggressively via
   the shorter IDLE_MS_FULLSCREEN timeout set in JS.

   The body class is kept in sync via the fullscreenchange event
   — we use it as the authoritative selector instead of
   :fullscreen so nested elements (like the slide inside the
   stage) can still inherit correctly in older WebKit. */
body.is-fullscreen {
  --safe-x: 0px;
  --safe-y: 0px;
}
body.is-fullscreen .slide img {
  border-radius: 0;
}
body.is-fullscreen .stage__frame {
  top: 0; right: 0; bottom: 0; left: 0;
}
body.is-fullscreen .hud {
  left: 32px;
  right: 32px;
  bottom: calc(var(--hud-h) + 32px);
}
body.is-fullscreen .toolbar {
  bottom: 22px;
}
/* Native :fullscreen fallback — for browsers that somehow miss
   the fullscreenchange event (unlikely but cheap insurance). */
:-webkit-full-screen .stage__frame,
:fullscreen .stage__frame {
  top: 0; right: 0; bottom: 0; left: 0;
}
:-webkit-full-screen .slide img,
:fullscreen .slide img {
  border-radius: 0;
}

/* ====== 1080p+ Smart TVs: native-pixel slides ======
   Drop overscan gutter so a 1920x1080 TV renders the slide at
   exactly 1920x1080 CSS pixels (1920x1077 image + 3px letterbox),
   preventing any downscale / softness. Toolbar auto-hides on idle,
   and while visible it simply floats on top of the slide. */
@media (min-width: 1900px) {
  :root {
    --safe-x: 0px;
    --safe-y: 0px;
    --hud-h: 64px;
  }
  .btn { font-size: 20px; padding: .8em 1.4em; }
  .hud__counter { font-size: 22px; }
  .hud {
    left: 24px;
    right: 24px;
    bottom: calc(var(--hud-h) + 24px);
  }
  .toolbar { bottom: 18px; }
  .slide img { border-radius: 0; } /* true edge-to-edge */
}

/* ====== 4K Smart TVs (3840 logical px) ====== */
@media (min-width: 3800px) {
  :root { --hud-h: 96px; }
  .btn { font-size: 28px; padding: 1em 1.8em; }
  .hud__counter { font-size: 32px; padding: .45em 1em; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(420px, 1fr)); gap: 28px; }
}

/* ====== Portrait phones (presentation still legible) ====== */
@media (orientation: portrait) and (max-width: 720px) {
  .stage__frame { bottom: calc(var(--safe-y) + var(--hud-h) + 8vh); }
}

/* Animated WebGL "liquid glass" hero (slide 1). Fills the slide;
   the shader letterboxes the 16:9 image, so any stage aspect is safe. */
.slide .lg-frame {
  display: block;
  width: 100%;
  height: 100%;
  border: 0;
  border-radius: 4px;
  background: #000;
  box-shadow: var(--shadow);
}
