:root {
  --color-bg: #f7f7fb;
  --color-text: #1a1a2e;
  --color-header-border: #e3e3ee;
  --color-tile-border: #d3d6da;
  --color-tile-border-filled: #9a9dab;
  --color-correct: #6aaa64;
  --color-present: #e8b923;
  --color-absent: #93959c;
  --color-key-bg: #e5e7ec;
  --color-key-text: #1a1a2e;
  --color-modal-bg: #ffffff;
  --color-modal-overlay: rgba(20, 20, 35, 0.6);
  --color-accent: #5a67f2;
  --color-accent-2: #ff8fa3;
  --font-fun: 'Baloo 2', 'Nunito', system-ui, sans-serif;
  --font-body: 'Nunito', system-ui, -apple-system, sans-serif;
}

[data-theme="dark"] {
  --color-bg: #121213;
  --color-text: #f5f5f7;
  --color-header-border: #2c2c30;
  --color-tile-border: #3a3a3c;
  --color-tile-border-filled: #565758;
  --color-correct: #538d4e;
  --color-present: #b59f3b;
  --color-absent: #3a3a3c;
  --color-key-bg: #565758;
  --color-key-text: #f5f5f7;
  --color-modal-bg: #1e1e20;
  --color-modal-overlay: rgba(0, 0, 0, 0.75);
}

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

html, body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: var(--font-body);
  height: 100%;
  overscroll-behavior: none;
  transition: background 0.25s ease, color 0.25s ease;
}

.app {
  max-width: 500px;
  margin: 0 auto;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  border-bottom: 1px solid var(--color-header-border);
}

.title {
  font-family: var(--font-fun);
  font-weight: 800;
  font-size: 1.5rem;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 6px;
  letter-spacing: 0.5px;
}
.title-icon { font-size: 1.3rem; }
.title-junior {
  color: var(--color-accent);
  background: linear-gradient(90deg, var(--color-accent), var(--color-accent-2));
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}

.header-actions { display: flex; gap: 4px; }

.icon-btn {
  background: none;
  border: none;
  cursor: pointer;
  color: var(--color-text);
  width: 36px;
  height: 36px;
  padding: 6px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s ease;
}
.icon-btn:hover { background: var(--color-header-border); }
.icon-btn svg { width: 22px; height: 22px; }

.subtitle-bar {
  text-align: center;
  font-size: 0.85rem;
  padding: 8px 16px;
  color: var(--color-text);
  opacity: 0.75;
}

/* Game area */
.game {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  /* Center the board+keyboard as one group instead of pinning the keyboard
     to the very bottom — on tall viewports (tablets, desktop) that used to
     leave a huge empty gap between them. The gap below scales gently with
     viewport height so it still feels intentional on any screen size. */
  justify-content: center;
  padding: 6px 12px 12px;
  gap: clamp(20px, 4vh, 56px);
  position: relative;
}

.board {
  display: grid;
  grid-template-rows: repeat(7, 1fr);
  gap: 6px;
  width: 100%;
  max-width: 300px;
  margin: 6px auto;
}

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

.tile {
  width: 100%;
  aspect-ratio: 1 / 1;
  border: 2px solid var(--color-tile-border);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--font-fun);
  font-size: 1.9rem;
  font-weight: 700;
  text-transform: uppercase;
  color: var(--color-text);
  user-select: none;
  transition: transform 0.1s ease;
}

.tile.small {
  width: 40px;
  height: 40px;
  font-size: 1.2rem;
  display: inline-flex;
  margin-right: 4px;
}

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

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

.tile.flip {
  animation: flip 0.55s ease forwards;
}

.board-row.shake {
  animation: shake 0.5s ease;
}

.board-row.win .tile {
  animation: bounce 0.55s ease;
}

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

@keyframes flip {
  0% { transform: rotateX(0deg); }
  50% { transform: rotateX(90deg); }
  51% { transform: rotateX(90deg); }
  100% { transform: rotateX(0deg); }
}

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

@keyframes bounce {
  0%, 20% { transform: translateY(0); }
  40% { transform: translateY(-18px); }
  50% { transform: translateY(0); }
  60% { transform: translateY(-8px); }
  80% { transform: translateY(0); }
}

/* Toasts */
.toast-container {
  position: absolute;
  top: 4px;
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  z-index: 50;
  pointer-events: none;
}

.toast {
  background: var(--color-text);
  color: var(--color-bg);
  padding: 10px 16px;
  border-radius: 8px;
  font-weight: 700;
  font-size: 0.9rem;
  opacity: 0.95;
  animation: fadeInOut 1.6s ease forwards;
  white-space: nowrap;
}

@keyframes fadeInOut {
  0% { opacity: 0; transform: translateY(-8px); }
  10% { opacity: 0.95; transform: translateY(0); }
  85% { opacity: 0.95; }
  100% { opacity: 0; }
}

/* Keyboard */
.keyboard {
  width: 100%;
  max-width: 480px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.keyboard-row {
  display: flex;
  justify-content: center;
  gap: 6px;
}

.key {
  font-family: var(--font-body);
  border: none;
  border-radius: 6px;
  background: var(--color-key-bg);
  color: var(--color-key-text);
  font-weight: 700;
  font-size: 0.85rem;
  height: 46px;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  text-transform: uppercase;
  transition: transform 0.05s ease, background 0.2s ease;
}
.key:active { transform: scale(0.94); }
.key.wide { flex: 1.6; font-size: 0.7rem; }
.key svg { width: 20px; height: 20px; fill: var(--color-key-text); }

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

/* Footer */
.footer-actions {
  display: flex;
  justify-content: center;
  padding: 10px 0 18px;
}

.pill-btn {
  background: var(--color-accent);
  color: #fff;
  border: none;
  border-radius: 24px;
  padding: 12px 26px;
  font-family: var(--font-fun);
  font-weight: 700;
  font-size: 1rem;
  cursor: pointer;
  box-shadow: 0 3px 0 rgba(0,0,0,0.15);
  transition: transform 0.1s ease;
}
.pill-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,0.15); }
.hidden { display: none !important; }

/* Modals */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: var(--color-modal-overlay);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-overlay.open { display: flex; }

.modal {
  background: var(--color-modal-bg);
  color: var(--color-text);
  border-radius: 16px;
  padding: 24px;
  max-width: 380px;
  width: 100%;
  position: relative;
  max-height: 85vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}

.modal h2 {
  font-family: var(--font-fun);
  margin-top: 0;
  text-align: center;
}

.modal p { font-size: 0.92rem; line-height: 1.4; }

.modal-close {
  position: absolute;
  top: 10px;
  right: 14px;
  background: none;
  border: none;
  font-size: 1.6rem;
  cursor: pointer;
  color: var(--color-text);
  line-height: 1;
}

.example-row { display: flex; margin: 10px 0; }

.fun-fact {
  background: rgba(90, 103, 242, 0.1);
  border-radius: 10px;
  padding: 10px;
  font-weight: 600;
}

.stats-row {
  display: flex;
  justify-content: space-around;
  text-align: center;
  margin: 16px 0;
}
.stat-item { display: flex; flex-direction: column; }
.stat-num { font-size: 1.8rem; font-weight: 800; font-family: var(--font-fun); }
.stat-label { font-size: 0.7rem; opacity: 0.75; max-width: 70px; }

.guess-dist { display: flex; flex-direction: column; gap: 6px; margin-top: 8px; }
.dist-row { display: flex; align-items: center; gap: 8px; font-size: 0.85rem; }
.dist-num { width: 14px; font-weight: 700; }
.dist-bar-wrap { flex: 1; background: var(--color-header-border); border-radius: 4px; overflow: hidden; }
.dist-bar {
  background: var(--color-absent);
  color: #fff;
  text-align: right;
  padding: 2px 6px;
  font-weight: 700;
  font-size: 0.8rem;
  min-width: 22px;
  border-radius: 4px;
}
.dist-bar.highlight { background: var(--color-correct); }

.modal-next-wrap { text-align: center; margin-top: 16px; }

.end-word {
  text-align: center;
  font-family: var(--font-fun);
  font-size: 1.4rem;
  font-weight: 800;
  letter-spacing: 2px;
  margin: 10px 0 16px;
  color: var(--color-accent);
}

#end-modal .pill-btn { display: block; margin: 0 auto; }

@media (max-width: 380px) {
  .title { font-size: 1.25rem; }
  .tile { font-size: 1.5rem; }
  .key { height: 42px; font-size: 0.75rem; }
}
