:root {
  --paper: #f4ecdc; --sheet: #fffaf0; --ink: #23382f; --soft: #6b7568; --line: #cfc4ab;
  --red: #c22f25; --yellow: #f3b92f;
  --serif: "Iowan Old Style", Palatino, "Palatino Linotype", Georgia, "DejaVu Serif", serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column; min-height: 100dvh; height: 100dvh;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  background: var(--paper); color: var(--ink); font-family: var(--serif);
  touch-action: manipulation; overflow: hidden;
}
button { font: inherit; color: inherit; cursor: pointer; }
:focus-visible { outline: 3px solid var(--ink); outline-offset: 2px; }

header {
  display: flex; align-items: center; gap: 14px; width: 100%; max-width: 620px; margin: 0 auto;
  padding: 12px 14px; border-bottom: 3px double var(--line);
}
h1 { margin: 0; font-size: 1.5rem; font-style: italic; font-weight: 600; }
h1::first-letter { color: var(--red); font-size: 1.5em; font-style: normal; }
dl { display: flex; gap: 16px; margin: 0 0 0 auto; text-align: center; }
dt { font-size: .75rem; color: var(--soft); }
dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }
.round-btn { flex: none; width: 38px; height: 38px; padding: 0; display: grid; place-items: center; border: 1px solid var(--ink); border-radius: 50%; background: none; }
.actions { display: flex; gap: 10px; }
.actions button { flex: 1; padding: 12px; border: 1px solid var(--ink); background: none; font-weight: 700; }
.actions .primary { background: var(--ink); }
@media (max-width: 400px) { header { gap: 8px; padding-inline: 10px; } h1 { font-size: 1.15rem; } dl { gap: 9px; } }
@media (max-width: 350px) { header { gap: 5px; padding-inline: 6px; } h1 { font-size: 1rem; } dl { gap: 6px; } dt { font-size: .68rem; } dd { font-size: .85rem; } .round-btn { width: 34px; height: 34px; } }

nav { display: flex; justify-content: center; gap: clamp(10px, 5vw, 24px); padding-top: 4px; }
nav button, .tabs button { padding: 6px 4px; border: 0; border-bottom: 3px solid transparent; background: none; color: var(--soft); }
nav [aria-pressed="true"], .tabs [aria-pressed="true"] { color: var(--ink); border-color: var(--red); font-weight: 700; }

main {
  flex: 1; min-height: 0; display: flex; flex-direction: column; align-items: center;
  justify-content: safe center; padding: 8px 12px; overflow: hidden;
}
#board { display: grid; gap: calc(var(--cell, 42px) * .25); }
.row { display: flex; flex-wrap: wrap; justify-content: center; column-gap: calc(var(--cell) * .499); width: var(--width); opacity: .55; }
.row.active, .row:has(.good, .near, .miss) { opacity: 1; }
.group { display: flex; }
.mark { width: calc(var(--cell) * .35); text-align: center; font-size: calc(var(--cell) * .56); font-weight: 700; }
.tile {
  width: calc(var(--cell, 42px) * .92); height: calc(var(--cell, 42px) * .92); margin: calc(var(--cell, 42px) * .04);
  display: inline-grid; place-items: center; border-bottom: 3px solid var(--line); background: var(--sheet);
  font-size: calc(var(--cell, 42px) * .54); font-weight: 700; line-height: 1;
}
.tile.initial { color: var(--red); border-color: var(--red); }
.tile.ghost { color: #a8604f; }
.tile.cursor { border-color: var(--ink); }
.tile.cursor.near, .tile.cursor.miss { box-shadow: 0 0 0 2px var(--ink); }
.good, .near, .miss { animation: stamp .22s ease-out; }
.tile.good { background: var(--red); color: #fff; border: 0; border-radius: 0; }
.tile.near { background: var(--yellow); color: var(--ink); border: 0; border-radius: 50%; }
.tile.miss { background: none; color: var(--soft); border-bottom-color: transparent; }
.row.shake { animation: shake .3s; }
.row.won .tile { animation: hop .5s ease-out; }
@keyframes stamp { from { transform: scale(1.25); opacity: .3; } }
@keyframes shake { 25% { transform: translateX(-7px); } 75% { transform: translateX(7px); } }
@keyframes hop { 40% { transform: translateY(-8px); } }

#tries { display: flex; gap: 8px; margin-top: 14px; }
#tries[hidden] { display: none; }
#tries i { width: 12px; height: 12px; border: 2px solid var(--ink); border-radius: 50%; }
#tries .used { background: var(--ink); }
#message { min-height: 1.5em; margin: 8px 0 0; font-style: italic; text-align: center; }
#message.sr { color: transparent; font-size: 1px; line-height: 24px; overflow: hidden; height: 24px; }

#keyboard { width: 100%; max-width: 620px; margin: 0 auto; padding: 6px 6px 12px; display: grid; gap: 6px; user-select: none; }
#keyboard div { display: flex; gap: 5px; }
#keyboard button {
  flex: 1; min-width: 0; height: clamp(40px, 7dvh, 54px); padding: 0; border: 0; border-radius: 3px;
  background: var(--ink); color: var(--sheet); font-weight: 700;
}
#keyboard button:active { transform: translateY(2px); }
#keyboard .wide { flex: 1.9; background: none; color: var(--ink); border: 1px solid var(--ink); font-size: .85rem; }
#keyboard .k-good { background: var(--red); color: #fff; }
#keyboard .k-near { background: var(--yellow); color: var(--ink); border-radius: 24px; }
#keyboard .k-miss { background: #ddd2b9; color: #6f6a58; }

dialog {
  max-width: min(420px, calc(100vw - 32px)); padding: 24px; border: 3px double var(--ink);
  background: var(--sheet); color: var(--ink); line-height: 1.45;
}
dialog::backdrop { background: rgb(35 56 47 / .6); }
dialog h2 { margin: 0 0 10px; font-style: italic; }
.legend { list-style: none; padding: 0; display: grid; gap: 8px; }
.legend li { display: flex; align-items: center; gap: 12px; }
.legend .tile { flex: none; }
#answer { margin: 0; font-size: 1.8rem; line-height: 1.2; font-weight: 700; color: var(--red); }
.primary { width: 100%; padding: 12px; border: 0; background: var(--ink); color: var(--sheet); font-weight: 700; }
#scores-btn { text-decoration: underline; text-underline-offset: 4px; }
#login-form { display: grid; gap: 12px; }
#login-form label { display: grid; gap: 4px; }
input { width: 100%; padding: 10px; border: 1px solid var(--ink); border-radius: 0; background: #fff; color: var(--ink); font: inherit; font-size: 1.1rem; }
.note { margin: 0; color: var(--soft); font-size: .9rem; }
.error { margin: 0; min-height: 1.4em; color: var(--red); }
.link { border: 0; background: none; text-decoration: underline; padding: 6px; }
button:disabled { opacity: .5; cursor: default; }
#scores { width: 420px; }
.tabs { display: flex; gap: 20px; margin-bottom: 8px; }
.scroll { max-height: 46dvh; overflow-y: auto; margin-bottom: 12px; }
table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
th { font-size: .8rem; font-weight: 400; color: var(--soft); }
th, td { padding: 7px 4px; border-bottom: 1px solid var(--line); text-align: right; }
th:nth-child(2), td:nth-child(2) { text-align: left; width: 100%; overflow-wrap: anywhere; }
.me td { font-weight: 700; color: var(--red); }
#scores .actions { margin-top: 12px; }
@media (max-height: 520px) { header { padding: 4px 14px; } nav button { padding: 2px 4px; } h1 { font-size: 1.1rem; } #keyboard button { height: 32px; } }
@media (prefers-reduced-motion: reduce) { * { animation: none !important; } }
