:root {
  --bg: #ffffff;
  --text: #1a1a1b;
  --text-dim: #6b7280;
  --border: #d3d6da;
  --surface: #f6f7f8;

  --gray: #787c7f;
  --gray-text: #ffffff;
  --amber: #f0b429;
  --amber-text: #1a1a1b;
  --green: #6aaa64;
  --green-text: #ffffff;

  --accent: #3b82f6;
  --accent-text: #ffffff;

  --radius: 6px;
  --topbar-h: 52px;
  --card-w: 70px;
  --card-gap: 6px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121213;
    --text: #f2f2f2;
    --text-dim: #8b8b8c;
    --border: #3a3a3c;
    --surface: #1c1c1e;

    --gray: #3a3a3c;
    --gray-text: #d7dadc;
    --amber: #b59f3b;
    --amber-text: #121213;
    --green: #538d4e;
    --green-text: #ffffff;

    --accent: #4f8dfb;
    --accent-text: #121213;
  }
}

* { box-sizing: border-box; }

html, body {
  overflow-x: hidden;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Helvetica, Arial, sans-serif;
  display: flex;
  flex-direction: column;
  align-items: stretch;
}

/* ---------- top menu bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 5;
  width: 100%;
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top);
}

.topbar-inner {
  max-width: 480px;
  margin: 0 auto;
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 0 12px;
}

.topbar-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 800;
  letter-spacing: 2px;
  white-space: nowrap;
  flex: none;
}

.topbar-tabs {
  display: flex;
  gap: 6px;
  min-width: 0;
}

/* ---------- page content ---------- */

.wrap {
  width: 100%;
  max-width: 480px;
  margin: 0 auto;
  padding: 16px 14px 40px;
}

.tab-btn {
  padding: 6px 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text-dim);
  font-size: 0.7rem;
  font-weight: 700;
  letter-spacing: 0.2px;
  white-space: nowrap;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}

.tab-btn:hover { background: var(--border); }

.tab-btn.active {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}

.hidden { display: none !important; }

.status-bar {
  /* 3-column grid (not flex) so the middle column -- Mistakes -- sits
     truly centered regardless of the left/right content being different
     widths; space-between can't do that once the two ends are unequal. */
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 10px;
  max-width: 320px;
  margin: 0 auto 14px;
}

.status-left {
  justify-self: start;
  display: flex;
  align-items: center;
}

.date-label {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-dim);
  white-space: nowrap;
}

.status-left .btn {
  padding: 7px 14px;
  font-size: 0.72rem;
}

.icon-group {
  justify-self: end;
  display: flex;
  gap: 8px;
}

.icon-btn {
  width: 34px;
  height: 34px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 1.05rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.icon-btn:hover { background: var(--border); }
.icon-btn:active { transform: scale(0.93); }

.stat-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.stat-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--text-dim);
}

.stat-value {
  font-size: 1.1rem;
  font-weight: 800;
  font-variant-numeric: tabular-nums;
}

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

.pip {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--amber);
  flex: none;
}

.pip.used {
  background: transparent;
  border: 2px solid var(--border);
}

main {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
}

.board {
  display: grid;
  gap: 6px;
  width: 100%;
  max-width: 320px;
  aspect-ratio: 1 / 1;
}

.cell {
  position: relative;
  border-radius: var(--radius);
  min-width: 0;
  min-height: 0;
}

.cell.empty {
  border: 2px dashed var(--border);
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s, background 0.15s;
}

.cell.empty.droppable {
  border-color: var(--accent);
  border-style: solid;
  background: color-mix(in srgb, var(--accent) 10%, transparent);
  cursor: pointer;
}

.hand {
  display: flex;
  flex-wrap: wrap;
  gap: var(--card-gap);
  justify-content: center;
  /* Constrain the row to fit exactly --hand-cols cards (set inline by JS),
     so a hand bigger than one row wraps into groups of that size instead
     of however many happen to fit the viewport. Flexbox (not grid) is what
     makes this work for a ragged last row too: justify-content: center
     centers each wrapped line independently, so an incomplete final row
     (e.g. 9 cards -> 4 + 4 + 1) still sits centered instead of hugging the
     left edge the way a fixed-column grid would. */
  width: calc(var(--hand-cols, 1) * var(--card-w) + (var(--hand-cols, 1) - 1) * var(--card-gap));
  max-width: 100%;
  margin: 0 auto;
}

.card {
  position: relative;
  width: var(--card-w);
  height: var(--card-w); /* square, kept in sync with the board's own cell size by JS -- see syncCardSize() */
  border-radius: var(--radius);
  background: var(--gray);
  cursor: pointer;
  user-select: none;
  transition: transform 0.12s ease;
  flex: none;
}

.board .card {
  width: 100%;
  height: 100%;
}

.card.owned-enemy {
  background: var(--gray);
}

.card.owned-player {
  background: var(--green);
}

.card.pending {
  background: var(--amber);
}

.card.placeholder {
  background: transparent;
  border: 2px dashed var(--border);
  cursor: default;
  pointer-events: none;
}

.board.locked .card.placed {
  cursor: not-allowed;
}

.card.placed:not(.last-placed) {
  cursor: not-allowed;
}

.card.selected {
  transform: translateY(-5px);
  box-shadow: 0 0 0 3px var(--text);
}

.card.last-placed {
  box-shadow: 0 0 0 3px var(--amber);
}

.card:not(.board .card):hover {
  transform: translateY(-3px);
}

.card.flipping {
  animation: flip 0.45s ease;
}

@keyframes flip {
  0%   { transform: scale(1) rotateY(0deg); }
  40%  { transform: scale(1.06) rotateY(90deg); }
  60%  { transform: scale(1.06) rotateY(90deg); }
  100% { transform: scale(1) rotateY(0deg); }
}

.card.shake {
  animation: shake 0.4s ease;
}

@keyframes shake {
  20%, 80% { transform: translateX(3px); }
  30%, 70% { transform: translateX(-6px); }
  40%, 60% { transform: translateX(6px); }
  50%      { transform: translateX(-6px); }
}

.stat {
  position: absolute;
  font-size: 0.85rem;
  font-weight: 800;
  line-height: 1;
  width: 18px;
  text-align: center;
}

.card.owned-enemy .stat { color: var(--gray-text); }
.card.owned-player .stat { color: var(--green-text); }
.card.pending .stat { color: var(--amber-text); }

.stat.dim {
  opacity: 0.4;
}

.stat.top    { top: 5px; left: 50%; transform: translateX(-50%); }
.stat.bottom { bottom: 5px; left: 50%; transform: translateX(-50%); }
.stat.left   { left: 5px; top: 50%; transform: translateY(-50%); }
.stat.right  { right: 5px; top: 50%; transform: translateY(-50%); }

.btn {
  padding: 11px 18px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
  font-weight: 700;
  letter-spacing: 0.3px;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
}

.btn:hover { background: var(--border); }
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--accent-text);
}
.btn-primary:hover { filter: brightness(1.08); }

.btn-ghost {
  background: transparent;
  border-color: transparent;
  color: var(--text-dim);
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  z-index: 10;
}

.modal-card {
  position: relative;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px;
  max-width: 360px;
  width: 100%;
  text-align: center;
}

.modal-card h2 {
  margin: 0 0 10px;
  font-weight: 800;
  letter-spacing: 0.5px;
}

.modal-card p {
  color: var(--text-dim);
  margin: 0 0 18px;
  line-height: 1.5;
}

.modal-x {
  position: absolute;
  top: 10px;
  right: 10px;
  width: 30px;
  height: 30px;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--text-dim);
  font-size: 1.3rem;
  line-height: 1;
  cursor: pointer;
}

.modal-x:hover { background: var(--surface); color: var(--text); }

.rules-list {
  margin: 0;
  padding-left: 20px;
  text-align: left;
  color: var(--text-dim);
  font-size: 0.85rem;
  line-height: 1.5;
}

.rules-list li { margin-bottom: 10px; }
.rules-list li:last-child { margin-bottom: 0; }

.result-stats {
  display: flex;
  justify-content: center;
  gap: 22px;
  margin: 0 0 22px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.modal-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

/* ---------- responsive ---------- */

@media (min-width: 420px) {
  :root { --card-gap: 8px; }
  .stat { font-size: 0.95rem; }
  .board { gap: 8px; }
}

@media (max-width: 340px) {
  .wrap { padding: 12px 8px 32px; }
  .status-bar { gap: 14px; }
  .stat { font-size: 0.75rem; width: 16px; }
  .topbar-title { font-size: 0.9rem; letter-spacing: 1px; }
  .tab-btn { padding: 5px 7px; font-size: 0.62rem; }
}
