:root {
  color-scheme: dark;

  --bg: #0f1115;
  --bg-elev: #14171d;
  --surface: #1a1e26;
  --surface-2: #232833;

  --ink: #f1f3f5;
  --ink-strong: #ffffff;
  --muted: #9aa3b2;
  --faint: #5d6675;

  --line: #232833;
  --line-strong: #2f3542;
  --line-bright: #404758;

  --accent: #10b981;
  --accent-dark: #059669;
  --accent-soft: rgba(16, 185, 129, 0.12);

  --correct: #16a34a;
  --correct-dark: #0f7d36;
  --present: #d97706;
  --present-dark: #b15800;
  --absent: #2a3040;
  --absent-ink: #6b7384;

  --danger: #f87171;
  --shadow-lg: 0 24px 60px rgba(0, 0, 0, 0.5);
  --shadow-md: 0 10px 24px rgba(0, 0, 0, 0.35);

  --radius-lg: 16px;
  --radius-md: 10px;
  --radius-sm: 6px;
  --radius-pill: 999px;

  --tile-size: min(64px, calc((100vw - 24px - 4 * 6px) / 5));
  --tile-gap: 6px;
  --key-h: 56px;

  --font-sans: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", "PingFang SC", "Noto Sans CJK SC", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-sans);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  overscroll-behavior-y: none;
  -webkit-tap-highlight-color: transparent;
}

body::before {
  position: fixed;
  inset: 0;
  z-index: -1;
  pointer-events: none;
  background:
    radial-gradient(ellipse at 50% -10%, rgba(16, 185, 129, 0.10), transparent 40%),
    radial-gradient(ellipse at 50% 110%, rgba(96, 165, 250, 0.04), transparent 40%);
  content: "";
}

/* ---------------- Top bar ---------------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: grid;
  grid-template-columns: 44px 1fr 44px;
  align-items: center;
  gap: 0.5rem;
  height: 60px;
  padding: 0 0.75rem;
  border-bottom: 1px solid var(--line);
  background: rgba(15, 17, 21, 0.78);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
}

.brand {
  margin: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.55rem;
  font-size: 1.25rem;
  font-weight: 800;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--ink-strong);
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 1.9rem;
  height: 1.9rem;
  border-radius: var(--radius-sm);
  background: linear-gradient(135deg, var(--accent), var(--accent-dark));
  color: #06121a;
  font-weight: 900;
  font-size: 1.05rem;
  letter-spacing: 0;
  box-shadow: 0 6px 16px rgba(16, 185, 129, 0.35);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 40px;
  height: 40px;
  border: 1px solid transparent;
  border-radius: var(--radius-md);
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, border-color 0.15s ease;
}

.icon-btn:hover {
  background: var(--surface);
  color: var(--ink);
}

.icon-btn:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.3);
}

.icon-btn svg { width: 22px; height: 22px; }

/* ---------------- Stage ---------------- */

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.5rem;
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 1rem 0.75rem 1.25rem;
}

/* ---------------- Board ---------------- */

.board {
  display: grid;
  grid-template-rows: repeat(6, var(--tile-size));
  gap: var(--tile-gap);
  padding: 0.5rem 0 0.75rem;
}

.row {
  display: grid;
  grid-template-columns: repeat(5, var(--tile-size));
  gap: var(--tile-gap);
}

.row.shake { animation: shake 0.45s cubic-bezier(.36,.07,.19,.97); }
.row.bounce .tile { animation: bounce 0.7s ease forwards; }
.row.bounce .tile:nth-child(2) { animation-delay: 0.08s; }
.row.bounce .tile:nth-child(3) { animation-delay: 0.16s; }
.row.bounce .tile:nth-child(4) { animation-delay: 0.24s; }
.row.bounce .tile:nth-child(5) { animation-delay: 0.32s; }

.tile {
  display: grid;
  place-items: center;
  width: var(--tile-size);
  height: var(--tile-size);
  border: 2px solid var(--line-strong);
  border-radius: 4px;
  background: transparent;
  color: var(--ink-strong);
  font-size: clamp(1.6rem, 5vw, 2.1rem);
  font-weight: 700;
  text-transform: uppercase;
  user-select: none;
  transition: border-color 0.12s ease, transform 0.1s ease;
}

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

.tile.flip { animation: flip 0.55s ease forwards; }
.tile.flip:nth-child(2) { animation-delay: 0.18s; }
.tile.flip:nth-child(3) { animation-delay: 0.36s; }
.tile.flip:nth-child(4) { animation-delay: 0.54s; }
.tile.flip:nth-child(5) { animation-delay: 0.72s; }

.tile.reveal { animation: none; }
.tile.correct {
  background: linear-gradient(180deg, var(--correct), var(--correct-dark));
  border-color: transparent;
  color: #fff;
}
.tile.present {
  background: linear-gradient(180deg, var(--present), var(--present-dark));
  border-color: transparent;
  color: #fff;
}
.tile.absent {
  background: var(--absent);
  border-color: transparent;
  color: var(--absent-ink);
}

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

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  10% { transform: translateX(-6px); }
  20% { transform: translateX(6px); }
  30% { transform: translateX(-6px); }
  40% { transform: translateX(6px); }
  50% { transform: translateX(-4px); }
  60% { transform: translateX(4px); }
  70% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
  90% { transform: translateX(0); }
}

@keyframes flip {
  0%   { transform: rotateX(0); }
  45%  { transform: rotateX(90deg); border-color: transparent; }
  55%  { transform: rotateX(90deg); border-color: transparent; }
  100% { transform: rotateX(0); border-color: transparent; }
}

@keyframes bounce {
  0%   { transform: translateY(0); }
  20%  { transform: translateY(-18px); }
  50%  { transform: translateY(4px); }
  70%  { transform: translateY(-6px); }
  100% { transform: translateY(0); }
}

/* ---------------- Game message ---------------- */

.game-message {
  margin: 0;
  min-height: 1.25rem;
  font-size: 0.86rem;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0.02em;
}

.game-message.win  { color: var(--correct); font-weight: 600; }
.game-message.lose { color: var(--danger); font-weight: 600; }

/* ---------------- Keyboard ---------------- */

.keyboard {
  display: grid;
  grid-template-rows: repeat(3, auto);
  gap: 6px;
  width: 100%;
  max-width: 500px;
  margin-top: 0.5rem;
  padding: 0 4px;
}

.kb-row {
  display: flex;
  gap: 5px;
  justify-content: center;
  width: 100%;
}

.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: 44px;
  height: var(--key-h);
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--ink);
  font: inherit;
  font-size: 1rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.06s ease;
  user-select: none;
}

.key:hover { background: #2f3441; }
.key:active { transform: scale(0.96); }

.key.wide {
  flex: 1.4 1 0;
  max-width: 60px;
  font-size: 0.74rem;
  letter-spacing: 0.02em;
}

.key svg { width: 18px; height: 18px; pointer-events: none; }

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

.key:focus-visible {
  outline: none;
  box-shadow: 0 0 0 3px rgba(16, 185, 129, 0.4);
}

/* ---------------- Modal ---------------- */

.modal {
  width: min(440px, calc(100% - 2rem));
  max-height: 90vh;
  margin: auto;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: var(--radius-lg);
  background: var(--bg-elev);
  color: var(--ink);
  box-shadow: var(--shadow-lg);
}

.modal::backdrop {
  background: rgba(5, 7, 10, 0.6);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
}

.modal-inner {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  padding: 1.25rem 1.35rem 1.4rem;
  overflow-y: auto;
  max-height: 90vh;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 0.25rem;
}

.modal h2 {
  margin: 0;
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--ink-strong);
}

.modal p, .modal li {
  margin: 0;
  color: var(--muted);
  font-size: 0.92rem;
  line-height: 1.55;
}

.modal strong { color: var(--ink-strong); }

.rules { padding-left: 1.2rem; display: flex; flex-direction: column; gap: 0.35rem; }

.examples-title, .guess-dist-title {
  margin: 0.4rem 0 0;
  color: var(--ink-strong);
  font-size: 0.82rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
}

.example { display: flex; flex-direction: column; gap: 0.45rem; }

.example-row {
  display: flex;
  gap: 4px;
}

.example-row .tile {
  width: 38px;
  height: 38px;
  font-size: 1.15rem;
  border-width: 2px;
}

.modal-foot {
  margin-top: 0.4rem;
  padding-top: 0.8rem;
  border-top: 1px dashed var(--line);
  color: var(--faint);
  font-size: 0.8rem;
}

/* ---------------- Stats ---------------- */

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 0.5rem;
  margin: 0.2rem 0;
}

.stats-grid > div {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  padding: 0.6rem 0.3rem;
  border-radius: var(--radius-md);
  background: var(--surface);
}

.stats-grid strong {
  color: var(--ink-strong);
  font-size: 1.5rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.02em;
}

.stats-grid span {
  color: var(--muted);
  font-size: 0.7rem;
  text-align: center;
  line-height: 1.2;
  letter-spacing: 0.02em;
}

.guess-dist {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.guess-dist-row {
  display: grid;
  grid-template-columns: 1rem 1fr auto;
  gap: 0.5rem;
  align-items: center;
  font-size: 0.82rem;
  color: var(--muted);
}

.guess-dist-row > .dist-label {
  font-weight: 700;
  color: var(--ink);
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.dist-bar {
  height: 22px;
  border-radius: var(--radius-sm);
  background: var(--surface);
  position: relative;
  overflow: hidden;
}

.dist-bar-fill {
  position: absolute;
  inset: 0 auto 0 0;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 0.78rem;
  font-variant-numeric: tabular-nums;
  padding-right: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  min-width: 1.6rem;
  border-radius: inherit;
  transition: width 0.4s ease;
}

.dist-bar-fill.current {
  background: linear-gradient(180deg, var(--correct), var(--correct-dark));
}

.dist-bar-fill.empty {
  background: var(--surface-2);
  color: var(--muted);
}

.result-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.8rem;
  margin-top: 0.6rem;
  padding-top: 0.9rem;
  border-top: 1px dashed var(--line);
}

.next-puzzle { display: flex; flex-direction: column; gap: 0.15rem; }
.next-puzzle-label {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}
.next-puzzle-time {
  color: var(--ink-strong);
  font-family: ui-monospace, SFMono-Regular, monospace;
  font-size: 1.4rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

.share-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  height: 44px;
  padding: 0 1.1rem;
  border: 0;
  border-radius: var(--radius-pill);
  background: linear-gradient(180deg, var(--accent), var(--accent-dark));
  color: #06121a;
  font: inherit;
  font-weight: 800;
  font-size: 0.95rem;
  letter-spacing: 0.02em;
  cursor: pointer;
  box-shadow: 0 8px 18px rgba(16, 185, 129, 0.3);
  transition: transform 0.08s ease, box-shadow 0.15s ease;
}

.share-btn:hover {
  box-shadow: 0 10px 22px rgba(16, 185, 129, 0.4);
}

.share-btn:active { transform: scale(0.97); }

.share-btn svg { width: 18px; height: 18px; }

/* ---------------- Toast ---------------- */

.toast {
  position: fixed;
  top: 80px;
  left: 50%;
  transform: translate(-50%, -8px);
  opacity: 0;
  pointer-events: none;
  border-radius: var(--radius-md);
  background: var(--ink);
  color: #06121a;
  padding: 0.75rem 1.1rem;
  font-size: 0.88rem;
  font-weight: 600;
  letter-spacing: 0.01em;
  box-shadow: var(--shadow-md);
  transition: transform 0.22s ease, opacity 0.22s ease;
  z-index: 50;
}

.toast.show {
  transform: translate(-50%, 0);
  opacity: 1;
}

.toast.success { background: var(--correct); color: #fff; }
.toast.error   { background: var(--danger); color: #fff; }

/* ---------------- Responsive ---------------- */

@media (max-width: 360px) {
  :root { --tile-size: 46px; --key-h: 48px; }
  .key { font-size: 0.92rem; max-width: 40px; }
}

@media (min-width: 420px) and (max-height: 720px) {
  :root { --tile-size: 56px; --key-h: 50px; }
}

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