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

:root {
  --bg: #fafafa;
  --surface: #ffffff;
  --text: #1a1a1a;
  --text-muted: #666;
  --accent: #2563eb;
  --danger: #dc2626;
  --border: #e5e5e5;
  --radius: 8px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
}

.hidden { display: none !important; }

/* --- Library View --- */

#library header {
  position: sticky;
  top: 0;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  z-index: 10;
}

.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#library h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

#bookmarks-toggle {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  padding: 4px 8px;
  color: var(--text);
}

/* Login screen */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100dvh;
  background: var(--bg);
}

#login-form {
  background: var(--surface);
  padding: 32px 24px;
  border-radius: 12px;
  border: 1px solid var(--border);
  width: 90%;
  max-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  text-align: center;
}

#login-form h1 {
  font-size: 1.25rem;
  font-weight: 600;
}

#login-password {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.95rem;
  text-align: center;
}

#login-form button[type="submit"] {
  padding: 10px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

#login-form button[type="submit"]:active {
  opacity: 0.8;
}

#login-error {
  color: var(--danger);
  font-size: 0.85rem;
}

/* Upload section */
#upload-section {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--surface);
}

.upload-label {
  display: inline-block;
  padding: 10px 20px;
  background: var(--accent);
  color: white;
  border-radius: var(--radius);
  cursor: pointer;
  font-size: 0.9rem;
  font-weight: 500;
}

.upload-label:active {
  opacity: 0.8;
}

#upload-preview {
  margin-top: 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#upload-preview input[type="text"] {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.9rem;
}

#upload-preview button {
  padding: 10px;
  border: none;
  border-radius: var(--radius);
  font-size: 0.9rem;
  font-weight: 500;
  cursor: pointer;
}

#upload-preview button[type="submit"] {
  background: var(--accent);
  color: white;
}

#upload-preview button[type="button"] {
  background: var(--border);
  color: var(--text);
}

/* Book list */
#book-list {
  padding: 8px;
}

.book-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  margin: 4px 0;
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
}

.book-card:active {
  background: #f0f0f0;
}

.book-info {
  flex: 1;
  min-width: 0;
}

.book-title {
  font-weight: 500;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.book-author {
  font-size: 0.8rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.book-edit {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 1rem;
  padding: 8px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: 8px;
}

.book-edit:active {
  color: var(--accent);
}

.book-title-input {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  font-size: 0.95rem;
  font-weight: 500;
  font-family: inherit;
  outline: none;
}

.book-delete {
  background: none;
  border: none;
  color: var(--danger);
  font-size: 1.1rem;
  padding: 8px;
  cursor: pointer;
  flex-shrink: 0;
  margin-left: 8px;
}

.book-bookmark-count {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-left: 6px;
  cursor: pointer;
}

#empty-message {
  text-align: center;
  color: var(--text-muted);
  padding: 48px 16px;
  font-size: 0.95rem;
}

/* --- Bookmarks View (global) --- */

.bm-group {
  margin-bottom: 12px;
}

.bm-group-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text);
  user-select: none;
}

.bm-group-header .bm-chevron {
  font-size: 0.7rem;
  transition: transform 0.15s;
}

.bm-group-header.collapsed .bm-chevron {
  transform: rotate(-90deg);
}

.bm-group-items {
  padding-left: 8px;
}

.bm-card {
  padding: 10px 12px;
  margin: 4px 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: background 0.15s;
}

.bm-card:active {
  background: #f0f0f0;
}

.bm-card-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.bm-name {
  font-weight: 500;
  font-size: 0.9rem;
}

.bm-location {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.bm-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.bm-actions button {
  background: none;
  border: none;
  padding: 4px 6px;
  cursor: pointer;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.bm-actions .bm-delete-btn {
  color: var(--danger);
}

.bm-notes-section {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
}

.bm-note {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-start;
  justify-content: space-between;
  padding: 4px 0;
  font-size: 0.8rem;
  color: var(--text);
}

.bm-note-content {
  flex: 1;
  min-width: 0;
  word-break: break-word;
}

.bm-note-actions {
  display: flex;
  gap: 2px;
  flex-shrink: 0;
}

.bm-note-actions button {
  background: none;
  border: none;
  padding: 2px 4px;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.bm-note-actions .bm-note-delete {
  color: var(--danger);
}

.bm-note-label {
  display: block;
  font-size: 0.7rem;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 2px;
}
.bm-note--stacked {
  display: block;
}
.bm-note-quote-ref {
  font-size: 0.75rem;
  color: var(--text-muted);
  font-style: italic;
  margin-top: 4px;
  padding-left: 8px;
  border-left: 2px solid var(--border);
}

.bm-add-note-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-top: 6px;
}

.bm-add-note-row input {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.8rem;
  font-family: inherit;
}

.bm-add-note-row button {
  align-self: flex-start;
  padding: 4px 10px;
  border: none;
  border-radius: 4px;
  background: var(--accent);
  color: white;
  font-size: 0.8rem;
  cursor: pointer;
}

.bm-name-input {
  width: 100%;
  padding: 4px 6px;
  border: 1px solid var(--accent);
  border-radius: 4px;
  font-size: 0.9rem;
  font-weight: 500;
  font-family: inherit;
  outline: none;
}

.bm-empty {
  text-align: center;
  color: var(--text-muted);
  padding: 32px 16px;
  font-size: 0.9rem;
}

/* --- Reader Bookmark Panel --- */

#bookmark-panel-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 90;
}

#bookmark-panel {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(320px, 85vw);
  background: var(--surface);
  z-index: 100;
  display: flex;
  flex-direction: column;
  box-shadow: -2px 0 8px rgba(0,0,0,0.15);
  transform: translateX(0);
  transition: transform 0.2s;
}

.bm-panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.95rem;
  flex-shrink: 0;
}

.bm-panel-header button {
  background: none;
  border: none;
  font-size: 1.2rem;
  cursor: pointer;
  padding: 4px;
  color: var(--text-muted);
}

#bm-panel-back {
  color: var(--accent);
  font-size: 1.1rem;
  margin-right: 4px;
}

#bm-panel-title {
  flex: 1;
}

#bm-panel-add {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#bm-panel-add > input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  font-size: 0.85rem;
  font-family: inherit;
}

#bm-panel-add > button {
  padding: 8px 14px;
  border: none;
  border-radius: var(--radius);
  background: var(--accent);
  color: white;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
}

#bm-add-tags {
  width: 100%;
}
#bm-add-tags:empty {
  display: none;
}

#bm-panel-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}

#reader-bookmarks-btn {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  padding: 4px;
  margin-left: auto;
  flex-shrink: 0;
}

/* --- Reader View --- */

#reader {
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

#reader-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}

#reader-back {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 0.95rem;
  cursor: pointer;
  padding: 4px;
  white-space: nowrap;
}

#reader-title {
  font-size: 0.9rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  flex: 1;
}

/* Highlight colour palette (3x2 grid; first row's first two cells merged) */
.palette-grid {
  display: grid;
  grid-template-columns: repeat(3, 16px);
  grid-template-rows: repeat(2, 16px);
  gap: 2px;
  flex-shrink: 0;
}

.palette-grid .palette-cell {
  width: 16px;
  height: 16px;
  border: 1px solid rgba(0, 0, 0, 0.2);
  border-radius: 2px;
  cursor: pointer;
  padding: 0;
  background-clip: padding-box;
}

.palette-grid .palette-cell.palette-default {
  grid-column: 1 / span 2;
  width: auto;
  border: 2px solid #333;
  border-radius: 3px;
}

.palette-popup {
  position: fixed;
  z-index: 1000;
  background: var(--surface, #fff);
  border: 1px solid var(--border, #ccc);
  border-radius: 6px;
  padding: 6px;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
  grid-template-columns: repeat(3, 24px);
  grid-template-rows: repeat(2, 24px);
  gap: 4px;
}

.palette-popup .palette-cell {
  width: 24px;
  height: 24px;
}

.palette-popup .palette-cell.palette-default {
  border: 2px solid #333;
}

#viewer {
  flex: 1;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #f0f0f0;
  max-width: 800px;
  margin: 0 auto;
  width: 100%;
}

.pdf-canvas {
  display: block;
  max-width: 100%;
  max-height: 100%;
}

#reader-nav {
  display: flex;
  align-items: center;
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--surface);
}

#reader-nav button {
  flex: 1;
  padding: 14px;
  background: none;
  border: none;
  font-size: 1.5rem;
  cursor: pointer;
  color: var(--text);
}

#page-loading {
  font-size: 0.8rem;
  color: var(--text-muted);
}

#page-loading::after {
  content: '';
  animation: dots 1.5s steps(3, end) infinite;
}

@keyframes dots {
  0%   { content: ''; }
  33%  { content: '.'; }
  66%  { content: '..'; }
  100% { content: '...'; }
}

#page-indicator {
  font-size: 0.8rem;
  color: var(--text-muted);
  white-space: nowrap;
}

#page-input {
  width: 4ch;
  text-align: center;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 4px;
  font-size: 0.8rem;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
}

.book-type {
  display: inline-block;
  font-size: 0.65rem;
  font-weight: 600;
  background: var(--border);
  color: var(--text-muted);
  padding: 1px 5px;
  border-radius: 3px;
  vertical-align: middle;
  margin-left: 4px;
}

#reader-nav button:active {
  background: #f0f0f0;
}

/* --- Multi-level Menu --- */

.menu-heading {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin: 4px 0;
  background: var(--surface);
  cursor: pointer;
  font-weight: 500;
  font-size: 0.95rem;
  transition: background 0.15s;
}

.menu-heading:active {
  background: #f0f0f0;
}

/* Bookmark sort bar */

.bm-sort-bar {
  display: flex;
  justify-content: flex-end;
  padding: 4px 12px;
}

.bm-sort-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 2px 8px;
  font-size: 0.75rem;
  color: var(--text-muted);
  cursor: pointer;
  font-family: inherit;
}

/* Pages / Chapters sections */

.section-container {
  border-bottom: 1px solid var(--border);
}

.section-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px;
  font-weight: 500;
  font-size: 0.9rem;
}

.section-add-btn {
  background: none;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 1rem;
  line-height: 1;
  padding: 0 6px;
  cursor: pointer;
  color: var(--text-muted);
  flex-shrink: 0;
}

.section-label {
  cursor: pointer;
  color: var(--accent);
}

.section-label:active {
  opacity: 0.7;
}

/* Page notes */

.page-note {
  padding: 4px 12px 4px 24px;
}

.page-note-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.85rem;
}

.page-note-name {
  flex: 1;
  min-width: 0;
  word-break: break-word;
  font-weight: 500;
}

.page-note-right {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.page-note-right button {
  background: none;
  border: none;
  padding: 2px 4px;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text-muted);
}

.page-note-delete {
  color: var(--danger) !important;
}

.page-note-toggle {
  font-size: 0.6rem !important;
}

.page-note-content {
  padding: 4px 0 8px 0;
  font-size: 0.8rem;
  color: var(--text-muted);
  word-break: break-word;
}

/* Add note form under a section */

.section-add-note {
  padding: 6px 12px 8px 24px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.section-add-note input {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 0.8rem;
  font-family: inherit;
}

.section-add-note button {
  align-self: flex-start;
  padding: 4px 10px;
  border: none;
  border-radius: 4px;
  background: var(--accent);
  color: white;
  font-size: 0.8rem;
  cursor: pointer;
}

/* --- Quote Menu --- */

#quote-menu {
  position: fixed;
  z-index: 1000;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.15);
  padding: 4px;
  transform: translateX(-50%);
  display: flex;
  gap: 4px;
}

#quote-menu button {
  display: block;
  padding: 6px 16px;
  border: none;
  border-radius: 4px;
  background: var(--accent);
  color: white;
  font-size: 0.85rem;
  font-family: inherit;
  cursor: pointer;
  white-space: nowrap;
}

#quote-menu button:disabled {
  background: var(--border);
  opacity: 0.6;
  cursor: default;
}

.quote-text {
  font-size: 0.85rem;
  font-style: italic;
  color: var(--text-muted);
  word-break: break-word;
}
.quote-cite {
  font-size: 0.75rem;
  font-style: normal;
  color: var(--text-muted);
  margin-top: 6px;
  text-align: right;
}

/* --- Tags --- */
.tag-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 6px;
}
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  background: #e0edff;
  color: var(--accent);
  cursor: default;
  white-space: nowrap;
}
.tag-chip-remove {
  background: none;
  border: none;
  font-size: 0.65rem;
  color: var(--accent);
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
}
.tag-chip-add {
  display: inline-flex;
  align-items: center;
  background: none;
  border: 1px dashed var(--border);
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
}
.tag-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  padding: 6px 0;
  align-items: center;
}
.tag-picker-chip {
  display: inline-flex;
  align-items: center;
  font-size: 0.7rem;
  padding: 2px 8px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  white-space: nowrap;
}
.tag-picker-chip.selected {
  background: #e0edff;
  color: var(--accent);
  border-color: var(--accent);
}
.tag-picker-input {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 2px 8px;
  font-size: 0.7rem;
  width: 80px;
  outline: none;
}

.tag-header-actions {
  float: right;
  display: inline-flex;
  gap: 4px;
}
.tag-header-actions button {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 0.85rem;
  padding: 0 4px;
}
.tag-header-actions button:hover { color: var(--accent); }
.tag-header-actions .bm-delete-btn:hover { color: #c00; }

.bm-note-preview {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* --- Responsive --- */

@media (min-width: 600px) {
  #book-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(280px, 1fr));
    gap: 8px;
    padding: 16px;
  }

  .book-card {
    margin: 0;
  }
}
