:root {
  color-scheme: light dark;
  --bg: #0e1420;
  --panel-bg: rgba(24, 20, 16, 0.72);
  --panel-border: rgba(255, 214, 150, 0.25);
  --text: #f3ecdf;
  --a-color: #4fa3ff;
  --b-color: #ff6b5e;
  --accent: #e8b86d;
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}

#scene-container {
  position: fixed;
  inset: 0;
}

#scene-container canvas {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
}

.hud {
  position: fixed;
  inset: 0;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  padding: 1.25rem;
}

.hud-top {
  pointer-events: auto;
  text-align: center;
  text-shadow: 0 2px 12px rgba(0, 0, 0, 0.6);
}

.hud-top h1 {
  margin: 0 0 0.15rem;
  font-size: 1.5rem;
  letter-spacing: 0.02em;
}

.turn-indicator {
  margin: 0;
  font-weight: 600;
  font-size: 1.05rem;
  color: var(--accent);
}

.side-panel {
  pointer-events: auto;
  position: absolute;
  top: 5rem;
  right: 1.25rem;
  width: 300px;
  max-width: calc(100vw - 2.5rem);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  padding: 0.9rem 1rem;
  backdrop-filter: blur(6px);
}

.message {
  min-height: 1.4rem;
  font-weight: 600;
  color: #ff9d8a;
  margin-bottom: 0.4rem;
}

.game-mode { margin: 0 0 0.6rem; color: var(--accent); font-size: 0.82rem; font-weight: 700; }

.hint {
  font-size: 0.78rem;
  opacity: 0.75;
  margin: 0.5rem 0;
}

#restart,
#rename,
#sound-toggle {
  width: 100%;
  padding: 0.5rem 1rem;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  cursor: pointer;
  font-weight: 600;
  touch-action: manipulation;
}

#restart:hover,
#rename:hover,
#sound-toggle:hover {
  background: rgba(255, 255, 255, 0.16);
}

#rename,
#sound-toggle {
  margin-top: 0.5rem;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(6, 8, 12, 0.72);
  backdrop-filter: blur(4px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.modal-backdrop.hidden {
  display: none;
}

.modal-card {
  background: #1c1712;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  padding: 1.75rem 2rem;
  width: 320px;
  max-width: calc(100vw - 2.5rem);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.5);
}

.modal-card h2 {
  margin: 0 0 0.75rem;
  text-align: center;
  color: var(--accent);
}

.modal-card label {
  font-size: 0.8rem;
  opacity: 0.8;
  margin-top: 0.4rem;
}

.modal-card input,
.modal-card select {
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--panel-border);
  background: rgba(255, 255, 255, 0.06);
  color: var(--text);
  font-size: 1rem;
}

.player-fields { display: flex; flex-direction: column; gap: 0.4rem; }
.player-fields.hidden { display: none; }

.modal-card input:focus {
  outline: 2px solid var(--a-color);
}

.modal-card button[type='submit'] {
  margin-top: 1.1rem;
  padding: 0.6rem 1rem;
  border-radius: 8px;
  border: none;
  background: var(--accent);
  color: #241a0c;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
}

.modal-card button[type='submit']:hover {
  filter: brightness(1.08);
}

.result-backdrop {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  overflow: hidden;
  padding: 1.25rem;
  background: radial-gradient(circle at center, rgba(75, 42, 5, 0.35), rgba(3, 6, 12, 0.9));
  backdrop-filter: blur(8px);
}

.result-backdrop.hidden { display: none; }

.result-card {
  position: relative;
  z-index: 2;
  width: min(440px, 100%);
  padding: 2rem;
  border: 2px solid #ffd166;
  border-radius: 26px;
  text-align: center;
  background: linear-gradient(145deg, #251708, #111827 70%);
  box-shadow: 0 0 45px rgba(255, 193, 69, 0.3), 0 25px 80px #000;
  animation: result-pop 0.55s cubic-bezier(.2, 1.4, .4, 1);
}

.result-card[data-outcome='lose'] { border-color: #ff6b6b; box-shadow: 0 0 45px rgba(255, 70, 70, 0.3), 0 25px 80px #000; }
.result-card[data-outcome='draw'] { border-color: #61dafb; box-shadow: 0 0 45px rgba(80, 200, 255, 0.3), 0 25px 80px #000; }
.result-icon { font-size: 5rem; line-height: 1; animation: result-bounce 1s ease-in-out infinite alternate; }
.result-kicker { margin: 1rem 0 0.25rem; color: #ffd166; font-weight: 900; letter-spacing: 0.16em; }
.result-card[data-outcome='lose'] .result-kicker { color: #ff8c8c; }
.result-card[data-outcome='draw'] .result-kicker { color: #61dafb; }
.result-card h2 { margin: 0; font-size: clamp(2rem, 9vw, 3.35rem); line-height: 1; text-shadow: 0 3px 20px rgba(255,255,255,.2); }
.result-score { display: inline-block; margin: 1.25rem 0 0.5rem; padding: 0.55rem 1rem; border-radius: 999px; background: rgba(255,255,255,.1); font-size: 1.2rem; font-weight: 900; }
.result-message { margin: 0.6rem auto 1.4rem; max-width: 34ch; opacity: 0.86; }
.play-again, .result-settings { width: 100%; border: 0; border-radius: 12px; padding: 0.85rem; cursor: pointer; font-size: 1rem; font-weight: 900; }
.play-again { color: #251708; background: linear-gradient(90deg, #ffd166, #ff9f1c); box-shadow: 0 8px 24px rgba(255,159,28,.3); }
.play-again:hover { transform: translateY(-2px) scale(1.01); filter: brightness(1.08); }
.result-settings { margin-top: 0.65rem; color: var(--text); background: rgba(255,255,255,.09); }

.celebration { position: absolute; inset: 0; pointer-events: none; }
.celebration i { position: absolute; top: -10%; left: var(--x); width: 11px; height: 22px; border-radius: 3px; background: hsl(var(--hue), 90%, 60%); animation: confetti-fall 2.8s var(--delay) linear infinite; }
.celebration i:nth-child(3n) { width: 16px; height: 16px; border-radius: 50%; }
.celebration i:nth-child(4n) { background: #ffd166; }

@keyframes result-pop { from { opacity: 0; transform: scale(.55) rotate(-5deg); } to { opacity: 1; transform: scale(1); } }
@keyframes result-bounce { to { transform: translateY(-8px) rotate(5deg) scale(1.08); } }
@keyframes confetti-fall { to { transform: translateY(120vh) rotate(var(--spin)); } }

@media (prefers-reduced-motion: reduce) {
  .result-card, .result-icon, .celebration i { animation: none; }
}

.seed-label {
  position: absolute;
  transform: translate(-50%, -50%);
  pointer-events: none;
  opacity: 0.65;
  font-weight: 700;
  font-size: 0.95rem;
  color: #fff8ec;
  text-shadow: 0 1px 3px rgba(0, 0, 0, 0.9), 0 0 8px rgba(0, 0, 0, 0.6);
}

.seed-label.owner-a {
  color: #cfe6ff;
}

.seed-label.owner-b {
  color: #ffd7d0;
}

.seed-label.store-label-3d {
  font-size: 1.15rem;
  color: var(--accent);
}

.loading {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  opacity: 0.8;
  transition: opacity 0.3s ease;
  pointer-events: none;
}

.loading.hidden {
  opacity: 0;
}

@media (max-width: 700px), (max-height: 520px) {
  .hud {
    padding: max(0.65rem, env(safe-area-inset-top)) max(0.65rem, env(safe-area-inset-right))
      max(0.65rem, env(safe-area-inset-bottom)) max(0.65rem, env(safe-area-inset-left));
  }

  .hud-top {
    pointer-events: none;
  }

  .hud-top h1 {
    font-size: 1.15rem;
  }

  .turn-indicator {
    font-size: 0.9rem;
  }

  .side-panel {
    top: auto;
    right: max(0.65rem, env(safe-area-inset-right));
    bottom: max(0.65rem, env(safe-area-inset-bottom));
    left: max(0.65rem, env(safe-area-inset-left));
    width: auto;
    max-width: none;
    padding: 0.55rem;
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 0.45rem;
  }

  .message,
  .game-mode,
  .hint {
    grid-column: 1 / -1;
    margin: 0;
    text-align: center;
  }

  .message {
    min-height: 0;
    font-size: 0.8rem;
  }

  .game-mode {
    font-size: 0.72rem;
  }

  .hint {
    font-size: 0.7rem;
  }

  #restart,
  #rename,
  #sound-toggle {
    min-height: 44px;
    margin: 0;
    padding: 0.6rem 0.45rem;
  }

  #sound-toggle {
    grid-column: 1 / -1;
    min-height: 38px;
  }

  .modal-backdrop {
    padding: max(0.75rem, env(safe-area-inset-top)) max(0.75rem, env(safe-area-inset-right))
      max(0.75rem, env(safe-area-inset-bottom)) max(0.75rem, env(safe-area-inset-left));
  }

  .modal-card {
    width: 100%;
    max-width: 360px;
    padding: 1.35rem 1.25rem;
  }

  .modal-card input,
  .modal-card button[type='submit'] {
    min-height: 44px;
  }

  .seed-label {
    font-size: 0.82rem;
  }

  .seed-label.store-label-3d {
    font-size: 1rem;
  }
}

@media (max-height: 520px) {
  .hint {
    display: none;
  }

  .side-panel {
    padding: 0.4rem;
  }

  .modal-card {
    padding: 0.9rem 1.1rem;
  }

  .modal-card h2 {
    margin-bottom: 0.3rem;
    font-size: 1.2rem;
  }
}
