:root {
  --bg: #f6f1e7;
  --fg: #2b2622;
  --muted: #8a7f73;
  --line: #d9cfbf;
  --tile-border: #cbbfad;
  --tile-filled: #8a7f73;
  --key: #e4dacb;
  --key-fg: #2b2622;
  --ok: #4f7d4a;
  --near: #d39b2a;
  --no: #8f867c;
  --accent: #b4532a;
  --panel: #fffaf2;
}
:root[data-theme="dark"] {
  --bg: #1d1a17;
  --fg: #f1ebe2;
  --muted: #a39888;
  --line: #3a342e;
  --tile-border: #4a433b;
  --tile-filled: #7c7266;
  --key: #4a433b;
  --key-fg: #f1ebe2;
  --ok: #5e9157;
  --near: #c48d22;
  --no: #3f3934;
  --accent: #d06a3d;
  --panel: #27231f;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #1d1a17; --fg: #f1ebe2; --muted: #a39888; --line: #3a342e;
    --tile-border: #4a433b; --tile-filled: #7c7266; --key: #4a433b; --key-fg: #f1ebe2;
    --ok: #5e9157; --near: #c48d22; --no: #3f3934; --accent: #d06a3d; --panel: #27231f;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--fg);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
  height: 100dvh;
  -webkit-tap-highlight-color: transparent;
}
button { font: inherit; color: inherit; cursor: pointer; }

.topbar {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  padding: 8px 16px; border-bottom: 1px solid var(--line);
}
.topbar h1 {
  margin: 0; font-family: Georgia, "Times New Roman", serif;
  font-size: 30px; letter-spacing: .04em; color: var(--accent);
}
.topbar .right { justify-self: end; display: flex; gap: 6px; }
.icon {
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line);
  background: transparent; font-weight: 700; display: grid; place-items: center;
}
.icon:hover { background: var(--key); }

main {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: space-between; padding: 12px 8px 10px; position: relative;
  width: 100%; max-width: 520px; margin: 0 auto; min-height: 0;
}

.datebar { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.datebar input {
  font: inherit; color: var(--fg); background: var(--panel); border: 1px solid var(--line);
  border-radius: 6px; padding: 5px 8px; color-scheme: light dark;
}
#datelabel { margin-top: 6px; font-family: Georgia, serif; font-size: 17px; font-weight: 700; }
#datelabel small { font-family: system-ui, sans-serif; font-size: 12px; font-weight: 600; text-transform: none; color: var(--accent); margin-left: 6px; }
.link { background: none; border: 0; color: var(--accent); font-weight: 700; text-decoration: underline; padding: 4px; }

#board {
  display: grid; grid-template-rows: repeat(6, 1fr); gap: 6px;
  width: min(330px, 100%, calc((100dvh - 360px) * 5 / 6));
  aspect-ratio: 5 / 6; margin: auto 0;
}
.row { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; }
.row.shake { animation: shake .45s; }
.tile {
  display: grid; place-items: center; border: 2px solid var(--tile-border);
  border-radius: 6px; font-size: clamp(20px, 7vw, 32px); font-weight: 800;
  text-transform: uppercase; user-select: none; background: transparent;
}
.tile.filled { border-color: var(--tile-filled); animation: pop .1s; }
.tile.ok, .tile.near, .tile.no { color: #fff; border-color: transparent; }
.tile.ok { background: var(--ok); }
.tile.near { background: var(--near); }
.tile.no { background: var(--no); }
.tile.flip { animation: flip .5s ease forwards; }
.tile.win { animation: bounce .6s ease; }

#keyboard { width: 100%; display: flex; flex-direction: column; gap: 7px; user-select: none; }
.krow { display: flex; gap: 5px; justify-content: center; }
.key {
  flex: 1; max-width: 44px; height: 56px; border: 0; border-radius: 6px;
  background: var(--key); color: var(--key-fg); font-weight: 700; font-size: 15px;
  text-transform: uppercase; padding: 0;
}
.key.wide { max-width: 68px; flex: 1.5; font-size: 12px; }
.key.ok, .key.near, .key.no { color: #fff; }
.key.ok { background: var(--ok); }
.key.near { background: var(--near); }
.key.no { background: var(--no); }
.key:active { filter: brightness(.9); }

#toast {
  position: absolute; top: 10px; left: 50%; transform: translateX(-50%);
  z-index: 10; display: flex; flex-direction: column; gap: 6px; align-items: center;
  pointer-events: none;
}
#toast div {
  background: var(--fg); color: var(--bg); padding: 10px 14px; border-radius: 6px;
  font-weight: 700; font-size: 14px; transition: opacity .3s;
}

dialog {
  border: 1px solid var(--line); border-radius: 12px; background: var(--panel);
  color: var(--fg); width: min(440px, calc(100% - 32px)); padding: 20px 22px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25);
}
dialog::backdrop { background: rgba(0,0,0,.4); }
dialog h2 { margin: 0 0 12px; font-family: Georgia, serif; text-align: center; }
dialog h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .08em; margin: 18px 0 8px; text-align: center; }
dialog p { line-height: 1.45; margin: 8px 0; }
.close { position: absolute; top: 10px; right: 12px; border: 0; background: none; font-size: 26px; line-height: 1; }
.muted { color: var(--muted); }
.ex { display: flex; gap: 4px; margin-top: 12px; }
.ex .tile { width: 38px; height: 38px; font-size: 20px; border-color: var(--tile-filled); }
.ex .tile.ok, .ex .tile.near, .ex .tile.no { border-color: transparent; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); text-align: center; }
.stats b { display: block; font-size: 30px; }
.stats span { font-size: 11px; color: var(--muted); }
#dist { display: flex; flex-direction: column; gap: 4px; }
.bar { display: flex; align-items: center; gap: 6px; font-size: 14px; }
.bar i {
  font-style: normal; background: var(--no); color: #fff; padding: 2px 8px;
  text-align: right; min-width: 24px; border-radius: 3px; font-weight: 700;
}
.bar i.cur { background: var(--ok); }
#answer { text-align: center; font-size: 16px; }
.endrow { display: flex; justify-content: space-around; align-items: center; margin-top: 14px; gap: 12px; }
.endrow > div { display: flex; flex-direction: column; text-align: center; font-size: 13px; }
.endrow b { font-size: 26px; font-variant-numeric: tabular-nums; }
.primary {
  background: var(--ok); color: #fff; border: 0; border-radius: 24px;
  padding: 12px 22px; font-weight: 700; font-size: 16px;
}

@keyframes pop { 50% { transform: scale(1.08); } }
@keyframes flip { 0% { transform: rotateX(0); } 50% { transform: rotateX(90deg); } 100% { transform: rotateX(0); } }
@keyframes shake { 20%,60% { transform: translateX(-6px); } 40%,80% { transform: translateX(6px); } }
@keyframes bounce { 30% { transform: translateY(-14px); } 60% { transform: translateY(2px); } }

@media (max-height: 600px) { .key { height: 46px; } }
