:root {
  color-scheme: light;
  --desktop: #d7dbe0;
  --panel: #f5f5f5;
  --panel-alt: #e8e9eb;
  --input: #ffffff;
  --text: #202020;
  --muted: #5d6268;
  --border: #8a8f96;
  --border-light: #c7cacf;
  --title: #345f91;
  --title-text: #ffffff;
  --accent: #174f8a;
  --accent-text: #ffffff;
  --focus: #0067c0;
  --danger: #a12622;
  --shadow: 2px 2px 0 rgb(0 0 0 / 18%);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --desktop: #181a1d;
  --panel: #24272b;
  --panel-alt: #30343a;
  --input: #16181b;
  --text: #ededed;
  --muted: #b0b5bc;
  --border: #6b7179;
  --border-light: #464b52;
  --title: #263f5d;
  --title-text: #ffffff;
  --accent: #7db4ee;
  --accent-text: #101820;
  --focus: #8bc4ff;
  --danger: #ff938c;
  --shadow: 2px 2px 0 rgb(0 0 0 / 50%);
}

:root[data-theme="sepia"] {
  color-scheme: light;
  --desktop: #d9cfbb;
  --panel: #f3ead7;
  --panel-alt: #e5d8c0;
  --input: #fffaf0;
  --text: #44392c;
  --muted: #746553;
  --border: #927f66;
  --border-light: #cbbba0;
  --title: #725838;
  --title-text: #fffaf0;
  --accent: #704f28;
  --accent-text: #ffffff;
  --focus: #315f8b;
  --danger: #9b3028;
  --shadow: 2px 2px 0 rgb(68 45 23 / 22%);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { min-height: 100%; }
body {
  min-height: 100vh;
  margin: 0;
  background: var(--desktop);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.35;
}
button, input, select { font: inherit; }
button, .button-link {
  min-height: 30px;
  border: 1px solid var(--border);
  border-radius: 2px;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  padding: 4px 12px;
  text-decoration: none;
}
button.secondary, .icon-button {
  background: var(--panel);
  color: var(--text);
}
button:hover, .button-link:hover { filter: brightness(0.94); }
button:active { box-shadow: inset 1px 1px 1px rgb(0 0 0 / 25%); }
button:disabled { cursor: not-allowed; opacity: 0.5; }
button:focus-visible, a:focus-visible, input:focus-visible, select:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 1px;
}
input, select {
  min-height: 30px;
  border: 1px solid var(--border);
  border-radius: 1px;
  background: var(--input);
  color: var(--text);
  padding: 3px 6px;
}
input[type="file"] { padding: 3px; }
a { color: var(--accent); }
h1, h2, p { margin-top: 0; }
h1, h2 { font: inherit; font-weight: 700; }
form { margin: 0; }
.skip-link {
  position: fixed;
  top: -60px;
  left: 8px;
  z-index: 30;
  border: 1px solid var(--border);
  background: var(--panel);
  padding: 7px;
}
.skip-link:focus { top: 8px; }
.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}
.field-note { color: var(--muted); font-size: 12px; }

.title-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 42px;
  border-bottom: 1px solid #1e3d60;
  background: var(--title);
  color: var(--title-text);
  padding: 5px 10px;
}
.title-bar > strong { font-size: 15px; }
.header-controls { display: flex; align-items: center; gap: 8px; }
.header-controls form { display: flex; }
.compact-field { display: flex; align-items: center; gap: 5px; font-size: 12px; }
.title-bar .compact-field { color: var(--title-text); }
.compact-field select { min-height: 28px; }

.desktop-shell {
  display: grid;
  gap: 12px;
  width: min(1400px, calc(100% - 20px));
  margin: 10px auto;
}
.window {
  border: 1px solid var(--border);
  border-radius: 2px;
  background: var(--panel);
  box-shadow: var(--shadow);
}
.window-title {
  min-height: 32px;
  border-bottom: 1px solid var(--border);
  background: var(--panel-alt);
  padding: 6px 9px;
}
.window-title h1 { margin: 0; }
.upload-toolbar {
  display: grid;
  grid-template-columns: auto minmax(220px, 1fr) auto auto;
  gap: 8px;
  align-items: center;
  padding: 9px;
}
.upload-toolbar > label { font-weight: 600; }
.status { padding: 0 9px 9px; font-size: 13px; }
.status:empty { display: none; }
.status p { margin: 0; }
.status ul { margin: 4px 0 0; padding-left: 20px; }
.error, .form-error { color: var(--danger); }

.library-title-row, .library-title-row > div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 9px;
}
.library-title-row #book-count { color: var(--muted); font-size: 12px; }
.view-toggle { display: flex; }
.view-toggle button { border-radius: 0; }
.view-toggle button + button { border-left: 0; }
.view-toggle button[aria-pressed="true"] {
  background: var(--input);
  box-shadow: inset 0 0 0 1px var(--accent);
  color: var(--text);
}
.filters {
  display: grid;
  grid-template-columns: minmax(220px, 2fr) minmax(110px, 0.7fr) minmax(150px, 0.9fr) auto;
  gap: 8px;
  align-items: end;
  border-bottom: 1px solid var(--border-light);
  padding: 8px 9px;
}
.filters label { display: grid; gap: 2px; color: var(--muted); font-size: 12px; }
.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(250px, 1fr));
  gap: 9px;
  padding: 9px;
}
.book-card {
  min-width: 0;
  border: 1px solid var(--border);
  border-radius: 2px;
  background: var(--input);
  padding: 9px;
}
.book-card-heading {
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 8px;
  min-height: 34px;
  margin-bottom: 7px;
  border-bottom: 1px solid var(--border-light);
}
.book-card h2 { margin: 0 0 6px; font-size: 15px; overflow-wrap: anywhere; }
.card-field { display: grid; grid-template-columns: 65px minmax(0, 1fr); gap: 6px; margin: 4px 0; font-size: 12px; }
.card-field > span:first-child { color: var(--muted); }
.card-field > span:last-child { overflow-wrap: anywhere; }
.book-card .action-links { margin-top: 9px; border-top: 1px solid var(--border-light); padding-top: 7px; }
.format-badge {
  display: inline-block;
  border: 1px solid var(--border);
  border-radius: 2px;
  background: var(--panel-alt);
  padding: 1px 5px;
  color: var(--text);
  font-size: 10px;
  font-weight: 700;
  text-transform: uppercase;
}
.action-links { display: flex; gap: 10px; white-space: nowrap; }
.table-wrap { overflow-x: auto; margin: 9px; border: 1px solid var(--border); background: var(--input); }
table { width: 100%; border-collapse: collapse; }
th, td { padding: 7px 8px; border-bottom: 1px solid var(--border-light); text-align: left; vertical-align: middle; }
th { background: var(--panel-alt); color: var(--muted); font-size: 11px; }
tbody tr:last-child td { border-bottom: 0; }
tbody tr:nth-child(even) { background: color-mix(in srgb, var(--panel-alt) 45%, transparent); }
td.book-name { min-width: 170px; font-weight: 600; }
td.filename { max-width: 240px; overflow-wrap: anywhere; color: var(--muted); font-size: 12px; }
.empty-state { margin: 9px; border: 1px solid var(--border); background: var(--input); color: var(--muted); padding: 24px 10px; text-align: center; }

.auth-body { display: flex; min-height: 100vh; flex-direction: column; }
.auth-shell { display: grid; flex: 1; place-items: start center; padding: 12vh 12px 24px; }
.auth-window { width: min(390px, 100%); }
.auth-form { display: grid; gap: 10px; padding: 14px; }
.auth-form > p { margin-bottom: 2px; }
.auth-form label { display: grid; gap: 3px; font-weight: 600; }
.auth-form .field-note { margin: -4px 0 0; }
.form-error { border: 1px solid var(--danger); background: var(--input); padding: 7px; }
.dialog-actions { display: flex; justify-content: flex-end; border-top: 1px solid var(--border-light); padding-top: 10px; }

.reader-body { height: 100vh; overflow: hidden; }
.reader-header { height: 42px; }
.reader-header a { color: var(--title-text); }
.reader-title { min-width: 0; padding: 0 10px; text-align: center; }
.reader-title strong, .reader-title span { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reader-title span { font-size: 11px; opacity: 0.85; }
.reader-layout { display: grid; grid-template-columns: 250px minmax(0, 1fr); height: calc(100vh - 42px); }
.toc-panel { z-index: 3; overflow-y: auto; border-right: 1px solid var(--border); background: var(--panel); padding: 8px; }
.toc-heading { display: flex; align-items: center; justify-content: space-between; border-bottom: 1px solid var(--border); padding-bottom: 6px; }
.toc-heading h1 { margin: 0; }
.icon-button { min-width: 28px; min-height: 26px; padding: 1px 7px; font-size: 17px; }
#toc { margin-top: 7px; }
#toc ul { margin: 0; padding-left: 14px; list-style: none; }
#toc > ul { padding-left: 0; }
#toc li { margin: 1px 0; }
#toc button { width: 100%; min-height: 26px; border: 0; background: transparent; color: var(--text); padding: 3px 5px; text-align: left; }
#toc button:hover { background: var(--panel-alt); }
.toc-empty { color: var(--muted); font-size: 12px; }
.reader-stage { display: flex; min-width: 0; min-height: 0; flex-direction: column; }
.reader-toolbar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  min-height: 44px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
  padding: 5px 8px;
}
.reader-toolbar > :last-child { justify-self: end; }
.reader-actions { display: flex; align-items: center; gap: 6px; }
.page-controls { display: flex; align-items: center; gap: 6px; }
.page-entry { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.page-entry input { width: 58px; }
.reader-status { min-height: 0; border-bottom: 1px solid var(--border-light); background: var(--panel); color: var(--muted); padding: 4px 8px; font-size: 12px; text-align: center; }
.reader-status:empty { display: none; }
.pdf-container { flex: 1; overflow: auto; overscroll-behavior: contain; background: var(--desktop); padding: 16px; text-align: center; }
#pdf-canvas { display: block; max-width: none; margin: 0 auto; background: #fff; box-shadow: var(--shadow); }
.epub-container { flex: 1; min-height: 0; background: var(--input); overscroll-behavior: contain; }
.epub-container iframe { border: 0; }
.reader-layout.toc-closed { grid-template-columns: 0 minmax(0, 1fr); }
.reader-layout.toc-closed .toc-panel { visibility: hidden; padding: 0; }

.simple-page { width: min(420px, calc(100% - 24px)); margin: 15vh auto; border: 1px solid var(--border); background: var(--panel); box-shadow: var(--shadow); padding: 16px; }
.simple-page h1 { font-size: 18px; }
.simple-page .button-link { display: inline-block; margin-top: 8px; }

@media (max-width: 800px) {
  .filters { grid-template-columns: 1fr 1fr; }
  .reader-layout { grid-template-columns: minmax(0, 1fr); position: relative; }
  .toc-panel { position: absolute; inset: 0 auto 0 0; width: min(85vw, 280px); box-shadow: var(--shadow); }
  .reader-layout.toc-closed { grid-template-columns: minmax(0, 1fr); }
  .reader-layout.toc-closed .toc-panel { width: 0; }
  .reader-toolbar { grid-template-columns: auto 1fr auto; gap: 5px; }
}

@media (max-width: 580px) {
  .title-bar { padding-inline: 7px; }
  .title-bar .compact-field { font-size: 0; gap: 0; }
  .title-bar .compact-field select { font-size: 12px; }
  .header-controls { gap: 4px; }
  .upload-toolbar { grid-template-columns: 1fr auto; }
  .upload-toolbar > label, .upload-toolbar .field-note { display: none; }
  .filters { grid-template-columns: 1fr; }
  .library-title-row { align-items: start; }
  .reader-title { text-align: left; }
  .reader-title span { display: none; }
  .reader-header .header-controls .compact-field { display: none; }
  .reader-toolbar { grid-template-columns: auto 1fr; }
  .reader-toolbar > :last-child { display: none !important; }
  .page-controls { justify-self: end; gap: 3px; }
  .page-controls button { padding-inline: 6px; font-size: 12px; }
  .page-entry label { display: none; }
  .page-entry input { width: 48px; }
  .pdf-container { padding: 8px; }
}
