/* Marginalia — styles */

:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface2: #efece4;
  --text: #201d17;
  --text2: #6b6557;
  --border: #e3ded2;
  --accent: #7048b6;
  --accent-strong: #5c37a0;
  --accent-contrast: #ffffff;
  --danger: #b3362c;
  --yellow: #fdf6c9;
  --yellow-edge: #d9b909;
  --green: #ddf3d2;
  --green-edge: #4c9a3f;
  --pink: #fde3ee;
  --pink-edge: #d55190;
  --shadow: 0 1px 3px rgba(32, 26, 12, 0.1);
  --shadow-lg: 0 12px 40px rgba(32, 26, 12, 0.22);
  --radius: 10px;
  color-scheme: light;
}

.dark {
  --bg: #14121a;
  --surface: #1e1b26;
  --surface2: #262230;
  --text: #ece9f1;
  --text2: #a49db3;
  --border: #37323f;
  --accent: #a685e0;
  --accent-strong: #bfa3ec;
  --accent-contrast: #1a1024;
  --danger: #e5766c;
  --yellow: #453d10;
  --yellow-edge: #c7ab27;
  --green: #1f3d1a;
  --green-edge: #63b854;
  --pink: #46202f;
  --pink-edge: #d9699e;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.5);
  --shadow-lg: 0 12px 40px rgba(0, 0, 0, 0.6);
  color-scheme: dark;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

.hidden { display: none !important; }
[hidden] { display: none !important; }

html { scroll-behavior: smooth; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  line-height: 1.5;
  display: flex;
  flex-direction: column;
}

a { color: var(--accent); }
a:hover { color: var(--accent-strong); }

button { font: inherit; color: inherit; }

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  margin: -1px; padding: 0; border: 0;
  clip: rect(0 0 0 0); clip-path: inset(50%);
  overflow: hidden; white-space: nowrap;
}

.skip-link {
  position: absolute;
  left: 12px; top: -60px;
  background: var(--accent);
  color: var(--accent-contrast);
  padding: 10px 16px;
  border-radius: 8px;
  z-index: 500;
  transition: top 0.15s;
}
.skip-link:focus { top: 12px; color: var(--accent-contrast); }

/* ---------- Header ---------- */

.header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  position: sticky;
  top: 0;
  z-index: 100;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  text-decoration: none;
  color: var(--text);
}
.brand:hover { color: var(--text); }
.brand svg { width: 30px; height: 30px; flex-shrink: 0; }
.brand h1 { font-size: 1.1rem; white-space: nowrap; letter-spacing: -0.01em; }

.search-wrap { flex: 1; min-width: 160px; max-width: 440px; position: relative; }
.search-box {
  width: 100%;
  padding: 9px 34px 9px 36px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.92rem;
  background: var(--bg);
  color: var(--text);
}
.search-box::placeholder { color: var(--text2); }
.search-wrap .search-icon {
  position: absolute; left: 12px; top: 50%;
  transform: translateY(-50%);
  width: 16px; height: 16px;
  color: var(--text2);
  pointer-events: none;
}
.search-clear {
  position: absolute; right: 6px; top: 50%;
  transform: translateY(-50%);
  border: none; background: none;
  color: var(--text2);
  font-size: 1.1rem;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
  border-radius: 50%;
}
.search-clear:hover { color: var(--text); }

.header-actions { display: flex; gap: 8px; align-items: center; margin-left: auto; }

.btn {
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.88rem;
  font-weight: 500;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { background: var(--surface2); text-decoration: none; }
a.btn, a.btn:hover { color: var(--text); }
.btn-primary, .btn-primary:hover { color: var(--accent-contrast); }
.btn svg { width: 15px; height: 15px; }

.btn-primary {
  background: var(--accent);
  color: var(--accent-contrast);
  border-color: var(--accent);
}
.btn-primary:hover { background: var(--accent-strong); border-color: var(--accent-strong); }

.btn-danger { color: var(--danger); border-color: var(--danger); background: transparent; }
.btn-danger:hover { background: rgba(179, 54, 44, 0.08); }

.btn-icon { padding: 8px 10px; }
.btn-icon svg { width: 17px; height: 17px; }

/* ---------- Toolbar ---------- */

.toolbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 8px 20px;
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
}
.toolbar select {
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font-size: 0.85rem;
  cursor: pointer;
  max-width: 200px;
}
.result-count {
  font-size: 0.82rem;
  color: var(--text2);
  margin-left: auto;
  white-space: nowrap;
}

.stats-bar {
  padding: 10px 20px 0;
  display: flex;
  gap: 20px;
  flex-wrap: wrap;
  font-size: 0.85rem;
  color: var(--text2);
  max-width: 1400px;
  margin: 0 auto;
  width: 100%;
}
.stat-item .stat-value { font-weight: 700; color: var(--text); }

/* ---------- Demo banner ---------- */

.demo-banner {
  background: linear-gradient(100deg, var(--accent) 0%, var(--accent-strong) 100%);
  color: #fff;
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 0.9rem;
  text-align: center;
}
.dark .demo-banner { color: var(--accent-contrast); }
.demo-banner strong { font-weight: 700; }
.demo-banner .btn {
  background: rgba(255, 255, 255, 0.92);
  color: #3d2470;
  border: none;
  padding: 6px 12px;
  font-size: 0.85rem;
}
.demo-banner .btn:hover { background: #fff; }

/* ---------- Grid ---------- */

main { flex: 1; width: 100%; }

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(175px, 1fr));
  gap: 18px;
  padding: 18px 20px 40px;
  max-width: 1400px;
  margin: 0 auto;
  list-style: none;
}

.card {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: transform 0.15s, box-shadow 0.15s;
  position: relative;
}
.card:hover, .card:focus-within {
  transform: translateY(-3px);
  box-shadow: 0 6px 18px rgba(32, 26, 12, 0.18);
}
.card-btn {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}

.card-cover { width: 100%; aspect-ratio: 2/3; object-fit: cover; display: block; background: var(--surface2); }

.cover-placeholder {
  width: 100%;
  aspect-ratio: 2/3;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  padding: 14px 12px;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}
.cover-placeholder .ph-title {
  font-family: Georgia, "Times New Roman", serif;
  font-size: 1rem;
  font-weight: 700;
  line-height: 1.25;
  overflow-wrap: break-word;
  display: -webkit-box;
  -webkit-line-clamp: 6;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.cover-placeholder .ph-author {
  font-size: 0.72rem;
  opacity: 0.9;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.card-info { padding: 10px 12px 12px; }
.card-title {
  font-size: 0.88rem;
  font-weight: 650;
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.card-author {
  font-size: 0.78rem;
  color: var(--text2);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.card-meta { display: flex; gap: 5px; margin-top: 8px; align-items: center; flex-wrap: wrap; }

.badge {
  font-size: 0.64rem;
  padding: 2px 7px;
  border-radius: 999px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
}
.badge-ebook { background: #e0e9fb; color: #1c4587; }
.badge-audiobook { background: #fbeccd; color: #7c4a03; }
.dark .badge-ebook { background: #223a5e; color: #a8c7f0; }
.dark .badge-audiobook { background: #4d3a10; color: #f0ce8a; }
.badge-hl { background: var(--yellow); color: #6e5d04; }
.dark .badge-hl { color: #e8d34e; }
.badge-bm { background: #e5e3f7; color: #3d3591; }
.dark .badge-bm { background: #2d2a52; color: #b5affa; }

.progress-strip { height: 4px; background: var(--surface2); }
.progress-strip .fill { height: 100%; background: var(--accent); }

/* ---------- Empty / no-results states ---------- */

.state-panel {
  max-width: 620px;
  margin: 40px auto;
  padding: 36px 28px;
  text-align: center;
  color: var(--text2);
}
.state-panel .state-icon { width: 56px; height: 56px; margin: 0 auto 16px; color: var(--accent); }
.state-panel h2 { color: var(--text); font-size: 1.3rem; margin-bottom: 8px; }
.state-panel p { margin-bottom: 14px; font-size: 0.95rem; }
.state-panel .btn { margin: 4px; }

/* ---------- Loading skeletons ---------- */

.skeleton-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(175px, 1fr));
  gap: 18px;
  padding: 18px 20px;
  max-width: 1400px;
  margin: 0 auto;
}
.skeleton-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
}
.skeleton-card .sk-cover { aspect-ratio: 2/3; }
.skeleton-card .sk-line { height: 12px; margin: 10px 12px; border-radius: 6px; }
.skeleton-card .sk-line.short { width: 55%; margin-top: 0; }
.sk-shimmer {
  background: linear-gradient(90deg, var(--surface2) 25%, var(--border) 50%, var(--surface2) 75%);
  background-size: 200% 100%;
  animation: shimmer 1.4s infinite;
}
@keyframes shimmer { to { background-position: -200% 0; } }

/* ---------- Dialogs ---------- */

dialog {
  border: none;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  padding: 0;
  width: min(680px, calc(100vw - 32px));
  max-height: calc(100dvh - 48px);
  margin: auto;
}
dialog::backdrop { background: rgba(15, 10, 25, 0.55); backdrop-filter: blur(2px); }

.dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 18px 22px 0;
}
.dialog-head h2 { font-size: 1.25rem; }
.dialog-close {
  border: none;
  background: var(--surface2);
  color: var(--text2);
  width: 34px; height: 34px;
  border-radius: 50%;
  font-size: 1.2rem;
  line-height: 1;
  cursor: pointer;
  flex-shrink: 0;
}
.dialog-close:hover { color: var(--text); background: var(--border); }
.dialog-body { padding: 14px 22px 22px; overflow-y: auto; }

/* ---------- Import dialog ---------- */

.drop-zone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 34px 20px;
  text-align: center;
  color: var(--text2);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  background: var(--bg);
}
.drop-zone:hover, .drop-zone.dragover {
  border-color: var(--accent);
  background: color-mix(in srgb, var(--accent) 7%, var(--bg));
}
.drop-zone svg { width: 40px; height: 40px; color: var(--accent); margin-bottom: 10px; }
.drop-zone strong { color: var(--text); display: block; font-size: 1rem; margin-bottom: 4px; }
.drop-zone .hint { font-size: 0.83rem; }

.import-privacy {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-size: 0.83rem;
  color: var(--text2);
  background: var(--surface2);
  border-radius: 10px;
  padding: 12px 14px;
  margin-top: 14px;
}
.import-privacy svg { width: 18px; height: 18px; flex-shrink: 0; color: var(--accent); margin-top: 1px; }

.howto { margin-top: 14px; border: 1px solid var(--border); border-radius: 10px; }
.howto summary {
  cursor: pointer;
  padding: 12px 14px;
  font-weight: 600;
  font-size: 0.9rem;
  list-style: none;
  display: flex;
  align-items: center;
  gap: 8px;
}
.howto summary::-webkit-details-marker { display: none; }
.howto summary::before { content: "▸"; color: var(--accent); transition: transform 0.15s; }
.howto[open] summary::before { transform: rotate(90deg); }
.howto-body { padding: 0 16px 14px; font-size: 0.88rem; color: var(--text2); }
.howto-body ol { padding-left: 20px; margin: 6px 0 10px; }
.howto-body li { margin-bottom: 6px; }
.howto-body li strong { color: var(--text); }

.import-actions {
  display: flex;
  gap: 8px;
  margin-top: 16px;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: center;
}

.import-error {
  margin-top: 12px;
  background: color-mix(in srgb, var(--danger) 10%, var(--surface));
  border: 1px solid color-mix(in srgb, var(--danger) 40%, var(--border));
  color: var(--danger);
  border-radius: 10px;
  padding: 10px 14px;
  font-size: 0.86rem;
}

/* ---------- Detail dialog ---------- */

.detail-dialog { width: min(820px, calc(100vw - 32px)); }

.detail-header { display: flex; gap: 20px; padding: 22px 22px 18px; border-bottom: 1px solid var(--border); }
.detail-cover { width: 128px; flex-shrink: 0; border-radius: 8px; overflow: hidden; box-shadow: var(--shadow); align-self: flex-start; }
.detail-cover img { width: 100%; display: block; }
.detail-cover .cover-placeholder { aspect-ratio: 2/3; }
.detail-meta { flex: 1; min-width: 0; }
.detail-meta h2 { font-size: 1.35rem; line-height: 1.25; margin-bottom: 4px; padding-right: 36px; }
.detail-meta .author { color: var(--text2); margin-bottom: 10px; }
.detail-meta .meta-row { font-size: 0.85rem; color: var(--text2); margin-bottom: 5px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.progress-bar { height: 7px; background: var(--surface2); border-radius: 4px; overflow: hidden; margin: 6px 0 10px; max-width: 320px; }
.progress-bar .fill { height: 100%; background: var(--accent); border-radius: 4px; }

.detail-tools { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.detail-tools .btn { font-size: 0.82rem; padding: 6px 11px; }

.detail-body { padding: 16px 22px 26px; }

.section-title {
  font-size: 0.82rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text2);
  margin: 20px 0 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
}
.section-title:first-child { margin-top: 4px; }
.expand-toggle {
  font-size: 0.78rem;
  font-weight: 500;
  text-transform: none;
  letter-spacing: 0;
  color: var(--accent);
  cursor: pointer;
  background: none;
  border: none;
  padding: 2px 6px;
}
.expand-toggle:hover { text-decoration: underline; }

.highlight {
  padding: 10px 14px;
  margin-bottom: 8px;
  border-radius: 8px;
  border-left: 4px solid var(--yellow-edge);
  background: var(--yellow);
  font-size: 0.92rem;
  line-height: 1.55;
}
.highlight[data-color="#DFC"] { background: var(--green); border-color: var(--green-edge); }
.highlight[data-color="#FFE0EC"] { background: var(--pink); border-color: var(--pink-edge); }
.hl-toggle {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  cursor: pointer;
  padding: 0;
}
.hl-header { display: flex; justify-content: space-between; gap: 10px; font-size: 0.76rem; color: var(--text2); font-weight: 600; flex-wrap: wrap; }
.hl-header .chev::before { content: "▸ "; }
.highlight.expanded .hl-header .chev::before { content: "▾ "; }
.hl-preview { font-size: 0.87rem; color: var(--text2); margin-top: 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.highlight.expanded .hl-preview { display: none; }
.hl-body { display: none; margin-top: 8px; }
.highlight.expanded .hl-body { display: block; }
.hl-quote { white-space: pre-wrap; }
.hl-note { font-style: italic; margin-top: 8px; color: var(--text2); font-size: 0.87rem; border-top: 1px dashed color-mix(in srgb, var(--text2) 35%, transparent); padding-top: 8px; }
.hl-note::before { content: "My note: "; font-weight: 700; font-style: normal; }

.bookmark {
  padding: 9px 14px;
  margin-bottom: 6px;
  border-radius: 8px;
  background: var(--surface2);
  font-size: 0.88rem;
  color: var(--text2);
}
.bm-header { display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap; }
.bm-header .bm-chapter { color: var(--text); font-weight: 550; }
.bm-note { margin-top: 6px; color: var(--text); font-style: italic; white-space: pre-wrap; }

.circ-item {
  padding: 9px 14px;
  margin-bottom: 6px;
  border-radius: 8px;
  background: var(--surface2);
  font-size: 0.87rem;
  display: flex;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
  color: var(--text2);
}
.circ-item .circ-activity { font-weight: 650; color: var(--text); }

.detail-empty { color: var(--text2); font-size: 0.9rem; font-style: italic; padding: 4px 0 8px; }

/* ---------- Drag overlay ---------- */

.drag-overlay {
  position: fixed;
  inset: 0;
  z-index: 400;
  background: color-mix(in srgb, var(--accent) 18%, rgba(20, 12, 36, 0.7));
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}
.drag-overlay .drag-card {
  background: var(--surface);
  border: 2px dashed var(--accent);
  border-radius: 16px;
  padding: 40px 56px;
  text-align: center;
  font-size: 1.15rem;
  font-weight: 650;
  color: var(--text);
  box-shadow: var(--shadow-lg);
}
.drag-overlay svg { width: 44px; height: 44px; color: var(--accent); display: block; margin: 0 auto 12px; }

/* ---------- Toast ---------- */

.toast-region { position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 600; display: flex; flex-direction: column; gap: 8px; align-items: center; width: max-content; max-width: calc(100vw - 32px); }
.toast {
  background: var(--text);
  color: var(--bg);
  padding: 10px 18px;
  border-radius: 999px;
  font-size: 0.88rem;
  box-shadow: var(--shadow-lg);
  animation: toast-in 0.2s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translateY(8px); } }

/* ---------- Footer ---------- */

.footer {
  border-top: 1px solid var(--border);
  padding: 22px 20px 30px;
  font-size: 0.82rem;
  color: var(--text2);
  text-align: center;
}
.footer p { max-width: 640px; margin: 0 auto 6px; }
.footer nav { display: flex; gap: 16px; justify-content: center; margin-top: 8px; flex-wrap: wrap; }

/* ---------- Mobile ---------- */

@media (max-width: 720px) {
  .header { padding: 10px 12px; gap: 8px; }
  .brand h1 { font-size: 1rem; }
  .brand svg { width: 26px; height: 26px; }
  .search-wrap { order: 3; flex-basis: 100%; max-width: none; }
  .header-actions .btn-label { display: none; }
  .header-actions .btn { padding: 8px 10px; }
  .toolbar { padding: 8px 12px; overflow-x: auto; flex-wrap: nowrap; }
  .toolbar select { flex-shrink: 0; }
  .result-count { display: none; }
  .grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; padding: 12px 12px 32px; }
  .skeleton-grid { grid-template-columns: repeat(auto-fill, minmax(130px, 1fr)); gap: 12px; padding: 12px; }
  .stats-bar { padding: 10px 12px 0; gap: 12px; font-size: 0.8rem; }
  .detail-header { flex-direction: column; align-items: center; text-align: center; padding: 18px 16px 14px; }
  .detail-meta h2 { padding-right: 0; }
  .detail-meta .meta-row, .detail-tools { justify-content: center; }
  .progress-bar { margin-left: auto; margin-right: auto; }
  .detail-cover { width: 110px; }
  .detail-body { padding: 12px 16px 20px; }
  .dialog-body { padding: 12px 16px 18px; }
}
