:root {
  --paper: #f4efe3;
  --paper-2: #ebe4d3;
  --ink: #2b2622;
  --ink-soft: #6b625a;
  --line: #d8cfbd;
  --teal: #2a9d8f;
  --teal-dark: #1d6f66;
  --coral: #e76f51;
  --coral-dark: #a9472f;
  --plum: #8e5fb5;
  --plum-dark: #5f3b80;
  --accent: #f2b84b;
  --danger: #c0392b;
  --hand: "Patrick Hand", "Comic Sans MS", "Segoe Print", cursive;
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font: 15px/1.4 var(--ui);
  overflow: hidden;
}
[hidden] { display: none !important; }

.screen { height: 100vh; height: 100dvh; display: flex; flex-direction: column; }

/* --- Typography & primitives -------------------------------------------- */
.logo {
  font-family: var(--hand);
  font-weight: 400;
  font-size: clamp(48px, 9vw, 92px);
  line-height: 1;
  margin: 0;
  letter-spacing: 1px;
  color: var(--ink);
  text-shadow: 3px 3px 0 var(--accent);
  text-align: center;
}
.logo.small { font-size: 32px; text-shadow: 2px 2px 0 var(--accent); }
.logo.tiny { font-size: 24px; text-shadow: 2px 2px 0 var(--accent); }

h2 { font-family: var(--hand); font-weight: 400; font-size: 28px; margin: 0 0 12px; }
h3 { font-family: var(--hand); font-weight: 400; font-size: 21px; margin: 14px 0 6px; }
h2 small, h3 small { font-family: var(--ui); font-size: 12px; color: var(--ink-soft); font-weight: 400; }

.card {
  background: #fffdf7;
  border: 2px solid var(--ink);
  border-radius: 14px 18px 12px 16px;
  box-shadow: 4px 5px 0 rgba(43, 38, 34, 0.18);
  padding: 18px 20px;
}

.btn {
  font: 16px var(--hand);
  letter-spacing: 0.3px;
  background: #fffdf7;
  color: var(--ink);
  border: 2px solid var(--ink);
  border-radius: 10px 12px 9px 11px;
  padding: 6px 14px;
  cursor: pointer;
  box-shadow: 2px 3px 0 rgba(43, 38, 34, 0.25);
  transition: transform 0.06s, box-shadow 0.06s, background 0.15s;
  white-space: nowrap;
}
.btn:hover { background: #fff6dc; }
.btn:active { transform: translate(2px, 2px); box-shadow: 0 1px 0 rgba(43, 38, 34, 0.25); }
.btn:disabled { opacity: 0.45; cursor: not-allowed; transform: none; }
.btn.primary { background: var(--accent); }
.btn.primary:hover { background: #f6c86a; }
.btn.big { font-size: 22px; padding: 8px 20px; }
.btn.small { font-size: 15px; padding: 3px 10px; }
.btn.ghost { background: transparent; box-shadow: none; border-style: dashed; }
.btn.armed { background: #e76f51; color: #fff; border-style: solid; }
.btn.active { background: var(--ink); color: var(--paper); }
.btn.ready-on { background: #7cc36a; }

input, select {
  font: 16px var(--ui);
  color: var(--ink);
  background: #fff;
  border: 2px solid var(--ink);
  border-radius: 8px;
  padding: 7px 10px;
  outline: none;
}
input:focus, select:focus { box-shadow: 0 0 0 3px rgba(242, 184, 75, 0.55); }
input:disabled, select:disabled { background: var(--paper-2); color: var(--ink-soft); border-color: var(--line); }

.hint { color: var(--ink-soft); font-size: 13px; margin: 6px 0 0; }
.spacer { flex: 1; }

/* --- Home ------------------------------------------------------------------ */
#home { align-items: center; justify-content: center; padding: 16px; overflow: auto; }
.home-inner { display: flex; flex-direction: column; align-items: center; gap: 14px; max-width: 460px; width: 100%; }
.tagline { font-family: var(--hand); font-size: 22px; margin: 0; text-align: center; color: var(--ink-soft); }
.home-card { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.field-label { font-weight: 600; font-size: 14px; }
.divider { display: flex; align-items: center; gap: 10px; color: var(--ink-soft); font-family: var(--hand); font-size: 17px; }
.divider::before, .divider::after { content: ""; flex: 1; border-top: 2px dashed var(--line); }
.join-row { display: flex; gap: 8px; }
.join-row input { flex: 1; min-width: 0; text-transform: uppercase; letter-spacing: 6px; font-weight: 700; text-align: center; }
.fineprint { font-size: 13px; color: var(--ink-soft); text-align: center; margin: 0; }

/* --- Lobby ----------------------------------------------------------------- */
#lobby { overflow: auto; padding: 14px 18px 24px; }
.lobby-head { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 16px; }
.room-code { font-family: var(--hand); font-size: 22px; }
.room-code strong { letter-spacing: 4px; background: var(--accent); padding: 0 8px; border-radius: 6px; }
.lobby-grid { display: grid; grid-template-columns: minmax(260px, 340px) 1fr; gap: 18px; align-items: start; }
@media (max-width: 820px) { .lobby-grid { grid-template-columns: 1fr; } }

.player-list { list-style: none; padding: 0; margin: 0 0 14px; display: flex; flex-direction: column; gap: 8px; }
.player-list li {
  display: flex; align-items: center; gap: 10px;
  border: 2px dashed var(--line); border-radius: 10px; padding: 8px 10px; min-height: 46px;
}
.player-list li.filled { border-style: solid; border-color: var(--ink); }
.swatch { width: 18px; height: 18px; border-radius: 50%; border: 2px solid var(--ink); flex: none; }
.swatch.s0 { background: var(--teal); }
.swatch.s1 { background: var(--coral); }
.swatch.s2 { background: var(--plum); }
.pname { font-family: var(--hand); font-size: 20px; }
.tag { font-size: 11px; text-transform: uppercase; letter-spacing: 0.5px; padding: 1px 6px; border-radius: 4px; background: var(--paper-2); color: var(--ink-soft); }
.tag.you { background: var(--ink); color: var(--paper); }
.tag.off { background: #f6d5cf; color: var(--danger); }

.rule-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 6px 0; border-bottom: 1px dashed var(--line); flex-wrap: wrap; }
.rule-row label { font-weight: 600; }
.rule-row input { width: 120px; }
.rule-desc { flex-basis: 100%; font-size: 12px; opacity: 0.7; margin-top: -2px; }
.cost-tables { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
@media (max-width: 1100px) { .cost-tables { grid-template-columns: 1fr; } }
.cost-table { width: 100%; border-collapse: collapse; }
.cost-table td { padding: 4px 4px; border-bottom: 1px dashed var(--line); vertical-align: middle; }
.cost-table td.icon { width: 48px; }
.cost-table td.nm { font-weight: 600; }
.cost-table td.nm small { display: block; font-weight: 400; color: var(--ink-soft); font-size: 12px; line-height: 1.25; }
.cost-table input { width: 78px; padding: 4px 6px; text-align: right; }
.terrain-chip { width: 36px; height: 26px; border-radius: 6px; border: 2px solid var(--ink); display: inline-block; }

/* --- Game ------------------------------------------------------------------ */
.topbar { display: flex; align-items: center; gap: 16px; padding: 6px 14px; border-bottom: 2px solid var(--ink); background: #fffdf7; flex-wrap: wrap; }
.phase { font-family: var(--hand); font-size: 22px; }
.timer { font-family: var(--hand); font-size: 22px; font-variant-numeric: tabular-nums; min-width: 60px; }
.timer.urgent { color: var(--danger); }
.vs { font-family: var(--hand); font-size: 19px; display: flex; gap: 6px; align-items: center; }
.vs .s0 { color: var(--teal-dark); }
.vs .s1 { color: var(--coral-dark); }
.vs .s2 { color: var(--plum-dark); }

#stageWrap { flex: 1; min-height: 0; position: relative; display: flex; align-items: center; justify-content: center; padding: 8px; }
#stage {
  display: block;
  border: 2px solid var(--ink);
  border-radius: 6px;
  box-shadow: 4px 5px 0 rgba(43, 38, 34, 0.18);
  touch-action: none;
  cursor: crosshair;
}
#stage.locked { cursor: default; }

#battleHud {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%);
  display: flex; align-items: center; gap: 10px;
  background: rgba(255, 253, 247, 0.92); border: 2px solid var(--ink); border-radius: 12px;
  padding: 4px 12px; font-family: var(--hand); font-size: 19px; pointer-events: none;
}
.hud-side { display: flex; gap: 6px; align-items: center; }
.hud-side.s0 { color: var(--teal-dark); }
.hud-side.s1 { color: var(--coral-dark); }
.hud-side.s2 { color: var(--plum-dark); }
.hud-count { font-variant-numeric: tabular-nums; font-weight: 700; min-width: 2.5em; text-align: center; }
.hud-bar { width: 180px; height: 10px; border: 2px solid var(--ink); border-radius: 6px; display: flex; overflow: hidden; background: var(--paper-2); }
.hud-fill { height: 100%; transition: width 0.3s; }
.hud-fill.s0 { background: var(--teal); }
.hud-fill.s1 { background: var(--coral); }
.hud-fill.s2 { background: var(--plum); }
#battleHud.ffa .hud-bar { width: 120px; }
.map-tag { font-size: 12px; text-transform: uppercase; letter-spacing: 0.5px; padding: 2px 8px; border-radius: 4px; background: var(--ink); color: var(--paper); margin-left: 6px; vertical-align: middle; }

#countdown {
  position: absolute; inset: 0; display: flex; align-items: center; justify-content: center;
  font-family: var(--hand); font-size: 140px; color: var(--ink); text-shadow: 5px 5px 0 var(--accent);
  pointer-events: none;
}

#results {
  position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%);
  min-width: min(440px, 92vw); max-width: 92vw; text-align: center;
}
#results h2 { font-size: 46px; margin-bottom: 2px; }
#results .reason { color: var(--ink-soft); margin: 0 0 12px; }
#results table { margin: 0 auto 14px; border-collapse: collapse; font-variant-numeric: tabular-nums; }
#results th, #results td { padding: 4px 14px; border-bottom: 1px dashed var(--line); }
#results th { font-family: var(--hand); font-weight: 400; font-size: 19px; }
#results td:first-child { text-align: left; color: var(--ink-soft); }
#results .actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }
#results.collapsed { top: auto; bottom: 16px; transform: translateX(-50%); padding: 8px 14px; min-width: 0; }

/* --- Palette ---------------------------------------------------------------- */
#palette {
  display: flex; gap: 16px; align-items: stretch;
  border-top: 2px solid var(--ink); background: #fffdf7; padding: 8px 14px;
  flex-wrap: wrap;
}
.pal-left { flex: 1; min-width: 280px; display: flex; flex-direction: column; gap: 6px; }
.tabs { display: flex; gap: 4px; }
.tab {
  font: 17px var(--hand); background: transparent; border: 2px solid transparent; border-radius: 8px;
  padding: 1px 12px; cursor: pointer; color: var(--ink-soft);
}
.tab.active { border-color: var(--ink); color: var(--ink); background: var(--paper); }
.items { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
.item {
  flex: none; width: 82px; display: flex; flex-direction: column; align-items: center; gap: 1px;
  background: var(--paper); border: 2px solid var(--line); border-radius: 10px; padding: 4px 2px 3px;
  cursor: pointer; font: 14px var(--hand); color: var(--ink);
}
.item:hover { border-color: var(--ink-soft); }
.item.active { border-color: var(--ink); background: #fff6dc; box-shadow: 2px 2px 0 rgba(43, 38, 34, 0.25); }
.item .cost { font: 12px var(--ui); font-weight: 700; color: var(--ink-soft); }
.item .chip { width: 44px; height: 44px; border-radius: 8px; border: 2px solid var(--ink); }
.item .key { position: absolute; }
.item.unaffordable { opacity: 0.45; }

.pal-tools { display: flex; flex-direction: column; justify-content: center; gap: 8px; }
.brush { display: flex; align-items: center; gap: 8px; font: 17px var(--hand); }
.brush input { width: 150px; padding: 0; border: none; accent-color: var(--ink); }
.tool-row { display: flex; gap: 6px; }

.pal-right { display: flex; align-items: center; gap: 14px; }
.budget { width: 200px; }
.budget-top { display: flex; justify-content: space-between; font: 18px var(--hand); }
.budget-bar { height: 14px; border: 2px solid var(--ink); border-radius: 8px; overflow: hidden; background: var(--paper-2); }
#budgetFill { height: 100%; width: 0; background: var(--teal); transition: width 0.12s, background 0.2s; }
#budgetFill.full { background: var(--coral); }
#readyHint { max-width: 140px; }

/* --- Toasts & banner -------------------------------------------------------- */
#toasts { position: fixed; top: 12px; left: 50%; transform: translateX(-50%); z-index: 50; display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast {
  background: var(--ink); color: var(--paper); padding: 7px 14px; border-radius: 10px;
  font: 17px var(--hand); box-shadow: 3px 3px 0 rgba(43, 38, 34, 0.25);
  animation: toast-in 0.18s ease-out;
}
.toast.err { background: var(--danger); }
@keyframes toast-in { from { opacity: 0; transform: translateY(-6px); } }
#connBanner {
  position: fixed; bottom: 12px; left: 50%; transform: translateX(-50%); z-index: 60;
  background: var(--danger); color: #fff; padding: 6px 14px; border-radius: 10px; font: 17px var(--hand);
}

@media (max-width: 700px) {
  .topbar { gap: 8px; }
  .logo.tiny { display: none; }
  .pal-right { width: 100%; justify-content: space-between; }
}
