/* ============ Design tokens ============ */
:root {
  --bg: #010409;
  --surface: rgba(13, 17, 23, 0.72);
  --glass: rgba(240, 246, 252, 0.045);
  --glass-strong: rgba(240, 246, 252, 0.09);
  --border: rgba(240, 246, 252, 0.10);
  --border-strong: rgba(240, 246, 252, 0.28);
  --text: #e6edf3;
  --text-dim: #8b949e;
  --blue: #58a6ff;

  --green-1: #4fd365;
  --green-2: #238636;
  --amber-1: #f0c04a;
  --amber-2: #b7790b;
  --absent: #1c2129;
  --absent-border: #2d333b;

  /* Height budget: ~300px for header + keyboard, the rest shared by 6 rows. */
  --tile: clamp(30px, min(14vw, calc((100dvh - 300px) / 6.8)), 64px);
  --gap: clamp(5px, 1.2dvh, 8px);
  --radius: 12px;
  --font: "JetBrains Mono", ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  --ease-out: cubic-bezier(.2, .8, .2, 1);
  --ease-spring: cubic-bezier(.34, 1.56, .64, 1);
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body { height: 100%; }

html { background: var(--bg); }

body {
  font-family: var(--font);
  color: var(--text);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

button { font: inherit; color: inherit; border: 0; background: none; cursor: pointer; }
a { color: var(--blue); text-decoration: none; }
a:hover { text-decoration: underline; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

.muted { color: var(--text-dim); }
.small { font-size: 12px; }

/* ============ Background ============ */
.bg { position: fixed; inset: 0; z-index: -1; overflow: hidden; pointer-events: none; }
.glow {
  position: absolute;
  width: 70vmax; height: 70vmax;
  border-radius: 50%;
  filter: blur(90px);
  opacity: .5;
  animation: drift 24s ease-in-out infinite alternate;
}
.glow-a { background: radial-gradient(circle, rgba(63, 185, 80, .35), transparent 60%); top: -35vmax; left: -25vmax; }
.glow-b { background: radial-gradient(circle, rgba(88, 166, 255, .28), transparent 60%); bottom: -40vmax; right: -25vmax; animation-delay: -12s; }
.grid-lines {
  position: absolute; inset: 0;
  background-image:
    linear-gradient(rgba(240, 246, 252, .035) 1px, transparent 1px),
    linear-gradient(90deg, rgba(240, 246, 252, .035) 1px, transparent 1px);
  background-size: 44px 44px;
  mask-image: radial-gradient(ellipse at center, #000 20%, transparent 75%);
}
@keyframes drift {
  to { transform: translate(8vmax, 6vmax) scale(1.1); }
}

/* ============ Layout ============ */
.app {
  height: 100dvh;
  max-width: 520px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  padding: max(10px, env(safe-area-inset-top)) 10px max(10px, env(safe-area-inset-bottom));
}

/* Terminal-style header */
.bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 14px;
  backdrop-filter: blur(16px) saturate(140%);
  -webkit-backdrop-filter: blur(16px) saturate(140%);
  box-shadow: 0 10px 40px rgba(0, 0, 0, .35), inset 0 1px 0 rgba(255, 255, 255, .04);
}
.dots { display: flex; gap: 7px; }
.dot { width: 11px; height: 11px; border-radius: 50%; }
.dot-red { background: #ff5f57; }
.dot-yellow { background: #febc2e; }
.dot-green { background: #28c840; }

.title { font-size: 16px; font-weight: 700; letter-spacing: .02em; white-space: nowrap; }
.prompt { color: #3fb950; }
.path { color: var(--blue); }
.cursor {
  display: inline-block; width: 8px; height: 16px; margin-left: 4px;
  background: #3fb950; vertical-align: -2px;
  animation: blink 1s steps(1) infinite;
}
@keyframes blink { 50% { opacity: 0; } }

.actions { display: flex; justify-content: flex-end; gap: 4px; }
.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 9px;
  color: var(--text-dim);
  transition: background .2s, color .2s, transform .2s var(--ease-spring);
}
.icon-btn:hover { background: var(--glass-strong); color: var(--text); }
.icon-btn:active { transform: scale(.9); }
.icon-btn svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.icon-btn svg .fill { fill: currentColor; }

.subline { text-align: center; font-size: 12px; margin: 12px 0 4px; letter-spacing: .08em; text-transform: uppercase; }
.subline #game-number { color: var(--text); font-weight: 700; }

/* ============ Board ============ */
.board-wrap { flex: 1; display: grid; place-items: center; min-height: 0; }
.board { display: grid; grid-template-rows: repeat(6, var(--tile)); gap: var(--gap); }
.row { display: grid; grid-template-columns: repeat(5, var(--tile)); gap: var(--gap); }

.tile {
  position: relative;
  display: grid; place-items: center;
  font-size: calc(var(--tile) * .5);
  font-weight: 800;
  text-transform: none;
  border-radius: var(--radius);
  background: var(--glass);
  border: 1.5px solid var(--border);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  user-select: none;
  transition: border-color .15s, background .15s;
}
.tile.filled {
  border-color: var(--border-strong);
  background: var(--glass-strong);
  animation: pop .12s var(--ease-out);
}
.row.active .tile:not(.filled) { border-color: rgba(240, 246, 252, .16); }

.tile[data-state="correct"], .key[data-state="correct"], .mini.correct {
  background: linear-gradient(150deg, var(--green-1), var(--green-2));
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 22px rgba(63, 185, 80, .32), inset 0 1px 0 rgba(255, 255, 255, .25);
}
.tile[data-state="present"], .key[data-state="present"], .mini.present {
  background: linear-gradient(150deg, var(--amber-1), var(--amber-2));
  border-color: transparent;
  color: #fff;
  box-shadow: 0 6px 22px rgba(210, 153, 34, .28), inset 0 1px 0 rgba(255, 255, 255, .25);
}
.tile[data-state="absent"], .mini.absent {
  background: var(--absent);
  border-color: var(--absent-border);
  color: var(--text-dim);
}

.tile.flip-in { animation: flip-in .25s ease-in forwards; }
.tile.flip-out { animation: flip-out .25s ease-out forwards; }
.row.shake { animation: shake .5s; }
.tile.bounce { animation: bounce .9s var(--ease-out); }

@keyframes pop { 0% { transform: scale(.85); } 60% { transform: scale(1.08); } 100% { transform: scale(1); } }
@keyframes flip-in { to { transform: rotateX(90deg); } }
@keyframes flip-out { from { transform: rotateX(90deg); } to { transform: rotateX(0); } }
@keyframes shake {
  10%, 90% { transform: translateX(-2px); }
  20%, 80% { transform: translateX(4px); }
  30%, 50%, 70% { transform: translateX(-7px); }
  40%, 60% { transform: translateX(7px); }
}
@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-38%); }
  50% { transform: translateY(4%); }
  60% { transform: translateY(-14%); }
  80% { transform: translateY(2%); }
  100% { transform: translateY(0); }
}

/* ============ Keyboard ============ */
.keyboard { display: flex; flex-direction: column; gap: 7px; padding-top: 10px; user-select: none; }
.kb-row { display: flex; justify-content: center; gap: 5px; }
.key {
  flex: 1 1 0;
  max-width: 44px;
  height: clamp(36px, 7dvh, 58px);
  display: grid; place-items: center;
  font-size: 15px; font-weight: 700;
  border-radius: 9px;
  background: rgba(240, 246, 252, .075);
  border: 1px solid rgba(240, 246, 252, .07);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, .05), 0 2px 0 rgba(0, 0, 0, .35);
  transition: background .25s, transform .12s var(--ease-spring), box-shadow .25s, color .25s;
}
.key:hover { background: rgba(240, 246, 252, .13); }
.key:active, .key.pressed { transform: translateY(1px) scale(.94); }
.key.wide { flex: 1.6 1 0; max-width: 72px; font-size: 11px; letter-spacing: .04em; }
.key.wide svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.key[data-state="absent"] { background: rgba(240, 246, 252, .02); color: #484f58; border-color: transparent; box-shadow: none; }

/* ============ Toasts ============ */
.toasts {
  position: fixed; top: 84px; left: 50%; transform: translateX(-50%);
  display: flex; flex-direction: column; align-items: center; gap: 8px;
  z-index: 50; pointer-events: none;
}
.toast {
  padding: 10px 16px;
  font-size: 14px; font-weight: 700;
  color: #0d1117;
  background: #f0f6fc;
  border-radius: 10px;
  box-shadow: 0 12px 32px rgba(0, 0, 0, .45);
  animation: toast-in .25s var(--ease-spring);
}
.toast.out { animation: toast-out .3s ease forwards; }
@keyframes toast-in { from { opacity: 0; transform: translateY(-8px) scale(.95); } }
@keyframes toast-out { to { opacity: 0; transform: translateY(-6px); } }

/* ============ Modals ============ */
.modal {
  position: fixed; inset: 0; z-index: 40;
  display: grid; place-items: center;
  padding: 16px;
  background: rgba(1, 4, 9, .55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  animation: fade .2s ease;
}
.modal[hidden] { display: none; }
.modal.closing { animation: fade .18s ease reverse forwards; }
.modal-panel {
  position: relative;
  width: 100%; max-width: 420px; max-height: 90dvh; overflow-y: auto;
  padding: 26px 24px 24px;
  background: linear-gradient(160deg, rgba(22, 27, 34, .92), rgba(13, 17, 23, .88));
  border: 1px solid var(--border);
  border-radius: 20px;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .6), inset 0 1px 0 rgba(255, 255, 255, .05);
  backdrop-filter: blur(24px) saturate(150%);
  -webkit-backdrop-filter: blur(24px) saturate(150%);
  animation: rise .35s var(--ease-spring);
}
.modal.closing .modal-panel { animation: rise .18s ease reverse forwards; }
.modal-panel:focus { outline: none; }
@keyframes fade { from { opacity: 0; } }
@keyframes rise { from { opacity: 0; transform: translateY(18px) scale(.97); } }

.close-btn {
  position: absolute; top: 14px; right: 14px;
  width: 32px; height: 32px; border-radius: 8px;
  color: var(--text-dim);
  transition: background .2s, color .2s;
}
.close-btn:hover { background: var(--glass-strong); color: var(--text); }

.modal h2 { font-size: 18px; margin-bottom: 14px; }
.modal h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .1em; color: var(--text-dim); margin: 20px 0 10px; }
.modal p { font-size: 14px; line-height: 1.6; }
.rules { margin: 10px 0 0 18px; font-size: 14px; line-height: 1.6; }
.rules li { margin-bottom: 6px; }
.rules li::marker { color: #3fb950; }

.example { margin-bottom: 14px; }
.example p { margin-top: 6px; color: var(--text-dim); }
.example strong { color: var(--text); }
.mini-row { display: flex; gap: 5px; }
.mini {
  width: 38px; height: 38px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 18px;
  border-radius: 8px;
  border: 1.5px solid var(--border-strong);
  background: var(--glass);
}

/* Result + stats */
.result { text-align: center; margin-bottom: 18px; }
.result-label { font-size: 13px; text-transform: uppercase; letter-spacing: .12em; color: var(--text-dim); }
.result-word { display: flex; justify-content: center; gap: 5px; margin-top: 10px; }

.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  padding: 12px 4px;
  background: var(--glass);
  border: 1px solid var(--border);
  border-radius: 12px;
}
.stat-num { font-size: 24px; font-weight: 800; }
.stat-label { font-size: 10px; color: var(--text-dim); text-align: center; }

.dist { display: flex; flex-direction: column; gap: 5px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.dist-row > span { width: 10px; color: var(--text-dim); }
.dist-bar {
  min-width: 26px;
  padding: 3px 8px;
  text-align: right; font-weight: 700;
  background: rgba(240, 246, 252, .1);
  border-radius: 6px;
  transform-origin: left;
  animation: grow .6s var(--ease-out) both;
}
.dist-bar.highlight { background: linear-gradient(90deg, var(--green-2), var(--green-1)); }
@keyframes grow { from { transform: scaleX(0); } }

.footer-row {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  margin-top: 22px; padding-top: 18px;
  border-top: 1px solid var(--border);
}
.footer-row[hidden] { display: none; }
.countdown { display: flex; flex-direction: column; }
.timer { font-size: 26px; font-weight: 800; letter-spacing: .04em; font-variant-numeric: tabular-nums; }
.share-btn {
  display: inline-flex; align-items: center; gap: 8px;
  padding: 12px 20px;
  font-weight: 800; font-size: 15px;
  color: #fff;
  border-radius: 12px;
  background: linear-gradient(150deg, var(--green-1), var(--green-2));
  box-shadow: 0 10px 28px rgba(63, 185, 80, .35), inset 0 1px 0 rgba(255, 255, 255, .25);
  transition: transform .2s var(--ease-spring), box-shadow .2s;
}
.share-btn:hover { transform: translateY(-2px); box-shadow: 0 14px 34px rgba(63, 185, 80, .45), inset 0 1px 0 rgba(255, 255, 255, .25); }
.share-btn:active { transform: scale(.96); }
.share-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2.4; stroke-linecap: round; stroke-linejoin: round; }

.credit { position: fixed; right: 16px; bottom: 12px; font-size: 12px; opacity: .55; }
.credit:hover { opacity: 1; }
@media (max-width: 760px), (max-height: 700px) { .credit { display: none; } }

@media (max-width: 380px) {
  .title { font-size: 14px; }
  .dots { gap: 5px; }
  .dot { width: 9px; height: 9px; }
  .key { font-size: 13px; border-radius: 7px; }
  .kb-row { gap: 4px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .001ms !important; animation-iteration-count: 1 !important; transition-duration: .001ms !important; }
}
