:root {
  --ink: #ece6d6;
  --dim: #a59d89;
  --panel: rgba(18, 17, 14, 0.78);
  --line: rgba(236, 230, 214, 0.14);
  --amber: #ffbe5c;
  --red: #ff5a45;
  --green: #5ff08a;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", Menlo, Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: dark;
}

* { box-sizing: border-box; }
/* `hidden` always wins, also over the display rules of elements that use it (rows, lunch fields). */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #14130f; color: var(--ink); font: 15px/1.4 var(--sans); }
#scene { position: fixed; inset: 0; width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#scene:active { cursor: grabbing; }

/* The TV's player, in a CSS 3D layer over the canvas (js/tv.js). The layer lets clicks through to the
   canvas everywhere except the player itself, which takes them while it shows. */
#tv-layer, #tv-layer .tv-css { position: fixed; inset: 0; pointer-events: none; overflow: hidden; }
.tv-player { background: #000; backface-visibility: hidden; }
.tv-player iframe { display: block; border: 0; }

#hint {
  position: fixed; left: 50%; bottom: 72px; transform: translateX(-50%); margin: 0;
  padding: 6px 12px; border-radius: 999px; background: var(--panel); color: var(--dim); font-size: 13px;
  pointer-events: none; transition: opacity 0.6s; white-space: nowrap;
}
#hint.gone { opacity: 0; }

/* --- Dock and radio --- */
#dock { position: fixed; left: 16px; bottom: max(16px, env(safe-area-inset-bottom)); display: flex; gap: 8px; }
button {
  font: 500 14px var(--sans); color: var(--ink); background: var(--panel); border: 1px solid var(--line);
  border-radius: 8px; padding: 8px 14px; cursor: pointer;
}
button:hover { border-color: rgba(236, 230, 214, 0.35); }
button:focus-visible, input:focus-visible { outline: 2px solid var(--amber); outline-offset: 2px; }
#radio-btn[aria-pressed="true"] { color: var(--amber); border-color: rgba(255, 190, 92, 0.5); }

#radio-panel {
  position: fixed; right: 16px; bottom: max(16px, env(safe-area-inset-bottom)); width: 290px;
  padding: 10px 14px; background: var(--panel); border: 1px solid var(--line); border-radius: 10px;
  display: grid; gap: 6px; font-size: 13px; transition: opacity 0.3s, transform 0.3s;
}
#radio-panel[data-state="off"] { opacity: 0; transform: translateY(8px); pointer-events: none; }
#radio-panel .row { display: flex; align-items: center; gap: 8px; color: var(--dim); }
#radio-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--red); }
#radio-panel[data-state="on"] #radio-dot { background: var(--green); box-shadow: 0 0 8px var(--green); }
#radio-panel[data-state="tuning"] #radio-dot { background: var(--amber); animation: blink 0.6s infinite alternate; }
/* The station you are on is the headline; the arrows either side tune to the one before or after. */
#station-row { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: 8px; }
#station-now { min-width: 0; }
#station-name, #station-track { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#station-name { color: var(--amber); font-size: 15px; font-weight: 600; }
#station-track { color: var(--dim); font-size: 12px; }
.tune-btn { width: 28px; height: 28px; padding: 0 0 2px; border-radius: 50%; border: 1px solid var(--line); background: none; color: var(--ink); font-size: 18px; line-height: 1; cursor: pointer; }
.tune-btn:hover { border-color: rgba(255, 190, 92, 0.5); color: var(--amber); }
#radio-volume { flex: 1; accent-color: var(--amber); }
button.link { background: none; border: 0; padding: 0; color: var(--amber); font-size: 13px; text-align: left; justify-self: start; }
button.link:hover { text-decoration: underline; }
@keyframes blink { to { opacity: 0.3; } }

/* V: the minimal view. Only the cell is left; toasts still show, briefly. */
body.minimal #dock, body.minimal #radio-panel, body.minimal #hint, body.minimal #tip { display: none; }

/* --- Messages --- */
#tip { position: fixed; left: 0; top: 0; padding: 4px 9px; border-radius: 6px; background: var(--panel); font-size: 13px; pointer-events: none; white-space: nowrap; z-index: 5; }
#toast {
  position: fixed; left: 50%; top: max(20px, env(safe-area-inset-top)); transform: translate(-50%, -6px); max-width: calc(100vw - 32px);
  padding: 9px 16px; border-radius: 8px; background: var(--panel); border: 1px solid var(--line);
  font-size: 14px; text-align: center; opacity: 0; transition: opacity 0.3s, transform 0.3s; pointer-events: none;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); }
/* A message with a button (go for lunch, end it early) takes clicks; plain ones let them through. */
#toast.has-action.show { pointer-events: auto; }
#toast button.link { font-size: 14px; margin-left: 4px; }

/* --- Moving between the cell and the yard ---
   The screen goes black while you are moved, so the cut is a fade rather than a jump. It is on top
   of everything (the dock, the panel, a toast), and lets clicks through only when it is clear. */
#fade {
  position: fixed; inset: 0; background: #000; opacity: 0; z-index: 20; pointer-events: none;
  transition: opacity 900ms ease;
}
#fade.dark { opacity: 1; pointer-events: auto; }
@media (prefers-reduced-motion: reduce) { #fade { transition-duration: 1ms; } }

/* --- Settings --- */
dialog {
  width: min(400px, calc(100vw - 32px)); padding: 22px; color: var(--ink);
  background: #1d1b16; border: 1px solid var(--line); border-radius: 12px;
}
dialog::backdrop { background: rgba(0, 0, 0, 0.55); }
dialog h2 { margin: 0 0 8px; font: 600 18px var(--mono); }
dialog p { margin: 0 0 14px; color: var(--dim); font-size: 14px; }
dialog p.note { font-size: 12px; margin: 10px 0 0; }
/* Terms, privacy and contact: in the schedule dialog and the Commissary, one click from the dock. */
dialog p.legal a { color: inherit; }
/* "Write to the warden" sits in the same row but opens the mail dialog rather than a page, so it is
   a button wearing a link's clothes: same size and colour as its neighbours, no button chrome. */
dialog p.legal .linkish {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  padding: 0;
  margin: 0;
  min-height: 0;
  text-decoration: underline;
  cursor: pointer;
}
dialog p.legal .linkish:hover { color: var(--ink); }
.times { display: flex; gap: 12px; }
.times label { flex: 1; display: grid; gap: 4px; font-size: 13px; color: var(--dim); }
input[type="time"] { font: 16px var(--mono); color: var(--ink); background: #12110d; border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
input[type="time"]:disabled { opacity: 0.4; }
fieldset { border: 0; padding: 0; margin: 14px 0; display: flex; flex-wrap: wrap; gap: 6px; }
legend { font-size: 13px; color: var(--dim); margin-bottom: 6px; padding: 0; }
.day input { position: absolute; opacity: 0; }
.day span { display: inline-block; padding: 6px 9px; border: 1px solid var(--line); border-radius: 6px; font-size: 13px; cursor: pointer; user-select: none; }
.day input:checked + span { background: var(--amber); color: #1d1b16; border-color: var(--amber); }
.day input:focus-visible + span { outline: 2px solid var(--amber); outline-offset: 2px; }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; font-size: 14px; }
/* Lunch: its kind and length on one line; the times only for a fixed lunch. */
.lunch-head { display: flex; flex-wrap: wrap; align-items: end; gap: 12px; margin-bottom: 8px; }
.lunch-head label { display: grid; gap: 4px; font-size: 13px; color: var(--dim); }
.lunch-head select, .lunch-head input { font: 14px var(--sans); color: var(--ink); background: #12110d; border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
#lunch-len { grid-auto-flow: column; align-items: center; }
#lunch-len input { width: 4.5em; }
#lunch-now { margin-top: 4px; }
/* A working day's own hours: its name, then start and end. */
#day-hours { display: grid; gap: 6px; margin: 0 0 14px; }
.day-row { display: grid; grid-template-columns: 3em 1fr 1fr; align-items: center; gap: 8px; font-size: 13px; color: var(--dim); }
.day-row input { min-width: 0; }
.check input { accent-color: var(--amber); width: 16px; height: 16px; }
.actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
button.primary { background: var(--amber); color: #1d1b16; border-color: var(--amber); font-weight: 600; }

@media (max-width: 600px) {
  #radio-panel { left: 16px; right: 16px; width: auto; bottom: calc(max(16px, env(safe-area-inset-bottom)) + 52px); }
  #hint { bottom: calc(max(16px, env(safe-area-inset-bottom)) + 56px); }
}

/* --- The commissary ---
   A carbon-copy order form on a clipboard: paper, typewriter ink, a stamp for what you own. */
#commissary {
  width: min(460px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow: auto; padding: 20px 22px;
  background: #ebe4d0; color: #2a2620; border: 0; border-radius: 4px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  font-family: var(--mono); color-scheme: light;
}
#commissary header { border-bottom: 2px solid #2a2620; padding-bottom: 8px; margin-bottom: 12px; }
#commissary h2 { font: 700 22px var(--mono); letter-spacing: 0.12em; text-transform: uppercase; margin: 0; color: #2a2620; }
.cm-sub { font-size: 12px; color: #6b6355; }
#commissary p { color: #4d4639; font-size: 12px; margin: 0 0 10px; }
#cm-closed, #cm-trial { background: #d8cfb6; padding: 6px 9px; border-left: 3px solid #a3412f; }
.cm-item {
  display: grid; grid-template-columns: 22px 1fr auto; gap: 2px 8px; align-items: baseline;
  padding: 9px 0; border-bottom: 1px dashed #b7ad94; cursor: pointer;
}
.cm-item input { accent-color: #2a2620; width: 15px; height: 15px; margin: 0; align-self: center; }
.cm-name { font-weight: 700; font-size: 14px; }
.cm-price { font-size: 14px; }
.cm-blurb { grid-column: 2 / 4; font: 12px/1.35 var(--sans); color: #5b5446; }
.cm-item.owned { cursor: pointer; }
.cm-item.owned .cm-price {
  color: #a3412f; border: 2px solid #a3412f; border-radius: 3px; padding: 0 5px; font-weight: 700;
  font-size: 11px; letter-spacing: 0.1em; text-transform: uppercase; transform: rotate(-6deg);
}
.cm-item.owned.away .cm-price { color: #8a8170; border-color: #8a8170; }
.cm-item.owned.away .cm-name { color: #6b6355; }
.cm-total { display: flex; justify-content: space-between; font-size: 15px; padding: 10px 0 6px; }
#commissary .cm-note { margin-top: 4px; }
#commissary .actions { margin-top: 10px; }
#commissary button { background: #f6f1e3; color: #2a2620; border-color: #9c927a; }
#commissary button.primary { background: #2a2620; color: #ebe4d0; border-color: #2a2620; }
#commissary button:disabled { opacity: 0.45; cursor: default; }
#cm-key-box { margin-top: 16px; padding-top: 12px; border-top: 2px solid #2a2620; display: grid; gap: 6px; }
.cm-label { font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: #6b6355; }
.cm-keyrow { display: flex; gap: 8px; align-items: center; }
#cm-key { flex: 1; font: 700 15px var(--mono); letter-spacing: 0.04em; background: #f6f1e3; padding: 6px 8px; border-radius: 3px; user-select: all; }
#commissary input[type="text"], #commissary input:not([type]) {
  flex: 1; min-width: 0; font: 13px var(--mono); color: #2a2620; background: #f6f1e3; border: 1px solid #9c927a; border-radius: 4px; padding: 6px 8px;
}
#cm-redeem { margin-top: 14px; }

/* --- Mail: a sheet of ruled writing paper, the same paper as the commissary's order form --- */
#mail {
  width: min(460px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); overflow: auto; padding: 20px 22px;
  background: #eee7d3; color: #2a2620; border: 0; border-radius: 4px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  font-family: var(--mono); color-scheme: light;
}
#mail header { border-bottom: 2px solid #4b5a63; padding-bottom: 8px; margin-bottom: 12px; }
#mail h2 { font: 700 18px var(--mono); letter-spacing: 0.12em; text-transform: uppercase; margin: 0; color: #4b5a63; }
.mail-sub { font-size: 12px; color: #6b6355; }
.mail-label { display: block; font-size: 11px; text-transform: uppercase; letter-spacing: 0.08em; color: #6b6355; margin: 10px 0 4px; }
/* Patrick Hand (fonts/, OFL), print-style handwriting the same on every device: a joined script is hard
   to read for many (Android's default `cursive` is one), and system handwriting fonts differ by OS.
   Only the Latin letters are kept (33 KB). The pad on the desk (js/mail.js) asks for it as the cell is
   built, after the first paint, so it is there before the letter is opened. */
@font-face { font-family: "Patrick Hand"; src: url("../fonts/patrick-hand.woff2") format("woff2"); font-display: swap; }
/* Ruled like the pad on the desk: blue lines 26 px apart, a red margin, pencil-grey writing on them. */
#mail-text {
  display: block; box-sizing: border-box; width: 100%; resize: vertical; border: 0; border-radius: 0; outline: none;
  padding: 0 8px 0 40px; font: 18px/26px "Patrick Hand", system-ui, sans-serif; color: #3a3a40;
  background: linear-gradient(90deg, transparent 31px, rgba(180, 60, 50, 0.5) 31px 33px, transparent 33px),
    repeating-linear-gradient(transparent 0 25px, rgba(80, 110, 150, 0.45) 25px 26px), #f4eedd;
  background-attachment: local;
}
#mail-text:focus-visible { box-shadow: 0 0 0 2px #4b5a63; }
#mail-email { box-sizing: border-box; width: 100%; font: 14px var(--mono); color: #2a2620; background: #f6f1e3; border: 1px solid #9c927a; border-radius: 4px; padding: 6px 8px; }
#mail .mail-note { color: #6b6355; font-size: 11px; margin: 10px 0 0; }
#mail .mail-note a { color: inherit; }
#mail .actions { margin-top: 12px; }
#mail button { background: #f6f1e3; color: #2a2620; border-color: #9c927a; }
#mail button.primary { background: #4b5a63; color: #eee7d3; border-color: #4b5a63; }
#mail button:disabled { opacity: 0.45; cursor: default; }

/* --- The TV's channels --- */
#tv-channels { display: grid; gap: 4px; margin: 4px 0 14px; }
.tv-ch { display: flex; align-items: center; gap: 8px; min-width: 0; padding: 7px 10px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-size: 14px; }
.tv-ch[aria-current="true"] { border-color: var(--amber); color: var(--amber); }
.tv-ch .name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tv-ch button { padding: 2px 8px; font-size: 12px; }
.tv-ch a { color: var(--dim); font-size: 12px; }
.tv-add { display: grid; gap: 4px; font-size: 13px; color: var(--dim); }
.tv-add input { font: 14px var(--sans); color: var(--ink); background: #12110d; border: 1px solid var(--line); border-radius: 6px; padding: 7px 8px; }
#commissary button.cm-preview { background: none; border: 0; padding: 0; color: #a3412f; font: 600 12px var(--sans); text-decoration: underline; cursor: pointer; }
#rain-btn[aria-pressed="true"] { color: #9cc7ff; border-color: rgba(156, 199, 255, 0.5); }
#photo-btn { padding: 6px 9px; line-height: 0; }
/* The shutter: a white flash over everything, gone in a moment. */
#fade.flash { background: #fff; opacity: 0.7; transition: none; }
/* On a phone the dock stays one row (the radio panel sits just above it), so its buttons get tighter. */
@media (max-width: 600px) {
  #dock { right: 16px; gap: 6px; }
  #dock button { padding: 7px 9px; font-size: 13px; white-space: nowrap; }
}
/* The narrowest phones: the commissary's button says Shop and the mail button becomes an envelope,
   so the camera still fits on the row. Seven buttons do not fit as words on any phone. */
#shop-btn .short, #mail-btn .short { display: none; }
@media (max-width: 600px) {
  #mail-btn .long { display: none; }
  #mail-btn .short { display: inline; }
  #mail-btn { padding: 6px 9px; line-height: 0; }
}
/* Below about 370px the seven buttons only fit if the row itself tightens. */
@media (max-width: 380px) {
  #dock { gap: 4px; }
  #dock button { padding: 7px 6px; }
  #photo-btn, #mail-btn { padding: 6px 6px; }
}
@media (max-width: 420px) {
  #shop-btn .long { display: none; }
  #shop-btn .short { display: inline; }
  #hint { white-space: normal; text-align: center; width: max-content; max-width: calc(100vw - 32px); border-radius: 12px; }
}

/* 2D: the radio and the LED countdown on their own (js/flat.js), the cell switched off. */
#flat {
  position: fixed; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: clamp(24px, 7vh, 64px); padding: 16px 16px 96px;
  background: radial-gradient(ellipse at 50% 40%, #2c2f31, #121415 75%);
}
#flat[hidden] { display: none; }
#flat-led-box { padding: 10px; border-radius: 6px; background: linear-gradient(#3a3d40, #232527); box-shadow: 0 6px 18px rgba(0, 0, 0, 0.5); }
#flat-led { display: block; width: min(76vw, 420px, 52vh); height: auto; border-radius: 2px; }
#flat-radio { display: block; width: min(96vw, 600px, 64vh); height: auto; filter: drop-shadow(0 10px 18px rgba(0, 0, 0, 0.55)); }
body.flat #scene, body.flat #tv-layer, body.flat #hint, body.flat #photo-btn { display: none; }
