:root {
  --bg: #1e1f22;
  --bg-2: #2b2d31;
  --bg-3: #383a40;
  --fg: #e6e6e6;
  --muted: #9aa0a6;
  --accent: #3b82f6;
  --accent-2: #2563eb;
  --border: #3a3c42;
  --hl: rgba(59, 130, 246, 0.35);
  --canvasbg: #313338;
}

:root[data-theme="light"] {
  --bg: #f5f6f8;
  --bg-2: #ffffff;
  --bg-3: #e9ebef;
  --fg: #1f2329;
  --muted: #6b7280;
  --accent: #2563eb;
  --accent-2: #1d4ed8;
  --border: #d7dbe0;
  --hl: rgba(37, 99, 235, 0.22);
  --canvasbg: #c9ccd2;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body {
  font: 14px/1.45 "Segoe UI", system-ui, sans-serif;
  color: var(--fg);
  background: var(--bg);
  display: flex;
  flex-direction: column;
}
.muted { color: var(--muted); font-size: 12px; }
.hidden { display: none !important; }

/* ---- Topbar ---- */
#topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 12px; padding: 6px 10px;
  background: var(--bg-2); border-bottom: 1px solid var(--border);
  flex: 0 0 auto;
}
.tb-group { display: flex; align-items: center; gap: 8px; }
.tb-center { flex: 1; justify-content: center; }
.doc-title { font-weight: 600; max-width: 220px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.icon-btn {
  background: transparent; border: 1px solid transparent; color: var(--fg);
  width: 30px; height: 30px; border-radius: 6px; cursor: pointer; font-size: 15px;
}
.icon-btn:hover { background: var(--bg-3); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; border-radius: 4px; }
.tab[aria-selected="true"] { color: var(--fg); }
.search-box, .page-box, .zoom-box {
  display: flex; align-items: center; gap: 4px;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 2px 6px;
}
.search-box input { width: 200px; }
input, textarea, select {
  background: var(--bg); color: var(--fg); border: 1px solid var(--border);
  border-radius: 6px; padding: 5px 8px; outline: none;
}
.search-box input, .page-box input { border: none; background: transparent; }
.page-box input { width: 46px; text-align: center; }

/* ---- Layout ---- */
#layout { flex: 1; display: flex; min-height: 0; }
#left-panel { width: 260px; background: var(--bg-2); border-right: 1px solid var(--border); display: flex; flex-direction: column; }
#right-panel { width: 360px; background: var(--bg-2); border-left: 1px solid var(--border); display: flex; flex-direction: column; }
#left-panel.collapsed, #right-panel.collapsed { display: none; }

/* Адаптив: узкие окна — уже панели, затем оверлей поверх просмотрщика */
@media (max-width: 1100px) {
  #left-panel { width: 210px; }
  #right-panel { width: 300px; }
}
@media (max-width: 820px) {
  #left-panel, #right-panel { position: absolute; top: 0; bottom: 0; z-index: 200; box-shadow: 0 0 24px rgba(0,0,0,.5); }
  #left-panel { left: 0; width: 260px; }
  #right-panel { right: 0; width: 320px; }
}

/* ---- Left panel ---- */
.tabs { display: flex; border-bottom: 1px solid var(--border); }
.tab { flex: 1; background: transparent; border: none; color: var(--muted); padding: 9px 4px; cursor: pointer; font-size: 12px; }
.tab.active { color: var(--fg); border-bottom: 2px solid var(--accent); }
.tab-pane { flex: 1; overflow: auto; padding: 8px; display: none; }
.tab-pane.active { display: block; }
#pane-outline a { display: block; color: var(--fg); text-decoration: none; padding: 3px 4px; border-radius: 4px; font-size: 13px; }
#pane-outline a:hover { background: var(--bg-3); }
.thumb { cursor: pointer; margin-bottom: 10px; border: 2px solid transparent; border-radius: 4px; }
.thumb.active { border-color: var(--accent); }
.thumb canvas { width: 100%; display: block; border-radius: 2px; }
.thumb-ph { width: 100%; background: var(--bg-3); border-radius: 2px; }
.thumb .num { text-align: center; font-size: 11px; color: var(--muted); }
#bookmark-list { list-style: none; padding: 0; margin: 8px 0 0; }
#bookmark-list li { display: flex; justify-content: space-between; padding: 5px 4px; border-radius: 4px; cursor: pointer; }
#bookmark-list li:hover { background: var(--bg-3); }
#bookmark-list .ai-tag { color: var(--accent); font-size: 10px; }
.ghost-btn { width: 100%; background: var(--bg-3); border: 1px dashed var(--border); color: var(--fg); padding: 6px; border-radius: 6px; cursor: pointer; }
#comment-list { list-style: none; padding: 0; margin: 0; }
#comment-list .cm-empty { padding: 10px 6px; }
.comment-item { display: flex; align-items: flex-start; gap: 6px; padding: 7px 4px; border-bottom: 1px solid var(--border); }
.comment-item .cm-main { flex: 1; cursor: pointer; font-size: 13px; }
.comment-item:hover { background: var(--bg-3); }
.cm-page { color: var(--accent); font-size: 11px; }
.cm-text { color: var(--fg); }
.cm-note { color: var(--muted); font-size: 12px; margin-top: 2px; }
.cm-del { background: transparent; border: none; color: var(--muted); cursor: pointer; font-size: 12px; }
.cm-del:hover { color: #ef4444; }

/* ---- Viewer ---- */
#viewer-container { flex: 1; overflow: auto; position: relative; background: var(--canvasbg); }
#pages { display: flex; flex-direction: column; align-items: center; gap: 16px; padding: 20px 0; }
#pages.mode-two { flex-flow: row wrap; align-items: flex-start; align-content: flex-start; }
#pages.mode-single .page-wrap { display: none; }
#pages.mode-single .page-wrap.single-current { display: block; }
.page-wrap { position: relative; box-shadow: 0 2px 12px rgba(0,0,0,.5); background: #fff; }
.page-wrap canvas { display: block; }
.text-layer { position: absolute; inset: 0; overflow: hidden; opacity: 0.25; line-height: 1; }
.text-layer span { position: absolute; color: transparent; white-space: pre; cursor: text; transform-origin: 0 0; }
.text-layer ::selection { background: var(--hl); }
.search-hit { background: rgba(255, 213, 0, .5); }
.search-hit.current { background: rgba(255, 145, 0, .8); }

/* Аннотации (М3) */
.ann-layer { position: absolute; inset: 0; pointer-events: none; }
.ann { position: absolute; pointer-events: auto; cursor: pointer; }
.ann-highlight { mix-blend-mode: multiply; opacity: .55; border-radius: 2px; }
.ann-strike::after { content: ""; position: absolute; left: 0; right: 0; top: 50%; border-top: 2px solid var(--strike, #ef4444); }
.ann-has-note { box-shadow: 0 0 0 1px rgba(245,158,11,.8) inset; }
.ann-note-pin { position: absolute; transform: translate(-100%, -4px); font-size: 13px; cursor: pointer; pointer-events: auto; filter: drop-shadow(0 1px 2px rgba(0,0,0,.5)); }
.ann-draw-svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; pointer-events: none; }
.ann-draw-svg polyline { pointer-events: stroke; cursor: pointer; }
#pages.drawing .text-layer { pointer-events: none; }
#pages.drawing .page-wrap { cursor: crosshair; }
#draw-color { width: 26px; height: 26px; padding: 0; border: 1px solid var(--border); border-radius: 6px; background: none; cursor: pointer; }
.menu-sep { height: 1px; background: var(--border); margin: 4px 2px; }

/* Инверсия цветов PDF (тёмное чтение) — только canvas, текст-слой прозрачный */
#pages.invert .page-wrap canvas { filter: invert(1) hue-rotate(180deg); }
.icon-btn.active { background: var(--accent); color: #fff; }
.icon-btn.opt { width: 26px; height: 26px; font-size: 11px; }

/* Оверлей горячих клавиш */
#help-overlay { position: fixed; inset: 0; z-index: 2000; display: flex; align-items: center; justify-content: center; background: rgba(0,0,0,.55); }
.help-box { background: var(--bg-2); border: 1px solid var(--border); border-radius: 12px; padding: 20px 24px; min-width: 320px; box-shadow: 0 10px 40px rgba(0,0,0,.5); }
.help-box h3 { margin: 0 0 12px; }
.help-box table { width: 100%; border-collapse: collapse; margin-bottom: 14px; }
.help-box td { padding: 5px 6px; font-size: 13px; }
.help-box td:first-child { color: var(--accent); white-space: nowrap; font-family: monospace; }
.help-box td:last-child { color: var(--muted); }

/* Печать: только страницы документа, без интерфейса */
@media print {
  #topbar, #left-panel, #right-panel, #status-overlay, #selection-menu, #help-overlay { display: none !important; }
  #layout, #viewer-container, #pages { display: block; overflow: visible; height: auto; background: #fff; }
  .text-layer, .ann-layer { display: none !important; }
  .page-wrap { box-shadow: none; margin: 0 auto; break-after: page; page-break-after: always; }
  .page-wrap canvas { filter: none !important; }
}

#status-overlay { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; background: rgba(20,20,22,.7); }
.open-prompt { display: flex; flex-direction: column; align-items: center; gap: 8px; background: var(--bg-2); border: 1px solid var(--border); padding: 24px 28px; border-radius: 12px; }
#viewer-container.dragover { outline: 3px dashed var(--accent); outline-offset: -6px; }
.open-prompt .recent { margin-top: 8px; width: 100%; max-width: 360px; }
.open-prompt .recent ul { list-style: none; padding: 0; margin: 4px 0 0; max-height: 200px; overflow: auto; }
.open-prompt .recent li { padding: 6px 8px; border-radius: 6px; cursor: pointer; color: var(--accent); font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.open-prompt .recent li:hover { background: var(--bg-3); }
#ocr-banner { position: sticky; top: 0; z-index: 50; display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--bg-2); border-bottom: 1px solid var(--border); }
#ocr-banner .ghost-btn { width: auto; }
#ocr-banner .primary-btn, #ocr-banner .ghost-btn { padding: 4px 10px; }
.spinner { width: 36px; height: 36px; border: 3px solid var(--bg-3); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* ---- AI panel ---- */
.ai-header { display: flex; align-items: center; justify-content: space-between; padding: 10px; border-bottom: 1px solid var(--border); font-weight: 600; }
.ai-header select { font-size: 12px; }
#quick-actions { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px; border-bottom: 1px solid var(--border); max-height: 160px; overflow: auto; }
.qa-btn { background: var(--bg-3); border: 1px solid var(--border); color: var(--fg); padding: 5px 9px; border-radius: 14px; font-size: 12px; cursor: pointer; }
.qa-btn:hover { background: var(--accent); border-color: var(--accent); }
#chat-messages { flex: 1; overflow: auto; padding: 10px; display: flex; flex-direction: column; gap: 10px; }
.msg { padding: 8px 10px; border-radius: 10px; max-width: 95%; white-space: pre-wrap; word-wrap: break-word; }
.msg.user { align-self: flex-end; background: var(--accent-2); }
.msg.assistant { align-self: flex-start; background: var(--bg-3); }
.msg .cite { color: var(--accent); cursor: pointer; text-decoration: underline; }
.chat-input { display: flex; gap: 6px; padding: 8px; border-top: 1px solid var(--border); }
.chat-input textarea { flex: 1; resize: none; }
.primary-btn { background: var(--accent); border: none; color: #fff; padding: 6px 12px; border-radius: 6px; cursor: pointer; }
.primary-btn:hover { background: var(--accent-2); }
.primary-btn:disabled { opacity: .5; cursor: default; }
#ai-meta { padding: 4px 10px 8px; }

/* ---- Selection context menu ---- */
#selection-menu { position: fixed; z-index: 1000; background: var(--bg-2); border: 1px solid var(--border); border-radius: 8px; padding: 4px; box-shadow: 0 6px 20px rgba(0,0,0,.5); }
#selection-menu button { display: block; width: 100%; text-align: left; background: transparent; border: none; color: var(--fg); padding: 6px 12px; border-radius: 4px; cursor: pointer; font-size: 13px; }
#selection-menu button:hover { background: var(--accent); }

/* Превью страницы при наведении на оглавление */
#page-preview { position: fixed; z-index: 1500; background: var(--bg-2); border: 1px solid var(--border); border-radius: 6px; padding: 4px; box-shadow: 0 6px 24px rgba(0,0,0,.5); pointer-events: none; }
#page-preview canvas { display: block; width: 180px; height: auto; border-radius: 3px; }
#page-preview .pp-num { text-align: center; font-size: 11px; color: var(--muted); padding-top: 2px; }
