*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}
html,
body {
  height: 100%;
  overflow: hidden;
}
body {
  height: 100%;
  display: flex;
  flex-direction: column;
  font-family:
    -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: #f0f8ff;
}

/* ── Top bar ── */
#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  background: #fff8dc;
  z-index: 10;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
  min-height: 44px;
}
#topbar-left {
  display: flex;
  gap: 3px;
  align-items: center;
}
#topbar-center {
  font-weight: 700;
  font-size: clamp(16px, 3vw, 24px);
  color: #333;
  display: flex;
  gap: 6px;
  align-items: center;
}

#topbar-right {
  display: flex;
  align-items: center;
  gap: 6px;
}

#topbar #gameBtn {
  font-size: clamp(13px, 1.8vw, 16px);
  padding: 4px 12px;
  border: none;
  border-radius: 6px;
  background: #ffb347;
  color: #fff;
  cursor: pointer;
  transition: background 0.2s;
}
#topbar #gameBtn:disabled {
  background: #ccc;
  cursor: not-allowed;
}
#topbar #gameBtn:not(:disabled):active {
  background: #ff8800;
}

/* ── Stats panel ── */
#stats-panel {
  background: #fffdf5;
  border-bottom: 1px solid #eee;
  padding: 8px 10px;
  z-index: 9;
}

#stats-letters {
  width: 100%;
}

#letter-grid {
  display: grid;
  grid-template-columns: repeat(26, 1fr);
  gap: 3px;
  width: 100%;
}
#letter-grid .letter-cell {
  position: relative;
  width: 100%;
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(10px, 1.3vw, 18px);
  font-weight: 700;
  border-radius: 4px;
  background: #eee;
  color: #bbb;
  cursor: pointer;
  transition: all 0.2s;
}
#letter-grid .letter-cell.popped {
  color: #fff;
}
.lc-letter {
  position: relative;
  z-index: 1;
}
.lc-count {
  position: absolute;
  top: -4px;
  right: -4px;
  font-size: clamp(7px, 1vw, 12px);
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  min-width: clamp(13px, 1.6vw, 18px);
  height: clamp(13px, 1.6vw, 18px);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 2px;
  line-height: 1;
  z-index: 2;
}
.lc-count:empty {
  display: none;
}

/* ── Canvas ── */
#canvas-container {
  flex: 1;
  min-height: 0;
  width: 100%;
  position: relative;
  background: #e0f7fa;
  overflow: hidden;
}

#balloonCanvas {
  display: block;
  width: 100%;
  height: 100%;
  background: transparent;
  position: absolute;
  top: 0;
  left: 0;
  z-index: 1;
  touch-action: none;
}

@media (max-width: 600px) {
  #topbar {
    padding: 4px 6px;
  }
  #topbar-left {
    gap: 4px;
  }
  #topbar #gameBtn {
    padding: 3px 8px;
  }
  #letter-grid {
    grid-template-columns: repeat(13, 1fr);
    gap: 2px;
  }
  #letter-grid .letter-cell {
    font-size: clamp(10px, 2.5vw, 16px);
  }
  .lc-count {
    font-size: clamp(7px, 1.8vw, 11px);
    min-width: clamp(12px, 3vw, 16px);
    height: clamp(12px, 3vw, 16px);
    top: -3px;
    right: -3px;
  }
}

/* ── Game button states ── */
#gameBtn.restart {
  background: #ff6b6b;
}
#gameBtn.restart:active {
  background: #e04040;
}
#continueBtn {
  font-size: clamp(13px, 1.8vw, 16px);
  padding: 4px 12px;
  border: none;
  border-radius: 6px;
  background: #69c97b;
  color: #fff;
  cursor: pointer;
  transition: background 0.2s;
}
#continueBtn:active {
  background: #4da85e;
}
#continueBtn.hidden {
  display: none;
}

/* ── Settings ── */
#settings-wrap {
  position: relative;
}
#settingsBtn {
  font-size: clamp(18px, 2.8vw, 24px);
  line-height: 1;
  padding: 4px 8px;
  background: none;
  border: 1px solid #ddd;
  border-radius: 6px;
  cursor: pointer;
  color: #888;
  transition:
    color 0.2s,
    border-color 0.2s;
}
#settingsBtn:hover,
#settingsBtn:active {
  color: #ffb347;
  border-color: #ffb347;
}
#settings-dropdown {
  position: absolute;
  top: calc(100% + 6px);
  right: 0;
  background: #fff;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 10px 14px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  z-index: 50;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-width: 170px;
  font-size: clamp(13px, 1.8vw, 15px);
}
#settings-dropdown.hidden {
  display: none;
}
#settings-dropdown label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  white-space: nowrap;
  color: #444;
}
#settings-dropdown select {
  margin-left: auto;
  padding: 2px 4px;
  border-radius: 4px;
  border: 1px solid #ccc;
  font-size: inherit;
}
#settings-dropdown input[type="range"] {
  margin-left: auto;
  width: 90px;
  cursor: pointer;
  accent-color: #ffb347;
}

/* ── Modal ── */
#modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
#modal-overlay.hidden {
  display: none;
}
#modal {
  background: #fff;
  border-radius: 14px;
  padding: 24px 28px;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.2);
  text-align: center;
  max-width: 320px;
  width: 90vw;
}
#modal-msg {
  font-size: clamp(15px, 2.5vw, 18px);
  color: #333;
  margin-bottom: 18px;
  line-height: 1.4;
}
#modal-actions {
  display: flex;
  gap: 10px;
  justify-content: center;
}
#modal-actions button {
  font-size: clamp(14px, 2vw, 16px);
  padding: 8px 20px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 600;
  transition: background 0.15s;
}
#modal-cancel {
  background: #eee;
  color: #555;
}
#modal-cancel:active {
  background: #ddd;
}
#modal-confirm {
  background: #ff6b6b;
  color: #fff;
}
#modal-confirm:active {
  background: #e04040;
}

/* ── Lang toggle switch ── */
.lang-switch-label {
  display: flex;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-weight: 600;
  font-size: clamp(13px, 1.8vw, 15px);
  color: #444;
  position: relative;
}
.lang-switch-label input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  width: 36px;
  height: 20px;
  background: #ccc;
  border-radius: 10px;
  position: relative;
  cursor: pointer;
  transition: background 0.2s;
  flex-shrink: 0;
}
.lang-switch-label input[type="checkbox"]::after {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 16px;
  height: 16px;
  background: #fff;
  border-radius: 50%;
  transition: transform 0.2s;
}
.lang-switch-label input[type="checkbox"]:checked {
  background: #ffb347;
}
.lang-switch-label input[type="checkbox"]:checked::after {
  transform: translateX(16px);
}

/* ── Math visual hint ── */
#topbar-center {
  position: relative;
}
#hint-badge {
  font-size: clamp(12px, 2vw, 16px);
  background: #fff3e0;
  border: 1.5px solid #ffb347;
  border-radius: 50%;
  width: clamp(22px, 3.5vw, 28px);
  height: clamp(22px, 3.5vw, 28px);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  line-height: 1;
  animation: pulse-badge 1.5s ease-in-out infinite;
  flex-shrink: 0;
}
#hint-badge.hidden {
  display: none;
}
@keyframes pulse-badge {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(255, 179, 71, 0.4);
  }
  50% {
    box-shadow: 0 0 0 5px rgba(255, 179, 71, 0);
  }
}
#math-hint {
  position: absolute;
  top: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: #fff;
  border: 1px solid #eee;
  border-radius: 10px;
  padding: 6px 10px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.1);
  z-index: 50;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  max-width: 360px;
  justify-content: center;
  line-height: 1;
}
#math-hint.hidden {
  display: none;
}
.hint-item {
  font-size: clamp(20px, 4vw, 30px);
  transition: opacity 0.2s;
}
.hint-plus {
  font-size: clamp(18px, 3.5vw, 26px);
  font-weight: 700;
  color: #ffb347;
  margin: 0 4px;
}
.hint-removed {
  opacity: 0.25;
  text-decoration: line-through;
  filter: grayscale(1);
}
.hint-unknown {
  background: #fff3e0;
  border: 2px dashed #ffb347;
  border-radius: 6px;
  padding: 0 4px;
  color: #ffb347;
  font-weight: 700;
}

/* ── Mode buttons ── */
.mode-btn {
  font-size: clamp(14px, 2.2vw, 20px);
  padding: 1px 3px;
  border: 2px solid transparent;
  border-radius: 4px;
  background: none;
  cursor: pointer;
  line-height: 1;
  transition: border-color 0.2s;
  margin: 0;
}
.mode-btn.mode-active {
  border-color: #ffb347;
  background: rgba(255, 179, 71, 0.15);
}
.mode-btn:active {
  transform: scale(0.95);
}

/* ── Challenge display ── */
#challenge-display {
  font-size: clamp(14px, 2.5vw, 22px);
  font-weight: 700;
  color: #555;
  letter-spacing: 2px;
}
#challenge-display.hidden {
  display: none;
}
.hidden {
  display: none;
}

/* ── Rewards display ── */
#rewards-display {
  font-size: clamp(12px, 2vw, 18px);
  line-height: 1;
  white-space: nowrap;
}

/* ── Grid challenge highlights ── */
#letter-grid .letter-cell.challenge-target {
  border: 2px solid #ffb347;
  background: #fff3e0;
  color: #333;
}
#letter-grid .letter-cell.challenge-next {
  border: 2px solid #ff6b6b;
  background: #fff0f0;
  color: #333;
  animation: pulse 0.8s ease-in-out infinite;
}
#letter-grid .letter-cell.challenge-done {
  background: #69c97b;
  color: #fff;
  border: 2px solid #69c97b;
}
#letter-grid .letter-cell.challenge-dimmed {
  opacity: 0.35;
}

/* ── Grid math layout ── */
#letter-grid.math-grid {
  grid-template-columns: repeat(11, 1fr);
}
#letter-grid.math-grid .letter-cell {
  font-size: clamp(24px, 5vw, 48px);
}

/* ── Animations ── */
@keyframes shake {
  0%,
  100% {
    transform: translateX(0);
  }
  20% {
    transform: translateX(-4px);
  }
  40% {
    transform: translateX(4px);
  }
  60% {
    transform: translateX(-3px);
  }
  80% {
    transform: translateX(3px);
  }
}
@keyframes pulse {
  0%,
  100% {
    box-shadow: 0 0 0 0 rgba(255, 107, 107, 0.4);
  }
  50% {
    box-shadow: 0 0 0 4px rgba(255, 107, 107, 0.2);
  }
}
#letter-grid .letter-cell.shake {
  animation: shake 0.4s ease;
}

@media (max-width: 600px) {
  #letter-grid.math-grid {
    grid-template-columns: repeat(6, 1fr);
  }
  #letter-grid.math-grid .letter-cell {
    font-size: clamp(20px, 8vw, 36px);
  }
}
