:root {
  color-scheme: light;

  --paper: #faf7f2;
  --paper-2: #f2ede4;
  --card: #ffffff;
  --ink: #24211c;
  --ink-soft: #6b6459;
  --line: #e6ddd0;
  --green: #6aaa64;
  --green-dark: #4a8a44;
  --yellow: #c9b458;
  --gray: #787c7e;
  --danger: #d64545;
  --key: #e8e2d7;

  /* Accents as *text* need more lift on a dark surface than accents as *fill*
     do, so the two are separate. The tile/key fills keep using --green etc. */
  --green-text: var(--green-dark);
  --danger-text: var(--danger);

  /* Translucent tints and shadows go through an RGB triplet so they follow
     the palette swap instead of staying stuck at light values. */
  --shadow-rgb: 36, 33, 28;
  --backdrop-a: 0.4;
  --danger-rgb: 214, 69, 69;

  /* V2 title badge: the darker "pressed" edge under the tile. */
  --v2-edge: #3d7438;

  /* Seasonal decorations. Snow can't be white on a light page. */
  --snow: #9db5ca;
  --heart: #e0587e;
}

:root[data-theme="dark"] {
  color-scheme: dark;

  --paper: #16130f;
  --paper-2: #262119;
  --card: #1e1a15;
  --ink: #f2ede4;
  --ink-soft: #a79d8e;
  --line: #38312a;
  /* Absent tiles should recede against a dark board, not glow. --key has to
     stay *lighter* than --gray here so eliminated keys still read as dimmed. */
  --gray: #56514b;
  --key: #6f6860;

  --green-text: #8bc784;
  --danger-text: #e86a6a;

  --shadow-rgb: 0, 0, 0;
  --backdrop-a: 0.6;
  --danger-rgb: 232, 106, 106;

  --v2-edge: #2a5226;

  --snow: #e8eff6;
  --heart: #ff7d9d;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must win over any `display` rule below. */
[hidden] { display: none !important; }

html, body { margin: 0; padding: 0; height: 100%; }

/* Kill double-tap-to-zoom (the viewport meta only covers pinch). */
html { touch-action: manipulation; -webkit-text-size-adjust: 100%; }

body {
  background: var(--paper);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji";
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* A fixed viewport height (not min-height) so .play has a definite size that
   app.js can fit the board into, whatever the word length. */
.wrap {
  max-width: 520px;
  margin: 0 auto;
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: 1rem 1rem 0.75rem;
}

/* --- header --- */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding-bottom: 0.75rem;
  border-bottom: 1px solid var(--line);
  margin-bottom: 0.75rem;
}
/* Both sides are the same width so the title stays optically centred. */
.hdr-side {
  flex: none;
  display: flex;
  gap: 0.375rem;
  min-width: 86px;
}
.hdr-side:last-child { justify-content: flex-end; }

.title { text-align: center; min-width: 0; }
/* "Rordle" is styled exactly like V1's title; only the badge is new. */
.title h1 {
  margin: 0;
  font-size: 1.8rem;
  font-weight: 800;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  white-space: nowrap;
  line-height: 1.5;
}
.title p {
  margin: 0;
  font-size: 0.8rem;
  color: var(--ink-soft);
}

/* The V2 badge: a tilted, glossy green-to-gold tile, raised off the baseline. */
.title .v2 {
  position: relative;
  display: inline-block;
  overflow: hidden;
  margin-left: -0.02em;
  padding: 0.2em 0.36em 0.16em 0.3em;
  font-size: 0.5em;
  font-weight: 900;
  font-style: italic;
  letter-spacing: 0.03em;
  line-height: 1;
  vertical-align: middle;
  color: #fff;
  background: linear-gradient(135deg, var(--green) 0%, var(--green-dark) 55%, var(--yellow) 120%);
  border-radius: 0.34em;
  box-shadow: 0 0.14em 0 var(--v2-edge), 0 0.5em 0.9em -0.4em rgba(var(--shadow-rgb), 0.55);
  transform: translateY(-0.55em) rotate(-9deg);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.18);
}
/* A sheen sweeps across every few seconds. */
.title .v2::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(100deg, transparent 30%, rgba(255, 255, 255, 0.6) 50%, transparent 70%);
  transform: translateX(-130%);
  animation: sheen 5s ease-in-out 1.2s infinite;
}

.hdr-btn {
  flex: none;
  width: 40px;
  height: 40px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--ink-soft);
  font-size: 1.05rem;
  font-weight: 700;
  cursor: pointer;
  transition: color 0.15s ease, border-color 0.15s ease, transform 0.15s ease;
}
/* Show the icon for the theme you'd switch *to*. */
#themeBtn .icon-sun { display: none; }
:root[data-theme="dark"] #themeBtn .icon-sun { display: block; }
:root[data-theme="dark"] #themeBtn .icon-moon { display: none; }

.hdr-btn:hover {
  color: var(--green-text);
  border-color: var(--green);
  transform: translateY(-1px);
}

/* --- toast --- */
.toast {
  position: fixed;
  top: 4.5rem;
  left: 50%;
  transform: translateX(-50%);
  background: var(--ink);
  color: var(--paper);
  font-weight: 600;
  font-size: 0.9rem;
  padding: 0.55rem 1rem;
  border-radius: 10px;
  box-shadow: 0 10px 24px -10px rgba(var(--shadow-rgb), 0.5);
  z-index: 60;
  white-space: nowrap;
}
.toast.error { background: var(--danger); color: white; }

/* --- play area --- */
main {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.75rem;
}
.play {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 12px; /* keep in sync with fitBoard() in app.js */
}
.topline {
  flex: none;
  height: 2rem;
  display: flex;
  align-items: center;
}
.badge {
  font-size: 0.78rem;
  color: var(--ink-soft);
  padding: 0.15rem 0.75rem;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--card);
  white-space: nowrap;
}
.badge b { color: var(--ink); }
.badge .sandbox {
  margin-left: 0.35rem;
  padding: 0 0.4rem;
  border-radius: 999px;
  background: var(--yellow);
  color: white;
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

/* --- board: --cols, --rows and --tile are set by app.js --- */
.board {
  --tile: 56px;
  display: grid;
  grid-template-rows: repeat(var(--rows, 6), var(--tile));
  gap: 6px; /* keep in sync with GAP_PX in app.js */
}
.row {
  display: grid;
  grid-template-columns: repeat(var(--cols, 5), var(--tile));
  gap: 6px;
}
.row.shake { animation: shake 0.5s; }

.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: calc(var(--tile) * 0.5);
  font-weight: 800;
  line-height: 1;
  text-transform: uppercase;
  border: 2px solid var(--line);
  border-radius: calc(var(--tile) * 0.13);
  background: var(--card);
  color: var(--ink);
  user-select: none;
  transition: transform 0.08s ease;
}
.tile.filled {
  border-color: var(--ink-soft);
  animation: pop 0.1s ease;
}
.tile.flip { animation: flip 0.5s ease forwards; }
.tile.correct, .tile.present, .tile.absent {
  border-color: transparent;
  color: white;
}
.tile.correct { background: var(--green); }
.tile.present { background: var(--yellow); }
.tile.absent  { background: var(--gray); }
.tile.win { animation: bounce 0.9s ease; }

/* small tiles in the help modal */
.tile.small {
  width: 2.1rem;
  height: 2.1rem;
  font-size: 1.1rem;
  display: inline-flex;
  border-radius: 6px;
}

/* --- reveal (loss) banner, sits where the badge was --- */
.reveal {
  background: rgba(var(--danger-rgb), 0.1);
  border: 1px solid rgba(var(--danger-rgb), 0.35);
  color: var(--danger-text);
  border-radius: 999px;
  padding: 0.1rem 0.9rem;
  font-size: 0.88rem;
  white-space: nowrap;
}
.reveal strong { text-transform: uppercase; letter-spacing: 0.06em; }

/* --- keyboard --- */
.keyboard {
  flex: none;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-bottom: 0.25rem;
}
.kb-row {
  display: flex;
  justify-content: center;
  gap: 5px;
}
.key {
  flex: 1;
  max-width: 44px;
  height: 54px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 8px;
  background: var(--key);
  color: var(--ink);
  font: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  user-select: none;
  transition: filter 0.1s ease;
  padding: 0;
}
.key:active { filter: brightness(0.9); }
.key.wide {
  flex: 1.6;
  max-width: 72px;
  font-size: 0.72rem;
}
.key.correct { background: var(--green); color: white; }
.key.present { background: var(--yellow); color: white; }
.key.absent  { background: var(--gray); color: white; }

/* --- footer --- */
footer {
  flex: none;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 0.75rem;
  padding: 0.5rem 0 0.2rem;
  font-size: 0.78rem;
  color: var(--ink-soft);
}
footer a { color: var(--ink-soft); text-decoration: none; }
footer a:hover { color: var(--ink); }
.season-strip {
  font-size: 0.95rem;
  line-height: 1;
  letter-spacing: 0.2em;
  white-space: nowrap;
}

/* --- seasonal decorations --- */
.season {
  position: fixed;
  inset: 0;
  overflow: hidden;
  pointer-events: none;
  z-index: 5; /* over the board, under modals (50), toast (60) and debug (40) */
}
.particle {
  position: absolute;
  top: 0;
  width: var(--size);
  height: var(--size);
  animation-iteration-count: infinite;
  will-change: transform;
}
.particle svg { display: block; width: 100%; height: 100%; }
.flake { color: var(--snow); animation-name: fall; animation-timing-function: linear; }
.heart { color: var(--heart); animation-name: rise; animation-timing-function: ease-in-out; }

/* Pumpkins and beach things sit in the side gutters, only once there are
   gutters to sit in; on phones they live in the footer strip instead. */
.corner {
  position: absolute;
  bottom: 0;
  display: none;
  align-items: flex-end;
  gap: 0.3rem;
  padding: 0 1.25rem 0.6rem;
  line-height: 1;
}
.corner.left { left: 0; }
.corner.right { right: 0; flex-direction: row-reverse; }
.corner span { display: inline-block; transform-origin: 50% 90%; }
.corner span:nth-child(1) { font-size: 3.2rem; }
.corner span:nth-child(2) { font-size: 2.3rem; }
.corner span:nth-child(3) { font-size: 1.7rem; }
@media (min-width: 760px) {
  .corner { display: flex; }
}
.corner.pumpkins span { animation: wobble 3.4s ease-in-out infinite; }
.corner.pumpkins span:nth-child(2) { animation-delay: -1.2s; }
.corner.pumpkins span:nth-child(3) { animation-delay: -2.3s; }
.corner.beach span:nth-child(2) { animation: bob 4s ease-in-out infinite; }

/* --- debug menu (?debug) --- */
.debug {
  position: fixed;
  left: 12px;
  bottom: 12px;
  z-index: 40;
  font-size: 0.8rem;
}
.debug-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--line);
  background: var(--card);
  font-size: 1.15rem;
  cursor: pointer;
  box-shadow: 0 6px 16px -8px rgba(var(--shadow-rgb), 0.5);
}
.debug-panel {
  position: absolute;
  left: 0;
  bottom: 50px;
  width: 232px;
  display: flex;
  flex-direction: column;
  gap: 0.6rem;
  padding: 0.8rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--card);
  color: var(--ink);
  box-shadow: 0 16px 36px -16px rgba(var(--shadow-rgb), 0.55);
}
.dbg-title { font-weight: 800; text-transform: uppercase; letter-spacing: 0.08em; color: var(--ink-soft); font-size: 0.7rem; }
.dbg-field { display: flex; flex-direction: column; gap: 0.25rem; color: var(--ink-soft); }
.dbg-field select { font: inherit; color: var(--ink); background: var(--paper); border: 1px solid var(--line); border-radius: 6px; padding: 0.2rem; }
.dbg-lens { display: flex; gap: 4px; }
.dbg-lens button, .dbg-reset {
  flex: 1;
  font: inherit;
  padding: 0.25rem 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--paper);
  color: var(--ink);
  cursor: pointer;
}
.dbg-lens button.active { background: var(--green); border-color: var(--green); color: white; }
.dbg-check { display: flex; align-items: center; gap: 0.35rem; }
.dbg-check code { margin-left: auto; font-weight: 700; text-transform: uppercase; }
.dbg-info { color: var(--ink-soft); font-size: 0.72rem; }

/* --- modals --- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(var(--shadow-rgb), var(--backdrop-a));
  display: flex;
  align-items: flex-start;
  justify-content: center;
  overflow-y: auto;
  padding: max(1.5rem, 6vh) 1.5rem 1.5rem;
  z-index: 50;
}
.modal {
  position: relative;
  background: var(--card);
  border-radius: 18px;
  padding: 1.75rem;
  width: 100%;
  max-width: 26rem;
  margin: auto;
  box-shadow: 0 24px 60px -20px rgba(var(--shadow-rgb), 0.4);
}
.modal-close {
  position: absolute;
  top: 0.6rem;
  right: 0.7rem;
  width: 34px;
  height: 34px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  background: none;
  border-radius: 9px;
  color: var(--ink-soft);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { background: var(--paper-2); color: var(--ink); }
.modal h2 { margin: 0 0 0.6rem; font-size: 1.3rem; letter-spacing: -0.01em; }
.modal h3 { margin: 1.25rem 0 0.5rem; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--ink-soft); }
.modal .muted { margin: 0 0 1rem; color: var(--ink-soft); font-size: 0.9rem; }
.modal .muted strong { color: var(--ink); }

.example { border-top: 1px solid var(--line); padding-top: 1rem; }
.example p { margin: 0.4rem 0 1rem; font-size: 0.85rem; color: var(--ink-soft); }
.ex-row { display: flex; gap: 4px; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  text-align: center;
}
.stat { display: flex; flex-direction: column; }
.stat-num { font-size: 1.7rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-label { font-size: 0.7rem; color: var(--ink-soft); text-transform: uppercase; letter-spacing: 0.04em; }

.dist { display: flex; flex-direction: column; gap: 4px; }
.dist-row { display: flex; align-items: center; gap: 0.5rem; font-size: 0.85rem; }
.dist-row .n { width: 0.8rem; color: var(--ink-soft); font-weight: 700; }
.dist-bar {
  min-width: 1.4rem;
  padding: 0.1rem 0.5rem;
  border-radius: 5px;
  background: var(--gray);
  color: white;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
}
.dist-bar.hit { background: var(--green); }

.btn.share {
  display: block;
  width: 100%;
  margin-top: 1.25rem;
  border: none;
  border-radius: 10px;
  padding: 0.7rem 1.1rem;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  background: linear-gradient(155deg, var(--green), var(--green-dark));
  color: white;
}
.btn.share:hover { filter: brightness(1.05); }

/* --- animations --- */
@keyframes pop {
  50% { transform: scale(1.1); }
}
@keyframes flip {
  0%   { transform: rotateX(0); }
  50%  { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-24px); }
  55% { transform: translateY(4px); }
  70% { transform: translateY(-12px); }
  82% { transform: translateY(2px); }
  92% { transform: translateY(-4px); }
  100% { transform: translateY(0); }
}
@keyframes sheen {
  0%, 55% { transform: translateX(-130%); }
  80%, 100% { transform: translateX(130%); }
}
@keyframes fall {
  0%   { transform: translate3d(0, -6vh, 0) rotate(0deg); }
  50%  { transform: translate3d(var(--sway), 50vh, 0) rotate(180deg); }
  100% { transform: translate3d(0, 106vh, 0) rotate(360deg); }
}
@keyframes rise {
  0%   { transform: translate3d(0, 106vh, 0) scale(0.85); }
  50%  { transform: translate3d(var(--sway), 50vh, 0) scale(1); }
  100% { transform: translate3d(0, -6vh, 0) scale(0.85); }
}
@keyframes wobble {
  0%, 100% { transform: rotate(-7deg); }
  50% { transform: rotate(7deg); }
}
@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

/* Still scattered, just not moving. */
@media (prefers-reduced-motion: reduce) {
  .particle { top: var(--y); animation: none; }
  .corner span, .title .v2::after { animation: none !important; }
}

@media (max-width: 420px) {
  .key { height: 50px; }
  /* Three buttons plus a centred title with its badge is tight here. */
  header { gap: 0.4rem; }
  .hdr-btn { width: 34px; height: 34px; font-size: 0.95rem; }
  .hdr-side { min-width: 74px; gap: 0.25rem; }
  .title h1 { font-size: 1.6rem; }
}
