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

:root {
  --bg: #111827;
  --surface: #1f2937;
  --surface2: #374151;
  --border: #4b5563;
  --text: #f9fafb;
  --text-dim: #9ca3af;
  --accent: #3b82f6;
  --accent-hover: #2563eb;
  --danger: #ef4444;
  --highlight: #06b6d4;
  --valid: #22c55e;
  --wild-bg: #f59e0b;
  --wild-text: #1f2937;

  --c1:  #ef4444; --c2:  #f97316; --c3:  #eab308;
  --c4:  #84cc16; --c5:  #22c55e; --c6:  #14b8a6;
  --c7:  #06b6d4; --c8:  #3b82f6; --c9:  #8b5cf6;
  --c10: #ec4899; --c11: #f43f5e; --c12: #a855f7;

  --card-w: 58px;
  --card-h: 82px;
  --gap: 8px;
  --radius: 8px;
}

body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow: hidden;
}

/* ── Screens ── */
.screen { display: none; }
.screen.active { display: flex; }

#screen-setup {
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  gap: 8px;
}

.game-title {
  font-size: 3.5rem;
  font-weight: 800;
  letter-spacing: -1px;
  color: var(--text);
}

.tagline { color: var(--text-dim); font-size: 1rem; margin-bottom: 24px; }

.setup-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
  width: 280px;
}

.setup-form label {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 0.85rem;
  font-weight: 600;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.setup-form select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 10px 12px;
  font-size: 1rem;
  cursor: pointer;
  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='%239ca3af' stroke-width='1.5' fill='none' stroke-linecap='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

/* ── Buttons ── */
.btn {
  padding: 10px 20px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s, opacity 0.15s;
  line-height: 1;
}
.btn--primary { background: var(--accent); color: #fff; }
.btn--primary:hover { background: var(--accent-hover); }
.btn--accent { background: #7c3aed; color: #fff; }
.btn--accent:hover { background: #6d28d9; }
.btn--ghost {
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border);
}
.btn--ghost:hover { background: var(--surface2); color: var(--text); }
.btn--sm { padding: 6px 12px; font-size: 0.8rem; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

/* ── Game Screen ── */
#screen-game {
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto;
  overflow-x: hidden;
}

#topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.game-title-small {
  font-size: 1.2rem;
  font-weight: 800;
  letter-spacing: -0.5px;
}

#score-display {
  font-size: 0.78rem;
  color: var(--text-dim);
  display: flex;
  gap: 16px;
}

.score-item { display: flex; flex-direction: column; align-items: center; gap: 1px; }
.score-name { font-size: 0.65rem; text-transform: uppercase; letter-spacing: 0.05em; }
.score-val { font-size: 1rem; font-weight: 700; color: var(--text); }

.topbar-actions { display: flex; gap: 8px; }

/* ── AI Zone ── */
#ai-zone {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

.ai-player {
  display: flex;
  align-items: center;
  gap: 12px;
}

.ai-label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-dim);
  min-width: 60px;
}

.ai-thinking {
  font-size: 0.68rem;
  color: var(--accent);
  animation: pulse 1s ease-in-out infinite;
}
.ai-thinking.hidden { display: none; }

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

.ai-stock-area {
  display: flex;
  align-items: center;
  gap: 4px;
}

.ai-stock-card {
  width: calc(var(--card-w) * 0.7);
  height: calc(var(--card-h) * 0.7);
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  color: rgba(255,255,255,0.95);
}

.ai-stock-count {
  font-size: 0.75rem;
  color: var(--text-dim);
  font-weight: 600;
  min-width: 24px;
}

.ai-discards {
  display: flex;
  gap: 4px;
}

.ai-discard-card {
  width: calc(var(--card-w) * 0.7);
  height: calc(var(--card-h) * 0.7);
  border-radius: 4px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0.75rem;
  font-weight: 700;
  border: 1px dashed var(--border);
}

/* ── Building Zone ── */
#building-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 12px 16px;
  flex: 1;
  min-height: 80px;
}

.zone-label {
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--text-dim);
  margin-bottom: 10px;
  font-weight: 600;
}

#building-piles {
  display: flex;
  gap: 16px;
  align-items: center;
  justify-content: center;
}

.building-pile-slot {
  width: var(--card-w);
  height: var(--card-h);
  position: relative;
  cursor: pointer;
  border-radius: var(--radius);
  transition: transform 0.1s;
}

.building-pile-slot:hover { transform: translateY(-2px); }

/* ── Player Zone ── */
#player-zone {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  flex-shrink: 0;
}

#player-left { display: flex; flex-direction: column; align-items: center; gap: 4px; }

#player-discards { display: flex; flex-direction: column; align-items: center; gap: 4px; }

.discard-row { display: flex; gap: 6px; }

#player-right {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
  flex: 1;
}

#player-hand {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  justify-content: center;
}

.pile-label {
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--text-dim);
  font-weight: 600;
}

.stock-pile {
  width: var(--card-w);
  height: var(--card-h);
  cursor: pointer;
  border-radius: var(--radius);
  transition: transform 0.1s;
}
.stock-pile:hover { transform: translateY(-2px); }

.count-badge {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--text-dim);
}

.discard-pile-slot {
  width: var(--card-w);
  height: var(--card-h);
  cursor: pointer;
  border-radius: var(--radius);
  transition: transform 0.1s;
}
.discard-pile-slot:hover { transform: translateY(-2px); }

/* ── Cards ── */
.card {
  width: var(--card-w);
  height: var(--card-h);
  border-radius: var(--radius);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.4rem;
  font-weight: 800;
  cursor: pointer;
  user-select: none;
  position: relative;
  transition: transform 0.1s, box-shadow 0.1s;
  color: rgba(255,255,255,0.95);
  text-shadow: 0 1px 3px rgba(0,0,0,0.4);
  box-shadow: 0 2px 6px rgba(0,0,0,0.4);
}

.card:hover { transform: translateY(-3px); box-shadow: 0 6px 16px rgba(0,0,0,0.5); }

.card--num-1  { background: var(--c1); }
.card--num-2  { background: var(--c2); }
.card--num-3  { background: var(--c3); color: rgba(0,0,0,0.8); text-shadow: none; }
.card--num-4  { background: var(--c4); color: rgba(0,0,0,0.8); text-shadow: none; }
.card--num-5  { background: var(--c5); }
.card--num-6  { background: var(--c6); }
.card--num-7  { background: var(--c7); }
.card--num-8  { background: var(--c8); }
.card--num-9  { background: var(--c9); }
.card--num-10 { background: var(--c10); }
.card--num-11 { background: var(--c11); }
.card--num-12 { background: var(--c12); }
.card--wild   { background: var(--wild-bg); color: var(--wild-text); font-size: 1rem; }

.card--wild-top::after {
  content: 'W';
  position: absolute;
  bottom: 3px;
  right: 4px;
  font-size: 0.55rem;
  font-weight: 700;
  opacity: 0.65;
  line-height: 1;
}

.card--face-down {
  background: repeating-linear-gradient(
    135deg,
    var(--surface2) 0px,
    var(--surface2) 6px,
    var(--surface) 6px,
    var(--surface) 12px
  );
  border: 1px solid var(--border);
  cursor: default;
  font-size: 0;
}

.card--empty {
  background: transparent;
  border: 2px dashed var(--border);
  box-shadow: none;
  cursor: default;
  font-size: 0.65rem;
  color: var(--text-dim);
  font-weight: 600;
  text-shadow: none;
}
.card--empty:hover { transform: none; box-shadow: none; }

.card--selected {
  outline: 3px solid var(--highlight);
  outline-offset: 2px;
  transform: translateY(-4px);
  box-shadow: 0 8px 20px rgba(6,182,212,0.4);
}

.card--valid {
  outline: 3px solid var(--valid);
  outline-offset: 2px;
}

.card--discard-target {
  outline: 3px solid #f59e0b;
  outline-offset: 2px;
}

.card--inactive {
  opacity: 0.45;
  cursor: not-allowed;
  pointer-events: none;
}

.card--drag-over {
  outline: 2px dashed var(--accent);
  outline-offset: 3px;
  background: rgba(59,130,246,0.12);
  border-radius: var(--radius);
}

.card--ghost {
  position: fixed;
  pointer-events: none;
  z-index: 999;
  opacity: 0.75;
  transform: translate(-50%, -50%) rotate(4deg) scale(1.08);
  transition: none;
  box-shadow: 0 12px 32px rgba(0,0,0,0.6);
}

/* ── Status Bar ── */
#status-bar {
  padding: 6px 16px;
  background: var(--bg);
  border-top: 1px solid var(--border);
  font-size: 0.82rem;
  color: var(--text-dim);
  text-align: center;
  flex-shrink: 0;
}

#status-message { font-weight: 500; }

/* ── Win Screen ── */
#screen-win {
  align-items: center;
  justify-content: center;
  min-height: 100vh;
}

.win-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 48px;
  background: var(--surface);
  border-radius: 16px;
  border: 1px solid var(--border);
  text-align: center;
  max-width: 400px;
  width: 90%;
}

#win-message {
  font-size: 2rem;
  font-weight: 800;
  line-height: 1.2;
}

#win-score {
  font-size: 0.85rem;
  color: var(--text-dim);
  display: flex;
  gap: 20px;
  justify-content: center;
}

.win-score-item { display: flex; flex-direction: column; align-items: center; gap: 2px; }
.win-score-name { font-size: 0.7rem; text-transform: uppercase; letter-spacing: 0.05em; }
.win-score-val { font-size: 1.4rem; font-weight: 700; color: var(--text); }
.win-score-val.winner { color: var(--valid); }

.win-actions { display: flex; gap: 12px; }

/* ── Modal ── */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden { display: none; }

.modal-box {
  background: var(--surface);
  border-radius: 16px;
  border: 1px solid var(--border);
  max-width: 560px;
  width: 94%;
  max-height: 85vh;
  overflow-y: auto;
  padding: 32px;
  position: relative;
}

.modal-box h2 { font-size: 1.4rem; margin-bottom: 20px; }

.modal-body h3 {
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  margin: 20px 0 6px;
}
.modal-body h3:first-child { margin-top: 0; }

.modal-body p, .modal-body ul {
  font-size: 0.9rem;
  color: var(--text-dim);
  line-height: 1.6;
}

.modal-body ul { padding-left: 20px; margin-top: 6px; }
.modal-body li { margin-bottom: 4px; }
.modal-body strong { color: var(--text); }

.modal-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: var(--surface2);
  border: none;
  color: var(--text-dim);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  cursor: pointer;
  font-size: 0.9rem;
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal-close:hover { background: var(--border); color: var(--text); }

/* ── Responsive ── */
@media (max-width: 600px) {
  :root {
    --card-w: 44px;
    --card-h: 62px;
    --gap: 5px;
  }

  .game-title { font-size: 2.5rem; }
  #building-piles { gap: 8px; }
  #player-zone { gap: 8px; padding: 8px; }
  .discard-row { gap: 4px; }
  #player-hand { gap: 4px; }
  .card { font-size: 1.1rem; }

  #score-display { gap: 8px; }
  .score-val { font-size: 0.85rem; }
}

/* Wrap hand onto its own row so stock + discards don't fight for space */
@media (max-width: 540px) {
  #player-zone {
    flex-wrap: wrap;
  }

  #player-right {
    width: 100%;
    flex: none;
  }
}

@media (max-width: 400px) {
  :root { --card-w: 38px; --card-h: 54px; }
  .card { font-size: 0.9rem; }

  /* 2×2 building pile grid on very small screens */
  #building-piles {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    justify-items: center;
  }

  /* Stack AI discard cards so they don't overflow */
  .ai-discards { flex-wrap: wrap; max-width: 120px; }
}
