/* Aertel Reborn: a 40x25 teletext screen on a CRT, as large as the window allows at 4:3,
   with a remote control that stays out of the way until it is wanted. */

@font-face {
  font-family: "Bedstead";
  src: url("fonts/bedstead.woff2") format("woff2");
  font-weight: normal;
  font-style: normal;
  font-display: block;
}

:root {
  --tt-black: #000; --tt-red: #f00; --tt-green: #0f0; --tt-yellow: #ff0;
  --tt-blue: #00f; --tt-magenta: #f0f; --tt-cyan: #0ff; --tt-white: #fff;
  --edge: clamp(4px, 1.4vmin, 14px); /* gap between the set and the window, so no row touches the edge */
  --bezel: 0px;              /* cabinet around the glass (CRT mode) */
  --scan: 2px;               /* scanline period, set by the script from the screen height */
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; background: #000; color: #ddd; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

/* the set is centred and as big as the window allows, always 4:3, never stretched */
.stage {
  position: fixed; inset: 0;
  display: flex; align-items: center; justify-content: center;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
}

/* ---- the television ---------------------------------------------------------
   .tv is the cabinet (only visible in CRT mode), .screen the glass. */
.tv { position: relative; padding: var(--bezel); }

.screen {
  --room: calc(2 * (var(--edge) + var(--bezel)));
  position: relative;
  /* --size (0.4-1) is the viewer's own size setting: + and - keys, Ctrl/Cmd +/-, Ctrl+wheel, A-/A+ */
  width: calc(var(--size, 1) * min(calc(100vw - var(--room)), calc((100vh - var(--room)) * 4 / 3)));
  width: calc(var(--size, 1) * min(calc(100dvw - var(--room) - env(safe-area-inset-left) - env(safe-area-inset-right)),
             calc((100dvh - var(--room) - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * 4 / 3)));
  aspect-ratio: 4 / 3;
  overflow: hidden;
  background: #000;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}
@supports not (aspect-ratio: 1 / 1) {
  .screen::before { content: ""; display: block; padding-top: 75%; }
}

/* The page is laid out at a 12x21 px cell (480x525 px in all), then scaled as one
   layer to fit the glass, stretched 4:3 like a 625-line set. Whole-pixel cells keep
   the block graphics seamless. The script sets --sx/--sy. */
.tt {
  position: absolute;
  left: var(--ox, 0px); top: var(--oy, 0px);
  width: 480px;
  height: 525px;
  font-family: "Bedstead", "Courier New", monospace;
  font-size: 20px;
  line-height: 21px;
  transform: scale(var(--sx, 1), var(--sy, 1));
  transform-origin: 0 0;
  white-space: pre;
  color: #fff;
  font-variant-ligatures: none;
  letter-spacing: 0;
  text-rendering: geometricPrecision;
  -webkit-font-smoothing: none;
}
.tt .r { height: 21px; white-space: pre; overflow: visible; }
.tt span { display: inline-block; height: 21px; vertical-align: top; }

/* the eight teletext colours */
.f0 { color: var(--tt-black); }  .f1 { color: var(--tt-red); }    .f2 { color: var(--tt-green); } .f3 { color: var(--tt-yellow); }
.f4 { color: var(--tt-blue); }   .f5 { color: var(--tt-magenta); } .f6 { color: var(--tt-cyan); }  .f7 { color: var(--tt-white); }
.b0 { background: var(--tt-black); }  .b1 { background: var(--tt-red); }    .b2 { background: var(--tt-green); } .b3 { background: var(--tt-yellow); }
.b4 { background: var(--tt-blue); }   .b5 { background: var(--tt-magenta); } .b6 { background: var(--tt-cyan); }  .b7 { background: var(--tt-white); }

/* block graphics and thin rules: one SVG path per run */
.tt .gx { display: inline-block; height: 21px; vertical-align: top; }
.tt svg.mo { display: block; height: 21px; overflow: visible; }

/* double height: the whole row is stretched over the blank row beneath */
.tt .r.dhr { transform: scaleY(2); transform-origin: 0 0; position: relative; z-index: 1; }

/* flash */
.tt .fl { animation: ttflash 1s steps(1) infinite; }
@keyframes ttflash { 50% { color: transparent; } }

/* page-number links */
.tt a.lnk { color: inherit; text-decoration: none; cursor: pointer; display: inline-block; height: 21px; }
.tt a.lnk:active span, .tt a.lnk:focus-visible span { filter: brightness(0.7); }

/* row 23 of a story page: the full story on rte.ie, in a new tab */
.tt a.ext { display: block; height: 21px; color: inherit; text-decoration: none; cursor: pointer; }
.tt a.ext:hover span, .tt a.ext:focus-visible span { filter: brightness(1.25); }
.tt a.ext:active span { filter: brightness(0.7); }

/* header row: the page being keyed in appears in the first eight cells */
.tt .hdr-in { color: var(--tt-white); }
.tt .hdr-in.typing { color: var(--tt-green); }

/* ---- CRT mode (CRT on the remote): cabinet, curved glass, phosphor glow, vignette --------- */
body.crt { --edge: clamp(4px, 1.2vmin, 14px); --bezel: clamp(6px, 1.8vmin, 22px); }
body.crt .tv {
  border-radius: clamp(10px, 2.6vmin, 28px);
  background: linear-gradient(160deg, #3a3a3a 0%, #1b1b1b 45%, #0d0d0d 100%);
  box-shadow: 0 2px 0 #4a4a4a inset, 0 -2px 0 #050505 inset, 0 6px 18px rgba(0,0,0,.6);
}
body.crt .screen {
  border-radius: 4.5% / 6%;
  box-shadow: 0 0 0 2px #050505, 0 0 18px rgba(0,0,0,.9) inset;
}
body.crt .tt { filter: saturate(1.15) brightness(1.06) contrast(1.04); }
body.crt .tt span { text-shadow: 0 0 2.5px currentColor, 0 0 0.6px currentColor; }
body.crt .tt .gx { text-shadow: none; }
body.crt .tt svg.mo { filter: drop-shadow(0 0 1.4px currentColor); }
/* vignette and a faint reflection on the glass */
body.crt .screen .glass {
  position: absolute; inset: 0; pointer-events: none; z-index: 3;
  background:
    radial-gradient(ellipse 120% 95% at 50% 45%, rgba(0,0,0,0) 62%, rgba(0,0,0,.42) 100%),
    linear-gradient(170deg, rgba(255,255,255,.055) 0%, rgba(255,255,255,0) 32%);
}

/* ---- scan lines (separate switch) ---------------------------------------------- */
body.lines .screen .scan {
  position: absolute; inset: 0; pointer-events: none; z-index: 4;
  background-image: linear-gradient(to bottom, rgba(0,0,0,0) 0, rgba(0,0,0,0) 50%, rgba(0,0,0,.36) 50%, rgba(0,0,0,.36) 100%);
  background-size: 100% var(--scan);
}
.screen .glass, .screen .scan { display: none; }
body.crt .screen .glass, body.lines .screen .scan { display: block; }

/* ---- remote control ----------------------------------------------------------
   Hidden until wanted. Phones: tap the screen (not a page number) or the handle, and
   it rises from the bottom; swipe left or right on the screen to turn subpages.
   Laptops: move the pointer to the right edge, click the screen, or press M; the
   keyboard works without it (digits, arrows, Backspace, R G Y C, F for full screen). */
.remote {
  position: fixed; z-index: 20;
  display: grid; gap: 12px;
  width: min(300px, 100vw - 24px);
  padding: 18px 18px calc(20px + env(safe-area-inset-bottom));
  background: linear-gradient(90deg, #1a1a1a, #2b2b2b 18%, #2b2b2b 82%, #161616);
  box-shadow: inset 0 2px 1px rgba(255,255,255,.08), inset 0 -3px 2px rgba(0,0,0,.6), 0 18px 40px rgba(0,0,0,.7);
  opacity: 0; visibility: hidden; pointer-events: none;
  transition: transform .22s ease, opacity .22s ease, visibility 0s linear .22s;
  left: 0; right: 0; bottom: 0; margin: 0 auto;
  border-radius: 34px 34px 0 0;
  transform: translateY(100%);
}
body.remote-open .remote {
  opacity: 1; visibility: visible; pointer-events: auto;
  transform: none;
  transition: transform .22s ease, opacity .22s ease, visibility 0s;
}
/* the remote itself: a 90s black brick with big keys (design C in black) */
.remote .rk {
  appearance: none; border: 0; padding: 0; font: inherit; cursor: pointer; display: grid; place-items: center;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent; transition: transform .05s, filter .05s;
}
.remote .rk:active { transform: translateY(1.5px); }
.rc-top { display: flex; justify-content: space-between; align-items: center; }
.rc-ir { display: block; width: 46px; height: 9px; border-radius: 5px; background: linear-gradient(180deg, #3a0d12, #120406); box-shadow: inset 0 1px 2px #000; }
.remote .rc-pwr { width: 30px; height: 20px; border-radius: 10px; background: radial-gradient(circle at 50% 35%, #ff5a4f, #b8150b); box-shadow: 0 2px 0 #6d0b05, 0 3px 5px rgba(0,0,0,.5); }
.fastext { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; justify-items: center; }
.remote .fx { width: 100%; max-width: 46px; aspect-ratio: 1; border-radius: 50%; box-shadow: 0 3px 0 rgba(0,0,0,.6), inset 0 2px 0 rgba(255,255,255,.35); }
.remote .fx-r { background: linear-gradient(#ff4b3e, #c8160b); } .remote .fx-g { background: linear-gradient(#46e05a, #119a22); }
.remote .fx-y { background: linear-gradient(#ffe54a, #d6b300); } .remote .fx-c { background: linear-gradient(#4fd6ff, #0a98c8); }
.remote .fx:disabled { filter: saturate(.25) brightness(.55); }
.digits { display: grid; grid-template-columns: repeat(3, 1fr); gap: 9px; }
.remote .num { height: 40px; border-radius: 12px; font-weight: 700; font-size: 18px; color: #f2f2f2;
  background: linear-gradient(#5d5d5d, #3e3e3e); box-shadow: 0 3px 0 #1a1a1a, inset 0 1px 0 rgba(255,255,255,.18); }
.remote .num svg { width: 20px; height: 15px; }
.ttkeys { display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px 9px; }
.ttkeys .kw { display: grid; justify-items: center; gap: 3px; }
.ttkeys .kw[hidden] { display: none; }
.ttkeys small { font-size: 9px; letter-spacing: .08em; font-weight: 700; color: #9a9a9a; }
.remote .tk, .remote .pm { width: 100%; height: 28px; border-radius: 10px; color: #e8e8e8;
  background: linear-gradient(#4a4a4a, #303030); box-shadow: 0 3px 0 #141414, inset 0 1px 0 rgba(255,255,255,.14); }
.remote .tk svg { width: 21px; height: 16px; }
.remote .tk[aria-pressed="true"] { color: #0f0; filter: drop-shadow(0 0 3px rgba(0,255,0,.45)); }
.rocker { display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px; }
.remote .pm { font-size: 13px; }
.rocker .pl { font-size: 9px; letter-spacing: .12em; font-weight: 700; color: #9a9a9a; text-align: center; min-width: 6ch; }
.subind { display: block; font-family: "Bedstead", monospace; color: #0f0; font-size: 11px; letter-spacing: 0; font-weight: normal; min-height: 1em; }
.rc-brand { text-align: center; color: #0ff; font-family: "Bedstead", monospace; letter-spacing: .2em; font-size: 13px; margin-top: 2px; }

/* the handle: a small remote in the corner, faint until the pointer or a finger finds it */
.remote-handle {
  position: fixed; z-index: 19;
  right: calc(12px + env(safe-area-inset-right)); bottom: calc(12px + env(safe-area-inset-bottom));
  width: 42px; height: 42px; border-radius: 50%;
  display: grid; place-items: center;
  color: #ccc; background: rgba(20, 20, 20, .55); border: 1px solid rgba(255, 255, 255, .12);
  opacity: .35; cursor: pointer; padding: 0;
  transition: opacity .2s ease;
  -webkit-tap-highlight-color: transparent;
}
.remote-handle:hover, .remote-handle:focus-visible { opacity: 1; }
body.remote-open .remote-handle { opacity: 0; pointer-events: none; }
body.hint .remote-handle { animation: hint 1.2s ease-in-out 3; }
@keyframes hint { 50% { opacity: 1; transform: scale(1.12); } }
/* in full screen the handle goes once the pointer has been still for a while */
body.idle .remote-handle { opacity: 0; }

/* phones held upright: the set sits at the top, so the remote rises into the empty
   space beneath it instead of covering the page */
@media (orientation: portrait) and (max-width: 899px) {
  .stage { align-items: flex-start; padding-top: max(env(safe-area-inset-top), 10px); }
}

/* "add to home screen" hint: a teletext-style box, shown once (see aertel.js) */
.install-hint {
  position: fixed; z-index: 30; left: 50%; bottom: calc(14px + env(safe-area-inset-bottom));
  transform: translateX(-50%); width: min(420px, calc(100vw - 24px));
  background: #000; border: 2px solid #0ff; padding: 12px 14px;
  font-family: "Bedstead", monospace; font-size: 13px; line-height: 1.55; color: #fff;
  box-shadow: 0 10px 30px rgba(0,0,0,.8);
}
.install-hint[hidden] { display: none; }
.install-hint p { margin: 0 0 10px; }
.install-hint b { color: #ff0; font-weight: normal; }
.install-hint .ih-keys { display: flex; gap: 10px; justify-content: flex-end; }
.install-hint button { font: inherit; border: 0; padding: 6px 12px; cursor: pointer; color: #000; background: #0f0; }
.install-hint #install-no { background: #f00; color: #fff; }
body.remote-open .install-hint { display: none; }

/* laptops and desktops: the remote slides in from the right edge */
@media (min-width: 900px) and (hover: hover) {
  .remote {
    left: auto; right: 16px; bottom: auto; top: 50%;
    width: 214px; margin: 0; gap: 11px;
    padding: 16px 16px 18px; border-radius: 26px 26px 34px 34px;
    transform: translate(calc(100% + 24px), -50%);
  }
  body.remote-open .remote { transform: translate(0, -50%); }
  .remote .num { height: 34px; font-size: 16px; }
  .remote .tk, .remote .pm { height: 25px; }
}
/* short laptop screens: a slightly smaller remote so it always fits */
@media (min-width: 900px) and (hover: hover) and (max-height: 700px) {
  .remote { gap: 8px; padding: 12px 14px 14px; }
  .remote .num { height: 29px; }
  .remote .fx { max-width: 36px; }
}
@media (prefers-reduced-motion: reduce) {
  .tt .fl { animation: none; }
  .remote { transition: none; }
  body.hint .remote-handle { animation: none; opacity: .8; }
}

/* ---- phones held upright: a tall screen and a dock of the keys used most --------------
   The 40x25 page is unchanged; each cell is simply drawn taller (as teletext looked on a
   real set), so the page fills the phone down to the dock. Landscape and computers keep
   the 4:3 set and the slide-in remote. config.php 'phone_dock' => false turns this off. */
.dock { display: none; }
@media (orientation: portrait) and (max-width: 760px) {
  body.has-dock { --dock-h: 112px; }
  body.has-dock .stage { align-items: flex-start; padding-bottom: calc(var(--dock-h) + env(safe-area-inset-bottom)); }
  body.has-dock .screen {
    aspect-ratio: auto;
    width: calc(var(--size, 1) * (100vw - var(--room)));
    height: calc(var(--size, 1) * (100vh - var(--room) - var(--dock-h)));
    width: calc(var(--size, 1) * (100dvw - var(--room) - env(safe-area-inset-left) - env(safe-area-inset-right)));
    height: calc(var(--size, 1) * (100dvh - var(--room) - var(--dock-h) - env(safe-area-inset-top) - env(safe-area-inset-bottom)));
    margin-top: var(--edge);
  }
  body.has-dock .screen::before { display: none; }
  body.has-dock .remote-handle { display: none; }
  body.has-dock .dock {
    display: block; position: fixed; left: 0; right: 0; bottom: 0; z-index: 15;
    padding: 9px 12px calc(11px + env(safe-area-inset-bottom));
    background: linear-gradient(#1c1c1c, #0b0b0b); border-top: 1px solid #2a2a2a;
    box-shadow: 0 -6px 18px rgba(0,0,0,.6);
  }
}
.dk-top { display: flex; justify-content: space-between; align-items: center; margin-bottom: 9px; }
.dk-p { font-family: "Bedstead", monospace; color: #fff; font-size: 17px; letter-spacing: .06em; min-width: 5ch; }
.dk-p b { color: #0f0; font-weight: normal; }
.dk-btns { display: flex; gap: 8px; }
.dock .dk {
  height: 38px; min-width: 46px; padding: 0 12px; border-radius: 12px;
  background: linear-gradient(#2e2e2e, #191919); border: 1px solid #353535; color: #ddd;
  font: 700 14px system-ui, sans-serif; box-shadow: 0 2px 0 #000, inset 0 1px 0 rgba(255,255,255,.08);
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.dock .dk:active, .dock .dfx:active { transform: translateY(1.5px); }
.dock .dk:disabled { opacity: .35; }
.dock .dk-sub { font-family: "Bedstead", monospace; font-weight: normal; color: #0ff; min-width: 58px; }
.dock .dk-keys { color: #fff; letter-spacing: .04em; }
.dk-fx { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.dock .dfx {
  height: 44px; border: 0; border-radius: 13px; color: #000; padding: 0 4px;
  font-family: "Bedstead", monospace; font-size: 11px; line-height: 1.1;
  overflow: hidden; box-shadow: 0 3px 0 rgba(0,0,0,.6), inset 0 1px 0 rgba(255,255,255,.35);
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.dock .dfx:disabled { opacity: .3; }
.d-r { background: #f00; } .d-g { background: #0f0; } .d-y { background: #ff0; } .d-c { background: #0ff; }

/* REVEAL: concealed text (quiz answers) is in the page but hidden until revealed */
body:not(.reveal) .tt span.cc { color: transparent !important; }
.dock .dk-reveal { color: #ff0; font-family: "Bedstead", monospace; font-weight: normal; }
body.reveal .dock .dk-reveal, body.reveal .remote [data-act="reveal"] { background: linear-gradient(#5a5a12, #2e2e08); }
/* quiz: options can be tapped; after a pick the right one goes green, a wrong pick red */
.tt .r[data-opt] { cursor: pointer; }
.tt .r.q-right span { background: var(--tt-green) !important; color: var(--tt-black) !important; }
.tt .r.q-wrong span { background: var(--tt-red) !important; color: var(--tt-white) !important; }
/* picture puzzle (530): the board covers rows 5-20, columns 4-35 of the page */
.tt .puz { position: absolute; left: 48px; top: 105px; width: 384px; height: 336px; background: #000; z-index: 2; touch-action: manipulation; }
.tt .puz .pt { position: absolute; width: 25%; height: 25%; box-sizing: border-box; padding: 3px; cursor: pointer;
  transition: left .12s ease-out, top .12s ease-out, padding .3s; }
.tt .puz .pt canvas { display: block; width: 100%; height: 100%; image-rendering: pixelated; image-rendering: crisp-edges; }
.tt .puz .pt.gap { visibility: hidden; }
.tt .puz.done .pt { padding: 0; cursor: default; }
.tt .puz-reset { cursor: pointer; }
/* a row that leads to a page: the whole row can be tapped */
.tt .r.go { cursor: pointer; }
@media (hover: hover) { .tt .r.go:hover span { filter: brightness(1.3); } }
.tt .r.go:active span { filter: brightness(0.75); }
