:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --ink: #1a1a1a;
  --muted: #6b6b6b;
  --faint: #9a9a9a;
  --line: #ececec;
  --line-strong: #d9d9d9;
  --chip: #f5f5f5;
  --accent: #1f4e9c;
  --seen: #1f4e9c;
  --fresh: #1a9641;
  --lattice: #e07b00;
  --serif: Charter, "Iowan Old Style", "Source Serif 4", Georgia, "Times New Roman", serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font: 14px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
}

/* ============================ top bar ============================ */
/* brand left, tabs centered via equal 1fr gutters */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center;
  gap: 8px 24px;
  padding: 9px 28px;
  min-height: 52px;
  background: rgba(255,255,255,0.88);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: baseline; gap: 12px; min-width: 0; }
.brand-name { font-family: var(--serif); font-size: 16px; font-weight: 650;
  letter-spacing: -0.005em; }

/* main tabs */
.appx-tabs { display: flex; gap: 4px; flex-wrap: wrap; align-items: center;
  justify-content: center; grid-column: 2; }
.appx-tabs[hidden] { display: none; }
.appx-tabs button {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 12.5px; font-weight: 550;
  padding: 4px 14px; cursor: pointer;
  border-bottom: 2px solid transparent; transition: color .12s;
}
.appx-tabs button:hover { color: var(--ink); }
.appx-tabs button.on { color: var(--ink); border-bottom-color: var(--ink); font-weight: 650; }

/* ============================ toolbar (dashboard controls) ============================ */
/* one selector per line */
.toolbar {
  display: flex; flex-direction: column; align-items: flex-start; gap: 5px;
  padding: 12px 24px;
  border-bottom: 1px solid var(--line);
}
.ctrl { display: flex; align-items: center; gap: 12px; }
.ctrl > label {
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.07em;
  color: var(--faint); font-weight: 600; min-width: 58px;
}

.seg { display: inline-flex; gap: 1px; flex-wrap: wrap; }
.seg button {
  border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 12.5px; padding: 4px 9px; border-radius: 6px;
  cursor: pointer; white-space: nowrap; transition: background .12s, color .12s;
}
.seg button:hover { color: var(--ink); background: var(--chip); }
.seg button.on { background: var(--chip); color: var(--ink); font-weight: 600; }

/* ============================ views ============================ */
main { padding: 28px 28px 88px; }
#figures-view[hidden], #dashboard-view[hidden] { display: none; }

.panel { background: transparent; padding: 0; }
.desc { color: var(--muted); font-size: 12.5px; line-height: 1.6; margin: 0 0 16px; max-width: 78ch;
  border-left: 2px solid var(--line); padding: 1px 0 1px 12px; }
.desc b { color: var(--ink); font-weight: 600; }
/* full-width figure-footer notes */
.desc.desc-wide { max-width: none; }
.desc i { font-style: italic; }
.hint { color: var(--faint); font-size: 12px; margin: 14px 2px 0; }

.legend { display: flex; gap: 16px; flex-wrap: wrap; align-items: center; font-size: 12px; color: var(--muted); }
.legend .item { display: inline-flex; align-items: center; gap: 6px; }
.legend .swatch { width: 18px; height: 3px; border-radius: 2px; }
.legend .swatch.dash { background: repeating-linear-gradient(90deg, currentColor 0 4px, transparent 4px 7px); height: 2px; }
.legend .cbar { display: inline-flex; align-items: center; gap: 4px; }
.legend .cbar-cap { color: var(--muted); margin-right: 2px; }
.legend .cbar-lab { color: var(--faint); font-size: 10px; }
.legend .grad { width: 40px; height: 10px; border: 1px solid var(--line-strong); }
.legend .grad-lo { border-radius: 3px 0 0 3px; border-right: none; background: linear-gradient(90deg,#053061,#2166ac,#4393c3,#92c5de,#f7f7f7); }
.legend .grad-hi { border-radius: 0 3px 3px 0; border-left: none; background: linear-gradient(90deg,#f7f7f7,#f4a582,#d6604d,#b2182b,#67001f); }

/* ============================ grid ============================ */
.grid-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; background: var(--panel); }
/* shrink to content and center; margins collapse to 0 on overflow */
.grid { display: grid; gap: 1px; background: var(--line); width: max-content; margin: 0 auto; }
.grid .corner, .grid .colhead, .grid .rowhead, .grid .cell { background: var(--panel); }

.colhead {
  position: sticky; top: 0; z-index: 5;
  padding: 7px 4px; text-align: center; font-size: 11px; font-weight: 600;
  border-bottom: 1px solid var(--line);
}
.colhead .grp { display: block; font-size: 9px; font-weight: 600; text-transform: uppercase; letter-spacing: .05em; }
.colhead .jmat { display: block; width: 52px; height: 52px; margin: 4px auto 0; image-rendering: pixelated; border: 1px solid var(--line); }
.colhead.seen .grp { color: var(--seen); }
.colhead.fresh .grp { color: var(--fresh); }
.colhead.lattice .grp { color: var(--lattice); }

.corner { position: sticky; left: 0; top: 0; z-index: 6; }
.rowhead {
  position: sticky; left: 0; z-index: 4;
  display: flex; align-items: center; gap: 6px; justify-content: flex-end;
  padding: 0 10px; font-size: 11px; text-align: right; white-space: nowrap;
  border-right: 1px solid var(--line);
}
.rowhead.clickable { cursor: pointer; }
.rowhead.clickable:hover { color: var(--accent); }
.rowhead.clickable:hover > span:first-child { text-decoration: underline; text-underline-offset: 2px; }
.badge { font-size: 8.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em;
  padding: 1px 5px; border-radius: 4px; color: #fff; }
.badge.train { background: #9a9a9a; }
.badge.test { background: #1f4e9c; }
.badge.all { background: #5b8a5b; }

.cell { padding: 0; cursor: pointer; position: relative; line-height: 0; }
.cell canvas { display: block; width: 100%; height: 100%; }
.cell:hover { outline: 2px solid var(--accent); outline-offset: -2px; z-index: 3; }
.cell.empty { background: #f7f7f7; cursor: default; }

/* ============================ detail / question modals ============================ */
.detail {
  position: fixed; inset: 0; z-index: 40;
  display: flex; align-items: center; justify-content: center;
  background: rgba(20,20,20,0.30); padding: 24px;
}
.detail.hidden { display: none; }
.detail-card {
  background: var(--panel); border-radius: 14px; border: 1px solid var(--line-strong);
  box-shadow: 0 20px 60px rgba(0,0,0,.18);
  width: min(880px, 96vw); max-height: 92vh; overflow: auto; padding: 18px 22px 22px;
}
.detail-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 6px; }
.detail-head h3 { margin: 0; font-size: 15px; font-weight: 600; }
#detail-close, #qmodal-close { border: 0; background: var(--chip); width: 30px; height: 30px; border-radius: 8px; font-size: 18px; cursor: pointer; color: var(--muted); }
#detail-close:hover, #qmodal-close:hover { color: var(--ink); }
.qcard { max-width: 560px; }
.qtext { margin: 8px 0 2px; font-size: 15px; line-height: 1.5; color: var(--ink); white-space: pre-wrap; }
.poles { margin-top: 14px; border-top: 1px solid var(--line); padding-top: 12px; display: flex; flex-direction: column; gap: 8px; }
.pole { display: flex; align-items: center; gap: 8px; font-size: 13px; }
.pole-sw { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--line-strong); flex: none; }
.pole-val { font-variant-numeric: tabular-nums; color: var(--muted); width: 20px; font-size: 12px; }
.pole-key { color: var(--ink); }
.pole.correct .pole-key { font-weight: 600; }
.pole-flag { color: #2e7d32; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .03em; margin-left: 4px; }
.detail-body { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; margin: 14px 0; }
.rep { border: 1px solid var(--line); border-radius: 10px; padding: 8px; }
.rep h4 { margin: 0 0 6px; font-size: 11px; color: var(--muted); font-weight: 600; text-align: center; }
.rep canvas { display: block; width: 100%; height: auto; }
.rep.empty { display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 11px; min-height: 120px; }
.rep.wide { grid-column: 1 / -1; display: flex; justify-content: center; }
.rep.wide canvas { width: auto; max-width: 100%; }

/* ============================ figures pane ============================ */
.figpane { max-width: 1000px; margin: 0 auto; }

/* selector-row tab bars */
.fig-tabs { display: flex; gap: 4px; flex-wrap: wrap; margin: 0 0 18px; border-bottom: 1px solid var(--line); }
.fig-tabs button { border: 0; background: transparent; color: var(--muted);
  font: inherit; font-size: 12.5px; padding: 7px 12px; cursor: pointer;
  border-bottom: 2px solid transparent; margin-bottom: -1px; transition: color .12s; }
.fig-tabs button:hover { color: var(--ink); }
.fig-tabs button.on { color: var(--ink); border-bottom-color: var(--ink); font-weight: 600; }

.fig-content .fig-tabs { border-bottom: 0; margin-bottom: 6px; }
.fig-content .fig-tabs button { font-size: 12px; padding: 5px 10px; border-radius: 6px; border-bottom: 0; }
.fig-content .fig-tabs button.on { background: var(--chip); color: var(--ink); }
.fig-title { font-family: var(--serif); font-size: 16.5px; font-weight: 650;
  letter-spacing: -0.005em; margin: 32px 0 12px; }
.fig-content > .fig-title:first-of-type { margin-top: 6px; }
.cat-table { border-collapse: collapse; font-size: 12px; margin: 4px 0 8px; }
.cat-table th, .cat-table td { border: 1px solid var(--line); padding: 4px 10px; text-align: right; font-variant-numeric: tabular-nums; }
.cat-table th { background: var(--chip); font-weight: 600; }
.cat-table th:first-child, .cat-table th:nth-child(2),
.cat-table td:first-child, .cat-table td:nth-child(2) { text-align: left; }

/* question bank + persona roster */
.q-table { margin: 4px 0 20px; }
.q-table th, .q-table td { text-align: left; font-variant-numeric: normal; vertical-align: top; }
.q-table td.q-text { max-width: 52ch; line-height: 1.45; }
.pers-list { max-width: 90ch; margin: 4px 0 20px; }
.pers-list .insp-persona { margin: 0 0 6px; }
.pers-tag { font-weight: 400; color: var(--muted); margin-left: 8px; font-size: 12px; }
.q-table td.lean-left { color: #1f4e9c; font-weight: 600; }
.q-table td.lean-right { color: #c0392b; font-weight: 600; }
.q-table td.lean-amb { color: var(--muted); text-align: center; }

@media (max-width: 720px) {
  .detail-body { grid-template-columns: repeat(2, 1fr); }
  /* narrow: stack brand and tabs */
  .topbar { grid-template-columns: 1fr; justify-items: center; }
  .appx-tabs { grid-column: 1; }
}
.foot { padding: 10px 28px 28px; color: var(--faint); font-size: 11px;
  letter-spacing: 0.02em; border-top: 1px solid var(--line); }

/* ============================ messages ============================ */

.insp-bar { display: flex; flex-direction: column; align-items: flex-start; gap: 5px;
  margin: 0; }
.insp-field { display: inline-flex; align-items: center; gap: 12px; }
/* match .ctrl > label */
.insp-field label { font-size: 10px; font-weight: 600; text-transform: uppercase;
  letter-spacing: .07em; color: var(--faint); min-width: 58px; }
.insp-field select {
  font: inherit; font-size: 13px; color: var(--ink); background: var(--panel);
  border: 1px solid var(--line-strong); border-radius: 7px; padding: 5px 8px;
  cursor: pointer; max-width: 360px; }
.insp-field select:hover { border-color: var(--accent); }

.insp-question { border: 1px solid var(--line); border-radius: 10px;
  padding: 14px 16px; margin-bottom: 18px; background: var(--panel); }
.insp-qhead { font-size: 13px; font-weight: 650; color: var(--ink); }
.insp-qhead .badge { vertical-align: middle; margin-left: 8px; }
.insp-qtext { font-size: 13.5px; line-height: 1.6; color: var(--ink); margin-top: 8px;
  white-space: pre-wrap; max-width: 90ch; }
.insp-question .poles { margin-top: 12px; padding-top: 10px; }

.insp-main { display: grid; grid-template-columns: 232px minmax(0, 1fr); gap: 22px;
  align-items: start; }
@media (max-width: 720px) { .insp-main { grid-template-columns: 1fr; } }

.insp-sub { color: var(--faint); font-weight: 400; font-size: 11px; }
.insp-agents { position: sticky; top: 70px; }
.insp-agents-head { font-size: 11px; font-weight: 650; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted); margin-bottom: 10px; }
.insp-agent-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px; }
.insp-agent { font: inherit; font-size: 11px; font-variant-numeric: tabular-nums;
  border: 1px solid var(--line-strong); border-radius: 6px; padding: 7px 0;
  cursor: pointer; text-align: center; transition: transform .08s; }
.insp-agent:hover { transform: translateY(-1px); }
.insp-agent.on { outline: 2px solid var(--ink); outline-offset: 1px; font-weight: 700; }
.insp-legend { margin-top: 12px; }

.insp-focus { min-width: 0; }
.insp-focus-head { display: flex; flex-wrap: wrap; align-items: center;
  justify-content: space-between; gap: 12px; margin-bottom: 6px; }
.insp-focus-title { font-size: 16px; font-weight: 680; letter-spacing: -0.01em; }
.insp-spark { display: inline-flex; align-items: center; gap: 3px; }
.spark-cell { width: 14px; height: 14px; border-radius: 3px;
  border: 1px solid var(--line-strong); }
.insp-persona { margin: 4px 0 16px; font-size: 12.5px; color: var(--muted); }
.insp-persona summary { cursor: pointer; font-weight: 600; color: var(--accent); }
.insp-persona > div { margin-top: 8px; line-height: 1.6; white-space: pre-wrap;
  max-width: 90ch; }

.insp-round { border-left: 2px solid var(--line); padding: 4px 0 4px 16px;
  margin-left: 6px; }
.insp-round + .insp-round { margin-top: 6px; }
.insp-round-head { font-size: 12px; font-weight: 650; color: var(--ink);
  margin: 10px 0 8px; }
.insp-step-line { display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  padding: 8px 0; }
.insp-step-line.after { border-top: 1px dashed var(--line); margin-top: 8px; }
.insp-step-label { font-size: 12px; font-weight: 600; color: var(--muted); }
.insp-flip { font-size: 11px; font-weight: 700; color: #b5511f;
  text-transform: uppercase; letter-spacing: .03em; }

.op-chip { display: inline-flex; align-items: baseline; gap: 6px; font-size: 12px;
  font-weight: 650; font-variant-numeric: tabular-nums; padding: 3px 9px;
  border-radius: 999px; border: 1px solid rgba(0,0,0,.08); white-space: nowrap; }
.op-chip .op-m { font-weight: 400; font-size: 11px; opacity: .85; }

.insp-msgs { margin: 8px 0; }
.insp-msgs-cap { font-size: 11px; font-weight: 650; text-transform: uppercase;
  letter-spacing: .05em; color: var(--muted); margin-bottom: 6px; }
.insp-none { font-size: 12px; color: var(--faint); font-style: italic; padding: 2px 0; }

/* inboxes split by coupling sign */
.insp-inboxes { display: grid; grid-template-columns: 1fr 1fr; gap: 14px;
  align-items: start; }
@media (max-width: 900px) { .insp-inboxes { grid-template-columns: 1fr; } }
.insp-inbox { border: 1px solid var(--line); border-radius: 10px; padding: 10px 11px;
  background: #fcfcfc; }
.insp-inbox.pos { border-top: 3px solid var(--fresh); }
.insp-inbox.neg { border-top: 3px solid var(--lattice); }
.insp-inbox.other { border-top: 3px solid var(--line-strong); margin-top: 12px; }
.insp-inbox-cap { font-size: 12px; font-weight: 650; color: var(--ink);
  margin-bottom: 8px; font-variant-numeric: tabular-nums; }
.insp-inbox.pos .insp-inbox-cap b { color: var(--fresh); }
.insp-inbox.neg .insp-inbox-cap b { color: var(--lattice); }
.cpl-pos { color: var(--fresh); }
.cpl-neg { color: var(--lattice); }

.msg { border: 1px solid var(--line); border-radius: 9px; padding: 9px 12px;
  margin-bottom: 7px; background: var(--panel); }
.msg.in { border-left: 3px solid var(--accent); }
.insp-inbox.pos .msg.in { border-left-color: var(--fresh); }
.insp-inbox.neg .msg.in { border-left-color: var(--lattice); }
.msg.out { border-left: 3px solid var(--lattice); }
.msg-from { font-size: 11.5px; color: var(--muted); margin-bottom: 5px;
  display: flex; align-items: center; gap: 8px; }
.msg-from b { color: var(--ink); }
.msg-text { font-size: 13px; line-height: 1.55; color: var(--ink); white-space: pre-wrap; }

.insp-empty { color: var(--muted); font-size: 13.5px; line-height: 1.7;
  padding: 40px 8px; text-align: center; }
.insp-empty code { background: var(--chip); padding: 2px 6px; border-radius: 5px;
  font-size: 12px; }
