/* ---------------------------------------------------------------
   Five — an English word game
   Palette is defined once on :root (light), then re-stated for the
   two dark cases: system-dark without an explicit override, and an
   explicit dark choice.
   --------------------------------------------------------------- */

:root {
  --bg: #f7f8fa;
  --bg-elev: #ffffff;
  --ink: #16181d;
  --ink-soft: #5c6473;
  --ink-faint: #8d95a3;
  --line: #d5d9e0;
  --line-soft: #e6e9ee;
  --key: #dfe3ea;
  --key-ink: #16181d;
  --key-hover: #ccd2dc;
  --key-off: #9ba2ae;
  --key-off-ink: #ffffff;
  --correct: #2f9e63;
  --present: #d29a22;
  --absent: #848b98;
  --on-filled: #ffffff;
  --accent: #2f9e63;
  --shadow: 0 10px 30px rgba(16, 20, 30, .12);
  --radius: 10px;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12141a;
    --bg-elev: #1b1e26;
    --ink: #eef1f6;
    --ink-soft: #a2abbb;
    --ink-faint: #79818f;
    --line: #333947;
    --line-soft: #262b35;
    --key: #3a4150;
    --key-ink: #eef1f6;
    --key-hover: #4a5263;
    --key-off: #23272f;
    --key-off-ink: #6d7583;
    --correct: #35a76c;
    --present: #cf9f33;
    --absent: #4b515e;
    --shadow: 0 10px 30px rgba(0, 0, 0, .45);
  }
}

:root[data-theme="dark"] {
  --bg: #12141a;
  --bg-elev: #1b1e26;
  --ink: #eef1f6;
  --ink-soft: #a2abbb;
  --ink-faint: #79818f;
  --line: #333947;
  --line-soft: #262b35;
  --key: #3a4150;
  --key-ink: #eef1f6;
  --key-hover: #4a5263;
  --key-off: #23272f;
  --key-off-ink: #6d7583;
  --correct: #35a76c;
  --present: #cf9f33;
  --absent: #4b515e;
  --shadow: 0 10px 30px rgba(0, 0, 0, .45);
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

button { font: inherit; color: inherit; }

.app {
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  padding: 0 12px env(safe-area-inset-bottom);
  display: flex;
  flex-direction: column;
}

/* ---------- top bar ---------- */

.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  padding: 10px 0 8px;
  border-bottom: 1px solid var(--line-soft);
}

.topbar-right { display: flex; justify-content: flex-end; gap: 2px; }

.wordmark {
  margin: 0;
  text-align: center;
  font-size: 1.45rem;
  font-weight: 800;
  letter-spacing: .06em;
  text-transform: uppercase;
  line-height: 1;
}

.wordmark-sub {
  display: block;
  margin-top: 3px;
  font-size: .58rem;
  font-weight: 500;
  letter-spacing: .14em;
  color: var(--ink-faint);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-soft);
  cursor: pointer;
  transition: background .15s, color .15s;
}

.icon-btn:hover { background: var(--line-soft); color: var(--ink); }

.icon-btn svg {
  width: 21px;
  height: 21px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.ico-moon { display: none; }
:root[data-theme="dark"] .ico-sun { display: none; }
:root[data-theme="dark"] .ico-moon { display: block; }

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .ico-sun { display: none; }
  :root:not([data-theme="light"]) .ico-moon { display: block; }
}

/* ---------- mode switch ---------- */

.modes {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 2px 4px;
}

.segmented {
  display: inline-flex;
  padding: 3px;
  gap: 2px;
  border-radius: 999px;
  background: var(--line-soft);
}

.seg {
  border: 0;
  padding: 5px 15px;
  border-radius: 999px;
  background: transparent;
  color: var(--ink-soft);
  font-size: .82rem;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s, color .15s;
}

.seg.is-active {
  background: var(--bg-elev);
  color: var(--ink);
  box-shadow: 0 1px 3px rgba(0, 0, 0, .14);
}

.puzzle-id {
  margin: 0;
  font-size: .72rem;
  font-variant-numeric: tabular-nums;
  letter-spacing: .06em;
  color: var(--ink-faint);
}

/* ---------- play area ---------- */

/* "safe center" centers the board when there is room, but falls back to
   top-aligned scrolling on short screens instead of clipping the first row. */
.play {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: safe center;
  overflow-y: auto;
}

.toast-slot { position: relative; height: 0; width: 100%; z-index: 5; }

.toast {
  position: absolute;
  top: 6px;
  left: 50%;
  transform: translate(-50%, -6px);
  padding: 9px 16px;
  border-radius: 8px;
  background: var(--ink);
  color: var(--bg);
  font-size: .85rem;
  font-weight: 600;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .18s, transform .18s;
}

.toast.show { opacity: 1; transform: translate(-50%, 0); }

.board-wrap {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  padding: 8px 0;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: var(--gap);
  --gap: 6px;
  --tile: clamp(34px, min(16.5vw, 8.4dvh), 62px);
}

.row {
  display: grid;
  grid-template-columns: repeat(5, var(--tile));
  gap: var(--gap);
}

.tile {
  display: grid;
  place-items: center;
  width: var(--tile);
  height: var(--tile);
  border: 2px solid var(--line);
  border-radius: 6px;
  background: transparent;
  font-size: calc(var(--tile) * .52);
  font-weight: 700;
  text-transform: uppercase;
  line-height: 1;
  user-select: none;
  color: var(--ink);
}

.tile.filled { border-color: var(--ink-faint); animation: pop .1s ease-out; }

.tile.correct { background: var(--correct); border-color: var(--correct); color: var(--on-filled); }
.tile.present { background: var(--present); border-color: var(--present); color: var(--on-filled); }
.tile.absent  { background: var(--absent);  border-color: var(--absent);  color: var(--on-filled); }

.tile.flip { animation: flip .48s ease forwards; }
.tile.dance { animation: dance .55s ease; }
.row.shake { animation: shake .5s; }

@keyframes pop {
  0% { transform: scale(.9); }
  100% { transform: scale(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(-7px); }
  40%, 60% { transform: translateX(7px); }
}

@keyframes dance {
  0%, 100% { transform: translateY(0); }
  35% { transform: translateY(-24px); }
  60% { transform: translateY(6px); }
  80% { transform: translateY(-3px); }
}

/* ---------- result card ---------- */

.result {
  width: 100%;
  margin-top: 4px;
  padding: 12px 14px 14px;
  border: 1px solid var(--line-soft);
  border-radius: var(--radius);
  background: var(--bg-elev);
  text-align: center;
  animation: rise .3s ease both;
}

@keyframes rise {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

.result-headline {
  margin: 0;
  font-size: .74rem;
  font-weight: 700;
  letter-spacing: .12em;
  text-transform: uppercase;
  color: var(--accent);
}

.result.is-loss .result-headline { color: var(--ink-faint); }

.result-word {
  margin: 3px 0 2px;
  font-size: 1.5rem;
  font-weight: 800;
  letter-spacing: .1em;
  text-transform: uppercase;
}

.result-def {
  margin: 0 0 11px;
  font-size: .88rem;
  color: var(--ink-soft);
  line-height: 1.4;
}

.result-actions { display: flex; gap: 8px; justify-content: center; }

.countdown {
  margin: 9px 0 0;
  font-size: .74rem;
  font-variant-numeric: tabular-nums;
  color: var(--ink-faint);
}

.btn {
  padding: 9px 18px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg-elev);
  font-size: .87rem;
  font-weight: 600;
  cursor: pointer;
  transition: transform .1s, filter .15s, background .15s;
}

.btn:hover { filter: brightness(.97); }
.btn:active { transform: scale(.97); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.btn-quiet {
  margin-top: 14px;
  width: 100%;
  color: var(--ink-soft);
  font-weight: 500;
}

/* ---------- hint ---------- */

.hint-line {
  margin: 8px 0 4px;
  min-height: 20px;
  text-align: center;
  font-size: .82rem;
}

.linkish {
  border: 0;
  padding: 2px 4px;
  background: none;
  color: var(--ink-faint);
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.linkish:hover { color: var(--ink); }
.hint-text { color: var(--ink-soft); font-style: italic; }

/* ---------- keyboard ---------- */

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 0 12px;
}

/* The keyboard steps aside for the result card once the round is over. */
.keyboard[hidden] { display: none; }

.krow { display: flex; gap: 5px; justify-content: center; }

.key {
  flex: 1;
  min-width: 0;
  height: clamp(42px, 7dvh, 56px);
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-ink);
  font-size: .84rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  transition: background .15s, transform .06s, color .15s;
}

.key:hover { background: var(--key-hover); }
.key:active { transform: scale(.94); }
.key.wide { flex: 1.55; font-size: .68rem; letter-spacing: .04em; }

.key svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }

.key.correct { background: var(--correct); color: var(--on-filled); }
.key.present { background: var(--present); color: var(--on-filled); }
.key.absent  { background: var(--key-off);  color: var(--key-off-ink); }

/* ---------- modals ---------- */

.modal { position: fixed; inset: 0; z-index: 40; display: grid; place-items: center; }
.modal[hidden] { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(10, 12, 18, .55);
  animation: fade .2s ease both;
}

@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.modal-card {
  position: relative;
  width: min(430px, calc(100vw - 28px));
  max-height: min(86dvh, 720px);
  overflow-y: auto;
  padding: 22px 22px 24px;
  border-radius: 14px;
  background: var(--bg-elev);
  box-shadow: var(--shadow);
  animation: rise .22s ease both;
}

.modal-card:focus { outline: none; }

.modal-card h2 { margin: 0 0 12px; font-size: 1.1rem; letter-spacing: .04em; text-transform: uppercase; }
.modal-card h3 { margin: 20px 0 6px; font-size: .78rem; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-soft); }
.modal-card p { margin: 0 0 10px; font-size: .88rem; line-height: 1.5; color: var(--ink-soft); }
.modal-card p b { color: var(--ink); }

.modal-x {
  position: absolute;
  top: 10px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-faint);
  font-size: 1.5rem;
  line-height: 1;
  cursor: pointer;
}

.modal-x:hover { background: var(--line-soft); color: var(--ink); }

kbd {
  padding: 1px 6px;
  border: 1px solid var(--line);
  border-bottom-width: 2px;
  border-radius: 5px;
  background: var(--bg);
  font-family: inherit;
  font-size: .8em;
  font-weight: 600;
  color: var(--ink);
}

.example {
  margin: 12px 0;
  padding-top: 12px;
  border-top: 1px solid var(--line-soft);
}

.example p { margin: 8px 0 0; }
.ex-row { display: flex; gap: 4px; }
.tile.sm { --tile: 34px; font-size: 17px; border-width: 2px; }

/* ---------- hard-mode switch ---------- */

.switch {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--line-soft);
  cursor: pointer;
}

.switch input { position: absolute; opacity: 0; width: 0; height: 0; }

.switch-body {
  flex: none;
  position: relative;
  width: 42px;
  height: 24px;
  margin-top: 2px;
  border-radius: 999px;
  background: var(--line);
  transition: background .18s;
}

.switch-knob {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 18px;
  height: 18px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  transition: transform .18s;
}

.switch input:checked + .switch-body { background: var(--accent); }
.switch input:checked + .switch-body .switch-knob { transform: translateX(18px); }
.switch input:focus-visible + .switch-body { outline: 2px solid var(--accent); outline-offset: 2px; }

.switch-label { font-size: .88rem; line-height: 1.45; }
.switch-label small { display: block; color: var(--ink-soft); font-size: .82rem; }

/* ---------- statistics ---------- */

.stat-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; text-align: center; }
.stat b { display: block; font-size: 1.7rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat span { font-size: .68rem; color: var(--ink-soft); letter-spacing: .04em; }

.dist { display: flex; flex-direction: column; gap: 4px; }

.dist-row { display: grid; grid-template-columns: 12px 1fr; gap: 8px; align-items: center; font-size: .8rem; }

.dist-bar {
  min-width: 26px;
  padding: 2px 7px;
  border-radius: 4px;
  background: var(--absent);
  color: var(--on-filled);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-align: right;
  transition: width .4s ease;
}

.dist-bar.current { background: var(--correct); }

.fineprint { margin-top: 14px !important; font-size: .74rem !important; color: var(--ink-faint) !important; }

/* ---------- misc ---------- */

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

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}

@media (max-height: 620px) {
  .wordmark-sub { display: none; }
  .hint-line { margin: 4px 0 0; }
}
