/*
 * Pocket design system — calm, focused, readable.
 * Restrained palette, generous spacing, large hit targets, light/dark aware.
 */

:root {
  --bg: #f7f6f3;
  --surface: #ffffff;
  --surface-2: #f0efea;
  --border: #e3e1da;
  --text: #23221f;
  --text-muted: #6b6a64;
  --text-faint: #78766f; /* WCAG AA (~4.6:1) on --bg; still clearly de-emphasized */
  --accent: #3b5bdb;
  --accent-contrast: #ffffff;
  --danger: #c0392b;
  --success: #2f855a;
  --warning: #b7791f;
  --radius: 10px;
  --radius-sm: 6px;
  /* Shadow tinted toward the warm neutral rather than pure black. */
  --shadow: 0 1px 2px rgba(40, 34, 24, 0.05), 0 6px 20px rgba(40, 34, 24, 0.06);
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-serif: Georgia, "Iowan Old Style", "Times New Roman", serif;
  --maxw: 960px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17171a;
    --surface: #1f1f23;
    --surface-2: #26262b;
    --border: #33333a;
    --text: #e8e7e3;
    --text-muted: #a3a29b;
    --text-faint: #8b897f; /* WCAG AA on the dark --bg */
    --accent: #748ffc;
    --accent-contrast: #12121a;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.3), 0 4px 16px rgba(0, 0, 0, 0.35);
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  line-height: 1.55;
  font-size: 16px;
  -webkit-font-smoothing: antialiased;
}

a { color: var(--accent); text-decoration: none; text-underline-offset: 2px; }
a:hover { text-decoration: underline; }

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

/* Controlled hierarchy through weight + tracking, not raw scale. */
h1, h2, h3 { line-height: 1.2; font-weight: 640; letter-spacing: -0.012em; text-wrap: balance; }
h1 { font-size: 1.7rem; margin: 0 0 0.5rem; }
h2 { font-size: 1.25rem; margin: 1.5rem 0 0.75rem; letter-spacing: -0.008em; }
h3 { letter-spacing: -0.006em; }

/* ---------- Layout shell ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 20;
  background: color-mix(in srgb, var(--surface) 92%, transparent);
  backdrop-filter: saturate(150%) blur(8px);
  border-bottom: 1px solid var(--border);
}
.app-header__inner {
  max-width: var(--maxw);
  margin: 0 auto;
  padding: 0.6rem 1rem;
  display: flex;
  align-items: center;
  gap: 1rem;
}
.brand {
  font-weight: 750;
  font-size: 1.1rem;
  color: var(--text);
  letter-spacing: -0.01em;
}
.brand:hover { text-decoration: none; }

/* Global search box in the header (grows to fill the middle). */
.header-search {
  flex: 1 1 auto;
  max-width: 460px;
  position: relative;
  display: flex;
  align-items: center;
}
.header-search__icon {
  position: absolute;
  left: 0.6rem;
  font-size: 0.85rem;
  pointer-events: none;
  opacity: 0.7;
}
.header-search__input {
  width: 100%;
  min-height: 38px;
  padding-left: 2rem;
  background: var(--surface-2);
  border-color: transparent;
}
.header-search__input:focus { background: var(--surface); border-color: var(--border); }

.app-nav {
  display: flex;
  gap: 0.25rem;
  flex: 0 0 auto;
  flex-wrap: wrap;
}
.app-nav a {
  color: var(--text-muted);
  padding: 0.35rem 0.6rem;
  border-radius: var(--radius-sm);
  font-size: 0.95rem;
}
.app-nav a:hover { background: var(--surface-2); text-decoration: none; }
.app-nav a[aria-current="page"] { color: var(--text); background: var(--surface-2); font-weight: 600; }

main { max-width: var(--maxw); margin: 0 auto; padding: 1.5rem 1rem 4rem; }

@media (max-width: 640px) {
  .app-header__inner { flex-wrap: wrap; row-gap: 0.4rem; }
  .header-search { order: 4; flex-basis: 100%; max-width: none; }
  .app-nav { order: 3; flex-wrap: nowrap; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .app-nav a { white-space: nowrap; }
}

/* ---------- Flash / notices ---------- */
.flash { max-width: var(--maxw); margin: 0.75rem auto 0; padding: 0 1rem; }
.notice {
  padding: 0.7rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  gap: 0.5rem;
  align-items: flex-start;
}
.notice--alert { border-color: color-mix(in srgb, var(--danger) 40%, var(--border)); }
.notice--success { border-color: color-mix(in srgb, var(--success) 40%, var(--border)); }
.notice__icon { font-weight: 700; }

/* ---------- Cards / panels ---------- */
.panel {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
}
.stack > * + * { margin-top: 0.75rem; }
.muted { color: var(--text-muted); }
.faint { color: var(--text-faint); }
.small { font-size: 0.875rem; }

/* ---------- Buttons & forms ---------- */
.btn, button, input[type="submit"] {
  font: inherit;
  cursor: pointer;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  padding: 0.5rem 0.9rem;
  border-radius: var(--radius-sm);
  min-height: 40px;
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  transition: background 0.12s ease, border-color 0.12s ease, transform 0.06s ease;
}
.btn:hover, button:hover { background: var(--surface-2); text-decoration: none; }
/* Tactile press — a subtle physical push on click. */
.btn:active, button:active, input[type="submit"]:active { transform: translateY(1px); }
.btn--primary, input[type="submit"] {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: transparent;
  font-weight: 600;
}
.btn--primary:hover, input[type="submit"]:hover { filter: brightness(1.05); background: var(--accent); }
.btn--danger { color: var(--danger); border-color: color-mix(in srgb, var(--danger) 35%, var(--border)); }
.btn--ghost { background: transparent; border-color: transparent; color: var(--text-muted); }

label { display: block; font-size: 0.9rem; font-weight: 600; margin-bottom: 0.25rem; }
input[type="text"], input[type="email"], input[type="password"], input[type="url"],
input[type="number"], textarea, select {
  font: inherit;
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  min-height: 40px;
}
textarea { min-height: 96px; resize: vertical; }
.field { margin-bottom: 1rem; }
.field__error { color: var(--danger); font-size: 0.85rem; margin-top: 0.25rem; }

/* ---------- Auth pages ---------- */
.auth-wrap { max-width: 380px; margin: 8vh auto 0; }
.auth-wrap .panel { padding: 1.75rem; }

/* ---------- Utility ---------- */
.row { display: flex; gap: 0.75rem; align-items: center; flex-wrap: wrap; }
.row--between { justify-content: space-between; }
.spacer { flex: 1; }
.hidden { display: none !important; }
.visually-hidden {
  position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

/* ---------- Inbox / tabs / filters ---------- */
.inbox > * + * { margin-top: 1rem; }

.tabs { display: flex; gap: 0.25rem; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.tabs a {
  padding: 0.5rem 0.85rem;
  color: var(--text-muted);
  border-bottom: 2px solid transparent;
  margin-bottom: -1px;
}
.tabs a:hover { text-decoration: none; color: var(--text); }
.tabs a.is-active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }
.tabs .count { color: var(--text-faint); font-size: 0.8rem; margin-left: 0.15rem; }

.filters { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }
.filters input[type="search"] { flex: 1; min-width: 180px; }
.filters select { width: auto; min-width: 120px; }
.filters__density { margin-left: auto; display: flex; gap: 0.5rem; font-size: 0.85rem; }
.filters__density a.is-active { font-weight: 700; color: var(--text); }

.bulk { display: flex; gap: 0.4rem; align-items: center; flex-wrap: wrap; }

/* ---------- Document list ---------- */
.doc-list { display: flex; flex-direction: column; gap: 0.75rem; }
.doc {
  display: flex;
  gap: 0.75rem;
  padding: 1rem;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.doc-list--compact .doc { padding: 0.6rem 0.85rem; }
.doc-list--compact .doc__meta, .doc-list--compact .doc__badges { font-size: 0.8rem; }
.doc__select { padding-top: 0.2rem; }
.doc__body { flex: 1; min-width: 0; }
.doc__title-row { display: flex; align-items: baseline; gap: 0.4rem; flex-wrap: wrap; }
.doc__title { font-weight: 650; font-size: 1.05rem; color: var(--text); }
.doc__star { color: var(--warning); }
.doc__icon { font-size: 1rem; }
.doc__meta { margin-top: 0.2rem; display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.doc__meta .doc__source { color: var(--text-muted); }
.doc__badges { margin-top: 0.4rem; display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.doc__failure { color: var(--danger); margin-top: 0.4rem; }
.doc__dup-notice {
  margin-top: 0.5rem; padding: 0.5rem 0.7rem; border-radius: var(--radius-sm);
  background: var(--surface-2); display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap;
}
.doc__actions { display: flex; flex-direction: column; gap: 0.35rem; align-items: stretch; }
@media (max-width: 640px) {
  .doc { flex-wrap: wrap; }
  .doc__actions { flex-direction: row; flex-wrap: wrap; width: 100%; }
}

/* ---------- Badges & chips ---------- */
.badge {
  display: inline-flex; align-items: center; gap: 0.25rem;
  padding: 0.1rem 0.45rem; border-radius: 999px; font-size: 0.78rem;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted);
}
.badge--ok { border-color: color-mix(in srgb, var(--success) 45%, var(--border)); color: var(--success); }
.badge--fail { border-color: color-mix(in srgb, var(--danger) 45%, var(--border)); color: var(--danger); }
.badge--pending { border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); color: var(--warning); }
.tag-chip {
  display: inline-block; padding: 0.08rem 0.45rem; border-radius: 999px;
  font-size: 0.78rem; background: var(--surface-2); color: var(--text-muted); border: 1px solid var(--border);
}
a.tag-chip:hover { color: var(--text); text-decoration: none; }

.notice--warn { border-color: color-mix(in srgb, var(--warning) 45%, var(--border)); }
.empty { text-align: center; padding: 2.5rem 1.5rem; }
.empty > * + * { margin-top: 0.5rem; }

/* ---------- Capture / forms ---------- */
.capture { max-width: 620px; margin: 0 auto; }
.capture > * + * { margin-top: 1rem; }
.dup-warning > * + * { margin-top: 0.75rem; }
.bookmarklet-code { width: 100%; font-family: monospace; font-size: 0.8rem; }

/* ---------- Reader ---------- */
.reader { max-width: 720px; margin: 0 auto; }
.reader__header { border-bottom: 1px solid var(--border); padding-bottom: 1rem; margin-bottom: 1.5rem; }
.reader__title { font-size: 2rem; margin: 0.5rem 0; }
.reader__byline { margin: 0.25rem 0 0.75rem; }
.reader__actions { margin: 0.75rem 0; }
.reader__body { font-size: 1.125rem; line-height: 1.75; }
.reader__body p { margin: 0 0 1.15em; }
.reader__body h1, .reader__body h2, .reader__body h3 { margin: 1.5em 0 0.5em; }
.reader__body img { max-width: 100%; height: auto; border-radius: var(--radius-sm); }
.reader__body blockquote {
  margin: 1em 0; padding: 0.25em 1em; border-left: 3px solid var(--border); color: var(--text-muted);
}
.reader__body pre { overflow-x: auto; background: var(--surface-2); padding: 1em; border-radius: var(--radius-sm); }

/* ---------- Reading progress bar ---------- */
.reader-progress {
  position: fixed; top: 0; left: 0; right: 0; height: 3px;
  background: transparent; z-index: 30;
}
.reader-progress__bar { height: 100%; width: 0; background: var(--accent); transition: width 0.15s ease; }

/* ---------- Typography controls ---------- */
.typo-bar {
  display: flex; gap: 1rem; flex-wrap: wrap; align-items: center;
  padding: 0.5rem 0; margin-bottom: 1.5rem;
  border-top: 1px solid var(--border); border-bottom: 1px solid var(--border);
}
.typo-bar__group { display: inline-flex; gap: 0.25rem; }
.typo-bar .btn[aria-pressed="true"] { background: var(--accent); color: var(--accent-contrast); border-color: transparent; }

/* Reader typography responds to data-* set by the typography controller. */
.reader-wrap[data-font="sans"] .reader__body { font-family: var(--font-sans); }
.reader-wrap[data-font="serif"] .reader__body { font-family: var(--font-serif); }
.reader-wrap[data-size="s"]  .reader__body { font-size: 1rem; }
.reader-wrap[data-size="m"]  .reader__body { font-size: 1.125rem; }
.reader-wrap[data-size="l"]  .reader__body { font-size: 1.28rem; }
.reader-wrap[data-size="xl"] .reader__body { font-size: 1.45rem; }
.reader-wrap[data-width="narrow"] .reader { max-width: 680px; }
.reader-wrap[data-width="wide"]   .reader { max-width: 900px; }

/* Per-reader theme override (independent of the page theme). */
.reader-wrap[data-reader-theme="sepia"] { --bg: #f4ecd8; --surface: #f4ecd8; --text: #3a3222; --text-muted: #6b5f47; --border: #e0d4b8; }
.reader-wrap[data-reader-theme="light"] { --bg: #ffffff; --surface: #ffffff; --text: #1a1a1a; --text-muted: #555; --border: #e3e1da; }
.reader-wrap[data-reader-theme="dark"]  { --bg: #16161a; --surface: #16161a; --text: #e8e7e3; --text-muted: #a3a29b; --border: #33333a; }
.reader-wrap[data-reader-theme] { background: var(--bg); color: var(--text); border-radius: var(--radius); }
.reader-wrap[data-reader-theme] .reader { padding: 1.25rem; }

/* ---------- Search ---------- */
.search > * + * { margin-top: 1rem; }
.results { list-style: none; padding: 0; margin: 1rem 0 0; display: flex; flex-direction: column; gap: 1rem; counter-reset: r; }
.result { padding: 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.result__head { display: flex; gap: 0.6rem; align-items: baseline; flex-wrap: wrap; justify-content: space-between; }
.result__title { font-weight: 650; font-size: 1.08rem; }
.match {
  display: inline-flex; align-items: center; gap: 0.3rem; white-space: nowrap;
  font-size: 0.78rem; padding: 0.1rem 0.5rem; border-radius: 999px;
  border: 1px solid var(--border); background: var(--surface-2); color: var(--text-muted);
}
.match--both { border-color: color-mix(in srgb, var(--accent) 45%, var(--border)); color: var(--accent); }
.result__meta { margin: 0.35rem 0; display: flex; gap: 0.4rem; flex-wrap: wrap; align-items: center; }
.result__snippet { margin: 0.35rem 0 0; color: var(--text); }
.result__snippet mark { background: color-mix(in srgb, var(--warning) 30%, transparent); color: inherit; }
.result__snippet--semantic { color: var(--text-muted); }

/* ---------- Imports ---------- */
.import, .imports-index { max-width: 640px; margin: 0 auto; }
.imports-index > * + * { margin-top: 1rem; }
.import-status > * + * { margin-top: 0.75rem; }
.import-status__name { margin: 0; font-size: 1.15rem; }
.import-sample {
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  padding: 0.75rem; overflow-x: auto; font-size: 0.82rem; white-space: pre;
}
.progress { height: 8px; background: var(--surface-2); border-radius: 999px; overflow: hidden; }
.progress__bar { height: 100%; background: var(--accent); transition: width 0.2s ease; }
.import-tally { list-style: none; padding: 0; margin: 0; display: flex; gap: 1.25rem; flex-wrap: wrap; }
.import-tally__num { font-size: 1.3rem; font-weight: 700; display: block; }
.import-errors { margin-top: 0.5rem; }
.import-errors code { background: var(--surface-2); padding: 0 0.25rem; border-radius: 3px; }
.import-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; }
.import-list__item {
  display: flex; gap: 0.6rem; align-items: center; flex-wrap: wrap;
  padding: 0.75rem 1rem; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
}
.import-list__name { font-weight: 600; }

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; animation: none !important; scroll-behavior: auto !important; }
}
