:root { color-scheme: light; }

.theme-toggle {
  position: fixed;
  top: calc(10px + env(safe-area-inset-top));
  right: calc(10px + env(safe-area-inset-right));
  z-index: 20;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  min-width: 36px;
  min-height: 36px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: var(--panel);
  color: var(--ink);
  box-shadow: 0 2px 8px #0002;
}
.theme-toggle:hover { border-color: var(--brand-strong); }
.theme-toggle:focus-visible { outline: 3px solid var(--gold); outline-offset: 3px; }
.theme-toggle img { grid-area: 1 / 1; }
.theme-sun { display: none; }
.topbar { padding-right: calc(62px + env(safe-area-inset-right)); }
.login-page { padding-top: 62px; }

:root[data-theme="dark"] {
  color-scheme: dark;
  --ink: #ededed;
  --muted: #a1a1a1;
  --line: #292929;
  --panel: #0a0a0a;
  --wash: #000000;
  --surface-raised: #141414;
  --control-line: #333333;
  --brand-strong: #ffb5ab;
  --gold: #edc968;
  --gold-soft: #19160e;
  --warn: #f3d18b;
  --bad: #ff9e97;
}
[data-theme="dark"] body { background: var(--wash); }
[data-theme="dark"] .theme-moon { display: none; }
[data-theme="dark"] .theme-sun { display: block; filter: invert(1); }
[data-theme="dark"] .topbar { background: var(--wash); border-color: var(--line); }
[data-theme="dark"] :is(input, select, textarea) {
  background: var(--wash); color: var(--ink); border-color: var(--control-line);
}
[data-theme="dark"] :is(input, textarea)::placeholder { color: #888888; opacity: 1; }
[data-theme="dark"] :is(button.secondary, .toolbar .sub-category-button) {
  background: var(--surface-raised); color: var(--ink); border-color: var(--control-line);
}
[data-theme="dark"] button.danger { background: #a92c28; color: #fff; }
[data-theme="dark"] a:not([class]) { color: #91c9f7; }
[data-theme="dark"] :is(.nav a.active, .nav a:hover, .nav a.upload-live-link.done) {
  background: var(--surface-raised); color: var(--brand-strong);
}
[data-theme="dark"] :is(.stat-card, .continue-card, .table, .notice,
  .category-tree-row a, .study-program-tabs a, .study-semester-bar a,
  .study-subject-folder, .study-admin-summary article, .study-access-card,
  .library-results, .library-search-panel) {
  background: var(--panel); border-color: var(--line);
}
[data-theme="dark"] :is(.admin-tabs, .storage-panel, .searchbar,
  .subcategory-ribbon, .continue-reading, .category-tree-group, .upload-log,
  .upload-progress, .reader-bar, .epub-reader, .epub-stage, .epub-stage iframe,
  .study-semester-bar, .study-subject-folder > header, .study-password-result,
  .reader-errors-table th, .study-enrollment-line, .reader iframe) {
  background: var(--panel); border-color: var(--line);
}
[data-theme="dark"] :is(.stat-total, .folder-drop, .upload-skipped-details,
  .temp-password, .badge, .badge.published, .badge.pending, .subcategory-ribbon a,
  .category-ribbon a, .category-tree-row a:hover, .book-cover-thumb.cover-fallback) {
  background: var(--surface-raised); border-color: var(--control-line);
}
[data-theme="dark"] .book-cover-thumb small { color: var(--ink); }
[data-theme="dark"] .library-hero {
  background: var(--panel); border-color: var(--line); box-shadow: none;
}
[data-theme="dark"] .library-search-panel { box-shadow: none; }
[data-theme="dark"] :is(.category-ribbon a.active, .category-ribbon a:hover,
  .subcategory-ribbon a:hover, .study-semester-bar a.active, .study-semester-bar a:hover,
  .pdf-controls button.active) { background: var(--brand); color: #fff; }
[data-theme="dark"] .storage-meter { background: var(--line); }
[data-theme="dark"] .login-card { background: var(--panel); border-color: var(--line); }
[data-theme="dark"] .login-card :is(label, .subtle) { color: var(--muted); }
[data-theme="dark"] .login-card input { background: var(--wash); color: var(--ink); }
[data-theme="dark"] .login-card button.secondary { background: var(--surface-raised); }
[data-theme="dark"] .login-page::after { background: #000c; }
[data-theme="dark"] :is(.study-page-heading .library-kicker, .study-subject-code,
  .study-subject-folder > header > strong, .reader-errors-table .alert-file-check-available) { color: #8edbb9; }
[data-theme="dark"] :is(.study-program-tabs a.active, .study-program-tabs a:hover,
  .study-material-heading span, .alert-status-resolved, .alert-chip-resolved) {
  background: #071a12; color: #a0dfbc;
}
[data-theme="dark"] :is(.alert-status-new, .alert-status-acknowledged,
  .reader-errors-table .alert-suggestion) { background: #17130a; }
[data-theme="dark"] :is(.alert-chip-new, .alert-chip-acknowledged) { background: #241c0c; color: #f3d18b; }
[data-theme="dark"] .reader-errors-table .suggestion-kicker { color: var(--warn); }
[data-theme="dark"] .reader-errors-table td strong { color: var(--ink); }
[data-theme="dark"] .reader-errors-table .subtle { color: var(--muted); }
[data-theme="dark"] :is(.alert-file-check-invalid, .alert-file-check-missing, .alert-file-check-unreadable) { color: var(--bad); }
[data-theme="dark"] :is(.pdf-viewer, .epub-stage, .epub-stage iframe) { background: var(--wash); }
/* Presentation only: original PDF bytes and downloaded files are unchanged. */
[data-theme="dark"] :is(.pdf-stage canvas, .pdf-scroll-page canvas) {
  filter: invert(1) hue-rotate(180deg) brightness(0.94);
  box-shadow: none;
}

@media (max-width: 760px) {
  .theme-toggle { width: 40px; height: 40px; min-width: 40px; min-height: 40px; }
  .topbar { padding-right: calc(62px + env(safe-area-inset-right)); }
}
@media print {
  .theme-toggle { display: none; }
  [data-theme="dark"] :is(.pdf-stage canvas, .pdf-scroll-page canvas) { filter: none; }
}
