:root {
  --bg: #faf8f4;
  --surface: #ffffff;
  --ink: #1f1d1a;
  --muted: #77716a;
  --faint: #a9a39b;
  --line: #e7e2d9;
  --accent: #b5492c;
  --accent-ink: #ffffff;
  --serif: "Iowan Old Style", Charter, "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", sans-serif;
  --reader-font: var(--serif);
  --reader-size: 20px;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f4ecd8; --surface: #fbf5e6; --ink: #3b3225; --muted: #7d6f58; --faint: #a5977e; --line: #e2d6b9; --accent: #a4502a;
}
:root[data-theme="dark"] {
  --bg: #171614; --surface: #201f1c; --ink: #e8e4dc; --muted: #a09a90; --faint: #6f6a62; --line: #2f2d29; --accent: #e0805f; --accent-ink: #171614;
  color-scheme: dark;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme]) {
    --bg: #171614; --surface: #201f1c; --ink: #e8e4dc; --muted: #a09a90; --faint: #6f6a62; --line: #2f2d29; --accent: #e0805f; --accent-ink: #171614;
    color-scheme: dark;
  }
}
:root[data-font="sans"] { --reader-font: var(--sans); }

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 16px/1.5 var(--sans); -webkit-font-smoothing: antialiased;
  padding-bottom: env(safe-area-inset-bottom);
}
button, input, textarea { font: inherit; color: inherit; }
button { cursor: pointer; background: none; border: 0; padding: 0; }
a { color: inherit; }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; border-radius: 4px; }

.wrap { max-width: 720px; margin: 0 auto; padding: 0 20px; }

/* ---------- top bar ---------- */
.bar {
  position: sticky; top: 0; z-index: 5;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.4) blur(12px); -webkit-backdrop-filter: saturate(1.4) blur(12px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
.bar .wrap { display: flex; align-items: center; gap: 6px; height: 56px; }
.brand { display: flex; align-items: center; gap: 9px; font: 600 20px/1 var(--serif); letter-spacing: -0.01em; text-decoration: none; margin-right: auto; }
.brand img { width: 26px; height: 26px; border-radius: 6px; }
.icon-btn {
  width: 40px; height: 40px; display: grid; place-items: center; border-radius: 10px; color: var(--muted);
  text-decoration: none; flex: none;
}
.icon-btn:hover { background: color-mix(in srgb, var(--ink) 7%, transparent); color: var(--ink); }
.icon-btn.on { color: var(--accent); }
.icon-btn svg { width: 21px; height: 21px; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  height: 40px; padding: 0 16px; border-radius: 10px; font-weight: 600; font-size: 15px;
  background: var(--accent); color: var(--accent-ink); text-decoration: none; white-space: nowrap;
}
.btn:hover { filter: brightness(1.06); }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn.quiet { background: transparent; color: var(--ink); border: 1px solid var(--line); }
.btn.quiet:hover { background: color-mix(in srgb, var(--ink) 5%, transparent); filter: none; }
.btn svg { width: 18px; height: 18px; }

/* ---------- add / search ---------- */
.field {
  width: 100%; height: 44px; padding: 0 14px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface); font-size: 16px; min-width: 0;
}
.field::placeholder { color: var(--faint); }
.add-row { display: flex; gap: 8px; padding: 14px 0 2px; }
.tools { display: flex; align-items: center; gap: 4px; padding: 14px 0 0; border-bottom: 1px solid var(--line); }
.tab {
  padding: 8px 12px 11px; font-weight: 600; font-size: 15px; color: var(--muted);
  border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap;
}
.tab:hover { color: var(--ink); }
.tab[aria-selected="true"] { color: var(--ink); border-bottom-color: var(--accent); }
.tab .count { font-weight: 500; color: var(--faint); margin-left: 5px; font-variant-numeric: tabular-nums; }
.search { margin-left: auto; position: relative; flex: 0 1 220px; min-width: 0; margin-bottom: 6px; }
.search input { height: 36px; padding-left: 34px; font-size: 15px; border-radius: 18px; }
.search svg { position: absolute; left: 11px; top: 9px; width: 18px; height: 18px; color: var(--faint); pointer-events: none; }

/* ---------- list ---------- */
.list { list-style: none; margin: 0; padding: 0 0 80px; }
.item { display: flex; gap: 18px; padding: 20px 0 12px; border-bottom: 1px solid var(--line); }
.item-text { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 6px; }
.item-main { text-decoration: none; min-width: 0; display: block; }
.item h2 { margin: 0 0 6px; font: 600 20px/1.28 var(--serif); letter-spacing: -0.005em; text-wrap: pretty; overflow-wrap: anywhere; }
.item-main:hover h2 { color: var(--accent); }
.item .excerpt {
  margin: 0; color: var(--muted); font-size: 15px; line-height: 1.45;
  display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden;
}
.thumb { width: 92px; height: 92px; border-radius: 8px; object-fit: cover; background: var(--line); flex: none; }
.item-foot { display: flex; align-items: center; gap: 2px; min-width: 0; margin: auto 0 0 -9px; }
.meta { color: var(--faint); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin: 0 auto 0 9px; min-width: 0; }
.meta .site { color: var(--muted); font-weight: 500; }
.item-foot .icon-btn { width: 36px; height: 36px; color: var(--faint); }
.item-foot .icon-btn svg { width: 18px; height: 18px; }
.item-foot .icon-btn.on { color: var(--accent); }
.progress { height: 3px; width: 44px; border-radius: 2px; background: var(--line); overflow: hidden; margin-right: 8px; flex: none; }
.progress i { display: block; height: 100%; background: var(--accent); }
.empty { text-align: center; color: var(--muted); padding: 72px 20px; }
.empty h2 { font: 600 22px/1.3 var(--serif); color: var(--ink); margin: 0 0 8px; }
.empty p { margin: 0 auto; max-width: 380px; }

/* ---------- reader ---------- */
.reader-bar .wrap { max-width: 760px; }
.reader-bar .spacer { margin-right: auto; }
.reader-progress { position: absolute; left: 0; bottom: -1px; height: 2px; background: var(--accent); width: 0; }
.reader { max-width: 700px; margin: 0 auto; padding: 36px 22px 120px; }
.reader header { margin-bottom: 30px; padding-bottom: 22px; border-bottom: 1px solid var(--line); }
.reader h1 { font: 700 clamp(28px, 5.4vw, 38px)/1.16 var(--serif); letter-spacing: -0.015em; margin: 0 0 14px; text-wrap: balance; overflow-wrap: anywhere; }
.byline { color: var(--muted); font-size: 14px; display: flex; flex-wrap: wrap; gap: 4px 8px; }
.byline a { color: var(--accent); text-decoration: none; }
.byline a:hover { text-decoration: underline; }
.prose { font: var(--reader-size)/1.62 var(--reader-font); overflow-wrap: break-word; }
.prose > :first-child { margin-top: 0; }
.prose p, .prose ul, .prose ol, .prose blockquote, .prose pre, .prose figure, .prose table { margin: 0 0 1.15em; }
.prose h1, .prose h2, .prose h3, .prose h4 { font-family: var(--reader-font); line-height: 1.25; margin: 1.7em 0 0.55em; letter-spacing: -0.01em; }
.prose h1, .prose h2 { font-size: 1.4em; }
.prose h3 { font-size: 1.18em; }
.prose h4 { font-size: 1em; }
.prose a { color: var(--accent); text-decoration-thickness: 1px; text-underline-offset: 2px; }
.prose img, .prose video, .prose iframe { max-width: 100%; height: auto; border-radius: 6px; display: block; margin: 0 auto; }
.prose iframe { width: 100%; aspect-ratio: 16 / 9; border: 0; }
.prose figure { margin-left: 0; margin-right: 0; }
.prose figcaption { font: 13.5px/1.45 var(--sans); color: var(--muted); margin-top: 8px; text-align: center; }
.prose blockquote { margin-left: 0; margin-right: 0; padding-left: 1em; border-left: 3px solid var(--accent); color: var(--muted); font-style: italic; }
.prose pre { background: var(--surface); border: 1px solid var(--line); border-radius: 8px; padding: 14px; overflow-x: auto; font: 14px/1.5 ui-monospace, Menlo, monospace; }
.prose code { font: 0.86em ui-monospace, Menlo, monospace; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: 0.1em 0.3em; border-radius: 4px; }
.prose pre code { background: none; padding: 0; font-size: inherit; }
.prose hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }
.prose table { display: block; overflow-x: auto; border-collapse: collapse; font: 15px/1.4 var(--sans); }
.prose td, .prose th { border: 1px solid var(--line); padding: 6px 10px; text-align: left; }
.prose li { margin-bottom: 0.35em; }
.note { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 22px; color: var(--muted); font-size: 16px; }
.note .btn { margin-top: 14px; }
.end { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin-top: 56px; padding-top: 28px; border-top: 1px solid var(--line); }

.popover {
  position: absolute; right: 12px; top: calc(100% + 6px); width: 260px; z-index: 6;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 14px;
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.16); display: grid; gap: 12px;
}
.seg { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { height: 38px; font-size: 14px; font-weight: 600; color: var(--muted); border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed="true"] { background: color-mix(in srgb, var(--accent) 14%, transparent); color: var(--ink); }
.swatch { width: 18px; height: 18px; border-radius: 50%; border: 1px solid var(--faint); display: inline-block; vertical-align: -4px; }

/* ---------- highlights ---------- */
.prose mark, mark[data-h] { background: color-mix(in srgb, #f5c542 42%, transparent); color: inherit; border-radius: 2px; padding: 0.08em 0; cursor: pointer; -webkit-box-decoration-break: clone; box-decoration-break: clone; }
mark[data-note] { border-bottom: 2px solid var(--accent); }
.hl-btn {
  position: fixed; z-index: 15; height: 38px; padding: 0 16px; border-radius: 19px; font: 600 14.5px/1 var(--sans);
  background: var(--ink); color: var(--bg); box-shadow: 0 6px 20px rgba(0, 0, 0, 0.25);
}
.hl-pop {
  position: fixed; z-index: 15; width: min(340px, calc(100vw - 16px)); background: var(--surface); border: 1px solid var(--line);
  border-radius: 14px; padding: 12px; box-shadow: 0 12px 34px rgba(0, 0, 0, 0.18); display: grid; gap: 10px; font: 15px/1.4 var(--sans);
}
.hl-pop textarea { height: auto; padding: 10px 12px; resize: vertical; font-size: 15px; }
.hl-pop .btn { height: 36px; font-size: 14px; padding: 0 12px; }
.hl-count { font-size: 12px; font-weight: 700; color: var(--ink); background: color-mix(in srgb, #f5c542 55%, transparent); border-radius: 9px; padding: 1px 7px; margin-right: 6px; flex: none; }
.stack { display: grid; gap: 10px; }
.panel a.link { text-decoration: none; }

/* ---------- gate + settings ---------- */
.gate { min-height: 100dvh; display: grid; place-items: center; padding: 24px; }
.card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 30px 26px; text-align: center; }
.card img { width: 60px; height: 60px; border-radius: 14px; }
.card h1 { font: 700 26px/1.2 var(--serif); margin: 16px 0 6px; }
.card p { color: var(--muted); margin: 0 0 20px; font-size: 15px; }
.card form { display: grid; gap: 10px; }
.card .btn { height: 46px; }
.error { color: #c0392b; font-size: 14px; min-height: 20px; margin: 0; }

.settings { padding-top: 26px; padding-bottom: 100px; }
.settings h1 { font: 700 30px/1.2 var(--serif); margin: 0 0 22px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 22px; margin-bottom: 16px; }
.panel h2 { font: 600 19px/1.3 var(--serif); margin: 0 0 6px; }
.panel > p { color: var(--muted); margin: 0 0 14px; font-size: 15px; }
.panel ol { margin: 0 0 16px; padding-left: 22px; color: var(--ink); font-size: 15px; }
.panel ol li { margin-bottom: 7px; }
.panel code { font: 13px ui-monospace, Menlo, monospace; background: color-mix(in srgb, var(--ink) 7%, transparent); padding: 2px 6px; border-radius: 5px; word-break: break-all; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.token-box { display: flex; gap: 8px; margin: 12px 0 4px; }
.token-box input { font: 13px ui-monospace, Menlo, monospace; }
.bookmarklet { cursor: grab; }
.small { font-size: 13px; color: var(--muted); }
.token-list { list-style: none; margin: 14px 0 0; padding: 0; font-size: 14px; }
.token-list li { display: flex; justify-content: space-between; align-items: center; padding: 8px 0; border-top: 1px solid var(--line); color: var(--muted); }
.link { color: var(--accent); font-weight: 600; font-size: 14px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; left: 50%; bottom: calc(26px + env(safe-area-inset-bottom)); transform: translate(-50%, 20px);
  background: var(--ink); color: var(--bg); padding: 11px 18px; border-radius: 22px; font-size: 14.5px; font-weight: 500;
  opacity: 0; pointer-events: none; transition: opacity 0.2s, transform 0.2s; z-index: 20; max-width: calc(100vw - 32px);
  display: flex; gap: 14px; align-items: center;
}
#toast.show { opacity: 1; transform: translate(-50%, 0); pointer-events: auto; }
#toast button { color: inherit; font-weight: 700; text-decoration: underline; }

@media (max-width: 560px) {
  .wrap { padding: 0 16px; }
  .item h2 { font-size: 18px; }
  .thumb { width: 72px; height: 72px; }
  .tools { flex-wrap: wrap; }
  .search { flex: 1 0 100%; order: -1; margin: 0 0 8px; }
  .tab { padding-left: 10px; padding-right: 10px; }
  .reader { padding: 26px 18px 110px; }
  .hide-narrow { display: none; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
