/* DUNCE. Brand tokens match chasedunham.com. */
:root {
  --bg: #1e1510;
  --bg-2: #261b15;
  --surface: #2b2019;
  --surface-2: #35291f;
  --text: #f4ede2;
  --muted: #b9a998;
  --line: rgba(244, 237, 226, .12);

  --tile-empty: #2b2019;
  --tile-filled: #3a2d24;
  --ring: rgba(244, 237, 226, .42);

  /* Results. Lime = correct (bright, rewarding). Sky blue = right letter, wrong place (calm, opposite lime on the
     wheel, and yellow vs blue is the safest pair for colour-blind players). Taupe = absent (recedes). */
  --correct: #e3df52;
  --on-correct: #1e1510;
  --present: #93b3db;
  --on-present: #1e1510;
  --absent: #43362d;
  --on-absent: #cdbfae;

  --key: #6a5a4e;
  --key-text: #f4ede2;
  --key-absent: #2e231c;
  --key-absent-text: #8c7b6c;

  --accent: #e4572e;
  --on-accent: #1e1510;
  --lime: #e3df52;

  --glow: rgba(228, 87, 46, .22);
  --shadow: 0 18px 50px rgba(0, 0, 0, .45);

  --font: "Plus Jakarta Sans", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --serif: "Instrument Serif", ui-serif, Georgia, serif;

  --ease: cubic-bezier(.22, 1, .36, 1);
  --spring: cubic-bezier(.3, 1.5, .5, 1);
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #f4ede2;
  --bg-2: #efe6d8;
  --surface: #eae0d0;
  --surface-2: #e2d6c3;
  --text: #1e1510;
  --muted: #6b5c50;
  --line: rgba(30, 21, 16, .14);
  --tile-empty: #e6dccb;
  --tile-filled: #faf5ec;
  --ring: rgba(30, 21, 16, .4);
  --absent: #c9bba8;
  --on-absent: #4a3d33;
  --key: #d8cbb8;
  --key-text: #1e1510;
  --key-absent: #bfae98;
  --key-absent-text: #7d6e5f;
  --glow: rgba(228, 87, 46, .14);
  --shadow: 0 18px 50px rgba(30, 21, 16, .18);
  color-scheme: light;
}
:root[data-theme="light"] .tile.correct,
:root[data-theme="light"] .tile.present,
:root[data-theme="light"] .tile.absent { box-shadow: inset 0 0 0 2px rgba(30, 21, 16, .16); }

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
  background-image: radial-gradient(60rem 36rem at 90% -10%, var(--glow), transparent 60%);
  background-attachment: fixed;
}
[hidden] { display: none !important; }
button { font: inherit; color: inherit; }
h1, h2, h3, p { margin: 0; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.skip { position: absolute; left: -999px; top: 0; background: var(--accent); color: var(--on-accent); padding: .5rem 1rem; z-index: 100; }
.skip:focus { left: 1rem; top: 1rem; }
.sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0; }

/* ---------- App shell ---------- */
.app {
  height: 100dvh;
  max-width: 36rem;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) max(env(safe-area-inset-right), .75rem) env(safe-area-inset-bottom) max(env(safe-area-inset-left), .75rem);
}

.topbar { display: flex; align-items: center; justify-content: space-between; gap: .5rem; padding: .6rem 0 .25rem; }
.logo { font-weight: 800; font-size: 1.7rem; line-height: 1; letter-spacing: -.02em; }
.sub { margin-top: .15rem; font-size: .78rem; letter-spacing: .06em; text-transform: uppercase; color: var(--muted); }
.topbar__actions { display: flex; gap: .15rem; }
.icon-btn {
  width: 2.75rem; height: 2.75rem; display: grid; place-items: center; border: 0; border-radius: 14px;
  background: transparent; cursor: pointer; transition: background-color .2s ease, transform .12s ease;
}
.icon-btn:hover { background: var(--line); }
.icon-btn:active { transform: scale(.92); }
.icon-btn svg { width: 1.45rem; height: 1.45rem; fill: currentColor; }

.controls { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .35rem 0 .6rem; }
.seg { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.seg__btn {
  border: 0; background: transparent; padding: .45rem 1rem; border-radius: 999px; cursor: pointer;
  font-weight: 700; font-size: .85rem; color: var(--muted); transition: background-color .25s ease, color .25s ease;
}
.seg__btn[aria-pressed="true"] { background: var(--text); color: var(--bg); }
.chip {
  display: inline-flex; align-items: center; gap: .4rem; border: 1px solid var(--line); background: var(--surface);
  border-radius: 999px; padding: .42rem .8rem; font-weight: 700; font-size: .85rem; cursor: pointer;
  transition: background-color .2s ease, transform .12s ease;
}
.chip:hover { background: var(--surface-2); }
.chip:active { transform: scale(.96); }
.chip svg { width: .8rem; height: .8rem; fill: currentColor; opacity: .7; }
.chip[data-locked="true"] { cursor: default; opacity: .8; }

/* ---------- Board ---------- */
.stage { flex: 1 1 auto; min-height: 0; display: grid; place-items: center; position: relative; container-type: size; }
.board {
  --gap: clamp(4px, 1.6cqmin, 8px);
  --rows: 6;
  container-type: inline-size;
  width: min(100cqw, calc((100cqh - (var(--rows) - 1) * var(--gap)) * 5 / var(--rows) + 4 * var(--gap)), 26rem);
  display: grid;
  gap: var(--gap);
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: var(--gap); }

.tile {
  position: relative;
  aspect-ratio: 1;
  display: grid; place-items: center;
  border-radius: 12px;
  background: var(--tile-empty);
  color: var(--text);
  font-weight: 800; text-transform: uppercase;
  font-size: 10.5cqw; line-height: 1;
  user-select: none; -webkit-user-select: none;
  transition: background-color .2s ease, box-shadow .2s ease, color .2s ease, opacity .25s ease;
}
.tile.filled { background: var(--tile-filled); box-shadow: inset 0 0 0 2px var(--ring); }
.tile.cursor { background: var(--tile-filled); box-shadow: inset 0 0 0 2px var(--text); }
.tile.locked { box-shadow: inset 0 0 0 2px var(--correct); color: var(--correct); }
.tile.correct { --to-bg: var(--correct); --to-fg: var(--on-correct); background: var(--correct); color: var(--on-correct); box-shadow: none; }
.tile.present { --to-bg: var(--present); --to-fg: var(--on-present); background: var(--present); color: var(--on-present); box-shadow: none; }
.tile.absent  { --to-bg: var(--absent);  --to-fg: var(--on-absent);  background: var(--absent);  color: var(--on-absent);  box-shadow: none; }

/* A letter lands with a small springy pop. */
.tile.pop { animation: pop .17s var(--spring); }
@keyframes pop { 0% { transform: scale(.86); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }

/* A scored tile turns over like a card, one after another (--i is its place in the row). */
.tile.reveal { animation: flip .56s ease-in-out calc(var(--i, 0) * 210ms) both; }
@keyframes flip {
  0%   { transform: perspective(450px) rotateX(0);     background: var(--tile-filled); color: var(--text); box-shadow: inset 0 0 0 2px var(--ring); }
  45%  { transform: perspective(450px) rotateX(90deg); background: var(--tile-filled); color: var(--text); box-shadow: inset 0 0 0 2px var(--ring); }
  55%  { transform: perspective(450px) rotateX(90deg); background: var(--to-bg);       color: var(--to-fg); box-shadow: none; }
  100% { transform: perspective(450px) rotateX(0);     background: var(--to-bg);       color: var(--to-fg); box-shadow: none; }
}
/* The winning row hops, left to right. */
.tile.win { animation: hop .55s cubic-bezier(.3, .7, .4, 1) calc(var(--i, 0) * 90ms) both; }
@keyframes hop { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-14%); } 70% { transform: translateY(2%); } }
/* The board arrives with a soft rise. */
.board.enter .row { animation: rise .5s var(--ease) both; animation-delay: calc(var(--r, 0) * 45ms); }
@keyframes rise { from { opacity: 0; transform: translateY(10px); } to { opacity: 1; transform: none; } }

/* Colour-blind marks: a dot on right-letter-wrong-place, a bar on correct. */
:root[data-marks="on"] .tile.present::after { content: ""; position: absolute; top: 9%; right: 9%; width: 15%; aspect-ratio: 1; border-radius: 50%; background: currentColor; opacity: .75; }
:root[data-marks="on"] .tile.correct::after { content: ""; position: absolute; left: 22%; right: 22%; bottom: 9%; height: 4px; border-radius: 4px; background: currentColor; opacity: .75; }

/* ---------- Toast ---------- */
.toast {
  position: absolute; left: 50%; top: .25rem; transform: translate(-50%, -8px); opacity: 0; pointer-events: none;
  max-width: calc(100% - 1rem); padding: .55rem 1rem; border-radius: 999px; z-index: 5;
  background: var(--text); color: var(--bg); font-weight: 700; font-size: .85rem; text-align: center;
  box-shadow: var(--shadow);
  transition: opacity .2s ease, transform .3s var(--ease);
}
.toast.is-show { opacity: 1; transform: translate(-50%, 0); }
.toast.is-warn { background: var(--accent); color: var(--on-accent); }

/* ---------- Dock: keyboard, letter map, end card ---------- */
.dock { padding: .5rem 0 .75rem; display: grid; gap: .5rem; align-content: end; min-height: 11.5rem; } /* steady height, so the board doesn't resize when the result card replaces the keys */
.kb { display: grid; gap: .4rem; }
.kb__row { display: flex; justify-content: center; gap: .3rem; }
.key {
  flex: 1 1 0; min-width: 0; height: clamp(46px, 7.4dvh, 60px);
  border: 0; border-radius: 10px; background: var(--key); color: var(--key-text);
  font-weight: 800; font-size: 1.05rem; text-transform: uppercase; cursor: pointer; touch-action: manipulation;
  display: grid; place-items: center; padding: 0;
  transition: background-color .4s ease, color .4s ease, transform .1s ease;
}
.key:active { transform: scale(.92); }
.key--wide { flex: 1.55 1 0; font-size: .8rem; letter-spacing: .04em; }
.key--enter { background: var(--accent); color: var(--on-accent); }
.key--back svg { width: 1.4rem; height: 1.4rem; fill: currentColor; }
.key.is-correct { background: var(--correct); color: var(--on-correct); }
.key.is-present { background: var(--present); color: var(--on-present); }
.key.is-absent  { background: var(--key-absent); color: var(--key-absent-text); }

/* Desktop with a real keyboard: a compact letter strip instead of keys. */
.lettermap { display: flex; flex-wrap: wrap; justify-content: center; gap: .3rem; padding: .25rem 0; }
.lm {
  width: 1.9rem; height: 2.1rem; border: 0; border-radius: 8px; background: var(--key); color: var(--key-text);
  font-weight: 800; font-size: .85rem; text-transform: uppercase; cursor: pointer; display: grid; place-items: center;
  transition: background-color .4s ease, color .4s ease, transform .1s ease;
}
.lm:active { transform: scale(.9); }
.lm.is-correct { background: var(--correct); color: var(--on-correct); }
.lm.is-present { background: var(--present); color: var(--on-present); }
.lm.is-absent  { background: var(--key-absent); color: var(--key-absent-text); }
.typehint { text-align: center; color: var(--muted); font-size: .8rem; }
.typehint kbd { font: inherit; font-weight: 700; padding: .05rem .4rem; border-radius: 6px; background: var(--surface); border: 1px solid var(--line); }

.endcard { display: grid; gap: .7rem; padding: .25rem 0 .1rem; animation: rise .5s var(--ease) both; }
.endcard__title { font-family: var(--serif); font-size: 2.4rem; line-height: 1; letter-spacing: -.01em; }
.endcard__title em { color: var(--accent); font-style: italic; }
.endcard__sub { color: var(--muted); font-size: .92rem; }
.endcard__sub strong { color: var(--text); letter-spacing: .06em; }
.endcard__actions { display: flex; gap: .5rem; flex-wrap: wrap; }
.endcard__next { color: var(--muted); font-size: .85rem; }
.endcard__next b { color: var(--text); font-variant-numeric: tabular-nums; }

/* ---------- Buttons and form bits ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: .5rem; border: 0; cursor: pointer;
  padding: .75rem 1.15rem; border-radius: 999px; font-weight: 800; font-size: .92rem; text-decoration: none;
  background: var(--accent); color: var(--on-accent);
  transition: transform .15s var(--ease), background-color .2s ease, opacity .2s ease;
}
.btn:hover { transform: translateY(-2px); }
.btn:active { transform: scale(.97); }
.btn--ghost { background: transparent; color: var(--text); box-shadow: inset 0 0 0 1.5px var(--line); }
.btn--ghost:hover { background: var(--line); }
.btn--lime { background: var(--lime); color: var(--on-correct); }
.btn--small { padding: .5rem .85rem; font-size: .82rem; }
.btn[disabled] { opacity: .5; pointer-events: none; }
.field { display: grid; gap: .35rem; }
.field label { font-size: .8rem; font-weight: 700; color: var(--muted); letter-spacing: .04em; text-transform: uppercase; }
.input {
  width: 100%; padding: .8rem .95rem; border-radius: 14px; border: 1.5px solid var(--line); background: var(--surface);
  color: var(--text); font: inherit; font-size: 1rem; transition: border-color .2s ease;
}
.input:focus { outline: none; border-color: var(--lime); }
.row-inline { display: flex; gap: .5rem; align-items: stretch; }
.row-inline .input { flex: 1; min-width: 0; }
.toggle { position: relative; width: 3rem; height: 1.8rem; flex: none; }
.toggle input { position: absolute; inset: 0; opacity: 0; margin: 0; cursor: pointer; z-index: 1; }
.toggle span { position: absolute; inset: 0; border-radius: 999px; background: var(--surface-2); transition: background-color .25s ease; }
.toggle span::after { content: ""; position: absolute; top: 3px; left: 3px; width: calc(1.8rem - 6px); height: calc(1.8rem - 6px); border-radius: 50%; background: var(--text); transition: transform .3s var(--spring); }
.toggle input:checked + span { background: var(--lime); }
.toggle input:checked + span::after { transform: translateX(1.2rem); background: var(--on-correct); }
.toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
.setting { display: flex; align-items: center; justify-content: space-between; gap: 1rem; padding: .85rem 0; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: 0; }
.setting b { display: block; font-weight: 700; }
.setting small { color: var(--muted); }
.opts { display: inline-flex; padding: 3px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.opts button { border: 0; background: transparent; padding: .35rem .8rem; border-radius: 999px; font-weight: 700; font-size: .8rem; color: var(--muted); cursor: pointer; }
.opts button[aria-pressed="true"] { background: var(--text); color: var(--bg); }

/* ---------- Sheets ---------- */
.scrim { position: fixed; inset: 0; z-index: 20; background: rgba(10, 6, 4, .55); backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px); animation: fade .25s ease both; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.sheet { position: fixed; inset: 0; z-index: 21; display: grid; align-items: end; justify-items: center; pointer-events: none; }
.sheet[hidden], .scrim[hidden] { display: none; }
.sheet__panel {
  pointer-events: auto; width: min(100%, 32rem); max-height: 90dvh; display: flex; flex-direction: column;
  background: var(--bg-2); color: var(--text); border-radius: 26px 26px 0 0; border: 1px solid var(--line); border-bottom: 0;
  box-shadow: var(--shadow); animation: slide-up .45s var(--ease) both;
  padding-bottom: env(safe-area-inset-bottom);
}
@keyframes slide-up { from { transform: translateY(100%); } to { transform: none; } }
.sheet__head { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.1rem .4rem 1.4rem; }
.sheet__head h2 { font-family: var(--serif); font-weight: 400; font-size: 1.9rem; line-height: 1; }
.sheet__body { overflow-y: auto; padding: .4rem 1.4rem 1.4rem; display: grid; gap: 1rem; align-content: start; overscroll-behavior: contain; }
.sheet__grip { width: 2.4rem; height: 4px; border-radius: 4px; background: var(--line); margin: .6rem auto 0; }
@media (min-width: 700px) {
  .sheet { align-items: center; }
  .sheet__panel { border-radius: 26px; border-bottom: 1px solid var(--line); animation: pop-in .35s var(--ease) both; }
  .sheet__grip { display: none; }
}
@keyframes pop-in { from { opacity: 0; transform: translateY(14px) scale(.97); } to { opacity: 1; transform: none; } }

.tabs { display: flex; gap: .25rem; padding: 3px; border-radius: 999px; background: var(--surface); border: 1px solid var(--line); }
.tab { flex: 1; border: 0; background: transparent; padding: .5rem .5rem; border-radius: 999px; font-weight: 700; font-size: .85rem; color: var(--muted); cursor: pointer; }
.tab[aria-selected="true"] { background: var(--text); color: var(--bg); }

.card { background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 1rem; display: grid; gap: .6rem; }
.card h3 { font-size: 1rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.hint { color: var(--muted); font-size: .88rem; }
.pillrow { display: flex; flex-wrap: wrap; gap: .4rem; }
.pill { display: inline-flex; align-items: center; gap: .3rem; padding: .2rem .65rem; border-radius: 999px; background: var(--surface-2); font-size: .78rem; font-weight: 700; }

/* Stats */
.statgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: .5rem; }
.stat { text-align: center; padding: .7rem .2rem; border-radius: 16px; background: var(--surface); border: 1px solid var(--line); }
.stat b { display: block; font-size: 1.7rem; line-height: 1.1; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat span { font-size: .68rem; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); }
.stat--hot b { color: var(--lime); }
.dist { display: grid; gap: .35rem; }
.dist__row { display: grid; grid-template-columns: 1rem 1fr; gap: .5rem; align-items: center; font-size: .85rem; font-weight: 700; }
.dist__bar { min-width: 1.8rem; height: 1.5rem; border-radius: 8px; background: var(--absent); color: var(--on-absent); display: flex; align-items: center; justify-content: flex-end; padding: 0 .5rem; font-size: .8rem; transform-origin: left; animation: grow .6s var(--ease) both; }
.dist__bar.is-last { background: var(--correct); color: var(--on-correct); }
@keyframes grow { from { transform: scaleX(.05); } to { transform: scaleX(1); } }
.list { display: grid; }
.list__row { display: flex; align-items: center; justify-content: space-between; gap: .75rem; padding: .6rem 0; border-bottom: 1px solid var(--line); font-size: .92rem; }
.list__row:last-child { border-bottom: 0; }
.res { display: inline-flex; align-items: center; justify-content: center; min-width: 2.6rem; padding: .15rem .6rem; border-radius: 999px; font-weight: 800; font-size: .8rem; background: var(--correct); color: var(--on-correct); }
.res--loss { background: var(--absent); color: var(--on-absent); }
.res--wait { background: transparent; box-shadow: inset 0 0 0 1.5px var(--line); color: var(--muted); }

/* Crew board */
.rank { display: grid; grid-template-columns: 1.7rem 1fr auto; gap: .65rem; align-items: center; padding: .65rem .7rem; border-radius: 16px; background: var(--surface); border: 1px solid var(--line); }
.rank.is-you { box-shadow: inset 0 0 0 1.5px var(--lime); }
.rank__n { width: 1.7rem; height: 1.7rem; border-radius: 50%; display: grid; place-items: center; font-weight: 800; font-size: .8rem; background: var(--surface-2); }
.rank__n.is-first { background: var(--lime); color: var(--on-correct); }
.rank__name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.rank__name small { color: var(--muted); font-weight: 600; margin-left: .35rem; }
.code { font-weight: 800; letter-spacing: .25em; font-size: 1.4rem; font-variant-numeric: tabular-nums; }

.example { display: flex; gap: 4px; }
.example .tile { width: 2.3rem; font-size: 1.15rem; border-radius: 9px; }

.confetti { position: fixed; inset: 0; width: 100%; height: 100%; pointer-events: none; z-index: 30; }

@media (prefers-reduced-motion: reduce) {
  .tile, .key, .lm, .icon-btn, .btn, .seg__btn, .chip { transition: none; }
  .tile.pop, .tile.reveal, .tile.win, .board.enter .row, .endcard, .dist__bar, .sheet__panel, .scrim { animation: none; }
}
:root[data-motion="off"] .tile.pop, :root[data-motion="off"] .tile.reveal, :root[data-motion="off"] .tile.win,
:root[data-motion="off"] .board.enter .row, :root[data-motion="off"] .endcard, :root[data-motion="off"] .dist__bar,
:root[data-motion="off"] .sheet__panel, :root[data-motion="off"] .scrim { animation: none; }
:root[data-motion="off"] .tile, :root[data-motion="off"] .key, :root[data-motion="off"] .lm { transition: none; }

@media (min-width: 900px) {
  .app { max-width: 38rem; }
  .logo { font-size: 1.9rem; }
}
