/* ============================================================
   Noveii PDF Editor — styles
   ============================================================ */
:root {
  --bg: #eef0f6;
  --bg-soft: #f6f7fb;
  --panel: #ffffff;
  --panel-2: #fbfbfe;
  --ink: #1d2033;
  --ink-soft: #565b75;
  --muted: #9499b1;
  --border: #e7e9f3;
  --border-2: #eef0f7;
  --accent: #4f46e5;
  --accent-2: #7c3aed;
  --accent-soft: #eef0ff;
  --danger: #ef4565;
  --shadow-sm: 0 1px 2px rgba(25, 28, 50, .06), 0 2px 6px rgba(25, 28, 50, .05);
  --shadow-md: 0 6px 22px rgba(25, 28, 50, .10);
  --shadow-lg: 0 18px 50px rgba(25, 28, 50, .22);
  --radius: 12px;
  --rail: 60px;
  --pages: 188px;
  --top: 58px;
  --prop: 50px;
}
[data-theme="dark"] {
  --bg: #0f1119;
  --bg-soft: #151826;
  --panel: #1a1d2b;
  --panel-2: #202434;
  --ink: #eef0fb;
  --ink-soft: #b9bedb;
  --muted: #8085a8;
  --border: #2a2f44;
  --border-2: #232840;
  --accent: #818cf8;
  --accent-2: #a78bfa;
  --accent-soft: #23243f;
  --shadow-sm: 0 1px 2px rgba(0,0,0,.4);
  --shadow-md: 0 8px 26px rgba(0,0,0,.5);
  --shadow-lg: 0 20px 60px rgba(0,0,0,.6);
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Inter', system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
.ic { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; display: block; }

/* ---------- Top bar ---------- */
.topbar {
  height: var(--top);
  display: flex; align-items: center; gap: 16px;
  padding: 0 16px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  position: relative; z-index: 30;
}
.back-link {
  display: grid; place-items: center;
  width: 34px; height: 34px; border-radius: 9px;
  color: var(--muted); border: 1px solid var(--border);
  background: var(--bg-soft); flex: none;
  transition: color .15s, border-color .15s, background .15s;
}
.back-link:hover { color: var(--accent); border-color: var(--accent); background: var(--panel-2); }
.back-link .ic { width: 18px; height: 18px; }
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center; color: #fff;
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  box-shadow: 0 4px 12px rgba(79,70,229,.4);
}
.brand-name {
  font-family: 'Space Grotesk', 'Inter', system-ui, sans-serif;
  font-weight: 600; font-size: 16px; letter-spacing: -.02em;
}
.brand-name i { color: var(--accent); font-style: normal; }
.brand-name b { color: var(--ink-soft); font-weight: 600; }

.doc-title { flex: 1; display: flex; justify-content: center; }
.doc-title input {
  border: 1px solid transparent; background: transparent;
  font: 600 14px 'Inter'; color: var(--ink); text-align: center;
  padding: 7px 14px; border-radius: 9px; width: min(360px, 40vw);
  transition: .15s;
}
.doc-title input:hover:not(:disabled) { background: var(--bg-soft); }
.doc-title input:focus { outline: none; background: var(--panel); border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.doc-title input:disabled { color: var(--muted); }

.top-actions { display: flex; align-items: center; gap: 8px; }

.btn {
  display: inline-flex; align-items: center; gap: 7px;
  height: 36px; padding: 0 14px; border-radius: 10px;
  border: 1px solid var(--border); background: var(--panel);
  color: var(--ink-soft); font: 600 13px 'Inter'; cursor: pointer;
  transition: .15s; white-space: nowrap;
}
.btn .ic { width: 16px; height: 16px; }
.btn:hover { border-color: var(--accent); color: var(--accent); background: var(--panel-2); }
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn:disabled:hover { border-color: var(--border); color: var(--ink-soft); background: var(--panel); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--bg-soft); border-color: transparent; color: var(--accent); }
.btn.primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; border-color: transparent; box-shadow: 0 4px 14px rgba(109,94,252,.35);
}
.btn.primary:hover { filter: brightness(1.05); color: #fff; }
.btn.primary:disabled { box-shadow: none; }
.btn.lg { height: 44px; padding: 0 24px; font-size: 14px; }

.icon-btn {
  width: 36px; height: 36px; border-radius: 9px; display: grid; place-items: center;
  border: 1px solid transparent; background: transparent; color: var(--ink-soft); cursor: pointer; transition: .15s;
}
.icon-btn:hover { background: var(--bg-soft); color: var(--accent); }
.icon-btn:active { transform: scale(.94); }

/* ---------- Property bar ---------- */
.propbar {
  height: var(--prop);
  display: flex; align-items: center; gap: 14px;
  padding: 0 14px; background: var(--panel-2);
  border-bottom: 1px solid var(--border); position: relative; z-index: 20;
  overflow-x: auto;
}
.propbar::-webkit-scrollbar { height: 0; }
.prop-group { display: flex; align-items: center; gap: 9px; flex-shrink: 0; }
.prop-group.tight { gap: 2px; }
.prop-name { font: 600 11px 'Inter'; color: var(--muted); text-transform: uppercase; letter-spacing: .5px; }
.prop-val { font: 600 12px 'Inter'; color: var(--ink-soft); min-width: 34px; }
.divider { width: 1px; height: 24px; background: var(--border); flex-shrink: 0; }
.spacer { flex: 1; min-width: 8px; }

.swatch-label { position: relative; width: 30px; height: 30px; cursor: pointer; display: block; }
.swatch-label input[type=color] { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.swatch { position: absolute; inset: 0; border-radius: 8px; border: 2px solid var(--panel); box-shadow: 0 0 0 1px var(--border), var(--shadow-sm); background: #e23b3b; }

.palette { display: flex; gap: 5px; }
.palette .chip { width: 18px; height: 18px; border-radius: 5px; cursor: pointer; border: 2px solid transparent; box-shadow: 0 0 0 1px rgba(0,0,0,.08) inset; transition: .12s; }
.palette .chip:hover { transform: scale(1.18); }
.palette .chip.active { border-color: var(--panel); box-shadow: 0 0 0 2px var(--accent); }

input[type=range] { -webkit-appearance: none; appearance: none; height: 5px; width: 96px; border-radius: 4px; background: var(--border); cursor: pointer; }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 15px; height: 15px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); box-shadow: var(--shadow-sm); }
input[type=range]::-moz-range-thumb { width: 13px; height: 13px; border-radius: 50%; background: var(--accent); border: 2px solid var(--panel); }

#propFont { width: 56px; height: 30px; border: 1px solid var(--border); border-radius: 8px; background: var(--panel); color: var(--ink); font: 600 13px 'Inter'; text-align: center; }
#propFont:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }

.zoom-label, .page-label { font: 600 12px 'Inter'; color: var(--ink-soft); min-width: 44px; text-align: center; }
.page-label b { color: var(--accent); }

/* ---------- Workspace ---------- */
.workspace { display: flex; height: calc(100vh - var(--top) - var(--prop)); }

.toolrail {
  width: var(--rail); flex-shrink: 0; background: var(--panel);
  border-right: 1px solid var(--border); padding: 10px 0;
  display: flex; flex-direction: column; align-items: center; gap: 4px; overflow-y: auto;
}
.tool-btn {
  width: 42px; height: 42px; border-radius: 11px; display: grid; place-items: center;
  border: 1px solid transparent; background: transparent; color: var(--ink-soft); cursor: pointer; transition: .15s; position: relative;
}
.tool-btn .ic { width: 20px; height: 20px; }
.tool-btn:hover { background: var(--bg-soft); color: var(--accent); }
.tool-btn.active { background: var(--accent-soft); color: var(--accent); }
.tool-btn.active::before { content: ''; position: absolute; left: -10px; top: 50%; transform: translateY(-50%); width: 3px; height: 22px; border-radius: 3px; background: var(--accent); }
.rail-sep { width: 30px; height: 1px; background: var(--border); margin: 6px 0; flex-shrink: 0; }
#detectBtn.on { background: var(--accent-soft); color: var(--accent); }
.tool-btn[data-count]:not([data-count=""])::after {
  content: attr(data-count); position: absolute; top: 2px; right: 2px;
  min-width: 16px; height: 16px; padding: 0 4px; border-radius: 9px;
  background: var(--accent); color: #fff; font: 700 9px 'Inter';
  display: grid; place-items: center; box-shadow: var(--shadow-sm);
}

/* Detected form-field markers (HTML overlay, never exported) */
.field-layer { position: absolute; inset: 0; z-index: 6; pointer-events: none; }
.field-marker {
  position: absolute; pointer-events: auto; cursor: pointer;
  border: 1.6px dashed var(--accent); background: rgba(109, 94, 252, .10);
  border-radius: 5px; transition: background .12s, opacity .15s;
}
.field-marker:hover { background: rgba(109, 94, 252, .22); }
.field-marker .fm-label {
  position: absolute; top: -10px; left: 6px;
  font: 700 9px 'Inter'; letter-spacing: .3px;
  background: var(--accent); color: #fff; padding: 1px 7px; border-radius: 20px;
  white-space: nowrap; box-shadow: var(--shadow-sm); opacity: 0; transition: opacity .12s;
}
.field-marker:hover .fm-label { opacity: 1; }
.fm-check { border-color: #16a34a; background: rgba(22, 163, 74, .12); }
.fm-check:hover { background: rgba(22, 163, 74, .24); }
.fm-check .fm-label { background: #16a34a; }
.fm-sign { border-color: #e2a90b; background: rgba(226, 169, 11, .12); }
.fm-sign:hover { background: rgba(226, 169, 11, .26); }
.fm-sign .fm-label { background: #e2a90b; }
.field-marker.suggested { border-style: dotted; }
.field-marker.done { opacity: 0; pointer-events: none; }
.field-marker { cursor: move; touch-action: none; }
.field-marker.dragging { background: rgba(109, 94, 252, .28); }
.fm-handle {
  position: absolute; right: -5px; bottom: -5px; width: 12px; height: 12px;
  border-radius: 3px; background: var(--accent); border: 2px solid #fff;
  box-shadow: var(--shadow-sm); cursor: nwse-resize; pointer-events: auto;
  opacity: 0; transition: opacity .12s;
}
.field-marker:hover .fm-handle { opacity: 1; }
.fm-check .fm-handle { background: #16a34a; }
.fm-sign .fm-handle { background: #e2a90b; }

/* ---------- Pages panel ---------- */
.pages-panel {
  width: var(--pages); flex-shrink: 0; background: var(--panel);
  border-right: 1px solid var(--border); display: flex; flex-direction: column;
}
.panel-head { padding: 12px 16px 8px; font: 700 11px 'Inter'; letter-spacing: 1px; text-transform: uppercase; color: var(--muted); }
.thumbs { flex: 1; overflow-y: auto; padding: 4px 12px 12px; display: flex; flex-direction: column; gap: 10px; }
.thumb {
  position: relative; border-radius: 9px; cursor: pointer; background: var(--panel);
  border: 2px solid var(--border-2); overflow: hidden; transition: .15s; box-shadow: var(--shadow-sm);
}
.thumb:hover { border-color: var(--accent-2); }
.thumb.active { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.thumb canvas { display: block; width: 100%; height: auto; }
.thumb .num { position: absolute; bottom: 5px; right: 6px; background: rgba(20,22,40,.78); color: #fff; font: 600 10px 'Inter'; padding: 2px 7px; border-radius: 20px; }
.thumb.dragging { opacity: .4; }
.thumb.drop-target { border-color: var(--accent); border-style: dashed; }

.page-ops { display: flex; gap: 4px; padding: 10px 12px; border-top: 1px solid var(--border); justify-content: space-around; }
.op-btn { flex: 1; height: 34px; border-radius: 8px; border: 1px solid var(--border); background: var(--panel); color: var(--ink-soft); display: grid; place-items: center; cursor: pointer; transition: .15s; }
.op-btn:hover { border-color: var(--accent); color: var(--accent); background: var(--panel-2); }
.op-btn.danger:hover { border-color: var(--danger); color: var(--danger); }

/* ---------- Viewer ---------- */
.viewer { flex: 1; position: relative; background:
  radial-gradient(circle at 20% 20%, rgba(109,94,252,.05), transparent 40%),
  radial-gradient(circle at 80% 80%, rgba(139,123,255,.05), transparent 40%),
  var(--bg); overflow: hidden; }
.canvas-scroll { position: absolute; inset: 0; overflow: auto; }
#pagesContainer { display: flex; flex-direction: column; align-items: center; gap: 26px; padding: 30px 24px 80px; min-height: 100%; }
.page-stage { position: relative; background: #fff; box-shadow: var(--shadow-lg); border-radius: 3px; flex-shrink: 0; }
.page-stage .pdf-bg { position: absolute; inset: 0; width: 100%; height: 100%; border-radius: 3px; }
.page-stage .canvas-container { position: absolute !important; inset: 0; width: 100% !important; height: 100% !important; }
.page-stage .canvas-container canvas { width: 100% !important; height: 100% !important; }
.page-badge { position: absolute; top: -11px; left: 10px; background: var(--ink); color: #fff; font: 700 10px 'Inter'; padding: 2px 9px; border-radius: 20px; box-shadow: var(--shadow-sm); pointer-events: none; z-index: 7; opacity: .55; }

/* Empty state */
.empty-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; }
.empty-card {
  width: min(520px, 92%); background: var(--panel); border: 2px dashed var(--border);
  border-radius: 22px; padding: 48px 36px; text-align: center; transition: .2s; box-shadow: var(--shadow-md); cursor: pointer;
}
.empty-card.drag { border-color: var(--accent); background: var(--accent-soft); transform: scale(1.01); }
.empty-icon { width: 88px; height: 88px; border-radius: 24px; margin: 0 auto 22px; display: grid; place-items: center; color: var(--accent); background: var(--accent-soft); }
.empty-card h2 { margin: 0 0 8px; font-size: 22px; font-weight: 700; }
.empty-card p { margin: 0 0 22px; color: var(--ink-soft); font-size: 14px; }
.empty-hint { margin-top: 20px; font-size: 12px; color: var(--muted); }

/* ---------- Modals ---------- */
.modal-backdrop { position: fixed; inset: 0; background: rgba(15,17,28,.5); backdrop-filter: blur(4px); display: none; place-items: center; z-index: 100; padding: 24px; }
.modal-backdrop.open { display: grid; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.modal { background: var(--panel); border-radius: 18px; box-shadow: var(--shadow-lg); width: min(640px, 100%); overflow: hidden; animation: pop .22s cubic-bezier(.2,.8,.3,1.2); }
.modal.wide { width: min(860px, 100%); }
@keyframes pop { from { transform: scale(.94) translateY(8px); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 18px 22px; border-bottom: 1px solid var(--border); }
.modal-head h3 { margin: 0; font-size: 17px; font-weight: 700; }
.modal-foot { display: flex; align-items: center; gap: 10px; padding: 16px 22px; border-top: 1px solid var(--border); }
.modal-foot .grow { flex: 1; }

.sig-wrap { padding: 22px; }
#sigCanvas { width: 100%; height: 220px; border: 2px dashed var(--border); border-radius: 12px; background: var(--bg-soft); touch-action: none; }
.sig-colors, .palette.sig { display: flex; gap: 8px; }
.sig-colors .chip { width: 24px; height: 24px; border-radius: 7px; cursor: pointer; border: 2px solid var(--panel); box-shadow: 0 0 0 1px var(--border); }
.sig-colors .chip.active { box-shadow: 0 0 0 2px var(--accent); }

.lib-grid { padding: 22px; display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 16px; max-height: 70vh; overflow-y: auto; }
.lib-card { border: 1px solid var(--border); border-radius: 14px; overflow: hidden; background: var(--panel-2); transition: .15s; }
.lib-card:hover { border-color: var(--accent); box-shadow: var(--shadow-md); transform: translateY(-2px); }
.lib-thumb { width: 100%; aspect-ratio: 3/4; background: var(--bg-soft); display: grid; place-items: center; cursor: pointer; overflow: hidden; }
.lib-thumb img { width: 100%; height: 100%; object-fit: cover; }
.lib-thumb .ph { color: var(--muted); }
.lib-meta { padding: 11px 13px; }
.lib-meta .name { font: 600 13px 'Inter'; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-meta .date { font: 500 11px 'Inter'; color: var(--muted); margin-top: 3px; }
.lib-actions { display: flex; gap: 6px; padding: 0 13px 13px; }
.lib-actions .btn { height: 32px; flex: 1; justify-content: center; padding: 0 8px; font-size: 12px; }
.lib-empty { grid-column: 1/-1; text-align: center; color: var(--muted); padding: 40px 0; font-size: 14px; }

/* ---------- Loading + toast ---------- */
.loading { position: fixed; inset: 0; background: rgba(238,240,246,.7); backdrop-filter: blur(3px); display: none; place-items: center; z-index: 200; gap: 14px; flex-direction: column; color: var(--ink-soft); font-weight: 600; }
[data-theme="dark"] .loading { background: rgba(15,17,25,.72); }
.loading.open { display: grid; }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 4px solid var(--border); border-top-color: var(--accent); animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff; padding: 12px 22px; border-radius: 12px; font: 600 13px 'Inter'; box-shadow: var(--shadow-lg); opacity: 0; pointer-events: none; transition: .25s; z-index: 300; display: flex; align-items: center; justify-content: center; gap: 9px; max-width: min(440px, calc(100vw - 32px)); text-align: center; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.success { background: linear-gradient(135deg, #16a34a, #22c55e); }
.toast.error { background: linear-gradient(135deg, #dc2626, var(--danger)); }

/* ---------- Detection progress bar ---------- */
.progress {
  position: fixed; left: 50%; top: calc(var(--top) + var(--prop) + 14px);
  transform: translateX(-50%) translateY(-12px);
  display: flex; align-items: center; gap: 12px;
  background: var(--panel); border: 1px solid var(--border);
  box-shadow: var(--shadow-lg); border-radius: 14px; padding: 11px 16px;
  width: min(420px, 90vw); z-index: 250; opacity: 0; pointer-events: none; transition: .25s;
}
.progress.open { opacity: 1; transform: translateX(-50%) translateY(0); }
.progress-spark { width: 30px; height: 30px; flex-shrink: 0; border-radius: 9px; display: grid; place-items: center; color: #fff; background: linear-gradient(135deg, var(--accent), var(--accent-2)); }
.progress-spark .ic { width: 17px; height: 17px; }
.progress-body { flex: 1; }
.progress-text { font: 600 12px 'Inter'; color: var(--ink); margin-bottom: 7px; }
.progress-track { height: 6px; border-radius: 4px; background: var(--border); overflow: hidden; }
.progress-fill { height: 100%; width: 0; border-radius: 4px; background: linear-gradient(90deg, var(--accent), var(--accent-2)); transition: width .35s ease; }

/* ---------- Modal body / inputs (API key) ---------- */
.modal-body { padding: 20px 22px 8px; }
.muted-text { margin: 0 0 14px; font-size: 13px; color: var(--ink-soft); line-height: 1.55; }
.muted-text.small { font-size: 12px; color: var(--muted); margin-top: 14px; margin-bottom: 4px; }
.muted-text .link { color: var(--accent); font-weight: 600; }
.text-input { width: 100%; height: 44px; border: 1px solid var(--border); border-radius: 11px; background: var(--bg-soft); color: var(--ink); font: 500 14px 'Inter'; padding: 0 14px; transition: .15s; }
.text-input:focus { outline: none; border-color: var(--accent); background: var(--panel); box-shadow: 0 0 0 3px var(--accent-soft); }
.text-area { width: 100%; min-height: 150px; resize: vertical; border: 1px solid var(--border); border-radius: 12px; background: var(--bg-soft); color: var(--ink); font: 500 13.5px/1.6 'Inter'; padding: 12px 14px; transition: .15s; }
.text-area:focus { outline: none; border-color: var(--accent); background: var(--panel); box-shadow: 0 0 0 3px var(--accent-soft); }
.range-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.range-row label { font: 600 13px 'Inter'; color: var(--ink-soft); }
.small-input { width: 90px; height: 40px; }

/* ---------- Find bar ---------- */
.findbar {
  position: fixed; right: 22px; top: calc(var(--top) + var(--prop) + 12px);
  display: none; align-items: center; gap: 6px;
  background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow-lg);
  border-radius: 12px; padding: 7px 10px; z-index: 240; width: min(380px, 92vw);
}
.findbar.open { display: flex; }
.findbar .find-ic { width: 16px; height: 16px; color: var(--muted); flex-shrink: 0; }
.findbar input { flex: 1; border: none; background: transparent; color: var(--ink); font: 500 13px 'Inter'; padding: 6px 4px; }
.findbar input:focus { outline: none; }
.find-count { font: 600 11px 'Inter'; color: var(--muted); min-width: 50px; text-align: right; }
.find-hl {
  position: absolute; z-index: 7; pointer-events: none;
  background: rgba(245, 185, 21, .45); border: 1.5px solid #f5b915; border-radius: 3px;
  box-shadow: 0 0 0 3px rgba(245, 185, 21, .22); animation: findpulse 1s ease;
}
@keyframes findpulse { 0% { box-shadow: 0 0 0 8px rgba(245, 185, 21, .4); } 100% { box-shadow: 0 0 0 3px rgba(245, 185, 21, .22); } }

/* ---------- Dropdown menu (export) ---------- */
.menu {
  position: fixed; display: none; flex-direction: column; min-width: 232px;
  background: var(--panel); border: 1px solid var(--border); box-shadow: var(--shadow-lg);
  border-radius: 12px; padding: 6px; z-index: 260;
}
.menu.open { display: flex; animation: pop .16s ease; }
.menu button {
  display: flex; align-items: center; gap: 10px; width: 100%;
  border: none; background: transparent; color: var(--ink); cursor: pointer;
  font: 600 13px 'Inter'; padding: 10px 11px; border-radius: 8px; text-align: left;
}
.menu button:hover { background: var(--bg-soft); color: var(--accent); }
.menu button .ic { width: 17px; height: 17px; color: var(--ink-soft); }
.menu button:hover .ic { color: var(--accent); }

/* Scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 8px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--muted); }
.thumbs::-webkit-scrollbar { width: 7px; }

/* Mobile-only chrome (hidden on desktop) */
.pages-toggle { display: none; }
.drawer-backdrop { display: none; }

/* ============ Responsive: tablet & down ============ */
@media (max-width: 880px) {
  .doc-title { display: none; }
  .top-actions .btn span { display: none; }
  .pages-toggle { display: grid; }

  /* Pages panel becomes a slide-in drawer */
  .pages-panel {
    position: fixed; top: var(--top); bottom: 0; left: 0;
    width: 264px; max-width: 82vw; z-index: 45;
    transform: translateX(-102%);
    transition: transform .26s cubic-bezier(.2, .8, .3, 1);
    box-shadow: var(--shadow-lg);
  }
  .pages-panel.open { transform: none; }
  .drawer-backdrop {
    display: block; position: fixed; top: var(--top); left: 0; right: 0; bottom: 0;
    background: rgba(15, 17, 28, .42); backdrop-filter: blur(2px);
    z-index: 44; opacity: 0; pointer-events: none; transition: opacity .26s;
  }
  .drawer-backdrop.open { opacity: 1; pointer-events: auto; }
}

/* ============ Responsive: phone ============ */
@media (max-width: 680px) {
  :root { --top: 52px; --prop: 46px; --toolbar: 58px; }

  /* Compact top bar — keep everything reachable, never clip */
  .topbar { gap: 6px; padding: 0 8px; }
  .brand-name { display: none; }
  .top-actions { gap: 2px; flex: 1; min-width: 0; justify-content: flex-end; overflow-x: auto; scrollbar-width: none; }
  .top-actions::-webkit-scrollbar { display: none; }
  .top-actions .btn { padding: 0 9px; flex: 0 0 auto; }
  .top-actions .icon-btn { flex: 0 0 auto; }

  /* Leave room for the bottom toolbar; use dynamic viewport height */
  .workspace {
    height: calc(100vh - var(--top) - var(--prop) - var(--toolbar));
    height: calc(100dvh - var(--top) - var(--prop) - var(--toolbar));
  }

  /* Tool rail -> horizontal scrollable bottom toolbar */
  .toolrail {
    position: fixed; left: 0; right: 0; bottom: 0;
    width: 100%; height: var(--toolbar);
    flex-direction: row; align-items: center; justify-content: flex-start;
    gap: 2px; padding: 0 8px;
    border-right: none; border-top: 1px solid var(--border);
    overflow-x: auto; overflow-y: hidden;
    -webkit-overflow-scrolling: touch; scrollbar-width: none;
    z-index: 35;
  }
  .toolrail::-webkit-scrollbar { display: none; }
  /* Right-edge fade — hints that more tools live off-screen */
  .workspace::after {
    content: ''; position: fixed; right: 0; bottom: 0;
    width: 40px; height: var(--toolbar);
    background: linear-gradient(to right, rgba(255, 255, 255, 0), var(--panel) 78%);
    pointer-events: none; z-index: 36;
  }
  .tool-btn { flex: 0 0 auto; width: 46px; height: 46px; }
  .tool-btn .ic { width: 22px; height: 22px; }
  .tool-btn.active::before { display: none; }
  .rail-sep { width: 1px; height: 30px; margin: 0 6px; flex: 0 0 auto; }

  /* Property bar: momentum scrolling for the contextual controls */
  .propbar { gap: 12px; padding: 0 12px; -webkit-overflow-scrolling: touch; }

  /* Document area: tighter padding so pages fill the screen width */
  #pagesContainer { gap: 18px; padding: 16px 10px 28px; }

  /* Keep transient UI clear of the bottom toolbar */
  .toast { bottom: calc(var(--toolbar) + 16px); }

  /* Center floating overlays with clean, symmetric margins */
  .modal-backdrop { padding: 16px; }
  .findbar { left: 12px; right: 12px; width: auto; }

  .pages-panel { width: 240px; }
}
