*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  /* Kill iOS double-tap zoom; pinch zoom still allowed where browser permits */
  touch-action: manipulation;
}

body {
  background: #202020;
  color: #f5f5f5;
  font-family: "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: center;
  height: 100dvh;
  max-height: 100dvh;
  padding: 0 8px env(safe-area-inset-bottom, 0);
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

button,
.key,
.tile,
.row,
a {
  touch-action: manipulation;
}

.top {
  flex-shrink: 0;
  width: 100%;
  max-width: 500px;
  text-align: center;
  padding: max(12px, env(safe-area-inset-top, 0px)) 0 8px;
  border-bottom: 1px solid #3a3a3c;
  background: #202020;
  z-index: 100;
}

.title-row {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
  width: 100%;
  min-height: 44px;
}

.title-row .hint-btn:not(.hint-btn--restart) {
  grid-column: 1;
  justify-self: start;
}

.title-row h1 {
  grid-column: 2;
  margin: 0;
  justify-self: center;
  font-size: 1.75rem;
  font-weight: 700;
  letter-spacing: 0.04em;
}

.title-row .hint-btn--restart {
  grid-column: 3;
  justify-self: end;
}

.player-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: center;
  gap: 8px 12px;
  margin-top: 4px;
  min-height: 1.25em;
}

.player-bar[hidden] {
  display: none;
}

.player-handle {
  margin: 0;
  font-size: 0.85rem;
  color: #b8b8b8;
  letter-spacing: 0.04em;
  text-decoration: none;
  cursor: default;
}

.player-handle--link {
  text-decoration: underline;
  text-underline-offset: 3px;
  cursor: pointer;
}

.player-handle--link:hover,
.player-handle--link:focus-visible {
  color: #f5f5f5;
}

.auth-link,
.save-progress {
  display: inline-block;
  font-size: 0.85rem;
  color: #b8b8b8;
  text-decoration: underline;
  text-underline-offset: 3px;
  background: none;
  border: none;
  padding: 4px 0;
  cursor: pointer;
  font-family: inherit;
  line-height: 1.2;
}

.auth-link--login {
  margin-top: 6px;
}

.auth-link[hidden],
.save-progress[hidden],
.auth-link--button[hidden] {
  display: none;
}

.auth-link:hover,
.auth-link:focus-visible,
.save-progress:hover,
.save-progress:focus-visible {
  color: #f5f5f5;
}

body.wriggly-ready .solve-carousel {
  visibility: visible;
}

body.wriggly-ready .solve-card .board,
body.wriggly-ready .solve-card__title {
  visibility: visible;
}

main {
  width: 100%;
  max-width: 500px;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  padding: 8px 0 4px;
  position: relative;
}

.solve-carousel {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: row;
  align-items: stretch;
  gap: 10px;
  overflow-x: auto;
  overflow-y: hidden;
  scroll-snap-type: x mandatory;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
  padding: 8px max(3%, 6px) 12px;
  visibility: hidden;
  scrollbar-width: none;
}

.solve-carousel::-webkit-scrollbar {
  display: none;
}

.solve-carousel--solo {
  padding-left: 6px;
  padding-right: 6px;
  justify-content: center;
}

.solve-carousel--solo .solve-card {
  flex-basis: min(100%, 480px);
  max-width: 480px;
}

.solve-card {
  flex: 0 0 94%;
  max-width: 480px;
  scroll-snap-align: center;
  scroll-snap-stop: always;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
  max-height: 100%;
  border: 1px solid #565758;
  border-radius: 10px;
  background: #2a2a2c;
  padding: 10px 10px 8px;
  box-sizing: border-box;
  transition: border-color 0.2s ease, opacity 0.2s ease;
  opacity: 0.72;
}

.solve-card--active {
  border-color: #818384;
  opacity: 1;
}

.solve-card--scored {
  border-color: var(--score-border);
}

.solve-card--scored.solve-card--active {
  border-color: var(--score-border);
}

.solve-card__head {
  flex-shrink: 0;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 6px;
  margin: 0 0 8px;
  min-height: 1.25em;
}

.solve-card__title {
  grid-column: 2;
  margin: 0;
  text-align: center;
  font-size: 0.95rem;
  letter-spacing: 0.08em;
  color: #b8b8b8;
  white-space: nowrap;
  visibility: hidden;
}

.solve-card__guesses {
  grid-column: 3;
  justify-self: end;
  margin: 0;
  font-size: 0.8rem;
  letter-spacing: 0.04em;
  color: #b8b8b8;
  font-variant-numeric: tabular-nums;
}

.solve-card__guesses[hidden] {
  display: none;
}

.solve-card__body {
  flex: 1;
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.solve-card__body--dim .board {
  filter: grayscale(0.55) brightness(0.72);
}

.solve-card .board {
  width: 100%;
  max-width: 280px;
}

.solve-card .keyboard--card {
  flex-shrink: 0;
  width: 100%;
  max-width: 100%;
  margin-top: 8px;
  padding: 6px 0 2px;
  background: #2a2a2c;
  visibility: visible;
  border-top: 1px solid #3a3a3c;
}

.solve-card .path-missing {
  inset: 4px;
}

@media (prefers-reduced-motion: reduce) {
  .solve-carousel {
    scroll-behavior: auto;
  }
}

.board-scroll {
  display: none;
}

.path-missing {
  position: absolute;
  inset: 8px 12px 12px;
  z-index: 80;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 20px 16px;
  border-radius: 8px;
  background: rgba(20, 20, 20, 0.88);
  border: 1px solid #6eb5b5;
  text-align: center;
  pointer-events: none;
}

.path-missing[hidden] {
  display: none;
}

.path-missing__title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 700;
  letter-spacing: 0.04em;
  color: #f5f5f5;
}

.path-missing__body {
  margin: 0;
  max-width: 280px;
  font-size: 0.9rem;
  line-height: 1.4;
  color: #b8b8b8;
}

.board {
  width: 100%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.row--current {
  scroll-margin-top: 8px;
  scroll-margin-bottom: 12px;
}

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

.tile {
  aspect-ratio: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 2px solid #3a3a3c;
  border-radius: 4px;
  font-size: clamp(1.4rem, 6vw, 2rem);
  font-weight: 700;
  text-transform: uppercase;
  color: #f5f5f5;
  background: transparent;
}

.tile--filled {
  border-color: #565758;
  background: #3a3a3c;
}

.row--locked .tile {
  border-color: #565758;
  background: #2a2a2c;
  color: #d7dadc;
}

.row--current .tile {
  border-color: #818384;
  animation: pulse-border 1.2s ease-in-out infinite;
}

.tile--hint {
  border-color: #6eb5b5;
  background: #2a4a4a;
  box-shadow: inset 0 0 0 1px rgba(110, 181, 181, 0.35);
  animation: none;
}

.tile--ghost {
  /* Same empty-tile chrome; only letter is faint */
  color: rgba(215, 218, 220, 0.35);
}

.hint-btn {
  flex: 0 0 auto;
  min-height: 36px;
  min-width: 0;
  padding: 6px 10px;
  border: 1px solid #565758;
  border-radius: 6px;
  background: #2a2a2c;
  color: #d7dadc;
  font-family: inherit;
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.04em;
  cursor: pointer;
  touch-action: manipulation;
  white-space: nowrap;
}

.hint-btn[hidden] {
  display: none;
}

.hint-btn:hover,
.hint-btn:focus-visible {
  color: #f5f5f5;
  border-color: #818384;
}

.hint-btn:disabled {
  opacity: 0.35;
  cursor: default;
}

.hint-btn[hidden] {
  display: none;
}

.ad-stub-note {
  color: #818384;
  font-size: 0.85rem;
}

.ad-countdown {
  margin: 16px 0 0;
  text-align: center;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #6eb5b5;
}

.modal-action:disabled {
  opacity: 0.4;
  cursor: default;
}

.modal-action--secondary {
  margin-top: 10px;
  background: #3a3a3c;
}

.modal-action--secondary:hover,
.modal-action--secondary:focus-visible {
  background: #4a4a4c;
}

@keyframes pulse-border {
  0%, 100% { border-color: #818384; }
  50% { border-color: #b8b8b8; }
}

.row--shake {
  animation: shake 0.4s ease;
}

@keyframes shake {
  0%, 100% { transform: translateX(0); }
  20% { transform: translateX(-4px); }
  40% { transform: translateX(4px); }
  60% { transform: translateX(-4px); }
  80% { transform: translateX(4px); }
}

.toast {
  position: fixed;
  top: 12%;
  left: 50%;
  transform: translateX(-50%);
  z-index: 2000;
  padding: 12px 18px;
  border-radius: 6px;
  background: #f5f5f5;
  color: #202020;
  font-size: 0.9rem;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none;
}

.toast[hidden] {
  display: none;
}

.keyboard {
  display: flex;
  flex-direction: column;
  gap: 6px;
  width: 100%;
}

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

.key {
  flex: 1 1 0;
  min-width: 0;
  max-width: 34px;
  height: 44px;
  border: none;
  border-radius: 4px;
  background: #818384;
  color: #f5f5f5;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  cursor: pointer;
  padding: 0 2px;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.key:active {
  background: #6a6b6c;
}

.keyboard--disabled {
  opacity: 0.4;
  pointer-events: none;
}

.key--wide {
  flex: 1.6 1 0;
  max-width: 52px;
  font-size: 0.65rem;
}

.key--wide[data-key="Backspace"] {
  font-size: 1rem;
}

#wriggly-menu-btn,
.wriggly-menu-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  margin: 0;
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
  color: inherit;
  cursor: pointer;
}

#wriggly-menu-btn:hover,
#wriggly-menu-btn:focus-visible,
.wriggly-menu-btn:hover,
.wriggly-menu-btn:focus-visible {
  background: #3a3a3c;
}

#wriggly-menu {
  position: fixed;
  top: calc(env(safe-area-inset-top, 0px) + 48px);
  right: 8px;
  z-index: 1100;
  box-sizing: border-box;
  width: 280px;
  max-height: min(70vh, 480px);
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  padding: 8px 0;
  background: #202020;
  color: #f5f5f5;
  border: 1px solid #3a3a3c;
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.45);
  font-family: inherit;
}

#wriggly-menu[hidden] {
  display: none;
}

#wriggly-menu button {
  display: flex;
  align-items: center;
  min-height: 44px;
  width: 100%;
  padding: 0 14px;
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  box-sizing: border-box;
}

#wriggly-menu button:hover,
#wriggly-menu button:focus-visible {
  background: #3a3a3c;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal[hidden] {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
}

.modal-panel {
  position: relative;
  z-index: 1;
  width: 100%;
  max-width: 420px;
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  background: #2a2a2c;
  border: 1px solid #3a3a3c;
  border-radius: 8px;
  padding: 24px 20px 20px;
  color: #f5f5f5;
}

.modal-panel h2 {
  margin: 0 0 16px;
  font-size: 1.25rem;
  text-align: center;
}

.modal-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 4px;
  background: transparent;
  color: #b8b8b8;
  font-size: 1.25rem;
  cursor: pointer;
  line-height: 1;
}

.modal-close:hover,
.modal-close:focus-visible {
  color: #f5f5f5;
}

.modal-body {
  font-size: 0.95rem;
  line-height: 1.5;
  color: #d7dadc;
}

.modal-body p {
  margin: 0 0 12px;
}

.modal-action {
  display: block;
  width: 100%;
  margin-top: 20px;
  padding: 12px;
  border: none;
  border-radius: 6px;
  background: #538d4e;
  color: #fff;
  font-size: 1rem;
  font-weight: 700;
  cursor: pointer;
}

.modal-action:hover,
.modal-action:focus-visible {
  background: #4a7d45;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-bottom: 20px;
}

.stat {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-value {
  font-size: 1.75rem;
  font-weight: 700;
}

.stat-label {
  font-size: 0.65rem;
  text-align: center;
  color: #b8b8b8;
  line-height: 1.2;
}

.distribution-title {
  margin: 0 0 8px;
  font-size: 0.85rem;
  font-weight: 600;
  color: #b8b8b8;
}

.dist-row {
  display: grid;
  grid-template-columns: 28px 1fr 24px;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 0.8rem;
}

.dist-label,
.dist-count {
  color: #b8b8b8;
  text-align: center;
}

.dist-track {
  height: 18px;
  background: #3a3a3c;
  border-radius: 2px;
  overflow: hidden;
}

.dist-bar {
  height: 100%;
  width: 0;
  min-width: 0;
  background: #538d4e;
  border-radius: 2px;
  transition: width 0.3s ease;
}

@media (max-width: 380px) {
  .key {
    height: 52px;
    font-size: 0.8rem;
  }

  .key--wide {
    max-width: 56px;
    font-size: 0.7rem;
  }

  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 12px;
  }
}
