/*
 * 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); }

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; }
}

/* ---------- 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; }

/* ---------- 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__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;
}
/* Quiet until you engage with the row, but never hidden — hover-only controls
   are unusable on touch and invisible to anyone tabbing through. */
.doc__actions { display: flex; gap: 0.15rem; align-items: flex-start; opacity: 0.6; transition: opacity 0.12s ease; }
.doc__actions form { margin: 0; display: inline-flex; }
.doc:hover .doc__actions,
.doc:focus-within .doc__actions,
.doc.is-selected .doc__actions { opacity: 1; }

.doc.is-selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 22%, transparent), var(--shadow);
}
.doc:focus { outline: none; }
.doc:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* Reading progress on the row: the answer to "what was I part-way through?" */
.doc__progress { margin-top: 0.45rem; display: flex; align-items: center; gap: 0.5rem; }
.doc__progress-track {
  display: block; flex: 0 0 110px; height: 4px; overflow: hidden;
  background: var(--surface-2); border-radius: 999px;
}
.doc__progress-bar { display: block; height: 100%; background: var(--accent); border-radius: 999px; }
.doc__progress--done .doc__progress-bar { background: var(--success); }

@media (max-width: 640px) {
  .doc { flex-wrap: wrap; }
  .doc__actions { width: 100%; opacity: 1; justify-content: flex-end; }
}

/* ---------- Icon buttons ---------- */
.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 38px; min-height: 38px; padding: 0 0.45rem;
  border: 1px solid transparent; border-radius: var(--radius-sm);
  background: transparent; color: var(--text-muted);
  font: inherit; line-height: 1; cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); text-decoration: none; }
.icon-btn.is-on { color: var(--warning); }

/* ---------- 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__back { margin: 0 0 0.75rem; }
.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-family: var(--font-serif); 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; }

/* ---------- 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; }

/* ---------- Header menu (details/summary — no JS needed) ---------- */
.menu { position: relative; flex: 0 0 auto; }
.menu__button {
  list-style: none; cursor: pointer; display: inline-flex; align-items: center; justify-content: center;
  min-width: 38px; min-height: 38px; border-radius: var(--radius-sm);
  color: var(--text-muted); font-size: 1.15rem; line-height: 1;
}
.menu__button::-webkit-details-marker { display: none; }
.menu__button:hover { background: var(--surface-2); color: var(--text); }
.menu[open] .menu__button { background: var(--surface-2); color: var(--text); }
.menu__panel {
  position: absolute; right: 0; top: calc(100% + 0.4rem); z-index: 40; min-width: 210px;
  display: flex; flex-direction: column; padding: 0.35rem;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: var(--radius); box-shadow: var(--shadow);
}
.menu__panel form { margin: 0; }
.menu__item {
  display: flex; align-items: center; width: 100%; min-height: 40px;
  padding: 0.5rem 0.6rem; border: 0; border-radius: var(--radius-sm);
  background: transparent; color: var(--text); font: inherit; text-align: left; cursor: pointer;
}
.menu__item:hover { background: var(--surface-2); text-decoration: none; }

/* ---------- Dialogs ---------- */
.modal {
  width: 100%; max-width: min(640px, calc(100vw - 2rem));
  padding: 0; border: 0; background: transparent; color: var(--text);
}
.modal::backdrop { background: rgba(20, 18, 14, 0.45); }
.modal__panel {
  padding: 1.25rem; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.modal__panel > * + * { margin-top: 1rem; }
.modal__title { font-size: 1.3rem; margin: 0; }
/* A panel inside the dialog would be a box in a box. */
.modal__panel .panel { padding: 0; border: 0; background: transparent; box-shadow: none; }

.shortcuts { display: grid; grid-template-columns: auto 1fr; gap: 0.5rem 1rem; margin: 0; }
.shortcuts dt { white-space: nowrap; }
.shortcuts dd { margin: 0; color: var(--text-muted); }
kbd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 0.8em;
  padding: 0.1rem 0.35rem; border: 1px solid var(--border); border-bottom-width: 2px;
  border-radius: 4px; background: var(--surface-2); color: var(--text);
}
.keyboard-hint { text-align: center; margin-top: 1.5rem; }
@media (max-width: 640px) { .keyboard-hint { display: none; } }

/* ---------- Paging ---------- */
.pagination { display: flex; justify-content: center; align-items: center; gap: 0.75rem; padding-top: 0.5rem; }
.pagination:empty { display: none; }

/* ---------- Undo ---------- */
.flash form.button_to { margin: 0 0 0 auto; }
.flash__undo { text-decoration: underline; }

/* ---------- Search result actions ---------- */
.result__actions { margin-top: 0.6rem; display: flex; gap: 0.35rem; flex-wrap: wrap; opacity: 0.7; transition: opacity 0.12s ease; }
.result:hover .result__actions, .result:focus-within .result__actions { opacity: 1; }
.result__actions form { margin: 0; }

.reader__resume { color: var(--accent); }

/* ---------- Bookmarklet window ---------- */
.popup__main { max-width: 100%; padding: 1rem; }
.popup .capture h1 { font-size: 1.35rem; }
.popup-saved { text-align: center; padding: 2rem 1rem; }
.popup-saved > * + * { margin-top: 0.5rem; }
.popup-saved__mark {
  margin: 0; font-size: 2.5rem; line-height: 1; color: var(--success);
}
.popup-saved__title { margin: 0; }
.popup-saved__actions { justify-content: center; margin-top: 1.25rem; }

/* ---------- Set-up instructions ---------- */
.steps { padding-left: 1.2rem; margin: 0; }
.steps > li + li { margin-top: 0.5rem; }
.steps__code {
  display: inline-block; margin-top: 0.35rem; padding: 0.15rem 0.4rem;
  background: var(--surface-2); border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 0.85rem; word-break: break-all;
}

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