:root {
  --bg: #f5f2ea;
  --surface: #fffdf8;
  --surface-2: #ece7da;
  --text: #252720;
  --muted: #6a6f65;
  --accent: #2f5b47;
  --accent-2: #c79845;
  --border: #d8d1c2;
  --highlight: #ffeca6;
  --font-scale: 1;
  --header-height: 64px;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #171b18;
  --surface: #202620;
  --surface-2: #2b332d;
  --text: #eef0e9;
  --muted: #b3baaf;
  --accent: #8fc6a5;
  --accent-2: #e1ba72;
  --border: #3c463e;
  --highlight: #735e18;
  color-scheme: dark;
}

* { box-sizing: border-box; }

html { scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, Aptos, "Segoe UI", sans-serif;
}

button, input, textarea { font: inherit; }
button { cursor: pointer; }

.app-header {
  position: fixed;
  inset: 0 0 auto;
  z-index: 20;
  height: var(--header-height);
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 18px;
  color: #fff;
  background: #24392f;
  box-shadow: 0 2px 12px #0003;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-mark {
  display: grid; place-items: center; width: 38px; height: 38px;
  border: 1px solid #ffffff80; border-radius: 50%;
  font: 700 21px Georgia, serif; color: #f4dba9;
}
.brand strong, .brand small { display: block; }
.brand strong { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.brand small { color: #d8e1da; margin-top: 2px; }
.header-actions { margin-left: auto; display: flex; gap: 7px; align-items: center; }

.icon-button, .text-button, .reader-toolbar button, .reader-footer button,
.annotation-actions button, .backup-tools button, .file-button, .search-row button {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  padding: 8px 11px;
}
.app-header .icon-button, .app-header .text-button {
  color: #fff; background: #ffffff12; border-color: #ffffff3d;
}
.icon-button { min-width: 38px; font-size: 20px; line-height: 1; }
.text-button { font-weight: 650; }
.count-badge { display: inline-block; min-width: 19px; padding: 1px 5px; border-radius: 10px; background: #fff2; }

.app-shell {
  display: grid;
  grid-template-columns: 310px minmax(0, 1fr) 330px;
  min-height: 100vh;
  padding-top: var(--header-height);
}

.sidebar, .notes-panel {
  position: sticky;
  top: var(--header-height);
  height: calc(100vh - var(--header-height));
  overflow: auto;
  background: var(--surface);
}
.sidebar { border-right: 1px solid var(--border); }
.notes-panel { border-left: 1px solid var(--border); }
.sidebar-tools, .notes-header, .annotation-actions, .backup-tools { padding: 16px; }
.sidebar-tools { position: sticky; top: 0; z-index: 2; background: var(--surface); border-bottom: 1px solid var(--border); }
.sidebar-tools label { display: block; font-size: 13px; font-weight: 700; margin-bottom: 6px; }
.search-row { display: grid; grid-template-columns: 1fr auto; gap: 6px; }
.search-row input { min-width: 0; padding: 9px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
.link-button { border: 0; background: none; color: var(--accent); padding: 10px 0 0; text-decoration: underline; }
.status { color: var(--muted); font-size: 13px; padding: 8px 16px; }
.library-tabs { display: grid; grid-template-columns: repeat(3, 1fr); border-bottom: 1px solid var(--border); }
.library-tabs button { border: 0; border-bottom: 3px solid transparent; padding: 10px 4px; background: var(--surface); color: var(--muted); font-size: 13px; }
.library-tabs button.active { color: var(--accent); border-bottom-color: var(--accent); font-weight: 750; }

.navigation details { border-bottom: 1px solid var(--border); }
.navigation summary { padding: 12px 16px; font-weight: 750; cursor: pointer; background: var(--surface-2); }
.nav-paper, .search-result {
  display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--border);
  background: transparent; color: var(--text); padding: 10px 16px;
}
.nav-paper:hover, .nav-paper.active, .search-result:hover { background: color-mix(in srgb, var(--accent) 12%, transparent); }
.nav-paper.active { border-left: 4px solid var(--accent); padding-left: 12px; }
.search-result strong { display: block; margin-bottom: 4px; }
.search-result small { color: var(--muted); line-height: 1.35; }
.search-result mark.search-match { background: #ffe184; color: #352700; border-radius: 3px; font-weight: 800; padding: 0 .08em; }
[data-theme="dark"] .search-result mark.search-match { background: #806819; color: #fff8d4; }

.reader { min-width: 0; padding: 18px clamp(18px, 5vw, 70px) 60px; }
.reader-toolbar, .reader-footer { display: flex; justify-content: space-between; align-items: center; max-width: 880px; margin: 0 auto 18px; }
.reader-footer { margin-top: 35px; }
.font-controls { display: flex; gap: 5px; }
.reading-actions { display: flex; gap: 5px; }
.paper {
  max-width: 820px;
  min-height: 70vh;
  margin: auto;
  padding: clamp(28px, 5vw, 64px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 4px;
  box-shadow: 0 16px 40px #0000000f;
  font: calc(18px * var(--font-scale))/1.7 Georgia, Cambria, serif;
}
.paper h1, .paper h2, .paper h3 { color: var(--text); line-height: 1.25; }
.paper h1 { font-size: 2rem; margin: 0 0 0.35em; }
.paper .paper-meta { color: var(--muted); font: 0.78rem/1.4 Inter, sans-serif; margin-bottom: 2.2rem; }
.paper h2 { font-size: 1.38rem; margin: 2.1em 0 0.55em; }
.paper h3 { font-size: 1.04rem; margin: 1.65em 0 0.4em; font-style: italic; }
.paper p { margin: 0 0 0.92em; }
.paper li { margin: 0.35em 0; }
.paper .note { font-style: italic; color: var(--muted); }
.paper .bible-ref { display: inline; border: 0; padding: 0; background: none; font: inherit; color: var(--accent); text-decoration: underline; text-decoration-style: dotted; }
.paper mark.reader-highlight { color: inherit; border-radius: 2px; cursor: pointer; outline-offset: 2px; }
.paper mark.reader-highlight:hover { box-shadow: 0 0 0 2px color-mix(in srgb, var(--accent) 52%, transparent); }
.paper mark.reader-highlight.yellow { background: #ffeca6; }
.paper mark.reader-highlight.green { background: #bde8ba; }
.paper mark.reader-highlight.blue { background: #bcdff5; }
.paper mark.reader-highlight.pink { background: #f4c4d8; }
.paper mark.reader-search-hit { background: #ffd45c; color: #2e2400; border-radius: 3px; padding: 0 .06em; box-shadow: 0 0 0 1px #d99e00; }
.paper mark.reader-search-hit.current { animation: search-pulse 1.2s ease-out 1; }
[data-theme="dark"] .paper mark.reader-highlight.yellow { background: #735e18; }
[data-theme="dark"] .paper mark.reader-highlight.green { background: #285d35; }
[data-theme="dark"] .paper mark.reader-highlight.blue { background: #234f68; }
[data-theme="dark"] .paper mark.reader-highlight.pink { background: #69324b; }
[data-theme="dark"] .paper mark.reader-search-hit { background: #886d13; color: #fff8d4; box-shadow: 0 0 0 1px #d4ad32; }

@keyframes search-pulse {
  0%, 35% { box-shadow: 0 0 0 5px color-mix(in srgb, var(--accent-2) 62%, transparent); }
  100% { box-shadow: 0 0 0 1px #d99e00; }
}
.paper .front-portrait { display: block; max-width: 250px; width: 60%; height: auto; margin: 20px auto; }
.loading { color: var(--muted); text-align: center; padding: 80px 10px; }

.notes-panel { display: flex; flex-direction: column; }
.notes-header { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); }
.notes-header small { display: block; margin-top: 3px; color: var(--muted); }
.annotation-actions { display: grid; gap: 7px; }
.highlight-colors { display: flex; justify-content: center; gap: 12px; }
.highlight-colors button { width: 27px; height: 27px; min-width: 0; padding: 0; border-radius: 50%; border: 2px solid var(--surface); outline: 1px solid var(--border); }
.highlight-colors button.selected { outline: 3px solid var(--accent); }
.highlight-colors [data-color="yellow"] { background: #f4d66d; }
.highlight-colors [data-color="green"] { background: #8fd38b; }
.highlight-colors [data-color="blue"] { background: #84c7ed; }
.highlight-colors [data-color="pink"] { background: #eaa1c1; }
.hint { padding: 0 16px 12px; color: var(--muted); font-size: 13px; }
.annotation-list { padding: 0 16px; flex: 1; }
.annotation { border-top: 1px solid var(--border); padding: 13px 0; }
.annotation blockquote { margin: 0 0 7px; padding-left: 10px; border-left: 3px solid var(--accent-2); color: var(--muted); font-size: 13px; }
.annotation p { margin: 5px 0; white-space: pre-wrap; }
.annotation small { color: var(--muted); }
.annotation button { float: right; border: 0; background: none; color: #a33; }
.backup-tools { border-top: 1px solid var(--border); display: grid; gap: 8px; }
.file-button { text-align: center; }
.file-button input { display: none; }

dialog { width: min(540px, calc(100vw - 28px)); border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: var(--text); padding: 22px; }
dialog::backdrop { background: #0008; }
dialog h2 { margin-top: 0; }
dialog label { display: block; font-weight: 700; margin-bottom: 6px; }
dialog textarea { width: 100%; resize: vertical; padding: 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
dialog blockquote { color: var(--muted); max-height: 100px; overflow: auto; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.dialog-actions button { padding: 9px 15px; border: 1px solid var(--border); border-radius: 8px; }
.dialog-actions .primary { background: var(--accent); color: #fff; border-color: var(--accent); }

.toast { position: fixed; left: 50%; bottom: 24px; z-index: 50; transform: translate(-50%, 20px); opacity: 0; pointer-events: none; background: #20251f; color: #fff; padding: 10px 16px; border-radius: 8px; transition: 180ms ease; }
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.mobile-only { display: none; }

@media (max-width: 1080px) {
  .app-shell { grid-template-columns: 285px minmax(0, 1fr); }
  .notes-panel { position: fixed; z-index: 30; right: 0; top: var(--header-height); width: min(360px, 92vw); transform: translateX(102%); transition: transform 180ms ease; box-shadow: -10px 0 30px #0003; }
  .notes-panel.open { transform: translateX(0); }
}

@media (max-width: 760px) {
  .mobile-only { display: inline-grid; place-items: center; }
  .brand small { display: none; }
  .brand strong { max-width: 45vw; }
  .app-shell { display: block; }
  .sidebar { position: fixed; z-index: 30; left: 0; top: var(--header-height); width: min(330px, 92vw); transform: translateX(-102%); transition: transform 180ms ease; box-shadow: 10px 0 30px #0003; }
  .sidebar.open { transform: translateX(0); }
  .reader { padding: 12px 10px 45px; }
  .paper { padding: 26px 20px; border-left: 0; border-right: 0; font-size: calc(17px * var(--font-scale)); }
  .reader-toolbar { margin: 0 4px 12px; }
  .reader-toolbar button { padding: 7px 8px; }
  .reader-toolbar { flex-wrap: wrap; gap: 7px; }
  .reading-actions { order: 4; width: 100%; justify-content: center; }
}

@media print {
  .app-header, .sidebar, .notes-panel, .reader-toolbar, .reader-footer { display: none !important; }
  .app-shell { display: block; padding: 0; }
  .reader { padding: 0; }
  .paper { max-width: none; border: 0; box-shadow: none; padding: 0; }
}
