:root {
  --bg-0: #07070d;
  --bg-1: #0e0e1a;
  --bg-2: #151527;
  --ink: #f4f4fb;
  --ink-dim: #9aa0c0;
  --line: #262640;
  --correct: #4bd07e;
  --correct-deep: #1f7a45;
  --present: #ffb020;
  --present-deep: #9c6400;
  --absent: #34344e;
  --gold: #ffcf5c;
  --shadow: 0 10px 40px rgba(0, 0, 0, 0.55);
  --font: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial,
    sans-serif;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg-0);
  color: var(--ink);
  font-family: var(--font);
  user-select: none;
  -webkit-user-select: none;
}

body {
  background:
    radial-gradient(120% 80% at 50% -10%, #1a1a33 0%, rgba(26, 26, 51, 0) 55%),
    radial-gradient(90% 60% at 50% 115%, #161628 0%, rgba(22, 22, 40, 0) 60%),
    var(--bg-0);
}

#app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 560px;
  margin: 0 auto;
  padding: env(safe-area-inset-top) 8px env(safe-area-inset-bottom);
}

/* ---------- Top bar ---------- */
#topbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  padding: 10px 4px 8px;
  border-bottom: 1px solid var(--line);
}

.brand {
  text-align: center;
  line-height: 1;
}
.brand h1 {
  margin: 0;
  font-size: clamp(22px, 7vw, 30px);
  font-weight: 900;
  letter-spacing: 0.22em;
  text-indent: 0.22em;
  background: linear-gradient(180deg, #fff 0%, #c8c8ee 100%);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 2px 10px rgba(120, 120, 255, 0.18));
}
.brand .sub {
  display: block;
  margin-top: 3px;
  font-size: 11px;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--ink-dim);
}
.brand .sub b {
  color: var(--gold);
  font-weight: 700;
}

.topbar-right {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
}

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 0;
  border-radius: 10px;
  background: transparent;
  color: var(--ink-dim);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.icon-btn:hover {
  background: var(--bg-2);
  color: var(--ink);
}
.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}
#help-btn {
  justify-self: start;
}
.ic-sound-off {
  display: none;
}
body.muted .ic-sound-on {
  display: none;
}
body.muted .ic-sound-off {
  display: block;
}

/* ---------- Stage / canvas ---------- */
#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
}
#board {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  touch-action: manipulation;
}

#toast {
  position: absolute;
  top: 12px;
  left: 50%;
  transform: translateX(-50%) translateY(-8px);
  background: var(--ink);
  color: #11111c;
  font-weight: 700;
  font-size: 14px;
  letter-spacing: 0.01em;
  padding: 10px 16px;
  border-radius: 8px;
  box-shadow: var(--shadow);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
  white-space: nowrap;
}
#toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}

/* ---------- Keyboard ---------- */
#keyboard {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 8px 2px 6px;
}
.krow {
  display: flex;
  justify-content: center;
  gap: 6px;
}
.key {
  flex: 1 1 0;
  min-width: 0;
  height: 54px;
  border: 0;
  border-radius: 9px;
  background: #2b2b46;
  color: var(--ink);
  font-family: inherit;
  font-size: 14px;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  transition: transform 0.05s, background 0.2s, color 0.2s, box-shadow 0.2s;
  display: grid;
  place-items: center;
}
.key:active {
  transform: translateY(1px) scale(0.97);
}
.key.wide {
  flex: 1.5 1 0;
  font-size: 11px;
  letter-spacing: 0.04em;
}
.key[data-state="correct"] {
  background: linear-gradient(180deg, var(--correct) 0%, var(--correct-deep) 100%);
  color: #06160c;
  box-shadow: 0 0 14px rgba(75, 208, 126, 0.4);
}
.key[data-state="present"] {
  background: linear-gradient(180deg, var(--present) 0%, var(--present-deep) 100%);
  color: #1c1303;
  box-shadow: 0 0 14px rgba(255, 176, 32, 0.32);
}
.key[data-state="absent"] {
  background: #1a1a2b;
  color: #5a5a78;
}

/* ---------- Modals ---------- */
.modal-scrim {
  position: fixed;
  inset: 0;
  background: rgba(4, 4, 10, 0.66);
  backdrop-filter: blur(6px);
  display: grid;
  place-items: center;
  z-index: 20;
  padding: 16px;
  animation: fade 0.18s ease;
}
.modal-scrim[hidden] {
  display: none;
}
.modal {
  position: relative;
  width: 100%;
  max-width: 400px;
  background: linear-gradient(180deg, var(--bg-2) 0%, var(--bg-1) 100%);
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 26px 22px 22px;
  box-shadow: var(--shadow);
  animation: pop 0.2s cubic-bezier(0.2, 0.9, 0.3, 1.2);
}
.modal h2 {
  margin: 0 0 14px;
  font-size: 16px;
  letter-spacing: 0.16em;
  text-transform: uppercase;
  text-align: center;
}
.modal h3 {
  margin: 18px 0 8px;
  font-size: 12px;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-dim);
  text-align: center;
}
.modal p {
  margin: 0 0 12px;
  line-height: 1.5;
  font-size: 15px;
  color: #d8d8ec;
}
.modal .muted {
  color: var(--ink-dim);
  font-size: 13px;
}
.close-btn {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 36px;
  height: 36px;
  border: 0;
  background: transparent;
  color: var(--ink-dim);
  font-size: 26px;
  line-height: 1;
  cursor: pointer;
  border-radius: 8px;
}
.close-btn:hover {
  background: var(--bg-0);
  color: var(--ink);
}

.legend {
  list-style: none;
  padding: 0;
  margin: 0 0 14px;
  display: flex;
  flex-direction: column;
  gap: 11px;
}
.legend li {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 14px;
  color: #d8d8ec;
}
.chip {
  flex: 0 0 auto;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  font-weight: 800;
  font-size: 17px;
  color: #0a0a12;
}
.chip-correct {
  background: linear-gradient(180deg, var(--correct), var(--correct-deep));
  color: #06160c;
  box-shadow: 0 0 14px rgba(75, 208, 126, 0.45);
}
.chip-present {
  background: linear-gradient(180deg, var(--present), var(--present-deep));
  color: #1c1303;
}
.chip.tilt {
  transform: rotate(-11deg);
}
.chip-absent {
  background: #20203400;
  border: 2px dashed #3a3a55;
  color: #4e4e6e;
}

/* ---------- Stats ---------- */
.stat-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  margin-bottom: 6px;
}
.stat-grid .stat {
  text-align: center;
}
.stat .num {
  font-size: 26px;
  font-weight: 800;
}
.stat .lbl {
  font-size: 10px;
  color: var(--ink-dim);
  line-height: 1.2;
  margin-top: 2px;
}
.dist {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.dist .bar-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
}
.dist .bar-row .gi {
  width: 10px;
  color: var(--ink-dim);
  text-align: right;
}
.dist .bar {
  background: var(--absent);
  height: 22px;
  border-radius: 4px;
  min-width: 24px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  padding: 0 7px;
  font-weight: 700;
  font-size: 12px;
  color: var(--ink);
  transition: width 0.5s cubic-bezier(0.2, 0.9, 0.3, 1);
}
.dist .bar.cur {
  background: linear-gradient(90deg, var(--correct-deep), var(--correct));
  color: #06160c;
}

.result-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 20px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}
.countdown {
  display: flex;
  flex-direction: column;
  gap: 2px;
}
.countdown .muted {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
}
#countdown {
  font-size: 22px;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}
.share-btn {
  border: 0;
  border-radius: 10px;
  padding: 14px 26px;
  font-family: inherit;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: #06160c;
  background: linear-gradient(180deg, var(--correct) 0%, var(--correct-deep) 100%);
  box-shadow: 0 0 20px rgba(75, 208, 126, 0.35);
  cursor: pointer;
  transition: transform 0.06s, filter 0.15s;
}
.share-btn:hover {
  filter: brightness(1.07);
}
.share-btn:active {
  transform: scale(0.97);
}

@keyframes fade {
  from {
    opacity: 0;
  }
}
@keyframes pop {
  from {
    opacity: 0;
    transform: translateY(10px) scale(0.96);
  }
}

@media (max-height: 680px) {
  .key {
    height: 46px;
  }
  .brand .sub {
    display: none;
  }
}
