@font-face {
  font-family: "VT323";
  src: url("fonts/VT323-Regular.ttf") format("truetype");
  font-display: swap;
}

@font-face {
  font-family: Arimo;
  src: url("fonts/Arimo.ttf") format("truetype");
  font-display: swap;
  font-weight: 100 900;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: #050505;
  color: #fff;
}

body {
  font-family: "VT323", monospace;
}

button,
input,
select {
  font: inherit;
  color: inherit;
}

button,
select {
  background: #151515;
  border: 1px solid #555;
  border-radius: 0;
  padding: 0.4em 0.65em;
}

select {
  font-size: 18px;
}

button {
  cursor: pointer;
}

button:hover,
button:focus-visible,
select:focus-visible {
  background: #0a2569;
  border-color: #f0cb40;
  outline: 2px solid #f0cb40;
}

button:disabled,
select:disabled {
  opacity: 0.45;
}

button[aria-pressed="true"] {
  background: #f0cb40;
  color: #06184a;
  border-color: #f0cb40;
}

a {
  color: #f0cb40;
}

/* The picture keeps its aspect ratio inside whatever height the browser shows above the remote. */
main {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
}

#stage {
  flex: 1 1 auto;
  min-height: 0;
  container-type: size;
  display: grid;
  place-items: center;
  background: #000;
}

#picture {
  position: relative;
  background: #000;
  width: min(100cqw, 100cqh * 16 / 9);
  aspect-ratio: 16/9;
  overflow: hidden;
  container-type: inline-size;

  --design-width: 1600;

  /* Resolved against #stage: an element's own container units skip itself. */
  --stage-width: 100cqw;
}

#picture.four-three {
  width: min(100cqw, 100cqh * 4 / 3);
  aspect-ratio: 4/3;

  --design-width: 1200;
}

video,
canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
}

#picture.four-three video {
  object-fit: cover;
}

@property --stage-width {
  syntax: "<length>";
  inherits: true;
  initial-value: 0;
}

/* A 16:9 frame in a wider window leaves margins at its sides: a film wider than 16:9 grows into them
   while it plays, never taller than the frame. A station screen or teletext covers the frame alone,
   so the video then shrinks back behind it and no frozen frame shows beside them. */
#picture:not(.four-three):has(#screen[hidden]):has(#teletext[hidden]) {
  overflow: visible;
}

#picture:not(.four-three):has(#screen[hidden]):has(#teletext[hidden]) video {
  left: 50%;
  width: var(--stage-width);
  translate: -50%;
}

#teletext {
  z-index: 5;
}

#channel-osd {
  position: absolute;
  top: 5%;
  right: 3.333%;
  width: 25%;
  height: 20%;
  color: #0f0;
  text-align: right;
  z-index: 6;
  text-shadow:
    -2px 0 #000,
    2px 0 #000,
    0 -2px #000,
    0 2px #000,
    2px 2px #000;
}

#channel-osd strong {
  display: block;
  font-size: calc(100cqw * 136 / var(--design-width));
  font-weight: normal;
  line-height: 1;
}

#channel-osd span {
  display: block;
  font-size: calc(100cqw * 53 / var(--design-width));
  line-height: 1.1;
}

#osd {
  position: absolute;
  bottom: 8.333%;
  left: 20%;
  width: 60%;
  padding: 1.4% 0;
  border-radius: 8px;
  font-size: calc(100cqw * 36.6 / var(--design-width));
  background: #000d;
  color: #0f0;
  text-align: center;
  z-index: 6;
}

#osd > div {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#osd > :first-child {
  color: #ff0;
}

#lower-third {
  position: absolute;
  bottom: 10%;
  left: 6.25%;
  width: 87.5%;
  height: 14.2857%;
  display: flex;
  font-family: Arimo, sans-serif;
  font-weight: 700;
  container-type: size;
}

#lower-number {
  flex: 0 0 auto;
  width: 120cqh;
  height: 100%;
  border-bottom: 10cqh solid #f0cb40;
  background: #06184a;
  color: #fff;
  display: grid;
  place-items: center;
  font-size: 50cqh;
}

#lower-third > div {
  flex: 1;
  min-width: 0;
  background: #0a2569;
  padding: 10cqh 30cqh;
}

#lower-title,
#lower-when {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

#lower-title {
  color: #fff;
  font-size: 30cqh;
  height: 46cqh;
}

#lower-when {
  color: #f0cb40;
  font-size: 25cqh;
}

#connect {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.quiet {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* Over the "plex.tv/link" line the test card paints (tv/screens.py login code). */
#plex-link {
  position: absolute;
  top: 86.6%;
  left: 50%;
  width: calc(100cqw * 220 / var(--design-width));
  height: 4.2%;
  transform: translateX(-50%);
  color: transparent;
  pointer-events: auto;
}

#plex-link:focus-visible {
  outline: 3px solid #f0cb40;
}

#help {
  position: absolute;
  inset: 0;
  z-index: 10;
  display: grid;
  align-content: center;
  justify-items: center;
  background: #000a;
  font-size: calc(100cqw * 34 / var(--design-width));
  cursor: pointer;
}

#help-title,
#help-keys,
#help-close {
  width: calc(100cqw * 864 / var(--design-width));
  margin: 0;
}

#help-title {
  padding: 0.25em 0.5em;
  background: #f00;
  color: #ff0;
  font-weight: normal;
  font-size: 2em;
  line-height: 1;
}

#help-keys {
  display: grid;
  grid-template-columns: 11ch 1fr;
  padding: 0.7em 0.5em;
  background: #000;
  line-height: 1.06;
}

#help-keys dt {
  color: #ff0;
}

#help-keys dd {
  margin: 0;
  color: #fff;
}

#help-close {
  border: 0;
  padding: 0.1em 0.5em;
  background: #0ff;
  color: #000;
  text-align: left;
  font-size: 1em;
}

#help-close:hover,
#help-close:focus-visible {
  background: #0ff;
  outline: 3px solid #ff0;
}

#resume {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  font-size: 4cqw;
  background: #0a2569;
}

/* The remote keeps one height whatever the channels, tracks or teletext show, so the picture never jumps. */
#remote {
  --bar: 64px;
  --key: 40px;

  flex: 0 0 auto;
  border-top: 3px solid #f0cb40;
  background: #0b0b0b;
  color: #ddd;
  font-size: 21px;
}

#remote-bar {
  height: var(--bar);
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
}

label {
  display: flex;
  gap: 7px;
  align-items: center;
}

#remote-bar button,
#page {
  flex: 0 0 auto;
  height: var(--key);
  min-width: var(--key);
  padding: 0 0.5em;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

#display {
  flex: 0 1 170px;
  min-width: 3.4em;
  height: 44px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px;
  overflow: hidden;
  background: #000;
  border: 1px solid #333;
  color: #0f0;
}

#display-number {
  flex: 0 0 auto;
  font-size: 36px;
  font-weight: normal;
  line-height: 1;
}

#display-name {
  min-width: 0;
  overflow: hidden;
  font-size: 18px;
  line-height: 1;
  white-space: nowrap;
  text-overflow: ellipsis;
}

#remote:has(#text-controls:not([hidden])) #display-number {
  color: #ff0;
}

#remote:has(#text-controls:not([hidden])) #display-name {
  color: #0ff;
}

/* Too many channels scroll sideways instead of wrapping into a new row. */
#tuner {
  flex: 1 1 auto;
  min-width: 0;
  overflow: auto hidden;
  scrollbar-width: none;
}

#channel-buttons,
#text-controls {
  width: max-content;
  display: flex;
  gap: 6px;
  align-items: center;
}

#page {
  width: 3.4em;
  background: #000;
  border: 1px solid #555;
}

/* The teletext Fastext colors of a remote's four colored keys. */
#fastext {
  flex: 0 0 auto;
  display: flex;
  gap: 8px;
}

#fastext button {
  border-top: 4px solid var(--fastext);
}

#info {
  --fastext: #e33;
}

#text {
  --fastext: #2c2;
}

#settings {
  --fastext: #f0cb40;
}

#keys-button {
  --fastext: #0ff;
}

#volume-control {
  flex: 0 0 auto;
  gap: 8px;
}

/* A link to the guide page, dressed as one more key of the remote. */
#guide-link {
  flex: 0 0 auto;
  height: var(--key);
  display: flex;
  align-items: center;
  border: 1px solid #555;
  padding: 0 0.5em;
  background: #151515;
  color: inherit;
  text-decoration: none;
  white-space: nowrap;
}

#guide-link:hover,
#guide-link:focus-visible {
  background: #0a2569;
  border-color: #f0cb40;
  outline: 2px solid #f0cb40;
}

/* Ten blocks like the TV's volume bar; app.mjs sets --level. */
#volume {
  width: 90px;
  height: 18px;
  margin: 0;
  appearance: none;
  background: transparent;
  cursor: pointer;
  mask: repeating-linear-gradient(90deg, #000 0 7px, transparent 7px 9px);
}

#volume::-webkit-slider-runnable-track {
  height: 18px;
  background: linear-gradient(90deg, #f0cb40 var(--level, 75%), #444 0);
}

#volume::-moz-range-track {
  height: 18px;
  background: linear-gradient(90deg, #f0cb40 var(--level, 75%), #444 0);
}

#volume::-webkit-slider-thumb {
  width: 1px;
  height: 18px;
  appearance: none;
  background: transparent;
}

#volume::-moz-range-thumb {
  width: 0;
  border: 0;
  background: transparent;
}

#volume:focus-visible {
  outline: none;
}

#volume-control:has(#volume:focus-visible) {
  outline: 2px solid #f0cb40;
  outline-offset: 4px;
}

#fullscreen::before {
  content: "";
  display: block;
  width: 20px;
  height: 14px;
  border: 2px solid currentcolor;
}

/* Opens upwards over the picture, so the remote itself never grows. */
#settings-menu {
  inset: auto 12px calc(var(--bar) + 11px) auto;
  width: min(360px, 100vw - 24px);
  margin: 0;
  padding: 0 0 0.5em;
  border: 1px solid #333;
  background: #000;
  color: #fff;
  font-size: 20px;
}

#settings-title {
  margin: 0 0 0.2em;
  padding: 0.1em 0.5em;
  background: #f00;
  color: #ff0;
  font-size: 1.2em;
  font-weight: normal;
  text-transform: uppercase;
}

#settings-menu label {
  display: grid;
  grid-template-columns: 6.5em minmax(0, 1fr);
  padding: 0.25em 0.5em;
  color: #ff0;
}

#settings-menu select {
  width: 100%;
  color: #fff;
}

#logout {
  display: block;
  width: calc(100% - 1em);
  margin: 0.4em 0.5em 0;
  border: 0;
  padding: 0.4em 0.65em;
  background: #0ff;
  color: #000;
  text-align: left;
  text-decoration: none;
}

#logout:hover,
#logout:focus-visible {
  background: #0ff;
  outline: 3px solid #ff0;
}

main:fullscreen {
  background: #000;
}

main:fullscreen #remote {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  background: #000d;
  opacity: 0;
  transition: opacity 0.2s;
}

main:fullscreen #remote:hover,
main:fullscreen #remote:focus-within,
main:fullscreen #remote:has(#settings-menu:popover-open) {
  opacity: 1;
}

[hidden] {
  display: none !important;
}

@media (width <= 1100px) {
  #display {
    flex-basis: 3.4em;
  }

  #display-name,
  #volume-control span {
    display: none;
  }
}

@media (width <= 650px) {
  #stage {
    align-items: start;
  }
}

/* A phone gets three fixed rows: display, guide and stepping, channels, the colored keys. */
@media (width <= 800px) {
  #remote {
    --bar: 156px;
  }

  #remote-bar {
    display: grid;
    grid-template:
      "display guide previous next fullscreen" 52px
      "tuner tuner tuner tuner tuner" 52px
      "fastext fastext fastext fastext fastext" 52px
      / minmax(0, 1fr) auto auto auto auto;
    gap: 0 6px;
    padding: 0 8px;
  }

  #display {
    grid-area: display;
  }

  #display-name {
    display: block;
  }

  #guide-link {
    grid-area: guide;
    font-size: 18px;
  }

  #previous {
    grid-area: previous;
  }

  #next {
    grid-area: next;
  }

  #fullscreen {
    grid-area: fullscreen;
  }

  #tuner {
    grid-area: tuner;
  }

  #fastext {
    grid-area: fastext;
    display: grid;
    grid-template-columns: repeat(4, minmax(0, 1fr));
    gap: 6px;
  }

  #fastext button {
    min-width: 0;
    font-size: 18px;
  }

  #volume-control {
    display: none;
  }
}
