/* ---------- tokens ---------- */
:root {
  color-scheme: light;
  --bg:        #faf9f7;
  --bg-elev:   #ffffff;
  --bg-sunk:   #f1efeb;
  --ink:       #1b1c1e;
  --ink-2:     #4a4e55;
  --ink-3:     #7c828c;
  --line:      #e3e0da;
  --line-2:    #d2cec6;
  --accent:    #0f6f6a;
  --accent-ink:#ffffff;
  --accent-soft:#e2f0ee;
  --warn:      #a4471f;
  --code-bg:   #f4f2ee;
  --shadow:    0 1px 2px rgba(20,18,14,.06), 0 8px 24px rgba(20,18,14,.06);

  --reading-size: 17px;
  --reading-width: 720px;
  --topbar-h: 0px;
  --sans: -apple-system, BlinkMacSystemFont, "Hiragino Sans", "Noto Sans JP", "Segoe UI", Roboto, sans-serif;
  --serif: "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", Georgia, serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, "Hiragino Sans", monospace;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) { color-scheme: dark;
    --bg:#14161a; --bg-elev:#1b1e24; --bg-sunk:#0f1114; --ink:#e8e9ec; --ink-2:#b3b8c0;
    --ink-3:#858c96; --line:#282c34; --line-2:#39404a; --accent:#5ecfc4; --accent-ink:#0d1418;
    --accent-soft:#1c2f2e; --warn:#e79a72; --code-bg:#12151a;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 28px rgba(0,0,0,.35);
  }
}
:root[data-theme="dark"] { color-scheme: dark;
  --bg:#14161a; --bg-elev:#1b1e24; --bg-sunk:#0f1114; --ink:#e8e9ec; --ink-2:#b3b8c0;
  --ink-3:#858c96; --line:#282c34; --line-2:#39404a; --accent:#5ecfc4; --accent-ink:#0d1418;
  --accent-soft:#1c2f2e; --warn:#e79a72; --code-bg:#12151a;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 10px 28px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
/* our display rules would otherwise beat the UA's [hidden] rule */
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font-family: var(--sans); font-size: 16px; line-height: 1.7;
  -webkit-font-smoothing: antialiased; overflow-wrap: anywhere;
  padding-bottom: env(safe-area-inset-bottom);
}
a { color: inherit; }
svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.9; stroke-linecap: round; stroke-linejoin: round; }
button { font: inherit; color: inherit; }
::selection { background: var(--accent-soft); }

/* ---------- top bar ---------- */
#progress {
  position: fixed; inset: 0 auto auto 0; height: 2px; width: 0;
  background: var(--accent); z-index: 60; transition: width .1s linear; opacity: 0;
}
#progress.on { opacity: 1; }

#topbar {
  position: sticky; top: 0; z-index: 40;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: saturate(1.6) blur(14px);
  border-bottom: 1px solid var(--line);
  padding-top: env(safe-area-inset-top);
}
#topbar.reading .searchbar, #topbar.reading #filters { display: none; }
.bar { display: flex; align-items: center; gap: 8px; padding: 8px 12px; max-width: 1100px; margin: 0 auto; }
.brand { display: flex; align-items: center; gap: 9px; text-decoration: none; margin-right: auto; min-width: 0; }
.brand-mark {
  width: 28px; height: 28px; flex: none; border-radius: 8px; background: var(--accent); color: var(--accent-ink);
  display: grid; place-items: center; font-weight: 700; font-size: 15px; letter-spacing: -.02em;
}
.brand-text { font-weight: 650; font-size: 15px; letter-spacing: -.01em; white-space: nowrap; }
.brand-text em { font-style: normal; color: var(--ink-3); font-weight: 500; margin-left: 6px; }
.bar-actions { display: flex; gap: 4px; }

.icon-btn {
  display: grid; place-items: center; width: 38px; height: 38px; flex: none;
  border: 0; border-radius: 10px; background: transparent; cursor: pointer;
}
.icon-btn:hover { background: var(--bg-sunk); }
.icon-btn.tiny { width: 30px; height: 30px; }
.icon-btn.tiny svg { width: 16px; height: 16px; }
.icon-btn.active { color: var(--accent); }
.icon-btn.active svg { fill: currentColor; stroke: currentColor; }

.searchbar { display: flex; align-items: center; gap: 8px; padding: 0 12px 10px; max-width: 1100px; margin: 0 auto; }
.searchbar { position: relative; }
.search-field { position: relative; display: flex; align-items: center; flex: 1; min-width: 0; }
.search-field .search-icon { position: absolute; left: 12px; color: var(--ink-3); pointer-events: none; }
#q, #tag-q {
  flex: 1; min-width: 0; height: 40px; border: 1px solid var(--line-2); border-radius: 12px;
  background: var(--bg-elev); color: var(--ink); padding: 0 36px 0 38px; font-size: 16px;
  -webkit-appearance: none; appearance: none;
}
#tag-q { padding: 0 12px; height: 36px; margin: 6px 0; width: 100%; }
#q:focus, #tag-q:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
#q::-webkit-search-cancel-button { display: none; }
#q-clear { position: absolute; right: 6px; }

.chip-btn {
  flex: none; height: 40px; padding: 0 14px; border-radius: 12px; cursor: pointer;
  border: 1px solid var(--line-2); background: var(--bg-elev); font-size: 14px; font-weight: 550;
}
.chip-btn[aria-expanded="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }

#filters { padding: 4px 12px 12px; max-width: 1100px; margin: 0 auto; border-top: 1px solid var(--line); }
.filter-group { margin-top: 10px; }
.filter-label { font-size: 12px; font-weight: 650; color: var(--ink-3); letter-spacing: .04em; margin-bottom: 6px; }
.filter-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  border: 1px solid var(--line-2); background: var(--bg-elev); border-radius: 999px;
  padding: 5px 11px; font-size: 13px; cursor: pointer; white-space: nowrap; line-height: 1.4;
}
.chip:hover { border-color: var(--accent); }
.chip .n { color: var(--ink-3); font-size: 11px; margin-left: 5px; }
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.chip[aria-pressed="true"] .n { color: inherit; opacity: .75; }
.chip.remove::after { content: "×"; margin-left: 6px; opacity: .8; }

/* ---------- list ---------- */
#view { max-width: 1100px; margin: 0 auto; padding: 16px 12px 64px; outline: none; }
.view-head { display: flex; align-items: baseline; gap: 10px; margin: 4px 4px 14px; }
.view-head h1 { font-size: 20px; margin: 0; letter-spacing: -.01em; }
.view-head .count { color: var(--ink-3); font-size: 13px; }

.cards { display: grid; gap: 10px; }
@media (min-width: 760px) { .cards { grid-template-columns: 1fr 1fr; } }

.card {
  display: grid; grid-template-columns: 1fr auto; gap: 14px; align-items: start;
  background: var(--bg-elev); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px; text-decoration: none; position: relative; box-shadow: var(--shadow);
}
.card:active { transform: scale(.995); }
.card-body { min-width: 0; }
.card-meta { display: flex; align-items: center; gap: 8px; font-size: 12px; color: var(--ink-3); margin-bottom: 6px; flex-wrap: wrap; }
.card-cat { color: var(--accent); font-weight: 650; }
.card h2 { font-size: 16px; line-height: 1.5; margin: 0 0 6px; letter-spacing: -.01em; font-weight: 650; }
.card p { margin: 0; font-size: 13.5px; color: var(--ink-2); line-height: 1.65;
  display: -webkit-box; -webkit-line-clamp: 2; line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.card-tags { display: flex; gap: 5px; flex-wrap: wrap; margin-top: 9px; }
.card-tags span { font-size: 11px; color: var(--ink-3); background: var(--bg-sunk); border-radius: 6px; padding: 2px 7px; }
.card-thumb { width: 84px; height: 84px; flex: none; border-radius: 10px; object-fit: cover; background: var(--bg-sunk); }
.card-save { position: absolute; top: 8px; right: 8px; opacity: 0; }
.card:hover .card-save, .card-save.active { opacity: 1; }
@media (hover: none) { .card-save { opacity: 1; } }

.skeleton { background: var(--bg-elev); border: 1px solid var(--line); border-radius: 14px; height: 116px;
  animation: pulse 1.4s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: .55 } 50% { opacity: .95 } }

.empty { text-align: center; color: var(--ink-3); padding: 56px 20px; }
.skeleton.tall { height: 200px; }
.empty-action { margin-top: 16px; }
.empty strong { display: block; color: var(--ink); font-size: 16px; margin-bottom: 6px; }
.more-btn {
  display: block; width: 100%; margin: 18px auto 0; max-width: 320px; padding: 12px;
  border-radius: 12px; border: 1px solid var(--line-2); background: var(--bg-elev); cursor: pointer; font-weight: 550;
}
.sentinel { height: 1px; }

/* ---------- article ---------- */
.article { max-width: var(--reading-width); margin: 0 auto; }
.article-head { margin: 6px 0 22px; }
.article-head h1 {
  font-size: clamp(23px, 5.2vw, 31px); line-height: 1.42; letter-spacing: -.02em;
  margin: 8px 0 12px; font-weight: 700;
}
.article-meta { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; font-size: 13px; color: var(--ink-3); }
.article-meta .dot { opacity: .5; }
.article-actions { display: flex; gap: 8px; margin-top: 16px; flex-wrap: wrap; }
.act-btn {
  display: inline-flex; align-items: center; gap: 7px; height: 36px; padding: 0 13px;
  border-radius: 10px; border: 1px solid var(--line-2); background: var(--bg-elev);
  font-size: 13px; font-weight: 550; cursor: pointer; text-decoration: none;
}
.act-btn svg { width: 16px; height: 16px; }
.act-btn.on { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.act-btn.on svg { fill: currentColor; }

.toc { border: 1px solid var(--line); background: var(--bg-elev); border-radius: 12px; margin: 0 0 26px; overflow: hidden; }
.toc summary { cursor: pointer; padding: 11px 14px; font-size: 13px; font-weight: 650; color: var(--ink-2); list-style: none; }
.toc summary::-webkit-details-marker { display: none; }
.toc summary::before { content: "▸ "; color: var(--ink-3); }
.toc[open] summary::before { content: "▾ "; }
.toc ol { margin: 0; padding: 0 16px 12px 30px; font-size: 13.5px; }
.toc li { margin: 5px 0; }
.toc a { color: var(--ink-2); text-decoration: none; }
.toc a:hover { color: var(--accent); }
.toc .lv3 { margin-left: 14px; font-size: 13px; color: var(--ink-3); }

.content { font-size: var(--reading-size); line-height: 1.92; letter-spacing: .012em; }
:root[data-serif="1"] .content { font-family: var(--serif); line-height: 2.0; }
.content > * { margin: 0 0 1.35em; }
.content h2 {
  font-size: 1.32em; line-height: 1.5; margin: 2.2em 0 .8em; letter-spacing: -.01em;
  padding-bottom: .35em; border-bottom: 1px solid var(--line); scroll-margin-top: 70px;
}
.content h3 { font-size: 1.12em; margin: 1.9em 0 .7em; scroll-margin-top: 70px; }
.content h4 { font-size: 1em; color: var(--ink-2); margin: 1.6em 0 .6em; }
.content ul, .content ol { padding-left: 1.5em; }
.content li { margin: .45em 0; }
.content a { color: var(--accent); text-underline-offset: 3px; text-decoration-thickness: 1px; }
.content strong { font-weight: 700; background: linear-gradient(transparent 62%, var(--accent-soft) 62%); padding: 0 1px; }
.content blockquote {
  border-left: 3px solid var(--line-2); padding: .2em 0 .2em 1.1em; color: var(--ink-2); margin-left: 0;
}
.content hr { border: 0; border-top: 1px solid var(--line); margin: 2.4em 0; }
.content table { width: 100%; border-collapse: collapse; font-size: .9em; }
.content figure { margin: 1.8em 0; }
.content figcaption { font-size: .8em; color: var(--ink-3); text-align: center; margin-top: .6em; line-height: 1.6; }
.content img {
  max-width: 100%; height: auto; border-radius: 10px; display: block; margin: 0 auto;
  background: var(--bg-sunk); cursor: zoom-in; border: 1px solid var(--line);
}
.table-wrap, .content > table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
.content th, .content td { border: 1px solid var(--line); padding: 7px 10px; text-align: left; }
.content th { background: var(--bg-sunk); }

/* inline code */
.content :not(pre) > code {
  font-family: var(--mono); font-size: .87em; background: var(--code-bg);
  border: 1px solid var(--line); border-radius: 5px; padding: .1em .38em; overflow-wrap: break-word;
}
/* code block */
.codeblock { position: relative; margin: 1.6em 0; border: 1px solid var(--line); border-radius: 12px; background: var(--code-bg); overflow: hidden; }
.codeblock-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 5px 6px 5px 12px; border-bottom: 1px solid var(--line); background: color-mix(in srgb, var(--code-bg) 60%, var(--bg-sunk));
}
.codeblock-lang { font: 600 11px/1 var(--mono); color: var(--ink-3); text-transform: uppercase; letter-spacing: .06em; }
.copy-btn {
  border: 1px solid transparent; background: transparent; border-radius: 8px; cursor: pointer;
  font-size: 11.5px; font-weight: 600; color: var(--ink-3); padding: 5px 9px; display: inline-flex; align-items: center; gap: 5px;
}
.copy-btn:hover { background: var(--bg-elev); color: var(--ink); }
.copy-btn.done { color: var(--accent); }
.copy-btn svg { width: 14px; height: 14px; }
.codeblock pre { margin: 0; overflow-x: auto; -webkit-overflow-scrolling: touch; padding: 13px 14px; }
.codeblock code { font-family: var(--mono); font-size: 13px; line-height: 1.72; white-space: pre; tab-size: 2; }
.codeblock.prose code { white-space: pre-wrap; overflow-wrap: anywhere; font-family: inherit; font-size: .94em; line-height: 1.85; }
.content > pre { background: var(--code-bg); border: 1px solid var(--line); border-radius: 12px; padding: 13px; overflow-x: auto; }

/* highlight.js tokens, themed by our own variables */
.hljs-comment, .hljs-quote { color: var(--ink-3); font-style: italic; }
.hljs-keyword, .hljs-selector-tag, .hljs-literal, .hljs-section, .hljs-doctag, .hljs-name { color: #b0429b; }
.hljs-string, .hljs-regexp, .hljs-addition, .hljs-attribute, .hljs-meta .hljs-string { color: #1f7a4d; }
.hljs-number, .hljs-symbol, .hljs-bullet, .hljs-variable, .hljs-template-variable, .hljs-link { color: #b5651d; }
.hljs-title, .hljs-title.function_, .hljs-built_in { color: #2059c7; }
.hljs-type, .hljs-class .hljs-title, .hljs-title.class_ { color: #8a6d1f; }
.hljs-attr, .hljs-selector-attr, .hljs-selector-class, .hljs-selector-id { color: #0f6f6a; }
.hljs-meta, .hljs-deletion { color: var(--warn); }
.hljs-emphasis { font-style: italic; } .hljs-strong { font-weight: 700; }
:root[data-theme="dark"], :root:not([data-theme="light"]) {
  --hl-set: 1;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --tk-key:#e08ad0; --tk-str:#7fc99b; --tk-num:#e0a56a; --tk-fn:#79aef7; --tk-type:#d6c07a; --tk-attr:#5ecfc4;
  }
}
:root[data-theme="dark"] { --tk-key:#e08ad0; --tk-str:#7fc99b; --tk-num:#e0a56a; --tk-fn:#79aef7; --tk-type:#d6c07a; --tk-attr:#5ecfc4; }
:root[data-theme="dark"] .hljs-keyword, :root[data-theme="dark"] .hljs-selector-tag, :root[data-theme="dark"] .hljs-literal,
:root[data-theme="dark"] .hljs-section, :root[data-theme="dark"] .hljs-doctag, :root[data-theme="dark"] .hljs-name { color: var(--tk-key); }
:root[data-theme="dark"] .hljs-string, :root[data-theme="dark"] .hljs-regexp, :root[data-theme="dark"] .hljs-addition,
:root[data-theme="dark"] .hljs-attribute { color: var(--tk-str); }
:root[data-theme="dark"] .hljs-number, :root[data-theme="dark"] .hljs-symbol, :root[data-theme="dark"] .hljs-bullet,
:root[data-theme="dark"] .hljs-variable, :root[data-theme="dark"] .hljs-template-variable, :root[data-theme="dark"] .hljs-link { color: var(--tk-num); }
:root[data-theme="dark"] .hljs-title, :root[data-theme="dark"] .hljs-title.function_, :root[data-theme="dark"] .hljs-built_in { color: var(--tk-fn); }
:root[data-theme="dark"] .hljs-type, :root[data-theme="dark"] .hljs-title.class_ { color: var(--tk-type); }
:root[data-theme="dark"] .hljs-attr, :root[data-theme="dark"] .hljs-selector-class, :root[data-theme="dark"] .hljs-selector-id { color: var(--tk-attr); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .hljs-keyword, :root:not([data-theme="light"]) .hljs-selector-tag,
  :root:not([data-theme="light"]) .hljs-literal, :root:not([data-theme="light"]) .hljs-section,
  :root:not([data-theme="light"]) .hljs-doctag, :root:not([data-theme="light"]) .hljs-name { color: var(--tk-key); }
  :root:not([data-theme="light"]) .hljs-string, :root:not([data-theme="light"]) .hljs-regexp,
  :root:not([data-theme="light"]) .hljs-addition, :root:not([data-theme="light"]) .hljs-attribute { color: var(--tk-str); }
  :root:not([data-theme="light"]) .hljs-number, :root:not([data-theme="light"]) .hljs-symbol,
  :root:not([data-theme="light"]) .hljs-bullet, :root:not([data-theme="light"]) .hljs-variable,
  :root:not([data-theme="light"]) .hljs-template-variable, :root:not([data-theme="light"]) .hljs-link { color: var(--tk-num); }
  :root:not([data-theme="light"]) .hljs-title, :root:not([data-theme="light"]) .hljs-title.function_,
  :root:not([data-theme="light"]) .hljs-built_in { color: var(--tk-fn); }
  :root:not([data-theme="light"]) .hljs-type, :root:not([data-theme="light"]) .hljs-title.class_ { color: var(--tk-type); }
  :root:not([data-theme="light"]) .hljs-attr, :root:not([data-theme="light"]) .hljs-selector-class,
  :root:not([data-theme="light"]) .hljs-selector-id { color: var(--tk-attr); }
}

.article-foot { margin: 40px 0 0; padding-top: 22px; border-top: 1px solid var(--line); }
.article-foot .filter-row { margin-bottom: 20px; }
.related h3 { font-size: 14px; color: var(--ink-3); margin: 0 0 10px; letter-spacing: .02em; }

/* ---------- settings sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 70; display: grid; align-items: end; }
.sheet-backdrop { position: absolute; inset: 0; background: rgba(0,0,0,.42); animation: fade .18s ease; }
.sheet-panel {
  position: relative; background: var(--bg-elev); border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
  padding: 6px 18px calc(22px + env(safe-area-inset-bottom)); animation: rise .22s cubic-bezier(.2,.8,.3,1);
  max-height: 88vh; overflow-y: auto; width: 100%; max-width: 520px; margin: 0 auto;
}
@media (min-width: 560px) { .sheet { align-items: center; } .sheet-panel { border-radius: 18px; padding-bottom: 22px; } }
@keyframes fade { from { opacity: 0 } }
@keyframes rise { from { transform: translateY(24px); opacity: .4 } }
.sheet-handle { width: 38px; height: 4px; border-radius: 2px; background: var(--line-2); margin: 8px auto 4px; }
.sheet-panel h2 { font-size: 15px; margin: 8px 0 16px; }
.set-row { display: flex; align-items: center; justify-content: space-between; gap: 14px; margin: 14px 0; font-size: 14px; }
.seg { display: flex; background: var(--bg-sunk); border-radius: 10px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; border-radius: 8px; padding: 6px 11px; font-size: 13px;
  cursor: pointer; color: var(--ink-2); font-weight: 550; white-space: nowrap;
}
.seg button[aria-pressed="true"] { background: var(--bg-elev); color: var(--ink); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.sheet-foot { margin-top: 24px; padding-top: 16px; border-top: 1px solid var(--line); }
.ghost-btn { border: 1px solid var(--line-2); background: transparent; border-radius: 10px; padding: 9px 14px; font-size: 13px; cursor: pointer; }
.hint { font-size: 12px; color: var(--ink-3); margin: 8px 0 0; }

/* ---------- lightbox ---------- */
.lightbox {
  position: fixed; inset: 0; z-index: 80; background: rgba(0,0,0,.92);
  display: grid; place-items: center; padding: 20px; animation: fade .15s ease;
}
.lightbox img { max-width: 100%; max-height: 84vh; object-fit: contain; border-radius: 6px; }
.lb-close { position: absolute; top: calc(10px + env(safe-area-inset-top)); right: 10px; color: #fff; background: rgba(255,255,255,.12); }
.lb-caption { color: #cfd2d6; font-size: 12.5px; text-align: center; margin: 14px 0 0; max-width: 640px; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%); bottom: calc(20px + env(safe-area-inset-bottom));
  background: var(--ink); color: var(--bg); padding: 10px 18px; border-radius: 999px; font-size: 13px;
  z-index: 90; box-shadow: var(--shadow); animation: rise .2s ease;
}
.offline-note {
  background: var(--accent-soft); border: 1px solid var(--accent); color: var(--ink);
  border-radius: 10px; padding: 10px 13px; font-size: 13px; margin-bottom: 14px;
}
@media (prefers-reduced-motion: reduce) { * { animation: none !important; transition: none !important; } }
