@import url('https://fonts.googleapis.com/css2?family=Cinzel:wght@400;600;700&family=Crimson+Pro:ital,wght@0,300;0,400;0,600;1,300;1,400&display=swap');

:root {
  --bg-void: #080a0d;
  --bg-deep: #0d1117;
  --bg-surface: #111820;
  --bg-raised: #161e28;
  --bg-panel: #1a2332;
  --bg-hover: #1f2b3a;
  --bg-active: #243347;

  --border-subtle: #1e2d3d;
  --border-normal: #253547;
  --border-accent: #2e4a6a;

  --text-muted: #3d5470;
  --text-dim: #5a7a9a;
  --text-secondary: #8aaccc;
  --text-primary: #c4d8ee;
  --text-bright: #e8f2fc;

  --gold: #c9a94a;
  --gold-dim: #8a7030;
  --gold-bright: #e8c96a;
  --gold-glow: rgba(201, 169, 74, 0.15);

  --red: #c44a3a;
  --red-dim: #7a2820;
  --green: #4a9a6a;
  --blue: #4a7ab0;

  --sidebar-width: 300px;
  --toolbar-height: 48px;
  --transition: 0.18s cubic-bezier(0.4, 0, 0.2, 1);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  font-family: 'Crimson Pro', Georgia, serif;
  background: var(--bg-void);
  color: var(--text-primary);
  font-size: 16px;
}

/* ─── SCROLLBARS ─── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb { background: var(--border-accent); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--gold-dim); }

/* ─── LAYOUT ─── */
#app {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  grid-template-rows: var(--toolbar-height) 1fr;
  grid-template-areas: "sidebar toolbar" "sidebar viewer";
  height: 100%;
  transition: grid-template-columns var(--transition);
}

#app.sidebar-collapsed {
  grid-template-columns: 0px 1fr;
}

/* ─── SIDEBAR ─── */
#sidebar {
  grid-area: sidebar;
  background: var(--bg-deep);
  border-right: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  position: relative;
  z-index: 10;
  min-width: 160px;
  max-width: 600px;
}

#sidebar-resize-handle {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 5px;
  cursor: col-resize;
  z-index: 20;
}

#sidebar-resize-handle:hover,
#sidebar-resize-handle.dragging {
  background: var(--gold-dim);
  opacity: 0.5;
}

#sidebar::after {
  content: '';
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 1px;
  background: linear-gradient(180deg, transparent, var(--gold-dim) 30%, var(--gold-dim) 70%, transparent);
  opacity: 0.4;
  pointer-events: none;
}

.sidebar-header {
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--border-subtle);
  flex-shrink: 0;
}

.sidebar-title {
  font-family: 'Cinzel', serif;
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.15em;
  color: var(--gold);
  text-transform: uppercase;
  margin-bottom: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.sidebar-title svg { opacity: 0.8; }

#btn-refresh-library svg { transition: transform 0.2s; }
#btn-refresh-library:hover svg { opacity: 1; color: var(--gold); }
#btn-refresh-library.spinning svg { animation: spin 0.7s linear infinite; }

/* ─── LIBRARY SYNC INDICATOR ─── */
#library-sync-bar {
  height: 2px;
  background: var(--bg-surface);
  overflow: hidden;
  flex-shrink: 0;
  position: relative;
}

#library-sync-bar.active::after {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 45%;
  background: linear-gradient(90deg, transparent, var(--gold), transparent);
  animation: library-sync-slide 1.4s ease-in-out infinite;
}

@keyframes library-sync-slide {
  0%   { transform: translateX(-120%); }
  100% { transform: translateX(330%); }
}

#library-new-notice {
  display: flex;
  align-items: center;
  gap: 7px;
  padding: 6px 12px 6px 14px;
  background: var(--gold-glow);
  border-bottom: 1px solid var(--gold-dim);
  font-size: 12px;
  color: var(--gold-bright);
  flex-shrink: 0;
  font-family: 'Cinzel', serif;
  letter-spacing: 0.04em;
}

#library-new-notice-text { flex: 1; }

#library-new-notice-dismiss {
  background: none;
  border: none;
  color: var(--gold-dim);
  cursor: pointer;
  padding: 0 2px;
  font-size: 11px;
  line-height: 1;
  flex-shrink: 0;
}

#library-new-notice-dismiss:hover { color: var(--gold); }

.search-box {
  position: relative;
}

.search-box input {
  width: 100%;
  background: var(--bg-surface);
  border: 1px solid var(--border-normal);
  color: var(--text-primary);
  padding: 7px 10px 7px 32px;
  font-family: 'Crimson Pro', serif;
  font-size: 14px;
  border-radius: 4px;
  outline: none;
  transition: border-color var(--transition);
}

.search-box input:focus { border-color: var(--gold-dim); }
.search-box input::placeholder { color: var(--text-muted); }

.search-box svg {
  position: absolute;
  left: 9px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

/* ─── BOOK TREE ─── */
#book-tree {
  flex: 1;
  overflow-y: auto;
  padding: 8px 0;
}

.tree-folder {
  user-select: none;
}

.tree-folder-header {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 14px;
  cursor: pointer;
  color: var(--text-dim);
  font-size: 12px;
  font-family: 'Cinzel', serif;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  transition: color var(--transition);
}

.tree-folder-header:hover { color: var(--text-secondary); }
.tree-folder-header .chevron { transition: transform var(--transition); }
.tree-folder.open .chevron { transform: rotate(90deg); }

.tree-folder-children { display: none; padding-left: 14px; }
.tree-folder.open > .tree-folder-children { display: block; }

.tree-book {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 14px;
  cursor: pointer;
  border-radius: 3px;
  margin: 1px 6px;
  transition: background var(--transition), color var(--transition);
  position: relative;
  overflow: hidden;
}

.tree-book:hover { background: var(--bg-hover); color: var(--text-bright); }

.tree-book.active {
  background: var(--bg-active);
  color: var(--gold);
}

.tree-book.active::before {
  content: '';
  position: absolute;
  left: 0;
  top: 0;
  bottom: 0;
  width: 2px;
  background: var(--gold);
}

.tree-book-name {
  font-size: 14px;
  line-height: 1.3;
  flex: 1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-book-size {
  font-size: 11px;
  color: var(--text-muted);
  flex-shrink: 0;
}

/* ─── REFERENCE PANEL (TOC + Bookmarks) ─── */
#ref-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 300px;
  background: var(--bg-deep);
  border-left: 1px solid var(--border-subtle);
  z-index: 15;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition);
}

#ref-panel.open { transform: translateX(0); }

/* A collapsible section inside the ref panel */
.ref-section {
  display: none; /* hidden by default; JS sets display:flex when content is available */
  flex-direction: column;
  border-top: 1px solid var(--border-subtle);
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

/* toc-section is always managed by JS (show when outline exists).
   bookmarks-section is always shown once ref panel is set up. */
#bookmarks-section { display: flex; }

.ref-section.collapsed { flex: 0 0 auto; }

.ref-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  cursor: pointer;
  color: var(--gold);
  font-family: 'Cinzel', serif;
  font-size: 12px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  flex-shrink: 0;
  user-select: none;
  transition: color var(--transition);
}

.ref-section-header:hover { color: var(--gold-bright); }

.ref-chevron {
  opacity: 0.5;
  transition: transform var(--transition);
  flex-shrink: 0;
}

.ref-section.collapsed .ref-chevron { transform: rotate(-90deg); }

/* ─── TABLE OF CONTENTS ─── */
.toc-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 8px;
  min-height: 0;
}

.ref-section.collapsed .toc-list,
.ref-section.collapsed .bookmarks-list { display: none; }

.toc-item {
  padding: 4px 8px;
  cursor: pointer;
  font-size: 13px;
  border-radius: 3px;
  color: var(--text-primary);
  transition: background var(--transition), color var(--transition);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.toc-item:hover { background: var(--bg-hover); color: var(--gold); }
.toc-item.depth-1 { color: var(--text-secondary); font-size: 12px; }
.toc-item.depth-2 { color: var(--text-dim); font-size: 11px; }

/* ─── BOOKMARKS ─── */
.bookmarks-list {
  flex: 1;
  overflow-y: auto;
  padding: 0 6px 8px;
  min-height: 0;
}

.bookmark-item {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  border-radius: 3px;
  cursor: pointer;
  font-size: 13px;
  transition: background var(--transition);
  group: 1;
}

.bookmark-item:hover { background: var(--bg-hover); }

.bookmark-icon { color: var(--gold); flex-shrink: 0; }

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

.bookmark-label {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--text-primary);
  font-size: 13px;
}

.bookmark-meta {
  font-size: 11px;
  color: var(--text-muted);
}

.bookmark-delete {
  opacity: 0;
  background: none;
  border: none;
  color: var(--red);
  cursor: pointer;
  padding: 2px;
  display: flex;
  transition: opacity var(--transition);
}

.bookmark-item:hover .bookmark-delete { opacity: 1; }

.no-bookmarks {
  padding: 12px 14px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
}

/* ─── TOOLBAR ─── */
#toolbar {
  grid-area: toolbar;
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 0 12px;
  z-index: 5;
}

.toolbar-divider {
  width: 1px;
  height: 22px;
  background: var(--border-normal);
  margin: 0 4px;
  flex-shrink: 0;
}

.toolbar-spacer { flex: 1; }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 5px 8px;
  border-radius: 4px;
  font-family: 'Crimson Pro', serif;
  font-size: 14px;
  transition: background var(--transition), color var(--transition);
  white-space: nowrap;
  flex-shrink: 0;
}

.btn:hover { background: var(--bg-raised); color: var(--text-bright); }
.btn:active { background: var(--bg-active); }
.btn.active { color: var(--gold); }
.btn svg { flex-shrink: 0; }

.btn-gold {
  color: var(--gold);
  border: 1px solid var(--gold-dim);
}

.btn-gold:hover {
  background: var(--gold-glow);
  color: var(--gold-bright);
}

#page-input-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  color: var(--text-secondary);
}

#page-input {
  background: var(--bg-raised);
  border: 1px solid var(--border-normal);
  color: var(--text-primary);
  width: 52px;
  padding: 3px 6px;
  text-align: center;
  font-family: 'Crimson Pro', serif;
  font-size: 14px;
  border-radius: 3px;
  outline: none;
}

#page-input:focus { border-color: var(--gold-dim); }

#book-title-display {
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: var(--gold);
  letter-spacing: 0.05em;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.zoom-display {
  font-size: 13px;
  color: var(--text-dim);
  min-width: 44px;
  text-align: center;
}

/* ─── READING PROGRESS ─── */
#reading-progress {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  height: 3px;
  z-index: 6;
  pointer-events: none;
}

#reading-progress-bar {
  height: 100%;
  background: var(--gold);
  width: 0%;
  transition: width 0.3s ease;
}

/* ─── SEPIA MODE ─── */
#app.sepia .pdf-page-wrapper canvas { filter: sepia(35%) brightness(88%); }
#app.sepia #epub-viewer { filter: sepia(35%) brightness(88%); }

/* ─── VIEWER ─── */
#viewer {
  grid-area: viewer;
  background: var(--bg-void);
  overflow: hidden;
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
}

#pdf-canvas-container {
  position: absolute;
  top: 0; right: 0; bottom: 0; left: 0;
  overflow: auto;
  padding: 16px;
}

#pdf-pages-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: max-content;
  min-width: 100%;
  padding-bottom: 40px;
}

.pdf-page-wrapper {
  flex-shrink: 0;
  background: var(--bg-raised);
  box-shadow: 0 4px 40px rgba(0,0,0,0.8), 0 0 0 1px rgba(201,169,74,0.08);
}

#epub-viewer {
  display: none;
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--bg-deep);
}

#epub-viewer iframe {
  border: none !important;
}

.book-type-badge {
  font-size: 9px;
  font-family: 'Cinzel', serif;
  color: var(--gold-dim);
  border: 1px solid var(--gold-dim);
  padding: 1px 4px;
  border-radius: 2px;
  flex-shrink: 0;
  opacity: 0.8;
}

.pdf-page-wrapper canvas {
  display: block;
}

#welcome-screen {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px;
  text-align: center;
  pointer-events: none;
}

.welcome-rune {
  font-family: 'Cinzel', serif;
  font-size: 64px;
  color: var(--gold-dim);
  opacity: 0.5;
  line-height: 1;
}

.welcome-title {
  font-family: 'Cinzel', serif;
  font-size: 22px;
  font-weight: 700;
  color: var(--gold);
  letter-spacing: 0.1em;
}

.welcome-sub {
  font-size: 16px;
  color: var(--text-muted);
  max-width: 360px;
  line-height: 1.6;
  font-style: italic;
}

/* ─── LOADING / SPINNER ─── */
#loading-overlay {
  position: absolute;
  inset: 0;
  background: rgba(8,10,13,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  z-index: 20;
  display: none;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 2px solid var(--border-accent);
  border-top-color: var(--gold);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-text {
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: var(--gold-dim);
  letter-spacing: 0.1em;
}

/* ─── NOTES PANEL ─── */
#notes-panel {
  position: absolute;
  right: 0;
  top: 0;
  bottom: 0;
  width: 300px;
  background: var(--bg-deep);
  border-left: 1px solid var(--border-subtle);
  z-index: 15;
  display: flex;
  flex-direction: column;
  transform: translateX(100%);
  transition: transform var(--transition);
}

#notes-panel.open { transform: translateX(0); }

.panel-header {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border-subtle);
  font-family: 'Cinzel', serif;
  font-size: 13px;
  color: var(--gold);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.panel-close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  padding: 4px;
  display: flex;
  transition: color var(--transition);
}

.panel-close:hover { color: var(--text-bright); }

.notes-book-label {
  padding: 8px 12px;
  font-size: 12px;
  color: var(--text-muted);
  background: var(--bg-surface);
  border-bottom: 1px solid var(--border-subtle);
  font-style: italic;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.notes-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.note-card {
  background: var(--bg-surface);
  border: 1px solid var(--border-subtle);
  border-radius: 4px;
  padding: 8px 10px;
  font-size: 13px;
}

.note-page { font-size: 11px; color: var(--gold-dim); margin-bottom: 4px; font-family: 'Cinzel', serif; }
.note-text { color: var(--text-primary); line-height: 1.4; }

.note-delete {
  float: right;
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 11px;
  padding: 0;
  transition: color var(--transition);
}

.note-delete:hover { color: var(--red); }

.notes-form {
  padding: 10px;
  border-top: 1px solid var(--border-subtle);
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.notes-form textarea {
  background: var(--bg-surface);
  border: 1px solid var(--border-normal);
  color: var(--text-primary);
  padding: 7px 9px;
  font-family: 'Crimson Pro', serif;
  font-size: 14px;
  border-radius: 4px;
  resize: none;
  outline: none;
  height: 70px;
  transition: border-color var(--transition);
}

.notes-form textarea:focus { border-color: var(--gold-dim); }
.notes-form textarea::placeholder { color: var(--text-muted); }

.notes-form-footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-muted);
}

/* ─── BOOKMARK TOAST ─── */
#toast {
  position: fixed;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%) translateY(60px);
  background: var(--bg-panel);
  border: 1px solid var(--gold-dim);
  color: var(--gold);
  padding: 8px 18px;
  border-radius: 4px;
  font-family: 'Cinzel', serif;
  font-size: 13px;
  letter-spacing: 0.06em;
  z-index: 100;
  transition: transform 0.3s ease;
  pointer-events: none;
}

#toast.show { transform: translateX(-50%) translateY(0); }

/* ─── CONTEXT MENU ─── */
#context-menu {
  position: fixed;
  background: var(--bg-panel);
  border: 1px solid var(--border-accent);
  border-radius: 4px;
  padding: 4px 0;
  z-index: 200;
  min-width: 160px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.6);
  display: none;
}

.context-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 14px;
  cursor: pointer;
  font-size: 14px;
  color: var(--text-primary);
  transition: background var(--transition);
}

.context-item:hover { background: var(--bg-hover); color: var(--gold); }
.context-divider { height: 1px; background: var(--border-subtle); margin: 4px 0; }

/* ─── KEYBOARD SHORTCUTS MODAL ─── */
#shortcuts-modal {
  position: fixed;
  inset: 0;
  background: rgba(8,10,13,0.85);
  z-index: 300;
  display: none;
  align-items: center;
  justify-content: center;
}

#shortcuts-modal.open { display: flex; }

.shortcuts-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-accent);
  border-radius: 6px;
  padding: 24px;
  max-width: 460px;
  width: 90%;
}

.shortcuts-title {
  font-family: 'Cinzel', serif;
  font-size: 16px;
  color: var(--gold);
  margin-bottom: 16px;
  text-align: center;
  letter-spacing: 0.1em;
}

.shortcut-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 0;
  border-bottom: 1px solid var(--border-subtle);
  font-size: 14px;
}

.shortcut-row:last-child { border-bottom: none; }
.shortcut-action { color: var(--text-secondary); }

kbd {
  background: var(--bg-surface);
  border: 1px solid var(--border-accent);
  border-radius: 3px;
  padding: 2px 6px;
  font-size: 12px;
  font-family: monospace;
  color: var(--gold);
}

/* ─── PRINT MODAL ─── */

#print-modal {
  position: fixed;
  inset: 0;
  background: rgba(8,10,13,0.85);
  z-index: 300;
  display: none;
  align-items: center;
  justify-content: center;
}

#print-modal.open { display: flex; }

.print-box {
  background: var(--bg-panel);
  border: 1px solid var(--border-accent);
  border-radius: 6px;
  padding: 24px;
  width: 360px;
  max-width: 92vw;
}

.print-title {
  font-family: 'Cinzel', serif;
  font-size: 15px;
  color: var(--gold);
  margin-bottom: 6px;
  text-align: center;
  letter-spacing: 0.1em;
}

.print-book-name {
  font-size: 12px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 18px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  font-style: italic;
}

#print-form { margin-bottom: 18px; }

.print-radio {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 14px;
  color: var(--text-primary);
  cursor: pointer;
  user-select: none;
}

.print-radio input[type=radio] { accent-color: var(--gold); cursor: pointer; }

.print-range-hint { color: var(--text-dim); font-size: 13px; }

#print-range-inputs {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 8px 0 4px 22px;
  font-size: 14px;
  color: var(--text-secondary);
}

#print-range-inputs.visible { display: flex; }

#print-range-inputs input {
  width: 64px;
  background: var(--bg-surface);
  border: 1px solid var(--border-normal);
  color: var(--text-primary);
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 14px;
  font-family: monospace;
  text-align: center;
}

#print-range-inputs input:focus { border-color: var(--gold-dim); outline: none; }

#print-progress-wrap {
  display: none;
  margin-bottom: 16px;
}

#print-progress-label {
  font-size: 13px;
  color: var(--text-secondary);
  margin-bottom: 7px;
}

.print-progress-track {
  background: var(--bg-surface);
  border: 1px solid var(--border-normal);
  border-radius: 3px;
  height: 7px;
  overflow: hidden;
}

#print-progress-fill {
  background: var(--gold);
  height: 100%;
  width: 0%;
  transition: width 0.1s linear;
  border-radius: 3px;
}

.print-footer {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  padding-top: 4px;
}

/* ─── EMPTY STATES ─── */
.empty-tree {
  padding: 20px 16px;
  font-size: 13px;
  color: var(--text-muted);
  font-style: italic;
  line-height: 1.6;
}

.error-msg {
  padding: 20px;
  text-align: center;
  color: var(--red);
  font-size: 14px;
}

/* ─── SCROLLBAR FOR CANVAS ─── */
#pdf-canvas-container::-webkit-scrollbar { width: 8px; height: 8px; }

/* ─── RESPONSIVE COLLAPSE TOGGLE ─── */
#sidebar-toggle {
  flex-shrink: 0;
}

/* ─── FULLSCREEN BUTTON ─── */
#btn-fullscreen {
  display: none; /* desktop: hidden — keyboard F11 works there */
}

/* ─── MOBILE ─── */
@media (max-width: 768px) {

  #btn-fullscreen {
    display: flex;
    position: fixed;
    bottom: 20px;
    right: 16px;
    width: 48px;
    height: 48px;
    align-items: center;
    justify-content: center;
    background: var(--bg-panel);
    border: 1px solid var(--border-accent);
    border-radius: 50%;
    color: var(--gold);
    cursor: pointer;
    z-index: 30;
    box-shadow: 0 2px 20px rgba(0, 0, 0, 0.7);
    -webkit-tap-highlight-color: transparent;
    transition: background var(--transition), transform 0.12s ease, opacity var(--transition);
  }

  #btn-fullscreen:active {
    background: var(--bg-active);
    transform: scale(0.9);
  }

  /* Fade the button when sidebar or notes panel are open */
  #btn-fullscreen.dimmed {
    opacity: 0.2;
    pointer-events: none;
  }

  /* ── Sidebar as full-height overlay ── */
  #app {
    grid-template-columns: 0 1fr !important;
  }

  #sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: min(85vw, 320px);
    max-width: none;
    transform: translateX(-100%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 50;
  }

  #sidebar.mobile-open {
    transform: translateX(0);
    box-shadow: 6px 0 32px rgba(0, 0, 0, 0.7);
  }

  #sidebar-resize-handle { display: none; }

  /* ── Backdrop ── */
  #sidebar-backdrop {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 49;
    -webkit-tap-highlight-color: transparent;
  }
  #sidebar-backdrop.visible { display: block; }

  /* ── Toolbar: scrollable, no text labels ── */
  #toolbar {
    overflow-x: auto;
    scrollbar-width: none;
    gap: 2px;
    padding: 0 max(6px, env(safe-area-inset-left));
    -webkit-overflow-scrolling: touch;
  }
  #toolbar::-webkit-scrollbar { display: none; }

  #book-title-display { display: none; }

  .btn-label { display: none; }

  /* ── Bigger touch targets ── */
  .btn {
    padding: 10px 11px;
    min-height: 44px;
    min-width: 38px;
    justify-content: center;
  }

  .btn-gold { min-width: 44px; }

  /* ── Tree items ── */
  .tree-book { padding: 11px 14px; }
  .tree-folder-header { padding: 11px 14px; }
  .toc-item { padding: 9px 8px; font-size: 14px; }
  .bookmark-item { padding: 11px 8px; }
  .bookmark-delete { opacity: 1; padding: 8px; }

  /* ── Right panels: full width on mobile ── */
  #notes-panel, #ref-panel { width: 100%; }

  /* ── Viewer padding ── */
  #pdf-canvas-container { padding: 6px; }

  /* ── Prevent iOS auto-zoom on input focus (requires 16px) ── */
  .search-box input { font-size: 16px; padding: 9px 10px 9px 32px; }
  #page-input { font-size: 16px; width: 58px; padding: 6px 4px; }
  #page-input-wrap { font-size: 15px; gap: 4px; }
  .notes-form textarea { font-size: 16px; }

  /* ── Welcome screen ── */
  .welcome-rune { font-size: 48px; }
  .welcome-title { font-size: 18px; }
  .welcome-sub { font-size: 14px; }

  /* ── Shortcuts modal ── */
  .shortcuts-box { padding: 16px; width: 95%; }
  .shortcut-row { font-size: 13px; gap: 8px; flex-wrap: wrap; }
}

/* ─── LANDSCAPE PHONES ───────────────────────────────────────────────────────
   Phones in landscape are wider than 768px (iPhone 14 = 844px) so the normal
   portrait breakpoint misses them. Short height (≤ 500px) reliably identifies
   a phone in landscape vs a tablet or desktop.                               */
@media (orientation: landscape) and (max-height: 600px) {
  /* Force sidebar into overlay mode, same as portrait mobile */
  #app { grid-template-columns: 0 1fr !important; }

  #sidebar {
    position: fixed;
    left: 0; top: 0; bottom: 0;
    width: min(75vw, 320px);
    max-width: none;
    transform: translateX(-100%);
    transition: transform 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 50;
  }
  #sidebar.mobile-open {
    transform: translateX(0);
    box-shadow: 6px 0 32px rgba(0, 0, 0, 0.7);
  }
  #sidebar-resize-handle { display: none; }

  /* Show fullscreen button */
  #btn-fullscreen { display: flex; }

  /* Compact touch targets for landscape (less height to spare) */
  .btn { min-height: 40px; }
  #notes-panel, #ref-panel { width: 100%; }
  #pdf-canvas-container { padding: 6px; }
}

/* ─── FULLSCREEN — hide sidebar and panels regardless of how they got open ── */
:fullscreen #sidebar,
:-webkit-full-screen #sidebar {
  transform: translateX(-100%) !important;
  box-shadow: none !important;
}
:fullscreen #sidebar-backdrop,
:-webkit-full-screen #sidebar-backdrop {
  display: none !important;
}
:fullscreen #ref-panel,
:-webkit-full-screen #ref-panel {
  transform: translateX(100%) !important;
}
