/* esbes — TV-set scene layout
 * The image fills the viewport (cropping wall edges where aspect mismatches),
 * with an extra punch-in zoom so the TV occupies more frame.
 * The .screen overlay is positioned in PERCENTAGES of the image, driven by
 * CSS custom properties below. Fine-tune those if alignment looks off.
 */

* { box-sizing: border-box; }

:root {
  /* TV-set placement on the wall (% of scene). The TV is now drawn by CSS,
   * not part of the image. Adjust to taste. */
  --tv-top: 20%;
  --tv-left: 22%;
  --tv-width: 56%;
  --tv-aspect: 16 / 9;
  --tv-bezel: 1.6%;          /* bezel padding around screen */
  /* Glow origin — keep loosely synced with the TV's geometric center.
   * Approx: tv-left + tv-width/2 (x) and tv-top + tv-height/2 (y). */
  --glow-x: 50%;
  --glow-y: 43%;

  /* How much the image overflows the viewport to bring the scene closer */
  --zoom: 1.28;

  /* Ambient-light glow color (set live by app.js from video content) */
  --tv-glow: rgba(0, 0, 0, 0);
  --tv-glow-soft: rgba(0, 0, 0, 0);
}

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: #e6e6e6;
  font-family: 'Helvetica Neue', Arial, sans-serif;
  overflow: hidden;
  user-select: none;
  -webkit-user-select: none;
}

/* Viewport — fills the window, hides image overflow */
.viewport {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

/* Scene — sized to cover the viewport, then zoomed further by --zoom */
.scene {
  position: relative;
  aspect-ratio: 2528 / 1684;
  width: calc(max(100vw, 100vh * (2528 / 1684)) * var(--zoom));
  height: auto;
}

/* Soft ambient-light tint emanating from the screen across the whole scene.
 * Below the image visually, but with a screen-blend so it lifts wood/wall warmth
 * only where they're already bright-ish. Falls off with distance from the screen.
 */
.scene::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(
    ellipse 55% 40% at var(--glow-x) var(--glow-y),
    var(--tv-glow-soft) 0%,
    transparent 65%
  );
  mix-blend-mode: screen;
  pointer-events: none;
  transition: background 220ms ease;
  z-index: 2;
}

.tv {
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
  -webkit-user-drag: none;
  position: relative;
  z-index: 1;
  /* Smooth upscaling — important since the image is being scaled past its
   * native resolution due to --zoom. */
  image-rendering: smooth;
  image-rendering: high-quality;
  -ms-interpolation-mode: bicubic;
  /* Promote to its own GPU layer to get the browser's higher-quality scaler */
  transform: translateZ(0);
  backface-visibility: hidden;
}

/* ---- TV SET (CSS-rendered bezel on the wall) ---- */
.tv-set {
  position: absolute;
  top: var(--tv-top);
  left: var(--tv-left);
  width: var(--tv-width);
  aspect-ratio: var(--tv-aspect);
  background: linear-gradient(180deg, #181614 0%, #0c0a09 100%);
  border-radius: 8px;
  padding: var(--tv-bezel);
  display: flex;
  z-index: 3;
  /* Drop shadow on the wall + crisp outer edge + subtle top highlight */
  box-shadow:
    0 22px 50px rgba(0, 0, 0, 0.55),
    0 0 0 1px rgba(0, 0, 0, 0.7),
    inset 0 1px 0 rgba(255, 255, 255, 0.04),
    inset 0 -1px 2px rgba(0, 0, 0, 0.6);
}

/* Power LED on the bezel, bottom-right */
.tv-set::after {
  content: '';
  position: absolute;
  bottom: 1.1%;
  right: 2.4%;
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #c43020;
  box-shadow: 0 0 3px rgba(196, 48, 32, 0.5);
  transition: background 240ms ease, box-shadow 240ms ease;
}
.scene[data-power="on"] .tv-set::after {
  background: #2adc52;
  box-shadow: 0 0 6px rgba(42, 220, 82, 0.75);
}

/* ---- SCREEN (inside the bezel) ---- */
.screen {
  position: relative;
  flex: 1;
  overflow: hidden;
  background: #000;
  border-radius: 3px;
  /* Outer glow — close halo + softer falloff. Extends beyond the bezel,
   * lighting the wall around the TV. */
  box-shadow:
    0 0 24px 3px var(--tv-glow),
    0 0 80px 22px var(--tv-glow-soft);
  transition: box-shadow 220ms ease;
}

#video,
#snow {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
}

#video {
  object-fit: cover;
  background: #000;
  transform-origin: center;
}

#snow {
  opacity: 0;
  transition: opacity 80ms linear;
  pointer-events: none;
}
#snow.on { opacity: 1; }

/* CRT power-off animation: collapse to a horizontal line, brief flare, then black */
.powerdown {
  position: absolute;
  inset: 0;
  background: #000;
  opacity: 1;
  transition: opacity 240ms ease;
  pointer-events: none;
  z-index: 4;
}
.scene[data-power="on"] .powerdown { opacity: 0; }

.scene[data-power="off"] #video,
.scene[data-power="off"] #snow {
  animation: crt-off 320ms ease-in forwards;
}
.scene[data-power="on"] #video,
.scene[data-power="on"] #snow {
  animation: crt-on 320ms ease-out;
}

/* When off, kill the glow too */
.scene[data-power="off"] {
  --tv-glow: rgba(0, 0, 0, 0);
  --tv-glow-soft: rgba(0, 0, 0, 0);
}

@keyframes crt-off {
  0%   { transform: scale(1, 1);     filter: brightness(1);   opacity: 1; }
  60%  { transform: scale(1, 0.006); filter: brightness(2.5); opacity: 1; }
  100% { transform: scale(0, 0);     filter: brightness(0);   opacity: 0; }
}
@keyframes crt-on {
  0%   { transform: scale(0, 0);     filter: brightness(0);   opacity: 0; }
  40%  { transform: scale(1, 0.006); filter: brightness(2.5); opacity: 1; }
  100% { transform: scale(1, 1);     filter: brightness(1);   opacity: 1; }
}

/* OSD pinned to the viewport top-right, outside the screen */
#osd {
  position: fixed;
  top: 18px;
  right: 18px;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(6px);
  border-radius: 8px;
  font-size: 12px;
  opacity: 0.55;
  transition: opacity 400ms ease, background 400ms ease;
  z-index: 60;
}
#osd.show {
  opacity: 1;
  background: rgba(0, 0, 0, 0.7);
}
#flag { font-size: 16px; line-height: 1; }
#channel-name {
  font-weight: 500;
  font-size: 12px;
  letter-spacing: 0.02em;
}
#country-name {
  font-size: 10px;
  opacity: 0.7;
  text-transform: uppercase;
  letter-spacing: 0.1em;
}

#loading {
  position: absolute;
  right: 4%;
  bottom: 4%;
  font-size: 10px;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  background: rgba(0, 0, 0, 0.55);
  padding: 4px 8px;
  border-radius: 5px;
  z-index: 5;
}
#loading.hidden { display: none; }

/* ---- Remote ----
 * Fixed to viewport bottom-right so it doesn't ride the zoom transform.
 * Subtle by default, brightens on hover.
 */
.remote {
  position: fixed;
  right: 24px;
  bottom: 24px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 12px 10px;
  background: rgba(20, 16, 14, 0.45);
  backdrop-filter: blur(6px);
  border-radius: 14px;
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.45);
  z-index: 50;
  opacity: 0.55;
  transition: opacity 220ms ease;
}
.remote:hover { opacity: 1; }

.rbtn {
  appearance: none;
  background: #1a1a1a;
  color: #e6e6e6;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 44px;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
  transition: background 120ms ease, transform 60ms ease;
}
.rbtn:hover { background: #222; }
.rbtn:active { transform: translateY(1px); background: #0a0a0a; }

.rbtn.power {
  color: #ff5a47;
  font-size: 16px;
  background: #141414;
}
.rbtn.power:hover { background: #1c1010; }

.rbtn.channel { font-size: 12px; }

.vol { display: flex; gap: 6px; }
.vol .volbtn { flex: 1; padding: 6px 8px; font-size: 14px; }

/* Debug helper — visit /?debug=1 to outline the TV bounding box */
.scene.debug .tv-set {
  outline: 2px solid #ff2d2d;
  outline-offset: 2px;
}
