:root {
  --bg: #eef1ec;
  --felt: #dfe7dc;
  --panel: #ffffff;
  --ink: #1d2420;
  --muted: #5d6a62;
  --line: #c9d3cb;
  --accent: #2f6f4f;
  --accent-ink: #ffffff;
  --warn: #a5521b;
  --danger: #a83232;
  --sel: #e2a72e;
  --card: #fbfaf6;
  --card-line: #9aa79e;
  --opp: #7a3b3b;
  --me: #2f5e8a;
  --shadow: 0 1px 3px rgba(0,0,0,.12);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141917; --felt: #1b2320; --panel: #212a26; --ink: #e5ebe6; --muted: #9aa8a0;
    --line: #34413b; --accent: #4f9d74; --accent-ink: #0d1411; --warn: #e0954f; --danger: #e06b6b;
    --sel: #e9b84a; --card: #2a3430; --card-line: #4d5c55; --opp: #d58a8a; --me: #86b4e0;
    --shadow: 0 1px 3px rgba(0,0,0,.5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141917; --felt: #1b2320; --panel: #212a26; --ink: #e5ebe6; --muted: #9aa8a0;
  --line: #34413b; --accent: #4f9d74; --accent-ink: #0d1411; --warn: #e0954f; --danger: #e06b6b;
  --sel: #e9b84a; --card: #2a3430; --card-line: #4d5c55; --opp: #d58a8a; --me: #86b4e0;
  --shadow: 0 1px 3px rgba(0,0,0,.5); color-scheme: dark;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--ink);
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  display: flex; flex-direction: column;
}
button, input, select, textarea { font: inherit; color: inherit; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; }
.ok { color: var(--accent); font-weight: 600; }

.top {
  display: flex; align-items: center; gap: 10px; padding: 8px 16px;
  background: var(--panel); border-bottom: 1px solid var(--line);
}
.brand { font-weight: 700; color: var(--ink); text-decoration: none; }
.topinfo { color: var(--muted); }
.spacer { flex: 1; }

button { cursor: pointer; border-radius: 6px; border: 1px solid var(--line); background: var(--panel); padding: 6px 12px; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 600; }
button.ghost { background: transparent; }
button.danger { color: var(--danger); }
button.opt { margin: 3px 4px 3px 0; text-align: left; }
button.opt.on { outline: 2px solid var(--sel); background: color-mix(in srgb, var(--sel) 18%, var(--panel)); }

.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); }

/* lobby */
.lobby { flex: 1; display: flex; justify-content: center; align-items: flex-start; padding: 32px 16px; }
.lobby-card { width: 100%; max-width: 560px; padding: 24px; }
.lobby-card h1 { margin: 0 0 6px; font-size: 22px; }
form label { display: block; margin: 12px 0 0; }
form label > span { display: block; font-weight: 600; margin-bottom: 4px; }
input, select, textarea { width: 100%; padding: 7px 9px; border: 1px solid var(--line); border-radius: 6px; background: var(--bg); }
.row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
details { margin-top: 12px; }
summary { cursor: pointer; color: var(--accent); }
details textarea { margin-top: 8px; font-family: ui-monospace, monospace; font-size: 12px; }
.actions { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 14px; }

/* game layout */
.game { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 340px; min-height: 0; }
.board { display: flex; flex-direction: column; gap: 8px; padding: 10px 12px; overflow: auto; background: var(--felt); min-height: 0; }
.board > * { flex-shrink: 0; }
.side { display: flex; flex-direction: column; border-left: 1px solid var(--line); background: var(--panel); min-height: 0; }
.side-head { display: flex; align-items: center; gap: 14px; padding: 8px 10px 0; flex-wrap: wrap; }
.side-head .close { padding: 0 9px; font-size: 20px; line-height: 1.3; border: 0; }
.game.side-closed { grid-template-columns: minmax(0, 1fr); }
.game.side-closed .side { display: none; }
#btn-panel { position: relative; }
#btn-panel.on { background: color-mix(in srgb, var(--accent) 14%, transparent); border-color: var(--accent); }
#btn-panel .dot { position: absolute; top: -3px; right: -3px; width: 10px; height: 10px; border-radius: 50%; background: var(--warn); border: 2px solid var(--panel); }
.side .muted.small { padding: 0 10px; margin: 4px 0; }
.chk { display: inline-flex !important; align-items: center; gap: 6px; margin: 0 !important; font-weight: 400; cursor: pointer; }
.chk input { width: auto; }
.feed { flex: 1; overflow-y: auto; padding: 6px 10px; display: flex; flex-direction: column; gap: 3px; min-height: 120px; }
.entry { font-size: 13px; padding: 2px 0; }
.entry.log { color: var(--muted); }
.entry.turn { font-size: 12px; text-transform: uppercase; letter-spacing: .03em; color: var(--ink); font-weight: 600; border-top: 1px solid var(--line); padding-top: 6px; margin-top: 4px; }
.entry.system { color: var(--warn); }
.entry.you, .entry.agent { padding: 6px 9px; border-radius: 8px; max-width: 92%; }
.entry.you { align-self: flex-end; background: color-mix(in srgb, var(--me) 16%, var(--panel)); }
.entry.agent { align-self: flex-start; background: color-mix(in srgb, var(--opp) 14%, var(--panel)); }
.entry.agent_reason { color: var(--ink); border-left: 3px solid var(--opp); padding-left: 7px; font-size: 12px; }
.entry .who { font-weight: 600; margin-right: 4px; }
.chat { display: flex; gap: 6px; padding: 8px 10px; border-top: 1px solid var(--line); }

/* zones */
.zone { background: color-mix(in srgb, var(--panel) 55%, transparent); border-radius: 10px; padding: 8px 10px; border: 1px solid var(--line); }
.zone .head { display: flex; flex-wrap: wrap; align-items: baseline; gap: 6px 14px; margin-bottom: 6px; }
.zone .pname { font-weight: 700; font-size: 15px; }
.zone.opp .pname { color: var(--opp); }
.zone.me .pname { color: var(--me); }
.life { font-weight: 700; font-size: 18px; }
.life.low { color: var(--danger); }
.zone .stat { color: var(--muted); }
.zone .stat button { padding: 0 6px; font-size: 12px; }
.zone.active { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent) inset; }
.rowlabel { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 2px 0; }
.cards { display: flex; flex-wrap: wrap; gap: 6px; min-height: 30px; }
.pool { color: var(--accent); font-weight: 600; }
.grave-list { font-size: 12px; color: var(--muted); margin-top: 4px; }

.card {
  position: relative; width: 112px; min-height: 66px; padding: 5px 6px; border-radius: 7px;
  background: var(--card); border: 1px solid var(--card-line); box-shadow: var(--shadow);
  font-size: 12px; cursor: default; transition: transform .12s;
}
.card .name { font-weight: 600; line-height: 1.15; padding-right: 2px; }
.card .cost { color: var(--muted); font-size: 11px; }
.card .type { color: var(--muted); font-size: 10.5px; margin-top: 2px; }
.card .pt { position: absolute; right: 5px; bottom: 4px; font-weight: 700; font-size: 12px; background: var(--panel); border: 1px solid var(--card-line); border-radius: 4px; padding: 0 4px; }
.card .pt.hurt { color: var(--danger); }
.card .badges { display: flex; flex-wrap: wrap; gap: 3px; margin-top: 3px; padding-right: 30px; }
.badge { font-size: 10px; padding: 0 4px; border-radius: 3px; background: var(--line); color: var(--ink); }
.badge.atk { background: var(--danger); color: #fff; }
.badge.blk { background: var(--me); color: #fff; }
.card.land { min-height: 40px; width: 96px; }
.card.tapped { transform: rotate(6deg); opacity: .6; margin: 0 6px; }
.card.can { border-color: var(--accent); border-width: 2px; cursor: pointer; }
.card.can:hover { transform: translateY(-2px); }
.card.tapped.can:hover { transform: rotate(6deg) translateY(-2px); }
.card.on { outline: 3px solid var(--sel); outline-offset: 1px; }
.card.facedown { background: repeating-linear-gradient(45deg, var(--line), var(--line) 6px, var(--card) 6px, var(--card) 12px); }
.hand-label { width: 100%; }
.hand { display: flex; flex-wrap: wrap; gap: 6px; padding: 6px 2px; min-height: 76px; }
.hand .card { width: 120px; }

.stack { background: var(--panel); border: 2px dashed var(--warn); border-radius: 10px; padding: 8px 10px; }
.stack .item { padding: 4px 0; border-bottom: 1px solid var(--line); }
.stack .item:last-child { border-bottom: 0; }
.stack .item.can { cursor: pointer; }
.stack .item.on { outline: 2px solid var(--sel); }
.combat-note { font-size: 13px; color: var(--ink); background: color-mix(in srgb, var(--danger) 10%, var(--panel)); border-radius: 8px; padding: 6px 10px; }

.decision {
  position: sticky; bottom: 0; margin-top: auto; background: var(--panel); border: 2px solid var(--accent);
  border-radius: 10px; padding: 10px 12px; box-shadow: 0 -2px 10px rgba(0,0,0,.08);
}
.decision.waiting { border-color: var(--line); color: var(--muted); }
.decision .dhead { display: flex; align-items: flex-start; gap: 8px; margin-bottom: 6px; }
.decision .prompt { font-weight: 600; flex: 1; min-width: 0; }
.decision .fold { flex: none; padding: 2px 8px; font-size: 12px; }
.decision.folded { padding-top: 6px; padding-bottom: 6px; }
.decision.folded .dhead { margin-bottom: 0; align-items: center; }
.decision.folded .prompt { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.decision .src { color: var(--muted); font-size: 12px; margin-bottom: 6px; }
.decision .opts { display: flex; flex-wrap: wrap; align-items: center; }
.decision .group { margin: 4px 0; }
.decision .group-label { font-size: 12px; color: var(--muted); margin-right: 6px; }
.decision .foot { display: flex; align-items: center; gap: 10px; margin-top: 8px; flex-wrap: wrap; }
.decision .err { color: var(--danger); }
.decision input[type=number] { width: 90px; }
.spinner { display: inline-block; width: 10px; height: 10px; border-radius: 50%; background: var(--warn); margin-right: 6px; animation: pulse 1s infinite; }
@keyframes pulse { 50% { opacity: .25; } }

.modal { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: flex; align-items: center; justify-content: center; padding: 16px; z-index: 20; }
.modal-card { width: 100%; max-width: 560px; padding: 20px; max-height: 90vh; overflow: auto; }
.modal-card h2 { margin-top: 0; }
.stops-grid { display: grid; grid-template-columns: 1fr auto auto; gap: 4px 14px; align-items: center; margin: 10px 0 14px; }
.stops-grid .h { font-weight: 600; font-size: 12px; color: var(--muted); }
.stops-grid input { width: auto; }
.rating { margin: 10px 0; }
.stars { display: flex; gap: 4px; margin-top: 4px; }
.stars button { width: 38px; padding: 4px 0; }
.stars button.on { background: var(--sel); border-color: var(--sel); color: #1d2420; font-weight: 700; }

.menu { position: fixed; z-index: 30; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.2); padding: 4px; display: flex; flex-direction: column; min-width: 200px; max-width: 320px; }
.menu button { border: 0; text-align: left; padding: 7px 10px; }
.menu button:hover { background: var(--bg); }
.tip { position: fixed; z-index: 25; pointer-events: none; background: var(--panel); border: 1px solid var(--line); border-radius: 8px; box-shadow: 0 4px 16px rgba(0,0,0,.25); padding: 8px; max-width: 280px; font-size: 12px; }
.tip img { width: 244px; border-radius: 10px; display: block; margin-bottom: 6px; }
.tip .t-name { font-weight: 700; }
.tip .t-text { white-space: pre-wrap; margin-top: 4px; }

@media (max-width: 860px) {
  .game, .game.side-closed { grid-template-columns: minmax(0, 1fr); }
  .side {
    position: fixed; top: 0; right: 0; bottom: 0; z-index: 15; width: min(86vw, 340px);
    box-shadow: -4px 0 18px rgba(0,0,0,.25);
  }
  .game.side-closed .side { display: none; }
  .top { gap: 6px; } .top button { padding: 5px 8px; font-size: 13px; }
  .row2 { grid-template-columns: 1fr; }
  /* Cards on a phone: a fixed number per row that shares the screen width, so a row never runs
     past the edge whatever the screen size or the phone's text size. */
  .cards, .hand { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 5px; }
  .hand .hand-label { grid-column: 1 / -1; }
  .card, .card.land, .hand .card { width: auto; min-width: 0; min-height: 54px; padding: 4px 5px; font-size: 11px; overflow: hidden; overflow-wrap: break-word; hyphens: auto; }
  .card.land { min-height: 34px; }
  .card .name { font-size: 11px; padding-right: 0; }
  .card .cost { font-size: 10px; }
  .card .type { font-size: 9.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .card .pt { font-size: 10.5px; right: 3px; bottom: 3px; padding: 0 3px; }
  .card .badges { padding-right: 24px; }
  .badge { font-size: 9px; }
  .card.tapped { transform: rotate(4deg); margin: 0; }
  .card.tapped.can:hover { transform: rotate(4deg); }
  .card.can:hover { transform: none; }
  .board { padding: 8px; gap: 6px; }
  .zone { padding: 6px 8px; }
  .cards:empty { min-height: 0; }
  .decision { padding: 8px 10px; }
  .decision .src { margin-bottom: 4px; }
  .top { padding: 6px 10px; }
  .brand, .topinfo { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topinfo { font-size: 12px; line-height: 1.25; white-space: normal; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; }
  body:has(#game:not(.hidden)) .brand { display: none; }
}
