/* The look comes from what you solve a puzzle book with: an eraser, a ballpoint and a pencil.
   Printed type belongs to the puzzle. Handwriting is only for letters the player enters. */

@font-face { font-family: "Bagel Fat One"; src: url("fonts/bagel-fat-one.woff2") format("woff2"); font-display: swap; }
@font-face { font-family: "Atkinson Hyperlegible"; src: url("fonts/atkinson-400.woff2") format("woff2"); font-weight: 400; font-display: swap; }
@font-face { font-family: "Atkinson Hyperlegible"; src: url("fonts/atkinson-700.woff2") format("woff2"); font-weight: 700; font-display: swap; }
@font-face { font-family: "Patrick Hand"; src: url("fonts/patrick-hand.woff2") format("woff2"); font-display: swap; }

:root {
  color-scheme: light;
  --eraser: #f5a8b8;
  --blush: #ffe3e8;
  --highlighter: #8df0a6;
  --paper: #ffffff;
  --print: #151b45;     /* text and black squares */
  --ink: #2440c4;       /* right spot: ballpoint */
  --pencil: #d65a00;    /* wrong spot: orange pencil */
  --graphite: #666a7c;  /* not in the word, and letters not guessed yet */
  --key: #eceef7;

  --display: "Bagel Fat One", "Arial Rounded MT Bold", system-ui, sans-serif;
  --text: "Atkinson Hyperlegible", system-ui, sans-serif;
  --hand: "Patrick Hand", "Chalkboard SE", "Comic Sans MS", cursive;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html:has(dialog[open]) { overflow: hidden; }

body {
  margin: 0;
  background: var(--eraser);
  color: var(--print);
  font: 1.0625rem/1.5 var(--text);
}
h1, h2, p, ul, ol { margin: 0; padding: 0; }
ul, ol { list-style: none; }
button { color: inherit; font: inherit; cursor: pointer; touch-action: manipulation; }
:focus-visible { outline: 3px solid var(--print); outline-offset: 3px; }

/* ---- Page ---- */

main {
  display: grid;
  gap: 1.25rem;
  max-width: 72rem;
  margin: 0 auto;
  padding: 1.25rem max(1rem, env(safe-area-inset-right)) max(2rem, env(safe-area-inset-bottom)) max(1rem, env(safe-area-inset-left));
}
.intro { display: grid; gap: .6rem; }
h1 {
  color: var(--ink);
  font: 400 clamp(4rem, 20.5vw, 6.5rem)/.9 var(--display);
  letter-spacing: -.025em;
}
.tagline { font-size: clamp(1rem, 4.6vw, 1.25rem); font-weight: 700; line-height: 1.3; }
.rules { display: grid; gap: .25rem; max-width: 34rem; padding-left: 1.2em; list-style: disc; }
.legend { display: flex; flex-wrap: wrap; gap: .4rem .7rem; font-size: .875rem; }
.legend li { display: flex; align-items: center; gap: .35rem; }
.legend .mark { width: 1.9rem; border-radius: 2px; font-size: 1.35rem; }
.legend .mark:not([data-mark="right"]) { background: var(--paper); }

.standing { display: grid; align-content: start; justify-items: start; gap: .6rem; }
.tally { display: flex; flex-wrap: wrap; gap: .3rem .6rem; }
.tally svg {
  width: 2rem;
  height: 1.9rem;
  fill: none;
  stroke: var(--print);
  stroke-width: 2.4;
  stroke-linecap: round;
}
.edition { font-size: .9375rem; }

.button {
  min-height: 3rem;
  padding: .6rem 1.4rem;
  border: 0;
  border-radius: .5rem;
  background: var(--print);
  color: #fff;
  font-weight: 700;
}

/* ---- Grid ---- */

.board, .strip { display: grid; gap: 2px; padding: 2px; background: var(--print); }
.board {
  grid-template-columns: repeat(var(--cols), minmax(0, 1fr));
  width: min(100%, calc(var(--cols) * 5.4rem));
  margin: 0 auto;
}
.black, .cell, .slot { aspect-ratio: 1; }
.cell, .slot {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: var(--paper);
  line-height: 1;
  font-family: var(--hand);
}
.cell { font-size: clamp(1.6rem, 10vw, 3.3rem); }
.cell .number, .slot .number { position: absolute; top: .2rem; left: .28rem; font: .75rem/1 var(--text); }
.cell.selected { background: var(--highlighter); }
.cell.inked, .slot.inked { background: var(--ink); color: #fff; }
.cell.inked.selected { box-shadow: inset 0 0 0 6px var(--highlighter); }
.cell.revealed, .slot.revealed { color: var(--graphite); }
.cell:focus-visible { z-index: 1; outline-offset: -5px; outline-color: var(--pencil); }

.cell.fresh { animation: ink-cell .45s calc(var(--order) * 110ms) ease-out backwards; }
@keyframes ink-cell {
  from { background: var(--paper); color: transparent; }
  40% { background: var(--paper); color: var(--ink); }
}

/* ---- Pen and pencil marks, on guesses, keys and the legend ---- */

.mark {
  display: grid;
  place-items: center;
  aspect-ratio: 1;
  min-width: 0;
  font-family: var(--hand);
  line-height: 1;
}
[data-mark] { position: relative; }
[data-mark="right"] { background: var(--ink); color: #fff; }
[data-mark="miss"] { color: var(--graphite); }
[data-mark="near"]::after, [data-mark="miss"]::after { content: ""; position: absolute; pointer-events: none; }
[data-mark="near"]::after {
  inset: 5%;
  border: 3px solid var(--pencil);
  border-radius: 47% 53% 56% 44% / 55% 45% 53% 47%;
  rotate: -9deg;
}
[data-mark="miss"]::after {
  top: calc(50% - 1px);
  left: 12%;
  width: 76%;
  border-top: 2.5px solid var(--graphite);
  border-radius: 2px;
  rotate: -48deg;
}

/* Marks on a new guess are made one at a time, left to right. */
.new .mark { --delay: calc(var(--order) * 110ms); }
.new [data-mark="right"] { animation: fill-in .3s var(--delay) ease-out backwards; }
.new [data-mark="near"]::after { animation: circle-in .3s var(--delay) ease-out backwards; }
.new [data-mark="miss"]::after { animation: slash-in .25s var(--delay) ease-out backwards; }
@keyframes fill-in { from { background: transparent; color: var(--print); } }
@keyframes circle-in { from { opacity: 0; scale: 1.35; } }
@keyframes slash-in { from { scale: 0 1; } }

/* ---- Pop-ups ---- */

dialog {
  width: 100%;
  max-width: 31rem;
  max-height: none;
  margin: auto auto 0;
  padding: 0;
  border: 0;
  border-radius: 1.25rem 1.25rem 0 0;
  background: var(--paper);
  color: inherit;
}
dialog::backdrop { background: rgb(21 27 69 / .6); }
dialog[open] { animation: sheet-in .22s ease-out; }
@keyframes sheet-in { from { translate: 0 3rem; opacity: 0; } }

.sheet {
  display: flex;
  flex-direction: column;
  gap: .7rem;
  max-height: calc(100vh - .75rem);
  max-height: calc(100dvh - .75rem);
  padding: 1rem 1rem max(1rem, env(safe-area-inset-bottom));
  overflow-y: auto;
}
.sheet > * { flex: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.sheet h2 { color: var(--ink); font: 400 2rem/1.1 var(--display); }
.close {
  min-height: 2.75rem;
  margin-right: -.6rem;
  padding: 0 .6rem;
  border: 0;
  border-radius: .5rem;
  background: none;
  text-decoration: underline;
  text-underline-offset: .2em;
}
.word-info { margin-top: 0; }
.word-info { margin-top: -.6rem; }
.crossings { display: flex; flex-wrap: wrap; align-items: center; gap: .4rem; }
.crossings span { margin-right: .2rem; }
.crossings button {
  min-width: 2.75rem;
  height: 2.75rem;
  border: 2px solid var(--print);
  border-radius: 0;
  background: var(--paper);
  font-weight: 700;
}
.crossings button.solved { border-color: var(--ink); background: var(--ink); color: #fff; }

/* Past guesses share the strip's columns, so each letter sits above its square. */
.sheet > .letters {
  --columns: repeat(var(--length), minmax(0, 1fr));
  display: flex;
  flex: 0 1 auto;
  flex-direction: column;
  align-self: center;
  width: min(100%, calc(var(--length) * 3.75rem));
  min-height: min(15rem, 36vh);
}
.history {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  gap: 4px;
  min-height: 0;
  padding: 2px 2px 8px;
  overflow-y: auto;
}
.history li:first-child { margin-top: auto; }
.history li { display: grid; flex: none; grid-template-columns: var(--columns); gap: 2px; }
.history .mark { height: 2.9rem; width: 2.9rem; max-width: 100%; justify-self: center; font-size: 2rem; }
.strip { flex: none; grid-template-columns: var(--columns); }
.slot { color: var(--graphite); font-size: clamp(1.4rem, 9vw, 2.5rem); }
.slot .number { color: var(--print); }
.slot.inked .number { color: inherit; }
.slot.cursor { background: var(--blush); box-shadow: inset 0 0 0 3px var(--ink); }
.slot:focus-visible { outline: none; } /* focus follows the cursor, which is already outlined */

.message { min-height: 1.5em; text-align: center; }

.keys { display: grid; gap: .35rem; }
.keys div { display: flex; justify-content: center; gap: .3rem; }
.keys button {
  flex: 1 1 0;
  min-width: 0;
  max-width: 2.75rem;
  height: 2.9rem;
  padding: 0;
  border: 0;
  border-radius: .35rem;
  background: var(--key);
  font-size: 1.125rem;
  font-weight: 700;
}
.keys button[data-mark="right"] { background: var(--ink); }
.keys button[data-mark="near"]::after { inset: 8% 2%; }
.keys button:active { translate: 0 1px; }
.keys .guess, .keys .erase { max-width: 11rem; }
.keys .guess { background: var(--print); color: #fff; }
/* The Delete key is the eraser, sleeve and all. */
.keys .erase {
  padding-left: 2.75rem;
  border-radius: .35rem .9rem .9rem .35rem;
  background: linear-gradient(90deg, var(--ink) 2.25rem, var(--key) 2.25rem 2.75rem, var(--eraser) 2.75rem);
}

#results-dialog textarea {
  width: 100%;
  padding: .9rem;
  border: 0;
  border-radius: .5rem;
  background: var(--blush);
  color: inherit;
  font: 1.0625rem/1.5 var(--text);
  resize: none;
}

/* ---- Wide screens ---- */

@media (min-width: 52rem) {
  main {
    grid-template: "intro play" auto "standing play" 1fr / minmax(0, 1fr) minmax(0, 1.1fr);
    gap: 2rem 4rem;
    padding-block: 3rem;
  }
  .intro { grid-area: intro; gap: 1rem; }
  .play { grid-area: play; }
  .standing { grid-area: standing; }
  h1 { font-size: clamp(5.5rem, 10.5vw, 8rem); }
  /* Keep the whole grid on screen. */
  .board { width: min(100%, calc(var(--cols) * 5.4rem), calc((100vh - 6rem) * var(--cols) / var(--rows))); }
}

@media (min-width: 36rem) and (min-height: 40rem) {
  dialog { margin: auto; border-radius: 1.25rem; }
  .sheet { padding: 1.5rem; }
}

/* ---- Short screens: keep the Guess key in view ---- */

@media (max-height: 46rem) {
  .sheet { gap: .4rem; padding-block: .6rem max(.6rem, env(safe-area-inset-bottom)); }
  .sheet h2 { font-size: 1.6rem; }
  .sheet > .letters { width: min(100%, calc(var(--length) * 3.25rem)); min-height: 7rem; }
  .history .mark { width: 2.4rem; height: 2.4rem; font-size: 1.7rem; }
  .slot { font-size: clamp(1.4rem, 8vw, 2.1rem); }
  .keys { gap: .25rem; }
  .keys button { height: 2.5rem; }
}

@media (max-height: 30rem) and (min-width: 36rem) {
  #word-dialog { max-width: 54rem; }
  #word-dialog .sheet {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1.1fr);
    grid-template-rows: repeat(3, auto) minmax(0, 1fr) auto;
    column-gap: 1.5rem;
    height: calc(100vh - .75rem);
    height: calc(100dvh - .75rem);
  }
  #word-dialog .sheet > * { grid-column: 1; }
  #word-dialog .letters { justify-self: center; }
  #word-dialog .keys, #word-dialog #done { grid-column: 2; grid-row: 1 / -1; align-self: center; }
}

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