:root {
  /* Classic teletext 8-colour palette */
  --tt-black: #000000;
  --tt-red: #ff2b2b;
  --tt-green: #22e622;
  --tt-yellow: #ffff2b;
  --tt-blue: #3b6bff;
  --tt-magenta: #ff44ff;
  --tt-cyan: #22e6e6;
  --tt-white: #ffffff;
  --cols: 40;
  --rows: 25;
  --cell: 22px;   /* character cell width */
  --line: 30px;   /* character cell height */
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: #000;
  color: #ddd;
  font-family: system-ui, -apple-system, sans-serif;
  overflow: hidden;
}

/* The teletext window fills the whole browser; the 4:3 screen is centred and
   letterboxed on black. Native full screen (on #bezel) then fills the display. */
#bezel {
  position: fixed;
  inset: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

#screen {
  position: relative;
  background: var(--tt-black);
  width: min(100vw, calc(100vh * 4 / 3));
  height: min(100vh, calc(100vw * 3 / 4));
  aspect-ratio: 4 / 3;
  overflow: hidden;
  /* subtle CRT glow + scanlines */
  box-shadow: inset 0 0 60px rgba(0,60,90,.25);
  font-family: 'VT323', 'Courier New', monospace;
  container-type: size;
}
/* Rows are laid out at their natural monospace width, then stretched
   horizontally (transform in app.js) to fill the 4:3 screen — the way
   teletext characters filled a CRT. */
#grid {
  position: absolute;
  top: 0.6cqh;
  left: 0;
  width: max-content;
  transform-origin: top left;
}
#screen::after {
  content: '';
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    to bottom, rgba(0,0,0,0) 0, rgba(0,0,0,0) 2px, rgba(0,0,0,.18) 3px, rgba(0,0,0,0) 4px);
  mix-blend-mode: multiply;
}

/* Each row is a 40-cell monospace line. Sized in container query units so the
   whole grid scales with the screen width and always fits 40 columns. */
.ttrow {
  display: block;
  white-space: pre;
  font-size: 3.36cqh;        /* sized by height so 25 rows fill the screen */
  line-height: 1.18;
  letter-spacing: 0;
  height: 1.18em;
  font-family: 'VT323', 'Courier New', monospace;
}
.ttrow.dh {                 /* double-height masthead / titles */
  font-size: 6.72cqh;
  line-height: 1.18;
  height: 1.18em;
  font-weight: 400;
}
.ttrow span { white-space: pre; }

/* overlay layer (SVG hemicycle on page 120) — above the grid, under scanlines */
#overlay {
  position: absolute;
  left: 0;
  right: 0;
  top: 16%;
  bottom: 4%;
  z-index: 2;
  pointer-events: none;
}
#overlay:empty { display: none; }
#overlay svg { width: 100%; height: 100%; display: block; }

/* colours */
.fg-black { color: var(--tt-black); }
.fg-red { color: var(--tt-red); }
.fg-green { color: var(--tt-green); }
.fg-yellow { color: var(--tt-yellow); }
.fg-blue { color: var(--tt-blue); }
.fg-magenta { color: var(--tt-magenta); }
.fg-cyan { color: var(--tt-cyan); }
.fg-white { color: var(--tt-white); }
.bg-black { background: var(--tt-black); }
.bg-red { background: var(--tt-red); }
.bg-green { background: var(--tt-green); }
.bg-yellow { background: var(--tt-yellow); }
.bg-blue { background: var(--tt-blue); }
.bg-magenta { background: var(--tt-magenta); }
.bg-cyan { background: var(--tt-cyan); }
.bg-white { background: var(--tt-white); }

.link { cursor: pointer; }
.link:hover { background: rgba(255,255,255,.18); }

/* --- floating controls (info + full screen) over the screen corner --- */
#controls {
  position: absolute;
  top: 12px;
  right: 14px;
  z-index: 5;
  display: flex;
  gap: 8px;
  opacity: 0.25;
  transition: opacity 0.2s;
}
#bezel:hover #controls, #controls:focus-within { opacity: 1; }
#controls button {
  width: 40px;
  height: 40px;
  font-size: 20px;
  line-height: 1;
  color: #cde;
  background: rgba(20, 24, 30, .7);
  border: 1px solid #3a5;
  border-radius: 8px;
  cursor: pointer;
}
#controls button:hover { background: #26303a; color: #fff; }

/* --- mobile TV remote --- */
#remote { display: none; }

@media (max-width: 760px), (pointer: coarse) and (max-width: 900px) {
  html, body { overflow: auto; height: auto; }
  #bezel {
    position: static;
    inset: auto;
    flex-direction: column;
    align-items: stretch;
    min-height: 100vh;
    background: #08080a;
  }
  #screen { width: 100vw; height: calc(100vw * 3 / 4); flex: none; }
  #controls { display: none; }

  #remote {
    display: flex;
    flex-direction: column;
    gap: 12px;
    width: 100%;
    max-width: 440px;
    margin: 14px auto 26px;
    padding: 16px 18px 22px;
    box-sizing: border-box;
    background: linear-gradient(180deg, #2c2e33, #17181b);
    border: 1px solid #000;
    border-radius: 26px;
    box-shadow: 0 12px 34px rgba(0,0,0,.6), inset 0 1px 0 rgba(255,255,255,.06);
    font-family: 'VT323', monospace;
    -webkit-user-select: none;
    user-select: none;
  }
  .r-brand {
    text-align: center;
    color: var(--tt-cyan);
    letter-spacing: 3px;
    font-size: 15px;
    opacity: .8;
  }
  #remote button {
    font-family: 'VT323', monospace;
    color: #e8f0f0;
    background: #3a3c42;
    border: 1px solid #0c0c0e;
    border-radius: 12px;
    box-shadow: 0 2px 0 #0c0c0e;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }
  #remote button:active { transform: translateY(1px); box-shadow: 0 1px 0 #0c0c0e; }

  .r-fn { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
  .r-fn button { font-size: 17px; padding: 12px 0; color: #cfe; }
  #holdbtn.on { background: var(--tt-cyan); color: #000; }

  .r-colours { display: flex; gap: 10px; }
  #remote .rc { flex: 1; height: 22px; background: var(--c); border: 1px solid #0c0c0e; border-radius: 6px; box-shadow: 0 2px 0 #0c0c0e; }

  .r-dpad {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    grid-template-areas: ".  up  ." "left mid right" ".  down  .";
    gap: 8px;
    align-items: center;
    justify-items: stretch;
  }
  .r-dpad button { font-size: 22px; padding: 16px 0; }
  .d-up { grid-area: up; }
  .d-left { grid-area: left; }
  .d-right { grid-area: right; }
  .d-down { grid-area: down; }
  .d-mid {
    grid-area: mid;
    text-align: center;
    color: #7fffd4;
    background: #000;
    border: 1px solid #2a4;
    border-radius: 10px;
    padding: 14px 0;
    font-size: 26px;
    letter-spacing: 2px;
  }

  .r-num { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
  .r-num button { font-size: 26px; padding: 16px 0; }
  .num-zero { grid-column: 2; }
}
