:root {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --text: #16181d;
  --text-dim: #666a73;
  --accent: #2a63f6;
  --accent-contrast: #ffffff;
  --border: #dfe1e6;
  --bubble-user: #2a63f6;
  --bubble-user-text: #ffffff;
  --bubble-assistant: #eef0f4;
  --bubble-assistant-text: #16181d;
  --danger: #e0432b;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #111318;
    --surface: #1b1e26;
    --text: #eceef2;
    --text-dim: #9aa0ab;
    --accent: #5b8dff;
    --accent-contrast: #0b0d12;
    --border: #2b2f3a;
    --bubble-user: #5b8dff;
    --bubble-user-text: #0b0d12;
    --bubble-assistant: #262a34;
    --bubble-assistant-text: #eceef2;
    --danger: #ff6b52;
  }
}

/* explicit theme overrides from Settings, independent of OS preference */
:root[data-theme="dark"] {
  --bg: #111318;
  --surface: #1b1e26;
  --text: #eceef2;
  --text-dim: #9aa0ab;
  --accent: #5b8dff;
  --accent-contrast: #0b0d12;
  --border: #2b2f3a;
  --bubble-user: #5b8dff;
  --bubble-user-text: #0b0d12;
  --bubble-assistant: #262a34;
  --bubble-assistant-text: #eceef2;
  --danger: #ff6b52;
}

:root[data-theme="light"] {
  --bg: #f4f4f6;
  --surface: #ffffff;
  --text: #16181d;
  --text-dim: #666a73;
  --accent: #2a63f6;
  --accent-contrast: #ffffff;
  --border: #dfe1e6;
  --bubble-user: #2a63f6;
  --bubble-user-text: #ffffff;
  --bubble-assistant: #eef0f4;
  --bubble-assistant-text: #16181d;
  --danger: #e0432b;
}

* { box-sizing: border-box; }

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
  /* Nothing outside the app's own scrollable regions (.viewer-container,
   * .sheet-messages, .history-list, ...) may ever scroll the *document*
   * itself: #topbar/#app sit in normal document flow, so any outer scroll
   * drags the whole app — including the top bar — up and off the top edge
   * (spec: "Страница не уходит с экрана никогда"). Belt-and-braces on top
   * of the .hiddenCanvasElement fix below, which removes the actual stray
   * content that was making body tall enough to scroll in the first place. */
  overflow: hidden;
}

/* pdf.js's TextLayer (vendor/pdfjs/pdf.mjs, the #getCtx font-metrics helper)
 * appends an off-DOM-tree `<canvas class="hiddenCanvasElement">` straight to
 * document.body to measure font ascent, and keeps reusing/resizing it for
 * the lifetime of the page — it expects the host app to ship pdf_viewer.css,
 * which hides it via this exact rule. We don't load that stylesheet (our
 * viewer chrome is entirely custom), so without this rule the canvas rendered
 * as a normal in-flow block appended after #app, making body ~20px taller
 * than the viewport. That stray overflow is exactly what a focused input or
 * a selection-drag near the screen edge auto-scrolls into view, which is
 * what dragged #topbar off the top edge in phone portrait (bug: topbar
 * clipped at the top). Copied verbatim from vendor/pdfjs/pdf_viewer.css. */
#hiddenCopyElement,
.hiddenCanvasElement {
  position: absolute;
  top: 0;
  left: 0;
  width: 0;
  height: 0;
  display: none;
}

.hidden { display: none !important; }

button {
  font: inherit;
  cursor: pointer;
}

/* ---------- start screen ---------- */

.start-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  padding-top: calc(24px + var(--safe-top));
  padding-bottom: calc(24px + var(--safe-bottom));
}

.start-card,
.library-card {
  max-width: 480px;
  width: 100%;
  text-align: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 32px 24px;
}

.start-card h1,
.library-card h1 {
  margin: 0 0 8px;
  font-size: 28px;
}

.start-card p,
.library-card p {
  color: var(--text-dim);
  line-height: 1.4;
}

.library-list {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: left;
  max-height: 50vh;
  overflow-y: auto;
}

.library-item {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
}

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

.library-item-title {
  font-weight: 600;
  font-size: 15px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.library-item-meta {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 2px;
}

.library-item-menu {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 20px;
  padding: 4px 8px;
  border-radius: 8px;
  flex: none;
}

.library-item-menu:hover {
  background: var(--surface);
}

.library-item-menu-wrap {
  position: relative;
  flex: none;
}

.library-item-dropdown {
  position: absolute;
  top: 100%;
  right: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  min-width: 150px;
  z-index: 5;
  overflow: hidden;
}

.library-item-dropdown.hidden {
  display: none;
}

.library-item-dropdown-item {
  border: none;
  background: none;
  text-align: left;
  padding: 9px 12px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}

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

.library-item-dropdown-item.danger {
  color: var(--danger);
}

.library-item-title-input {
  font-weight: 600;
  font-size: 15px;
  width: 100%;
  border: 1px solid var(--accent);
  border-radius: 6px;
  padding: 2px 6px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.btn {
  display: inline-block;
  border: none;
  border-radius: 10px;
  padding: 12px 24px;
  font-size: 16px;
  font-weight: 600;
  margin-top: 12px;
  text-align: center;
}

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

.hint {
  font-size: 13px;
  margin-top: 16px;
}

.start-screen.dragover .start-card,
.start-screen.dragover .library-card {
  outline: 2px dashed var(--accent);
}

.library-notice {
  margin-top: 10px;
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--danger-bg, rgba(220, 60, 60, 0.12));
  color: var(--danger);
  font-size: 13px;
}

.library-notice.hidden {
  display: none;
}

/* ---------- app shell ---------- */

.app {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  gap: 4px;
  height: 48px;
  min-height: 48px;
  padding: 0 6px;
  padding-top: var(--safe-top);
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  z-index: 20;
  white-space: nowrap;
  position: relative;
}

/* thin progress bar glued to the bottom edge of the top bar */
.progress-bar {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 3px;
  background: transparent;
}

.progress-bar-fill {
  height: 100%;
  width: 0%;
  background: var(--accent);
  transition: width 0.15s ease;
}

/* rect-mode ("Рамка") is a mouse-only tool: hide it on touch devices so the
   top bar stays uncluttered on the phone, the app's primary target. */
@media (pointer: coarse) {
  #btn-rect-mode {
    display: none;
  }
}

.icon-btn {
  background: none;
  border: none;
  color: var(--text);
  font-size: 18px;
  line-height: 1;
  width: 36px;
  height: 36px;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
  border-radius: 8px;
}

/* "← Назад" close buttons reuse .icon-btn but carry a text label, not a
   single glyph — let them size to their content instead of clipping to the
   36px icon box (which made the label spill over the screen title next to it). */
.icon-btn.icon-btn-labeled {
  width: auto;
  min-width: 36px;
  padding: 0 8px 0 4px;
  font-size: 15px;
}

.page-indicator {
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 3px;
  font-size: 15px;
  color: var(--text-dim);
  white-space: nowrap;
}

.page-indicator input {
  /* width is set inline (in `ch`, based on the book's total page-count
     digits) so 3-digit page numbers never clip; this is just a floor. */
  width: 2ch;
  min-width: 2ch;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 4px;
  font-size: 15px;
  text-align: center;
}

.page-percent {
  font-size: 12px;
  color: var(--text-dim);
  opacity: 0.8;
}
.page-percent:empty { display: none; }

.topbar-spacer { flex: 1; }

.viewer-container {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  background: var(--bg);
  position: relative;
  /* Native pinch-zoom is disabled (pan-x/pan-y only) so a 2-finger gesture
     reaches PdfView as plain pointer events instead of the browser's own
     zoom; single-finger panning is left to the browser as usual. */
  touch-action: pan-x pan-y;
}

.viewer-container.zoomed {
  overflow-x: auto;
}

.book-loading-indicator {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  color: var(--text-muted, var(--text));
  font-size: 15px;
}

.book-loading-indicator.hidden {
  display: none;
}

.viewer {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 10px 0 40px;
}

/* zoom > 1: pages are wider than the container, so centering via
   align-items would overflow unreachably on both sides — switch to
   flex-start and let PdfView center the initial scroll position itself. */
.viewer.zoomed {
  align-items: flex-start;
}

.page {
  position: relative;
  background: #fff;
  box-shadow: 0 1px 4px rgba(0,0,0,0.2);
}

.page canvas {
  display: block;
  -webkit-touch-callout: none;
  user-select: none;
  width: 100%;
  height: 100%;
}

.page .text-layer {
  position: absolute;
  inset: 0;
  overflow: hidden;
  line-height: 1;
  opacity: 1;
}

/* PDF.js's TextLayer sizes/positions every span in the page's *unrotated*
 * coordinate frame (see vendor pdf.mjs setLayerDimensions/rawDims) and only
 * tags the layer root with a data-main-rotation attribute — it expects the
 * host app to supply the CSS that actually rotates the whole layer to match
 * the (already-rotated) canvas beneath it. Without these rules, a page with
 * /Rotate 90|270 gets a text layer sized/boxed for the *un*rotated page
 * (width/height swapped relative to the visible canvas), so selection boxes
 * land nowhere near the glyphs. Math: the layer's own box is w0 x h0 (its
 * unrotated CSS width/height); rotating around the 0,0 origin and
 * translating by its own -100% height (for 90) or -100% width (for 270)
 * lands the rotated box exactly on [0,h0] x [0,w0], i.e. the canvas's own
 * (rotation-swapped) footprint. 180 keeps the same footprint, so a plain
 * center-origin rotate is enough. */
.page .text-layer[data-main-rotation='90'] {
  transform: rotate(90deg) translateY(-100%);
  transform-origin: 0 0;
}
.page .text-layer[data-main-rotation='180'] {
  transform: rotate(180deg);
}
.page .text-layer[data-main-rotation='270'] {
  transform: rotate(270deg) translateX(-100%);
  transform-origin: 0 0;
}

.page .text-layer span,
.page .text-layer br {
  color: transparent;
  position: absolute;
  white-space: pre;
  cursor: text;
  transform-origin: 0% 0%;
}

.page .text-layer ::selection {
  background: rgba(42, 99, 246, 0.35);
}

.page .highlight-mark {
  position: absolute;
  background: rgba(255, 196, 0, 0.35);
  border-radius: 2px;
  pointer-events: none;
  z-index: 5;
}

.page .mark {
  position: absolute;
  width: 18px;
  height: 18px;
  margin-left: -9px;
  margin-top: -9px;
  border-radius: 50%;
  background: rgba(42, 99, 246, 0.55);
  border: 2px solid var(--accent-contrast, #fff);
  box-shadow: 0 1px 4px rgba(0,0,0,0.35);
  pointer-events: auto;
  cursor: pointer;
  z-index: 6;
}

.page .mark.mark-flash {
  outline: 3px solid #ffc400;
}

/* ---------- topbar title / history close ---------- */

.topbar-title {
  font-weight: 600;
  font-size: 15px;
  margin-left: 4px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* ---------- history screen ---------- */

.history-screen {
  position: fixed;
  inset: 0;
  z-index: 60;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px 12px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.history-item {
  display: flex;
  gap: 10px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px;
  cursor: pointer;
}

.history-item img {
  width: 48px;
  height: 48px;
  object-fit: contain;
  object-position: center;
  background: var(--surface-2, var(--surface));
  border-radius: 8px;
  border: 1px solid var(--border);
  flex: none;
}

.history-item-body {
  flex: 1;
  min-width: 0;
}

.history-item-page {
  font-size: 12px;
  color: var(--text-dim);
}

.history-item-question {
  font-weight: 600;
  font-size: 14px;
  margin: 2px 0 4px;
}

.history-item-answer {
  font-size: 13px;
  color: var(--text-dim);
  line-height: 1.35;
}

.history-empty {
  text-align: center;
  color: var(--text-dim);
  padding: 40px 20px;
}

/* ---------- msg highlight flash (scroll-to-message) ---------- */

.msg-flash {
  outline: 2px solid #ffc400;
  outline-offset: 2px;
}

/* ---------- selection toolbar ---------- */

.selection-toolbar {
  position: absolute;
  z-index: 40;
  display: flex;
  gap: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 6px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.25);
  transform: translate(-50%, -100%);
}

.sel-btn {
  background: var(--accent);
  color: var(--accent-contrast);
  border: none;
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 14px;
  white-space: nowrap;
}

.sel-btn[data-action="copy"] {
  background: var(--bubble-assistant);
  color: var(--text);
}

/* ---------- settings screen ---------- */

.settings-form {
  flex: 1;
  overflow-y: auto;
  padding: 12px 16px calc(24px + var(--safe-bottom));
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.settings-label {
  font-size: 13px;
  color: var(--text-dim);
  margin-top: 14px;
}

.settings-textarea,
.settings-form select,
.settings-form input[type="number"] {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}

.settings-textarea {
  resize: vertical;
  min-height: 120px;
}

.settings-btn-secondary {
  align-self: flex-start;
  background: var(--bubble-assistant);
  color: var(--text);
  font-size: 13px;
  padding: 8px 14px;
}

.settings-spend {
  margin-top: 16px;
  font-size: 14px;
  color: var(--text-dim);
}

.settings-advanced {
  margin-top: 16px;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 12px 12px;
}

.settings-advanced summary {
  cursor: pointer;
  padding: 10px 0;
  font-size: 14px;
  font-weight: 600;
  color: var(--text);
}

.settings-advanced .hint {
  margin-top: 4px;
  color: var(--text-dim);
}

.settings-btn-save {
  margin-top: 20px;
}

/* ---------- account block (top of settings) ---------- */

.settings-account {
  padding: 14px 16px 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  border-bottom: 1px solid var(--border);
  padding-bottom: 14px;
}

.settings-account-status {
  font-size: 13px;
  color: var(--text-dim);
  margin: 0;
}

.settings-account-form {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.settings-account-form input {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 10px;
  font-size: 14px;
  font-family: inherit;
}

.settings-account-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.settings-account-error {
  font-size: 13px;
  color: #e5484d;
  margin: 0;
}

.settings-account-hint {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 9999;
  background: rgba(0, 0, 0, 0.85);
  color: #fff;
  font-size: 13px;
  padding: 10px 14px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  gap: 10px;
  max-width: calc(100vw - 32px);
}

.settings-account-hint button {
  background: transparent;
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.4);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
}

/* ---------- rectangle drawing (Entry C) ---------- */

.rect-draw {
  position: absolute;
  border: 2px solid var(--accent);
  background: rgba(42, 99, 246, 0.18);
  z-index: 35;
  pointer-events: auto;
}

.rect-handle {
  position: absolute;
  width: 20px;
  height: 20px;
  margin-left: -10px;
  margin-top: -10px;
  border-radius: 50%;
  background: var(--accent);
  border: 2px solid var(--accent-contrast, #fff);
  z-index: 36;
}

.rect-handle-nw { left: 0; top: 0; cursor: nwse-resize; }
.rect-handle-ne { left: 100%; top: 0; cursor: nesw-resize; }
.rect-handle-sw { left: 0; top: 100%; cursor: nesw-resize; }
.rect-handle-se { left: 100%; top: 100%; cursor: nwse-resize; }

/* ---------- landscape/desktop panel divider (hidden on mobile) ---------- */

.panel-divider {
  display: none;
}

/* ---------- side-panel collapse (header button + edge tab) ---------- */
/* Both hidden outside the landscape/side-panel layout (see the landscape
   media query below, which is the only place that flips them visible) —
   the portrait bottom sheet has its own swipe-to-close and never uses
   these. */

.panel-header {
  display: none;
}

.panel-collapse-btn {
  font-size: 18px;
  line-height: 1;
}

.panel-edge-tab {
  display: none;
  position: fixed;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  width: 28px;
  height: 72px;
  border: none;
  border-radius: 12px 0 0 12px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 18px;
  z-index: 25;
  box-shadow: -2px 0 10px rgba(0,0,0,0.3);
  align-items: center;
  justify-content: center;
}

.panel-edge-tab .badge-dot {
  display: none;
  position: absolute;
  top: 8px;
  right: 6px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--danger);
  border: 1px solid var(--accent-contrast, #fff);
}

.panel-edge-tab.has-badge .badge-dot {
  display: block;
}

/* ---------- fab ---------- */

.fab {
  position: fixed;
  right: calc(16px + var(--safe-right));
  bottom: calc(16px + var(--safe-bottom));
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 22px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
  z-index: 30;
}

/* ---------- onboarding hint ---------- */

.onboard-hint {
  position: fixed;
  left: 50%;
  bottom: calc(84px + var(--safe-bottom));
  transform: translateX(-50%);
  background: var(--text);
  color: var(--bg);
  padding: 8px 14px;
  border-radius: 20px;
  font-size: 13px;
  z-index: 30;
  opacity: 0.92;
}

/* ---------- bottom sheet ---------- */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.3);
  z-index: 45;
  opacity: 0;
  transition: opacity 0.2s ease;
}

.sheet-backdrop.hidden { display: none; }

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 50;
  background: var(--surface);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.3);
  display: flex;
  flex-direction: column;
  height: 30vh;
  transition: height 0.25s ease, transform 0.25s ease;
  touch-action: none;
  padding-bottom: var(--safe-bottom);
}

.sheet[data-snap="quarter"] { height: 30vh; }
.sheet[data-snap="half"] { height: 55vh; }
.sheet[data-snap="full"] { height: 92vh; }

.sheet.sheet-hidden {
  transform: translateY(110%);
}

/* Suppresses the height transition while the handle is actively being
 * dragged — see the comment in Sheet._wireDrag (sheet.js) for why a live
 * transition makes a slow drag's final height unreliable. */
.sheet.sheet-dragging {
  transition: none;
}

.sheet-handle-area {
  position: relative;
  padding: 10px 0 6px;
  display: flex;
  justify-content: center;
  touch-action: none;
  cursor: grab;
}

.sheet-handle {
  width: 40px;
  height: 4px;
  border-radius: 3px;
  background: var(--border);
}

.sheet-usage {
  position: absolute;
  right: 12px;
  top: 10px;
  font-size: 11px;
  color: var(--text-dim);
}

.msg.system-note {
  align-self: center;
  background: none;
  border: 1px solid var(--danger);
  color: var(--danger);
  font-size: 13px;
  text-align: center;
}

.msg-partial-note {
  margin-top: 6px;
  font-size: 12px;
  opacity: 0.7;
}

.sw-update-toast {
  position: fixed;
  left: 50%;
  bottom: calc(16px + var(--safe-bottom));
  transform: translateX(-50%);
  z-index: 100;
  background: var(--text);
  color: var(--bg);
  border: none;
  border-radius: 20px;
  padding: 10px 18px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: 0 4px 14px rgba(0,0,0,0.3);
}

.msg-partial-continue {
  background: none;
  border: none;
  color: var(--accent);
  font-size: 12px;
  padding: 0;
  text-decoration: underline;
}

/* "Повторить" under a failed reply (watchdog timeout or a hard error) —
   re-sends the same user message (see sheet.js _resendMessage). */
.msg-retry-btn {
  display: block;
  margin-top: 6px;
  background: none;
  border: none;
  color: var(--accent);
  font-size: 12px;
  padding: 0;
  text-decoration: underline;
  cursor: pointer;
}

/* Quarter snap only has ~30vh to work with; the auto-"Объяснить" user
 * bubble is normally a 96px thumbnail with no text (see _paintUserMessage in
 * sheet.js) — full-size, it alone can eat the entire quarter sheet, leaving
 * no room for the assistant's answer underneath it (spec: "четверть —
 * первые строки ответа, страница почти вся видна"). Shrink it down here so
 * several lines of the reply stay visible instead. */
.sheet[data-snap="quarter"] .msg-thumb {
  width: 28px;
  height: 28px;
}

.sheet-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 4px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  max-width: 92%;
}

.msg-row.msg-row-user {
  align-self: flex-end;
  flex-direction: row-reverse;
}

.msg-row.msg-row-assistant {
  align-self: flex-start;
}

.msg {
  max-width: 100%;
  min-width: 0;
  padding: 10px 12px;
  border-radius: 14px;
  line-height: 1.45;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

/* A wide display formula (long derivation, matrix, aligned system) must
 * scroll horizontally within its own bubble instead of being clipped or
 * blowing out the bubble's width. min-width:0 above is required for this to
 * work at all: without it, a flex/grid ancestor lets the bubble grow to fit
 * its widest child instead of constraining it, and overflow-x:auto never
 * kicks in because there's nothing to overflow against. */
.msg .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  padding: 2px 0 6px;
  -webkit-overflow-scrolling: touch;
}

.msg.user {
  background: var(--bubble-user);
  color: var(--bubble-user-text);
  border-bottom-right-radius: 4px;
}

.msg.assistant {
  background: var(--bubble-assistant);
  color: var(--bubble-assistant-text);
  border-bottom-left-radius: 4px;
}

/* Auto "Объяснить" bubble: no typed text, just the crop thumbnail + a
   muted page caption (never the synthetic instruction sentence). */
.msg-thumb {
  display: block;
  width: 96px;
  height: 96px;
  object-fit: contain;
  object-position: center;
  background: var(--surface-2, var(--surface));
  border-radius: 8px;
  cursor: pointer;
}

.msg-caption {
  margin-top: 4px;
  font-size: 12px;
  opacity: 0.7;
}

.msg p { margin: 0 0 8px; }
.msg p:last-child { margin-bottom: 0; }
.msg pre {
  background: rgba(0,0,0,0.25);
  padding: 8px;
  border-radius: 8px;
  overflow-x: auto;
  font-size: 13px;
}
.msg code {
  background: rgba(0,0,0,0.15);
  padding: 1px 4px;
  border-radius: 4px;
  font-size: 0.9em;
}
.msg hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 10px 0;
}
.msg table {
  border-collapse: collapse;
  width: 100%;
  margin: 8px 0;
  font-size: 13px;
}
.msg th, .msg td {
  border: 1px solid var(--border);
  padding: 4px 8px;
  text-align: left;
}
.msg th {
  background: rgba(0,0,0,0.08);
}
.msg blockquote {
  margin: 8px 0;
  padding: 2px 10px;
  border-left: 3px solid var(--border);
  color: var(--text-dim);
}
.msg blockquote p {
  margin: 0;
}

/* ---------- per-message ⋯ menu (edit/delete/copy) ---------- */

.msg-menu {
  position: relative;
  flex: none;
  align-self: flex-end;
  margin-bottom: 4px;
}

.msg-menu-btn {
  width: 24px;
  height: 24px;
  border: none;
  background: transparent;
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  border-radius: 6px;
  cursor: pointer;
  opacity: 1;
}

.msg-menu-btn:hover {
  background: var(--bg);
}

/* Desktop with a real pointer: keep the menu button out of the way until
   the message (or the button itself) is hovered/focused. Touch devices
   have no hover, so the button stays visible there by default (above). */
@media (hover: hover) and (pointer: fine) {
  .msg-menu-btn {
    opacity: 0;
    transition: opacity 0.15s ease;
  }
  .msg-row:hover .msg-menu-btn,
  .msg-menu-btn:focus {
    opacity: 1;
  }
}

.msg-menu-dropdown {
  position: absolute;
  bottom: 100%;
  right: 0;
  margin-bottom: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.25);
  display: flex;
  flex-direction: column;
  min-width: 140px;
  z-index: 5;
  overflow: hidden;
}

.msg-row-assistant .msg-menu-dropdown {
  right: auto;
  left: 0;
}

.msg-menu-dropdown.hidden {
  display: none;
}

/* For the topmost message(s), opening upward (the default) has no room and
   gets clipped by .sheet-messages' overflow — flip it to open downward.
   Set on the dropdown at open time once its available space is measured
   (see sheet.js _buildMsgMenu). */
.msg-menu-dropdown.open-down {
  bottom: auto;
  top: 100%;
  margin-bottom: 0;
  margin-top: 4px;
}

/* Measured/applied at open time (see sheet.js _buildMsgMenu) so the
   dropdown never sticks out past .sheet-messages and forces a horizontal
   scrollbar — most relevant in the narrow side-panel layout. */
.msg-menu-dropdown.align-right {
  left: auto;
  right: 0;
}
.msg-menu-dropdown.align-left {
  left: 0;
  right: auto;
}

.msg-menu-item {
  border: none;
  background: none;
  text-align: left;
  padding: 9px 12px;
  font-size: 13px;
  color: var(--text);
  cursor: pointer;
}

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

.msg-menu-item.danger {
  color: var(--danger);
}

.msg-menu-confirm-label {
  padding: 6px 12px 2px;
  font-size: 12px;
  color: var(--text-dim);
}

/* ---------- edit-mode banner above the input ---------- */

.sheet-edit-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  font-size: 12px;
  background: var(--bubble-assistant);
  color: var(--text);
  border-top: 1px solid var(--border);
}

.sheet-edit-banner.hidden {
  display: none;
}

.sheet-edit-banner button {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  padding: 2px 6px;
}

/* ---------- attachment chip above the input (Спросить / rect "Спросить") ---------- */

.sheet-attachment-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  border-top: 1px solid var(--border);
  background: var(--bubble-assistant);
}

.sheet-attachment-chip.hidden {
  display: none;
}

.sheet-attachment-chip img {
  width: 32px;
  height: 32px;
  object-fit: cover;
  border-radius: 6px;
  background: var(--surface);
  flex: none;
}

.sheet-attachment-chip-page {
  font-size: 12px;
  color: var(--text-dim);
  flex: 1;
}

.sheet-attachment-chip button {
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 14px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
  flex: none;
}

.sheet-input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 8px 12px 12px;
  border-top: 1px solid var(--border);
}

.sheet-input-row textarea {
  flex: 1;
  resize: none;
  max-height: 120px;
  box-sizing: border-box;
  overflow-y: hidden;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  font-size: 15px;
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.btn-send, .btn-stop {
  border: none;
  border-radius: 50%;
  width: 40px;
  height: 40px;
  flex: none;
  background: var(--accent);
  color: var(--accent-contrast);
  font-size: 16px;
}

.btn-stop {
  background: var(--danger);
  border-radius: 10px;
  width: auto;
  padding: 0 12px;
}

/* ---------- jump-back / jump-forward pills ---------- */

.jump-pills {
  position: fixed;
  left: calc(12px + var(--safe-left));
  bottom: calc(16px + var(--safe-bottom));
  z-index: 25;
  display: flex;
  gap: 6px;
  pointer-events: none;
}

.jump-pills.hidden {
  display: none;
}

.jump-pill {
  pointer-events: auto;
  border: none;
  background: var(--text);
  color: var(--bg);
  opacity: 0.92;
  font-size: 13px;
  padding: 8px 14px;
  border-radius: 20px;
  box-shadow: 0 2px 10px rgba(0,0,0,0.3);
  white-space: nowrap;
}

/* full-screen sheet covers the reading area entirely, so the pills (which
   jump around *in* that area) have nothing useful to point at. #jump-pills
   is a sibling of #sheet right after it in the DOM, so this is a plain CSS
   rule with no JS needed. */
#sheet[data-snap="full"]:not(.sheet-hidden) ~ #jump-pills {
  display: none !important;
}

/* quarter/half sheet still leaves the pills' usual corner uncovered on
   paper, but the sheet's z-index (50) sits above the pills' (25), so a tap
   on the pill silently hit the sheet instead. Lift the pills above the
   sheet and above its own height so they're always visible and tappable. */
#sheet[data-snap="quarter"]:not(.sheet-hidden) ~ #jump-pills {
  z-index: 55;
  bottom: calc(30vh + 16px + var(--safe-bottom));
}
#sheet[data-snap="half"]:not(.sheet-hidden) ~ #jump-pills {
  z-index: 55;
  bottom: calc(55vh + 16px + var(--safe-bottom));
}

/* ---------- outline (☰) screen ---------- */

.outline-list {
  padding: 8px 4px calc(24px + var(--safe-bottom));
}

.outline-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  font-size: 15px;
  color: var(--text);
}

.outline-item:active {
  background: var(--bubble-assistant);
}

/* ---------- landscape / tablet: side panel instead of sheet ---------- */
/* Also covers phone landscape (e.g. 851x393): the spec calls for
   book-left/chat-right there too, not the bottom sheet — min-width just
   needs to be low enough to include a phone turned sideways, not just
   tablets/desktop. */

@media (orientation: landscape) and (min-width: 640px) {
  .app {
    flex-direction: row;
  }
  .app .topbar {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
  }
  .viewer-container {
    flex: 1 1 auto;
    min-width: 0;
    margin-top: 48px;
  }
  /* The panel is a persistent part of the layout here, so the FAB (whose
     only job is discoverability/opening the sheet on mobile) has nothing
     to do — the side panel is always visible instead. */
  .fab {
    display: none;
  }
  .sheet-backdrop {
    display: none !important;
  }
  .sheet {
    position: static;
    height: auto;
    width: var(--panel-width, 420px);
    flex: none;
    border-radius: 0;
    border-left: 1px solid var(--border);
    box-shadow: none;
    margin-top: 48px;
    transition: none;
  }
  /* Unlike mobile, the panel never collapses to width:0 — it's always
     visible with an empty state until there's an attachment or a typed
     question. */
  .sheet.sheet-hidden {
    transform: none;
    width: var(--panel-width, 420px);
    border-left: 1px solid var(--border);
    overflow: visible;
  }
  .panel-divider {
    display: block;
    width: 6px;
    flex: none;
    align-self: stretch;
    margin-top: 48px;
    cursor: col-resize;
    background: var(--border);
    touch-action: none;
  }
  .panel-divider:hover,
  .panel-divider:active {
    background: var(--accent);
  }
  .sheet[data-snap] {
    height: calc(100vh - 48px);
  }
  .sheet:not([data-snap]) {
    height: calc(100vh - 48px);
  }
  .sheet-handle-area {
    display: none;
  }
  .panel-header {
    display: flex;
    justify-content: flex-end;
    padding: 6px 8px 0;
  }
  .panel-collapse-btn {
    width: 28px;
    height: 28px;
  }
  /* Programmatic collapse/expand (button, edge tab, drag-past-threshold)
     animates width over 0.2s; a live divider drag deliberately does NOT
     (see .panel-divider's pointermove handler in app.js) — a transition
     there would make every drag step visibly lag behind the pointer. */
  .sheet.panel-anim {
    transition: width 0.2s ease;
  }
  .sheet.panel-collapsed,
  .sheet.sheet-hidden.panel-collapsed {
    width: 0;
    min-width: 0;
    border-left: none;
    overflow: hidden;
  }
  .app.panel-collapsed .panel-edge-tab {
    display: flex;
  }
  /* Phone landscape (e.g. 851x393) has much less vertical room than a
     tablet/desktop in the same orientation — shrink the top bar so it
     doesn't eat a disproportionate slice of the screen. */
  @media (max-height: 500px) {
    .app .topbar {
      height: 36px;
      min-height: 36px;
      padding-top: var(--safe-top);
    }
    .app .topbar .icon-btn {
      width: 30px;
      height: 30px;
      font-size: 16px;
    }
    .viewer-container {
      margin-top: 36px;
    }
    .sheet {
      margin-top: 36px;
    }
    .panel-divider {
      margin-top: 36px;
    }
    .sheet[data-snap],
    .sheet:not([data-snap]) {
      height: calc(100vh - 36px);
    }
  }
  .sheet-messages:empty::before {
    content: 'Выделите непонятное место или задайте вопрос';
    display: block;
    margin: auto 0;
    padding: 24px 12px;
    color: var(--text-secondary, #888);
    font-size: 14px;
    text-align: center;
  }
  .sheet-messages:empty {
    justify-content: center;
  }
}
