:root {
  --bg: #FBF4E6;
  --bg-2: #F3E4C8;
  --ink: #1B2A4A;
  --ink-soft: #3A4663;
  --paper: #FFFBF4;
  --line: #D9C8A8;
  --teal: #0E7C6B;
  --teal-ink: #06352E;
  --gold: #E29A12;
  --gold-ink: #4A3203;
  --gray: #5C5A57;
  --gray-bg: #6F6C68;
  --coral: #C54C2E;
  --coral-dark: #8E2F18;
  --shadow: 0 8px 24px rgba(27, 42, 74, 0.10);
  --tile: min(72px, calc((100vw - 2.4rem - 2rem) / 5));
  --radius: 14px;
  --font: "Atkinson Hyperlegible", "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --display: "Fredoka", "Atkinson Hyperlegible", "Segoe UI", sans-serif;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font);
  font-size: 20px;
  line-height: 1.45;
}
body {
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}
button {
  font-family: inherit;
  font-size: 1.05rem;
  cursor: pointer;
  touch-action: manipulation;
}
h1, h2 { font-family: var(--display); font-weight: 650; margin: 0; }

.bg {
  position: fixed;
  inset: 0;
  z-index: -1;
  background:
    radial-gradient(900px 500px at 10% -10%, rgba(14, 124, 107, 0.12), transparent 60%),
    radial-gradient(700px 420px at 110% 0%, rgba(226, 154, 18, 0.16), transparent 55%),
    linear-gradient(180deg, var(--bg) 0%, var(--bg-2) 100%);
}

.top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0.75rem 1rem 0.5rem;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}
.brand { display: flex; align-items: center; gap: 0.7rem; min-width: 0; }
.logo {
  width: 52px; height: 52px; flex: 0 0 52px;
  border-radius: 14px;
  background: var(--teal);
  color: #fff;
  display: grid; place-items: center;
  font-family: var(--display);
  font-weight: 700;
  font-size: 1.8rem;
  box-shadow: 0 4px 0 var(--teal-ink);
}
h1 { font-size: 1.65rem; letter-spacing: 0.01em; color: var(--ink); line-height: 1.1; }
.tagline { margin: 0.15rem 0 0; color: var(--ink-soft); font-size: 0.95rem; }

.tools { display: flex; gap: 0.4rem; flex-shrink: 0; }
.icon-btn {
  width: 48px; height: 48px;
  border: 2px solid var(--line);
  background: var(--paper);
  color: var(--ink);
  border-radius: 12px;
  font-size: 1.3rem;
  display: grid; place-items: center;
  box-shadow: 0 2px 0 rgba(27, 42, 74, 0.08);
}
.icon-btn:hover, .icon-btn:focus-visible { border-color: var(--teal); outline: 3px solid rgba(14,124,107,0.25); }
.icon-btn[aria-pressed="true"] {
  background: var(--teal);
  color: #fff;
  border-color: var(--teal-ink);
}
.icon-btn.is-muted { background: #eee8dc; color: var(--gray); }

.wrap {
  flex: 1;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  padding: 0.25rem 1rem 1rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.7rem;
}

.mode-row {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.6rem;
}
.mode-label {
  font-weight: 700;
  font-size: 1.05rem;
  color: var(--teal-ink);
  background: rgba(14, 124, 107, 0.12);
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
}
.hint-btn {
  border: 2px solid var(--gold);
  background: #FFF6E0;
  color: var(--gold-ink);
  font-weight: 700;
  border-radius: 999px;
  padding: 0.45rem 0.95rem;
  box-shadow: 0 3px 0 #c9860e;
}
.hint-btn:hover, .hint-btn:focus-visible { filter: brightness(1.03); outline: 3px solid rgba(226,154,18,0.35); }
.hint-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #c9860e; }
.hint-btn[disabled] {
  box-shadow: none;
  transform: none;
  background: #f3ece0;
  border-color: var(--line);
  color: var(--ink-soft);
  cursor: default;
}

.message {
  min-height: 2.4em;
  margin: 0;
  text-align: center;
  font-size: 1.15rem;
  color: var(--ink);
  font-weight: 700;
  max-width: 32rem;
}
.message.good { color: var(--teal-ink); }
.message.warn { color: var(--coral-dark); }

.board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile));
  gap: 8px;
  margin: 0.15rem 0 0.35rem;
}
.row {
  display: grid;
  grid-template-columns: repeat(5, var(--tile));
  gap: 8px;
}
.tile {
  width: var(--tile);
  height: var(--tile);
  border: 3px solid #C9B896;
  border-radius: 10px;
  background: var(--paper);
  color: var(--ink);
  display: grid;
  place-items: center;
  position: relative;
  font-family: var(--display);
  font-weight: 700;
  font-size: calc(var(--tile) * 0.52);
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
  transition: transform 0.12s ease;
}
.tile.filled { border-color: #8E8164; animation: pop 0.12s ease; }
.tile.small {
  --tile: 48px;
  flex: 0 0 48px;
  border-width: 2px;
}
.tile .mark {
  position: absolute;
  top: 3px;
  right: 3px;
  bottom: auto;
  width: calc(var(--tile) * 0.30);
  height: calc(var(--tile) * 0.30);
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.32);
  color: #fff;
  font-size: calc(var(--tile) * 0.20);
  font-family: var(--font);
  font-weight: 700;
  line-height: 1;
}
.tile .mark:empty,
.key .kmark:empty {
  display: none;
  background: none;
}
.tile.present .mark { background: rgba(0, 0, 0, 0.42); color: #fff; }
.tile.correct .mark { background: rgba(0, 0, 0, 0.30); color: #fff; }
.tile.absent .mark { background: rgba(255, 255, 255, 0.22); color: #fff; }
.tile.small .mark {
  width: 14px;
  height: 14px;
  font-size: 10px;
  top: 2px;
  right: 2px;
}
.tile.correct {
  background: var(--teal);
  border-color: var(--teal-ink);
  color: #fff;
}
.tile.present {
  background: var(--gold);
  border-color: var(--gold-ink);
  color: #1B1506;
}
.tile.absent {
  background: var(--gray-bg);
  border-color: #3F3D3B;
  color: #fff;
}
.tile.flip { animation: flip 0.5s ease; }

@keyframes pop {
  from { transform: scale(1.08); }
  to { transform: scale(1); }
}
@keyframes flip {
  0% { transform: rotateX(0); }
  45% { transform: rotateX(90deg); }
  100% { transform: rotateX(0); }
}

.keyboard {
  width: 100%;
  max-width: 620px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: auto;
  padding-bottom: env(safe-area-inset-bottom, 0);
}
.kb-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}
.key {
  flex: 1 1 0;
  min-width: 0;
  min-height: 58px;
  max-width: 56px;
  border: 0;
  border-radius: 10px;
  background: #EFE4CC;
  color: var(--ink);
  font-family: var(--display);
  font-weight: 650;
  font-size: 1.25rem;
  box-shadow: 0 3px 0 #C9B896;
  position: relative;
  padding: 0;
}
.key:hover, .key:focus-visible { filter: brightness(1.04); outline: 3px solid rgba(14,124,107,0.3); }
.key:active { transform: translateY(2px); box-shadow: 0 1px 0 #C9B896; }
.key.wide { max-width: 92px; flex: 1.7 1 0; font-size: 0.95rem; font-family: var(--font); font-weight: 700; }
.key .kmark {
  position: absolute;
  top: 3px;
  right: 4px;
  bottom: auto;
  width: 14px;
  height: 14px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: rgba(0, 0, 0, 0.28);
  color: #fff;
  font-size: 0.65rem;
  font-weight: 700;
  font-family: var(--font);
}
.key.absent .kmark { background: rgba(255, 255, 255, 0.22); }
.key:not(.correct):not(.present):not(.absent) .kmark { display: none; }
.key.correct { background: var(--teal); color: #fff; box-shadow: 0 3px 0 var(--teal-ink); }
.key.present { background: var(--gold); color: #1B1506; box-shadow: 0 3px 0 var(--gold-ink); }
.key.absent { background: var(--gray-bg); color: #fff; box-shadow: 0 3px 0 #3F3D3B; }

.footer-actions {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.2rem;
  margin-top: 0.2rem;
}
.text-btn {
  background: none;
  border: 0;
  color: var(--teal-ink);
  text-decoration: underline;
  text-underline-offset: 3px;
  font-weight: 700;
  padding: 0.4rem 0.6rem;
  min-height: 44px;
}
.streak-line {
  margin: 0;
  color: var(--ink-soft);
  font-size: 1rem;
  text-align: center;
}
.dedication {
  text-align: center;
  color: var(--ink-soft);
  font-size: 0.95rem;
  margin: 0 1rem 0.9rem;
}

/* Modals */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(27, 42, 74, 0.48);
  display: grid;
  place-items: center;
  padding: 1rem;
  z-index: 40;
  overflow: auto;
}
.modal[hidden] { display: none; }
.modal-card {
  background: var(--paper);
  color: var(--ink);
  width: min(560px, 100%);
  border-radius: 22px;
  padding: 1.4rem 1.35rem 1.25rem;
  box-shadow: var(--shadow);
  border: 3px solid #E8D7B5;
  max-height: calc(100dvh - 2rem);
  overflow: auto;
}
.modal-card h2 { font-size: 1.8rem; margin-bottom: 0.4rem; }
.lead { font-size: 1.12rem; margin: 0 0 0.8rem; }
.howto { margin: 0 0 1rem 1.2rem; padding: 0; }
.howto li { margin: 0.35rem 0; }
.examples { display: flex; flex-direction: column; gap: 0.85rem; margin-bottom: 1rem; }
.ex-row { display: flex; flex-wrap: wrap; align-items: center; gap: 0.35rem 0.4rem; }
.ex-row p { flex: 1 1 180px; margin: 0; font-size: 1.02rem; }

.primary, .secondary {
  display: block;
  width: 100%;
  border: 0;
  border-radius: 14px;
  min-height: 56px;
  font-weight: 700;
  font-size: 1.15rem;
  margin-top: 0.65rem;
}
.primary {
  background: var(--teal);
  color: #fff;
  box-shadow: 0 4px 0 var(--teal-ink);
}
.primary:hover, .primary:focus-visible { filter: brightness(1.05); outline: 3px solid rgba(14,124,107,0.35); }
.primary:active { transform: translateY(2px); box-shadow: 0 2px 0 var(--teal-ink); }
.secondary {
  background: #FFF6E0;
  color: var(--gold-ink);
  border: 2px solid var(--gold);
  box-shadow: 0 3px 0 #c9860e;
}

.fact {
  background: #F6EBCC;
  border-left: 6px solid var(--gold);
  border-radius: 10px;
  padding: 0.85rem 1rem;
  font-size: 1.1rem;
  margin: 0.4rem 0 0.6rem;
}
.end-actions .text-btn { width: 100%; }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin: 0.6rem 0 1rem;
}
.stat {
  background: #F6EBCC;
  border-radius: 12px;
  padding: 0.55rem 0.2rem;
  text-align: center;
}
.stat b { display: block; font-size: 1.7rem; font-family: var(--display); line-height: 1.1; }
.stat span { font-size: 0.85rem; color: var(--ink-soft); }
.dist { display: grid; gap: 6px; margin: 0.4rem 0 0.8rem; }
.dist-row { display: grid; grid-template-columns: 1.6rem 1fr; align-items: center; gap: 0.4rem; }
.dist-bar-wrap { background: #efe4cc; border-radius: 8px; overflow: hidden; min-height: 28px; }
.dist-bar {
  background: var(--teal);
  color: #fff;
  font-weight: 700;
  min-width: 2rem;
  padding: 0.15rem 0.45rem;
  font-size: 0.95rem;
}

/* Confetti */
.confetti { position: fixed; inset: 0; pointer-events: none; z-index: 50; overflow: hidden; }
.piece {
  position: absolute;
  top: -16px;
  width: 12px; height: 18px;
  border-radius: 2px;
  animation: fall linear forwards;
}
@keyframes fall {
  to { transform: translateY(110dvh) rotate(360deg); opacity: 0.9; }
}

/* High contrast */
html.hc {
  --bg: #000000;
  --bg-2: #111111;
  --ink: #FFFFFF;
  --ink-soft: #F0F0F0;
  --paper: #111111;
  --line: #FFFFFF;
  --teal: #00E5C0;
  --teal-ink: #00352E;
  --gold: #FFD000;
  --gold-ink: #221900;
  --gray: #E6E6E6;
  --gray-bg: #2A2A2A;
  --coral: #FF7A59;
  --coral-dark: #FFD0C4;
}
html.hc body { background: #000; }
html.hc .bg { background: #000; }
html.hc .logo { box-shadow: 0 0 0 3px #000, 0 0 0 6px var(--teal); color: #000; }
html.hc .icon-btn, html.hc .tile, html.hc .key, html.hc .modal-card, html.hc .mode-label, html.hc .hint-btn, html.hc .stat, html.hc .fact {
  box-shadow: none;
}
html.hc .icon-btn, html.hc .tile { border-width: 3px; border-color: #fff; }
html.hc .tile.correct, html.hc .key.correct { background: #00E5C0; color: #000; border-color: #fff; }
html.hc .tile.present, html.hc .key.present { background: #FFD000; color: #000; border-color: #fff; }
html.hc .tile.absent, html.hc .key.absent { background: #2A2A2A; color: #fff; border: 3px dashed #c8c8c8; }
html.hc .modal { background: rgba(0,0,0,0.78); }
html.hc .modal-card { border-color: #fff; }
html.hc .primary { color: #000; }
html.hc .message.good { color: #00E5C0; }
html.hc .message.warn { color: #FFD000; }
html.hc .dedication, html.hc .streak-line { color: #F0F0F0; }
html.hc .text-btn { color: #00E5C0; }

@media (max-width: 380px) {
  html, body { font-size: 18px; }
  h1 { font-size: 1.4rem; }
  .tagline { display: none; }
  .logo { width: 44px; height: 44px; flex-basis: 44px; font-size: 1.5rem; }
  .key { min-height: 54px; font-size: 1.1rem; }
  :root { --tile: min(64px, calc((100vw - 1.6rem - 1.6rem) / 5)); }
}
@media (min-width: 700px) {
  .key { min-height: 64px; font-size: 1.35rem; }
}
@media (prefers-reduced-motion: reduce) {
  .tile.flip, .tile.filled, .piece, .key:active, .primary:active { animation: none; transition: none; transform: none; }
}
