/* Mycelia web adaptation — forest-floor theme with dark & light modes. */

:root {
  --bg: #1a2117;
  --bg2: #212a1c;
  --panel: #2a3524;
  --panel2: #33402c;
  --ink: #e8e6d9;
  --muted: #a5a894;
  --accent: #d1a53f;
  --accent-ink: #241d0c;
  --accent2: #8fbf6f;
  --danger: #c46a5a;
  --border: #3c4a34;
  --border2: #4a5840;
  --border3: #55684a;
  --shadow: rgba(0, 0, 0, 0.5);
  --board-a: #28331f;
  --board-b: #1a2117;
  --card-a: #3a4531;
  --card-b: #2c3625;
  --card-stripe: #313d29;
  --wild-dot: #ddd;
  --token-face: #1d2417;
  --overlay-veil: rgba(10, 14, 8, 0.82);
  --radius: 10px;
}

:root[data-theme="light"] {
  --bg: #e9e1cb;
  --bg2: #efe8d5;
  --panel: #f6f0df;
  --panel2: #e7dfc7;
  --ink: #33301f;
  --muted: #7b775f;
  --accent: #a97d1e;
  --accent-ink: #fff8e6;
  --accent2: #4c7a3c;
  --danger: #a34d3d;
  --border: #cfc5a5;
  --border2: #c2b892;
  --border3: #b3a980;
  --shadow: rgba(84, 72, 40, 0.35);
  --board-a: #efe8d2;
  --board-b: #ded4b4;
  --card-a: #fbf6e8;
  --card-b: #efe8d2;
  --card-stripe: #f3ecd9;
  --wild-dot: #fff;
  --token-face: #fbf6e8;
  --overlay-veil: rgba(90, 80, 52, 0.55);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: Georgia, 'Times New Roman', serif;
  -webkit-tap-highlight-color: transparent;
  /* Scroll panning only — page-level pinch zoom is disabled everywhere.
     The board opts back in to full gesture handling with touch-action:none
     and runs its own pinch zoom. */
  touch-action: pan-x pan-y;
  /* The page itself never scrolls or rubber-bands; only designated panels
     (sidebar, log, card strips, overlays) scroll internally. */
  overflow: hidden;
  overscroll-behavior: none;
}

button {
  font: inherit;
  color: var(--ink);
  background: linear-gradient(180deg, var(--panel2), color-mix(in srgb, var(--panel2) 82%, black));
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 7px 14px;
  cursor: pointer;
  touch-action: manipulation; /* no double-tap zoom delay on controls */
  user-select: none;
  -webkit-user-select: none;
  transition: transform 0.12s ease, box-shadow 0.12s ease, filter 0.12s ease;
}
button:hover:not(:disabled) { filter: brightness(1.12); box-shadow: 0 2px 8px var(--shadow); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.35; cursor: default; }
button.primary {
  background: linear-gradient(180deg, color-mix(in srgb, var(--accent) 88%, white), var(--accent));
  color: var(--accent-ink);
  border-color: color-mix(in srgb, var(--accent) 70%, black);
  font-weight: bold;
}
button.big { font-size: 1.15rem; padding: 12px 26px; }
button.pulse { animation: pulse 1.2s infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 16px var(--accent); } }

.theme-toggle { font-size: 1.05rem; padding: 6px 10px; line-height: 1; }

.hidden { display: none !important; }
.muted { color: var(--muted); font-size: 0.9em; }
.chip {
  display: inline-block;
  width: 14px; height: 14px;
  border-radius: 50%;
  vertical-align: -2px;
  border: 1px solid rgba(0, 0, 0, 0.4);
  box-shadow: inset 0 1px 1px rgba(255, 255, 255, 0.35);
}

/* ------------------------------ setup ------------------------------ */

.screen { min-height: 100vh; }

#setup-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100vh;
  height: 100dvh;
  overflow-y: auto; /* the setup card may exceed a short phone screen */
  background:
    radial-gradient(circle at 18% 18%, color-mix(in srgb, var(--accent2) 14%, transparent) 0%, transparent 45%),
    radial-gradient(circle at 82% 72%, color-mix(in srgb, var(--accent) 12%, transparent) 0%, transparent 45%),
    var(--bg);
}

.setup-card {
  background: var(--panel);
  border: 1px solid var(--border3);
  border-radius: 16px;
  padding: 32px 40px;
  max-width: 460px;
  width: 92%;
  box-shadow: 0 12px 40px var(--shadow);
  position: relative;
}
.setup-card h1 {
  margin: 0 0 4px;
  font-size: 2.4rem;
  color: var(--accent);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.25);
  letter-spacing: 0.02em;
}
.setup-theme { position: absolute; top: 14px; right: 14px; }
.subtitle { margin-top: 0; color: var(--muted); }
.tagline { margin: 4px 0 0; color: var(--accent); font-style: italic; letter-spacing: 0.02em; }
.setup-label { display: block; margin: 14px 0 4px; }
#player-count {
  font: inherit;
  background: var(--panel2);
  color: var(--ink);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 6px 10px;
  width: 100%;
}
.name-row { display: flex; align-items: center; gap: 8px; margin-top: 8px; }
.name-row input {
  flex: 1;
  font: inherit;
  background: var(--panel2);
  color: var(--ink);
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 6px 10px;
}
#automata-row { margin-top: 12px; }
#start-game { margin-top: 20px; width: 100%; }
#resume-row { margin-top: 12px; text-align: center; }
#resume-game { width: 100%; }
#resume-desc { margin: 6px 0 0; }
.small-print { margin-bottom: 0; }

/* ------------------------------ layout ------------------------------ */

#game-screen {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* track the real viewport as mobile browser chrome hides */
}

#layout { display: flex; flex: 1; min-height: 0; }

#board-wrap {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

#board-area {
  position: relative;
  flex: 1;
  display: flex;
  min-height: 0;
}

#board {
  flex: 1;
  width: 100%;
  min-height: 0;
  background:
    radial-gradient(circle at 50% 40%, var(--board-a) 0%, var(--board-b) 78%);
  cursor: grab;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}
#board:active { cursor: grabbing; }

/* Floating control clusters in the board's corners. */
.board-cluster {
  position: absolute;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 5;
}
.board-cluster button:not(.primary) {
  background: color-mix(in srgb, var(--panel2) 88%, transparent);
  backdrop-filter: blur(2px);
}
#board-tl { top: 10px; left: 10px; }
#board-tl button { padding: 7px 12px; font-size: 0.9rem; min-height: 38px; }
#board-controls { top: 10px; right: 10px; }
#board-controls button {
  width: 42px;
  height: 42px;
  padding: 0;
  font-size: 1.2rem;
  line-height: 1;
}
#board-bl { bottom: 10px; left: 10px; flex-direction: row; }
#board-bl button {
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}
#board-br { bottom: 10px; right: 10px; flex-direction: row; align-items: center; }
#board-br #btn-log {
  width: 40px;
  height: 40px;
  padding: 0;
  font-size: 1rem;
  line-height: 1;
}
#board-br #btn-end-turn { min-height: 40px; }

#log-panel {
  position: absolute;
  right: 10px;
  bottom: 58px;
  width: min(320px, 82%);
  background: color-mix(in srgb, var(--panel) 94%, transparent);
  backdrop-filter: blur(3px);
  border: 1px solid var(--border3);
  border-radius: var(--radius);
  padding: 8px 10px;
  z-index: 6;
  box-shadow: 0 6px 18px var(--shadow);
}

/* One consolidated line: whose turn, what to do, wind. */
#status-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px 12px;
  padding: 6px 12px;
  min-height: 34px;
  background: var(--bg2);
  border-top: 1px solid var(--border);
  font-size: 0.95rem;
}
#message { color: var(--accent); }
#wind-display {
  margin-left: auto;
  color: var(--accent2);
  font-size: 0.85rem;
  white-space: nowrap;
}
#btn-cancel { padding: 3px 10px; min-height: 26px; font-size: 0.85rem; }

#sidebar {
  width: 340px;
  overflow-y: auto;
  overscroll-behavior: contain; /* reaching the end never drags the page */
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 0 12px 16px;
  display: flex;
  flex-direction: column;
}

/* Compact tab bar (all screen sizes). */
#panel-tabs {
  display: flex;
  gap: 4px;
  padding: 6px 0;
  position: sticky;
  top: 0;
  background: var(--panel);
  z-index: 5;
}
#panel-tabs button {
  flex: 1;
  padding: 4px 2px;
  min-height: 32px;
  font-size: 0.85rem;
}
#panel-tabs button.active {
  background: var(--accent);
  color: var(--accent-ink);
  border-color: color-mix(in srgb, var(--accent) 70%, black);
  font-weight: bold;
}
.tab-badge {
  display: inline-block;
  min-width: 17px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--accent-ink);
  color: var(--accent);
  font-size: 0.7rem;
  font-weight: bold;
  text-align: center;
  line-height: 17px;
}
#panel-tabs button.active .tab-badge { background: var(--accent-ink); color: var(--accent); }
#panel-tabs button:not(.active) .tab-badge { background: var(--accent); color: var(--accent-ink); }

#sidebar section { display: none; }
#sidebar[data-tab="hand"] #sec-hand { display: block; }
#sidebar[data-tab="mats"] #sec-mats { display: block; }
#sidebar[data-tab="market"] #sec-market { display: block; }
#sidebar[data-tab="players"] #sec-players { display: block; }

/* Action buttons that live inside a tab. */
.tab-actions { display: flex; gap: 6px; margin: 2px 0 10px; }
.tab-actions button { flex: 1; min-height: 36px; }

/* ------------------------------ board pieces ------------------------------ */

/* Thin black tile border — the same weight as the insect token discs. */
.tile { stroke: #17140e; stroke-width: 0.7; }
.tile.highlight {
  stroke: #ffd35c;
  stroke-width: 2.5;
  cursor: pointer;
}
/* The glowing golden overlay does the beckoning; opacity-only animation
   keeps the geometry fixed so taps register cleanly. */
.hl-overlay {
  fill: #ffd35c;
  animation: hl-pulse 1.2s ease-in-out infinite;
}
@keyframes hl-pulse {
  0%, 100% { opacity: 0.3; }
  50% { opacity: 0.6; }
}
/* Four-point sparkles twinkling over highlighted tiles (staggered delays
   are set inline per sparkle). */
.hl-sparkle {
  fill: #fff6c8;
  animation: hl-twinkle 1.6s ease-in-out infinite;
  transform-box: fill-box;
  transform-origin: center;
}
@keyframes hl-twinkle {
  0%, 100% { opacity: 0; transform: scale(0.35) rotate(-20deg); }
  50% { opacity: 1; transform: scale(1) rotate(20deg); }
}
.tile.selected { stroke: #fff; stroke-width: 3; }
:root[data-theme="light"] .tile.selected { stroke: #4a4430; }
.ghost-tile {
  fill: color-mix(in srgb, var(--accent) 14%, transparent);
  stroke: var(--accent);
  stroke-dasharray: 5 5;
  stroke-width: 1.5;
  cursor: pointer;
}
.ghost-tile:hover { fill: color-mix(in srgb, var(--accent) 32%, transparent); }
.ghost-plus { fill: var(--accent); font-size: 22px; text-anchor: middle; pointer-events: none; }

.spore-cube { pointer-events: none; }
/* Badge fill/stroke are set per-element in the SVG (the owning player's
   colour, semi-transparent) — a fill here would override them. */
.spore-badge { pointer-events: none; }
.spore-count {
  fill: #f5efe0;
  font-size: 9px;
  font-weight: bold;
  text-anchor: middle;
  pointer-events: none;
  paint-order: stroke;
  stroke: rgba(23, 20, 14, 0.9);
  stroke-width: 1.4px;
  stroke-linejoin: round;
}
.token-pip-dot { fill: #fff; opacity: 0.92; pointer-events: none; }
.wind-chip {
  fill: rgba(245, 240, 226, 0.5);
  stroke: rgba(40, 30, 15, 0.25);
  stroke-width: 0.8;
  pointer-events: none;
}
.wind-chip.active {
  fill: var(--accent);
  stroke: #241d0c;
  animation: hl-pulse 1.2s ease-in-out infinite;
}
.wind-mark {
  stroke: #3a3020;
  stroke-width: 1.1;
  pointer-events: none;
}
.wind-mark.active { stroke: #241d0c; stroke-width: 1.4; }
.insect { pointer-events: none; }
.insect-disc { fill: #f8f4e9; stroke: #17140e; stroke-width: 0.7; }
.insect-face { font-size: 8px; text-anchor: middle; }

/* Pieces pop into place; geometry-safe (scale only, one-shot). */
.fx-pop {
  animation: fx-pop 0.28s ease-out;
  transform-box: fill-box;
  transform-origin: center;
}
@keyframes fx-pop {
  from { transform: scale(0.15); opacity: 0; }
  60% { transform: scale(1.18); }
  to { transform: scale(1); opacity: 1; }
}

#wind-display.rolling { animation: wind-shake 0.18s linear infinite; }
@keyframes wind-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-2px); }
  75% { transform: translateX(2px); }
}

/* ------------------------------ panels ------------------------------ */

.player-row {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px;
  border-radius: 6px;
  font-size: 0.92rem;
}
.player-row.active {
  background: var(--panel2);
  outline: 1px solid var(--border3);
  box-shadow: 0 1px 4px var(--shadow);
}
.p-name { font-weight: bold; }
.p-stats { margin-left: auto; color: var(--muted); font-size: 0.85rem; white-space: nowrap; }
.p-mats { letter-spacing: 2px; }

#mats { display: flex; gap: 6px; flex-wrap: wrap; align-items: flex-end; }
.mat-stack { display: flex; flex-direction: column; align-items: stretch; }
/* Decayed cards peek out from under the top edge of the mat. */
.mat-under {
  margin: 0 6px -5px;
  padding: 2px 6px 7px;
  border-radius: 6px 6px 0 0;
  background: color-mix(in srgb, var(--panel2) 70%, black);
  border: 1px solid var(--border2);
  border-bottom: none;
  color: var(--danger);
  font-size: 0.66rem;
  text-align: center;
  cursor: help;
}
.mat {
  position: relative;
  z-index: 1;
  background: linear-gradient(180deg, var(--panel2), color-mix(in srgb, var(--panel2) 88%, black));
  border: 1px solid var(--border2);
  border-radius: 8px;
  padding: 5px;
  width: 68px;
  text-align: center;
  font-size: 0.72rem;
  box-shadow: 0 1px 3px var(--shadow);
}
.mat-pips { color: var(--muted); letter-spacing: 1px; }
.mat-token {
  margin: 4px auto;
  width: 28px; height: 28px;
  border-radius: 50%;
  border: 3px solid;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: bold;
  background: var(--token-face);
}
.mat-card { min-height: 32px; padding: 2px 0; }
/* Spore counter track: colour slot -> 1 -> 2, marker on the current step. */
.mat-track {
  display: flex;
  gap: 3px;
  justify-content: center;
  margin-top: 4px;
}
.track-slot {
  width: 15px;
  height: 15px;
  border-radius: 3px;
  border: 1px solid var(--border2);
  background: var(--token-face);
  font-size: 0.62rem;
  line-height: 14px;
  color: var(--muted);
}
.track-slot.color { border-color: rgba(0, 0, 0, 0.4); }
.track-slot.active {
  outline: 2px solid var(--accent);
  color: var(--ink);
  font-weight: bold;
}
.mat-bottom { min-height: 12px; color: var(--danger); }
.mat-upg { color: var(--accent); cursor: help; }

/* ------------------------------ cards ------------------------------ */

.card-row { display: flex; flex-wrap: wrap; gap: 8px; }

.card {
  background: linear-gradient(160deg, var(--card-a), var(--card-b));
  border: 1px solid var(--border3);
  border-radius: var(--radius);
  padding: 8px;
  width: 148px;
  font-size: 0.8rem;
  position: relative;
  box-shadow: 0 2px 6px var(--shadow);
  transition: transform 0.12s ease, box-shadow 0.12s ease;
}
.card.small { width: 96px; font-size: 0.7rem; }
.card.large { width: 280px; font-size: 1rem; }
.card.inspect { cursor: zoom-in; }
.card.inspect:hover { transform: translateY(-2px); }
.card.clickable { cursor: pointer; outline: 2px solid var(--accent); }
.card.clickable:hover { transform: translateY(-3px); box-shadow: 0 8px 18px var(--shadow); }
.card.disabled { opacity: 0.45; }
.card.face-down {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 120px;
  background: repeating-linear-gradient(45deg, var(--card-b), var(--card-b) 8px, var(--card-stripe) 8px, var(--card-stripe) 16px);
}
.card-back { font-size: 2rem; }
.deck-count {
  position: absolute;
  bottom: 4px; right: 8px;
  color: var(--muted);
  font-size: 0.75rem;
}
.card-top { display: flex; justify-content: space-between; align-items: flex-start; }
/* Points sit in a gold diamond, like the rulebook describes. */
.card-pts {
  width: 26px;
  height: 26px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  clip-path: polygon(50% 0, 100% 50%, 50% 100%, 0 50%);
  background: linear-gradient(135deg, #e8c65e, #b0842a);
  color: #241d0c;
  font-weight: bold;
  font-size: 0.95em;
}
.card.small .card-pts { width: 21px; height: 21px; font-size: 0.85em; }
.card-sc { color: var(--accent2); }
.card-name { font-weight: bold; margin-top: 2px; }
.card-latin { font-style: italic; color: var(--muted); font-size: 0.9em; }
.card-art { width: 78%; display: block; margin: 2px auto 0; }
.card.large .card-art { width: 88%; }
.card-cost { margin: 6px 0; min-height: 14px; }
/* Cost pips drawn as little wooden cubes, matching the spore pieces. */
.cost-dot {
  display: inline-block;
  width: 13px; height: 13px;
  border-radius: 2px;
  margin-right: 3px;
  border: 1px solid rgba(0, 0, 0, 0.45);
  box-shadow:
    inset -2px -2px 0 rgba(0, 0, 0, 0.22),
    inset 2px 2px 0 rgba(255, 255, 255, 0.28);
}
.cost-dot.wild {
  background: var(--wild-dot);
  border-style: dashed;
  border-color: #888;
}
.card-decay {
  border-top: 1px dashed var(--border3);
  padding-top: 4px;
  color: var(--muted);
  font-size: 0.88em;
}

/* ------------------------------ log ------------------------------ */

#log {
  max-height: min(38vh, 320px);
  overflow-y: auto;
  overscroll-behavior: contain;
  font-size: 0.82rem;
  color: var(--muted);
  line-height: 1.45;
}

/* ------------------------------ overlays ------------------------------ */

.overlay {
  position: fixed;
  inset: 0;
  background: var(--overlay-veil);
  backdrop-filter: blur(2px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 50;
}
.overlay-card {
  background: var(--panel);
  border: 1px solid var(--border3);
  border-radius: 16px;
  padding: 26px 32px;
  max-width: 640px;
  width: 92%;
  max-height: 88vh;
  overflow-y: auto;
  overscroll-behavior: contain;
  box-shadow: 0 12px 40px var(--shadow);
  text-align: center;
}
.overlay-card h2 { margin-top: 0; color: var(--accent); }
#modal-buttons { display: flex; gap: 10px; justify-content: center; margin-top: 16px; flex-wrap: wrap; }
.card-choices { display: flex; gap: 12px; justify-content: center; flex-wrap: wrap; }
.slot-choices { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
.slot-btn { min-width: 110px; }

.help-card { text-align: left; }
.help-body { font-size: 0.92rem; line-height: 1.5; }
.help-body ul { padding-left: 20px; }

.score-table { margin: 0 auto; border-collapse: collapse; }
.score-table th, .score-table td { padding: 6px 12px; border-bottom: 1px solid var(--border); }

/* ------------------------------ small screens ------------------------------ */

@media (max-width: 900px) {
  /* App-like split: board on top, status line, tabbed panel below —
     everything within the viewport (no page scroll). */
  #layout { flex-direction: column; min-height: 0; }
  #board-wrap { flex: 0 0 auto; }
  #board-area { height: 48vh; height: 48dvh; min-height: 240px; flex: none; }
  #sidebar {
    flex: 1;
    min-height: 0;
    width: 100%;
    border-left: none;
    border-top: 1px solid var(--border);
    padding: 0 10px 12px;
  }

  /* Card rows become horizontal swipe strips. */
  .card-row {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding-bottom: 6px;
    -webkit-overflow-scrolling: touch;
  }
  .card, .card.small { flex: 0 0 auto; }
  .card { width: 132px; }
  .card.small { width: 104px; }
  #mats { flex-wrap: nowrap; overflow-x: auto; padding-bottom: 4px; }
  .mat { flex: 0 0 auto; }

  #status-bar { font-size: 0.85rem; padding: 5px 10px; min-height: 30px; }
  #board-tl button { min-height: 42px; }
  #board-controls button { width: 44px; height: 44px; }
  #board-bl button, #board-br #btn-log { width: 42px; height: 42px; }
  #board-br #btn-end-turn { min-height: 42px; }

  .overlay-card { padding: 18px 16px; max-height: 92vh; max-height: 92dvh; }
  .overlay-card h2 { font-size: 1.2rem; }
  #modal-buttons button, .slot-btn { min-height: 44px; }
  .help-card { font-size: 0.95rem; }
}

@media (max-width: 480px) {
  #board-area { height: 44vh; height: 44dvh; }
  #board-tl button { padding: 6px 8px; font-size: 0.82rem; }
  .setup-card { padding: 22px 18px; }
}

/* Phones held sideways: board left, tabbed panel right. */
@media (max-width: 950px) and (orientation: landscape) and (max-height: 520px) {
  #layout { flex-direction: row; }
  #board-wrap { flex: 1.3; }
  #board-area { height: auto; flex: 1; min-height: 0; }
  #sidebar {
    width: 44%;
    max-width: 400px;
    border-top: none;
    border-left: 1px solid var(--border);
  }
  #status-bar { min-height: 24px; font-size: 0.78rem; padding: 3px 8px; }
  #board-tl button { min-height: 34px; padding: 4px 8px; font-size: 0.78rem; }
  #board-controls button { width: 36px; height: 36px; }
  #board-bl button, #board-br #btn-log { width: 36px; height: 36px; }
  #board-br #btn-end-turn { min-height: 36px; font-size: 0.82rem; }
}
