/*
 * game/shared/matrix-board.css — The Tree's board, drawn as a table, not a repeated diagram.
 * Game-owned (never explainers/belief-tree/*, never site/theme.css, which is light-only). Defines
 * its own --mb-* tokens for dark/light so the board reads on either theme without touching the
 * global (light-only) tokens, the same technique explainers/belief-tree/tree-render.js uses for its
 * own --bt-* tokens. Fits inside .ps-tree-box (game/shared/play-shell.css): no fixed pixel sizes,
 * flex + CSS grid only, so it never needs its own resize-fit JS. Container queries (not viewport
 * width) pick the left-to-right vs. stacked layout, so it reflows with the STAGE's own box, exactly
 * like the game shell already expects.
 */
.mb-board {
  --mb-node-dark:#121a29; --mb-line-dark:#2e3a55; --mb-dim-dark:#a7b0bf; --mb-ink-dark:#f3ede2;
  --mb-chip-dark:#1b2438; --mb-chipline-dark:#3d4b6b; --mb-yes-dark:#3f8f68; --mb-no-dark:#d4553a;
  --mb-mid-dark:#8a8273; --mb-focus-dark:#cfe2ff;
  --mb-tint-dark:color-mix(in srgb, #ffffff 5%, var(--mb-node-dark));
  --mb-node:#f6f3ec; --mb-line:#cbc3b3; --mb-dim:#4f5663; --mb-ink:#1d1b18;
  --mb-chip:#ffffff; --mb-chipline:#c4bba9; --mb-yes:#177d56; --mb-no:#b3492f;
  --mb-mid:#7a7264; --mb-focus:#1f5fbf;
  --mb-tint:color-mix(in srgb, var(--mb-yes) 6%, var(--mb-node));

  /* the board paints its own opaque background: theme.css (the site's global tokens) is
     light-only, so a header cell here can never rely on the PAGE behind it to be dark too. */
  background: var(--mb-node);
  border-radius: 12px;
  /* the dot-detail popover below is positioned absolute against THIS box, and clamped (by the
     caller, tree-board.js) to stay inside it — position:relative makes that containment real. */
  position: relative;

  container-type: inline-size;
  container-name: mb;
  flex: 1 1 auto; min-height: 0; min-width: 0;
  display: flex; flex-direction: column; gap: 6px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color: var(--mb-ink);
}
@media (prefers-color-scheme: dark) {
  .mb-board[data-mb-theme="auto"] {
    --mb-node: var(--mb-node-dark); --mb-line: var(--mb-line-dark); --mb-dim: var(--mb-dim-dark);
    --mb-ink: var(--mb-ink-dark); --mb-chip: var(--mb-chip-dark); --mb-chipline: var(--mb-chipline-dark);
    --mb-yes: var(--mb-yes-dark); --mb-no: var(--mb-no-dark); --mb-mid: var(--mb-mid-dark);
    --mb-focus: var(--mb-focus-dark); --mb-tint: var(--mb-tint-dark);
  }
}
.mb-board[data-mb-theme="dark"] {
  --mb-node: var(--mb-node-dark); --mb-line: var(--mb-line-dark); --mb-dim: var(--mb-dim-dark);
  --mb-ink: var(--mb-ink-dark); --mb-chip: var(--mb-chip-dark); --mb-chipline: var(--mb-chipline-dark);
  --mb-yes: var(--mb-yes-dark); --mb-no: var(--mb-no-dark); --mb-mid: var(--mb-mid-dark);
  --mb-focus: var(--mb-focus-dark); --mb-tint: var(--mb-tint-dark);
}

.mb-board * { box-sizing: border-box; }
.mb-board svg { display: block; }

/* ── the gate: one header bar, its own yes/depends/no state ────────────────────────────────── */
.mb-gate {
  flex: 0 0 auto; display: flex; align-items: center; gap: 10px;
  border: 1px solid var(--mb-line); border-radius: 10px; background: var(--mb-node);
  padding: 7px 12px; min-height: 44px;
}
.mb-gate .mb-gate-q { font-weight: 800; font-size: 16px; line-height: 1.25; flex: 0 0 auto; }
/* Wraps rather than truncates: the gate row is flex:0 0 auto (not a fixed height), so at a narrow
   width it just grows a line taller instead of ellipsis-clipping the sentence — no header text on
   this board is ever cut off, only wrapped or (in is-compact, below) dropped in full. */
.mb-gate .mb-gate-sub { font-size: 13px; color: var(--mb-dim); flex: 1 1 auto; min-width: 0; }
.mb-gate .mb-answer { flex: 0 0 auto; }
/* On a genuinely short box (360×640 and similar) the gate's full sub-text and the corner's question
   reminder are the two things this board can most afford to drop — the Rules drawer still carries
   them in full, and dropping them here is what buys the grid enough height to show all 4 cells
   without any of it overlapping the row above. tree-board.js sets this class from the STAGE's own
   measured height (a plain class, not a block-size container query: this container only declares
   inline-size containment, which is all the width-based reflow above needs, and is the safer,
   better-supported choice for a "does everything fit" check like this one). */
.mb-board.is-compact .mb-gate { padding: 5px 10px; min-height: 36px; }
.mb-board.is-compact .mb-gate .mb-gate-sub { display: none; }
.mb-board.is-compact .mb-group-tag { display: none; }

/* ── answer marks: check / cross / half-filled — never colour alone, the word is always there ── */
.mb-answer { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; font-weight: 700;
  white-space: nowrap; }
.mb-answer svg { width: 17px; height: 17px; flex: 0 0 auto; }
.mb-answer.is-yes { color: var(--mb-yes); }
.mb-answer.is-no { color: var(--mb-no); }
.mb-answer.is-mid { color: var(--mb-mid); font-weight: 600; }

/* ── body: grid (left/main) + the outside lane (slim side, or a bottom strip on phone) ───────── */
.mb-body { flex: 1 1 auto; min-height: 0; display: flex; gap: 8px; }
@container mb (max-width: 640px) {
  .mb-body { flex-direction: column; }
}

.mb-gridwrap { flex: 1 1 auto; min-height: 0; min-width: 0; display: flex; }
/* Each question is written ONCE, as its own group-header row/column (mb-align-head above the two
   column headers, mb-contain-head — rotated — to the left of the two row headers), named by CSS
   grid-template-areas rather than row/column numbers so the markup order doesn't have to match the
   visual order. The corner is left empty (the gate's own state is in the gate bar above the whole
   grid, not repeated here). Column 2 (the row-header column) is wide enough for "yes"/"no" WITH
   their check/cross icon at every breakpoint down to 360px — the clipping the short labels replace
   (full words like "Containment: yes") is exactly what made that column need to be so cramped. */
.mb-grid {
  flex: 1 1 auto; display: grid; min-height: 0; min-width: 0; overflow: hidden;
  grid-template-columns: minmax(20px,26px) minmax(58px,78px) 1fr 1fr;
  /* minmax(0,1fr), never a bare 1fr: a bare fr track's automatic minimum is its content's
     max-content size, so on a genuinely short box (a phone in landscape, or 800×600) the ROWS would
     refuse to shrink and overflow past the grid's own box even though the grid container itself can
     (min-height:0, above) — minmax(0, 1fr) lets a cramped row actually shrink, and .mb-cell's own
     overflow:hidden clips its content cleanly instead of spilling into the staging strip below it. */
  grid-template-rows: auto auto minmax(0,1fr) minmax(0,1fr);
  grid-template-areas:
    "corner  corner  align align"
    "contain .       cyes  cno"
    "contain ryes    p11   p21"
    "contain rno     p12   p22";
  gap: 3px;
}
@container mb (max-width: 420px) {
  .mb-grid { grid-template-columns: minmax(16px,20px) minmax(46px,60px) 1fr 1fr; }
}

.mb-corner { grid-area: corner; }

.mb-group-head {
  display: flex; align-items: center; justify-content: center; text-align: center;
  font-size: 13px; font-weight: 700; color: var(--mb-dim); line-height: 1.25; gap: 4px;
  padding: 2px 4px; overflow: hidden;
}
.mb-align-head { grid-area: align; }
.mb-contain-head {
  grid-area: contain; writing-mode: vertical-rl; transform: rotate(180deg);
  padding: 4px 2px; white-space: nowrap;
}
.mb-contain-head > span { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.mb-group-tag { font-weight: 500; color: var(--mb-mid); font-size: 12px; }

.mb-colhead, .mb-rowhead {
  display: flex; flex-direction: row; align-items: center; justify-content: center;
  gap: 5px; padding: 5px 4px; border-radius: 8px; background: var(--mb-tint);
  border: 1px solid transparent; transition: background .5s ease, border-color .5s ease;
  min-width: 0; overflow: hidden;
}
.mb-colhead-yes { grid-area: cyes; }
.mb-colhead-no { grid-area: cno; }
.mb-rowhead-yes { grid-area: ryes; }
.mb-rowhead-no { grid-area: rno; }
.mb-colhead { text-align: center; }
.mb-rowhead { text-align: center; }
.mb-colhead .mb-htitle, .mb-rowhead .mb-htitle {
  font-size: 16px; font-weight: 800; line-height: 1.15; white-space: nowrap;
}
.mb-colhead svg, .mb-rowhead svg, .mb-corner svg { width: 18px; height: 18px; flex: 0 0 auto; }
.mb-colhead.is-flash, .mb-rowhead.is-flash {
  background: var(--mb-focus); border-color: var(--mb-focus);
  animation: mb-flash 1.1s ease-out 1;
}
@keyframes mb-flash { 0% { box-shadow: 0 0 0 3px var(--mb-focus); } 100% { box-shadow: 0 0 0 0 transparent; } }
body.ps-reduced-motion .mb-colhead.is-flash, body.ps-reduced-motion .mb-rowhead.is-flash { animation: none; }

.mb-cell {
  border: 1px solid var(--mb-line); border-radius: 10px; background: var(--mb-node);
  padding: 6px 8px; min-height: 0; overflow: hidden;
  display: flex; flex-direction: column; gap: 4px;
}
.mb-cell .mb-cell-title { font-size: 18px; font-weight: 800; line-height: 1.2; }
.mb-cell .mb-cell-chips { flex: 1 1 auto; min-height: 0; overflow: auto; display: flex; flex-wrap: wrap;
  align-content: flex-start; gap: 5px; padding-top: 2px; }
/* soft, neutral per-leaf tints — never a good/bad reading, just four distinct washes */
.mb-cell[data-leaf="proceed"] { background: color-mix(in srgb, var(--mb-yes) 7%, var(--mb-node)); }
.mb-cell[data-leaf="regulate"] { background: color-mix(in srgb, var(--mb-focus) 7%, var(--mb-node)); }
.mb-cell[data-leaf="contain"] { background: color-mix(in srgb, var(--mb-mid) 10%, var(--mb-node)); }
.mb-cell[data-leaf="shutdown"] { background: color-mix(in srgb, var(--mb-no) 7%, var(--mb-node)); }

/* the staging strip: characters still crossing gate/alignment/containment sit here, not yet in a cell */
.mb-staging { flex: 0 0 auto; display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
  padding: 4px 2px; }
.mb-staging .mb-staging-label { font-size: 12px; color: var(--mb-dim); margin-right: 2px; }

/* ── person chips: round avatars with initials, no photos ─────────────────────────────────────── */
.mb-chip {
  width: 34px; height: 34px; min-width: 44px; min-height: 44px; padding: 0;
  border-radius: 50%; border: 2px solid var(--mb-chipline); background: var(--mb-chip);
  color: var(--mb-ink); display: inline-flex; align-items: center; justify-content: center;
  font-size: 12.5px; font-weight: 800; cursor: pointer; position: relative;
  transition: left .5s ease, top .5s ease, transform .2s ease;
}
.mb-chip.is-active { border-color: var(--mb-focus); box-shadow: 0 0 0 2px var(--mb-focus); }
.mb-chip:focus-visible { outline: 2px solid var(--mb-focus); outline-offset: 2px; }
body.ps-reduced-motion .mb-chip { transition: none; }

/* ── trails: a faint dotted history with dated dots, an arrow to "now" ─────────────────────────── */
.mb-trails { flex: 0 0 auto; display: flex; flex-direction: column; gap: 4px; padding: 2px 2px 0; }
.mb-trail { display: flex; align-items: center; gap: 4px; font-size: 12px; color: var(--mb-dim); flex-wrap: wrap; }
.mb-trail .mb-trail-who { font-weight: 700; color: var(--mb-ink); }
.mb-dot {
  width: 14px; height: 14px; min-width: 14px; min-height: 14px; border-radius: 50%;
  border: 1.5px dashed var(--mb-dim); background: transparent; cursor: pointer; padding: 0;
  display: inline-flex; align-items: center; justify-content: center;
}
.mb-dot:focus-visible { outline: 2px solid var(--mb-focus); outline-offset: 2px; }
.mb-dot.is-game { border-style: solid; border-color: var(--mb-focus); background: var(--mb-focus); }
.mb-trail-arrow { color: var(--mb-dim); }
.mb-trail-toggle {
  min-height: 44px; min-width: 44px; border-radius: 999px; border: 1px solid var(--mb-line);
  background: var(--mb-chip); color: var(--mb-ink); display: inline-flex; align-items: center;
  gap: 6px; padding: 0 12px; font: inherit; font-size: 12.5px; font-weight: 600; cursor: pointer;
}
.mb-trail-toggle svg { width: 17px; height: 17px; }
.mb-trail-toggle[aria-pressed="true"] { border-color: var(--mb-focus); color: var(--mb-focus); }

.mb-dot-detail {
  position: absolute; z-index: 20; max-width: 280px; background: var(--mb-node);
  border: 1px solid var(--mb-line); border-radius: 10px; padding: 10px 12px; box-shadow: 0 14px 30px -18px rgba(0,0,0,.5);
  font-size: 13px; line-height: 1.45;
}
.mb-dot-detail .mb-dd-date { font-size: 11px; color: var(--mb-dim); text-transform: uppercase; letter-spacing: .04em; }
.mb-dot-detail blockquote { margin: 4px 0; font-style: italic; }
.mb-dot-detail a { color: var(--mb-focus); }
.mb-dot-detail .mb-dd-close { position: absolute; top: 6px; right: 6px; width: 22px; height: 22px; border-radius: 50%;
  border: none; background: transparent; color: var(--mb-dim); cursor: pointer; display: flex; align-items: center; justify-content: center; }

/* ── outside the tree: a slim, static, non-interactive lane ────────────────────────────────────── */
.mb-outside {
  flex: 0 0 168px; min-height: 0; border: 1px solid var(--mb-line); border-radius: 10px;
  background: var(--mb-node); padding: 8px 10px; overflow: auto; display: flex; flex-direction: column; gap: 6px;
}
@container mb (max-width: 640px) { .mb-outside { flex: 0 0 auto; max-height: 88px; flex-direction: row; flex-wrap: wrap; } }
.mb-outside .mb-outside-title { font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; color: var(--mb-dim); }
.mb-outside .mb-outside-person { font-size: 13px; }
.mb-outside .mb-outside-person b { display: block; }
.mb-outside .mb-outside-person span { color: var(--mb-dim); font-size: 12px; }

/* ── the race: a slim band under the grid ──────────────────────────────────────────────────────── */
.mb-race { flex: 0 0 auto; display: flex; gap: 3px; }
.mb-race .pip { flex: 1 1 0; height: 6px; min-width: 4px; border-radius: 3px; background: var(--mb-line); border: none; }
.mb-race .pip.is-filled { background: var(--mb-no); }

/* ── "cast a card": draw + reveal + target picker ──────────────────────────────────────────────── */
.mb-cast-reveal { display: flex; flex-direction: column; align-items: center; gap: 10px; padding: 12px 0; }
.mb-cast-card {
  width: 220px; min-height: 120px; border-radius: 12px; border: 1px solid var(--mb-line);
  background: var(--mb-node); padding: 14px; font-size: 14px; line-height: 1.4;
  transform: rotateY(90deg); animation: mb-flip .5s ease forwards;
}
@keyframes mb-flip { to { transform: rotateY(0deg); } }
body.ps-reduced-motion .mb-cast-card { animation: none; transform: none; }
.mb-cast-tag { font-size: 11px; font-weight: 800; text-transform: uppercase; letter-spacing: .04em; color: var(--mb-focus); }

.mb-group-targets, .mb-cast-targets { display: flex; gap: 6px; flex-wrap: wrap; margin: 8px 0; }
.mb-group-targets button, .mb-cast-targets button {
  min-height: 44px; border-radius: 9px; border: 1px solid var(--mb-line); background: var(--mb-chip);
  color: var(--mb-ink); font: inherit; font-size: 13px; padding: 8px 12px; cursor: pointer;
}
.mb-group-targets button.ok, .mb-cast-targets button.ok { border-color: var(--mb-yes); }
.mb-group-targets button.no, .mb-cast-targets button.no { opacity: .55; }

.mb-cast-log li { font-size: 13px; margin: 4px 0; }
.mb-cast-log .moved { color: var(--mb-yes); }
.mb-cast-log .stayed { color: var(--mb-dim); }

@media (max-width: 480px) {
  .mb-gate .mb-gate-q { font-size: 16px; }
  .mb-cell .mb-cell-title { font-size: 18px; }
}
