:root {
  --bg: #fbfaf7;
  --panel: #ffffff;
  --text: #1d1d1f;
  --muted: #6b6b70;
  --border: #e3e1dc;
  --accent: #3559c7;
  --sent-bg: #fff3c4;
  --word-bg: #ffd54a;
  --note-bg: #eef2fc;
  --note-border: #c9d4f3;
  --warn: #b25d00;
  --serif: "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #17181b; --panel: #202125; --text: #e7e6e3; --muted: #9a9aa0; --border: #34353a;
    --accent: #8aa6ff; --sent-bg: #3d3520; --word-bg: #7a5d00; --note-bg: #1f2638; --note-border: #34406a;
    --warn: #ffb054;
  }
  .doc img, .page-pane img { filter: brightness(.92); }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--text); font-family: var(--sans); }
[hidden] { display: none !important; }
.muted { color: var(--muted); }

.btn {
  font: inherit; font-size: 14px; border: 1px solid var(--border); background: var(--panel); color: var(--text);
  border-radius: 8px; padding: 6px 12px; cursor: pointer; text-decoration: none; white-space: nowrap;
}
.btn:hover { border-color: var(--accent); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { border-color: var(--border); }
.btn.ghost.on { border-color: var(--accent); color: var(--accent); }
.btn.primary { background: var(--accent); color: white; border-color: var(--accent); }
.btn.icon { width: 40px; height: 40px; padding: 0; font-size: 16px; }
.btn.small { font-size: 12px; padding: 2px 8px; }
.btn.danger:hover { border-color: #c33; color: #c33; }

/* Library */
#library { max-width: 760px; margin: 0 auto; padding: 48px 16px; }
.lib-header h1 { margin: 0 0 4px; }
.dropzone {
  display: block; margin: 24px 0; padding: 40px 16px; text-align: center; border: 2px dashed var(--border);
  border-radius: 12px; cursor: pointer; background: var(--panel);
}
.dropzone.drag, .dropzone:hover { border-color: var(--accent); }
.paper-list { list-style: none; padding: 0; margin: 0; }
.paper-list li {
  display: flex; align-items: center; gap: 12px; padding: 12px 16px; border: 1px solid var(--border);
  border-radius: 10px; margin-bottom: 8px; background: var(--panel);
}
.paper-list .info { flex: 1; min-width: 0; }
.paper-list .title { font-weight: 600; color: var(--text); text-decoration: none; display: block;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.paper-list .sub { font-size: 13px; color: var(--muted); }
.paper-list .err { color: var(--warn); }
.bar { height: 4px; background: var(--border); border-radius: 2px; margin-top: 6px; overflow: hidden; }
.bar > div { height: 100%; background: var(--accent); transition: width .3s; }

/* Reader */
#reader { display: flex; flex-direction: column; height: 100vh; height: 100dvh; }
.reader-bar {
  display: flex; align-items: center; gap: 8px; padding: 8px 12px; border-bottom: 1px solid var(--border);
  background: var(--panel);
  padding-top: max(8px, env(safe-area-inset-top));
  padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right));
}
.doc-title { flex: 1; min-width: 0; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.settings {
  display: flex; flex-wrap: wrap; gap: 8px 20px; padding: 10px 16px; font-size: 14px;
  border-bottom: 1px solid var(--border); background: var(--panel);
}
.audio-prep { width: 100%; display: flex; align-items: center; gap: 10px; flex-wrap: wrap;
  border-top: 1px solid var(--border); padding-top: 8px; }
.reader-body { flex: 1; display: flex; min-height: 0; position: relative; }
.doc { flex: 1; overflow-y: auto; padding: 32px 16px 120px; }
.doc > * { max-width: 720px; margin-left: auto; margin-right: auto; }
.page-pane { width: 45%; max-width: 760px; border-left: 1px solid var(--border); overflow-y: auto; background: var(--panel);
  position: relative; }
.page-tools { position: absolute; right: 8px; display: flex; align-items: center; gap: 6px; }
.page-tools .warn { color: var(--warn); font-size: 12px; cursor: help; }
@media (max-width: 600px) { .page-tools .warn { display: none; } .page-tools .btn { padding: 4px 6px; } }
.page-pane-head { position: sticky; top: 0; z-index: 2; display: flex; align-items: center; justify-content: center; gap: 8px;
  padding: 6px; background: var(--panel); border-bottom: 1px solid var(--border); font-size: 14px; }
.pages { padding: 8px; display: flex; flex-direction: column; gap: 10px; }
.page-wrap { position: relative; width: 100%; max-width: 980px; margin: 0 auto; background: white;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .15); cursor: pointer; }
.page-wrap img { width: 100%; height: 100%; display: block; }
.reader-body.mode-pages .doc { display: none; }
.reader-body.mode-pages .page-pane { width: 100%; max-width: none; border-left: none; background: var(--bg); }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg .btn { border: none; border-radius: 0; background: transparent; padding: 5px 10px; }
.seg .btn + .btn { border-left: 1px solid var(--border); }
.seg .btn.on { background: var(--accent); color: white; }
.page-hl { position: absolute; inset: 0; pointer-events: none; }
.page-hl .hl { position: absolute; border-radius: 2px; background: color-mix(in srgb, var(--word-bg) 35%, transparent);
  mix-blend-mode: multiply; }
.page-hl .hl.word { background: color-mix(in srgb, var(--word-bg) 75%, transparent); box-shadow: 0 0 0 1px var(--word-bg); }
.page-hl .hl.fig { background: transparent; outline: 3px solid var(--word-bg); }
.processing { position: absolute; inset: 120px 0 auto; text-align: center; }

.doc { font-family: var(--serif); font-size: 19px; line-height: 1.65; }
.doc h1 { font-size: 30px; line-height: 1.25; margin: 0 auto 12px; }
.doc h2 { font-size: 24px; margin-top: 1.6em; }
.doc h3 { font-size: 20px; margin-top: 1.4em; }
.doc h4 { font-size: 19px; font-style: italic; }
.doc p { margin: 0 auto 1em; }
.doc .authors { font-family: var(--sans); font-size: 14px; color: var(--muted); white-space: pre-line; margin-bottom: 24px; }
.doc .list-item { padding-left: 1.4em; position: relative; }
.doc .list-item::before { content: "•"; position: absolute; left: .4em; color: var(--muted); }
.doc .footnote { font-size: 15px; color: var(--muted); border-top: 1px solid var(--border); padding-top: 4px; }
.doc .reference { font-size: 15px; padding-left: 1.5em; text-indent: -1.5em; margin-bottom: .4em; }

.s { border-radius: 3px; cursor: pointer; transition: background .15s; }
.s:hover { background: color-mix(in srgb, var(--sent-bg) 45%, transparent); }
.s.cur { background: var(--sent-bg); }
.w.cur { background: var(--word-bg); border-radius: 3px; box-shadow: 0 0 0 2px var(--word-bg); }

.cite { color: var(--accent); cursor: pointer; border-radius: 3px; }
sup.cite { font-family: var(--sans); font-size: .7em; padding: 0 1px; }
.fn-num { color: var(--muted); }
.cite:hover, .cite:focus-visible { background: var(--note-bg); outline: none; text-decoration: underline; }
.cite-pop {
  position: fixed; z-index: 20; max-height: 50vh; overflow-y: auto; background: var(--panel); color: var(--text);
  border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 8px 30px rgba(0, 0, 0, .18);
  padding: 8px 14px 12px; font-family: var(--serif); font-size: 15px; line-height: 1.5;
}
.cite-head { display: flex; align-items: center; justify-content: space-between; font-family: var(--sans);
  font-size: 13px; color: var(--muted); margin-bottom: 4px; }
.cite-ref { padding: 6px 0; border-top: 1px solid var(--border); }
.cite-ref:first-of-type { border-top: none; }
.cite-jump { display: inline-block; font-family: var(--sans); font-size: 12px; color: var(--accent); margin-left: 6px; }
.flash { animation: flash 1.6s ease-out; }
@keyframes flash { 0%, 40% { background: var(--sent-bg); } 100% { background: transparent; } }
.eq { display: flex; align-items: center; gap: 12px; margin: 1em auto; overflow-x: auto; }
.eq .katex-display { flex: 1; min-width: 0; margin: 0; overflow-x: auto; overflow-y: hidden; padding: 4px 0;
  position: relative; /* contain KaTeX's absolutely-positioned MathML so wide equations can't widen the page */ }
.doc { overflow-x: hidden; position: relative; } /* positioned so KaTeX MathML is clipped here */
.eq .eq-label { color: var(--muted); font-family: var(--sans); font-size: 15px; }
.eq.cur { background: var(--sent-bg); border-radius: 6px; }

figure { margin: 1.5em auto; text-align: center; }
figure img { max-width: 100%; height: auto; background: white; border-radius: 4px; }
figure.cur { outline: 3px solid var(--word-bg); outline-offset: 6px; border-radius: 4px; }
figcaption { font-family: var(--sans); font-size: 15px; line-height: 1.5; text-align: left; margin-top: 8px; color: var(--muted); }
figcaption b { color: var(--text); }
.table-wrap { overflow-x: auto; margin: 1em auto; }
.table-wrap table { border-collapse: collapse; font-family: var(--sans); font-size: 14px; margin: 0 auto; }
.table-wrap th, .table-wrap td { border-bottom: 1px solid var(--border); padding: 4px 10px; text-align: center; }
.table-wrap thead th { border-bottom: 2px solid var(--text); }
pre.code { text-align: left; font-size: 14px; background: var(--panel); border: 1px solid var(--border);
  padding: 12px; border-radius: 6px; overflow-x: auto; }

.note {
  font-family: var(--sans); font-size: 16px; line-height: 1.6; background: var(--note-bg); border: 1px solid var(--note-border);
  border-radius: 10px; padding: 10px 14px; margin: .6em auto 1.2em;
}
.note .note-head { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--accent); margin-bottom: 4px;
  display: flex; gap: 8px; align-items: center; }
.note .note-head a { color: inherit; }
.hide-notes .note:not(.playing) { display: none; }

.banner { font-family: var(--sans); font-size: 14px; background: var(--note-bg); border: 1px solid var(--note-border);
  border-radius: 10px; padding: 10px 14px; margin-bottom: 1.5em; display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }

.player {
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-top: 1px solid var(--border);
  background: var(--panel); flex-wrap: wrap;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  padding-left: max(16px, env(safe-area-inset-left)); padding-right: max(16px, env(safe-area-inset-right));
}
.speed { display: flex; align-items: center; gap: 8px; }
.speed input { width: 140px; }
#speed-label { font-variant-numeric: tabular-nums; width: 3.2em; font-size: 14px; }
#voice { font: inherit; font-size: 14px; padding: 6px; border-radius: 8px; border: 1px solid var(--border);
  background: var(--panel); color: var(--text); }
.status { font-size: 13px; flex: 1; text-align: right; }

.toast { position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%); background: #2b2b2f; color: white;
  padding: 10px 16px; border-radius: 8px; font-size: 14px; max-width: 90vw; z-index: 10; }

/* Settings */
.settings-view { max-width: 760px; margin: 0 auto; padding: 32px 16px 64px;
  padding-top: max(32px, env(safe-area-inset-top)); }
.lib-title { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.lib-title h1 { margin: 0; }
.settings-view fieldset { border: 1px solid var(--border); border-radius: 12px; background: var(--panel);
  padding: 12px 16px 16px; margin: 0 0 16px; min-width: 0; }
.settings-view legend { font-weight: 600; padding: 0 6px; }
.settings-view label { display: block; font-size: 14px; margin-top: 10px; }
.settings-view label.inline { display: flex; align-items: center; gap: 8px; }
.settings-view input:not([type=checkbox]), .settings-view select, .settings-view textarea {
  display: block; width: 100%; margin-top: 4px; font: inherit; font-size: 16px; /* 16px stops iOS zoom-on-focus */
  padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text);
}
.settings-view .model-row { display: flex; align-items: flex-end; gap: 8px; }
.settings-view .model-row label { flex: 1; min-width: 0; }
.settings-view .model-row .btn { height: 40px; }
.settings-view .model-pick { margin-top: 8px; }
.settings-view details { margin-top: 12px; font-size: 14px; }
.settings-view summary { cursor: pointer; color: var(--muted); }
.check-status { font-size: 13px; margin: 6px 0 0; color: var(--muted); overflow-wrap: anywhere; }
.check-status.good { color: #2e8b57; }
.check-status.bad { color: var(--warn); }
.small { font-size: 13px; }
.form-actions { display: flex; align-items: center; gap: 12px; flex-wrap: wrap; }
code { font-size: .9em; }

.touch-only { display: none; }
@media (hover: none) {
  .touch-only { display: inline; }
  .mouse-only { display: none; }
  .s:hover { background: none; }
  .s.cur:hover { background: var(--sent-bg); }
}

@media (max-width: 800px) {
  .page-pane { position: absolute; inset: 0; width: auto; max-width: none; z-index: 5; }
  .split-only { display: none; }
  .pages { padding: 4px; gap: 6px; }
  .doc { font-size: 17px; }
}

@media (max-width: 600px) {
  .wide-only { display: none; }
  #library { padding: 24px 16px; padding-top: max(24px, env(safe-area-inset-top)); }
  .settings-view { padding-top: max(20px, env(safe-area-inset-top)); }
  .lib-header h1, .settings-view h1 { font-size: 26px; }
  .reader-bar { gap: 2px; padding-left: max(6px, env(safe-area-inset-left)); padding-right: max(6px, env(safe-area-inset-right)); }
  .reader-bar .btn { padding: 8px; }
  .doc-title { font-size: 14px; }
  .doc { padding: 20px 16px 48px; font-size: 17px; line-height: 1.6; }
  .doc h1 { font-size: 24px; }
  .doc h2 { font-size: 20px; }
  .doc h3 { font-size: 18px; }
  .note { font-size: 15px; }
  .eq { gap: 6px; }
  .settings { gap: 10px; }
  .settings label { width: 100%; }
  /* Player: transport + speed on one row, voice + status below. */
  .player { gap: 8px; padding-top: 8px; }
  .player .btn.icon { width: 44px; height: 44px; }
  .speed { order: 10; flex-basis: 100%; }
  .speed input { width: auto; flex: 1; min-width: 0; height: 32px; }
  #voice { flex: 0 1 auto; max-width: 45%; }
  .status { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .toast { bottom: calc(130px + env(safe-area-inset-bottom)); }
  .cite-pop { left: 0; right: 0; bottom: 0; top: auto; width: auto; max-height: 60vh; border-radius: 16px 16px 0 0;
    padding-bottom: max(16px, env(safe-area-inset-bottom)); font-size: 16px; }
}

/* Accounts */
body:not(.is-admin) .admin-only { display: none !important; }
.user-nav { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; justify-content: flex-end; }
.user-nav #account-link { max-width: 180px; overflow: hidden; text-overflow: ellipsis; }

.auth-view { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: 24px 16px; padding-top: max(24px, env(safe-area-inset-top)); }
.auth-card { width: 100%; max-width: 380px; background: var(--panel); border: 1px solid var(--border);
  border-radius: 14px; padding: 24px; box-shadow: 0 6px 24px rgba(0, 0, 0, .06); }
.auth-card h1 { margin: 0 0 8px; font-size: 26px; }
.auth-card label { display: block; font-size: 14px; margin-top: 12px; }
.auth-card input { display: block; width: 100%; margin-top: 4px; font: inherit; font-size: 16px; padding: 9px 11px;
  border: 1px solid var(--border); border-radius: 8px; background: var(--bg); color: var(--text); }
.auth-card input[readonly] { color: var(--muted); }
.auth-card .btn.primary { width: 100%; margin-top: 16px; padding: 10px; font-size: 15px; }
.auth-card .btn.primary:disabled { opacity: .5; cursor: not-allowed; }
.auth-card #auth-error:empty { display: none; }

.settings-view.wide { max-width: 980px; }
.user-list { display: flex; flex-direction: column; gap: 10px; }
.user-card { display: flex; gap: 12px; justify-content: space-between; align-items: flex-start; flex-wrap: wrap;
  border: 1px solid var(--border); border-radius: 10px; padding: 10px 12px; background: var(--bg); }
.user-card.inactive { opacity: .65; }
.user-main { flex: 1; min-width: 240px; display: flex; flex-direction: column; gap: 3px; }
.user-actions { display: flex; gap: 6px; flex-wrap: wrap; }
.badge { display: inline-block; font-family: var(--sans); font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
  padding: 1px 6px; border-radius: 999px; background: var(--note-bg); color: var(--accent); margin-left: 4px; }
.badge.off { background: transparent; border: 1px solid var(--warn); color: var(--warn); }
.badge.you { background: transparent; border: 1px solid var(--border); color: var(--muted); }
.link-result { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; margin-top: 8px; }
.link-result input { flex: 1; min-width: 200px; font: inherit; font-size: 14px; padding: 6px 8px; border: 1px solid var(--border);
  border-radius: 6px; background: var(--panel); color: var(--text); }
.link-result p { width: 100%; margin: 0; }
.plain-list { list-style: none; margin: 0; padding: 0; }
.plain-list li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 0;
  border-top: 1px solid var(--border); font-size: 14px; }
.plain-list li:first-child { border-top: none; }
@media (max-width: 600px) {
  .lib-title { flex-wrap: wrap; }
  .user-nav .btn { padding: 6px 8px; }
}
