:root {
  --bg: #fbfaf8; --surface: #ffffff; --text: #1f1d1a; --muted: #6b665e;
  --border: #e6e2db; --accent: #8a3b12; --accent-soft: #f6e9df; --row-hover: #f5f2ec;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #181715; --surface: #211f1c; --text: #ece8e1; --muted: #a39d93;
    --border: #3a3631; --accent: #e8a27a; --accent-soft: #3a2a20; --row-hover: #2a2723;
    color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif;
}
a { color: var(--accent); }
.page-header, main { max-width: 1280px; margin: 0 auto; padding: 0 16px; }
.page-header { padding-top: 32px; padding-bottom: 8px; }
h1 { margin: 0 0 4px; font-size: 28px; letter-spacing: -0.01em; }
.subtitle, .meta { color: var(--muted); margin: 0; }
.meta { display: flex; flex-wrap: wrap; gap: 6px 14px; }
.back { display: inline-block; margin-bottom: 12px; text-decoration: none; }

.filters { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin: 20px 0 12px; }
.filters input[type="search"], .filters select, .filters button {
  font: inherit; color: inherit; background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 6px 10px;
}
.filters input[type="search"] { flex: 1 1 240px; min-width: 0; }
.filters button { cursor: pointer; }
.check { display: inline-flex; gap: 6px; align-items: center; color: var(--muted); }

.table-wrap { overflow-x: auto; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; margin-bottom: 40px; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { position: sticky; top: 0; background: var(--surface); font-size: 13px; color: var(--muted); font-weight: 600; white-space: nowrap; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable::after { content: " ↕"; opacity: 0.35; }
th[aria-sort="ascending"]::after { content: " ↑"; opacity: 1; }
th[aria-sort="descending"]::after { content: " ↓"; opacity: 1; }
tbody tr:hover { background: var(--row-hover); }
tbody tr:last-child td { border-bottom: 0; }
td.title { min-width: 320px; font-weight: 500; }
td.title a { color: var(--text); text-decoration: none; }
td.title a:hover { color: var(--accent); text-decoration: underline; }
td.num { text-align: right; font-variant-numeric: tabular-nums; }
th[data-key="citations"] { text-align: right; }
.authors { font-weight: 400; font-size: 13px; color: var(--muted); margin: 2px 0 0; }
.page-header td.num { text-align: right; font-variant-numeric: tabular-nums; }
th[data-key="citations"] { text-align: right; }
.authors { font-size: 15px; margin: 4px 0 6px; }
.note { color: var(--muted); font-size: 13px; }
.badge { display: inline-block; font-size: 11px; font-weight: 600; padding: 1px 6px; border-radius: 999px; background: var(--accent-soft); color: var(--accent); vertical-align: 1px; }
.tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 4px; }
.tag { font-size: 11px; font-weight: 500; padding: 1px 7px; border-radius: 999px; border: 1px solid var(--border); color: var(--muted); }
.tag.key { border-color: var(--accent); color: var(--accent); }
.tag-notes { margin: 16px 0; padding: 12px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; }
.tag-notes h2 { margin: 0 0 6px; font-size: 15px; }
.tag-notes p { margin: 4px 0; }
.empty { color: var(--muted); padding: 24px 12px; margin: 0; }

.analysis { max-width: 760px; padding: 16px 0 64px; }
.analysis h2 { margin-top: 2em; font-size: 20px; }
.analysis h3 { margin-top: 1.5em; font-size: 16px; }
.analysis blockquote { margin: 1em 0; padding: 0 1em; border-left: 3px solid var(--border); color: var(--muted); }
.analysis code { background: var(--row-hover); padding: 1px 4px; border-radius: 4px; }
.analysis table { margin: 1em 0; border: 1px solid var(--border); }

.hl-bar { max-width: 760px; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; margin-top: 12px; font-size: 13px; color: var(--muted); }
.hl-bar button, .hl-popup button {
  font: inherit; font-size: 13px; color: inherit; background: var(--surface);
  border: 1px solid var(--border); border-radius: 6px; padding: 3px 10px; cursor: pointer;
}
.hl-count { margin-right: auto; }
.hl-popup { position: absolute; z-index: 10; }
.hl-popup button { background: var(--text); color: var(--bg); border-color: var(--text); box-shadow: 0 2px 8px rgb(0 0 0 / 0.2); }
mark.hl { background: #fde68a; color: #1f1d1a; border-radius: 2px; cursor: pointer; }
mark.hl.flash { animation: hl-flash 1s ease-out; }
@keyframes hl-flash { from { background: #f59e0b; } to { background: #fde68a; } }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) mark.hl { background: #a16207; color: #fef9c3; }
  :root:not([data-theme="light"]) mark.hl.flash { animation: none; }
}
.hl-popup { display: flex; gap: 4px; }

/* Ask AI panel */
.ai-toggle {
  position: fixed; right: 20px; bottom: 20px; z-index: 20;
  font: inherit; font-weight: 600; color: var(--bg); background: var(--accent);
  border: 0; border-radius: 999px; padding: 10px 18px; cursor: pointer; box-shadow: 0 2px 10px rgb(0 0 0 / 0.2);
}
body.ai-open .ai-toggle { display: none; }
.ai-panel {
  position: fixed; top: 0; right: 0; bottom: 0; z-index: 30; width: 400px; max-width: 100%;
  display: flex; flex-direction: column; gap: 8px; padding: 12px;
  background: var(--surface); border-left: 1px solid var(--border); box-shadow: -4px 0 16px rgb(0 0 0 / 0.08);
  font-size: 14px;
}
.ai-panel[hidden] { display: none; }
@media (min-width: 1100px) { body.ai-open { padding-right: 400px; } }
@media (max-width: 700px) {
  .ai-panel { top: auto; height: 75vh; width: 100%; border-left: 0; border-top: 1px solid var(--border); border-radius: 12px 12px 0 0; }
}
.ai-head { display: flex; align-items: center; gap: 8px; }
.ai-status { font-size: 12px; color: var(--muted); margin-right: auto; }
.ai-status[data-kind="ok"]::before { content: "● "; color: #16a34a; }
.ai-status[data-kind="err"]::before, .ai-status[data-kind="warn"]::before { content: "● "; color: #dc2626; }
.ai-panel button, .ai-panel select, .ai-panel input[type="url"], .ai-panel textarea {
  font: inherit; color: inherit; background: var(--surface); border: 1px solid var(--border); border-radius: 6px;
}
.ai-panel button { padding: 3px 10px; cursor: pointer; font-size: 13px; }
.ai-panel button:hover { background: var(--row-hover); }
.ai-close { border: 0 !important; font-size: 18px !important; line-height: 1; }
.ai-tools { display: flex; align-items: flex-start; gap: 8px; font-size: 13px; }
.ai-tools > button { margin-left: auto; }
.ai-settings { flex: 1; color: var(--muted); }
.ai-settings summary { cursor: pointer; }
.ai-settings label { display: flex; align-items: center; gap: 6px; margin: 6px 0; }
.ai-settings select, .ai-settings input[type="url"] { flex: 1; min-width: 0; padding: 3px 6px; }
.ai-quick { display: flex; flex-wrap: wrap; gap: 4px; }
.ai-quick button { border-radius: 999px !important; font-size: 12px !important; }
.ai-log { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 8px; padding: 4px 0; }
.ai-msg { padding: 8px 10px; border-radius: 8px; line-height: 1.5; overflow-wrap: anywhere; }
.ai-msg > :first-child { margin-top: 0; }
.ai-msg > :last-child { margin-bottom: 0; }
.ai-msg ul, .ai-msg ol { padding-left: 1.3em; }
.ai-msg code { background: var(--row-hover); padding: 1px 4px; border-radius: 4px; }
.ai-user { align-self: flex-end; max-width: 88%; background: var(--accent-soft); white-space: pre-wrap; }
.ai-assistant { background: var(--bg); border: 1px solid var(--border); }
.ai-assistant.pending { color: var(--muted); font-style: italic; }
.ai-error { color: #dc2626; }
.ai-help { background: var(--accent-soft); font-size: 13px; }
.ai-form { display: flex; flex-wrap: wrap; gap: 6px; align-items: flex-end; }
.ai-form textarea { flex: 1; min-width: 0; resize: vertical; padding: 6px 8px; }
.ai-send { background: var(--accent) !important; color: var(--bg); border-color: var(--accent) !important; padding: 6px 14px !important; }
.ai-quote { flex-basis: 100%; display: flex; gap: 6px; align-items: flex-start; font-size: 12px; color: var(--muted); border-left: 3px solid var(--accent); padding: 2px 0 2px 8px; }
.ai-quote[hidden] { display: none; }
.ai-quote span { flex: 1; font-style: italic; }
.ai-quote button { border: 0 !important; padding: 0 4px !important; }
