/* Paper Triage — light, flat, square. */
:root {
  --bg: #f3f3f1;
  --surface: #ffffff;
  --surface-2: #f7f7f5;
  --hover: #efefec;
  --border: #d8d8d3;
  --border-strong: #b9b9b2;
  --text: #1b1b1a;
  --muted: #6b6b66;
  --accent: #1f3f7a;
  --accent-hover: #17315f;
  --accent-soft: #e8edf6;
  --danger: #a32020;
  --ok: #2d6a3e;
  --p1: #b42318; --p2: #c4650f; --p3: #1f3f7a; --p4: #8a8a84; --p5: #c4c4be;
  --serif: "Charter", "Bitstream Charter", "Iowan Old Style", "Sitka Text", Cambria, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --r: 0px;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.5 var(--sans);
  display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
  -webkit-font-smoothing: antialiased;
}
h2 { font-size: 13px; margin: 0 0 14px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; color: var(--text); }
a { color: var(--accent); text-underline-offset: 2px; }

/* ---------- icons ---------- */
.i { width: 18px; height: 18px; flex: 0 0 auto; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: square; stroke-linejoin: miter; }
.i.fill { fill: currentColor; stroke: none; width: 14px; height: 14px; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: stretch; gap: 32px;
  padding: 0 24px; height: 52px; flex: 0 0 auto;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 15px; letter-spacing: -.01em; }
.brand .i { width: 20px; height: 20px; color: var(--accent); }
.tabs { display: flex; align-items: stretch; }
.tab {
  background: none; border: 0; border-bottom: 2px solid transparent; border-radius: 0;
  color: var(--muted); font: inherit; font-weight: 500; padding: 0 16px; cursor: pointer;
  display: flex; align-items: center; gap: 8px; margin-bottom: -1px;
}
.tab:hover:not(:disabled) { color: var(--text); }
.tab.active { color: var(--text); border-bottom-color: var(--accent); }
.tab:disabled { opacity: .35; cursor: default; }
.count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; border: 1px solid var(--border); padding: 0 6px; line-height: 18px; }
.tab.active .count { border-color: var(--accent); color: var(--accent); }

/* ---------- views ---------- */
.view { display: none; flex: 1 1 auto; min-height: 0; }
.view.active { display: block; overflow: auto; }
.page { max-width: 1080px; margin: 0 auto; padding: 28px 24px 60px; }
.page.wide { max-width: 1500px; }
.view.reader.active { display: flex; overflow: hidden; }

.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--r); padding: 20px; }

/* ---------- controls ---------- */
input[type=text], input[type=search], input:not([type]), select, textarea {
  font: inherit; color: var(--text); background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--r); padding: 8px 10px; width: 100%;
  -webkit-appearance: none; appearance: none;
}
select {
  width: auto; padding-right: 30px; cursor: pointer;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%236b6b66'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center;
}
textarea { resize: vertical; line-height: 1.5; }
input:hover, select:hover, textarea:hover { border-color: var(--border-strong); }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
::placeholder { color: #9a9a94; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; white-space: nowrap;
  font: inherit; font-weight: 500; text-decoration: none; color: var(--text);
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: var(--r);
  padding: 0 14px; height: 36px; cursor: pointer;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: #fbeaea; border-color: var(--danger); }
.btn.small { height: 32px; padding: 0 12px; font-size: 13px; }
.btn:disabled { opacity: .45; cursor: default; }
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 34px; height: 34px; padding: 0; background: none; border: 1px solid transparent; border-radius: var(--r);
  color: var(--muted); cursor: pointer; text-decoration: none;
}
.icon-btn:hover { background: var(--hover); color: var(--text); }
.icon-btn.bordered { border-color: var(--border-strong); color: var(--text); width: 36px; height: 36px; }
.icon-btn.del:hover { color: var(--danger); }
.spacer { flex: 1; }
.hint { color: var(--muted); font-size: 13px; margin-top: -10px; }

/* ---------- add form ---------- */
.add-card { margin-bottom: 36px; }
.add-row { display: flex; gap: 10px; align-items: center; margin-bottom: 10px; }
.add-row > input { flex: 1; height: 38px; }
.add-row select, .add-row .btn { height: 38px; }
.or { color: var(--muted); font-size: 13px; }
.file-drop {
  flex: 0 0 200px; height: 38px; display: flex; align-items: center; justify-content: center; gap: 8px;
  border: 1px dashed var(--border-strong); border-radius: var(--r);
  padding: 0 12px; cursor: pointer; color: var(--muted); overflow: hidden; white-space: nowrap;
}
.file-drop span { overflow: hidden; text-overflow: ellipsis; }
.file-drop:hover { color: var(--text); border-color: var(--text); }
.file-drop.has-file { border-style: solid; border-color: var(--accent); color: var(--accent); background: var(--accent-soft); }
.file-drop.drag { background: var(--accent-soft); border-color: var(--accent); }
.msg { min-height: 18px; font-size: 13px; color: var(--muted); }
.msg.err { color: var(--danger); }

/* ---------- stack ---------- */
.section-head { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 14px; }
.section-head h2 { margin-bottom: 14px; }
.stack-list { list-style: none; padding: 0; margin: 0; background: var(--surface); border: 1px solid var(--border); }
.stack-list:empty { display: none; }
.stack-item {
  display: flex; align-items: center; gap: 16px; padding: 14px 16px 14px 0;
  border-bottom: 1px solid var(--border);
}
.stack-item:last-child { border-bottom: 0; }
.stack-item:hover { background: var(--surface-2); }
.prio-bar { width: 4px; align-self: stretch; margin: -14px 0; }
.stack-item .pos { color: var(--muted); width: 18px; text-align: right; font-variant-numeric: tabular-nums; font-size: 13px; }
.stack-item .info { flex: 1; min-width: 0; }
.stack-item .t { font-family: var(--serif); font-size: 16px; font-weight: 600; line-height: 1.3; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stack-item .sub { color: var(--muted); font-size: 12.5px; margin-top: 3px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.stack-item .actions { display: flex; gap: 8px; align-items: center; }
.stack-item select { font-size: 13px; height: 32px; padding-top: 0; padding-bottom: 0; }
.tag {
  display: inline-block; font-family: var(--sans); font-size: 10.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase;
  padding: 1px 6px; margin-left: 8px; vertical-align: 2px; border: 1px solid;
}
.tag.reading { color: var(--accent); border-color: var(--accent); }
.tag.nopdf { color: var(--muted); border-color: var(--border-strong); }
.tag.blog { color: #6b5617; border-color: #c4a35a; }
.tag.cat { color: #3d5a3d; border-color: #8aaa8a; }
.empty { color: var(--muted); text-align: center; padding: 48px 0; }
.stack-toolbar {
  display: flex; gap: 12px; align-items: center; flex-wrap: wrap;
  margin-bottom: 14px; padding: 10px 12px; background: var(--surface); border: 1px solid var(--border);
}
.stack-toolbar[hidden] { display: none; }
.stack-toolbar .search { width: min(280px, 100%); flex: 0 1 280px; }
.stack-toolbar .chips { flex: 1; padding: 0; border: 0; background: transparent; }
.stack-item .actions .cat {
  width: 120px; height: 32px; font-size: 13px; padding: 0 8px;
}

/* ---------- discover ---------- */
.discover-card { margin-bottom: 22px; }
.discover-form .hint { margin-top: 10px; }
.discover-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.discover-q { flex: 1 1 280px; width: auto; min-width: 200px; }
.discover-row select { height: 38px; }
.discover-row .btn { height: 38px; }
.discover-list { display: flex; flex-direction: column; gap: 0; background: var(--surface); border: 1px solid var(--border); }
.discover-list:empty { display: none; }
.discover-item { padding: 18px 20px; border-bottom: 1px solid var(--border); }
.discover-item:last-child { border-bottom: 0; }
.discover-title { font-family: var(--serif); font-size: 17px; font-weight: 600; line-height: 1.35; margin: 0 0 4px; }
.discover-sub { color: var(--muted); font-size: 12.5px; margin-bottom: 10px; }
.discover-abs {
  margin: 0 0 14px; font-size: 13.5px; line-height: 1.55; color: var(--text);
  display: -webkit-box; -webkit-line-clamp: 5; -webkit-box-orient: vertical; overflow: hidden;
}
.discover-abs.muted { color: var(--muted); font-style: italic; }
.discover-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.discover-actions select { height: 32px; font-size: 13px; }
.discover-actions .discover-cat { width: 130px; height: 32px; font-size: 13px; padding: 0 8px; }
.ref-actions .ref-cat { width: 120px; height: 32px; font-size: 13px; padding: 0 8px; }
.discover-more { display: flex; justify-content: center; padding: 22px 0 8px; }
.discover-more[hidden] { display: none; }
.discover-meta[hidden] { display: none; }

/* ---------- reader ---------- */
.reader-main { flex: 1 1 auto; min-width: 200px; display: flex; flex-direction: column; }
.doc-toolbar {
  flex: 0 0 auto; height: 46px; display: flex; align-items: center; gap: 12px; padding: 0 8px 0 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.doc-title { flex: 1; min-width: 0; font-family: var(--serif); font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tool-group { display: flex; align-items: center; gap: 2px; }
.tool-group.hidden { display: none; }
.sep { width: 1px; height: 20px; background: var(--border); margin: 0 8px; }
.page-ind { display: flex; align-items: center; gap: 6px; color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.page-ind input { width: 44px; height: 28px; padding: 0 4px; text-align: center; font-size: 13px; }
.zoom-ind { width: 46px; text-align: center; font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }
.reader-doc { flex: 1 1 auto; position: relative; background: #e4e4e0; min-height: 0; }
.reader-doc iframe { width: 100%; height: 100%; border: 0; display: block; background: #fff; }
.reader-doc .no-doc {
  position: absolute; inset: 0; display: flex; flex-direction: column; gap: 12px;
  align-items: center; justify-content: center; color: var(--muted); text-align: center; padding: 20px;
}
.reader-doc .web-banner {
  flex: 0 0 auto; font-size: 13px; background: #fff8e6; border-bottom: 1px solid #ecd9a6; padding: 8px 14px; color: #6b5617;
}
.reader-doc.web { display: flex; flex-direction: column; }
.reader-doc.web iframe { flex: 1; height: auto; }

.splitter { flex: 0 0 5px; cursor: col-resize; background: var(--border); touch-action: none; }
.splitter:hover, .splitter.drag { background: var(--accent); }
.reader.side-hidden .splitter, .reader.side-hidden .reader-side { display: none; }
.reader.side-hidden #rToggleSide { background: var(--surface); }
.reader:not(.side-hidden) #rToggleSide { background: var(--accent-soft); border-color: var(--accent); color: var(--accent); }

.reader-side {
  flex: 0 0 420px; display: flex; flex-direction: column; min-width: 300px; max-width: 70vw;
  background: var(--surface); overflow: hidden;
}
.side-head { padding: 14px 18px 12px; border-bottom: 1px solid var(--border); position: relative; }
.drawer-close { display: none; position: absolute; top: 8px; right: 8px; }
.title-input {
  font-family: var(--serif); font-weight: 600; font-size: 17px; line-height: 1.3; resize: none;
  border-color: transparent !important; box-shadow: none !important; padding: 4px 6px !important; margin-left: -6px; width: calc(100% + 6px) !important;
}
.authors-input { font-size: 13px; color: var(--muted) !important; border-color: transparent !important; box-shadow: none !important; padding: 2px 6px !important; margin-left: -6px; }
.title-input:hover, .authors-input:hover, .title-input:focus, .authors-input:focus { background: var(--surface-2); }
.side-meta { display: flex; justify-content: space-between; gap: 10px; font-size: 12px; padding-top: 4px; }
.side-meta a { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.saved { color: var(--muted); white-space: nowrap; }
.saved.ok { color: var(--ok); }
.side-body { flex: 1; overflow: auto; -webkit-overflow-scrolling: touch; }
.side-body[hidden] { display: none; }
.class-form { padding: 16px 18px; display: flex; flex-direction: column; gap: 14px; }
.class-form label, .field { display: flex; flex-direction: column; gap: 5px; font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.class-form label > input, .class-form label > textarea { font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--text); font-size: 14px; }
.topics-field .hint { font-weight: 400; letter-spacing: 0; text-transform: none; font-size: 12px; color: var(--muted); line-height: 1.4; }
.topics-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.topics-head .field-label { margin: 0; }
.topic-rows { display: flex; flex-direction: column; gap: 10px; }
.topic-row {
  display: grid; grid-template-columns: 1fr auto; grid-template-rows: auto auto; gap: 6px 8px;
  padding: 10px; border: 1px solid var(--border); background: var(--surface-2);
}
.topic-row .topic-name { grid-column: 1; font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--text); font-size: 14px; }
.topic-row .topic-contrib { grid-column: 1 / -1; font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--text); font-size: 14px; }
.topic-row .topic-remove { grid-column: 2; grid-row: 1; align-self: center; }
.rating { display: flex; flex-wrap: wrap; }
.rating button {
  flex: 1; min-width: 36px; height: 36px; font: inherit; font-weight: 600; letter-spacing: 0; cursor: pointer; border-radius: 0;
  border: 1px solid var(--border-strong); margin-left: -1px; background: var(--surface); color: var(--text);
}
.rating button.nr { flex: 1.6 1 auto; min-width: 110px; font-size: 12px; }
.rating button:first-child { margin-left: 0; }
.rating button:hover { background: var(--hover); }
.rating button.on { background: var(--accent); border-color: var(--accent); color: #fff; position: relative; }
.rating button.nr.on { background: var(--danger); border-color: var(--danger); color: #fff; }
.field .hint { font-weight: 400; letter-spacing: 0; text-transform: none; font-size: 12px; color: var(--muted); line-height: 1.4; }
.side-actions { display: flex; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); background: var(--surface-2); flex-wrap: wrap; }
.side-actions .primary { margin-left: auto; }
.zotero-lib { height: 36px; max-width: 180px; font-size: 13px; }
.zotero-lib[hidden], #rZotero[hidden], #dZotero[hidden] { display: none; }
.tag.zotero { color: #8a4b12; border-color: #c48a4a; }

/* PDF.js pages */
.pdfjs { position: absolute; inset: 0; overflow: auto; -webkit-overflow-scrolling: touch; padding: 20px 0 40px; }
.pdfjs .pg { margin: 0 auto 16px; background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.08), 0 2px 6px rgba(0,0,0,.08); position: relative; }
.pdfjs .pg canvas { display: block; width: 100%; height: 100%; }
.pdfjs .pg canvas.annotLayer {
  position: absolute; inset: 0; width: 100%; height: 100%; z-index: 3; touch-action: none;
  pointer-events: none;
}
.pdfjs.annotating-ink .pg canvas.annotLayer { pointer-events: auto; cursor: crosshair; }
.pdfjs.annotating-erase .pg canvas.annotLayer { pointer-events: auto; cursor: none; }
.pdfjs .hlLayer { position: absolute; inset: 0; z-index: 2; pointer-events: none; }
.pdfjs .hlLayer .hl {
  position: absolute; mix-blend-mode: multiply; opacity: .45; border-radius: 1px; pointer-events: none;
}
.pdfjs.annotating-erase .hlLayer { z-index: 5; }
.pdfjs.annotating-erase .hlLayer .hl { pointer-events: auto; cursor: pointer; }
.pdfjs.annotating-erase .hlLayer .hl:hover { outline: 1px solid var(--danger); opacity: .7; }
.pdfjs .loading { text-align: center; color: var(--muted); padding: 40px; }
/* selectable text layer (PDF.js 3.x) */
.textLayer { position: absolute; inset: 0; overflow: hidden; line-height: 1; text-align: initial; opacity: 1; forced-color-adjust: none; transform-origin: 0 0; z-index: 4; }
.textLayer span, .textLayer br { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgba(31, 63, 122, .28); }
.pdfjs.annotating-highlight .textLayer ::selection { background: rgba(245, 215, 110, .55); }
.textLayer br::selection { background: transparent; }
.textLayer .endOfContent { display: block; position: absolute; inset: 100% 0 0; z-index: -1; cursor: default; user-select: none; }
.textLayer.selecting .endOfContent { top: 0; }
.pdfjs.annotating-ink .textLayer,
.pdfjs.annotating-erase .textLayer { pointer-events: none; user-select: none; }
.pdfjs .linkLayer { position: absolute; inset: 0; z-index: 5; pointer-events: none; }
.pdfjs .linkLayer .pdfLink {
  position: absolute; pointer-events: auto; cursor: pointer;
  background: transparent; border-radius: 2px;
}
.pdfjs .linkLayer .pdfLink:hover { background: rgba(31, 63, 122, .12); }
.pdfjs.annotating-ink .linkLayer,
.pdfjs.annotating-erase .linkLayer,
.pdfjs.annotating-highlight .linkLayer { pointer-events: none; }

.annot-tools { display: flex; align-items: center; gap: 2px; }
.annot-tools .icon-btn.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.annot-colors { display: none; align-items: center; gap: 2px; margin-left: 4px; }
.annot-tools.has-color .annot-colors { display: flex; }
.annot-colors .swatch { width: 26px; height: 26px; padding: 4px; }

/* ---------- library ---------- */
.lib-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 18px; flex-wrap: wrap; }
.lib-toolbar select, .lib-toolbar .btn { height: 38px; }
.lib-rq-hint { margin: -8px 0 16px; }
.lib-rq-hint[hidden] { display: none; }
.rq-modal-box { width: min(720px, 100%); }
.rq-modal-box > .hint { margin: 0 40px 18px 0; font-weight: 400; text-transform: none; letter-spacing: 0; }
.rq-list { display: flex; flex-direction: column; gap: 0; border: 1px solid var(--border); margin-bottom: 22px; background: var(--surface); }
.rq-list:empty { display: none; }
.rq-item { display: flex; gap: 12px; align-items: flex-start; padding: 12px 14px; border-bottom: 1px solid var(--border); }
.rq-item:last-child { border-bottom: 0; }
.rq-item-main { flex: 1; min-width: 0; }
.rq-item-title { font-family: var(--serif); font-size: 15px; font-weight: 600; line-height: 1.3; }
.rq-item-topics { color: var(--muted); font-size: 12.5px; margin-top: 4px; }
.rq-item-actions { display: flex; gap: 6px; flex-shrink: 0; }
.rq-form { display: flex; flex-direction: column; gap: 12px; border-top: 1px solid var(--border); padding-top: 18px; }
.rq-form h4 { margin: 0; }
.rq-form label { display: flex; flex-direction: column; gap: 5px; font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.rq-form label > input { font-weight: 400; letter-spacing: 0; text-transform: none; color: var(--text); font-size: 14px; }
.rq-topic-chips { display: flex; flex-wrap: wrap; gap: 6px; min-height: 28px; align-items: center; }
.rq-topic-chips .muted { color: var(--muted); font-size: 13px; font-weight: 400; text-transform: none; letter-spacing: 0; }
.rq-topic-add { display: flex; gap: 8px; }
.rq-topic-add input { flex: 1; }
.rq-form-actions { margin-top: 4px; }
.rq-form-actions .primary { margin-left: auto; }
.empty-note { color: var(--muted); padding: 16px; margin: 0; text-align: center; }
.search { position: relative; width: 340px; }
.search .i { position: absolute; left: 10px; top: 10px; color: var(--muted); }
.search input { padding-left: 36px; height: 38px; }
.stats { display: flex; margin-bottom: 18px; background: var(--surface); border: 1px solid var(--border); }
.stats:empty { display: none; }
.stat { padding: 14px 20px; border-right: 1px solid var(--border); min-width: 140px; }
.stat .v { font-size: 22px; font-weight: 600; font-variant-numeric: tabular-nums; line-height: 1.2; }
.stat .l { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; margin-top: 2px; }
.chips { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; align-content: center; flex: 1; padding: 10px 16px; }
.chip {
  font: inherit; font-size: 12.5px; cursor: pointer; border: 1px solid var(--border-strong); border-radius: 0;
  background: var(--surface); color: var(--text); padding: 3px 10px;
}
.chip:hover { border-color: var(--accent); }
.chip .n { color: var(--muted); margin-left: 4px; }
.chip.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.chip.on .n { color: #c9d4ea; }
.table-wrap { overflow: auto; background: var(--surface); border: 1px solid var(--border); }
.lib-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.lib-table th {
  position: sticky; top: 0; z-index: 1; background: var(--surface-2); text-align: left;
  font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted);
  padding: 10px 12px; border-bottom: 1px solid var(--border-strong); cursor: pointer; white-space: nowrap; user-select: none;
}
.lib-table th:hover { color: var(--text); }
.lib-table th.sorted { color: var(--text); }
.lib-table th.sorted::after { content: ""; display: inline-block; margin-left: 6px; border: 4px solid transparent; border-bottom: 5px solid currentColor; border-top: 0; vertical-align: 2px; }
.lib-table th.sorted.desc::after { border-bottom: 0; border-top: 5px solid currentColor; }
.lib-table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: top; max-width: 260px; }
.lib-table tbody tr { cursor: pointer; }
.lib-table tbody tr:hover td { background: var(--surface-2); }
.lib-table td .clamp { display: -webkit-box; -webkit-line-clamp: 3; -webkit-box-orient: vertical; overflow: hidden; }
.lib-table td.title { font-family: var(--serif); font-size: 14.5px; font-weight: 600; min-width: 240px; line-height: 1.35; }
.lib-table .num { text-align: center; }
.rel { display: inline-block; width: 26px; line-height: 22px; font-weight: 600; text-align: center; font-variant-numeric: tabular-nums; border: 1px solid; }
.rel-5 { background: #1f3f7a; border-color: #1f3f7a; color: #fff; }
.rel-4 { background: #4e6ea8; border-color: #4e6ea8; color: #fff; }
.rel-3 { background: #c9d4ea; border-color: #c9d4ea; color: #1f3f7a; }
.rel-2 { background: #fff; border-color: #c9d4ea; color: #4e6ea8; }
.rel-1 { background: #fff; border-color: var(--border); color: var(--muted); }
.rel-0 { background: #f5e8e8; border-color: var(--danger); color: var(--danger); font-size: 10px; letter-spacing: .04em; width: auto; padding: 0 6px; }
.nowrap { white-space: nowrap; color: var(--muted); font-variant-numeric: tabular-nums; }

/* ---------- modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(20, 20, 18, .4); display: flex; align-items: center; justify-content: center; z-index: 50; padding: 20px; }
.modal[hidden] { display: none; }
.modal-box { background: var(--surface); border: 1px solid var(--border-strong); width: min(820px, 100%); max-height: 90vh; overflow: auto; padding: 28px 32px; position: relative; box-shadow: 0 20px 50px rgba(0,0,0,.18); }
.modal-close { position: absolute; top: 12px; right: 12px; }
.modal h3 { margin: 0 40px 6px 0; font-family: var(--serif); font-size: 21px; line-height: 1.3; }
.modal .authors { color: var(--muted); margin-bottom: 6px; }
.modal dl { display: grid; grid-template-columns: 160px 1fr; margin: 22px 0; border-top: 1px solid var(--border); }
.modal dt, .modal dd { padding: 10px 0; border-bottom: 1px solid var(--border); }
.modal dt { font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); padding-top: 12px; }
.modal dd { margin: 0; white-space: pre-wrap; }
.modal h4 { margin: 0 0 8px; font-size: 11.5px; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.modal .notes { background: var(--surface-2); border-left: 3px solid var(--accent); padding: 12px 16px; white-space: pre-wrap; }
.modal-actions { display: flex; gap: 8px; margin-top: 24px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%) translateY(16px);
  background: var(--text); color: #fff; padding: 10px 18px; max-width: min(560px, 90vw);
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 100;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- side panel tabs ---------- */
.side-tabs { display: flex; border-bottom: 1px solid var(--border); background: var(--surface); flex: 0 0 auto; }
.side-tab {
  flex: 1; height: 40px; background: none; border: 0; border-bottom: 2px solid transparent; margin-bottom: -1px;
  font: inherit; font-size: 13px; font-weight: 500; color: var(--muted); cursor: pointer;
  display: flex; align-items: center; justify-content: center; gap: 8px; border-radius: 0;
}
.side-tab:hover { color: var(--text); }
.side-tab.active { color: var(--text); border-bottom-color: var(--accent); }

/* ---------- AI summary (collapsed by default) ---------- */
.ai-box { border-bottom: 1px solid var(--border); background: var(--surface-2); }
.ai-head {
  width: 100%; display: flex; align-items: center; gap: 10px; padding: 0 18px; height: 44px;
  background: none; border: 0; border-radius: 0; font: inherit; color: var(--text); cursor: pointer; text-align: left;
}
.ai-head:hover { background: var(--hover); }
.ai-title { font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; }
.ai-state { flex: 1; font-size: 12.5px; color: var(--muted); }
.ai-state.ok { color: var(--ok); }
.ai-state.err { color: var(--danger); }
.ai-head .chev { transition: transform .15s; color: var(--muted); }
.ai-head[aria-expanded="true"] .chev { transform: rotate(180deg); }
.ai-body { padding: 4px 18px 16px; font-size: 13.5px; }
.ai-body h5 { margin: 14px 0 6px; font-size: 11px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.ai-body p { margin: 0; line-height: 1.55; }
.ai-body ul { margin: 0; padding-left: 18px; }
.ai-body li { margin-bottom: 4px; line-height: 1.5; }
.ai-body .ai-actions { display: flex; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.ai-body .ai-meta { margin-top: 10px; font-size: 11.5px; color: var(--muted); }
.ai-body .warn { padding: 10px 12px; background: #fff8e6; border-left: 3px solid #c4950f; color: #6b5617; margin-top: 4px; }
.ai-body .error { padding: 10px 12px; background: #fbeaea; border-left: 3px solid var(--danger); color: #7a1c1c; margin-top: 4px; white-space: pre-wrap; }

/* ---------- handwritten notes ---------- */
.hw { padding: 4px 18px 22px; }
.hw-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 10px; }
.field-label { font-size: 11.5px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); }
.hw-actions { display: flex; gap: 6px; }
.hw-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 8px; }
.hw-grid:empty::before { content: "No handwritten notes yet."; color: var(--muted); font-size: 13px; grid-column: 1 / -1; }
.hw-item {
  position: relative; aspect-ratio: 3 / 4; border: 1px solid var(--border); background: #fff; cursor: pointer; overflow: hidden;
  display: flex; align-items: center; justify-content: center; padding: 0;
}
.hw-item:hover { border-color: var(--accent); }
.hw-item img { width: 100%; height: calc(100% - 18px); object-fit: contain; object-position: center top; display: block; align-self: flex-start; }
.hw-item .i { width: 28px; height: 28px; color: var(--muted); }
.hw-item .kind { position: absolute; left: 0; bottom: 0; right: 0; font-size: 10px; text-transform: uppercase; letter-spacing: .05em; background: rgba(255,255,255,.9); color: var(--muted); padding: 2px 6px; text-align: left; border-top: 1px solid var(--border); }

/* ---------- references ---------- */
.refs-tools { position: sticky; top: 0; z-index: 2; background: var(--surface); padding: 12px 18px; border-bottom: 1px solid var(--border); }
.refs-tools .search { width: 100%; }
.ref-list .empty-note { color: var(--muted); padding: 24px 18px; font-size: 13.5px; }
.ref { border-bottom: 1px solid var(--border); }
.ref.flash { animation: refFlash 1.2s ease; }
@keyframes refFlash {
  0%, 100% { background: transparent; }
  20%, 55% { background: var(--accent-soft); }
}
.ref-head {
  display: grid; grid-template-columns: 38px 1fr; gap: 0 8px; width: 100%; padding: 11px 18px 11px 12px;
  background: none; border: 0; border-radius: 0; font: inherit; color: var(--text); text-align: left; cursor: pointer;
}
.ref-head:hover { background: var(--surface-2); }
.ref.open .ref-head { background: var(--surface-2); }
.ref-label { grid-row: span 2; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; text-align: right; padding-top: 2px; }
.ref-title { font-family: var(--serif); font-weight: 600; font-size: 14px; line-height: 1.3; }
.ref-sub { font-size: 12px; color: var(--muted); margin-top: 3px; display: flex; flex-wrap: wrap; gap: 4px 8px; align-items: center; }
.rel-tag { font-size: 10px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; border: 1px solid var(--border-strong); padding: 0 5px; color: var(--muted); }
.rel-tag.key { border-color: var(--accent); color: var(--accent); }
.rel-tag.lib { background: var(--accent); border-color: var(--accent); color: #fff; }
.ref-body { padding: 4px 18px 16px 58px; background: var(--surface-2); font-size: 13.5px; }
.ref-body p { margin: 0 0 10px; line-height: 1.55; }
.ref-body .ctx { margin: 0 0 10px; padding: 8px 12px; border-left: 3px solid var(--border-strong); color: var(--muted); font-size: 12.5px; font-style: italic; background: var(--surface); }
.ref-body .ctx-h { font-size: 11px; font-weight: 600; letter-spacing: .05em; text-transform: uppercase; color: var(--muted); margin: 0 0 6px; }
.ref-actions { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.ref-actions select { height: 32px; font-size: 13px; padding-top: 0; padding-bottom: 0; }

/* ---------- drawing pad ---------- */
.pad { position: fixed; inset: 0; z-index: 60; background: #e4e4e0; display: flex; flex-direction: column; }
.pad[hidden] { display: none; }
.pad-bar {
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap; padding: 8px 12px;
  padding-top: calc(8px + env(safe-area-inset-top)); background: var(--surface); border-bottom: 1px solid var(--border);
}
.seg { display: flex; align-items: center; gap: 2px; padding-right: 12px; border-right: 1px solid var(--border); }
.seg .icon-btn.on { background: var(--accent-soft); color: var(--accent); border-color: var(--accent); }
.swatch { width: 30px; height: 30px; border: 1px solid var(--border); background: var(--surface); padding: 5px; cursor: pointer; border-radius: 0; }
.swatch::before { content: ""; display: block; width: 100%; height: 100%; background: var(--c); }
.swatch.on { border-color: var(--text); box-shadow: inset 0 0 0 1px var(--text); }
.size { width: 34px; height: 34px; border: 1px solid transparent; background: none; cursor: pointer; display: flex; align-items: center; justify-content: center; border-radius: 0; }
.size span { display: block; width: var(--s); height: var(--s); background: var(--text); }
.size.on { border-color: var(--text); }
.pad-stage { flex: 1; position: relative; overflow: hidden; touch-action: none; }
.pad-stage canvas { position: absolute; left: 50%; top: 16px; transform: translateX(-50%); background: transparent; touch-action: none; }
#padBase { background: #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.08), 0 2px 8px rgba(0,0,0,.1); }

/* ---------- attachment viewer ---------- */
.viewer-box { width: min(980px, 100%); }
.viewer-body { display: flex; justify-content: center; background: var(--surface-2); border: 1px solid var(--border); min-height: 200px; margin-top: 30px; }
.viewer-body img { max-width: 100%; max-height: 70vh; display: block; }
.viewer-body iframe { width: 100%; height: 70vh; border: 0; }

/* ---------- auth pages / top bar ---------- */
.auth-view.active { display: flex; align-items: center; justify-content: center; padding: 20px; }
.auth-card { width: min(380px, 100%); display: flex; flex-direction: column; gap: 12px; }
.auth-card input { height: 42px; }
.auth-card .btn { height: 42px; }
.hint-text { margin: 0; color: var(--muted); }
.topbar .spacer { flex: 1; }
.logout-form { display: flex; align-items: center; margin: 0; }
.tag.ai { color: var(--ok); border-color: var(--ok); }
.tag.ai-run { color: #8a6200; border-color: #c4950f; }
.tag.ai-err { color: var(--danger); border-color: var(--danger); }

/* detail modal extras */
.modal .ai-detail { margin: 18px 0; border: 1px solid var(--border); }
.modal .ai-detail .ai-head { height: 40px; }
.modal .hw-grid { margin-top: 8px; }

/* ---------- touch devices / tablets ---------- */
@media (pointer: coarse) {
  input[type=text], input[type=search], input:not([type]), select, textarea { font-size: 16px; }
  .btn, .add-row > input, .add-row select, .add-row .btn, .file-drop, .lib-toolbar select, .lib-toolbar .btn, .search input,
  .discover-row select, .discover-row .btn { height: 44px; }
  .btn.small { height: 40px; }
  .icon-btn { width: 44px; height: 44px; }
  .icon-btn.bordered { width: 44px; height: 44px; }
  .doc-toolbar { height: 54px; }
  .page-ind input { height: 36px; font-size: 16px; }
  .stack-item select { height: 40px; font-size: 16px; }
  .rating button { height: 46px; font-size: 16px; }
  .chip { padding: 7px 14px; font-size: 14px; }
  .side-tab { height: 48px; font-size: 14px; }
  .ai-head { height: 52px; }
  .ref-head { padding-top: 14px; padding-bottom: 14px; }
  .swatch { width: 40px; height: 40px; }
  .size { width: 44px; height: 44px; }
  .lib-table td, .lib-table th { padding: 13px 12px; }
  .search .i { top: 13px; }
  .splitter { flex-basis: 12px; background: var(--surface-2); border-left: 1px solid var(--border); border-right: 1px solid var(--border); position: relative; }
  .splitter::after { content: ""; position: absolute; top: 50%; left: 3px; width: 4px; height: 40px; margin-top: -20px; background: var(--border-strong); }
}

/* tablet landscape */
@media (max-width: 1200px) {
  .reader-side { flex-basis: 360px; }
  .search { width: 260px; }
  .doc-title { display: none; }
  .doc-toolbar { justify-content: space-between; }
}

/* tablet portrait / phones: classification panel becomes a slide-over drawer */
@media (max-width: 900px) {
  .topbar { gap: 16px; padding: 0 14px; }
  .page { padding: 18px 14px 40px; }
  .add-row { flex-wrap: wrap; }
  .add-row > input { flex: 1 1 100%; }
  .or { display: none; }
  .file-drop { flex: 1 1 auto; }
  .discover-q { flex: 1 1 100%; }
  .discover-actions { margin-top: 4px; }
  .splitter { display: none; }
  .reader-side {
    position: absolute; top: 0; right: 0; bottom: 0; z-index: 10;
    width: min(460px, 94vw); max-width: none; border-left: 1px solid var(--border-strong);
    box-shadow: -10px 0 30px rgba(0,0,0,.15);
    transform: translateX(105%); transition: transform .22s ease;
  }
  .view.reader { position: relative; }
  .reader.side-open .reader-side { transform: none; }
  .drawer-close { display: inline-flex; }
  .side-head .title-input { width: calc(100% - 40px) !important; }
  .reader.side-hidden .reader-side { display: flex; }
  .reader:not(.side-open) #rToggleSide { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
  .search { width: 100%; }
  .stats { flex-wrap: wrap; }
  .stat { flex: 1 1 25%; min-width: 0; padding: 12px 14px; }
  .chips { flex-basis: 100%; border-top: 1px solid var(--border); }
  .modal-box { padding: 24px 20px; }
  .modal dl { grid-template-columns: 1fr; }
  .modal dt { border-bottom: 0; padding-bottom: 0; }
  .stack-item { flex-wrap: wrap; row-gap: 10px; }
  .stack-item .info { flex-basis: calc(100% - 60px); }
  .stack-item .actions { margin-left: 38px; }
}
@media (max-width: 560px) {
  .brand span { display: none; }
  .tool-group .sep, #rFit, .annot-tools .annot-colors, #annotRedo { display: none; }
  .doc-toolbar { overflow-x: auto; }
  .stat { flex-basis: 50%; }
}
