:root {
  --sand: #f4e1a4;
  --sand-dark: #e0c373;
  --sky-top: #87cefa;
  --sky-mid: #ffe2a8;
  --ocean: #4fa9d2;
  --ocean-dark: #2c7ba6;
  --coral: #ff7e6b;
  --shell: #ffd5d2;
  --ink: #2a2435;
  --ink-2: #5a516a;
  --paper: #fffdf7;
  --shadow: 0 10px 30px rgba(40, 30, 60, 0.18);
  --font-display: "Cormorant Garamond", "Playfair Display", Georgia, serif;
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Twemoji injects these for emoji glyphs that wouldn't otherwise render on
   iOS WebView. Size them to match the surrounding text. */
img.emoji {
  height: 1em;
  width: 1em;
  margin: 0 0.05em 0 0.1em;
  vertical-align: -0.15em;
  display: inline-block;
}
.round-btn img.emoji,
.touch-btn img.emoji {
  /* Inside icon buttons, the emoji is the only content — let it fill more. */
  height: 1.1em;
  width: 1.1em;
  margin: 0;
  vertical-align: middle;
}

html, body {
  margin: 0; padding: 0;
  font-family: var(--font-ui);
  color: var(--ink);
  background:
    radial-gradient(800px 400px at 50% -10%, #ffe2a8, transparent 60%),
    linear-gradient(180deg, #e6f3fb 0%, #fff7e0 100%);
  min-height: 100vh;
  overflow: hidden;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.app {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  min-height: 100vh;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: min(1000px, 100%);
  margin-bottom: 0.75rem;
}

.title {
  margin: 0;
  font-family: var(--font-display);
  font-size: clamp(1.6rem, 4vw, 2.4rem);
  letter-spacing: 0.01em;
  color: var(--ink);
}

.scores {
  display: flex;
  gap: 0.5rem;
}

.score-item {
  background: rgba(255,255,255,0.85);
  border: 1px solid rgba(0,0,0,0.08);
  border-radius: 999px;
  padding: 0.35rem 0.85rem;
  font-size: 0.85rem;
  color: var(--ink-2);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-weight: 500;
}
.score-item strong {
  color: var(--ink);
  margin-left: 0.4rem;
  font-size: 1rem;
  font-family: var(--font-display);
}

.stage {
  position: relative;
  width: min(1000px, 100%);
  aspect-ratio: 1000 / 500;
  border-radius: 16px;
  overflow: hidden;
  box-shadow: var(--shadow);
  background: #87cefa;
}

.game-container {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}
.game-container canvas {
  display: block;
  width: 100% !important;
  height: 100% !important;
}

.overlay {
  position: absolute;
  inset: 0;
  background: linear-gradient(180deg, rgba(255,226,168,0.55), rgba(79,169,210,0.55));
  display: grid;
  place-items: center;
  padding: 1rem;
  backdrop-filter: blur(2px);
  -webkit-backdrop-filter: blur(2px);
  z-index: 10;
}

.overlay-card {
  background: var(--paper);
  border-radius: 16px;
  padding: 1.5rem 1.75rem;
  text-align: center;
  max-width: 440px;
  width: 100%;
  box-shadow: 0 20px 50px rgba(40, 30, 60, 0.25);
  border: 1px solid rgba(0,0,0,0.05);
}

.overlay-card h2 {
  margin: 0 0 0.5rem;
  font-family: var(--font-display);
  font-size: 2rem;
  color: var(--ink);
}

.overlay-card p {
  margin: 0 0 1rem;
  color: var(--ink-2);
  line-height: 1.5;
}

/* Side-by-side preview + character thumbnail grid */
#overlay-start .overlay-card {
  max-width: 780px;
  max-height: 90vh;
  overflow-y: auto;
  padding: 0.9rem 1.1rem;
}
#overlay-start .overlay-card h2 { margin: 0 0 0.2rem; font-size: 1.6rem; }
#welcome-username { color: var(--coral); }
#overlay-start .overlay-card p  { margin: 0 0 0.45rem; font-size: 0.92rem; }
.customize-grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 0.85rem;
  align-items: center;
  justify-items: start;
  text-align: left;
  margin: 0.35rem 0 0.5rem;
  position: relative;
  /* While the thumbnails are being captured, hide preview + grid and show
     the seashell loader centered within the entire card width. */
  min-height: 270px;
}
.customize-grid.loading > * { visibility: hidden; }
.customize-grid.loading::before,
.customize-grid.loading::after {
  position: absolute;
  left: 50%;
  pointer-events: none;
  line-height: 1;
}
.customize-grid.loading::before {
  content: "🐚";
  top: calc(50% - 1.6rem);
  width: 4rem;
  height: 4rem;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 3.2rem;
  transform: translate(-50%, -50%) rotate(-18deg);
  animation: shellRock 1.4s ease-in-out infinite;
}
.customize-grid.loading::after {
  content: "Loading…";
  top: calc(50% + 2rem);
  transform: translate(-50%, -50%);
  font-family: var(--font-display);
  font-style: italic;
  color: var(--ink-2);
  font-size: 1.05rem;
}
@keyframes shellRock {
  0%, 100% { transform: translate(-50%, -50%) rotate(-18deg); }
  50%      { transform: translate(-50%, -50%) rotate(18deg); }
}
.char-grid {
  display: grid;
  grid-template-columns: repeat(7, minmax(0, 72px));
  grid-template-rows: repeat(3, auto);
  justify-content: center;
  align-content: center;
  gap: 0.25rem;
  min-width: 0;
  width: 100%;
}
.char-thumb {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-end;
  background: rgba(255, 255, 255, 0.55);
  border: 2px solid transparent;
  border-radius: 6px;
  padding: 0.1rem 0.08rem 0.12rem;
  cursor: pointer;
  font-family: inherit;
  transition: transform 0.05s ease, box-shadow 0.1s ease, border-color 0.1s ease;
}
.char-thumb:hover {
  background: #fff;
  transform: translateY(-1px);
}
.char-thumb.selected {
  border-color: var(--coral);
  box-shadow: 0 2px 6px rgba(255, 126, 107, 0.32);
  background: #fff;
}
.char-thumb-img {
  display: block;
  width: 100%;
  aspect-ratio: 1 / 1;
  background: linear-gradient(180deg, #d8efff 0%, #fff5d8 100%);
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  border-radius: 50%;
  margin-bottom: 0.05rem;
}
.char-thumb-name {
  font-size: 0.55rem;
  color: var(--ink-2);
  font-family: var(--font-display);
  text-align: center;
  line-height: 1;
}
.char-thumb.selected .char-thumb-name { color: var(--ink); }
@media (max-width: 600px) {
  .customize-grid {
    grid-template-columns: 1fr;
    justify-items: center;
  }
  .char-grid { grid-template-columns: repeat(4, 1fr); }
}

/* Live 3D preview of the character */
.preview-canvas {
  display: block;
  width: 200px;
  height: 270px;
  background:
    radial-gradient(ellipse 70% 30% at 50% 95%, rgba(0,0,0,0.20), transparent 70%),
    linear-gradient(180deg, #d8efff 0%, #fff5d8 80%, #ffe0c0 100%);
  border-radius: 14px;
  box-shadow: inset 0 0 0 3px var(--coral),
              0 5px 14px rgba(40,30,60,0.16);
  margin: 0 auto;
  /* Hidden until renderPreview() has actually drawn a character frame.
     Until then the user sees through the canvas to the overlay-card's
     paper background — anything's better than a flash of the raw
     black GL backbuffer. The JS adds .ready on the first real render. */
  opacity: 0;
  transition: opacity 0.35s ease-out;
}
.preview-canvas.ready { opacity: 1; }

/* Outfit swatches — recolor specific palette cells used by the active character */
.outfit-row { align-items: flex-start; }
.outfit-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem 0.55rem;
  flex: 1;
  align-items: flex-start;
  min-height: 50px;
}
.outfit-hint {
  font-size: 0.78rem;
  color: var(--ink-2);
  font-style: italic;
  margin-left: 0.2rem;
}
.outfit-swatch-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.18rem;
}
.outfit-swatch {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.95);
  box-shadow: 0 2px 6px rgba(0,0,0,0.22), 0 0 0 1px rgba(0,0,0,0.18);
  cursor: pointer;
  padding: 0;
  transition: transform 0.05s ease, box-shadow 0.1s ease;
}
.outfit-swatch:hover {
  transform: translateY(-2px) scale(1.08);
  box-shadow: 0 4px 10px rgba(0,0,0,0.28), 0 0 0 1px rgba(0,0,0,0.22);
}
.outfit-swatch:active { transform: translateY(0) scale(1); }
.outfit-swatch-label {
  font-size: 0.7rem;
  color: var(--ink-2);
  font-family: var(--font-display);
  text-align: center;
  max-width: 50px;
  line-height: 1.1;
}

.overlay-card .hint {
  margin: 0.75rem 0 0;
  font-size: 0.85rem;
  font-style: italic;
  color: var(--ink-2);
}

.char-preview-canvas {
  display: block;
  margin: 0.5rem auto 0.85rem;
  background: linear-gradient(180deg, #d8efff 0%, #fff5d8 100%);
  border-radius: 50%;
  box-shadow: inset 0 0 0 3px var(--coral), 0 4px 12px rgba(0, 0, 0, 0.08);
  width: 140px;
  height: 140px;
}

.customize {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.45rem 0.75rem;
  margin: 0.5rem 0 1.1rem;
}

.custom-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.4rem;
  font-size: 0.85rem;
}

.custom-row .label {
  color: var(--ink-2);
  font-weight: 500;
}

.cycler {
  display: inline-flex;
  align-items: center;
  background: var(--paper);
  border: 1px solid rgba(0,0,0,0.1);
  border-radius: 999px;
  overflow: hidden;
  box-shadow: 0 1px 0 rgba(255,255,255,0.6) inset;
}

.cyc-btn {
  background: transparent;
  border: none;
  padding: 0.25rem 0.55rem;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 1rem;
  line-height: 1;
  font-family: inherit;
}
.cyc-btn:hover { background: rgba(0, 0, 0, 0.05); color: var(--ink); }

.cycler .value {
  padding: 0.25rem 0.4rem;
  font-size: 0.82rem;
  min-width: 64px;
  text-align: center;
  color: var(--ink);
}

.btn-primary {
  background: var(--coral);
  color: #fff;
  border: none;
  border-radius: 999px;
  padding: 0.75rem 1.6rem;
  font-size: 1.05rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
  box-shadow: 0 6px 16px rgba(255, 126, 107, 0.4);
}

.btn-ghost {
  background: var(--paper);
  color: var(--ink);
  border: 1px solid rgba(0,0,0,0.12);
  border-radius: 999px;
  padding: 0.45rem 1rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s ease, transform 0.05s ease;
}
.btn-ghost:hover { background: #fff; }
.btn-ghost:active { transform: translateY(1px); }
.btn-ghost:disabled { opacity: 0.5; cursor: default; }

/* Game-over score form + leaderboard */
.score-form { margin: 0.4rem 0 0.85rem; }
.score-label {
  display: block;
  font-size: 0.85rem;
  color: var(--ink-2);
  margin-bottom: 0.3rem;
}
.score-row { display: flex; gap: 0.4rem; justify-content: center; }
.score-input {
  flex: 0 1 180px;
  padding: 0.45rem 0.75rem;
  border-radius: 999px;
  border: 1px solid rgba(0,0,0,0.15);
  background: var(--paper);
  font-family: inherit;
  font-size: 0.9rem;
  color: var(--ink);
}
.score-status {
  margin: 0.4rem 0 0;
  min-height: 1em;
  font-size: 0.78rem;
  color: var(--ink-2);
  font-style: italic;
}
.leaderboard-title {
  margin: 0.5rem 0 0.3rem;
  font-family: var(--font-display);
  font-size: 1.15rem;
  color: var(--ink);
}
.leaderboard {
  list-style: none;
  margin: 0 0 0.85rem;
  padding: 0;
  text-align: left;
  max-width: 320px;
  margin-left: auto;
  margin-right: auto;
}
.leaderboard li {
  display: grid;
  grid-template-columns: 1.2rem 1fr auto;
  gap: 0.5rem;
  padding: 0.25rem 0.5rem;
  font-size: 0.88rem;
  color: var(--ink);
  border-bottom: 1px dashed rgba(0,0,0,0.08);
}
.leaderboard li:last-child { border-bottom: none; }
.leaderboard .lb-rank { color: var(--ink-2); text-align: right; }
.leaderboard .lb-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.leaderboard .lb-char {
  color: var(--ink-2);
  font-size: 0.78rem;
}
.leaderboard .lb-score {
  font-family: var(--font-display);
  font-weight: 600;
}
.leaderboard li.lb-mine {
  background: rgba(255, 126, 107, 0.12);
  border-radius: 6px;
}
.leaderboard li.lb-loading {
  display: block;
  text-align: center;
  font-style: italic;
  color: var(--ink-2);
}

.overlay-card-wide { max-width: 560px; }
.leaderboards {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
  margin: 0.5rem 0 1rem;
  text-align: left;
}
.leaderboard-col { min-width: 0; }
.leaderboards .leaderboard {
  max-width: 100%;
  margin: 0;
}
.leaderboards .leaderboard-title {
  text-align: center;
  margin: 0 0 0.3rem;
  font-size: 1rem;
}
@media (max-width: 540px) {
  .leaderboards { grid-template-columns: 1fr; }
}

.settings-h {
  margin: 1rem 0 0.3rem;
  font-family: var(--font-display);
  font-size: 1rem;
  color: var(--ink);
}
#settings-username-current {
  font-style: normal;
  font-size: 0.85rem;
  color: var(--ink);
  margin: 0 0 0.4rem;
}
.btn-primary:hover { background: #ff6650; }
.btn-primary:active { transform: translateY(1px); }

kbd {
  display: inline-block;
  background: var(--paper);
  border: 1px solid rgba(0,0,0,0.18);
  border-radius: 6px;
  padding: 1px 6px;
  font-family: ui-monospace, "SF Mono", Menlo, monospace;
  font-size: 0.85em;
  box-shadow: 0 1px 0 rgba(0,0,0,0.08);
  color: var(--ink);
}

.bottombar {
  margin-top: 0.6rem;
  font-size: 0.85rem;
  color: var(--ink-2);
}

.game-controls {
  position: absolute;
  top: 0.6rem; right: 0.6rem;
  display: flex;
  gap: 0.4rem;
  z-index: 5;
}

/* On-screen touch controls — only show on touch-primary devices. */
.touch-controls {
  display: none;
  position: absolute;
  left: 0; right: 0; bottom: 0;
  padding: 0.5rem 0.7rem 0.7rem;
  z-index: 6;
  pointer-events: none;
  justify-content: space-between;
  align-items: end;
  gap: 0.6rem;
}
.touch-btn {
  pointer-events: auto;
  width: 64px; height: 64px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.78);
  color: var(--ink);
  font-size: 1.7rem;
  line-height: 1;
  font-family: inherit;
  font-weight: 700;
  display: grid;
  place-items: center;
  box-shadow: 0 3px 10px rgba(0,0,0,0.18);
  cursor: pointer;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.touch-btn:active {
  transform: translateY(2px);
  background: rgba(255, 255, 255, 0.95);
}
.touch-group {
  display: flex;
  gap: 0.5rem;
  pointer-events: auto;
}
.touch-group-stack { flex-direction: column-reverse; }
@media (hover: none) and (pointer: coarse) {
  .touch-controls { display: flex; }
  /* Make in-game round buttons bigger so they're tappable. */
  .round-btn { width: 44px; height: 44px; font-size: 1.2rem; }
  /* Hide keyboard-only footer hint. */
  .bottombar { display: none; }
}
.round-btn {
  width: 38px; height: 38px;
  border-radius: 50%;
  border: none;
  background: rgba(255, 255, 255, 0.85);
  color: var(--ink);
  font-size: 1.1rem;
  cursor: pointer;
  display: grid;
  place-items: center;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  transition: background 0.15s ease, transform 0.05s ease;
  font-family: inherit;
}
.round-btn:hover { background: #fff; }
.round-btn:active { transform: translateY(1px); }

.best-msg {
  color: var(--coral);
  font-weight: 600;
}

@media (max-width: 600px) {
  .topbar { flex-direction: column; gap: 0.4rem; }
  .overlay-card { padding: 1.25rem 1.25rem; }
  .overlay-card h2 { font-size: 1.6rem; }
}

/* ---------- Phone layout (portrait + landscape) ---------- */
/* Goal: fill the screen with the game canvas, make the topbar compact,
   respect the notch/home-indicator safe areas, and don't let touch
   controls or the start overlay overflow on small screens. */
@media (max-width: 768px) {
  html, body { height: 100%; }
  .app {
    height: 100dvh;
    min-height: 100dvh;
    padding:
      max(0.4rem, env(safe-area-inset-top))
      max(0.4rem, env(safe-area-inset-right))
      max(0.4rem, env(safe-area-inset-bottom))
      max(0.4rem, env(safe-area-inset-left));
  }
  .topbar {
    flex-direction: row;
    gap: 0.5rem;
    margin-bottom: 0.4rem;
    padding: 0 0.2rem;
  }
  .title { font-size: 1.25rem; }
  .scores { gap: 0.3rem; }
  .score-item { padding: 0.2rem 0.55rem; font-size: 0.65rem; }
  .score-item strong { font-size: 0.85rem; margin-left: 0.25rem; }

  /* Stage fills remaining vertical space instead of the desktop 2:1 ratio. */
  .stage {
    aspect-ratio: auto;
    flex: 1 1 auto;
    width: 100%;
    min-height: 0;
    border-radius: 12px;
  }

  /* Tighter overlays so the card fits on a phone screen. */
  .overlay { padding: 0.6rem; }
  .overlay-card {
    padding: 1rem 1.1rem;
    max-height: 100%;
    overflow-y: auto;
  }
  .overlay-card h2 { font-size: 1.35rem; }
  .overlay-card p  { font-size: 0.9rem; margin-bottom: 0.7rem; }

  /* Start overlay: stack preview above the character grid instead of side-by-side. */
  #overlay-start .overlay-card { padding: 0.7rem 0.85rem; }
  #overlay-start .overlay-card h2 { font-size: 1.25rem; }
  .customize-grid {
    grid-template-columns: 1fr;
    justify-items: center;
    text-align: center;
    min-height: 0;
  }
  .preview-canvas { width: 140px; height: 180px; }

  /* Slightly smaller touch buttons so they don't hog the screen. */
  .touch-btn { width: 54px; height: 54px; font-size: 1.45rem; }
  .touch-controls { padding: 0.4rem 0.5rem 0.6rem; }

  /* Pause/leaderboard/settings round buttons in the corner */
  .game-controls { gap: 0.3rem; top: 0.4rem; right: 0.4rem; }
}
