/*
  Jumblé — style.css
  ------------------
  Sections, in order:
    1. Typeface and design tokens (colours, type, motion)
    2. Base elements and shared pieces (buttons, top bar)
    3. Home screen
    4. Pack screen
    5. Play screen (board, capsules, word list, result)
    6. Screen-to-screen transitions
    7. Reduced motion
*/

/* ---- 1. Typeface and tokens ----------------------------------------- */

/* One variable font file covers every weight (200-800) and width
   (75%-100%), so headings can be condensed and bold from the same file. */
@font-face {
  font-family: 'Bricolage Grotesque';
  src: url('../fonts/bricolage-grotesque.woff2') format('woff2');
  font-weight: 200 800;
  font-stretch: 75% 100%;
  font-display: swap;
}

:root {
  /* Two colours. Everything else is a mix of them, so dark mode only has
     to swap these two lines. */
  --paper: #ffffff;
  --ink: #000000;
  --ink-soft: color-mix(in srgb, var(--ink) 62%, var(--paper));  /* secondary text */
  --ink-faint: color-mix(in srgb, var(--ink) 32%, var(--paper)); /* filler letters in the masthead */
  --ink-wash: color-mix(in srgb, var(--ink) 13%, var(--paper));  /* tracks and rules */
  --letter: var(--ink); /* unfound letters in the grid; softened in dark mode below */

  --font: 'Bricolage Grotesque', ui-sans-serif, system-ui, sans-serif;

  /* Type scale: each step is 4/3 of the one before (a "perfect fourth"). */
  --step-0: 1.0625rem; /* 17px */
  --step-1: 1.4rem;
  --step-2: 1.9rem;
  --step-3: 2.5rem;

  --pad: clamp(1.25rem, 5vw, 2rem);
  --capsule: 0.68; /* capsule thickness, as a fraction of one cell */

  --ease: cubic-bezier(0.2, 0.8, 0.2, 1);
  --spring: cubic-bezier(0.3, 1.5, 0.5, 1);

  color-scheme: light dark;
}

/* A true spring curve where the browser supports it. */
@supports (transition-timing-function: linear(0, 1)) {
  :root {
    --spring: linear(0, 0.33 8%, 0.83 20%, 1.04 29%, 1.1 36%, 1.08 44%, 1.01 58%, 0.99 72%, 1);
  }
}

@media (prefers-color-scheme: dark) {
  :root {
    --paper: #000000;
    --ink: #ffffff;
    /* A full grid of pure white letters on pure black shimmers (the
       Hermann grid effect). Unfound letters sit at 70% so the capsules
       and found words, which stay pure white, are the brightest things
       on the board. Raise or lower the percentage to taste. */
    --letter: color-mix(in srgb, var(--ink) 70%, var(--paper));
  }
}

/* ---- 2. Base --------------------------------------------------------- */

*, *::before, *::after { box-sizing: border-box; }
* { margin: 0; }

html {
  background: var(--paper);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

body {
  min-height: 100dvh;
  background: var(--paper);
  color: var(--ink);
  font: 450 var(--step-0) / 1.4 var(--font);
  -webkit-font-smoothing: antialiased;
  /* It's a game, not a document: no text selection, no grey tap flash,
     no double-tap zoom. */
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

/* On the play screen, stop the page itself from being pulled around. */
html[data-screen='play'] body { overscroll-behavior: none; }

ul { list-style: none; padding: 0; }
a { color: inherit; text-decoration: none; }
button { font: inherit; color: inherit; cursor: pointer; }

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

.page {
  max-width: 34rem;
  margin-inline: auto;
  padding:
    env(safe-area-inset-top)
    max(var(--pad), env(safe-area-inset-right))
    max(2.5rem, env(safe-area-inset-bottom))
    max(var(--pad), env(safe-area-inset-left));
}

/* Top bar: back button, title, counter. */
.bar {
  display: grid;
  grid-template-columns: 2.75rem minmax(0, 1fr) auto auto;
  align-items: center;
  min-height: 3.25rem;
}
.icon-button {
  display: grid;
  place-items: center;
  width: 2.75rem;
  height: 2.75rem;
  margin-left: -0.7rem; /* lines the arrow's edge up with the text below */
  border: 0;
  border-radius: 50%;
  background: none;
}
/* The eye button on the puzzle screen. Dimmed while the list is hidden. */
.icon-button.eye { margin-left: 0; margin-right: -0.35rem; color: var(--ink-soft); }
.icon-button.eye[aria-pressed='true'] { color: var(--ink); }
.icon { fill: none; stroke: currentColor; stroke-width: 2.25; stroke-linecap: round; stroke-linejoin: round; }
.bar-title {
  font-size: var(--step-1);
  font-weight: 650;
  font-stretch: 88%;
  line-height: 1.2;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.bar-count { color: var(--ink-soft); font-variant-numeric: tabular-nums; padding-left: 0.6rem; }

/* Buttons. The solid one borrows the capsule shape from the board. */
.pill {
  display: block;
  width: 100%;
  min-height: 3.25rem;
  padding: 0.6rem 1.5rem;
  border: 0;
  border-radius: 1.625rem;
  background: var(--ink);
  color: var(--paper);
  font-weight: 650;
  transition: transform 0.2s var(--ease);
}
.pill:active { transform: scale(0.97); }
.quiet {
  min-height: 2.75rem;
  padding: 0 1rem;
  border: 0;
  background: none;
  font-weight: 550;
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 0.25em;
}

/* ---- 3. Home --------------------------------------------------------- */

/* The masthead: a 6x6 jumble with the name found on the diagonal.
   container-type lets the letters size themselves from the grid's width
   (the cqw unit below means "1% of this container's width"). */
.hero {
  --size: min(64vw, 17rem);
  position: relative;
  width: var(--size);
  aspect-ratio: 1;
  /* Nudge left so the capsule's edge, not the invisible cell edge, sits
     on the page margin. */
  margin: clamp(1.5rem, 8vh, 4.5rem) 0 clamp(2rem, 8vh, 4.5rem) calc(var(--size) * -0.027);
  container-type: inline-size;
  font-size: 1rem;
  font-weight: 700;
}
.hero .cells { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(6, 1fr); }
.hero .cell { display: grid; place-items: center; font-size: 8.4cqw; line-height: 1; color: var(--ink-faint); }
.hero .cell.name { color: var(--paper); }

/* Shared by the masthead and the board: the layer the capsules are drawn on. */
.marks { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.mark, .live { fill: none; stroke: var(--ink); stroke-width: var(--capsule); stroke-linecap: round; stroke-linejoin: round; }

/* First visit only: the capsule draws itself across the name and each
   letter flips as it is covered. "backwards" holds the starting look
   during the delay. */
.hero.intro .mark { stroke-dasharray: 1 1; animation: draw 0.75s cubic-bezier(0.5, 0, 0.3, 1) 0.45s backwards; }
.hero.intro .cell.name { animation: flip 0.18s linear calc(0.5s + var(--k) * 0.11s) backwards; }
@keyframes draw {
  0% { stroke-dashoffset: 1; opacity: 0; }
  4% { opacity: 1; }
  100% { stroke-dashoffset: 0; }
}
@keyframes flip { from { color: var(--ink); } }

.packs > li + li { border-top: 1px solid var(--ink-wash); }
.pack { display: grid; padding: 1.4rem 0 1.5rem; }
.pack-title { font-size: var(--step-3); font-weight: 700; font-stretch: 80%; line-height: 1; letter-spacing: -0.015em; }
.pack-note { margin-top: 0.4rem; color: var(--ink-soft); }
.pack-count { margin-top: 0.45rem; font-size: 0.9375rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

/* Progress bar. --part is a number from 0 to 1 set by app.js. */
.meter { position: relative; height: 0.375rem; margin-top: 1.1rem; border-radius: 1rem; background: var(--ink-wash); overflow: hidden; }
.meter::after { content: ''; position: absolute; inset: 0; width: calc(var(--part) * 100%); border-radius: inherit; background: var(--ink); }

/* ---- 4. Pack --------------------------------------------------------- */

.page-title { margin: 0.5rem 0 1.5rem; font-size: var(--step-3); font-weight: 700; font-stretch: 80%; line-height: 1; letter-spacing: -0.015em; }

/* Difficulty switch. The dark thumb slides behind the chosen label;
   --at is 0, 1 or 2 and is set by app.js. */
.levels {
  position: relative;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  height: 3rem;
  padding: 0.25rem;
  border-radius: 1.5rem;
  background: var(--ink-wash);
}
.levels button { position: relative; border: 0; border-radius: 1.25rem; background: none; font-weight: 600; transition: color 0.2s; }
.levels button[aria-checked='true'] { color: var(--paper); }
.thumb {
  position: absolute;
  top: 0.25rem;
  bottom: 0.25rem;
  left: 0.25rem;
  width: calc((100% - 0.5rem) / 3);
  border-radius: 1.25rem;
  background: var(--ink);
  transform: translateX(calc(var(--at, 0) * 100%));
  transition: transform 0.4s var(--spring);
}
.level-hint { min-height: 2.8em; margin-top: 0.75rem; color: var(--ink-soft); }

.group { margin-top: 1.25rem; }
.group-head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; padding-bottom: 0.6rem; }
.group-head h2 { font-size: var(--step-1); font-weight: 650; font-stretch: 88%; }
.group-tally { font-size: 0.9375rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }

.rows > li { border-top: 1px solid var(--ink-wash); }
.row { display: flex; align-items: center; justify-content: space-between; gap: 1rem; min-height: 3.5rem; font-size: 1.1875rem; font-weight: 500; }
.row:active .row-title { opacity: 0.55; }

/* Status ring: empty, part-way round, or a solid disc with a tick. */
.ring { flex: none; width: 1.5rem; height: 1.5rem; }
.ring svg { display: block; width: 100%; height: 100%; overflow: visible; }
.ring circle, .ring path { fill: none; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; }
.ring .track { stroke: var(--ink-wash); }
.ring .arc { stroke: var(--ink); stroke-dasharray: var(--part, 0) 1; transform: rotate(-90deg); transform-origin: center; opacity: 0; transition: stroke-dasharray 0.4s var(--ease); }
.ring .disc { fill: var(--ink); stroke: none; transform: scale(0); transform-origin: center; transition: transform 0.4s var(--spring); }
.ring .tick { stroke: var(--paper); opacity: 0; transition: opacity 0.2s 0.1s; }
.row[data-state='going'] .arc { opacity: 1; }
.row[data-state='done'] .disc { transform: scale(1); }
.row[data-state='done'] .tick { opacity: 1; }

/* Text for screen readers only (the ring says the same thing visually). */
.row-status { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }

/* ---- 5. Play --------------------------------------------------------- */

/* The screen is exactly one viewport tall and never scrolls. The bar sits
   on top; the "field" underneath holds the board and the word list. */
.play {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  --pad: clamp(1rem, 4vw, 2rem); /* slimmer side margins here: every pixel goes to the board */
  height: 100dvh;
  max-width: 72rem;
  margin-inline: auto;
  padding:
    env(safe-area-inset-top)
    max(var(--pad), env(safe-area-inset-right))
    max(1rem, env(safe-area-inset-bottom))
    max(var(--pad), env(safe-area-inset-left));

  /* How tall the board may be: the viewport minus the bar, the word list
     and the safe areas. Tall phones: the board is limited by width instead. */
  --fit: calc(100dvh - 14.5rem - env(safe-area-inset-top) - env(safe-area-inset-bottom));
}
.field { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 1.25rem; min-height: 0; }

.board {
  position: relative;
  flex: none;
  width: min(100%, var(--fit) * var(--cols) / var(--rows), 40rem);
  aspect-ratio: var(--cols) / var(--rows);
  container-type: inline-size;
  touch-action: none; /* the board handles every touch itself: no scrolling, no zooming */
  -webkit-touch-callout: none;
  cursor: pointer;
}
.board .cells { position: absolute; inset: 0; display: grid; grid-template-columns: repeat(var(--cols), 1fr); pointer-events: none; }
.board .cell {
  display: grid;
  place-items: center;
  font-size: calc(100cqw / var(--cols) * 0.52); /* about half a cell tall, at any board size */
  font-weight: 650;
  line-height: 1;
  color: var(--letter);
  transition: opacity 0.7s var(--ease);
}
.board .cell.on { color: var(--paper); }

/* Bent words pack closer together, so their capsules are a touch thinner
   to keep neighbours apart. */
.play[data-level='hard'] { --capsule: 0.6; }

/* Opening a puzzle: letters arrive in a diagonal sweep. */
.board.entering .cell { animation: arrive 0.5s var(--ease) calc(var(--d) * 22ms) backwards; }
@keyframes arrive { from { opacity: 0; transform: scale(0.5); } }

/* The capsule under your finger grows from nothing when you touch down. */
.live { stroke-width: 0; transition: stroke-width 0.14s var(--ease); }
.live.show { stroke-width: var(--capsule); }

/* Found: the capsule swells and settles, and the letters hop in turn. */
.mark.pop { animation: pop 0.5s var(--spring); }
@keyframes pop { from { stroke-width: 0.98; } }
.board .cell.hop { animation: hop 0.45s var(--ease) calc(var(--k) * 32ms); }
@keyframes hop { 35% { transform: translateY(-22%) scale(1.22); } }

/* Not a word: the capsule fades out. */
.mark.miss { animation: miss 0.3s ease-out forwards; }
@keyframes miss { to { opacity: 0; stroke-width: 0.3; } }

/* Solved: filler letters fall away and the words ripple once. */
.play.solved .cell:not(.on) { opacity: 0.14; }
.play.solved .board { cursor: default; }
.board.wave .cell.on { animation: hop 0.55s var(--ease) calc(var(--d) * 34ms); }

/* While tracing, the bar title shows the letters picked so far. */
.play.tracing .bar-title { font-weight: 750; font-stretch: 100%; letter-spacing: 0.1em; }

/* Word list and result share the space under (or beside) the board. */
.side { display: grid; justify-items: center; align-content: start; width: 100%; min-height: 0; overflow-y: auto; }
.words { display: flex; flex-wrap: wrap; justify-content: center; align-content: flex-start; gap: 0.2rem 1.15rem; max-width: 40rem; }
.word {
  font-weight: 500;
  line-height: 1.75;
  white-space: nowrap;
  /* The strike-through is a background line that grows from 0 to full width. */
  background: linear-gradient(currentColor, currentColor) no-repeat left 56% / 0 1.5px;
  transition: background-size 0.4s var(--ease), color 0.4s;
}
.word.found { color: var(--ink-soft); background-size: 100% 1.5px; }

/* A word that is still hidden: a blank bar the height of a text line. All
   bars are the same width so they give nothing away. */
.word.masked {
  width: 3.25rem;
  height: 0.5rem;
  margin-block: calc((1.75em - 0.5rem) / 2);
  border-radius: 1rem;
  background: var(--ink-wash);
  transition: none;
}
/* The moment a hidden word is found, its name fades up in place. */
.word.reveal { animation: reveal 0.5s var(--ease) backwards; }
@keyframes reveal { from { opacity: 0; transform: translateY(0.4rem); } }

.play.solved .words { display: none; }
.result { display: grid; justify-items: center; gap: 0.35rem; width: min(100%, 22rem); text-align: center; }
.result-title { font-size: var(--step-2); font-weight: 700; font-stretch: 82%; line-height: 1.1; font-variant-numeric: tabular-nums; }
.result-note { color: var(--ink-soft); }
.result .pill { margin-top: 0.75rem; }
.result.rise { animation: rise 0.6s var(--ease) 0.5s backwards; }
@keyframes rise { from { opacity: 0; transform: translateY(1.25rem); } }

/* Wider than tall (landscape phone, unfolded Fold on its side, desktop):
   board on the left, words in a column on the right. */
@media (min-aspect-ratio: 1/1) and (min-width: 36rem) {
  .play { --fit: calc(100dvh - 5.25rem - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .field { flex-direction: row; gap: clamp(1.5rem, 4vw, 3.5rem); }
  .board { flex: 0 1 auto; width: min(100% - 13rem, var(--fit) * var(--cols) / var(--rows), 46rem); }
  .side { width: auto; flex: 0 0 auto; min-width: 10rem; max-width: 20rem; max-height: 100%; justify-items: start; align-content: center; }
  .words { flex-direction: column; flex-wrap: nowrap; align-items: flex-start; gap: 0; }
  .word { font-size: 1.125rem; line-height: 1.85; }
  .word.masked { margin-block: calc((1.85em - 0.5rem) / 2); }
  .result { justify-items: start; text-align: left; width: 16rem; }
}

/* The confetti canvas covers the screen but lets every touch through. */
.confetti { position: fixed; inset: 0; z-index: 10; width: 100%; height: 100%; pointer-events: none; }

/* ---- 6. Screen transitions ------------------------------------------- */
/* app.js sets data-nav to forward, back or same before swapping screens. */

::view-transition-old(root), ::view-transition-new(root) { animation-duration: 0.3s; animation-timing-function: var(--ease); }
html[data-nav='forward']::view-transition-old(root) { animation-name: leave-left; }
html[data-nav='forward']::view-transition-new(root) { animation-name: enter-right; }
html[data-nav='back']::view-transition-old(root) { animation-name: leave-right; }
html[data-nav='back']::view-transition-new(root) { animation-name: enter-left; }
@keyframes leave-left { to { opacity: 0; transform: translateX(-1.75rem); } }
@keyframes enter-right { from { opacity: 0; transform: translateX(1.75rem); } }
@keyframes leave-right { to { opacity: 0; transform: translateX(1.75rem); } }
@keyframes enter-left { from { opacity: 0; transform: translateX(-1.75rem); } }

/* ---- 7. Reduced motion ------------------------------------------------ */
/* If the device asks for less motion, everything simply snaps into place. */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-delay: 0s !important;
    transition-duration: 0.01ms !important;
    transition-delay: 0s !important;
  }
}
