/* AI Lessons reader. Themes: light (default), dark, sepia; "auto" follows the OS. */
:root {
  --font-scale: 1;
  --bg: #fbfaf7;
  --surface: #ffffff;
  --surface-2: #f3f1ec;
  --text: #1f2328;
  --muted: #5b6168;
  --faint: #8a9098;
  --border: #e3e0d8;
  --accent: #2f6fed;
  --accent-soft: #e6eefe;
  --done: #1a7f37;
  --code-bg: #f6f5f1;
  --code-text: #24292f;
  --hl-keyword: #cf222e;
  --hl-string: #0a3069;
  --hl-comment: #6e7781;
  --hl-number: #0550ae;
  --hl-title: #8250df;
  --hl-attr: #116329;
  --hl-meta: #953800;
  --shadow: 0 1px 2px rgb(0 0 0 / 5%), 0 4px 16px rgb(0 0 0 / 5%);
  --serif: "Iowan Old Style", "Charter", "Palatino Linotype", Palatino, Georgia, "Times New Roman", serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: "JetBrains Mono", "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
  --measure: 70ch;
  color-scheme: light;
}
:root[data-theme="sepia"] {
  --bg: #f4ecd8;
  --surface: #f8f1e0;
  --surface-2: #ece2c9;
  --text: #3b2f22;
  --muted: #6b5a45;
  --faint: #93826b;
  --border: #dccfb2;
  --accent: #9a4f14;
  --accent-soft: #eddcbf;
  --code-bg: #efe5cc;
  --code-text: #3b2f22;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]):not([data-theme="sepia"]) {
    --bg: #15171a; --surface: #1c1f23; --surface-2: #23272c; --text: #e6e6e3; --muted: #a4a9b0; --faint: #7c828a;
    --border: #2f343a; --accent: #6ea8ff; --accent-soft: #1f2d45; --done: #4ac26b; --code-bg: #1f2227; --code-text: #e1e4e8;
    --hl-keyword: #ff7b72; --hl-string: #a5d6ff; --hl-comment: #8b949e; --hl-number: #79c0ff; --hl-title: #d2a8ff;
    --hl-attr: #7ee787; --hl-meta: #ffa657; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 30%);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171a; --surface: #1c1f23; --surface-2: #23272c; --text: #e6e6e3; --muted: #a4a9b0; --faint: #7c828a;
  --border: #2f343a; --accent: #6ea8ff; --accent-soft: #1f2d45; --done: #4ac26b; --code-bg: #1f2227; --code-text: #e1e4e8;
  --hl-keyword: #ff7b72; --hl-string: #a5d6ff; --hl-comment: #8b949e; --hl-number: #79c0ff; --hl-title: #d2a8ff;
  --hl-attr: #7ee787; --hl-meta: #ffa657; --shadow: 0 1px 2px rgb(0 0 0 / 30%), 0 4px 16px rgb(0 0 0 / 30%);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-padding-top: 4.5rem; }
body { margin: 0; background: var(--bg); color: var(--text); font: 16px/1.5 var(--sans); }
a { color: var(--accent); }
svg { width: 1.25rem; height: 1.25rem; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
[hidden] { display: none !important; }

/* Reading progress along the top edge */
.progress { position: fixed; inset: 0 0 auto 0; height: 3px; z-index: 30; pointer-events: none; }
.progress-bar { height: 100%; width: 0; background: var(--accent); transition: width 0.1s linear; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 1rem;
  padding: calc(0.6rem + env(safe-area-inset-top, 0px)) max(1rem, env(safe-area-inset-right)) 0.6rem max(1rem, env(safe-area-inset-left));
  background: color-mix(in srgb, var(--bg) 88%, transparent); backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 0.5rem; color: var(--text); text-decoration: none; font-weight: 650; white-space: nowrap; }
.crumbs { flex: 1; min-width: 0; color: var(--muted); font-size: 0.9rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.crumbs a { color: var(--muted); text-decoration: none; }
.crumbs a:hover { color: var(--text); }
.crumbs .sep { margin: 0 0.4rem; color: var(--faint); }
.tools { display: flex; gap: 0.25rem; }
.tool { display: grid; place-items: center; width: 2.25rem; height: 2.25rem; border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; }
.tool:hover { background: var(--surface-2); color: var(--text); }

/* Buttons and meters */
.button { display: inline-block; padding: 0.55rem 1rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--text); font: inherit; font-weight: 550; text-decoration: none; cursor: pointer; }
.button:hover { border-color: var(--faint); }
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
:root[data-theme="dark"] .button.primary { color: #0b1220; }
.meter { height: 6px; border-radius: 3px; background: var(--surface-2); overflow: hidden; margin: 0.75rem 0 0.35rem; }
.meter-fill { height: 100%; width: 0; background: var(--accent); border-radius: 3px; }
.meta, .book-status, .hint { color: var(--muted); font-size: 0.875rem; margin: 0.25rem 0; }
.eyebrow { margin: 0 0 0.35rem; color: var(--muted); font-size: 0.8rem; letter-spacing: 0.04em; text-transform: uppercase; }

/* Library */
.library, .book, .search { max-width: 64rem; margin: 0 auto; padding: 2rem 1rem 4rem; }
.library h1 { font: 700 2rem/1.2 var(--serif); margin: 0.5rem 0 1.5rem; }
.shelf h2 { font-size: 1.1rem; margin: 2rem 0 0.25rem; }
.shelf-desc { margin: 0 0 1rem; color: var(--muted); }
.books { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(100%, 28rem), 1fr)); gap: 1rem; }
.book-card { display: flex; gap: 1rem; padding: 1rem; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); color: inherit; text-decoration: none; box-shadow: var(--shadow); transition: transform 0.15s, border-color 0.15s; }
.book-card:hover { transform: translateY(-2px); border-color: var(--faint); }
.book-info { min-width: 0; flex: 1; }
.book-info h3 { margin: 0; font: 650 1.2rem/1.3 var(--serif); }
.subtitle { margin: 0.2rem 0 0; color: var(--muted); }
.desc { margin: 0.6rem 0 0; font-size: 0.925rem; line-height: 1.55; }
.spine {
  flex: none; width: 4.5rem; min-height: 7rem; border-radius: 4px 8px 8px 4px; padding: 0.6rem 0.4rem;
  background: linear-gradient(90deg, hsl(var(--hue) 45% 30%) 0 10%, hsl(var(--hue) 50% 42%) 10%);
  color: #fff; display: flex; align-items: flex-end; box-shadow: inset -2px 0 0 rgb(255 255 255 / 15%), var(--shadow);
}
.spine span { font: 600 0.7rem/1.2 var(--serif); }
.spine.big { width: 7rem; min-height: 10rem; }
.spine.big span { font-size: 0.85rem; }
.continue-banner { display: flex; flex-wrap: wrap; align-items: baseline; gap: 0.5rem 1rem; padding: 1rem 1.25rem; border-radius: 12px; background: var(--accent-soft); margin-bottom: 1rem; }
.continue-banner .label { font-size: 0.8rem; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.continue-banner a { font-weight: 600; text-decoration: none; }

/* Book contents */
.book-head { display: flex; gap: 1.5rem; align-items: flex-start; margin-bottom: 1.5rem; }
.book-head h1 { font: 700 2.1rem/1.2 var(--serif); margin: 0; }
.book-actions { display: flex; align-items: center; gap: 1rem; flex-wrap: wrap; margin-top: 0.75rem; }
.part h2 { font-size: 0.8rem; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); margin: 2rem 0 0.5rem; }
.toc { list-style: none; margin: 0; padding: 0; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); overflow: hidden; }
.toc li + li { border-top: 1px solid var(--border); }
.toc a { display: grid; grid-template-columns: 2rem 1fr auto 1.5rem; align-items: center; gap: 0.75rem; padding: 0.8rem 1rem; color: inherit; text-decoration: none; }
.toc a:hover { background: var(--surface-2); }
.toc .num { color: var(--faint); font-variant-numeric: tabular-nums; text-align: right; }
.toc .t { font-family: var(--serif); font-size: 1.05rem; }
.toc .mins { color: var(--faint); font-size: 0.85rem; white-space: nowrap; }
.state { display: inline-block; width: 1rem; height: 1rem; border-radius: 50%; border: 1.5px solid var(--border); flex: none; }
.state.partial { border-color: var(--accent); background: conic-gradient(var(--accent) calc(var(--p, 0) * 1%), transparent 0); }
.state.done { border-color: var(--done); background: var(--done); position: relative; }
.state.done::after { content: ""; position: absolute; left: 0.3rem; top: 0.12rem; width: 0.25rem; height: 0.5rem; border: solid #fff; border-width: 0 2px 2px 0; transform: rotate(45deg); }

/* Chapter layout: sidebar + text */
.reading { display: grid; grid-template-columns: minmax(15rem, 18rem) minmax(0, 1fr); gap: 2.5rem; max-width: 76rem; margin: 0 auto; padding: 0 1rem; }
.sidebar { position: sticky; top: 3.75rem; align-self: start; max-height: calc(100vh - 4rem); overflow: auto; padding: 1.5rem 0 2rem; font-size: 0.875rem; }
.sidebar-toggle { display: none; }
.on-page-title { margin: 0 0 0.5rem; font-weight: 650; }
.on-page-title a { color: var(--text); text-decoration: none; }
.part-title { margin: 1rem 0 0.25rem; color: var(--faint); font-size: 0.75rem; letter-spacing: 0.05em; text-transform: uppercase; }
.book-nav ol, .on-page ul { list-style: none; margin: 0; padding: 0; }
.book-nav li a { display: flex; align-items: center; gap: 0.5rem; padding: 0.3rem 0.5rem; border-radius: 6px; color: var(--muted); text-decoration: none; line-height: 1.35; }
.book-nav li a:hover { background: var(--surface-2); color: var(--text); }
.book-nav li.current a { background: var(--accent-soft); color: var(--text); font-weight: 600; }
.book-nav .state { width: 0.75rem; height: 0.75rem; }
.book-nav .state.done::after { left: 0.2rem; top: 0.05rem; width: 0.2rem; height: 0.4rem; }
.on-page { margin-top: 1.5rem; padding-top: 1rem; border-top: 1px solid var(--border); }
.on-page li a { display: block; padding: 0.25rem 0.5rem; border-left: 2px solid transparent; color: var(--muted); text-decoration: none; line-height: 1.35; }
.on-page li a.active { border-left-color: var(--accent); color: var(--text); }

.chapter { min-width: 0; max-width: calc(var(--measure) + 2rem); padding: 2rem 0 4rem; }
.chapter-head h1 { font: 700 calc(2.2rem * var(--font-scale))/1.2 var(--serif); margin: 0 0 1.5rem; letter-spacing: -0.01em; }

/* The text itself */
.prose { font: calc(1.125rem * var(--font-scale))/1.7 var(--serif); overflow-wrap: break-word; }
.prose > * { max-width: var(--measure); }
.prose h2, .prose h3, .prose h4 { font-family: var(--sans); line-height: 1.3; margin: 2.2em 0 0.6em; }
.prose h2 { font-size: 1.35em; padding-top: 0.5em; border-top: 1px solid var(--border); }
.prose h3 { font-size: 1.12em; }
.prose h2 a, .prose h3 a, .prose h4 a { color: inherit; text-decoration: none; }
.prose p, .prose ul, .prose ol { margin: 0 0 1em; }
.prose li { margin: 0.25em 0; }
.prose li > p { margin: 0.25em 0; }
.prose blockquote { margin: 1.5em 0; padding: 0.25em 1em; border-left: 3px solid var(--accent); color: var(--muted); }
.prose hr { border: 0; border-top: 1px solid var(--border); margin: 2.5em 0; }
.prose img { max-width: 100%; height: auto; border-radius: 8px; border: 1px solid var(--border); }
.prose :not(pre) > code { font: 0.85em var(--mono); background: var(--code-bg); padding: 0.12em 0.35em; border-radius: 4px; border: 1px solid var(--border); overflow-wrap: anywhere; }
.table-wrap { max-width: 100%; overflow-x: auto; margin: 1.25em 0; border: 1px solid var(--border); border-radius: 8px; }
.prose table { border-collapse: collapse; font: 0.8em/1.45 var(--sans); min-width: 100%; }
.prose th, .prose td { padding: 0.55em 0.75em; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.prose th { background: var(--surface-2); font-weight: 650; }
.prose tr:last-child td { border-bottom: 0; }
.prose details { margin: 1.25em 0; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); }
.prose details > summary { cursor: pointer; padding: 0.7em 1em; font: 600 0.85em var(--sans); color: var(--muted); list-style-position: inside; }
.prose details[open] > summary { border-bottom: 1px solid var(--border); color: var(--text); }
.prose details > *:not(summary) { margin-left: 1rem; margin-right: 1rem; }
.prose details > pre.code { margin: 1rem; }

/* Code blocks */
pre.code { position: relative; max-width: 100%; margin: 1.25em 0; padding: 1em 1.1em; overflow-x: auto; background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; font: calc(0.8rem * var(--font-scale))/1.55 var(--mono); color: var(--code-text); tab-size: 2; }
pre.code code { font: inherit; white-space: pre; }
.code-lang { position: absolute; top: 0.35rem; right: 0.6rem; font: 0.7rem var(--sans); color: var(--faint); text-transform: lowercase; }
.copy { position: absolute; top: 0.35rem; right: 0.35rem; padding: 0.2rem 0.5rem; border: 1px solid var(--border); border-radius: 6px; background: var(--surface); color: var(--muted); font: 0.75rem var(--sans); cursor: pointer; opacity: 0; transition: opacity 0.15s; }
pre.code:hover .copy, .copy:focus { opacity: 1; }
pre.code:hover .code-lang { opacity: 0; }
.hljs-keyword, .hljs-selector-tag, .hljs-built_in.hljs-keyword, .hljs-doctag { color: var(--hl-keyword); }
.hljs-string, .hljs-regexp, .hljs-symbol, .hljs-template-variable, .hljs-addition { color: var(--hl-string); }
.hljs-comment, .hljs-quote { color: var(--hl-comment); font-style: italic; }
.hljs-number, .hljs-literal, .hljs-variable, .hljs-template-tag, .hljs-selector-id, .hljs-selector-class { color: var(--hl-number); }
.hljs-title, .hljs-section, .hljs-title.function_, .hljs-title.class_ { color: var(--hl-title); }
.hljs-attr, .hljs-attribute, .hljs-name, .hljs-tag, .hljs-bullet { color: var(--hl-attr); }
.hljs-meta, .hljs-built_in, .hljs-type, .hljs-params, .hljs-deletion { color: var(--hl-meta); }
.hljs-emphasis { font-style: italic; }
.hljs-strong { font-weight: 700; }

/* End of chapter */
.chapter-end { max-width: var(--measure); margin: 3rem 0 1.5rem; display: flex; justify-content: center; }
.chapter-end .button.is-done { border-color: var(--done); color: var(--done); }
.pager { max-width: var(--measure); display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
.pager a { display: block; padding: 1rem; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); text-decoration: none; font-family: var(--serif); }
.pager a:hover { border-color: var(--accent); }
.pager a span { display: block; margin-bottom: 0.25rem; color: var(--muted); font: 0.8rem var(--sans); }
.pager .next { text-align: right; }

/* Search */
.search h1 { font: 700 2rem/1.2 var(--serif); margin: 0.5rem 0 1rem; }
.search input { width: 100%; padding: 0.8rem 1rem; font: inherit; font-size: 1.1rem; border: 1px solid var(--border); border-radius: 10px; background: var(--surface); color: var(--text); }
.search input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.results { list-style: none; margin: 1rem 0 0; padding: 0; }
.results li { padding: 1rem 0; border-bottom: 1px solid var(--border); }
.results a { font: 600 1.1rem var(--serif); text-decoration: none; }
.results .where { color: var(--muted); font-size: 0.85rem; margin: 0.15rem 0 0.35rem; }
.results .snippet { margin: 0; color: var(--muted); font-size: 0.925rem; }
.results mark { background: var(--accent-soft); color: var(--text); border-radius: 3px; padding: 0 0.1em; }

/* Settings dialog and toast */
dialog.settings { width: min(26rem, calc(100vw - 2rem)); border: 1px solid var(--border); border-radius: 14px; padding: 1.25rem; background: var(--surface); color: var(--text); box-shadow: var(--shadow); }
dialog.settings::backdrop { background: rgb(0 0 0 / 35%); }
dialog.settings h2 { margin: 0 0 1rem; font-size: 1.1rem; }
dialog.settings fieldset { border: 0; padding: 0; margin: 0 0 1.1rem; }
dialog.settings legend { font-weight: 600; margin-bottom: 0.4rem; font-size: 0.9rem; }
dialog.settings label { display: inline-flex; align-items: center; gap: 0.3rem; margin-right: 0.9rem; }
dialog.settings textarea { width: 100%; margin-top: 0.5rem; font: 0.75rem var(--mono); background: var(--surface-2); color: var(--text); border: 1px solid var(--border); border-radius: 6px; }
.size-row { display: flex; align-items: center; gap: 0.5rem; flex-wrap: wrap; }
.size-row button, .dialog-actions button { padding: 0.4rem 0.8rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface-2); color: var(--text); font: inherit; cursor: pointer; }
.size-row output { min-width: 3.5rem; text-align: center; font-variant-numeric: tabular-nums; }
.size-row .danger { color: #cf222e; }
.dialog-actions { display: flex; justify-content: flex-end; }
.toast { position: fixed; left: 50%; bottom: calc(1.25rem + env(safe-area-inset-bottom, 0px)); transform: translateX(-50%); z-index: 40; display: flex; align-items: center; gap: 0.75rem; padding: 0.65rem 0.9rem; border-radius: 10px; background: var(--text); color: var(--bg); font-size: 0.9rem; box-shadow: var(--shadow); max-width: calc(100vw - 2rem); }
.toast button { border: 0; background: transparent; color: inherit; text-decoration: underline; font: inherit; cursor: pointer; padding: 0; }

/* Narrow screens: the sidebar becomes a drawer behind a "Contents" button */
@media (max-width: 60rem) {
  .reading { display: block; }
  .sidebar { position: sticky; top: 3.4rem; z-index: 10; max-height: none; overflow: visible; padding: 0.5rem 0 0; background: var(--bg); }
  .sidebar-toggle { display: inline-flex; align-items: center; gap: 0.4rem; padding: 0.4rem 0.7rem; border: 1px solid var(--border); border-radius: 8px; background: var(--surface); color: var(--muted); font: inherit; font-size: 0.85rem; cursor: pointer; }
  .sidebar-body { display: none; position: absolute; left: 0; right: 0; top: 100%; max-height: 70vh; overflow: auto; padding: 1rem; border: 1px solid var(--border); border-radius: 12px; background: var(--surface); box-shadow: var(--shadow); }
  .sidebar.open .sidebar-body { display: block; }
  .chapter { padding-top: 1rem; }
  .book-head { flex-direction: column; }
  .spine.big { width: 100%; min-height: 4rem; border-radius: 8px; }
  .toc a { grid-template-columns: 1.5rem 1fr 1.25rem; }
  .toc .mins { display: none; }
  .crumbs { display: none; }
  .brand span { display: inline; }
  .topbar { justify-content: space-between; }
}
@media (max-width: 30rem) {
  .prose { font-size: calc(1.0625rem * var(--font-scale)); }
  .pager { grid-template-columns: 1fr; }
  .pager .next { text-align: left; }
  .spine:not(.big) { width: 3rem; min-height: 5.5rem; }
}
@media print {
  .topbar, .sidebar, .progress, .pager, .chapter-end, .toast { display: none !important; }
  .reading { display: block; }
}
