:root {
  --bg: #ffffff;
  --text: #1a1a1b;
  --muted: #6b6f72;
  --line: #d3d6da;
  --line-strong: #878a8c;
  --key: #d3d6da;
  --correct: #4f9d54;
  --present: #c9a227;
  --absent: #787c7e;
  --accent: #4f9d54;
  --error: #b3261e;
  --dialog: #ffffff;
  --toast-bg: #1a1a1b;
  --toast-text: #ffffff;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --text: #f3f3f3;
    --muted: #9a9ea1;
    --line: #3a3a3c;
    --line-strong: #565758;
    --key: #818384;
    --correct: #538d4e;
    --present: #b59f3b;
    --absent: #3a3a3c;
    --error: #f2877f;
    --dialog: #1c1c1e;
    --toast-bg: #f3f3f3;
    --toast-text: #121213;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

header {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 6px 12px;
  padding-top: max(6px, env(safe-area-inset-top));
  border-bottom: 1px solid var(--line);
}
h1 {
  margin: 0;
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(1.35rem, 6vw, 1.9rem);
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
}
.actions { display: flex; justify-content: flex-end; gap: 2px; }
button { font: inherit; color: inherit; cursor: pointer; }
.icon {
  width: 40px; height: 40px; padding: 8px;
  border: 0; border-radius: 8px; background: none;
}
.icon:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.icon svg { width: 100%; height: 100%; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.icon svg .dot { fill: currentColor; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#caption {
  min-height: 1.4em;
  margin: 8px 16px 0;
  text-align: center;
  font-size: 0.9rem;
  color: var(--muted);
}

main {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 8px 16px;
  overflow: hidden;
  container-type: size;
}
#board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  width: min(350px, 100cqw, 100cqh * 5 / 6);
  aspect-ratio: 5 / 6;
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 5px; min-height: 0; }
.tile {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  overflow: hidden;
  border: 2px solid var(--line);
  font-size: clamp(0.7rem, 8cqh, 2rem);
  font-weight: 700;
  line-height: 1;
  text-transform: uppercase;
  user-select: none;
  --mark-bg: var(--absent);
}
.tile.filled { border-color: var(--line-strong); }
.tile.pop { animation: pop 100ms ease-out; }
.tile[data-mark="c"] { --mark-bg: var(--correct); }
.tile[data-mark="p"] { --mark-bg: var(--present); }
.tile[data-mark="a"] { --mark-bg: var(--absent); }
.tile[data-mark] { background: var(--mark-bg); border-color: var(--mark-bg); color: #fff; }
.tile.reveal { animation: flip 500ms ease-in-out both; animation-delay: calc(var(--i) * 220ms); }
.tile.win { animation: bounce 700ms ease both; animation-delay: calc(var(--i) * 90ms); }
.row.shake { animation: shake 450ms; }

@keyframes pop { 50% { transform: scale(1.1); } }
@keyframes flip {
  0% { transform: rotateX(0); background: transparent; border-color: var(--line-strong); color: var(--text); }
  49.9% { transform: rotateX(90deg); background: transparent; border-color: var(--line-strong); color: var(--text); }
  50% { transform: rotateX(90deg); background: var(--mark-bg); border-color: var(--mark-bg); color: #fff; }
  100% { transform: rotateX(0); background: var(--mark-bg); border-color: var(--mark-bg); color: #fff; }
}
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  35% { transform: translateY(-28%); }
  60% { transform: translateY(6%); }
  80% { transform: translateY(-6%); }
}
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}
@media (prefers-reduced-motion: reduce) {
  .tile.reveal, .tile.win, .tile.pop, .row.shake { animation-duration: 1ms; animation-delay: 0ms; }
}

#keyboard {
  padding: 0 8px;
  padding-bottom: max(8px, env(safe-area-inset-bottom));
  user-select: none;
}
.krow { display: flex; justify-content: center; gap: 6px; margin-bottom: 8px; }
.krow.inset { padding: 0 5%; }
.key {
  flex: 1;
  height: clamp(44px, 8dvh, 58px);
  padding: 0;
  border: 0;
  border-radius: 5px;
  background: var(--key);
  color: var(--text);
  font-size: 1.15rem;
  font-weight: 700;
  text-transform: uppercase;
}
.key.wide { flex: 1.55; font-size: 0.75rem; }
.key svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; vertical-align: middle; }
.key:active { filter: brightness(0.9); }
.key[data-mark] { color: #fff; }
.key[data-mark="c"] { background: var(--correct); }
.key[data-mark="p"] { background: var(--present); }
.key[data-mark="a"] { background: var(--absent); }
@media (prefers-color-scheme: light) { .key[data-mark="a"] { background: #787c7e; } }

#toasts {
  position: fixed;
  top: 76px; left: 50%;
  transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 100;
  pointer-events: none;
}
.toast {
  padding: 12px 16px;
  border-radius: 6px;
  background: var(--toast-bg);
  color: var(--toast-text);
  font-weight: 700;
  font-size: 0.95rem;
  transition: opacity 300ms;
}
.toast.out { opacity: 0; }

dialog {
  width: min(440px, calc(100vw - 32px));
  max-height: calc(100dvh - 32px);
  padding: 24px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--dialog);
  color: var(--text);
  box-shadow: 0 8px 40px rgb(0 0 0 / 0.25);
}
dialog::backdrop { background: rgb(0 0 0 / 0.5); }
dialog[open] { animation: rise 180ms ease-out; }
@keyframes rise { from { opacity: 0; transform: translateY(16px); } }
dialog h2 { margin: 0 0 12px; font-size: 1.15rem; text-transform: uppercase; letter-spacing: 0.05em; text-align: center; }
dialog h3 { margin: 20px 0 8px; font-size: 0.85rem; text-transform: uppercase; letter-spacing: 0.05em; }
dialog p { line-height: 1.45; }
.close {
  position: absolute; top: 8px; right: 10px;
  width: 36px; height: 36px;
  border: 0; border-radius: 8px; background: none;
  font-size: 1.6rem; line-height: 1; color: var(--muted);
}
.legend { list-style: none; margin: 16px 0; padding: 0; display: grid; gap: 8px; }
.legend li { display: flex; align-items: center; gap: 12px; }
.tile.mini { width: 36px; height: 36px; font-size: 1.1rem; flex: none; }

#result-line { margin: 0 0 12px; text-align: center; font-weight: 600; }
.numbers { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; }
.numbers b { display: block; font-size: 2rem; font-weight: 500; }
.numbers span { font-size: 0.75rem; color: var(--muted); }
#distribution { display: grid; gap: 4px; }
.bar-row { display: grid; grid-template-columns: 1.2em 1fr; align-items: center; gap: 6px; font-size: 0.85rem; font-weight: 700; }
.bar {
  min-width: 1.8em;
  padding: 2px 7px;
  background: var(--absent);
  color: #fff;
  text-align: right;
  justify-self: start;
}
.bar.today { background: var(--correct); }
.footer { display: flex; align-items: center; justify-content: space-between; gap: 16px; margin-top: 20px; }
.next span { display: block; font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; color: var(--muted); }
.next b { font-size: 1.7rem; font-weight: 500; font-variant-numeric: tabular-nums; }
#guest-nudge { margin: 16px 0 0; font-size: 0.9rem; color: var(--muted); }
a { color: var(--accent); font-weight: 600; }

.primary, .secondary {
  min-height: 46px;
  padding: 0 24px;
  border-radius: 24px;
  font-weight: 700;
  font-size: 1rem;
}
.primary { border: 0; background: var(--accent); color: #fff; }
.secondary { border: 1.5px solid var(--line-strong); background: none; }
.primary:disabled { opacity: 0.6; cursor: default; }

#auth-form { display: grid; gap: 12px; }
#auth-form[hidden] { display: none; }
#auth-blurb { margin: 0; font-size: 0.9rem; color: var(--muted); text-align: center; }
#auth-blurb:empty { display: none; }
label { display: grid; gap: 4px; font-size: 0.8rem; font-weight: 700; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); }
input {
  height: 46px;
  padding: 0 12px;
  border: 1.5px solid var(--line-strong);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-size: 1rem;  /* 16px stops iOS zooming on focus */
}
#whoami { overflow-wrap: anywhere; }
#auth-error { min-height: 1.3em; margin: 0; font-size: 0.9rem; color: var(--error); }
.switch { margin: 0; text-align: center; font-size: 0.9rem; }

/* Short screens (phones in landscape): give the board what room there is. */
@media (max-height: 520px) {
  header { padding-top: 0; padding-bottom: 0; }
  h1 { font-size: 1.2rem; }
  #caption { display: none; }
  main { padding: 4px 16px; }
  #board, .row { gap: 3px; }
  .krow { margin-bottom: 4px; gap: 4px; }
  .key { height: 34px; font-size: 0.95rem; }
  #toasts { top: 48px; }
}
