:root {
  --ink: #0e1024;
  --night: #1b1f3b;
  --dusk: #262b52;
  --grid: #343b72;
  --steel: #7c879c;
  --silver: #b8c2d4;
  --white: #eef2f7;
  --gold: #ffd23f;
  --orange: #e8742c;
  --mint: #57c4b8;
  --teal: #2f8f86;

  --stage: var(--ink); /* around the board, where it doesn't fill the stage */

  --pixel-font: "Silkscreen", monospace;
  --body-font: "VT323", monospace;
  color-scheme: dark;
}

/* The light theme, for e-ink screens: dark ink on paper, with every colour
   deepened so it still reads. Same names, so the rules below follow along;
   the few that put light text on a colour are turned round at the end. */
:root[data-theme="light"] {
  --ink: #14161f;
  --night: #f4f2ec;
  --dusk: #e3dfd3;
  --grid: #c9c3b2;
  --steel: #5d6472;
  --silver: #3b4150;
  --white: #14161f;
  --gold: #7d5200;
  --orange: #b34a12;
  --mint: #0f6b62;
  --teal: #1f6f68;
  --stage: var(--dusk);
  color-scheme: light;
}

* { box-sizing: border-box; }

/* Author display rules would otherwise override the hidden attribute. */
[hidden] { display: none !important; }

body {
  margin: 0;
  background: var(--night);
  color: var(--white);
  font: 22px/1.2 var(--body-font);
}

main {
  max-width: 1320px;
  margin: 0 auto;
  padding: 16px;
}

/* A board page is as wide as a board filling the screen's height (8:5, under
   ~180px of title and status) plus the parts beside it, so big screens get a
   big board instead of a narrow column. It stops at the machine controller's
   MAX_ZOOM (3 x 512px), so a huge monitor isn't one huge board. */
main:has(.machine) {
  max-width: max(1320px, min(calc((100vh - 180px) * 1.6 + 272px), 1536px + 272px));
}

/* A lab board of another size (Lab::SIZES): the same, for its own shape and
   its own width at MAX_ZOOM. */
main:has(.machine--tall) { max-width: max(1320px, min(calc((100vh - 180px) * 512 / 480 + 272px), 1536px + 272px)); }
main:has(.machine--wide) { max-width: max(1320px, min(calc((100vh - 180px) * 768 / 320 + 272px), 2304px + 272px)); }
main:has(.machine--big) { max-width: max(1320px, min(calc((100vh - 180px) * 768 / 480 + 272px), 2304px + 272px)); }

h1, h2, button, summary {
  font-family: var(--pixel-font);
  font-weight: normal;
}

a { color: var(--gold); }

/* A chunky bevelled box, the house style for panels and buttons. */
.puzzle-card, .bin__item, .run-button, .advance__button, .machine__stage {
  border: 2px solid var(--ink);
  box-shadow: inset 2px 2px 0 var(--grid), inset -2px -2px 0 var(--ink);
}

.masthead h1 {
  margin: 24px 0 0;
  color: var(--gold);
  font-size: clamp(32px, 6vw, 64px);
  text-shadow: 4px 4px 0 var(--orange);
}

/* The pixel-art wordmark, three board pixels to a pixel where there's room. */
.masthead__logo img {
  display: block;
  max-width: 100%;
  height: auto;
  image-rendering: pixelated;
}

.masthead p { color: var(--silver); margin: 4px 0 24px; }

.chapter { margin-bottom: 32px; }

.chapter__heading { margin: 0 0 8px; font-size: 18px; color: var(--mint); }
.chapter__lock { margin: 0 0 12px; color: var(--silver); }

.chapter__unlock {
  margin: -4px 0 12px;
  color: var(--steel);

  summary { font-size: 12px; cursor: pointer; }
  form { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 6px; }
  input, button { font: inherit; font-size: 18px; }
}

.puzzles {
  list-style: none;
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(260px, 1fr));
  gap: 16px;
}

.puzzle-card {
  display: grid;
  gap: 6px;
  height: 100%;
  padding: 16px;
  background: var(--dusk);
  color: var(--white);
  text-decoration: none;

  &:hover { background: var(--grid); }
  strong { font-family: var(--pixel-font); font-weight: normal; color: var(--gold); }
  span { color: var(--silver); }
}

.puzzle-card--solved { background: color-mix(in oklab, var(--dusk), var(--teal) 25%); }

/* Not open yet: greyed, and not a link. */
.puzzle-card--locked {
  opacity: 0.45;

  &:hover { background: var(--dusk); }
  strong { color: var(--silver); }
}

.puzzle-card .puzzle-card__solved {
  float: right;
  color: var(--gold);
  font-size: 12px;
}

/* A chapter's much harder ninth puzzle. */
.boss-badge, .puzzle-card .boss-badge {
  font-family: var(--pixel-font);
  font-size: 14px;
  color: var(--orange);
  vertical-align: middle;
  white-space: nowrap;
}

.puzzle-card__number {
  font-family: var(--pixel-font);
  color: var(--mint) !important;
}

.puzzle-header {
  position: relative;
  margin-bottom: 12px;

  /* Level with the back link, its right edge on the play area's (the side
     panel, 220px, and the gap, 16px, beyond it: see .machine), well away
     from Start. */
  .header-clear { position: absolute; top: 0; right: calc(220px + 16px); }

  h1 { margin: 4px 0; font-size: 28px; color: var(--gold); }
  p { margin: 0; color: var(--silver); }
  .conditions { margin-top: 4px; font-family: var(--pixel-font); font-size: 14px; color: var(--orange); }
  .hint { margin-top: 4px; color: var(--white); }
  .hint--spent { color: var(--steel); }
  .hint-button { margin-top: 6px; }
  .back { font-family: var(--pixel-font); font-size: 14px; text-decoration: none; }
}

.machine {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 220px;
  gap: 8px 16px;
  align-items: start;
}

/* The status line sits under the board, the side panel beside both. */
.machine__stage { grid-column: 1; grid-row: 1; }
.machine > .status { grid-column: 1; grid-row: 2; }
.machine__side { grid-column: 2; grid-row: 1 / span 2; }

.machine__stage {
  position: relative;
  display: grid;
  place-items: center;
  background: var(--stage);
  min-width: 0;
}

/* Over the play area while a machine is checked before publishing: greyed,
   so the run shows through, with how it's going in a panel at the top. */
.checking {
  position: absolute;
  inset: 0;
  z-index: 2;
  display: grid;
  justify-content: center;
  align-content: start;
  padding-top: 12px;
  background: rgb(14 16 36 / 0.35);
  color: var(--white);
  font-family: var(--pixel-font);
  font-size: 16px;

  &[hidden] { display: none; }
  p { margin: 0; }
  progress { width: 160px; accent-color: var(--gold); }
}

.checking__panel {
  display: grid;
  grid-template-columns: auto auto;
  align-items: center;
  gap: 4px 10px;
  padding: 6px 12px;
  background: var(--night);
  border: 2px solid var(--gold);

  progress { grid-column: 1 / -1; }
}

.checking__gear {
  font-size: 28px;
  line-height: 1;
  color: var(--gold);
  animation: checking-turn 1.6s linear infinite;
}

@keyframes checking-turn { to { transform: rotate(1turn); } }
@media (prefers-reduced-motion: reduce) { .checking__gear { animation: none; } }

.timer-settings {
  position: absolute;
  z-index: 1;
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 4px 8px;
  max-width: calc(100% - 8px);
  padding: 4px 8px;
  background: var(--night);
  border: 2px solid var(--gold);
  color: var(--white);
  font-size: 18px;

  &[hidden] { display: none; }
}

.timer-settings__time { display: flex; align-items: center; gap: 6px; }
.timer-settings__label { font-family: var(--pixel-font); font-size: 12px; color: var(--silver); }
.timer-settings input { width: 110px; accent-color: var(--gold); }
.timer-settings output { min-width: 3.5ch; text-align: right; }

.timer-settings__toggle {
  padding: 2px 6px;
  background: none;
  border: 1px solid var(--steel);
  color: var(--silver);
  font-size: 12px;
  cursor: pointer;

  &:hover { border-color: var(--silver); color: var(--white); }
}

.machine__canvas {
  display: block;
  image-rendering: pixelated;
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none; /* no iOS press-and-hold menu over the board */
}

.machine--running .machine__canvas { cursor: default; }

.machine__side {
  display: grid;
  gap: 12px;

  h2 { margin: 0; font-size: 16px; color: var(--mint); }
}

.bin { display: grid; gap: 8px; }

/* A big bin (the lab's) scrolls on its own, so the page stays one screen tall:
   beside the board, the panel is as tall as the board and the bin fills what's
   left of it (contain: size keeps the panel from stretching the row). */
.bin--grouped {
  align-content: start;
  max-height: 60vh;
  overflow-y: auto;
  padding-right: 4px;
}

@media (min-width: 901px) {
  .machine__side:has(.bin--grouped) {
    contain: size;
    align-self: stretch;
    display: flex;
    flex-direction: column;

    .bin-filter { flex: 1 1 0; min-height: 160px; display: flex; flex-direction: column; }
    .bin--grouped { flex: 1 1 0; min-height: 0; max-height: none; }
    .sound-buttons { align-self: start; }
  }
}

.bin-filter { display: grid; gap: 6px; }
.bin-filter__select { font: inherit; font-size: 18px; }
.bin__group { display: grid; gap: 8px; align-content: start; }

/* One category at a time needs no heading; "All parts" does. */
.bin-filter:not(.bin-filter--all) .bin__heading { display: none; }

.bin__heading {
  margin: 8px 0 0;
  font-family: var(--pixel-font);
  font-size: 12px;
  color: var(--mint);

  &:first-child { margin-top: 0; }
}

.bin__item {
  display: grid;
  grid-template-columns: 48px 1fr auto;
  align-items: center;
  gap: 8px;
  padding: 6px 8px;
  background: var(--dusk);
  color: var(--white);
  font: 20px var(--body-font);
  text-align: left;
  cursor: grab;
  user-select: none;
  /* Sideways drags a part out to the board; up and down scrolls a long bin (the lab's). */
  touch-action: pan-y;

  img {
    width: 48px;
    height: 32px;
    object-fit: contain;
    image-rendering: pixelated;
    pointer-events: none;
  }

  &:hover:not(:disabled) { background: var(--grid); }
  &:disabled { opacity: 0.4; cursor: not-allowed; }
}

/* Tapped: picked up, waiting for a tap on the board. */
.bin__item--armed {
  background: var(--grid);
  outline: 2px solid var(--gold);
  outline-offset: -2px;
}

.bin__count { color: var(--gold); }

.run-button {
  padding: 12px;
  background: var(--teal);
  color: var(--white);
  font-size: 18px;
  cursor: pointer;

  &:hover { background: var(--mint); color: var(--ink); }
}

.machine--running .run-button { background: var(--orange); }

.sound-buttons { display: flex; flex-wrap: wrap; gap: 6px; justify-self: start; }

.lab-goal {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;

  .lab-goal__label { flex-basis: 100%; margin: 0; font-size: 16px; color: var(--silver); }
}

.clear-button, .fullscreen-button, .sound-button, .hint-button {
  justify-self: start;
  padding: 4px 8px;
  background: none;
  border: 1px solid var(--steel);
  color: var(--silver);
  font-size: 12px;
  cursor: pointer;

  &:hover:not(:disabled) { border-color: var(--silver); color: var(--white); }
  &:disabled { opacity: 0.4; cursor: default; }
}

.status {
  margin: 0;
  min-height: 1lh;
  color: var(--silver);

  &[data-tone="solved"] { color: var(--gold); }
}

/* Pops up over the middle of the board when it's solved, then fades so the
   machine can be watched (the status line keeps saying so). */
.solved-banner {
  position: absolute;
  top: 50%;
  left: 50%;
  translate: -50% -50%;
  margin: 0;
  padding: 16px 28px;
  background: var(--ink);
  border: 2px solid var(--gold);
  box-shadow: 4px 4px 0 var(--orange);
  color: var(--gold);
  font: 28px var(--pixel-font);
  white-space: nowrap;
  pointer-events: none;
  animation: solved-in 0.3s steps(3), solved-out 0.6s steps(4) 2.4s forwards;
}

@keyframes solved-in {
  from { scale: 0.4; opacity: 0; }
}

@keyframes solved-out {
  to { opacity: 0; visibility: hidden; }
}

.advance {
  display: grid;
  gap: 8px;

  p { margin: 0; color: var(--gold); }
}

.advance__button {
  display: block;
  padding: 12px;
  background: var(--gold);
  color: var(--ink);
  font: 16px var(--pixel-font);
  text-align: center;
  text-decoration: none;
  animation: advance-in 0.4s steps(4);

  &:hover { background: var(--white); }
}

@keyframes advance-in {
  from { transform: translateY(-8px); opacity: 0; }
}

.dev {
  color: var(--steel);

  summary { font-size: 12px; cursor: pointer; }
  button { margin-top: 8px; font-size: 12px; cursor: pointer; }
  p { margin: 6px 0 0; font-size: 18px; word-break: break-word; }
}

/* The lab (development only). */
.lab-new, .lab-settings form {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 16px;

  input, textarea, select, button { font: inherit; font-size: 18px; }
  input[type=text], textarea { flex: 1 1 200px; }
}

.lab-settings {
  color: var(--silver);

  summary { font-size: 12px; cursor: pointer; }
  /* Stacked, and as wide as the side column: no taller or wider than the parts bin. */
  form {
    flex-direction: column;
    margin: 8px 0;

    input[type=text], textarea { flex: none; }
  }
  input, textarea, select, button { width: 100%; min-width: 0; box-sizing: border-box; }
  p { margin: 0; font-size: 16px; color: var(--steel); }
}

/* Carrying a player over to another device. */
.device {
  max-width: 640px;

  p { color: var(--silver); }
  .device__phrase { font-family: var(--pixel-font); font-size: 24px; color: var(--gold); }
}

.lab-heading { font-family: var(--pixel-font); font-size: 16px; color: var(--mint); margin: 24px 0 12px; }

.lab-delete { font: inherit; font-size: 16px; color: var(--orange); background: none; border: 1px solid; cursor: pointer; }

.lab-copy {
  display: grid;
  gap: 6px;

  input { font: inherit; font-size: 18px; width: 100%; min-width: 0; box-sizing: border-box; }
}

.lab-tag { font-family: var(--pixel-font); font-size: 12px; color: var(--orange); }
.lab-author { font-size: 16px; color: var(--mint); }
.lab-empty { color: var(--silver); }

.lab-share {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;

  p { margin: 0; font-size: 16px; color: var(--silver); }
  form { margin: 0; }
}

.lab-publish {
  display: grid;
  gap: 6px;

  input { font: inherit; font-size: 18px; width: 100%; min-width: 0; box-sizing: border-box; }
}
.lab-publish__panel { display: grid; gap: 6px; }
.lab-publish__panel p { margin: 0; font-size: 16px; color: var(--silver); }

.flash { margin: 8px 0 0; font-size: 18px; color: var(--mint); }
.flash--alert { color: var(--orange); }

@media (max-width: 900px) {
  .machine { grid-template-columns: 1fr; }
  .puzzle-header .header-clear { right: 0; } /* the play area's full width now */
  .machine__stage, .machine > .status, .machine__side { grid-column: 1; grid-row: auto; }
}

/* Phones held sideways: the page is exactly one screen tall, the board takes
   all the space left of a narrow, scrolling parts panel.
   Keep this query in sync with COMPACT in machine_controller.js. */
@media (orientation: landscape) and (max-height: 500px) {
  main {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    padding: 6px 10px;
  }

  .puzzle-header {
    display: flex;
    align-items: baseline;
    gap: 12px;
    margin-bottom: 6px;

    h1 { margin: 0; font-size: 16px; }
    p, #hint { display: none; }
    .conditions { display: block; margin: 0; }
    /* Pushed right in the one-row header to the play area's edge (the side
       panel, 168px, and the gap, 8px, beyond it), leaving the fullscreen
       button on its own over the panel. */
    .header-clear { position: static; align-self: center; margin: 0 calc(168px + 8px) 0 auto; padding: 2px 8px; }
  }

  .fullscreen-button:not([hidden]) {
    display: block;
    position: absolute;
    right: 0;
    top: 50%;
    transform: translateY(-50%);
    padding: 2px 8px;
  }

  .machine {
    flex: 1;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr) 168px;
    grid-template-rows: minmax(0, 1fr) auto;
    gap: 4px 8px;
    align-items: stretch;
  }

  .machine__stage { min-height: 0; overflow: hidden; grid-column: 1; grid-row: 1; }
  .machine > .status { grid-column: 1; grid-row: 2; }
  .machine__side { grid-column: 2; grid-row: 1 / span 2; }

  /* A flex column, since grid rows in a fixed-height scroller get squeezed
     below their content and overlap. */
  .machine__side {
    display: flex;
    flex-direction: column;
    gap: 6px;
    overflow-y: auto;

    > * { flex: none; }
    h2 { font-size: 12px; }
  }

  .bin { gap: 4px; }

  /* The whole side panel scrolls, bin and all: a bin scrolling on its own,
     inside it, mostly sat below the fold. */
  .bin--grouped { max-height: none; overflow: visible; }

  .bin__item {
    grid-template-columns: 36px 1fr auto;
    padding: 4px 6px;
    font-size: 16px;

    img { width: 36px; height: 24px; }
  }

  .run-button { padding: 8px; font-size: 14px; }
  .advance__button { padding: 8px; font-size: 12px; }
  .solved-banner { padding: 10px 18px; font-size: 20px; }
  .status { min-height: 0; font-size: 15px; line-height: 1.1; }
}

/* Only phones held sideways need it. */
.fullscreen-button { display: none; }

/* A web page can't turn the phone for you, so ask. */
.rotate-hint { display: none; }

@media (orientation: portrait) and (max-width: 600px) {
  .rotate-hint {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 16px;
    padding: 24px;
    background: var(--night);
    text-align: center;

    svg { width: 96px; animation: rotate-hint 2.4s steps(6) infinite; }
    p { margin: 0; color: var(--gold); font: 16px/1.5 var(--pixel-font); }
    a { font: 12px var(--pixel-font); }
  }
}

@keyframes rotate-hint {
  0%, 30% { transform: rotate(0deg); }
  60%, 100% { transform: rotate(-90deg); }
}

/* Light text on a colour, turned round for the light theme. */
:root[data-theme="light"] {
  .run-button, .run-button:hover, .advance__button { color: var(--night); }
  .advance__button:hover { background: var(--orange); }
  .solved-banner { background: var(--night); }
}
