/* ── Reset & Variables ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f5f0;
  --surface: #ffffff;
  --surface2: #f0ede8;
  --border: #ddd;
  --text: #1a1a1a;
  --text2: #555;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --danger: #dc2626;
  --sidebar-w: 280px;
  --header-h: 52px;
}

/* Dark theme */
body.theme-dark {
  --bg: #1a1a1a;
  --surface: #242424;
  --surface2: #2e2e2e;
  --border: #3a3a3a;
  --text: #e8e8e8;
  --text2: #aaa;
  --accent: #60a5fa;
  --accent-hover: #93c5fd;
}

/* Sepia theme */
body.theme-sepia {
  --bg: #f4e8c1;
  --surface: #fdf3dc;
  --surface2: #ede0b0;
  --border: #c8b88a;
  --text: #3b2d1a;
  --text2: #6b5340;
  --accent: #8b5e3c;
  --accent-hover: #6b4a2e;
}

/* ── Base ── */
body {
  font-family: system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

button {
  cursor: pointer;
  border: none;
  border-radius: 6px;
  padding: 6px 14px;
  font-size: 13px;
  font-weight: 500;
  background: var(--accent);
  color: #fff;
  transition: background 0.15s;
}
button:hover { background: var(--accent-hover); }
button.secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}
button.secondary:hover { background: var(--border); }
button.danger { background: var(--danger); }

input, textarea {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 13px;
  font-family: inherit;
  outline: none;
}
input:focus, textarea:focus { border-color: var(--accent); }

/* ── Header ── */
#header {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 16px;
  gap: 12px;
  flex-shrink: 0;
  z-index: 10;
}

#header h1 { font-size: 17px; font-weight: 700; flex: 1; }

#header .theme-btns { display: flex; gap: 4px; }
#header .theme-btn {
  width: 28px; height: 28px;
  padding: 0;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px;
  border-radius: 6px;
}
#header .theme-btn.active { outline: 2px solid var(--accent); }

/* ── Main layout ── */
#app { display: flex; flex: 1; overflow: hidden; }

/* ── Library panel (home screen) ── */
#library-panel {
  flex: 1;
  overflow-y: auto;
  padding: 24px;
}

#library-toolbar {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 20px;
}

#library-toolbar h2 { font-size: 20px; font-weight: 700; flex: 1; }

#upload-zone {
  border: 2px dashed var(--border);
  border-radius: 12px;
  padding: 32px;
  text-align: center;
  color: var(--text2);
  margin-bottom: 24px;
  transition: border-color 0.2s, background 0.2s;
  cursor: pointer;
}
#upload-zone.drag-over { border-color: var(--accent); background: var(--surface2); }
#upload-zone p { margin-top: 8px; font-size: 13px; }

#pdf-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.pdf-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  cursor: pointer;
  transition: box-shadow 0.15s, border-color 0.15s;
}
.pdf-card:hover { box-shadow: 0 4px 12px rgba(0,0,0,.08); border-color: var(--accent); }
.pdf-card .pdf-icon { font-size: 32px; }
.pdf-card .pdf-name { font-weight: 600; font-size: 13px; word-break: break-word; }
.pdf-card .pdf-meta { font-size: 11px; color: var(--text2); }
.pdf-card .pdf-progress-bar {
  height: 4px; border-radius: 2px;
  background: var(--surface2);
  overflow: hidden;
}
.pdf-card .pdf-progress-bar span {
  display: block; height: 100%; background: var(--accent);
}
.pdf-card .card-actions { display: flex; gap: 6px; margin-top: 4px; }
.pdf-card .card-actions button { font-size: 11px; padding: 3px 8px; flex: 1; }

/* ── Viewer layout ── */
#viewer-panel {
  display: none;
  flex: 1;
  overflow: hidden;
  flex-direction: column;
}
#viewer-panel.active { display: flex; }

#viewer-toolbar {
  height: var(--header-h);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  padding: 0 12px;
  gap: 8px;
  flex-shrink: 0;
}
#viewer-toolbar .pdf-title { font-weight: 600; font-size: 14px; max-width: 260px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#page-info { font-size: 13px; color: var(--text2); min-width: 80px; text-align: center; }

#search-box { width: 160px; }
#search-count { font-size: 12px; color: var(--text2); white-space: nowrap; }

/* ── Viewer body (canvas + sidebar) ── */
#viewer-body {
  display: flex;
  flex: 1;
  overflow: hidden;
}

#pdf-canvas-container {
  flex: 1;
  overflow-y: auto;
  background: #888;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px 0;
  gap: 8px;
  position: relative;
}

.pdf-page-wrapper {
  position: relative;
  box-shadow: 0 2px 12px rgba(0,0,0,.3);
}
.pdf-page-wrapper canvas { display: block; }
.pdf-page-wrapper .textLayer {
  position: absolute;
  top: 0; left: 0;
  width: 100%; height: 100%;
  overflow: hidden;
  opacity: 0.2;
  line-height: 1;
  pointer-events: auto;
}
.pdf-page-wrapper .textLayer > span {
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}
mark.search-highlight { background: rgba(255, 200, 0, 0.55); border-radius: 2px; }
mark.search-highlight.current { background: rgba(255, 120, 0, 0.75); }

/* Dark PDF invert trick */
body.theme-dark #pdf-canvas-container .pdf-page-wrapper canvas {
  filter: invert(1) hue-rotate(180deg);
}
body.theme-sepia #pdf-canvas-container .pdf-page-wrapper canvas {
  filter: sepia(0.55) brightness(0.92);
}

/* ── Sidebar ── */
#sidebar {
  width: var(--sidebar-w);
  border-left: 1px solid var(--border);
  background: var(--surface);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  flex-shrink: 0;
}

#sidebar-tabs {
  display: flex;
  border-bottom: 1px solid var(--border);
}
.sidebar-tab {
  flex: 1;
  padding: 10px 6px;
  font-size: 12px;
  font-weight: 600;
  text-align: center;
  cursor: pointer;
  color: var(--text2);
  background: transparent;
  border-bottom: 2px solid transparent;
  border-radius: 0;
}
.sidebar-tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.sidebar-tab:hover { background: var(--surface2); }

.sidebar-pane { display: none; flex: 1; flex-direction: column; overflow: hidden; }
.sidebar-pane.active { display: flex; }

/* Bookmarks pane */
#bookmarks-pane { padding: 10px; gap: 8px; }
#bookmark-form { display: flex; flex-direction: column; gap: 6px; }
#bookmark-form input { width: 100%; }
#bookmark-list { overflow-y: auto; flex: 1; display: flex; flex-direction: column; gap: 6px; }
.bookmark-item {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px 10px;
  cursor: pointer;
  display: flex;
  align-items: flex-start;
  gap: 6px;
}
.bookmark-item:hover { border-color: var(--accent); }
.bookmark-item .bm-info { flex: 1; }
.bookmark-item .bm-page { font-size: 11px; color: var(--accent); font-weight: 600; }
.bookmark-item .bm-label { font-size: 12px; font-weight: 500; word-break: break-word; }
.bookmark-item .bm-del { background: transparent; color: var(--danger); padding: 2px 4px; font-size: 14px; }
.bookmark-item .bm-del:hover { background: var(--danger); color: #fff; }

/* C++ pane */
#cpp-pane { overflow: hidden; }
#cpp-pane .cpp-inner { display: flex; flex-direction: column; height: 100%; }
.cpp-editor-wrap { flex: 1; overflow: hidden; border-bottom: 1px solid var(--border); }
.cpp-editor-wrap .CodeMirror { height: 100%; font-size: 12px; }
.cpp-controls { padding: 8px; display: flex; flex-direction: column; gap: 6px; }
.cpp-controls textarea { resize: none; height: 50px; width: 100%; font-family: monospace; font-size: 12px; }
.cpp-controls .run-row { display: flex; gap: 6px; align-items: center; }
.cpp-controls .run-row button { flex-shrink: 0; }
#cpp-output {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 8px;
  font-family: monospace;
  font-size: 11px;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 120px;
  overflow-y: auto;
  color: var(--text);
}
#cpp-output .err { color: #f87171; }

/* ── Token modal ── */
#token-modal {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,.5);
  z-index: 100;
  align-items: center;
  justify-content: center;
}
#token-modal.show { display: flex; }
#token-modal .modal-box {
  background: var(--surface);
  border-radius: 12px;
  padding: 32px;
  min-width: 320px;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
#token-modal h2 { font-size: 18px; }
#token-modal p { font-size: 13px; color: var(--text2); }
#token-modal input { width: 100%; }
#token-modal .modal-actions { display: flex; justify-content: flex-end; }

/* ── Scrollbar (nice on WebKit) ── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
