:root {
  --bg: #11171a; --panel: #1a2327; --panel2: #222e33; --line: #33454c;
  --text: #e8eef0; --muted: #8fa3aa; --accent: #f0b429; --good: #4ade80; --bad: #f87171;
  --fire: #e5533d; --water: #3b8fe0; --earth: #b07a45; --air: #5cc8c0; --forest: #4caf50;
  --dark: #8e5bd6; --light: #f2d24b; --machine: #8c9aa8; --gold: #e0a52b; --shell: #f08cb4; --none: #9aa5ab;
  --vh: 100vh;
  --side: 300px;
  /* --cw: width of one field slot. Everything on the board is sized from it, so
     the whole match fits the screen without scrolling. Desktop with side panel: */
  --cw: clamp(48px, min(calc((var(--vh) - 200px) / 3.95), calc((100vw - var(--side) - 52px) / 7.35)), 150px);
  --hw: calc(var(--cw) * .85); /* hand card width */
}
@supports (height: 100dvh) { :root { --vh: 100dvh; } }
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.4 -apple-system, "PingFang HK", "Noto Sans TC", "Microsoft JhengHei", system-ui, sans-serif; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: var(--text); background: var(--panel2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer; touch-action: manipulation; }
button:hover:not(:disabled) { border-color: var(--accent); }
button:disabled { opacity: .4; cursor: default; }
button.primary { background: var(--accent); color: #1a1300; border-color: var(--accent); font-weight: 600; }
button.on { border-color: var(--accent); color: var(--accent); }
button.mini { padding: 0 6px; font-size: 12px; }
select, input[type=text], input[type=number] { font: inherit; font-size: 16px; color: var(--text); background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
h1, h2, h3 { margin: 0; }
h2 { font-size: 18px; }

/* ---- layout ---------------------------------------------------------------- */
.game { display: grid; grid-template-columns: minmax(0, 1fr) var(--side); grid-template-rows: auto minmax(0, 1fr); height: var(--vh); }
.top { grid-column: 1 / -1; display: flex; align-items: center; gap: 14px; padding: 6px 14px; background: var(--panel); border-bottom: 1px solid var(--line); min-width: 0; }
.top h1 { font-size: 16px; white-space: nowrap; }
.scores { display: flex; gap: 14px; flex: 1; min-width: 0; }
.score { flex: 1; max-width: 300px; min-width: 0; }
.score .row { display: flex; justify-content: space-between; align-items: baseline; gap: 6px; }
.score .nm { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.score b { font-size: 18px; font-variant-numeric: tabular-nums; }
.score .pts { white-space: nowrap; }
.score.me .nm { color: var(--accent); }
.bar { height: 4px; background: var(--bg); border-radius: 2px; overflow: hidden; }
.bar i { display: block; height: 100%; background: var(--accent); }
.meta { color: var(--muted); white-space: nowrap; font-size: 12px; }
.tools { display: flex; gap: 8px; }
.mbtn { display: none; }
.main { overflow: auto; padding: 6px 14px 8px; display: flex; flex-direction: column; gap: 6px; align-items: center; min-width: 0; }
.side { border-left: 1px solid var(--line); background: var(--panel); display: flex; flex-direction: column; min-height: 0; }

/* ---- field: one row per player, columns lined up so 天 sits over 天 -------- */
.field { display: grid; gap: 4px; grid-template-columns: calc(var(--cw) * 1.3) repeat(6, var(--cw)); grid-template-areas: "info sum left ten right help sp"; align-items: stretch; }
.slot, .info { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 3px; display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.slot-h { display: flex; align-items: center; gap: 2px; font-size: 11px; color: var(--muted); height: 16px; white-space: nowrap; overflow: hidden; }
.slot-h b { margin-right: 2px; color: var(--text); }
.slot .chip { font-size: 10px; padding: 0 3px; line-height: 14px; }
.slot.blocked { border-color: var(--bad); }
.slot.blocked .slot-h::after { content: "封截"; margin-left: auto; color: var(--bad); font-weight: 600; }
.slot.legal { border-color: var(--good); box-shadow: 0 0 0 1px var(--good) inset; cursor: pointer; }
.slot.target { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent) inset; cursor: pointer; }
.slot.picked { background: #3a3110; }
.empty { aspect-ratio: 500 / 692; border: 1px dashed var(--line); border-radius: 6px; opacity: .5; }
.chip { font-size: 11px; padding: 0 5px; border-radius: 8px; border: 1px solid var(--c); color: var(--c); white-space: nowrap; }
.info { font-size: 12px; padding: 5px 7px; overflow: hidden; contain: size; } /* the slots set the row height, not this text */
.info .sub { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.info.now { border-color: var(--good); }
.info .nm { display: flex; gap: 4px; align-items: center; flex-wrap: wrap; }
.info .nm b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.info .sub { font-size: 12px; color: var(--muted); }
.info .total { font-size: clamp(20px, calc(var(--cw) * .24), 30px); font-weight: 700; line-height: 1.05; font-variant-numeric: tabular-nums; }
.info .combo { color: var(--accent); font-weight: 600; min-height: 1.2em; }
.info a { color: var(--muted); cursor: pointer; text-decoration: underline dotted; }
.mid { color: var(--accent); font-size: 12px; text-align: center; }

/* ---- cards: text sizes follow the card's own width (container units) -------- */
.card { --c: var(--none); container-type: inline-size; flex: none; width: 100%; aspect-ratio: 500 / 692; position: relative; overflow: hidden; border-radius: 6px; border: 2px solid var(--c); background: color-mix(in srgb, var(--c) 14%, var(--panel2)); padding: 3px 5px; display: flex; flex-direction: column; justify-content: space-between; }
.card .art { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; }
.c-over { position: absolute; inset: auto 3px 3px 3px; display: flex; justify-content: space-between; align-items: flex-end; pointer-events: none; }
.c-now { background: rgba(0, 0, 0, .82); color: #fff; font-weight: 700; font-size: max(11px, 15cqw); line-height: 1; padding: 2px 5px; border-radius: 5px; border: 1px solid var(--line); font-variant-numeric: tabular-nums; }
.c-now.chg { border-color: var(--accent); color: var(--accent); }
.card.zero .art { filter: grayscale(1) brightness(.45); }
.card.zero .c-now, .card.zero .c-now.chg { border-color: var(--bad); color: var(--bad); }
.card.down .art { filter: brightness(.5); }
.c-top { display: flex; gap: 3px; align-items: center; font-size: max(8px, 10cqw); color: var(--muted); flex-wrap: wrap; }
.c-top .attr { color: var(--c); font-weight: 600; }
.c-name { font-weight: 600; line-height: 1.2; font-size: max(9px, 12.5cqw); }
.c-bot { display: flex; align-items: baseline; gap: 4px; font-size: max(8px, 10cqw); color: var(--muted); }
.c-pow { font-size: max(11px, 17cqw); font-weight: 700; color: var(--text); font-variant-numeric: tabular-nums; line-height: 1; }
.card.zero .c-pow { color: var(--bad); text-decoration: line-through; }
.tag { font-size: max(8px, 9cqw); padding: 0 3px; border-radius: 3px; background: var(--bad); color: #fff; }
.tag.warn { background: var(--accent); color: #1a1300; }
.back { flex: none; width: 100%; aspect-ratio: 500 / 692; border-radius: 6px; border: 1px solid var(--line); background: repeating-linear-gradient(45deg, #26343a, #26343a 6px, #1e2a2f 6px, #1e2a2f 12px); display: grid; place-items: center; color: var(--muted); font-size: 12px; }
.card.down { border-style: dashed; }
.extra { font-size: 10px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.big { display: block; width: 100%; max-width: 240px; margin: 0 auto 8px; border-radius: 8px; }
.a-fire { --c: var(--fire); } .a-water { --c: var(--water); } .a-earth { --c: var(--earth); } .a-air { --c: var(--air); }
.a-forest { --c: var(--forest); } .a-dark { --c: var(--dark); } .a-light { --c: var(--light); } .a-machine { --c: var(--machine); }
.a-gold { --c: var(--gold); } .a-shell { --c: var(--shell); } .a-none { --c: var(--none); }

/* ---- actions + hand ---------------------------------------------------------- */
.actions { display: flex; align-items: center; gap: 8px; width: 100%; max-width: calc(var(--cw) * 7.3 + 24px); }
.prompt { flex: 1; min-width: 0; font-weight: 600; }
.abtns { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.selbar { display: none; }
.hand { display: flex; gap: 6px; justify-content: safe center; max-width: 100%; overflow-x: auto; overscroll-behavior-x: contain; padding: 2px 2px 4px; flex: none; }
.hand .card, .hand .back { flex: 0 0 var(--hw); width: var(--hw); cursor: pointer; }
.hand.wrap { flex-wrap: wrap; overflow: visible; --hw: 92px; }
.hand .card.sel { outline: 3px solid var(--good); outline-offset: 1px; transform: translateY(-4px); }
.hand .card.pair { outline: 3px solid var(--accent); outline-offset: 1px; }
.hand .card.off { opacity: .4; cursor: default; }
.oppHand { display: flex; gap: 3px; height: 14px; }
.oppHand i { width: 10px; height: 14px; border-radius: 2px; border: 1px solid var(--line); background: #26343a; }

/* ---- side panel (desktop) and sheets (smaller screens) ---------------------- */
.detail { padding: 10px 12px; border-bottom: 1px solid var(--line); min-height: 190px; max-height: 68%; overflow: auto; }
.detail h3 { font-size: 16px; }
.detail .k { color: var(--muted); font-size: 12px; }
.detail p { margin: 6px 0 0; white-space: pre-wrap; }
.log { flex: 1; overflow: auto; padding: 8px 12px; font-size: 12.5px; }
.log div { padding: 2px 0; }
.log .head { color: var(--accent); margin-top: 6px; font-weight: 600; }
.log .dim { color: var(--muted); }
.log .warn { color: #ffb86b; }
.sheet-wrap { position: fixed; inset: 0; background: rgba(8, 12, 14, .7); z-index: 15; display: flex; align-items: flex-end; justify-content: center; }
.sheet { background: var(--panel); border: 1px solid var(--line); border-bottom: 0; border-radius: 14px 14px 0 0; width: min(560px, 100%); max-height: calc(var(--vh) * .82); display: flex; flex-direction: column; gap: 10px; padding: 14px 14px calc(14px + env(safe-area-inset-bottom)); }
.sheet .detail { border: 0; padding: 0; max-height: none; min-height: 0; overflow: auto; }
.sheet .log { padding: 0; max-height: calc(var(--vh) * .6); }
.sheet .btns { display: flex; justify-content: flex-end; }
.menu { display: flex; flex-direction: column; gap: 8px; }
.menu button { padding: 10px 12px; text-align: left; }

/* ---- overlays ------------------------------------------------------------------ */
.overlay { position: fixed; inset: 0; background: rgba(8, 12, 14, .82); display: grid; place-items: center; z-index: 10; padding: 16px; overflow: auto; }
.overlay.solid { background: var(--bg); }
.modal { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: 20px; width: min(560px, 100%); max-height: calc(var(--vh) - 32px); overflow: auto; display: flex; flex-direction: column; gap: 12px; }
.modal.center { text-align: center; }
.modal .btns { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }
.modal.center .btns { justify-content: center; }
.modal .btns.left { justify-content: flex-start; }
.modal table { border-collapse: collapse; width: 100%; }
.modal td, .modal th { padding: 4px 8px; text-align: left; border-bottom: 1px solid var(--line); }
.ord { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--line); border-left: 5px solid var(--c); border-radius: 6px; }
.ord .n { width: 18px; color: var(--muted); }
.ord .g { flex: 1; min-width: 0; }
.ord .mini { min-width: 34px; min-height: 30px; }
.list { display: flex; flex-direction: column; gap: 6px; }
.pick { cursor: pointer; }
.pick:hover { border-color: var(--accent); }
.toast { position: fixed; bottom: calc(18px + env(safe-area-inset-bottom)); left: 50%; transform: translateX(-50%); background: var(--bad); color: #fff; padding: 8px 14px; border-radius: 8px; z-index: 20; max-width: calc(100vw - 32px); }
.setup { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.setup label { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
.seg { display: grid; grid-template-columns: 1fr 1fr; gap: 0; }
.seg button { border-radius: 0; padding: 9px; }
.seg button:first-child { border-radius: 8px 0 0 8px; }
.seg button:last-child { border-radius: 0 8px 8px 0; border-left: 0; }
.seg button.on { background: color-mix(in srgb, var(--accent) 18%, var(--panel2)); }
.checks { display: flex; flex-direction: column; gap: 2px; }
.checks label { display: flex; gap: 8px; align-items: flex-start; color: var(--text); padding: 3px 0; }
.checks input { margin-top: 3px; }
.seed { display: flex; flex-direction: column; gap: 4px; }
.seed input { width: 160px; }
.note { color: var(--muted); font-size: 12px; }
.version { position: fixed; right: calc(10px + env(safe-area-inset-right)); bottom: calc(8px + env(safe-area-inset-bottom)); color: var(--muted); font-size: 11px; pointer-events: none; }
.crash-detail { font-family: ui-monospace, monospace; word-break: break-word; }

/* ---- tablets and small laptops: no side panel; detail + log open as sheets ---- */
@media (max-width: 1099px) {
  :root { --side: 0px; --cw: clamp(48px, min(calc((var(--vh) - 240px) / 3.95), calc((100vw - 52px) / 7.35)), 150px); }
  .game { grid-template-columns: minmax(0, 1fr); }
  .side { display: none; }
  .tools { display: none; }
  .mbtn { display: inline-block; }
  .selbar { display: flex; align-items: center; gap: 8px; width: 100%; max-width: calc(var(--cw) * 7.3 + 24px); min-height: 34px; font-size: 12px; }
  .selbar .txt { flex: 1; min-width: 0; display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
  .selbar.empty-bar { visibility: hidden; }
}

/* ---- phones and portrait tablets: each field becomes 2 rows of 4 ------------ */
@media (max-width: 760px), (max-width: 1099px) and (orientation: portrait) {
  :root { --cw: clamp(48px, min(calc((var(--vh) - 280px) / 6.62), calc((100vw - 24px) / 4)), 140px); --hw: max(58px, calc(var(--cw) * .78)); }
  .oppHand { display: none; }
  body { font-size: 13px; }
  .top { gap: 8px; padding: 5px 8px; padding-top: calc(5px + env(safe-area-inset-top)); }
  .top h1, .meta.round { display: none; }
  .scores { gap: 10px; }
  .score b { font-size: 16px; }
  .score .nm .meta { display: none; }
  .mbtn { padding: 6px 10px; }
  .main { padding: 6px 6px calc(6px + env(safe-area-inset-bottom)); gap: 5px; }
  .field { grid-template-columns: repeat(4, var(--cw)); grid-template-areas: "sum left ten right" "info info help sp"; }
  .slot-h { font-size: 10px; height: 14px; }
  .slot .chip { font-size: 9px; padding: 0 2px; line-height: 12px; }
  .info { padding: 4px 6px; font-size: 11px; }
  .info .sub { font-size: 10.5px; }
  .wide-only { display: none; }
  .actions { flex-direction: column; align-items: stretch; gap: 4px; }
  .prompt { font-size: 12.5px; line-height: 1.3; }
  .abtns { display: grid; grid-template-columns: repeat(auto-fit, minmax(0, 1fr)); gap: 5px; }
  .abtns button { padding: 8px 4px; min-height: 38px; }
  .hand { justify-content: flex-start; }
  .hand .card.sel { transform: none; }
  .modal { padding: 16px; }
  .setup { grid-template-columns: 1fr; }
}

/* ---- phones, landscape: the two fields side by side -------------------------- */
@media (max-width: 1099px) and (max-height: 560px) and (orientation: landscape) {
  :root { --cw: clamp(44px, min(calc((var(--vh) - 165px) / 3.85), calc((100vw - 56px) / 8)), 110px); --hw: max(56px, calc(var(--cw) * .9)); }
  .top { padding: 3px 10px; }
  .top h1 { display: none; }
  .main { display: grid; grid-template-columns: auto auto; justify-content: center; align-content: start; column-gap: 12px; row-gap: 4px; padding: 4px 10px; }
  .oppHand, .mid, .actions, .hand { grid-column: 1 / -1; }
  .oppHand { display: none; }
  .field { grid-template-columns: repeat(4, var(--cw)); grid-template-areas: "sum left ten right" "info info help sp"; }
  .field.theirs { grid-column: 1; grid-row: 1; }
  .field.mine { grid-column: 2; grid-row: 1; }
  .wide-only { display: none; }
  .slot-h { font-size: 10px; height: 14px; }
  .info, .info .sub { font-size: 11px; }
  .selbar { display: none; }
  .actions { max-width: none; }
  .abtns button { padding: 5px 10px; }
  .hand { justify-content: flex-start; }
}
