:root {
  --paper: #fbfaf7;
  --surface: #ffffff;
  --ink: #17251e;
  --muted: #647067;
  --faint: #92998f;
  --forest: #21372c;
  --forest-soft: #e8eee9;
  --gold: #f4b942;
  --gold-soft: #fff2c9;
  --border: #dfe4de;
  --danger: #a43d35;
  --focus: #1769aa;
  --shadow: 0 12px 30px rgba(23, 37, 30, 0.1);
  color: var(--ink);
  font-family: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans TC", sans-serif;
  font-synthesis: none;
}

* { box-sizing: border-box; }
html { background: var(--paper); }
body { min-width: 320px; margin: 0; color: var(--ink); line-height: 1.5; }
button, input, select { font: inherit; }
button { cursor: pointer; }
button, a, input { -webkit-tap-highlight-color: transparent; }
button:focus-visible, input:focus-visible, a:focus-visible, [tabindex]:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 3px;
}
button:disabled { cursor: wait; opacity: 0.58; }
[hidden] { display: none !important; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}
.skip-link {
  position: fixed;
  z-index: 100;
  top: 8px;
  left: 8px;
  padding: 10px 14px;
  transform: translateY(-150%);
  background: var(--forest);
  color: white;
}
.skip-link:focus { transform: translateY(0); }

.app-page { min-height: 100vh; background: var(--paper); padding-bottom: calc(94px + env(safe-area-inset-bottom)); }
.app-header {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 15px;
  width: min(100% - 32px, 1080px);
  margin: 0 auto;
  padding: calc(18px + env(safe-area-inset-top)) 0 14px;
}
.brand-lockup { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-lockup > span:last-child { display: grid; line-height: 1.05; }
.brand-lockup strong { font-size: 18px; letter-spacing: -0.03em; }
.brand-lockup small { margin-top: 3px; color: var(--muted); font-size: 11px; letter-spacing: 0.12em; text-transform: uppercase; }
.brand-mark {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  flex: 0 0 auto;
  border-radius: 14px;
  background: var(--gold);
  color: var(--forest);
  font-family: Georgia, "Times New Roman", serif;
  font-size: 27px;
  font-weight: 800;
  box-shadow: inset -7px -7px 0 rgba(33, 55, 44, 0.08);
}
.brand-mark.small { width: 44px; height: 44px; border-radius: 12px; font-size: 22px; }
.account-actions { display: flex; align-items: center; gap: 4px; }
.account-actions form { margin: 0; }
.icon-button {
  display: grid;
  width: 44px;
  min-height: 44px;
  place-items: center;
  border: 0;
  background: transparent;
  color: var(--muted);
  font-size: 19px;
  text-decoration: none;
}
.icon-button:hover { color: var(--forest); }
.search-field {
  display: flex;
  grid-column: 1 / -1;
  align-items: center;
  min-height: 50px;
  gap: 10px;
  padding: 0 14px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  color: var(--faint);
  box-shadow: 0 3px 12px rgba(23, 37, 30, 0.04);
}
.search-field:focus-within { border-color: var(--forest); box-shadow: 0 0 0 3px var(--forest-soft); }
.search-field input { width: 100%; min-width: 0; padding: 12px 0; border: 0; outline: 0; background: transparent; color: var(--ink); }
.search-field kbd { padding: 2px 7px; border: 1px solid var(--border); border-radius: 5px; background: var(--paper); font-size: 12px; }

.library-shell { width: min(100% - 32px, 1080px); min-height: 60vh; margin: 0 auto; }
.app-panel { padding: 24px 0 6px; scroll-margin-top: 20px; }
.section-heading, .catalog-heading, .dialog-heading { display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.eyebrow { margin: 0 0 4px; color: #806119; font-size: 11px; font-weight: 800; letter-spacing: 0.15em; }
.app-panel h2, .catalog-heading h2 { margin: 0; font-size: 27px; line-height: 1.2; letter-spacing: -0.035em; }
.panel-note { margin: 6px 0 0; color: var(--muted); }
.view-toggle { display: grid; grid-template-columns: 44px 44px; overflow: hidden; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); }
.view-button { min-height: 44px; border: 0; background: transparent; color: var(--muted); }
.view-button + .view-button { border-left: 1px solid var(--border); }
.view-button.active { background: var(--forest); color: white; }
.quick-filter { margin-top: 24px; }
.quick-filter h3 { margin: 0 0 10px; font-size: 13px; }
.chip-row { display: flex; gap: 8px; overflow-x: auto; padding: 2px 2px 7px; scrollbar-width: thin; scroll-snap-type: x proximity; }
.chip-row > span { flex: 0 0 auto; scroll-snap-align: start; }
.filter-chip {
  display: flex;
  align-items: center;
  min-height: 44px;
  gap: 9px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}
.filter-chip small { display: grid; min-width: 20px; height: 20px; place-items: center; border-radius: 50%; background: var(--paper); font-size: 10px; }
.filter-chip.active { border-color: var(--forest); background: var(--forest); color: white; }
.filter-chip.active small { background: rgba(255, 255, 255, 0.17); }

.catalog-section { padding: 25px 0 35px; }
.catalog-heading { margin-bottom: 14px; border-top: 1px solid var(--border); padding-top: 24px; }
.catalog-heading > div > p { margin: 3px 0 0; color: var(--muted); font-size: 13px; }
.catalog-heading > p { margin: 0; color: var(--muted); font-size: 13px; }
.catalog-heading strong { color: var(--ink); font-size: 19px; }
.book-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 14px; }
.book-card { min-width: 0; }
.book-card-button { display: block; width: 100%; min-height: 44px; padding: 0; border: 0; background: transparent; color: inherit; text-align: left; }
.book-cover { position: relative; display: grid; aspect-ratio: 4 / 3; place-items: center; overflow: hidden; border: 1px solid var(--border); border-radius: 11px; background: #e9ece5; }
.book-cover img { width: 100%; height: 100%; object-fit: cover; }
.media-carousel { display: flex; width: 100%; height: 100%; overflow-x: auto; overscroll-behavior-x: contain; scroll-snap-type: x mandatory; scrollbar-width: none; touch-action: pan-x pan-y; }
.media-carousel::-webkit-scrollbar { display: none; }
.media-slide { position: relative; display: grid; width: 100%; height: 100%; flex: 0 0 100%; place-items: center; overflow: hidden; scroll-snap-align: start; scroll-snap-stop: always; }
.gallery-indicator { position: absolute; right: 8px; top: 8px; z-index: 2; padding: 2px 7px; border-radius: 999px; background: rgba(23, 37, 30, 0.82); color: white; font-size: 10px; font-weight: 750; line-height: 1.5; pointer-events: none; }
.video-play-badge { display: grid; position: absolute; right: 9px; bottom: 9px; width: 34px; height: 34px; place-items: center; border: 2px solid rgba(255, 255, 255, 0.9); border-radius: 50%; background: rgba(23, 37, 30, 0.86); color: white; font-size: 13px; line-height: 1; box-shadow: 0 3px 9px rgba(0, 0, 0, 0.24); }
.book-monogram {
  display: grid;
  width: 46%;
  aspect-ratio: 0.72;
  place-items: center;
  border-radius: 3px 8px 8px 3px;
  background: var(--forest);
  color: var(--gold);
  font-family: Georgia, "Times New Roman", serif;
  font-size: clamp(25px, 8vw, 42px);
  font-weight: 700;
  box-shadow: 7px 9px 16px rgba(23, 37, 30, 0.2);
}
.book-card-body { padding: 10px 2px 6px; }
.book-meta { display: flex; align-items: center; gap: 5px; overflow: hidden; }
.status-badge, .content-type-badge, .tag { display: inline-flex; min-width: 0; padding: 3px 7px; border-radius: 5px; font-size: 10px; font-weight: 750; }
.content-type-badge { overflow: hidden; background: var(--gold-soft); color: #715411; text-overflow: ellipsis; white-space: nowrap; }
.status-badge { flex: 0 0 auto; background: var(--forest-soft); color: var(--forest); }
.book-card h3 { display: -webkit-box; overflow: hidden; margin: 9px 0 2px; font-size: 16px; line-height: 1.25; letter-spacing: -0.02em; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
.book-author, .capture-date, .book-summary { margin: 0; color: var(--muted); font-size: 12px; }
.book-author { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.capture-date { margin-top: 3px; color: var(--faint); font-size: 11px; }
.book-summary { display: none; }
.tag-list { display: none; gap: 5px; margin-top: 10px; }
.tag { background: var(--paper); color: var(--muted); }

.book-grid.list-view { grid-template-columns: 1fr; gap: 0; }
.list-view .book-card { display: grid; grid-template-columns: 96px minmax(0, 1fr); gap: 14px; padding: 12px 0; border-bottom: 1px solid var(--border); }
.list-view .book-card-button { display: block; align-self: center; }
.list-view .book-cover { aspect-ratio: 4 / 3; }
.list-view .book-card-body { align-self: center; padding: 0; }
.list-view .book-card h3 { font-size: 17px; }
.list-view .book-summary { display: -webkit-box; overflow: hidden; margin-top: 7px; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }

.category-browser { display: grid; gap: 0; margin-top: 20px; border-top: 1px solid var(--border); }
.category-tile { display: grid; grid-template-columns: 44px minmax(0, 1fr) auto; align-items: center; min-height: 72px; gap: 12px; padding: 10px 0; border: 0; border-bottom: 1px solid var(--border); background: transparent; color: inherit; text-align: left; }
.category-symbol { display: grid; width: 44px; height: 44px; place-items: center; border-radius: 10px; background: var(--forest-soft); color: var(--forest); font-family: Georgia, serif; font-size: 19px; font-weight: 700; }
.category-copy { display: grid; }
.category-copy small { color: var(--muted); }
.category-arrow { color: var(--faint); font-size: 28px; }
.ai-panel { max-width: 760px; margin: 0 auto; }
.ai-panel .quiet-symbol { display: grid; width: 56px; height: 56px; margin-bottom: 16px; place-items: center; border: 1px solid #e7cf86; border-radius: 50%; background: var(--gold-soft); color: #806119; font-size: 24px; }
.ai-search-form { display: grid; gap: 8px; margin-top: 24px; }
.ai-search-form label { font-size: 13px; font-weight: 750; }
.ai-search-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 10px; }
.ai-search-row input { min-width: 0; min-height: 48px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px; background: white; color: var(--ink); }
.ai-search-row .primary-button { margin-top: 0; }
.ai-search-status { min-height: 24px; margin: 16px 0; color: var(--muted); }
.ai-search-results { display: grid; gap: 12px; }
.ai-result { padding: 18px; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow-soft); }
.ai-result h3 { margin: 4px 0 8px; }
.ai-result-summary { color: var(--muted); line-height: 1.65; }
.ai-citation-id { margin: 0; color: #806119; font-size: 12px; font-weight: 750; overflow-wrap: anywhere; }

@media (max-width: 520px) {
  .ai-search-row { grid-template-columns: 1fr; }
  .ai-search-row .primary-button { width: 100%; }
}

.empty-state, .error-state { margin: 24px 0; padding: 42px 20px; border: 1px dashed #c8d0c8; background: rgba(255, 255, 255, 0.5); text-align: center; }
.empty-state > span { color: var(--gold); font-size: 42px; }
.empty-state h2 { margin: 8px 0 4px; font-size: 19px; }
.empty-state p { margin: 0; color: var(--muted); font-size: 14px; }
.error-state { color: var(--danger); }

.bottom-nav {
  position: fixed;
  z-index: 20;
  right: 0;
  bottom: 0;
  left: 0;
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  min-height: calc(72px + env(safe-area-inset-bottom));
  padding: 6px max(8px, env(safe-area-inset-right)) env(safe-area-inset-bottom) max(8px, env(safe-area-inset-left));
  border-top: 1px solid var(--border);
  background: rgba(251, 250, 247, 0.96);
  box-shadow: 0 -8px 24px rgba(23, 37, 30, 0.06);
}
.nav-item { display: grid; min-width: 44px; min-height: 56px; place-items: center; align-content: center; gap: 1px; border: 0; background: transparent; color: var(--muted); }
.nav-item > span { font-size: 21px; line-height: 1; }
.nav-item small { font-size: 10px; font-weight: 700; white-space: nowrap; }
.nav-item.active { color: var(--forest); }
.nav-item.active > span { color: #a87911; }
.add-button { display: grid; width: 56px; min-height: 56px; margin: -21px auto 0; place-items: center; align-self: center; border: 5px solid var(--paper); border-radius: 50%; background: var(--forest); color: white; box-shadow: 0 7px 20px rgba(23, 37, 30, 0.22); }
.add-button span { font-size: 29px; font-weight: 300; line-height: 1; }

.sheet-dialog, .book-dialog { width: min(600px, calc(100% - 24px)); max-height: calc(100dvh - 24px); margin: auto auto 0; padding: 0; border: 0; border-radius: 20px 20px 0 0; background: var(--surface); color: var(--ink); box-shadow: var(--shadow); }
.sheet-dialog::backdrop, .book-dialog::backdrop { background: rgba(10, 20, 15, 0.55); }
.sheet-content { display: grid; gap: 10px; padding: 10px 20px calc(24px + env(safe-area-inset-bottom)); }
.sheet-handle { width: 42px; height: 4px; margin: 0 auto 8px; border-radius: 4px; background: var(--border); }
.dialog-heading h2 { margin: 0; font-size: 24px; }
.dialog-close { display: grid; width: 44px; min-height: 44px; flex: 0 0 auto; place-items: center; border: 0; border-radius: 50%; background: var(--paper); color: var(--muted); font-size: 25px; }
.sheet-content label { margin-top: 6px; font-size: 13px; font-weight: 750; }
.sheet-content input, .login-form input, .login-card form:not(.login-form) input { width: 100%; min-height: 48px; padding: 11px 12px; border: 1px solid var(--border); border-radius: 9px; background: white; color: var(--ink); }
.form-hint { margin: 0; color: var(--muted); font-size: 12px; }
.primary-button, .secondary-button { min-height: 46px; margin-top: 8px; padding: 10px 16px; border: 1px solid transparent; border-radius: 9px; background: var(--forest); color: white; font-weight: 750; }
.secondary-button { width: 100%; background: var(--forest-soft); color: var(--forest); }
.ingest-status { display: grid; gap: 2px; margin-top: 7px; padding: 12px; border-left: 4px solid var(--forest); background: var(--forest-soft); font-size: 13px; }
.ingest-status span { color: var(--muted); }
.ingest-status.working, .ingest-status.duplicate { border-color: #b48620; background: var(--gold-soft); }
.ingest-status.failure { border-color: var(--danger); background: #fff0ee; }

.book-dialog { position: relative; margin: auto; max-height: 86dvh; padding: 28px 22px; border-radius: 16px; }
.book-dialog > .dialog-close { position: sticky; z-index: 1; top: 0; float: right; margin: -10px -8px 0 8px; }
.book-dialog h2 { margin: 0 0 4px; font-size: 27px; letter-spacing: -0.03em; }
.book-dialog h3 { margin: 24px 0 6px; }
.dialog-author, .dialog-summary { color: var(--muted); }
.source-link { display: inline-flex; min-height: 44px; align-items: center; color: var(--forest); font-weight: 750; }
.dialog-media { width: 100%; margin: 16px 0 8px; aspect-ratio: 4 / 3; border-radius: 12px; background: #17251e; }
.dialog-media img { object-fit: contain; background: #17251e; }
.notes-list { padding-left: 20px; }
.notes-list li { margin: 8px 0; }

/* Shared authentication and settings surfaces. */
.login-page { min-height: 100vh; background: var(--paper); }
.login-shell { display: grid; min-height: 100vh; place-items: center; padding: 24px; }
.login-card { width: min(430px, 100%); padding: 36px; border: 1px solid var(--border); border-radius: 16px; background: white; box-shadow: var(--shadow); }
.login-card .brand-mark { margin-bottom: 24px; }
.login-card h1 { margin: 0; font-size: 35px; letter-spacing: -0.04em; }
.login-card h2 { margin: 0 0 8px; font-size: 21px; }
.login-intro { margin: 10px 0 22px; color: var(--muted); }
.login-form { display: grid; gap: 7px; }
.login-form label, .login-card form:not(.login-form) label { display: block; margin-top: 8px; font-size: 13px; font-weight: 750; }
.privacy-note, .manual-secret { overflow-wrap: anywhere; color: var(--faint); font-size: 12px; text-align: center; }
.optional-label { color: var(--faint); font-size: 11px; font-weight: 500; }
.settings-divider { margin: 28px 0; border: 0; border-top: 1px solid var(--border); }
.back-link { display: inline-flex; min-height: 44px; align-items: center; margin-bottom: 16px; color: var(--muted); font-weight: 700; text-decoration: none; }
.ghost-button { min-height: 44px; padding: 8px 13px; border: 1px solid var(--border); border-radius: 8px; background: transparent; color: var(--muted); font-weight: 700; }
.button-link { display: inline-flex; align-items: center; text-decoration: none; }
.danger-zone { margin-top: 22px; color: var(--muted); }
.danger-zone summary { min-height: 44px; cursor: pointer; font-weight: 700; }
.danger-zone .login-form { margin-top: 12px; }
.totp-qr { display: block; width: 240px; max-width: 100%; margin: 20px auto; }

@media (min-width: 700px) {
  .app-header { padding-top: 26px; }
  .book-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 22px; }
  .book-summary { display: -webkit-box; overflow: hidden; margin-top: 8px; -webkit-box-orient: vertical; -webkit-line-clamp: 2; }
  .tag-list { display: flex; flex-wrap: wrap; }
  .bottom-nav { right: 50%; left: auto; width: min(620px, calc(100% - 32px)); transform: translateX(50%); border: 1px solid var(--border); border-bottom: 0; border-radius: 16px 16px 0 0; }
  .sheet-dialog { margin: auto; border-radius: 18px; }
  .sheet-content { padding: 18px 28px 28px; }
  .sheet-handle { display: none; }
}

@media (min-width: 980px) {
  .book-grid { grid-template-columns: repeat(4, minmax(0, 1fr)); }
}

@media (max-width: 380px) {
  .app-header, .library-shell { width: calc(100% - 24px); }
  .app-panel h2, .catalog-heading h2 { font-size: 24px; }
  .book-grid { gap: 10px; }
  .book-card h3 { font-size: 14px; }
  .nav-item small { font-size: 9px; }
  .login-card { padding: 28px 20px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { scroll-behavior: auto !important; transition-duration: 0.01ms !important; animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; }
}
