/*
 * Design rules (from skole-materiell/PRINSIPPER.md, binding):
 * - White background, air instead of frames. The material carries the colour,
 *   the page does not: the only colours are the Montessori place values and,
 *   on the bead-chain page, the bead colour of the chain.
 * - Place-value colours are the same hex values as the printed worksheets
 *   (skole-materiell/matte/bygg.mjs), so screen and paper match.
 * - Fonts are local stacks only: no font CDN, so the page makes no third-party
 *   requests at all. Palatino matches the numerals on the printed sheets.
 */

:root {
  --ink: #23211c;
  --muted: #6f6b62;
  --line: #d9d5cc;
  --paper: #ffffff;
  --wash: #f4f2ee;

  --green: #2e7d4f;
  --blue: #2b5fa8;
  --red: #b23b3b;

  --serif: Palatino, "Palatino Linotype", "Book Antiqua", Georgia, serif;
  --sans: "Avenir Next", Avenir, "Segoe UI", "Helvetica Neue", Arial, sans-serif;

  --focus: 3px solid var(--ink);
  --gap: clamp(1rem, 3vw, 2rem);
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--sans);
  font-size: 1.0625rem;
  line-height: 1.55;
  font-variant-numeric: tabular-nums;
  hyphens: auto;
  -webkit-hyphens: auto;
}

.page {
  max-width: 64rem;
  margin: 0 auto;
  padding: clamp(1.25rem, 4vw, 3rem) clamp(1rem, 4vw, 2.5rem) 4rem;
}

h1, h2, h3 {
  font-family: var(--serif);
  font-weight: 400;
  line-height: 1.15;
  margin: 0 0 0.5em;
}

h1 { font-size: clamp(2rem, 5.5vw, 3.4rem); letter-spacing: -0.01em; }
h2 { font-size: clamp(1.35rem, 3vw, 1.75rem); margin-top: 2.5rem; }
h3 { font-size: 1.2rem; }

p { margin: 0 0 1em; max-width: 38rem; }

.lead { font-size: 1.2rem; max-width: 36rem; }
.quiet { color: var(--muted); }
.small { font-size: 0.9rem; }

a { color: var(--ink); text-underline-offset: 0.18em; }

:focus-visible { outline: var(--focus); outline-offset: 2px; }

.back { display: inline-block; margin-bottom: 1.5rem; color: var(--muted); font-size: 0.95rem; }

/* ------------------------------------------------------------------ Buttons */

button { font: inherit; color: inherit; }

.btn {
  display: inline-flex; align-items: center; gap: 0.4em;
  min-height: 2.75rem;
  padding: 0.5rem 1.1rem;
  border: 1px solid var(--ink);
  border-radius: 0.3rem;
  background: var(--paper);
  cursor: pointer;
  transition: background-color 120ms ease-out;
}
.btn:hover { background: var(--wash); }
.btn-primary { background: var(--ink); color: var(--paper); }
.btn-primary:hover { background: #3a3730; }
.btn-text { border-color: transparent; text-decoration: underline; text-underline-offset: 0.18em; padding-inline: 0.4rem; }
.btn-text:hover { background: transparent; color: var(--muted); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
[hidden] { display: none !important; }

.actions { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: center; margin: 1.25rem 0; }

/* ---------------------------------------------------------- Task chooser */

.tasks { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: center; margin: 0 0 0.5rem; }

.own-task { display: flex; flex-wrap: wrap; gap: 0.5rem; align-items: end; margin: 0.75rem 0 0; }
.own-task label, .answer label { display: flex; flex-direction: column; font-size: 0.9rem; color: var(--muted); }
.own-task input, .answer input {
  font: inherit; font-size: 1.2rem; color: var(--ink);
  width: 6.5rem; min-height: 2.75rem; padding: 0.3rem 0.6rem;
  border: 1px solid var(--muted); border-radius: 0.3rem;
}
.own-task .times { font-size: 1.4rem; padding-bottom: 0.4rem; }
.error { color: var(--red); font-weight: 600; }

.task { font-family: var(--serif); font-size: clamp(1.8rem, 5vw, 2.6rem); margin: 1.5rem 0 0; }

/* ------------------------------------------------------------- Step text */

.step { margin: 1.25rem 0 1rem; padding-left: 1rem; border-left: 3px solid var(--ink); }
.step .count { display: block; font-size: 0.85rem; color: var(--muted); letter-spacing: 0.04em; }
.step h2 { margin: 0.1rem 0 0.35rem; }
.step p { margin: 0; }

.message { min-height: 1.6em; margin: 0.75rem 0; font-weight: 500; max-width: 40rem; }

/* --------------------------------------------- Place-value fills and text */

.pv-green { --pv: var(--green); }
.pv-blue  { --pv: var(--blue); }
.pv-red   { --pv: var(--red); }

.card {
  display: inline-grid; place-items: center;
  min-width: 2.2rem; min-height: 2.6rem; padding: 0 0.35rem;
  border: 1px solid var(--line); border-radius: 0.2rem;
  background: var(--paper);
  color: var(--pv, var(--ink));
  font-family: var(--serif); font-size: 1.6rem; line-height: 1;
}

/* ---------------------------------------------------------- Checkerboard */

.board {
  --cols: 6;
  display: grid;
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr)) auto;
  gap: 3px;
  width: min(100%, 42rem);
}

.sq {
  display: flex; flex-direction: column; justify-content: space-between;
  aspect-ratio: 1;
  min-width: 0;
  padding: 3px;
  border: 0; border-radius: 2px;
  background: var(--pv);
  color: var(--paper);
  cursor: pointer;
  text-align: left;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}
.sq:focus-visible { outline: var(--focus); outline-offset: 2px; z-index: 1; }
.sq.idle { opacity: 0.45; cursor: default; }
.sq.diag { box-shadow: inset 0 0 0 3px var(--paper), inset 0 0 0 5px var(--ink); }

.sq { container-type: inline-size; }
.sq .value { font-size: clamp(7px, 14cqw, 12px); line-height: 1; white-space: nowrap; }
.sq .n { font-size: clamp(11px, 21cqw, 19px); font-weight: 700; line-height: 1; align-self: flex-end; }

/* --k (set per square from JS) is the bead diameter in % of the square width. */
.beads {
  --b: calc(var(--k, 10) * 1cqw);
  display: flex; flex-wrap: wrap; align-items: flex-end; align-content: flex-end;
  gap: calc(var(--b) * 0.35);
  flex: 1; min-height: 0; padding: 1px 0;
}

/* A bead bar: a column of --len round beads, so ten is a full bar. */
.bar {
  width: var(--b);
  height: calc(var(--b) * var(--len));
  background: radial-gradient(circle, var(--paper) 0 62%, transparent 66%) 0 0 / var(--b) var(--b) repeat-y;
}

.col-label, .row-label {
  display: flex; flex-direction: column; align-items: center; gap: 0.25rem;
  font-size: clamp(0.6rem, 1.8vw, 0.8rem); color: var(--muted); white-space: nowrap;
}
.col-label { padding-top: 0.35rem; }
.row-label { flex-direction: row; justify-content: flex-start; padding-left: 0.4rem; }
.row-label .card, .col-label .card {
  font-size: clamp(1.05rem, 3.6vw, 1.6rem);
  min-width: clamp(1.5rem, 5vw, 2.2rem); min-height: clamp(1.8rem, 6vw, 2.6rem);
}

.diag-note { min-height: 3em; font-size: 0.95rem; color: var(--muted); max-width: 42rem; margin: 0.75rem 0 0; }

.answer { display: flex; flex-wrap: wrap; gap: 0.6rem; align-items: end; margin: 1rem 0; }
.answer input { width: 9rem; }

.result { margin: 1rem 0; }
.result .ok { font-weight: 600; }
.result .ok::before { content: "\2713\00a0"; }
.partials { border-collapse: collapse; margin: 0.5rem 0 0; }
.partials td { padding: 0.15rem 0.6rem 0.15rem 0; text-align: right; }
.partials tr.sum td { border-top: 2px solid var(--ink); font-weight: 700; }

/* ------------------------------------------------------------ Stamp game */

.stamps {
  display: grid;
  grid-template-columns: auto repeat(4, minmax(0, 1fr));
  gap: 0.5rem clamp(0.4rem, 2vw, 1.25rem);
  align-items: start;
  max-width: 44rem;
}
.stamps .head { text-align: center; font-size: 0.85rem; color: var(--muted); }
.stamps .head strong { display: block; color: var(--pv); font-size: 1rem; }
.stamps .rowname { font-size: 0.85rem; color: var(--muted); padding-top: 0.3rem; white-space: nowrap; }
.stamps .sumline { grid-column: 1 / -1; border-top: 2px solid var(--ink); margin: 0.25rem 0; }
.stamps .num { text-align: center; font-family: var(--serif); font-size: 1.5rem; color: var(--pv); line-height: 1.2; }

.pile {
  --stamp: clamp(1.45rem, 5vw, 2.1rem);
  display: grid;
  grid-template-columns: repeat(auto-fill, var(--stamp));
  justify-content: center;
  gap: 3px;
  align-content: start;
  min-height: calc(var(--stamp) + 0.6rem);
  padding: 0.3rem;
  border-radius: 0.3rem;
}
button.pile { width: 100%; border: 1px dashed var(--muted); background: var(--paper); cursor: pointer; }
button.pile.can { border: 2px solid var(--ink); }

.stamp {
  display: grid; place-items: center;
  width: var(--stamp); height: var(--stamp);
  border-radius: 2px;
  background: var(--pv); color: var(--paper);
  font-size: clamp(0.45rem, 1.5vw, 0.66rem); font-weight: 700; white-space: nowrap;
}

/* ---------------------------------------------------------- Hundred board */

.hundred {
  display: grid;
  grid-template-columns: repeat(10, minmax(0, 1fr));
  gap: 2px;
  width: min(100%, 34rem);
}
.cell {
  aspect-ratio: 1;
  display: grid; place-items: center;
  min-width: 0;
  border: 1px solid var(--line); border-radius: 2px;
  background: var(--paper);
  font-size: clamp(0.8rem, 2.6vw, 1.1rem);
  padding: 0;
  cursor: pointer;
}
.cell.filled { border-color: var(--ink); font-weight: 600; }
.cell.hint { color: var(--muted); }
.cell.wrong { border: 2px dashed var(--ink); background: var(--wash); text-decoration: line-through; }
.cell.drop { background: var(--wash); border-color: var(--ink); }

.tray { display: flex; flex-wrap: wrap; gap: 0.5rem; min-height: 3.5rem; margin: 1rem 0; }
.tile {
  min-width: 3rem; min-height: 3rem;
  border: 1px solid var(--ink); border-radius: 2px;
  background: var(--paper);
  font-size: 1.15rem; font-weight: 600;
  cursor: grab;
  touch-action: none;
}
.tile[aria-pressed="true"] { background: var(--ink); color: var(--paper); }
.tile.dragging {
  position: fixed; z-index: 10; pointer-events: none;
  width: 3rem; height: 3rem;
  box-shadow: 0 4px 14px rgba(35, 33, 28, 0.25);
}

/* ----------------------------------------------------------- Bead chains */

.chain { display: flex; flex-wrap: wrap; gap: 1rem 0.5rem; align-items: center; margin: 1.5rem 0; }
.chain-bar { display: flex; align-items: center; gap: 0.4rem; }
.chain-beads { display: flex; gap: 1px; padding: 2px 0; border-bottom: 1px solid var(--muted); }
.bead {
  width: clamp(1.15rem, 3.2vw, 1.5rem); height: clamp(1.15rem, 3.2vw, 1.5rem);
  padding: 0;
  border: 1px solid var(--ink); border-radius: 50%;
  background: var(--chain);
  cursor: pointer;
}
.bead.counted { box-shadow: inset 0 0 0 3px var(--paper); }
.bead.here { outline: 2px solid var(--ink); outline-offset: 1px; }

/* Arrow tickets point left, back at the bar they belong to. The head is a
   rotated square tucked behind the box; its diagonal equals the box height. */
.slot, .ticket {
  --h: 2.6rem;
  --line-style: solid;
  --edge: var(--ink);
  --fill: var(--paper);
  position: relative;
  isolation: isolate;
  min-width: 3.2rem; height: var(--h);
  margin-left: calc(var(--h) / 2);
  padding: 0 0.6rem 0 0.35rem;
  border: 2px var(--line-style) var(--edge);
  border-left: 0;
  background: var(--fill);
  font-size: 1.05rem; font-weight: 600;
  cursor: pointer;
}
.slot::before, .ticket::before {
  content: "";
  position: absolute; z-index: -1;
  top: 50%; left: 0;
  width: calc(var(--h) * 0.7071); height: calc(var(--h) * 0.7071);
  transform: translate(-50%, -50%) rotate(45deg);
  border-left: 2px var(--line-style) var(--edge);
  border-bottom: 2px var(--line-style) var(--edge);
  background: var(--fill);
}
.slot { --line-style: dashed; --edge: var(--muted); }
.slot.filled { --line-style: solid; --edge: var(--ink); }
.slot.wrong { --line-style: dashed; --edge: var(--ink); --fill: var(--wash); text-decoration: line-through; border-width: 3px; border-left: 0; }
.ticket[aria-pressed="true"] { --fill: var(--ink); color: var(--paper); }

.tickets { display: flex; flex-wrap: wrap; gap: 0.5rem; margin: 1rem 0; min-height: 2.6rem; }
.counter { font-family: var(--serif); font-size: 1.5rem; min-height: 2.2rem; margin: 0.5rem 0; }

/* -------------------------------------------------------------- Home page */

.intro { display: grid; gap: var(--gap); grid-template-columns: minmax(0, 1fr); }
@media (min-width: 52rem) { .intro { grid-template-columns: 7fr 4fr; align-items: end; } }

.swatch { display: flex; gap: 0.35rem; margin: 0.25rem 0 0.5rem; flex-wrap: wrap; }
.swatch span {
  display: inline-grid; place-items: center;
  min-width: 3.6rem; height: 2.4rem; padding: 0 0.45rem;
  background: var(--pv); color: var(--paper);
  font-size: 0.8rem; font-weight: 700; border-radius: 2px;
}

.materials { list-style: none; padding: 0; margin: 1rem 0 0; max-width: 44rem; }
.materials li { padding: 1.1rem 0; border-top: 1px solid var(--line); }
.materials li:last-child { border-bottom: 1px solid var(--line); }
.materials a { font-family: var(--serif); font-size: 1.45rem; text-decoration: none; }
.materials a:hover { text-decoration: underline; }
.materials p { margin: 0.2rem 0 0; color: var(--muted); }

footer { margin-top: 4rem; color: var(--muted); font-size: 0.9rem; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; }
}
