:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-soft: #eef2f5;
  --surface-strong: #e2e8ee;
  --text: #15202b;
  --muted: #5e6b78;
  --line: #cbd5df;
  --line-strong: #8b9aa9;
  --accent: #175a72;
  --accent-strong: #0f4559;
  --accent-soft: #dcecf1;
  --attention: #8a5c16;
  --attention-soft: #f5ead5;
  --good: #2c6247;
  --good-soft: #dfece5;
  --danger: #8a3535;
  --danger-soft: #f4e1e1;
  --shadow: 0 14px 40px rgba(20, 32, 43, 0.08);
  --radius: 16px;
  --radius-sm: 10px;
  --focus: 0 0 0 3px rgba(23, 90, 114, 0.28);
}

html[data-theme="dark"] {
  --bg: #0f151a;
  --surface: #172129;
  --surface-soft: #1e2a33;
  --surface-strong: #26343f;
  --text: #edf3f6;
  --muted: #aab8c2;
  --line: #344651;
  --line-strong: #5b707e;
  --accent: #7fc5da;
  --accent-strong: #a7d8e6;
  --accent-soft: #193845;
  --attention: #efc270;
  --attention-soft: #3e321d;
  --good: #8ac5a5;
  --good-soft: #1f392d;
  --danger: #e69a9a;
  --danger-soft: #422525;
  --shadow: 0 18px 48px rgba(0, 0, 0, 0.28);
  --focus: 0 0 0 3px rgba(127, 197, 218, 0.35);
}

* { box-sizing: border-box; }
html { scroll-behavior: smooth; }
body {
  margin: 0;
  min-width: 320px;
  background: var(--bg);
  color: var(--text);
  font-family: "Noto Sans Thai", "Leelawadee UI", "Segoe UI", sans-serif;
  line-height: 1.55;
}
button, input, select { font: inherit; }
a { color: inherit; }
button, a { -webkit-tap-highlight-color: transparent; }
button:focus-visible, a:focus-visible, [tabindex]:focus-visible { outline: none; box-shadow: var(--focus); }

.skip-link {
  position: fixed;
  z-index: 1000;
  left: 12px;
  top: 10px;
  transform: translateY(-160%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 12px;
  border-radius: 8px;
}
.skip-link:focus { transform: translateY(0); }

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: minmax(190px, .8fr) minmax(340px, 1.8fr) auto;
  align-items: center;
  gap: 20px;
  padding: 12px clamp(18px, 4vw, 64px);
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(14px);
}
.brand { display: flex; align-items: center; gap: 10px; text-decoration: none; min-width: 0; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 11px;
  background: var(--text);
  color: var(--surface);
  font-weight: 800;
  letter-spacing: -.04em;
}
.brand strong { display: block; font-size: 1.03rem; line-height: 1.2; }
.brand small { display: block; margin-top: 2px; color: var(--muted); font-size: .72rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.primary-nav { display: flex; justify-content: center; gap: 4px; }
.nav-link {
  padding: 8px 11px;
  border-radius: 9px;
  text-decoration: none;
  color: var(--muted);
  font-size: .91rem;
  font-weight: 600;
}
.nav-link:hover { background: var(--surface-soft); color: var(--text); }
.nav-link[aria-current="page"] { background: var(--accent-soft); color: var(--accent-strong); }
.header-actions { display: flex; gap: 7px; justify-content: flex-end; }
.compact-button, .icon-button, .ghost-button, .primary-button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  border-radius: 9px;
  cursor: pointer;
  font-weight: 650;
}
.compact-button { min-height: 36px; padding: 6px 10px; }
.icon-button { width: 38px; height: 38px; font-size: 1.35rem; }
.ghost-button { padding: 9px 13px; }
.primary-button { padding: 9px 14px; background: var(--text); color: var(--surface); border-color: var(--text); }

main { min-height: calc(100vh - 170px); }
.page { width: min(1240px, calc(100% - 36px)); margin: 0 auto; padding: clamp(34px, 5vw, 72px) 0 72px; }
.page.narrow { width: min(920px, calc(100% - 36px)); }
.eyebrow { margin: 0 0 7px; color: var(--accent); font-size: .72rem; font-weight: 800; letter-spacing: .11em; text-transform: uppercase; }
h1, h2, h3, p { margin-top: 0; }
h1 { margin-bottom: 14px; font-size: clamp(2rem, 5vw, 4.35rem); line-height: 1.06; letter-spacing: -.035em; }
h2 { font-size: clamp(1.35rem, 2.7vw, 2rem); line-height: 1.25; letter-spacing: -.018em; }
h3 { font-size: 1.05rem; }
.lead { max-width: 800px; color: var(--muted); font-size: clamp(1rem, 1.8vw, 1.18rem); }
.muted { color: var(--muted); }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; font-size: .82em; overflow-wrap: anywhere; }

.hero {
  display: grid;
  grid-template-columns: minmax(0, 1.45fr) minmax(280px, .75fr);
  gap: clamp(24px, 5vw, 72px);
  align-items: end;
  margin-bottom: 52px;
}
.hero h1 em { font-style: normal; color: var(--accent); }
.hero-note { padding: 20px; border-left: 3px solid var(--accent); background: var(--surface); box-shadow: var(--shadow); border-radius: 0 var(--radius) var(--radius) 0; }
.hero-note strong { display: block; margin-bottom: 5px; }

.metric-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 12px; margin: 26px 0 48px; }
.metric-card { padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.metric-card .value { display: block; font-size: clamp(1.65rem, 3vw, 2.55rem); font-weight: 800; line-height: 1; letter-spacing: -.035em; }
.metric-card .label { display: block; margin-top: 8px; color: var(--muted); font-size: .86rem; }

.section-head { display: flex; align-items: end; justify-content: space-between; gap: 20px; margin: 44px 0 17px; }
.section-head h2 { margin-bottom: 0; }
.section-head p { max-width: 560px; margin-bottom: 0; color: var(--muted); }
.case-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.case-card { display: flex; flex-direction: column; min-height: 315px; padding: 22px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); box-shadow: 0 3px 16px rgba(0,0,0,.025); }
.case-card:hover { border-color: var(--line-strong); }
.case-card .agency { color: var(--muted); font-size: .84rem; font-weight: 650; }
.case-card h3 { margin: 8px 0 10px; font-size: 1.35rem; }
.case-card .question { color: var(--muted); }
.case-card .card-footer { margin-top: auto; display: flex; align-items: center; justify-content: space-between; gap: 14px; }
.case-card a { text-decoration: none; font-weight: 750; color: var(--accent); }

.pill, .type-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 5px 9px;
  font-size: .74rem;
  font-weight: 750;
  line-height: 1.1;
}
.pill::before, .type-chip::before { content: ""; width: 7px; height: 7px; border-radius: 50%; background: currentColor; }
.pill.unresolved { color: var(--attention); background: var(--attention-soft); border-color: color-mix(in srgb, var(--attention) 38%, transparent); }
.pill.explained { color: var(--good); background: var(--good-soft); border-color: color-mix(in srgb, var(--good) 38%, transparent); }
.type-chip.fact { color: var(--good); background: var(--good-soft); }
.type-chip.source { color: var(--accent); background: var(--accent-soft); }
.type-chip.gap { color: var(--attention); background: var(--attention-soft); }
.type-chip.analysis { color: var(--text); background: var(--surface-soft); }

.notice { padding: 17px 19px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface-soft); color: var(--muted); }
.notice strong { color: var(--text); }

.explorer-toolbar { display: flex; gap: 10px; flex-wrap: wrap; margin: 18px 0; }
.search-box { flex: 1 1 260px; min-width: 0; padding: 10px 13px; border: 1px solid var(--line); border-radius: 10px; background: var(--surface); color: var(--text); }
.signal-list, .claim-list, .evidence-list, .method-list { display: grid; gap: 10px; }
.list-card { padding: 17px 18px; border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius-sm); }
.list-card .list-meta { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; color: var(--muted); font-size: .78rem; }
.list-card p:last-child { margin-bottom: 0; }

.case-header { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(250px, .6fr); gap: 28px; align-items: end; margin-bottom: 32px; }
.case-id { margin-top: 10px; color: var(--muted); }
.case-status-panel { padding: 18px; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); }
.case-status-panel dl { margin: 14px 0 0; display: grid; grid-template-columns: 1fr auto; gap: 8px 14px; font-size: .84rem; }
.case-status-panel dt { color: var(--muted); }
.case-status-panel dd { margin: 0; font-weight: 700; }

.tab-row { display: flex; gap: 5px; overflow-x: auto; padding: 4px 0; margin-bottom: 18px; }
.tab-button { white-space: nowrap; border: 1px solid var(--line); background: var(--surface); color: var(--muted); border-radius: 9px; padding: 8px 11px; cursor: pointer; font-weight: 700; }
.tab-button[aria-selected="true"] { color: var(--accent-strong); background: var(--accent-soft); border-color: var(--accent); }

.board-shell { display: grid; grid-template-columns: minmax(0, 1fr) 315px; border: 1px solid var(--line); border-radius: var(--radius); overflow: hidden; background: var(--surface); min-height: 650px; }
.board-stage-wrap { min-width: 0; position: relative; background: var(--surface-soft); overflow: hidden; }
.board-toolbar { position: absolute; z-index: 5; top: 12px; left: 12px; display: flex; gap: 6px; padding: 6px; border: 1px solid var(--line); border-radius: 10px; background: color-mix(in srgb, var(--surface) 92%, transparent); box-shadow: var(--shadow); }
.board-toolbar button { min-width: 34px; height: 32px; border: 1px solid var(--line); background: var(--surface); color: var(--text); border-radius: 7px; cursor: pointer; font-weight: 800; }
.board-canvas { width: 100%; height: 650px; display: block; touch-action: none; cursor: grab; }
.board-canvas.dragging { cursor: grabbing; }
.board-grid { fill: var(--surface-soft); }
.board-edge { stroke: var(--line-strong); stroke-width: 1.6; fill: none; }
.board-edge-label { fill: var(--muted); font-size: 10px; font-weight: 700; paint-order: stroke; stroke: var(--surface-soft); stroke-width: 4px; }
.board-node rect { fill: var(--surface); stroke: var(--line-strong); stroke-width: 1.5; rx: 11; }
.board-node text { fill: var(--text); pointer-events: none; }
.board-node .node-kind { fill: var(--muted); font-size: 9px; font-weight: 800; letter-spacing: .08em; }
.board-node .node-title { font-size: 12px; font-weight: 750; }
.board-node[data-kind="CASE"] rect { stroke: var(--accent); stroke-width: 2.4; }
.board-node[data-kind="PATTERN"] rect { stroke: var(--attention); }
.board-node[data-kind="MISSING"] rect { stroke-dasharray: 5 4; stroke: var(--attention); }
.board-node[data-kind="EVIDENCE"] rect, .board-node[data-kind="CALCULATION"] rect { stroke: var(--good); }
.board-node[data-kind="HYPOTHESIS"] rect { stroke: var(--line-strong); }
.board-node:focus rect, .board-node.selected rect { stroke-width: 3; filter: drop-shadow(0 3px 6px rgba(0,0,0,.14)); }
.board-detail { border-left: 1px solid var(--line); padding: 18px; overflow-y: auto; background: var(--surface); }
.board-detail h3 { margin-bottom: 6px; }
.board-detail .relation-note { padding: 10px 0; border-top: 1px solid var(--line); }
.board-detail .detail-actions { display: flex; gap: 7px; flex-wrap: wrap; margin-top: 14px; }
.board-legend { display: flex; flex-wrap: wrap; gap: 7px; margin: 12px 0 18px; }
.legend-item { display: inline-flex; align-items: center; gap: 6px; padding: 5px 8px; border: 1px solid var(--line); border-radius: 8px; font-size: .73rem; color: var(--muted); background: var(--surface); }
.legend-shape { width: 12px; height: 8px; border: 2px solid var(--line-strong); border-radius: 3px; }
.legend-shape.pattern { border-color: var(--attention); }
.legend-shape.evidence { border-color: var(--good); }
.legend-shape.missing { border-color: var(--attention); border-style: dashed; }

.two-column { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.stack { display: grid; gap: 12px; }
.hypothesis-card { padding: 18px; border: 1px solid var(--line); border-radius: var(--radius-sm); background: var(--surface); }
.hypothesis-card.leading { border-left: 4px solid var(--accent); }
.hypothesis-card.alternative { border-left: 4px solid var(--line-strong); }

.source-location { margin: 14px 0 0; display: grid; grid-template-columns: max-content 1fr; gap: 6px 12px; font-size: .82rem; }
.source-location dt { color: var(--muted); }
.source-location dd { margin: 0; overflow-wrap: anywhere; }
.calculation-expression { padding: 13px; margin: 14px 0; border-radius: 9px; background: var(--surface-soft); font-family: ui-monospace, SFMono-Regular, Consolas, monospace; overflow-x: auto; }

.site-footer { display: flex; justify-content: space-between; gap: 24px; padding: 28px clamp(18px, 4vw, 64px); border-top: 1px solid var(--line); color: var(--muted); font-size: .82rem; }
.site-footer p { margin: 5px 0 0; max-width: 650px; }
.footer-meta { display: flex; gap: 12px; align-items: start; font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }

.detail-dialog { width: min(760px, calc(100% - 24px)); max-height: min(84vh, 820px); padding: 0; border: 1px solid var(--line); border-radius: var(--radius); background: var(--surface); color: var(--text); box-shadow: 0 24px 80px rgba(0,0,0,.28); }
.detail-dialog::backdrop { background: rgba(8, 15, 20, .58); backdrop-filter: blur(3px); }
.dialog-shell { max-height: inherit; overflow: auto; }
.dialog-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: start; justify-content: space-between; gap: 20px; padding: 18px 20px; border-bottom: 1px solid var(--line); background: var(--surface); }
.dialog-head h2 { margin-bottom: 0; }
.dialog-content { padding: 20px; }

.empty-state { padding: 44px 20px; text-align: center; border: 1px dashed var(--line-strong); border-radius: var(--radius); color: var(--muted); }
.error-state { width: min(720px, calc(100% - 36px)); margin: 80px auto; padding: 28px; border: 1px solid var(--danger); border-radius: var(--radius); background: var(--danger-soft); }

@media (max-width: 980px) {
  .topbar { grid-template-columns: 1fr auto; }
  .primary-nav { grid-column: 1 / -1; justify-content: start; overflow-x: auto; order: 3; }
  .brand small { max-width: 220px; }
  .hero, .case-header { grid-template-columns: 1fr; }
  .metric-grid { grid-template-columns: repeat(2, 1fr); }
  .board-shell { grid-template-columns: 1fr; }
  .board-detail { border-left: 0; border-top: 1px solid var(--line); min-height: 220px; }
  .board-canvas { height: 560px; }
}

@media (max-width: 640px) {
  .topbar { padding: 10px 14px; gap: 10px; }
  .brand small { display: none; }
  .button-label { display: none; }
  .page { width: min(100% - 24px, 1240px); padding-top: 30px; }
  .hero { margin-bottom: 34px; }
  .metric-grid, .case-grid, .two-column { grid-template-columns: 1fr; }
  .metric-card { padding: 15px; }
  .section-head { align-items: start; flex-direction: column; gap: 5px; }
  .case-card { min-height: 260px; }
  .board-shell { border-radius: 10px; }
  .board-canvas { height: 500px; }
  .site-footer { flex-direction: column; }
  .footer-meta { flex-wrap: wrap; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition: none !important; animation: none !important; }
}
