:root {
  --bg: #f7f5f0;
  --surface: #ffffff;
  --text: #1f2328;
  --muted: #5d636b;
  --line: #dcd8cf;
  --accent: #2f4a7a;
  --accent-text: #ffffff;
  --accent-soft: #e5ebf5;
  --danger: #a3271f;
  --mark: #ffe58a;
  --highlight: rgb(255 214 64 / 0.55);
  --warn: #8a5a00;
  --radius: 12px;
  --tabbar: 60px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #15171a;
    --surface: #1e2125;
    --text: #e8e6e1;
    --muted: #a3a8ae;
    --line: #353a40;
    --accent: #8fb0e8;
    --accent-text: #0e1420;
    --accent-soft: #24304a;
    --danger: #f08a80;
    --mark: #6b5a12;
    --highlight: rgb(255 200 40 / 0.45);
    --warn: #e7b357;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  padding-bottom: calc(var(--tabbar) + env(safe-area-inset-bottom));
}
main { max-width: 760px; margin: 0 auto; padding: 16px 16px 32px; outline: none; }
/* Reading screens use the full width of large screens. */
body[data-view="reading"] main, body[data-view="web"] main { max-width: 1680px; }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 4px 0 8px; }
h2 { font-size: 1.1rem; margin: 20px 0 8px; }
a { color: var(--accent); }
mark { background: var(--mark); color: inherit; border-radius: 2px; }
small { color: var(--muted); }
.skip { position: absolute; left: -999px; }
.skip:focus { left: 8px; top: 8px; z-index: 10; background: var(--surface); padding: 8px; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }

.row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.row.end { justify-content: flex-end; }
.stack { display: grid; gap: 14px; }
.hint, .meta { color: var(--muted); font-size: 0.9rem; margin: 4px 0; }
.empty { color: var(--muted); padding: 16px 0; }
.break { overflow-wrap: anywhere; }

.btn {
  min-height: 44px;
  padding: 8px 16px;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}
.btn.small { min-height: 36px; padding: 4px 12px; font-size: 0.9rem; }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
.btn.danger { color: var(--danger); }
.btn.block { width: 100%; }
.btn:disabled { opacity: 0.6; }
.icon-btn {
  min-width: 44px; min-height: 44px;
  border: none; background: none; color: var(--muted);
  font-size: 1.4rem; cursor: pointer; border-radius: 8px;
}

input, select, textarea {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  min-height: 44px;
  width: 100%;
}
textarea { resize: vertical; }
input[type="checkbox"] { width: 20px; height: 20px; min-height: 0; }
.field { display: grid; gap: 4px; }
.field > span { font-weight: 600; font-size: 0.95rem; }

/* Tab bar */
.tabbar {
  position: fixed; inset: auto 0 0 0; z-index: 5;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.tabbar a {
  flex: 1; min-height: var(--tabbar);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  color: var(--muted); text-decoration: none; font-size: 0.8rem;
}
.tabbar a span { font-size: 1.3rem; line-height: 1; }
.tabbar a[aria-current="page"] { color: var(--accent); font-weight: 600; }

/* Sources list */
.page-head .row { margin-top: 8px; }
.source-list { list-style: none; margin: 8px 0; padding: 0; display: grid; gap: 8px; }
.source-row {
  display: grid; grid-template-columns: 44px 28px 1fr 44px; align-items: center;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 4px; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none;
}
.source-row.pinned { border-color: var(--accent); }
.source-row.archived { opacity: 0.6; }
.source-row.dragging, .source-row.grabbed { box-shadow: 0 6px 20px rgb(0 0 0 / 0.2); border-color: var(--accent); position: relative; z-index: 2; }
.handle {
  min-height: 44px; border: none; background: none; color: var(--muted);
  font-size: 1.4rem; cursor: grab; touch-action: none; border-radius: 8px;
}
.handle[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); }
.rank { color: var(--muted); font-variant-numeric: tabular-nums; text-align: center; }
.source-main { display: grid; padding: 6px 4px; color: inherit; text-decoration: none; min-width: 0; }
.source-main strong, .source-main small { overflow-wrap: anywhere; }

/* Cards */
.card-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 8px; }
.card {
  display: grid; gap: 4px; padding: 12px 14px;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  color: inherit; text-decoration: none;
}
.card .snippet { color: var(--text); opacity: 0.85; }
.badge { font-size: 0.75rem; padding: 2px 8px; border-radius: 999px; background: var(--accent-soft); color: var(--text); }
.badge.warn { color: var(--warn); }
.status-done { opacity: 0.8; }
.link-list { padding-left: 20px; }
.link-list li { margin: 8px 0; }

/* Search */
.search-bar input { font-size: 1.1rem; border-radius: 999px; padding-left: 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 10px 0; }
.chips select { width: auto; min-height: 36px; padding: 4px 10px; border-radius: 999px; font-size: 0.9rem; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 4px 12px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); font-size: 0.9rem; min-height: 36px; }

/* Add */
.segmented { display: flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; margin: 8px 0 16px; }
.segmented button { flex: 1; min-height: 44px; border: none; background: var(--surface); color: var(--text); font: inherit; cursor: pointer; }
.segmented button[aria-selected="true"] { background: var(--accent); color: var(--accent-text); font-weight: 600; }

/* Reading */
.reading-head h1 { margin-bottom: 2px; }
.back { display: inline-block; padding: 8px 0; text-decoration: none; }
.reading-layout { display: grid; gap: 16px; margin-top: 12px; }
.reader {
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius);
  /* Nearly a full screen high: scroll the page past the title to read full-screen. */
  height: calc(100dvh - var(--tabbar) - 24px); min-height: 320px; overflow: auto; outline: none;
}
.text-reader {
  white-space: pre-wrap; padding: 16px 32px; max-width: 120ch; margin: 0 auto;
  font-family: Georgia, "Times New Roman", serif; font-size: 1.08rem; line-height: 1.7; overflow-wrap: break-word;
}
/* Phones: the reader runs edge to edge. */
@media (max-width: 899px) {
  body[data-view="reading"] main, body[data-view="web"] main { padding-left: 0; padding-right: 0; }
  body[data-view="reading"] .reading-head, body[data-view="web"] .reading-head { padding: 0 16px; }
  .reader { border-left: none; border-right: none; border-radius: 0; }
  .text-reader { padding: 14px 16px; font-size: 1.05rem; }
}
.pdf { display: grid; justify-items: center; gap: 8px; padding: 8px 0; }
.pdf-page { position: relative; background: #fff; box-shadow: 0 1px 4px rgb(0 0 0 / 0.15); max-width: 100%; }
.pdf-page canvas { display: block; max-width: 100%; height: auto !important; }

/* Annotation layers. The text layer is invisible text placed over the page
   image so it can be selected (rules follow pdf.js's own viewer CSS). */
.highlight-layer { position: absolute; inset: 0; pointer-events: none; }
.highlight { position: absolute; background: var(--highlight); mix-blend-mode: multiply; border-radius: 2px; }
.textLayer {
  position: absolute; inset: 0; overflow: clip; opacity: 1; line-height: 1;
  text-align: initial; -webkit-text-size-adjust: none; text-size-adjust: none;
  forced-color-adjust: none; transform-origin: 0 0; caret-color: CanvasText; z-index: 1;
}
.textLayer :is(span, br) { color: transparent; position: absolute; white-space: pre; cursor: text; transform-origin: 0% 0%; }
.textLayer span.markedContent { top: 0; height: 0; }
.textLayer ::selection { background: rgb(47 74 122 / 0.3); }
.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; }
.text-reader mark.highlight { background: var(--highlight); color: inherit; cursor: pointer; border-radius: 2px; }

.selection-bar {
  position: fixed; left: 50%; transform: translateX(-50%); z-index: 7;
  bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom));
  display: flex; gap: 8px; padding: 8px; border-radius: 999px;
  background: var(--surface); border: 1px solid var(--line); box-shadow: 0 6px 20px rgb(0 0 0 / 0.25);
}
.selection-bar[hidden] { display: none; }
.link-card { padding: 24px 16px; display: grid; gap: 8px; justify-items: start; }

.notes-panel { display: none; }
.notes-toggle {
  position: fixed; right: 16px; bottom: calc(var(--tabbar) + 16px + env(safe-area-inset-bottom)); z-index: 4;
  box-shadow: 0 4px 14px rgb(0 0 0 / 0.25);
}
body.notes-open .notes-panel {
  display: block; position: fixed; inset: 15dvh 0 calc(var(--tabbar) + env(safe-area-inset-bottom)) 0; z-index: 6;
  background: var(--bg); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
  box-shadow: 0 -6px 24px rgb(0 0 0 / 0.2); overflow: auto; padding: 0 16px 24px;
}
body.notes-open .notes-toggle { display: none; }
.panel-head { position: sticky; top: 0; background: var(--bg); display: flex; align-items: center; gap: 8px; padding: 12px 0; z-index: 1; }
.panel-head h2 { margin: 0; flex: 1; }
.note { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px; margin-bottom: 10px; display: grid; gap: 8px; }
.note.editing { border-color: var(--accent); }
.note.flash { animation: flash 1.2s ease-out; }
@keyframes flash { from { box-shadow: 0 0 0 4px var(--highlight); } to { box-shadow: none; } }
.note-quote { margin: 0; padding: 6px 10px; border-left: 3px solid var(--line); font-family: Georgia, serif; overflow-wrap: anywhere; }
.note-quote.highlighted { border-left-color: #e0b100; background: var(--highlight); border-radius: 0 6px 6px 0; }
.note-quote-inline { font-family: Georgia, serif; }
.note-source {
  display: grid; text-align: left; gap: 2px; width: 100%;
  border: none; border-left: 3px solid var(--accent); background: var(--accent-soft); color: var(--text);
  padding: 6px 10px; border-radius: 6px; font: inherit; font-size: 0.85rem; cursor: pointer;
}
.note-source span { color: var(--muted); overflow-wrap: anywhere; }
.note-body { overflow-wrap: anywhere; }
.note-body > :first-child { margin-top: 0; }
.note-body > :last-child { margin-bottom: 0; }
.note-body blockquote { margin: 8px 0; padding-left: 12px; border-left: 3px solid var(--line); color: var(--muted); }
.note-foot small { flex: 1; }
.save-state { flex: 1; }

@media (min-width: 900px) {
  .reading-layout { grid-template-columns: minmax(0, 1fr) 360px; align-items: start; }
  .notes-panel, body.notes-open .notes-panel {
    display: block; position: static; inset: auto; box-shadow: none; border: none; border-radius: 0;
    height: calc(100dvh - var(--tabbar) - 24px); overflow: auto; padding: 0 0 24px;
  }
  .notes-toggle, .panel-close { display: none; }
}

/* Dialogs and toasts */
dialog.sheet {
  border: none; border-radius: 16px; padding: 20px; width: min(520px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 10px 40px rgb(0 0 0 / 0.3);
}
dialog.sheet::backdrop { background: rgb(0 0 0 / 0.4); }
dialog.sheet h2 { margin-top: 0; }
dialog.menu { display: grid; gap: 2px; padding: 12px; }
dialog.menu[open] { display: grid; }
.menu-item { min-height: 48px; text-align: left; padding: 0 12px; border: none; background: none; color: var(--text); font: inherit; border-radius: 8px; cursor: pointer; }
.menu-item:hover { background: var(--accent-soft); }
.menu-item.danger { color: var(--danger); }
@media (max-width: 599px) {
  dialog.sheet { margin: auto 0 0; width: 100vw; max-width: 100vw; border-radius: 16px 16px 0 0; padding-bottom: calc(20px + env(safe-area-inset-bottom)); }
}
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(var(--tabbar) + 80px); z-index: 20;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 0.9rem; max-width: calc(100vw - 32px);
}

/* Sync (S11) */
.tabbar a { position: relative; }
.badge-dot { position: absolute; top: 6px; left: calc(50% + 8px); min-width: 18px; height: 18px; padding: 0 5px; border-radius: 9px; background: var(--danger); color: #fff; font-size: 0.7rem; line-height: 18px; }
.badge-dot[hidden] { display: none; }
.sync-status { color: var(--muted); }
.note.conflict { border-color: var(--danger); }
.conflict-box { border: 1px dashed var(--danger); border-radius: 8px; padding: 8px 10px; display: grid; gap: 6px; font-size: 0.9rem; }

/* Website results (S14) */
.web-results h3 { font-size: 0.95rem; margin: 14px 0 6px; color: var(--muted); }

/* All notes */
.notes-group h2 { margin: 22px 0 0; font-size: 1.05rem; }
.notes-group h2 a { color: inherit; text-decoration: none; }
.notes-group .meta { margin: 2px 0 8px; }

/* Library */
.chip.on { background: var(--accent); color: var(--accent-text); border-color: var(--accent); }
button.chip { font: inherit; font-size: 0.9rem; cursor: pointer; color: var(--text); }
button.chip.on { color: var(--accent-text); }

.add-row { margin: 10px 0 2px; }
.card-list li.with-action { display: flex; align-items: stretch; gap: 4px; }
.card-list li.with-action > .card { flex: 1; min-width: 0; }
.delete-btn { align-self: center; font-size: 1.15rem; }
.delete-btn:hover { color: var(--danger); background: var(--accent-soft); }
