/* ── Reset & base ─────────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0f0f1a;
  --surface:     #1a1a2e;
  --surface2:    #16213e;
  --border:      #2a2a4a;

  --player-color:   #38bdf8;
  --player-glow:    #38bdf880;
  --player-dim:     #38bdf830;

  --ai-color:       #f97316;
  --ai-glow:        #f9731680;
  --ai-dim:         #f9731630;

  --accent:      #a855f7;
  --success:     #22c55e;
  --danger:      #ef4444;
  --text:        #e2e8f0;
  --text-muted:  #64748b;

  --radius:      16px;
  --radius-sm:   10px;
  --shadow:      0 4px 24px rgba(0,0,0,.5);
  --transition:  0.25s ease;
}

html { height: 100%; }

body {
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: 'Nunito', system-ui, sans-serif;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  position: sticky;
  top: 0;
  z-index: 10;
}

header h1 {
  font-size: 1.4rem;
  font-weight: 800;
  background: linear-gradient(135deg, var(--player-color), var(--accent));
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
  letter-spacing: -0.5px;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 8px;
}

#difficulty-select {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  outline: none;
  -webkit-appearance: none;
  appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1l5 5 5-5' stroke='%2364748b' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  padding-right: 26px;
}

.btn-icon {
  background: var(--surface2);
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 0.82rem;
  font-family: inherit;
  font-weight: 600;
  cursor: pointer;
  transition: var(--transition);
  white-space: nowrap;
}
.btn-icon:hover { color: var(--text); border-color: var(--accent); }

#reset-btn {
  font-size: 0.78rem;
  padding: 5px 9px;
  opacity: 0.7;
}
#reset-btn:hover { opacity: 1; color: var(--danger); border-color: var(--danger); }

/* ── Main layout ─────────────────────────────────────────────────────────── */
main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  padding: 16px;
  gap: 12px;
  max-width: 480px;
  width: 100%;
  margin: 0 auto;
}

/* ── Player areas ────────────────────────────────────────────────────────── */
.player-area {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.player-label {
  font-size: 0.75rem;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
}

.hands-container {
  display: flex;
  gap: 16px;
  justify-content: center;
  width: 100%;
}

/* ── Hand cards ──────────────────────────────────────────────────────────── */
.hand-card {
  flex: 1;
  max-width: 150px;
  min-height: 130px;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: var(--radius);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 16px 12px;
  cursor: pointer;
  user-select: none;
  transition: transform var(--transition), box-shadow var(--transition), border-color var(--transition), opacity var(--transition);
  position: relative;
  -webkit-tap-highlight-color: transparent;
}

.hand-card:active { transform: scale(0.96); }

.hand-label {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.finger-count {
  font-size: 2.6rem;
  font-weight: 900;
  line-height: 1;
  transition: transform 0.2s cubic-bezier(0.34, 1.56, 0.64, 1), color var(--transition);
}

.finger-pips {
  display: flex;
  gap: 5px;
  height: 14px;
  align-items: center;
}

.pip {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--border);
  transition: background var(--transition), transform var(--transition);
}
.pip.active { transform: scale(1.1); }

/* Player hand colors */
.player-area .hand-card { border-color: var(--player-dim); }
.player-area .finger-count { color: var(--player-color); }
.player-area .pip.active { background: var(--player-color); }

/* AI hand colors */
.opponent-area .hand-card { border-color: var(--ai-dim); }
.opponent-area .finger-count { color: var(--ai-color); }
.opponent-area .pip.active { background: var(--ai-color); }

/* Eliminated state */
.hand-card.eliminated {
  opacity: 0.35;
  border-style: dashed;
  cursor: default;
}
.hand-card.eliminated .finger-count { color: var(--text-muted); }

/* Glow: player selected */
.hand-card.glow-player {
  border-color: var(--player-color);
  box-shadow: 0 0 0 2px var(--player-color), 0 0 20px 4px var(--player-glow);
  transform: scale(1.04);
}

/* Glow: AI source */
.hand-card.glow-ai {
  border-color: var(--ai-color);
  box-shadow: 0 0 0 2px var(--ai-color), 0 0 20px 4px var(--ai-glow);
  transform: scale(1.04);
}

/* Glow: AI target (player hand being attacked) */
.hand-card.glow-target {
  border-color: var(--danger);
  box-shadow: 0 0 0 2px var(--danger), 0 0 20px 4px #ef444460;
  transform: scale(1.04);
}

/* Finger count "pop" animation */
@keyframes popIn {
  0%   { transform: scale(1); }
  40%  { transform: scale(1.35); }
  100% { transform: scale(1); }
}
.finger-count.popping { animation: popIn 0.35s cubic-bezier(0.34, 1.56, 0.64, 1); }

/* ── Status bar ──────────────────────────────────────────────────────────── */
.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 20px;
  gap: 12px;
}

#turn-indicator {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text);
  transition: opacity 0.3s;
}

#turn-indicator.thinking {
  color: var(--ai-color);
  animation: pulse 1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.45; }
}

#move-counter {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--text-muted);
  white-space: nowrap;
}

/* ── Hint bar ────────────────────────────────────────────────────────────── */
#hint-bar {
  width: 100%;
  text-align: center;
  font-size: 0.78rem;
  color: var(--text-muted);
  min-height: 1.2em;
  transition: opacity 0.3s;
}

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.65);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
  animation: fadeIn 0.2s ease;
}
.modal-overlay.hidden { display: none; }

@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal-box {
  position: relative;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 24px 20px;
  width: 100%;
  max-width: 340px;
  box-shadow: var(--shadow);
  animation: slideUp 0.25s cubic-bezier(0.34, 1.56, 0.64, 1);
}

/* Positioned in top-right of .modal-box; styled via .btn-icon */
.modal-close-x {
  -webkit-appearance: none;
  appearance: none;
  position: absolute !important;
  top: 12px !important;
  right: 12px !important;
  z-index: 10;
}

@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to   { transform: translateY(0);    opacity: 1; }
}

.modal-box h3 {
  font-size: 1.1rem;
  font-weight: 800;
  margin-bottom: 18px;
  text-align: center;
}

.modal-actions {
  display: flex;
  gap: 10px;
  margin-top: 20px;
  justify-content: center;
}

/* ── Redistribution modal ───────────────────────────────────────────────── */
.redistribute-hands {
  display: flex;
  gap: 20px;
  justify-content: center;
  margin-bottom: 14px;
}

.hand-control {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.hand-control .counter-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-muted);
}

.counter {
  display: flex;
  align-items: center;
  gap: 0;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
}

.counter-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 1.3rem;
  font-weight: 700;
  padding: 8px 14px;
  cursor: pointer;
  transition: background var(--transition);
  line-height: 1;
}
.counter-btn:hover:not(:disabled) { background: var(--border); }
.counter-btn:disabled { color: var(--text-muted); cursor: default; }

.counter-value {
  font-size: 1.6rem;
  font-weight: 900;
  min-width: 40px;
  text-align: center;
  color: var(--player-color);
}

.redistribute-total {
  text-align: center;
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.redistribute-warning {
  text-align: center;
  font-size: 0.78rem;
  color: var(--danger);
  min-height: 1.2em;
  margin-top: 6px;
}

/* ── Button styles ───────────────────────────────────────────────────────── */
.btn {
  border: none;
  border-radius: var(--radius-sm);
  padding: 10px 22px;
  font-size: 0.9rem;
  font-family: inherit;
  font-weight: 700;
  cursor: pointer;
  transition: var(--transition);
  letter-spacing: 0.02em;
}

.btn-primary {
  background: var(--player-color);
  color: #000;
}
.btn-primary:hover:not(:disabled) { filter: brightness(1.15); }
.btn-primary:disabled { opacity: 0.4; cursor: default; }

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
.btn-secondary:hover { border-color: var(--accent); }

.btn-danger {
  background: var(--danger);
  color: #fff;
}
.btn-danger:hover { filter: brightness(1.15); }

.btn-accent {
  background: var(--accent);
  color: #fff;
}
.btn-accent:hover { filter: brightness(1.15); }

/* ── Stats modal ─────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  margin-bottom: 14px;
}

.stat-block {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.stat-block.full-width { grid-column: 1 / -1; }

.stat-label {
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-muted);
  font-weight: 700;
}

.stat-value {
  font-size: 1.3rem;
  font-weight: 900;
  color: var(--text);
}

.stat-value.green { color: var(--success); }
.stat-value.orange { color: var(--ai-color); }

.best-wins-list {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 130px;
  overflow-y: auto;
}

.best-wins-list li {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 5px 8px;
  background: var(--surface2);
  border-radius: 6px;
  font-size: 0.8rem;
}

.best-wins-list .rank {
  font-weight: 800;
  color: var(--accent);
  min-width: 24px;
}

.best-wins-list .moves-count {
  font-weight: 700;
  color: var(--success);
}

.diff-rows { display: flex; flex-direction: column; gap: 4px; }

.diff-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.8rem;
  padding: 3px 0;
  border-bottom: 1px solid var(--border);
}
.diff-row:last-child { border-bottom: none; }

.diff-record { font-weight: 700; }
.diff-record .w { color: var(--success); }
.diff-record .l { color: var(--danger); }

/* ── Game over modal ─────────────────────────────────────────────────────── */
.game-over-icon {
  font-size: 3rem;
  text-align: center;
  margin-bottom: 8px;
  line-height: 1;
}

.game-over-title {
  font-size: 1.5rem;
  font-weight: 900;
  text-align: center;
  margin-bottom: 6px;
}

.game-over-subtitle {
  font-size: 0.88rem;
  color: var(--text-muted);
  text-align: center;
  margin-bottom: 4px;
}

.game-over-encourage {
  font-size: 0.85rem;
  color: var(--accent);
  text-align: center;
  font-weight: 700;
  margin-top: 8px;
}

/* ── Confetti canvas ─────────────────────────────────────────────────────── */
#confetti-canvas {
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 200;
  display: none;
}

/* ── Help / Instructions modal ───────────────────────────────────────────── */
.btn-help {
  font-weight: 900;
  color: var(--accent);
  border-color: var(--accent);
  min-width: 30px;
}
.btn-help:hover { background: var(--accent); color: #fff; }

.help-box {
  display: flex;
  flex-direction: column;
  max-height: 88vh;
}
.help-scroll-body {
  overflow-y: auto;
}

.help-goal {
  text-align: center;
  font-size: 0.88rem;
  color: var(--text-muted);
  margin-bottom: 14px;
  line-height: 1.5;
}

.help-section {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 10px;
}

.help-section-title {
  font-size: 0.82rem;
  font-weight: 800;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text);
  margin-bottom: 10px;
}

.help-body {
  font-size: 0.84rem;
  color: var(--text-muted);
  line-height: 1.5;
}

.help-steps {
  list-style: none;
  counter-reset: steps;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.help-steps li {
  counter-increment: steps;
  display: flex;
  align-items: flex-start;
  gap: 9px;
  font-size: 0.84rem;
  color: var(--text-muted);
  line-height: 1.45;
}

.help-steps li::before {
  content: counter(steps);
  background: var(--accent);
  color: #fff;
  font-size: 0.68rem;
  font-weight: 900;
  min-width: 19px;
  height: 19px;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  margin-top: 1px;
}

.help-step-intro {
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 8px;
}

.help-em {
  font-weight: 800;
  color: var(--text);
}

.help-tips {
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.help-tips li {
  font-size: 0.84rem;
  color: var(--text-muted);
  line-height: 1.45;
  padding-left: 12px;
  border-left: 2px solid var(--accent);
}

/* ── Scrollbar styling ───────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 4px; }

/* ── Utility ─────────────────────────────────────────────────────────────── */
.hidden { display: none !important; }

/* ── Desktop tweaks ──────────────────────────────────────────────────────── */
@media (min-width: 500px) {
  .hand-card { min-height: 150px; }
  .finger-count { font-size: 3rem; }
  header h1 { font-size: 1.6rem; }
}
