@import url('https://fonts.googleapis.com/css2?family=Fraunces:opsz,wght@9..144,600;9..144,700&family=Inter:wght@400;500;600;700&display=swap');

:root {
  --bg: #0f1115;
  --bg-elev: #181b21;
  --fg: #f4f5f7;
  --fg-dim: #9aa0ab;
  --line: #2b2f37;
  --tile-empty: #0f1115;
  --tile-border: #3a3f49;
  --tile-filled-border: #565d6b;
  --key: #353a44;
  --key-fg: #f4f5f7;
  --correct: #3aa757;
  --present: #d2a73a;
  --absent: #3a3f49;
  --accent: #3aa757;
  --shadow: 0 12px 40px rgba(0,0,0,.45);
  --radius: 14px;
  color-scheme: dark;
}

:root[data-theme="light"] {
  --bg: #ffffff;
  --bg-elev: #ffffff;
  --fg: #14151a;
  --fg-dim: #6b7280;
  --line: #e4e6ea;
  --tile-empty: #ffffff;
  --tile-border: #d3d6db;
  --tile-filled-border: #8a8f99;
  --key: #d3d6da;
  --key-fg: #14151a;
  --correct: #2f9e4d;
  --present: #c79a2f;
  --absent: #898f99;
  --accent: #2f9e4d;
  --shadow: 0 12px 40px rgba(20,21,26,.12);
  color-scheme: light;
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--fg);
  font-family: 'Inter', system-ui, -apple-system, sans-serif;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
  overflow: hidden;
  -webkit-tap-highlight-color: transparent;
}

/* ---------- Top bar ---------- */
.topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px max(14px, env(safe-area-inset-left)) 10px max(14px, env(safe-area-inset-right));
  border-bottom: 1px solid var(--line);
  flex: 0 0 auto;
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  justify-self: center;
}

.brand h1 {
  font-family: 'Fraunces', Georgia, serif;
  font-weight: 700;
  font-size: clamp(1.4rem, 5vw, 1.85rem);
  letter-spacing: .01em;
  margin: 0;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border-radius: 9px;
  background: var(--accent);
  color: #fff;
  font-family: 'Fraunces', serif;
  font-weight: 700;
  font-size: 1.25rem;
  box-shadow: inset 0 -2px 0 rgba(0,0,0,.18);
}

.topbar-right {
  justify-self: end;
  display: flex;
  gap: 4px;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--fg);
  cursor: pointer;
  transition: background .15s;
}
.icon-btn:hover { background: var(--line); }
.icon-btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.icon-btn svg {
  width: 22px; height: 22px;
  fill: none; stroke: currentColor;
  stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
#help-btn:first-child { justify-self: start; }

/* ---------- Main / board ---------- */
main {
  flex: 1 1 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 500px;
  margin: 0 auto;
  padding: 0 8px;
  min-height: 0;
}

.board-wrap {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 0;
  padding: 12px 0;
}

.board {
  display: grid;
  grid-template-rows: repeat(6, 1fr);
  gap: 5px;
  width: 100%;
  max-width: min(330px, 70vh / 6 * 5);
  aspect-ratio: 5 / 6;
}

.board-row {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
}

.tile {
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  border: 2px solid var(--tile-border);
  background: var(--tile-empty);
  border-radius: 4px;
  font-family: 'Inter', sans-serif;
  font-weight: 700;
  font-size: clamp(1.4rem, 8vw, 2rem);
  text-transform: uppercase;
  color: var(--fg);
  user-select: none;
  transition: transform .08s ease;
}

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

@keyframes pop {
  0% { transform: scale(.9); }
  50% { transform: scale(1.08); }
  100% { transform: scale(1); }
}

.tile.reveal { animation: flip .55s ease forwards; }
@keyframes flip {
  0% { transform: rotateX(0); }
  45% { transform: rotateX(-90deg); }
  55% { transform: rotateX(-90deg); }
  100% { transform: rotateX(0); }
}

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

.board-row.shake { animation: shake .5s; }
@keyframes shake {
  10%, 90% { transform: translateX(-1px); }
  20%, 80% { transform: translateX(2px); }
  30%, 50%, 70% { transform: translateX(-4px); }
  40%, 60% { transform: translateX(4px); }
}

.board-row.win .tile { animation: bounce .9s ease; }
.board-row.win .tile:nth-child(2) { animation-delay: .08s; }
.board-row.win .tile:nth-child(3) { animation-delay: .16s; }
.board-row.win .tile:nth-child(4) { animation-delay: .24s; }
.board-row.win .tile:nth-child(5) { animation-delay: .32s; }
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-26px); }
  50% { transform: translateY(4px); }
  60% { transform: translateY(-12px); }
  80% { transform: translateY(2px); }
  100% { transform: translateY(0); }
}

/* ---------- Message toast ---------- */
.message {
  position: fixed;
  top: 64px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
  flex-direction: column;
  align-items: center;
  z-index: 60;
  pointer-events: none;
}
.toast {
  background: var(--fg);
  color: var(--bg);
  padding: 11px 17px;
  border-radius: 8px;
  font-weight: 600;
  font-size: .92rem;
  box-shadow: var(--shadow);
  animation: toast-in .15s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } }
.toast.fade { animation: toast-out .3s ease forwards; }
@keyframes toast-out { to { opacity: 0; } }

/* ---------- Keyboard ---------- */
.keyboard {
  flex: 0 0 auto;
  width: 100%;
  max-width: 500px;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 6px 6px max(10px, env(safe-area-inset-bottom));
  user-select: none;
}
.kb-row {
  display: flex;
  gap: 5px;
  justify-content: center;
  touch-action: manipulation;
}
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 54px;
  border: none;
  border-radius: 6px;
  background: var(--key);
  color: var(--key-fg);
  font-family: 'Inter', sans-serif;
  font-weight: 600;
  font-size: 1.05rem;
  text-transform: uppercase;
  cursor: pointer;
  transition: background .12s, transform .04s;
}
.key:active { transform: scale(.95); }
.key:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.key.wide { flex: 1.5 1 0; font-size: .72rem; letter-spacing: .03em; }
.key.correct { background: var(--correct); color: #fff; }
.key.present { background: var(--present); color: #fff; }
.key.absent  { background: var(--absent);  color: #fff; opacity: .9; }
.key svg { width: 22px; height: 22px; fill: var(--key-fg); }

/* ---------- Modals ---------- */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.55);
  display: grid;
  place-items: start center;
  padding: 6vh 16px;
  z-index: 100;
  overflow-y: auto;
  animation: fade .2s ease;
}
:root[data-theme="light"] .modal-backdrop { background: rgba(20,21,26,.4); }
@keyframes fade { from { opacity: 0; } }
.modal-backdrop[hidden] { display: none; }

.modal {
  position: relative;
  background: var(--bg-elev);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  width: 100%;
  max-width: 420px;
  padding: 26px 26px 24px;
  animation: rise .22s ease;
}
@keyframes rise { from { opacity: 0; transform: translateY(14px); } }

.modal h2 {
  font-family: 'Fraunces', serif;
  font-size: 1.5rem;
  margin: 0 0 14px;
  text-align: center;
}
.modal h3 { font-size: .8rem; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-dim); text-align: center; margin: 22px 0 12px; }
.modal p { line-height: 1.5; margin: 8px 0; }
.modal-close {
  position: absolute;
  top: 12px; right: 14px;
  background: none; border: none;
  color: var(--fg-dim);
  font-size: 1.9rem;
  line-height: 1;
  cursor: pointer;
}
.modal-close:hover { color: var(--fg); }
.modal-foot { color: var(--fg-dim); font-size: .85rem; border-top: 1px solid var(--line); padding-top: 14px; margin-top: 18px; }

.rules { padding-left: 20px; line-height: 1.6; }
.rules li { margin: 4px 0; }
.examples { border-top: 1px solid var(--line); border-bottom: 1px solid var(--line); padding: 8px 0; margin: 16px 0; }
.example-row { display: flex; gap: 4px; margin: 14px 0 4px; }
.mini-tile {
  display: grid; place-items: center;
  width: 36px; height: 36px;
  border: 2px solid var(--tile-filled-border);
  border-radius: 3px;
  font-weight: 700; font-size: 1.1rem;
}
.mini-tile.correct { background: var(--correct); border-color: var(--correct); color: #fff; }
.mini-tile.present { background: var(--present); border-color: var(--present); color: #fff; }
.mini-tile.absent  { background: var(--absent);  border-color: var(--absent);  color: #fff; }

/* ---------- Stats ---------- */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  text-align: center;
}
.stat-num { font-size: 2rem; font-weight: 700; line-height: 1.1; font-family: 'Fraunces', serif; }
.stat-label { font-size: .68rem; color: var(--fg-dim); line-height: 1.2; margin-top: 2px; }

.dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: .85rem; font-variant-numeric: tabular-nums; }
.dist-key { width: 12px; text-align: center; font-weight: 600; color: var(--fg-dim); }
.dist-bar-wrap { flex: 1; }
.dist-bar {
  background: var(--absent);
  color: #fff;
  min-width: 24px;
  padding: 3px 8px;
  border-radius: 3px;
  text-align: right;
  font-weight: 600;
  font-size: .8rem;
  transition: width .5s ease;
}
.dist-bar.current { background: var(--correct); }

.share-row {
  display: flex;
  align-items: center;
  gap: 14px;
  margin-top: 22px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.countdown { flex: 1; display: flex; flex-direction: column; }
.cd-label { font-size: .68rem; text-transform: uppercase; letter-spacing: .08em; color: var(--fg-dim); }
.cd-time { font-size: 1.6rem; font-weight: 700; font-variant-numeric: tabular-nums; font-family: 'Fraunces', serif; }
.share-btn {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 14px;
  font-family: 'Inter', sans-serif;
  font-weight: 700;
  font-size: 1.05rem;
  text-transform: uppercase;
  letter-spacing: .04em;
  cursor: pointer;
  transition: filter .15s, transform .05s;
}
.share-btn:hover { filter: brightness(1.08); }
.share-btn:active { transform: scale(.98); }
.share-btn svg { width: 20px; height: 20px; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

.practice-row { text-align: center; margin-top: 14px; }
.ghost-btn {
  background: none;
  border: 1px solid var(--line);
  color: var(--fg-dim);
  border-radius: 8px;
  padding: 9px 16px;
  font-family: 'Inter', sans-serif;
  font-size: .85rem;
  font-weight: 500;
  cursor: pointer;
  transition: border-color .15s, color .15s;
}
.ghost-btn:hover { border-color: var(--accent); color: var(--fg); }

/* ---------- Responsive tweaks ---------- */
@media (max-width: 360px) {
  .key { height: 48px; font-size: .95rem; }
  .stat-num { font-size: 1.6rem; }
}
@media (min-height: 760px) {
  .key { height: 58px; }
}

/* Spacefast badge spacing safety */
[data-spacefast-badge] { z-index: 90; }

@media (prefers-reduced-motion: reduce) {
  .tile.reveal, .board-row.win .tile, .tile.filled { animation: none; }
}
