:root {
  --bg: #f7f7f5;
  --panel: #ffffff;
  --text: #1f2328;
  --muted: #656d76;
  --border: #d8dee4;
  --accent: #2563eb;
  --present: #15803d;
  --present-bg: #dcfce7;
  --absent: #b91c1c;
  --absent-bg: #fee2e2;
  --uncertain: #a16207;
  --uncertain-bg: #fef3c7;
  --family: #6d28d9;
  --family-bg: #ede9fe;
  --clause-a: #e0ecff;
  --clause-b: #fdf0d5;
  --code-bg: #f1f3f5;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --panel: #171a21;
    --text: #e6e6e6;
    --muted: #9aa4b2;
    --border: #2d333b;
    --accent: #6ea8fe;
    --present: #4ade80;
    --present-bg: #14341f;
    --absent: #f87171;
    --absent-bg: #3b1616;
    --uncertain: #facc15;
    --uncertain-bg: #3a300c;
    --family: #c4b5fd;
    --family-bg: #2a2145;
    --clause-a: #1d2a44;
    --clause-b: #3a2f17;
    --code-bg: #1f232b;
  }
}

* { box-sizing: border-box; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.55 system-ui, -apple-system, "PingFang TC", "Noto Sans TC", sans-serif;
}
header, main { max-width: 1100px; margin: 0 auto; padding: 0 16px; }
header { padding-top: 24px; }
h1 { font-size: 22px; margin: 0; }
.sub { color: var(--muted); margin: 4px 0 16px; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  margin-bottom: 16px;
}
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin: 8px 0; }
.spacer { flex: 1; }
label, .label { font-weight: 600; }

textarea {
  width: 100%;
  font: 14px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  resize: vertical;
}
button {
  font: inherit;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  border-radius: 6px;
  padding: 4px 10px;
  cursor: pointer;
}
button#run { background: var(--accent); color: #fff; border-color: var(--accent); padding: 6px 18px; }
button:disabled { opacity: .6; cursor: default; }
#status { color: var(--muted); }
#status.error { color: var(--absent); }

#step-toggles { display: flex; flex-wrap: wrap; gap: 8px 16px; }
#step-toggles label { font-weight: 400; display: inline-flex; gap: 6px; align-items: center; }

/* ---- flow ---- */
.step {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  margin-bottom: 8px;
  overflow: hidden;
}
.step-head { padding: 12px 16px; border-bottom: 1px solid var(--border); }
.step-head h2 { font-size: 17px; margin: 0; }
.step-head .num {
  display: inline-block; min-width: 24px; height: 24px; line-height: 24px;
  text-align: center; border-radius: 12px; background: var(--accent); color: #fff;
  font-size: 13px; margin-right: 8px;
}
.step-head .desc { color: var(--muted); margin: 4px 0 6px; }
.io { font-size: 13px; color: var(--muted); }
.io code { background: var(--code-bg); padding: 1px 6px; border-radius: 4px; color: var(--text); }
.step-body { padding: 12px 16px; overflow-x: auto; }
.arrow { text-align: center; color: var(--muted); margin: 0 0 8px; font-size: 18px; }
details.raw summary { cursor: pointer; color: var(--muted); font-size: 13px; margin-top: 8px; }
pre {
  background: var(--code-bg);
  padding: 10px;
  border-radius: 6px;
  overflow: auto;
  max-height: 360px;
  font-size: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}
.rawtext { white-space: pre-wrap; font-family: ui-monospace, Menlo, monospace; font-size: 13px; line-height: 1.8; }

table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--muted); font-weight: 600; font-size: 13px; }
td.mono, .mono { font-family: ui-monospace, Menlo, monospace; font-size: 12px; }

.clause-hl { border-radius: 3px; padding: 1px 0; }
.clause-hl.a { background: var(--clause-a); }
.clause-hl.b { background: var(--clause-b); }

.hit { border-radius: 3px; padding: 1px 2px; font-weight: 600; cursor: help; }
.tag { display: inline-block; font-size: 12px; padding: 0 7px; border-radius: 10px; white-space: nowrap; }
.present { color: var(--present); background: var(--present-bg); }
.absent { color: var(--absent); background: var(--absent-bg); }
.uncertain { color: var(--uncertain); background: var(--uncertain-bg); }
.family { color: var(--family); background: var(--family-bg); }
.patient { color: var(--muted); background: var(--code-bg); }
.reason { color: var(--muted); font-size: 12px; }
.reason q { color: var(--text); }
.muted { color: var(--muted); }

.buckets { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; }
.bucket { border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
.bucket h3 { margin: 0 0 8px; font-size: 15px; }
.bucket ul { margin: 0; padding-left: 18px; }
.bucket li { margin-bottom: 4px; }
@media (max-width: 720px) { .buckets { grid-template-columns: 1fr; } }

/* ---- top tabs ---- */
.tabs { display: flex; gap: 4px; margin: 12px 0 4px; border-bottom: 1px solid var(--border); }
.tabs a {
  padding: 6px 14px; color: var(--muted); text-decoration: none;
  border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a[aria-current="true"] { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
main[hidden] { display: none; }

/* ---- dictionary page ---- */
#dict-q {
  flex: 1; min-width: 0; font: inherit; padding: 8px 10px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text);
}
#sem-q { flex: 1; min-width: 0; font: inherit; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
#sem-status.err, #sem-msg.err { color: var(--absent); }
#sem-msg.ok { color: var(--present); }
.pick-col { width: 36px; }
.score-col { width: 34%; }
#sem-results tr { cursor: pointer; }
#sem-results tr:hover, #sem-results tr.picked { background: var(--code-bg); }
#sem-results input[type="radio"] { accent-color: var(--accent); }
.score { display: flex; align-items: center; gap: 8px; }
.score .bar { flex: 1; height: 6px; border-radius: 3px; background: var(--code-bg); overflow: hidden; }
.score .bar span { display: block; height: 100%; background: var(--accent); }
.score .num { font-family: ui-monospace, Menlo, monospace; font-size: 12px; min-width: 44px; text-align: right; }
.sem-add { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; }
#sem-surface { width: 240px; max-width: 100%; }
.panel-title { font-size: 15px; margin: 0 0 4px; }
.add-row input { font: inherit; padding: 7px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
#add-surface { width: 200px; max-width: 100%; }
.picker { position: relative; flex: 1; min-width: 220px; }
.picker input { width: 100%; }
.suggest {
  position: absolute; z-index: 10; left: 0; right: 0; top: calc(100% + 4px);
  list-style: none; margin: 0; padding: 4px; max-height: 280px; overflow: auto;
  background: var(--panel); border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, .18);
}
.suggest[hidden] { display: none; }
.suggest li { padding: 6px 8px; border-radius: 6px; cursor: pointer; }
.suggest li[aria-selected="true"], .suggest li:hover { background: var(--code-bg); }
#add-msg.ok { color: var(--present); }
#add-msg.err { color: var(--absent); }
.chip.editable { display: inline-flex; align-items: center; gap: 4px; }
.chip .del {
  border: 0; background: none; padding: 0 2px; line-height: 1; font-size: 14px;
  color: var(--muted); cursor: pointer;
}
.chip .del:hover { color: var(--absent); }
.chip.confirming { box-shadow: inset 0 0 0 1px var(--absent); }
.chip .yes { border: 0; border-radius: 4px; padding: 0 6px; background: var(--absent); color: #fff; font-size: 12px; }
.chip .no { border: 0; border-radius: 4px; padding: 0 6px; font-size: 12px; }
.chip.pending { background: transparent; box-shadow: inset 0 0 0 1px var(--uncertain); }
.error-box { margin-top: 8px; padding: 8px 10px; border-radius: 6px; color: var(--absent); background: var(--absent-bg); }
.error-box[hidden] { display: none; }
mark { background: var(--uncertain-bg); color: inherit; border-radius: 2px; }
.chip.unmatchable { outline: 1px dashed var(--absent); color: var(--muted); }
.term-link { color: var(--accent); text-decoration: none; }
.term-link:hover { text-decoration: underline; }
a { color: var(--accent); }

/* ---- view toggle ---- */
.view-row { margin: 0 0 12px; }
.view-row[hidden] { display: none; }
.segmented { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.segmented button { border: 0; border-radius: 0; padding: 5px 16px; background: var(--panel); }
.segmented button[aria-checked="true"] { background: var(--accent); color: #fff; }
.legend { display: inline-flex; gap: 6px; margin-left: auto; flex-wrap: wrap; }

/* ---- summary view ---- */
.flow-summary .step-head { padding: 10px 16px; border-bottom: 0; }
.flow-summary .step-body { padding: 0 16px 14px; }
.flow-summary .step-head .summary-line { color: var(--muted); font-size: 14px; margin-top: 2px; }
.clause-hl.fam { box-shadow: inset 0 -2px 0 var(--family); }
.term-cloud { display: flex; flex-wrap: wrap; gap: 6px; }
.term-cloud .tag { font-size: 13px; padding: 2px 10px; }
.bucket.compact li { margin-bottom: 2px; }

.rule { margin: 12px 0; }
.rule h3 { font-size: 15px; margin: 0; }
.chips { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.chip { font-family: ui-monospace, Menlo, monospace; font-size: 12px; background: var(--code-bg); padding: 1px 6px; border-radius: 4px; }
