:root {
  color-scheme: light dark;
  --bg: #f6eefc;
  --surface: #fffbfe;
  --ink: #3a2e42;
  --muted: #6b5d79;
  --line: #ead9f2;
  --owner-a: #b6377a;
  --owner-a-dark: #7a2350;
  --owner-a-glyph: #ffffff;
  --owner-b: #5064d4;
  --owner-b-dark: #33409e;
  --owner-b-glyph: #ffffff;
  --accent: #358066;
  --on-accent: #ffffff;
  --shadow: 0 2px 12px rgba(120, 70, 130, 0.12);
  --text-size: 1.2rem;
  --text-weight: 700;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1c1526;
    --surface: #2c2138;
    --ink: #f1ede4;
    --muted: #a89bb0;
    --line: #3d3049;
    --owner-a: #e893be;
    --owner-a-dark: #c15f92;
    --owner-a-glyph: #241b30;
    --owner-b: #a3b0f0;
    --owner-b-dark: #6c78c9;
    --owner-b-glyph: #241b30;
    --accent: #5fb99a;
    --on-accent: #241b30;
    --shadow: 0 2px 16px rgba(0, 0, 0, 0.35);
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
  }
}

* {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
}

body {
  font-family: ui-rounded, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 1rem;
  background: var(--bg);
  color: var(--ink);
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
}

#app {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

button {
  font: inherit;
  color: inherit;
}

/* ── Menu / difficulty screens ─────────────────────────────────────────── */

.screen {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1.25rem;
  padding: 2rem 1.25rem env(safe-area-inset-bottom);
  text-align: center;
}

.title {
  font-size: 2.5rem;
  margin: 0;
  letter-spacing: 0.02em;
}

.subtitle {
  color: var(--muted);
  margin: -0.75rem 0 0.5rem;
  max-width: 26rem;
  font-size: var(--text-size);
  font-weight: var(--text-weight);
  line-height: 1.5;
  text-align: left;
}

.menu-pieces {
  display: flex;
  justify-content: center;
  align-items: flex-end;
  gap: 1.5rem;
}

.menu-pieces .piece.pebble {
  width: 2.75rem;
  height: 2.75rem;
}

.menu-pieces .piece.boulder {
  width: 3.75rem;
  height: 3.75rem;
}

.btn-stack {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  width: 100%;
  max-width: 22rem;
}

.btn-big {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--ink);
  border-radius: 0.9rem;
  min-height: 3.25rem;
  font-size: var(--text-size);
  font-weight: var(--text-weight);
  padding: 0.5rem 1rem;
  box-shadow: var(--shadow);
  cursor: pointer;
}

.btn-big:active {
  transform: translateY(1px);
}

.btn-big.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.btn-big .desc {
  display: block;
  font-weight: var(--text-weight);
  font-size: var(--text-size);
  color: var(--muted);
  margin-top: 0.15rem;
}

.btn-big.primary .desc {
  color: color-mix(in srgb, var(--on-accent) 85%, transparent);
}

/* ── Playing screen ────────────────────────────────────────────────────── */

.game {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 100dvh;
}

.status-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  gap: 0.5rem;
}

.turn-group {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 0.15rem;
}

.status-bar .turn {
  font-weight: var(--text-weight);
  font-size: var(--text-size);
}

.status-bar .opp-hand {
  color: var(--muted);
  font-size: var(--text-size);
  font-weight: var(--text-weight);
}

.log-toggle {
  appearance: none;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  border-radius: 0.6rem;
  font-size: var(--text-size);
  font-weight: var(--text-weight);
  padding: 0.3rem 0.6rem;
  min-height: 2.75rem;
  min-width: 2.75rem;
  cursor: pointer;
  flex-shrink: 0;
}

.log-toggle[disabled] {
  opacity: 0.35;
  cursor: default;
}

.log-card {
  max-height: 80vh;
}

.log-list {
  list-style: none;
  margin: 0;
  padding: 0 0 0 0.2rem;
  max-height: 50vh;
  overflow-y: auto;
  text-align: left;
  width: 100%;
  position: relative;
}

.log-list .empty {
  color: var(--muted);
  text-align: center;
  padding: 0.4rem 0.6rem;
}

.log-item {
  display: flex;
  align-items: center;
  gap: 0.65rem;
  padding: 0.35rem 0.4rem;
  position: relative;
}

.log-item:nth-child(even):not(.win) {
  background: color-mix(in srgb, var(--ink) 6%, var(--surface));
  border-radius: 0.5rem;
}

.log-meta-wrap {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.log-detail {
  font-size: var(--text-size);
  font-weight: var(--text-weight);
  color: var(--muted);
}

.log-board {
  flex-shrink: 0;
  width: 3.4rem;
  height: 3.4rem;
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 1px;
  background: var(--line);
  border-radius: 0.4rem;
  padding: 1px;
}

.log-board .mini-cell {
  background: var(--surface);
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

.log-board .mini-cell.at {
  box-shadow: inset 0 0 0 1px var(--accent);
}

.log-board .piece {
  font-size: 0;
}

.log-item.win .mini-cell.at {
  box-shadow: inset 0 0 0 2px var(--accent);
}

.log-meta {
  font-size: var(--text-size);
  font-weight: var(--text-weight);
  color: var(--ink);
}

.log-item.win .log-meta {
  color: var(--accent);
}

.board-wrap {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0.5rem;
}

.board {
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  grid-template-rows: repeat(6, 1fr);
  gap: 4px;
  width: min(92vw, 480px);
  aspect-ratio: 1 / 1;
  background: var(--line);
  border-radius: 0.75rem;
  padding: 4px;
}

.cell {
  position: relative;
  background: var(--surface);
  border-radius: 0.5rem;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cell.legal {
  outline: 2px dashed var(--accent);
  outline-offset: -2px;
  background: color-mix(in srgb, var(--accent) 10%, var(--surface));
}

.cell.legal:active {
  background: color-mix(in srgb, var(--accent) 25%, var(--surface));
}

.piece {
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 0;
  font-weight: 700;
  user-select: none;
  transform: rotate(var(--tilt, 0deg));
}

.piece.pebble {
  width: 60%;
  height: 60%;
}

.piece.boulder {
  width: 84%;
  height: 84%;
}

.piece.owner-A, .turn-icon.owner-A {
  background: var(--owner-a);
  color: var(--owner-a-glyph);
  --ring: var(--owner-a-dark);
  box-shadow: inset 0 0 0 2px var(--ring);
  border-radius: 0;
  clip-path: polygon(25% 6.7%, 75% 6.7%, 100% 50%, 75% 93.3%, 25% 93.3%, 0% 50%);
}

.piece.owner-B, .turn-icon.owner-B {
  background: var(--owner-b);
  color: var(--owner-b-glyph);
  --ring: var(--owner-b-dark);
  box-shadow: inset 0 0 0 2px var(--ring);
  border-radius: 6%;
}

.turn-icon {
  display: inline-block;
  width: 0.75rem;
  height: 0.75rem;
  border-radius: 50%;
  vertical-align: middle;
  margin-right: 0.4rem;
}

.piece.placed {
  animation: drop-in 260ms cubic-bezier(0.34, 1.56, 0.64, 1);
}

.cell.bumped {
  animation: cell-pulse 250ms ease-out;
}

.hand-stack .count.pop {
  animation: count-pop 250ms ease-out;
}

@keyframes drop-in {
  0% {
    opacity: 0;
    transform: scale(0.15) rotate(0deg);
    box-shadow: inset 0 0 0 2px var(--ring), 0 0 0 rgba(0, 0, 0, 0);
  }
  60% {
    opacity: 1;
    transform: scale(1.15) rotate(var(--tilt, 0deg));
    box-shadow: inset 0 0 0 2px var(--ring), 0 10px 14px rgba(0, 0, 0, 0.28);
  }
  100% {
    opacity: 1;
    transform: scale(1) rotate(var(--tilt, 0deg));
    box-shadow: inset 0 0 0 2px var(--ring), 0 2px 4px rgba(0, 0, 0, 0.12);
  }
}

@keyframes cell-pulse {
  0% {
    background: color-mix(in srgb, var(--accent) 35%, var(--surface));
    transform: scale(1);
  }
  50% {
    transform: scale(1.04);
  }
  100% {
    background: var(--surface);
    transform: scale(1);
  }
}

@keyframes count-pop {
  0% {
    transform: scale(1);
  }
  40% {
    transform: scale(1.3);
  }
  100% {
    transform: scale(1);
  }
}

.hand-tray {
  display: flex;
  justify-content: center;
  gap: 0.75rem;
  padding: 0.75rem 1rem calc(0.75rem + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.hand-stack {
  appearance: none;
  border: 2px solid var(--line);
  background: var(--bg);
  border-radius: 0.9rem;
  min-width: 5.5rem;
  min-height: 5.5rem;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  position: relative;
  cursor: pointer;
}

.hand-stack[disabled] {
  opacity: 0.35;
  cursor: default;
}

.hand-stack.selected {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 12%, var(--bg));
}

.hand-stack .piece {
  margin-bottom: 0.1rem;
}

.hand-stack .piece.pebble {
  width: 1.35rem;
  height: 1.35rem;
}

.hand-stack .piece.boulder {
  width: 1.9rem;
  height: 1.9rem;
}

.hand-stack .label {
  font-size: var(--text-size);
  color: var(--muted);
  font-weight: var(--text-weight);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.hand-stack .count {
  font-size: var(--text-size);
  font-weight: var(--text-weight);
}

/* ── Overlay ───────────────────────────────────────────────────────────── */

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 10;
}

.card {
  background: var(--surface);
  border-radius: 1rem;
  padding: 2rem 1.5rem;
  text-align: center;
  box-shadow: var(--shadow);
  max-width: 24rem;
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  align-items: center;
}

.card h2 {
  margin: 0;
  font-size: 1.6rem;
}

@media (min-width: 640px) {
  .title {
    font-size: 3rem;
  }
}
