/* ============================================================
   PDF Workspace — Digital Opus
   Hand-authored stylesheet (replaces the original Tailwind CDN build)
   ============================================================ */

:root {
  --bg:            #020617;
  --bg-2:          #0b1220;
  --panel:         rgba(15, 23, 42, 0.78);
  --panel-solid:   #0f172a;
  --card:          rgba(30, 41, 59, 0.55);
  --border:        #1e293b;
  --border-soft:   rgba(255, 255, 255, 0.07);
  --text:          #e2e8f0;
  --text-strong:   #f8fafc;
  --muted:         #94a3b8;
  --muted-dim:     #64748b;
  --accent:        #6366f1;
  --accent-2:      #4f46e5;
  --accent-soft:   rgba(99, 102, 241, 0.14);
  --good:          #10b981;
  --good-2:        #059669;
  --danger:        #ef4444;
  --amber:         #f59e0b;
  --radius:        14px;
  --radius-sm:     10px;
  --header-h:      56px;
  --tap:           44px;
  --shadow:        0 10px 30px -10px rgba(0, 0, 0, 0.7);
}

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

/* Author `display` rules otherwise beat the UA rule for [hidden]; several
   panels are toggled with el.hidden, so make the attribute authoritative. */
[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  height: 100vh;
  height: 100dvh;
}

body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.45;
  overflow: hidden;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  -webkit-text-size-adjust: 100%;
}

h1, h2, h3, h4 { margin: 0; font-weight: 600; color: var(--text-strong); }

code, kbd, .mono, .counter {
  font-family: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;
}

kbd {
  background: var(--panel-solid);
  border: 1px solid var(--border);
  border-bottom-width: 2px;
  border-radius: 5px;
  padding: 1px 5px;
  font-size: 11px;
  color: var(--text);
}

.visually-hidden {
  position: absolute !important;
  width: 1px; height: 1px;
  padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0);
  white-space: nowrap; border: 0;
}

/* ---------- scrollbars ---------- */
::-webkit-scrollbar { width: 9px; height: 9px; }
::-webkit-scrollbar-track { background: var(--bg); }
::-webkit-scrollbar-thumb { background: #334155; border-radius: 5px; }
::-webkit-scrollbar-thumb:hover { background: #475569; }

/* ============================================================
   Dependency failure banner
   ============================================================ */
.dep-fail {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  background: rgba(239, 68, 68, 0.14);
  border-bottom: 1px solid rgba(239, 68, 68, 0.45);
  color: #fecaca;
  padding: 12px 16px;
  font-size: 13px;
  flex-shrink: 0;
}
.dep-fail i { font-size: 18px; margin-top: 1px; }
.dep-fail strong { display: block; color: #fff; }
.dep-fail span { display: block; margin: 2px 0; }
.dep-fail em { display: block; opacity: 0.85; font-size: 12px; }

/* ============================================================
   Header
   ============================================================ */
.app-header {
  flex-shrink: 0;
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  background: rgba(15, 23, 42, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  text-decoration: none;
  color: var(--text-strong);
  min-height: 36px;
}
.brand-mark {
  width: 30px; height: 30px;
  border-radius: 9px;
  display: grid; place-items: center;
  font-weight: 800; font-size: 12px; letter-spacing: 0.5px;
  color: #fff;
  background: linear-gradient(135deg, var(--accent-2), var(--amber));
  box-shadow: 0 4px 14px -4px rgba(99, 102, 241, 0.7);
}
.brand-word {
  font-weight: 700;
  letter-spacing: 0.3px;
  background: linear-gradient(135deg, #38bdf8, #818cf8);
  -webkit-background-clip: text;
  background-clip: text;
  -webkit-text-fill-color: transparent;
}
.brand:hover .brand-word { filter: brightness(1.25); }

.brand-divider { width: 1px; height: 26px; background: var(--border); }

.title-block h1 { font-size: 16px; letter-spacing: 0.2px; }
.title-block h3 { font-size: 15px; }
.subtitle { margin: 0; font-size: 11.5px; color: var(--muted); }

.header-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.privacy-strip {
  flex-shrink: 0;
  padding: 6px 16px;
  font-size: 11.5px;
  color: var(--muted);
  text-align: center;
  background: rgba(16, 185, 129, 0.06);
  border-bottom: 1px solid rgba(16, 185, 129, 0.18);
}
.privacy-strip i { color: var(--good); margin-right: 5px; }

/* ============================================================
   Buttons / controls
   ============================================================ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  min-height: 38px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, border-color 0.15s, color 0.15s, transform 0.08s;
  white-space: nowrap;
  text-decoration: none;
}
.btn:active:not(:disabled) { transform: scale(0.97); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn:focus-visible, .icon-btn:focus-visible, .nav-tab:focus-visible,
.input:focus-visible, .dropzone:focus-visible, .chip:focus-visible,
.seg:focus-visible, .md-format-btn:focus-visible, .stamp-choice:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.btn-primary { background: var(--accent-2); color: #fff; box-shadow: 0 6px 18px -8px rgba(79, 70, 229, 0.9); }
.btn-primary:hover:not(:disabled) { background: var(--accent); }

.btn-success { background: var(--good-2); color: #fff; box-shadow: 0 6px 18px -8px rgba(5, 150, 105, 0.9); }
.btn-success:hover:not(:disabled) { background: var(--good); }
.btn-success:disabled { background: #1e293b; color: var(--muted-dim); box-shadow: none; }

.btn-soft { background: #1e293b; border-color: #334155; color: var(--text); }
.btn-soft:hover:not(:disabled) { background: #273449; }

.btn-ghost { color: var(--muted); border-color: transparent; }
.btn-ghost:hover:not(:disabled) { color: var(--text-strong); background: #1e293b; }

.btn-danger { background: rgba(239, 68, 68, 0.12); color: #fca5a5; border-color: rgba(239, 68, 68, 0.28); }
.btn-danger:hover:not(:disabled) { background: rgba(239, 68, 68, 0.22); }

.btn-amber { background: rgba(245, 158, 11, 0.12); color: #fcd34d; border-color: rgba(245, 158, 11, 0.3); }
.btn-amber:hover:not(:disabled) { background: rgba(245, 158, 11, 0.22); }

.btn-sm { min-height: 32px; padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }
.btn-round { width: 38px; padding: 0; border-radius: 50%; font-weight: 700; font-size: 15px; border-color: var(--border); }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 34px; height: 34px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  transition: color 0.15s, background 0.15s;
}
.icon-btn:hover:not(:disabled) { color: var(--text-strong); background: #1e293b; }
.icon-btn:disabled { opacity: 0.3; cursor: not-allowed; }
.icon-btn-danger:hover:not(:disabled) { color: #fca5a5; }

.input {
  width: 100%;
  min-height: 38px;
  padding: 8px 11px;
  background: var(--bg-2);
  border: 1px solid #334155;
  border-radius: var(--radius-sm);
  color: var(--text);
  font: inherit;
  font-size: 13px;
}
.input:focus { outline: none; border-color: var(--accent); }
.input-compact { width: auto; min-width: 110px; min-height: 34px; padding: 5px 9px; font-size: 12px; }
.input-range-syntax { max-width: 220px; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }

select.input {
  appearance: none;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 16px) 50%, calc(100% - 11px) 50%;
  background-size: 5px 5px, 5px 5px;
  background-repeat: no-repeat;
  padding-right: 30px;
}

.color-input {
  width: 38px; height: 38px;
  padding: 2px;
  background: var(--bg-2);
  border: 1px solid #334155;
  border-radius: var(--radius-sm);
  cursor: pointer;
}

.range { width: 100%; accent-color: var(--accent); height: 26px; cursor: pointer; }

.counter { font-size: 11.5px; color: var(--muted); white-space: nowrap; }
.hint { margin: 4px 0 0; font-size: 11.5px; color: var(--muted-dim); line-height: 1.5; }
.field-label { font-size: 12px; color: var(--muted); white-space: nowrap; }

.pager {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 2px 5px;
}

/* ============================================================
   Layout: sidebar + workspace
   ============================================================ */
.app-body {
  flex: 1;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.sidebar {
  width: 232px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px;
  background: rgba(15, 23, 42, 0.5);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}

.nav-tab {
  display: flex;
  align-items: center;
  gap: 11px;
  width: 100%;
  min-height: var(--tap);
  padding: 10px 12px;
  border-radius: var(--radius);
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  text-align: left;
  cursor: pointer;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}
.nav-tab i { width: 20px; text-align: center; font-size: 16px; }
.nav-tab:hover { color: var(--text); background: rgba(30, 41, 59, 0.6); }
.nav-tab.active {
  color: #a5b4fc;
  background: var(--accent-soft);
  border-color: rgba(99, 102, 241, 0.32);
}

.sidebar-foot {
  margin-top: auto;
  padding: 12px 4px 4px;
  border-top: 1px solid var(--border);
  font-size: 11px;
  color: var(--muted-dim);
  line-height: 1.5;
}
.sidebar-foot p { margin: 0; }

.workspace {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.tab-content {
  display: none;
  flex: 1;
  min-height: 0;
  flex-direction: column;
  gap: 12px;
  padding: 14px;
  overflow: auto;
}
.tab-content.active { display: flex; }

/* ============================================================
   Panels
   ============================================================ */
.panel {
  background: var(--panel);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius);
  backdrop-filter: blur(10px);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  padding: 12px;
  flex-shrink: 0;
}
.toolbar-sub { padding: 10px 12px; }
.toolbar-group { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.toolbar-end { margin-left: auto; }

.tool-group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  padding: 4px;
  background: rgba(2, 6, 23, 0.6);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}
.edit-tool-btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 34px;
  padding: 6px 10px;
  border-radius: 9px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.edit-tool-btn:hover { color: var(--text-strong); background: #1e293b; }
.edit-tool-btn.active { color: #a5b4fc; background: rgba(99, 102, 241, 0.2); }

.split {
  flex: 1;
  min-height: 0;
  display: flex;
  gap: 14px;
}
.side-panel {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 16px;
  overflow-y: auto;
}
.side-head h3 { display: flex; align-items: center; gap: 8px; font-size: 15px; }
.side-head h3 i { color: var(--accent); }
.side-head p { margin: 3px 0 0; font-size: 11.5px; color: var(--muted); }
.side-foot { margin-top: auto; padding-top: 14px; border-top: 1px solid var(--border); }

.grow-panel {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  padding: 14px;
  overflow: hidden;
}
.center-panel { align-items: center; justify-content: center; }

.panel-head {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  padding-bottom: 11px;
  margin-bottom: 11px;
  border-bottom: 1px solid var(--border);
}
.panel-head .btn-ghost { margin-left: auto; }

.field { display: flex; flex-direction: column; gap: 6px; }
.field > label { font-size: 12px; font-weight: 500; color: var(--text); }
.field-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.field-row-tight { display: flex; align-items: center; gap: 9px; }

.seg-group { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.seg {
  min-height: var(--tap);
  padding: 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--muted);
  font: inherit; font-size: 12px; font-weight: 500;
  cursor: pointer;
}
.seg:hover { border-color: #475569; color: var(--text); }
.seg.active { background: var(--accent-2); border-color: var(--accent); color: #fff; }

.chip-row { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 30px;
  padding: 5px 10px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--text);
  font: inherit; font-size: 11.5px;
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
}
.chip:hover { background: var(--accent-2); color: #fff; border-color: var(--accent); }

/* ============================================================
   Dropzones
   ============================================================ */
.dropzone {
  flex: 1;
  min-height: 190px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 26px 18px;
  text-align: center;
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  background: rgba(15, 23, 42, 0.25);
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
}
.dropzone:hover, .dropzone.drag-over {
  border-color: rgba(99, 102, 241, 0.7);
  background: rgba(99, 102, 241, 0.06);
}
.dropzone h3 { font-size: 16px; }
.dropzone p { margin: 0; max-width: 460px; font-size: 12.5px; color: var(--muted); }
.dropzone-icon {
  width: 60px; height: 60px;
  border-radius: 16px;
  display: grid; place-items: center;
  font-size: 22px;
  color: #a5b4fc;
  background: var(--accent-soft);
  border: 1px solid rgba(99, 102, 241, 0.25);
}
.dropzone-fill { width: 100%; height: 100%; }
.dropzone-inline { grid-column: 1 / -1; min-height: 150px; }
.dropzone-inline i { font-size: 28px; color: var(--muted-dim); }

/* ============================================================
   Organizer page grid
   ============================================================ */
.pages-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  padding: 4px;
  overflow-y: auto;
  align-content: start;
}
.pages-grid[hidden] { display: none; }

.page-card {
  position: relative;
  display: flex;
  flex-direction: column;
  padding: 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card);
  transition: border-color 0.15s, box-shadow 0.15s, opacity 0.15s;
}
.page-card:hover { border-color: rgba(99, 102, 241, 0.45); box-shadow: 0 8px 22px -10px rgba(99, 102, 241, 0.35); }
.page-card.selected { border-color: var(--accent); background: rgba(99, 102, 241, 0.13); }
.page-card.dragging { opacity: 0.35; }
.page-card.drop-before { box-shadow: -3px 0 0 0 var(--accent); }
.page-card.drop-after  { box-shadow:  3px 0 0 0 var(--accent); }

.page-card-top {
  position: absolute;
  top: 8px; left: 8px; right: 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  z-index: 2;
}
.page-card-top .spacer { flex: 1; }
.page-chk { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }

.drag-handle {
  display: inline-grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.85);
  color: var(--muted);
  cursor: grab;
  font-size: 12px;
  /* Only the handle disables native touch gestures — the grid still scrolls. */
  touch-action: none;
}
.drag-handle:active { cursor: grabbing; color: #a5b4fc; }

.page-mini-btn {
  display: inline-grid;
  place-items: center;
  width: 30px; height: 30px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: rgba(2, 6, 23, 0.85);
  color: var(--text);
  cursor: pointer;
  font-size: 11px;
}
.page-mini-btn:hover { background: #1e293b; }
.page-mini-btn.danger { color: #fca5a5; border-color: rgba(239, 68, 68, 0.4); }
.page-mini-btn.danger:hover { background: rgba(239, 68, 68, 0.25); }

.page-thumb {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 150px;
  margin: 34px 0 8px;
  padding: 6px;
  border-radius: var(--radius-sm);
  background: rgba(2, 6, 23, 0.5);
  overflow: hidden;
}
.page-thumb img {
  max-width: 100%;
  max-height: 140px;
  object-fit: contain;
  transition: transform 0.2s;
  pointer-events: none;
}

.page-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  font-size: 10.5px;
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.page-meta .fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.page-index {
  position: absolute;
  bottom: 8px; right: 8px;
  min-width: 20px;
  padding: 1px 5px;
  border-radius: 6px;
  background: rgba(99, 102, 241, 0.2);
  color: #a5b4fc;
  font-size: 10px;
  font-weight: 700;
  text-align: center;
}

.drag-ghost {
  position: fixed;
  z-index: 4000;
  pointer-events: none;
  opacity: 0.9;
  border-radius: var(--radius);
  border: 1px solid var(--accent);
  background: var(--panel-solid);
  box-shadow: var(--shadow);
  padding: 6px;
}
.drag-ghost img { display: block; max-width: 110px; max-height: 130px; }

/* ============================================================
   Editor stage
   ============================================================ */
.stage-panel {
  flex: 1;
  min-height: 320px;
  display: flex;
  padding: 14px;
  /* Scrolling lives on the panel, not the stage, so the absolutely
     positioned overlay canvas stays glued to the page canvas. */
  overflow: auto;
}
.editor-stage {
  position: relative;
  /* `margin:auto` centres in a scrollable flex container without the
     classic flex-centering overflow clipping. Width is set from JS to the
     rendered page width; max-width lets it shrink on small screens with
     both canvases scaling together. */
  margin: auto;
  max-width: 100%;
  flex: 0 0 auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  background: #fff;
}
#editorPdfCanvas { display: block; width: 100%; height: auto; }
.annotation-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  cursor: crosshair;
  /* Canvas owns the gesture so drawing never scrolls the page. */
  touch-action: none;
}
.annotation-canvas.tool-select { cursor: default; }
.text-overlay { position: absolute; inset: 0; z-index: 20; pointer-events: none; }
.text-box {
  position: absolute;
  min-width: 110px;
  padding: 3px 5px;
  border-radius: 6px;
  border: 1px solid rgba(99, 102, 241, 0.85);
  background: rgba(15, 23, 42, 0.92);
  color: #fff;
  pointer-events: auto;
  outline: none;
}

/* ============================================================
   Images grid
   ============================================================ */
.image-grid {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
  gap: 14px;
  overflow-y: auto;
  align-content: start;
}
.image-card {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 7px;
  padding: 10px;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  background: var(--card);
}
.image-card .thumb {
  display: flex; align-items: center; justify-content: center;
  height: 120px;
  border-radius: var(--radius-sm);
  background: rgba(2, 6, 23, 0.5);
  overflow: hidden;
}
.image-card .thumb img { max-width: 100%; max-height: 110px; object-fit: contain; }
.image-card .name {
  font-size: 11px; color: var(--muted); text-align: center;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
.image-card .page-mini-btn { position: absolute; top: 8px; right: 8px; }

/* ============================================================
   Watermark / sheet preview
   ============================================================ */
.preview-wrap {
  flex: 1;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: auto;
  padding: 6px;
}
.preview-wrap[hidden] { display: none; }
.preview-canvas {
  max-width: 100%;
  max-height: 100%;
  height: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  cursor: grab;
  touch-action: none;
}
.preview-canvas:active { cursor: grabbing; }

.sheet-frame {
  padding: 8px;
  background: #fff;
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow);
  max-width: 100%;
  overflow: auto;
}
#sheetCanvas { display: block; max-width: 100%; height: auto; max-height: 62vh; border: 1px solid #e2e8f0; }

/* ============================================================
   Markdown studio
   ============================================================ */
.md-split {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}
.md-pane { display: flex; flex-direction: column; min-height: 260px; padding: 10px; overflow: hidden; }

.md-toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 3px;
  padding-bottom: 8px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--border);
}
.md-format-btn {
  min-width: 32px; min-height: 32px;
  padding: 4px 8px;
  border: 1px solid transparent;
  border-radius: 7px;
  background: transparent;
  color: var(--text);
  font: inherit; font-size: 12px;
  cursor: pointer;
}
.md-format-btn:hover { background: #1e293b; }
.v-rule { width: 1px; height: 16px; background: var(--border); margin: 0 4px; }
.md-stats {
  margin-left: auto;
  display: flex; gap: 12px;
  font-size: 10.5px; color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

#markdownInput {
  flex: 1;
  min-height: 200px;
  width: 100%;
  padding: 14px;
  resize: none;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--bg-2);
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono", monospace;
  font-size: 13px;
  line-height: 1.65;
}
#markdownInput:focus { outline: none; border-color: var(--accent); }

.md-preview-head {
  display: flex; justify-content: space-between;
  padding: 0 4px 8px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.6px;
  text-transform: uppercase;
  color: var(--muted);
}

.markdown-body {
  flex: 1;
  min-height: 200px;
  padding: 22px;
  overflow-y: auto;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.6);
  overflow-wrap: anywhere;
}
.markdown-body h1 { font-size: 1.8rem; margin: 0 0 0.7rem; padding-bottom: 0.4rem; border-bottom: 1px solid #334155; color: var(--text-strong); }
.markdown-body h2 { font-size: 1.4rem; margin: 1.2rem 0 0.5rem; color: #f1f5f9; }
.markdown-body h3 { font-size: 1.15rem; margin: 1rem 0 0.45rem; }
.markdown-body h4, .markdown-body h5, .markdown-body h6 { margin: 0.9rem 0 0.4rem; }
.markdown-body p { margin: 0 0 0.7rem; line-height: 1.65; color: #cbd5e1; }
.markdown-body ul { list-style: disc; padding-left: 1.5rem; margin: 0 0 0.7rem; color: #cbd5e1; }
.markdown-body ol { list-style: decimal; padding-left: 1.5rem; margin: 0 0 0.7rem; color: #cbd5e1; }
.markdown-body li { margin-bottom: 0.25rem; }
.markdown-body a { color: #818cf8; }
.markdown-body hr { border: none; border-top: 1px solid #334155; margin: 1.1rem 0; }
.markdown-body code { background: #1e293b; padding: 0.15rem 0.35rem; border-radius: 5px; font-size: 0.87em; color: #a5b4fc; }
.markdown-body pre { background: #020617; padding: 0.9rem; border-radius: 9px; overflow-x: auto; margin: 0 0 0.7rem; border: 1px solid #334155; }
.markdown-body pre code { background: none; padding: 0; color: #cbd5e1; }
.markdown-body blockquote { margin: 0 0 0.7rem; border-left: 4px solid var(--accent); padding-left: 0.9rem; color: var(--muted); font-style: italic; }
.markdown-body table { width: 100%; border-collapse: collapse; margin-bottom: 0.9rem; font-size: 0.92em; display: block; overflow-x: auto; }
.markdown-body th, .markdown-body td { border: 1px solid #334155; padding: 0.4rem 0.65rem; text-align: left; }
.markdown-body th { background: #1e293b; }
.markdown-body img { max-width: 100%; height: auto; }

/* ============================================================
   Modals
   ============================================================ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 3000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(2, 6, 23, 0.8);
  backdrop-filter: blur(6px);
  overflow-y: auto;
}
.modal[hidden] { display: none; }

.modal-card {
  width: 100%;
  max-width: 480px;
  max-height: 88vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 13px;
  padding: 20px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: #0f172a;
  box-shadow: var(--shadow);
}
.modal-sm { max-width: 380px; }
.modal-lg { max-width: 660px; }

.modal-head { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.modal-head h3 { display: flex; align-items: center; gap: 9px; font-size: 15px; }
.modal-head i.fa-signature, .modal-head i.fa-certificate { color: var(--amber); }
.modal-foot { display: flex; align-items: center; justify-content: space-between; gap: 10px; padding-top: 6px; }

.sig-frame { background: #fff; border-radius: var(--radius); border: 1px solid #cbd5e1; overflow: hidden; }
#sigCanvas {
  display: block;
  width: 100%;
  height: 190px;
  cursor: crosshair;
  touch-action: none;
}

.stamp-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 11px; }
.stamp-choice {
  min-height: var(--tap);
  padding: 12px 8px;
  border-radius: var(--radius-sm);
  border: 2px solid currentColor;
  background: rgba(2, 6, 23, 0.5);
  font: inherit; font-weight: 800; font-size: 12.5px; letter-spacing: 0.6px;
  cursor: pointer;
  transition: background 0.15s, transform 0.08s;
}
.stamp-choice:hover { background: rgba(255, 255, 255, 0.07); }
.stamp-choice:active { transform: scale(0.97); }

.projects-list { display: flex; flex-direction: column; gap: 8px; max-height: 46vh; overflow-y: auto; }
.project-row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--card);
}
.project-row .meta { flex: 1; min-width: 0; }
.project-row .meta strong { display: block; font-size: 13px; color: var(--text-strong); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.project-row .meta span { font-size: 11px; color: var(--muted); }
.projects-empty { padding: 26px 12px; text-align: center; color: var(--muted-dim); font-size: 12.5px; }

.help-body { font-size: 13px; color: var(--text); line-height: 1.65; }
.help-body h4 { margin: 14px 0 5px; font-size: 13px; color: #a5b4fc; text-transform: uppercase; letter-spacing: 0.5px; }
.help-body h4:first-child { margin-top: 0; }
.help-body ul { margin: 0; padding-left: 20px; color: var(--muted); }
.help-body li { margin-bottom: 4px; }
.help-body strong { color: var(--text-strong); }
.help-body code { background: #1e293b; padding: 1px 5px; border-radius: 4px; font-size: 12px; color: #a5b4fc; }

/* ============================================================
   Toasts + loading
   ============================================================ */
.toast-container {
  position: fixed;
  right: 16px;
  bottom: calc(16px + env(safe-area-inset-bottom));
  left: 16px;
  z-index: 5000;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
  pointer-events: none;
}
.toast {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  max-width: min(420px, 100%);
  padding: 11px 14px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: #1e293b;
  color: var(--text);
  font-size: 12.5px;
  line-height: 1.45;
  box-shadow: var(--shadow);
  pointer-events: auto;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.25s, transform 0.25s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast i { margin-top: 1px; }
.toast-success { background: rgba(5, 150, 105, 0.95); border-color: #34d399; color: #fff; }
.toast-error   { background: rgba(220, 38, 38, 0.95); border-color: #f87171; color: #fff; }
.toast-warn    { background: rgba(217, 119, 6, 0.95); border-color: #fbbf24; color: #fff; }

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 4500;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  background: rgba(2, 6, 23, 0.82);
  backdrop-filter: blur(6px);
}
.loading-overlay[hidden] { display: none; }
.loading-overlay p { margin: 0; font-size: 13px; color: var(--text); }
.spinner {
  width: 46px; height: 46px;
  border: 4px solid rgba(99, 102, 241, 0.28);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: pw-spin 0.75s linear infinite;
}
@keyframes pw-spin { to { transform: rotate(360deg); } }


/* Local icon fallback: no icon-font network dependency. */
i.fa-solid, i.fa-regular { font-style: normal; display: inline-block; min-width: 1em; text-align: center; }
i.fa-solid::before, i.fa-regular::before { content: "•"; }
i.fa-triangle-exclamation::before { content: "!"; }
i.fa-xmark::before { content: "×"; }
i.fa-circle-info::before { content: "i"; }
i.fa-circle-question::before { content: "?"; }
i.fa-circle-check::before, i.fa-check-double::before, i.fa-square-check::before { content: "✓"; }
i.fa-shield-halved::before { content: "◆"; }
i.fa-plus::before, i.fa-file-circle-plus::before { content: "+"; }
i.fa-download::before, i.fa-file-arrow-down::before, i.fa-file-export::before { content: "↓"; }
i.fa-trash::before, i.fa-trash-can::before { content: "×"; }
i.fa-rotate::before, i.fa-rotate-right::before { content: "↻"; }
i.fa-rotate-left::before { content: "↺"; }
i.fa-chevron-left::before { content: "‹"; }
i.fa-chevron-right::before { content: "›"; }
i.fa-grip-vertical::before { content: "⋮"; }
i.fa-scissors::before { content: "✂"; }
i.fa-arrow-pointer::before { content: "↖"; }
i.fa-font::before { content: "T"; }
i.fa-pen::before, i.fa-pen-to-square::before, i.fa-file-pen::before { content: "✎"; }
i.fa-highlighter::before { content: "▰"; }
i.fa-eraser::before { content: "□"; }
i.fa-square::before, i.fa-border-top-left::before { content: "□"; }
i.fa-certificate::before, i.fa-stamp::before { content: "◎"; }
i.fa-signature::before { content: "S"; }
i.fa-floppy-disk::before { content: "▣"; }
i.fa-folder-open::before { content: "▱"; }
i.fa-file-pdf::before { content: "PDF"; font-size: .72em; }
i.fa-file-image::before, i.fa-images::before { content: "▧"; }
i.fa-layer-group::before { content: "▤"; }
i.fa-list-ul::before { content: "•≡"; font-size: .8em; }
i.fa-list-ol::before { content: "1≡"; font-size: .8em; }
i.fa-quote-left::before { content: "“"; }
i.fa-code::before { content: "<>"; font-size: .75em; }
i.fa-table::before { content: "▦"; }
i.fa-crosshairs::before { content: "+"; }
i.fa-clock-rotate-left::before { content: "◷"; }
i.fa-sliders::before { content: "≡"; }
.md-export-note { margin: 8px 0 0; padding: 0 4px 4px; }

/* ============================================================
   Responsive
   ============================================================ */
@media (max-width: 900px) {
  .app-body { flex-direction: column; }

  .sidebar {
    width: 100%;
    flex-direction: row;
    gap: 6px;
    padding: 8px 10px;
    border-right: none;
    border-bottom: 1px solid var(--border);
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
  }
  .nav-tab { width: auto; flex: 0 0 auto; padding: 8px 12px; }
  .nav-tab span { display: none; }
  .nav-tab.active span { display: inline; }
  .sidebar-foot { display: none; }

  .split { flex-direction: column; }
  .side-panel { width: 100%; max-height: none; }
  .md-split { grid-template-columns: 1fr; }
  .md-pane { min-height: 240px; }
  .stage-panel { min-height: 260px; }
}

@media (max-width: 640px) {
  .title-block .subtitle { display: none; }
  .btn-label { display: none; }
  .toolbar-end { margin-left: 0; width: 100%; }
  .pages-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 10px; }
  .image-grid { grid-template-columns: repeat(auto-fill, minmax(128px, 1fr)); gap: 10px; }
  .input-range-syntax { max-width: 100%; flex: 1; }
  .toast-container { align-items: stretch; }
  .toast { max-width: 100%; }
  .modal { padding: 10px; align-items: flex-start; }
  .modal-card { padding: 16px; }
}

@media (max-width: 380px) {
  .brand-word { display: none; }
  .brand-divider { display: none; }
  .edit-tool-btn span { display: none; }
  .pages-grid { grid-template-columns: repeat(auto-fill, minmax(112px, 1fr)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .spinner { animation: pw-spin 1.2s linear infinite !important; }
}

@media (max-width: 640px) {
  .app-header { padding-left: 10px; padding-right: 10px; gap: 8px; }
  .header-actions { gap: 4px; min-width: 0; }
  .toolbar, .toolbar-group { max-width: 100%; }
  .toolbar-group { flex-wrap: wrap; }
  .privacy-strip { line-height: 1.35; height: auto; min-height: 32px; padding-top: 6px; padding-bottom: 6px; }
  .side-panel .side-foot .btn, .toolbar .btn { min-height: 44px; }
  .nav-tab { min-height: 44px; }
  .input, input, select, textarea, button { max-width: 100%; }
  .project-row { align-items: flex-start; flex-wrap: wrap; }
  .project-row .meta { flex-basis: calc(100% - 80px); }
}
@media (max-width: 360px) {
  .app-header .brand-mark { width: 30px; height: 30px; }
  .app-header h1 { font-size: 15px; }
  .header-actions .btn { padding-left: 8px; padding-right: 8px; }
}
