:root {
  --bg: #faf9f6;
  --surface: #ffffff;
  --text: #1c1c1c;
  --muted: #6f6c66;
  --line: #e7e4dd;
  --accent: #c2410c;
  --like: #e11d48;
  --link-line: #7cc8ff;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616;
    --surface: #1f1f1f;
    --text: #e8e6e1;
    --muted: #9a968e;
    --line: #2d2c2a;
    --accent: #fb923c;
    --like: #fb7185;
    --link-line: #5ab8ff;
    color-scheme: dark;
  }
}

/* Bookerly: files live in public/fonts (not in git); only downloaded when chosen */
@font-face { font-family: "Bookerly"; src: url("/fonts/bookerly-regular.ttf") format("truetype"); font-weight: 400; font-style: normal; font-display: swap; }
@font-face { font-family: "Bookerly"; src: url("/fonts/bookerly-italic.ttf") format("truetype"); font-weight: 400; font-style: italic; font-display: swap; }
@font-face { font-family: "Bookerly"; src: url("/fonts/bookerly-bold.ttf") format("truetype"); font-weight: 700; font-style: normal; font-display: swap; }
@font-face { font-family: "Bookerly"; src: url("/fonts/bookerly-bold-italic.ttf") format("truetype"); font-weight: 700; font-style: italic; font-display: swap; }

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  line-height: 1.5;
}
[hidden] { display: none !important; }
.muted { color: var(--muted); font-weight: 400; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  max-width: 720px;
  margin: 0 auto;
  padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}
.topbar h1 { margin: 0; font-size: 1.25rem; font-weight: 650; letter-spacing: -0.01em; }
.actions { display: flex; align-items: center; gap: 8px; }
.actions form { margin: 0; }

.icon-btn, .pill {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-size: 0.9rem;
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.icon-btn { width: 38px; height: 38px; border-radius: 50%; }
.pill { height: 38px; padding: 0 14px; border-radius: 999px; }
.icon-btn:hover, .pill:hover { border-color: var(--muted); }
.pill:disabled { opacity: 0.5; cursor: default; }
/* ---------- floating article bar ---------- */
.doc-bar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(16px + env(safe-area-inset-bottom));
  z-index: 20;
  display: flex;
  justify-content: space-between;
  align-items: center;
  max-width: 680px;
  margin: 0 auto;
  padding: 0 16px;
  pointer-events: none;
  transition: transform 0.25s ease, opacity 0.25s ease;
}
.doc-bar.hidden { transform: translateY(calc(100% + 24px)); opacity: 0; }
.doc-bar:not(.hidden) > * { pointer-events: auto; }
.float-btn, .float-pill {
  height: 56px;
  background: color-mix(in srgb, var(--surface) 82%, transparent);
  backdrop-filter: blur(16px) saturate(1.4);
  -webkit-backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid var(--line);
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.18);
  color: var(--text);
}
.float-btn {
  width: 56px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  -webkit-tap-highlight-color: transparent;
}
.float-pill { display: flex; align-items: center; gap: 4px; padding: 0 10px; border-radius: 999px; }
.float-pill button {
  width: 52px;
  height: 52px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: none;
  color: inherit;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.float-pill button:active, .float-btn:active { transform: scale(0.92); }
.float-pill button:disabled { opacity: 0.4; cursor: default; }
#like svg { fill: none; transition: fill 0.15s, color 0.15s; }
#like[aria-pressed="true"] { color: var(--like); }
#like[aria-pressed="true"] svg { fill: currentColor; }

.float-pill .aa { font-family: var(--serif); font-size: 1.35rem; line-height: 1; }
.float-pill .aa[aria-expanded="true"] { background: color-mix(in srgb, var(--text) 12%, transparent); }

/* ---------- text settings panel ---------- */
.type-panel {
  position: fixed;
  z-index: 25;
  right: max(16px, calc((100vw - 680px) / 2 + 16px));
  bottom: calc(16px + 56px + 12px + env(safe-area-inset-bottom));
  width: min(300px, calc(100vw - 32px));
  padding: 8px 16px;
  border-radius: 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: blur(16px) saturate(1.4);
  -webkit-backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid var(--line);
  box-shadow: 0 10px 32px rgb(0 0 0 / 0.22);
}
.font-choice { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; padding: 8px 0; border-bottom: 1px solid var(--line); }
.font-choice button {
  height: 44px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: none;
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.font-choice button[aria-checked="true"] { border-color: var(--accent); color: var(--accent); }
.f-serif { font-family: var(--serif); }
.f-bookerly { font-family: "Bookerly", var(--serif); }
.f-sans { font-family: var(--sans); }
.type-row { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 8px 0; }
.type-row + .type-row { border-top: 1px solid var(--line); }
.type-label { font-size: 0.95rem; color: var(--muted); }
.stepper { display: flex; align-items: center; gap: 6px; }
.stepper button {
  width: 44px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: none;
  color: var(--text);
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.stepper button:active { transform: scale(0.94); }
.stepper button:disabled { opacity: 0.35; cursor: default; }
.step-val { min-width: 2.6em; text-align: center; font-variant-numeric: tabular-nums; font-size: 0.9rem; }
.a-small { font-family: var(--serif); font-size: 0.95rem; }
.a-large { font-family: var(--serif); font-size: 1.45rem; }

/* blurred strip behind the status bar when installed as a home-screen app */
.safe-top {
  position: fixed;
  inset: 0 0 auto 0;
  height: env(safe-area-inset-top);
  z-index: 15;
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.spinning svg { animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- sort menu ---------- */
.sort-wrap { position: relative; }
.sort-menu {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 30;
  min-width: 210px;
  padding: 6px;
  display: flex;
  flex-direction: column;
  border-radius: 14px;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: blur(16px) saturate(1.4);
  -webkit-backdrop-filter: blur(16px) saturate(1.4);
  border: 1px solid var(--line);
  box-shadow: 0 10px 32px rgb(0 0 0 / 0.22);
}
.sort-menu button {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 12px;
  border: 0;
  border-radius: 9px;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 0.95rem;
  text-align: left;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.sort-menu button:hover { background: color-mix(in srgb, var(--text) 8%, transparent); }
.sort-menu button[aria-checked="true"]::after { content: "✓"; color: var(--accent); }

/* ---------- list ---------- */
.list { list-style: none; margin: 0 auto; padding: 0 0 8px; max-width: 720px; }
.list li + li { border-top: 1px solid var(--line); }
.item {
  display: flex;
  align-items: flex-start;
  gap: 12px;
  padding: 14px 16px;
  color: inherit;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.item:hover .item-title { color: var(--accent); }
.item-text { display: flex; flex-direction: column; gap: 3px; min-width: 0; flex: 1; }
.item-title { font-weight: 600; font-size: 1.02rem; line-height: 1.3; overflow-wrap: anywhere; }
.item-meta { color: var(--muted); font-size: 0.85rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.item-liked { color: var(--like); font-size: 0.95rem; padding-top: 2px; }
/* Tablets: touch screens big in both dimensions (excludes phones in landscape and desktops) */
@media (pointer: coarse) and (min-width: 600px) and (min-height: 600px) {
  .item { padding: 18px 20px; gap: 14px; }
  .item-text { gap: 4px; }
  .item-title { font-size: 1.25rem; }
  .item-meta { font-size: 1rem; }
  .item-liked { font-size: 1.15rem; }
}

.status { text-align: center; color: var(--muted); padding: 24px 16px; margin: 0; }
.status:empty { display: none; }

/* ---------- article ---------- */
.article {
  max-width: 680px;
  margin: 0 auto;
  padding: calc(24px + env(safe-area-inset-top)) 16px calc(120px + env(safe-area-inset-bottom));
}
.article-head h1 {
  font-family: var(--reader-font, var(--serif));
  font-size: clamp(1.6rem, 5vw, 2.2rem);
  line-height: 1.2;
  margin: 8px 0 10px;
  overflow-wrap: anywhere;
}
.article-meta { color: var(--muted); font-size: 0.9rem; margin: 0 0 6px; }
.title-link { color: inherit; text-decoration: none; }
.article-head { padding-bottom: 20px; margin-bottom: 24px; border-bottom: 1px solid var(--line); }

.article-body {
  font-family: var(--reader-font, var(--serif));
  font-size: var(--reader-size, 18px);
  line-height: var(--reader-leading, 1.7);
  overflow-wrap: break-word;
  -webkit-hyphens: auto;
  hyphens: auto;
  hyphenate-limit-chars: 6 3 3;
}
.article-body h1, .article-body h2, .article-body h3, .article-body h4,
.article-body pre, .article-body code { -webkit-hyphens: manual; hyphens: manual; }
.article-body a { color: inherit; text-decoration: underline; text-decoration-color: var(--link-line); text-decoration-thickness: 1.5px; text-underline-offset: 0.18em; }
.article-body video { max-width: 100%; height: auto; border-radius: 4px; }
.article-body figure { margin: 1.5em 0; }
.article-body figcaption { font-family: var(--sans); font-size: 0.85rem; color: var(--muted); margin-top: 6px; }
.article-body h1, .article-body h2, .article-body h3, .article-body h4 { line-height: 1.3; margin: 1.6em 0 0.5em; }
.article-body blockquote { margin: 1.2em 0; padding-left: 1em; border-left: 3px solid var(--line); color: var(--muted); }
.article-body pre { overflow-x: auto; background: var(--surface); border: 1px solid var(--line); padding: 12px; border-radius: 6px; font-size: 0.85rem; line-height: 1.5; }
.article-body code { font-size: 0.88em; }
.article-body table { display: block; overflow-x: auto; border-collapse: collapse; font-size: 0.95rem; }
.article-body td, .article-body th { border: 1px solid var(--line); padding: 6px 8px; }
.article-body .end-mark { font-size: 0.8em; }
.article-body hr { border: 0; border-top: 1px solid var(--line); margin: 2em 0; }

/* ---------- toast ---------- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(20px + env(safe-area-inset-bottom));
  transform: translate(-50%, 20px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 0.9rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  max-width: calc(100% - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* ---------- login ---------- */
.login {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 16px;
}
.login form { width: 100%; max-width: 320px; display: flex; flex-direction: column; gap: 12px; }
.login h1 { margin: 0 0 8px; font-size: 1.5rem; text-align: center; }
.login input, .login button {
  font: inherit;
  height: 44px;
  border-radius: 10px;
  border: 1px solid var(--line);
  padding: 0 12px;
  background: var(--surface);
  color: var(--text);
}
.login button { background: var(--text); color: var(--bg); border: 0; cursor: pointer; font-weight: 600; }
.login .error { color: var(--like); text-align: center; margin: 0; font-size: 0.9rem; }

/* ---------- advance mode toggle ---------- */
.mode-choice { grid-template-columns: repeat(2, 1fr); border-bottom: 0; border-top: 1px solid var(--line); }
.mode-choice button { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-family: var(--sans); font-size: 0.95rem; }

/* ---------- paged mode ---------- */
/* The article becomes a fixed viewport; its content flows into one column per page
   and pages slide horizontally. JS sets --page-col, --page-pad and --page-h. */
body.paged { overflow: hidden; overscroll-behavior: none; }
body.paged .article {
  position: fixed;
  left: 0;
  right: 0;
  top: calc(20px + env(safe-area-inset-top));
  bottom: calc(28px + env(safe-area-inset-bottom));
  max-width: none;
  margin: 0;
  padding: 0;
  overflow: hidden;
  touch-action: none;
}
body.paged .pages {
  height: 100%;
  padding: 0 var(--page-pad, 16px);
  column-width: var(--page-col, 648px);
  column-gap: calc(var(--page-pad, 16px) * 2);
  column-fill: auto;
  will-change: transform;
}
body.paged .article-body video {
  max-height: calc(var(--page-h, 600px) - 3em);
  width: auto;
  object-fit: contain;
}
body.paged .article-body figure { break-inside: avoid; }
body.paged .article-body pre { white-space: pre-wrap; }
body.paged .article-body table { display: table; table-layout: fixed; width: 100%; }

/* thin reading-progress line along the very bottom edge (paged mode) */
.page-progress { display: none; position: fixed; left: 0; right: 0; bottom: 0; height: 2px; z-index: 4; pointer-events: none; }
.page-progress > div { height: 100%; width: 0; background: var(--muted); opacity: 0.7; transition: width 0.2s ease; }
body.paged .page-progress { display: block; }

/* log out sits at the end of the list, out of the way */
.logout { max-width: 720px; margin: 0 auto; padding: 8px 16px calc(32px + env(safe-area-inset-bottom)); text-align: center; }
.logout button { border: 0; background: none; color: var(--muted); font: inherit; font-size: 0.85rem; text-decoration: underline; text-underline-offset: 0.18em; cursor: pointer; padding: 8px; }

/* offline badge in the list header */
.offline { display: inline-block; vertical-align: middle; margin-left: 6px; padding: 2px 8px; border: 1px solid var(--line); border-radius: 999px; font-size: 0.72rem; font-weight: 500; color: var(--muted); letter-spacing: 0.02em; }
