:root {
  --bg: #f3f2ef;
  --surface: #ffffff;
  --paper: #ffffff;
  --ink: #16161a;
  --muted: #6b6b72;
  --line: #dcdad4;
  --line-strong: #b9b6ae;
  --accent: #16161a;
  --accent-ink: #ffffff;
  --danger: #b42318;
  --hover: #f0efea;
  --radius: 10px;
  --bubble: 30px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: "Inter", system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.45;
}
button, input, select, textarea { font: inherit; color: inherit; }

/* Top bar */
.topbar {
  position: sticky; top: 0; z-index: 10;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.logo { display: inline-flex; gap: 3px; }
.logo i { width: 10px; height: 10px; border-radius: 50%; border: 1.5px solid var(--ink); display: block; }
.logo i.on { background: var(--ink); }
.topbar-actions { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.progress { color: var(--muted); font-variant-numeric: tabular-nums; margin-right: 6px; }

/* Buttons */
.btn {
  border: 1px solid var(--line-strong);
  background: var(--surface);
  padding: 7px 14px;
  border-radius: 8px;
  cursor: pointer;
  font-weight: 500;
  white-space: nowrap;
}
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.btn.primary:hover { opacity: .88; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--hover); }
.btn.small { padding: 5px 10px; font-size: 13px; }
.btn.danger { color: var(--danger); }
.file-btn { display: inline-block; }

/* Layout */
body { height: 100vh; display: flex; flex-direction: column; }
.layout { display: grid; grid-template-columns: 300px 1fr; grid-template-rows: minmax(0, 1fr); flex: 1; min-height: 0; }
.sidebar {
  border-right: 1px solid var(--line);
  background: var(--surface);
  padding: 8px 16px 40px;
  overflow-y: auto;
  min-height: 0;
}
.panel { padding: 14px 0; border-bottom: 1px solid var(--line); }
.panel:last-child { border-bottom: 0; }
.panel h2 { font-size: 13px; font-weight: 600; margin: 0 0 10px; color: var(--muted); }
.panel-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.panel-head h2 { margin: 0; }

.library { list-style: none; padding: 0; margin: 0 0 8px; max-height: 200px; overflow-y: auto; }
.library li {
  padding: 7px 10px; border-radius: 8px; cursor: pointer;
  display: flex; justify-content: space-between; gap: 8px;
}
.library li:hover { background: var(--hover); }
.library li.active { background: var(--ink); color: var(--accent-ink); }
.library li .meta { color: var(--muted); font-size: 12px; white-space: nowrap; }
.library li.active .meta { color: #c9c9cf; }
.library li .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row-btns { display: flex; gap: 4px; flex-wrap: wrap; }

.templates { display: grid; gap: 6px; }
.template {
  text-align: left; border: 1px solid var(--line); background: var(--surface);
  border-radius: 8px; padding: 8px 10px; cursor: pointer;
}
.template:hover { border-color: var(--ink); }
.template strong { display: block; font-weight: 600; }
.template span { color: var(--muted); font-size: 12px; }
.template { width: 100%; }
.tpl-actions { display: flex; gap: 2px; margin-top: 2px; }
.tpl-actions button { border: 0; background: transparent; color: var(--muted); font-size: 12px; padding: 2px 6px; border-radius: 6px; cursor: pointer; }
.tpl-actions button:hover { background: var(--hover); color: var(--ink); }
.tpl-actions button.danger { color: var(--danger); }
.templates .hint { margin: 0; }

.field { display: block; margin-bottom: 10px; font-size: 12px; color: var(--muted); }
.field input, .field select, textarea {
  display: block; width: 100%; margin-top: 4px;
  padding: 7px 9px; border: 1px solid var(--line-strong); border-radius: 7px;
  background: var(--surface); color: var(--ink); font-size: 14px;
}
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.check { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }
.check input { width: 16px; height: 16px; accent-color: var(--ink); }
.hint { color: var(--muted); font-size: 12px; margin: 8px 0 0; }
textarea { resize: vertical; margin-bottom: 8px; }

/* Sheet */
.main { height: 100%; overflow: hidden; min-width: 0; }
.split { display: flex; height: 100%; --doc: 50%; }
.pane { overflow: auto; min-width: 0; }
.sheet-pane { flex: 1 1 auto; padding: 28px 20px 80px; }
.doc-pane { flex: 0 0 var(--doc); display: flex; flex-direction: column; background: #3b3d40; }
.doc-pane[hidden], .divider[hidden] { display: none; }
.divider { flex: 0 0 8px; cursor: col-resize; background: var(--line); position: relative; touch-action: none; }
.divider::after { content: ""; position: absolute; left: 3px; top: 50%; width: 2px; height: 36px; margin-top: -18px; border-radius: 2px; background: var(--line-strong); }
.divider:hover, body.dragging .divider { background: var(--line-strong); }
body.dragging { cursor: col-resize; user-select: none; }
body.dragging .doc-pages { pointer-events: none; }
.doc-bar { display: flex; align-items: center; justify-content: space-between; gap: 8px; flex-wrap: wrap; padding: 8px 12px; background: var(--surface); border-bottom: 1px solid var(--line); }
.doc-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; flex: 1; }
.doc-tools { display: flex; align-items: center; gap: 4px; }
.doc-zoom { min-width: 44px; text-align: center; font-variant-numeric: tabular-nums; color: var(--muted); font-size: 13px; }
.doc-pages { flex: 1; overflow: auto; padding: 16px; display: flex; flex-direction: column; align-items: center; gap: 12px; }
.doc-page { background: #fff; flex-shrink: 0; box-shadow: 0 2px 10px rgba(0,0,0,.35); }
.doc-page canvas { display: block; width: 100%; height: 100%; }
.doc-msg { color: #e8e8e8; margin-top: 40px; text-align: center; }
/* Collapsible sidebar */
.layout { transition: grid-template-columns .22s ease; }
.sidebar { transition: opacity .18s ease, padding .22s ease; }
body.no-sidebar .layout { grid-template-columns: 0px 1fr; }
body.no-sidebar .sidebar { opacity: 0; padding-left: 0; padding-right: 0; border-right-width: 0; overflow: hidden; pointer-events: none; }
.icon-btn {
  width: 34px; height: 34px; display: inline-flex; align-items: center; justify-content: center;
  border: 0; border-radius: 8px; background: transparent; color: var(--muted); cursor: pointer; padding: 0;
}
.icon-btn:hover { background: var(--hover); color: var(--ink); }
.icon-btn:focus-visible { outline: 2px solid #4a6cf7; outline-offset: 1px; }
.icon-btn .pane-fill { transition: opacity .15s; }
body.no-sidebar .icon-btn .pane-fill { opacity: 0; }
@media (prefers-reduced-motion: reduce) { .layout, .sidebar { transition: none; } }
/* On-screen sheet = A4 portrait page (210 × 297 mm, 16 mm margins like the PDF) */
.sheet {
  background: var(--paper);
  width: 210mm; max-width: 100%; margin: 0 auto;
  aspect-ratio: 210 / 297; /* grows taller when content needs more than one page */
  padding: 16mm;
  border: 1px solid var(--line);
  border-radius: 4px;
  box-shadow: 0 1px 2px rgba(0,0,0,.04), 0 8px 24px rgba(0,0,0,.05);
}
.sheet-head { border-bottom: 2px solid var(--ink); padding-bottom: 14px; margin-bottom: 18px; }
.sheet-head h1 { font-size: 24px; margin: 0; letter-spacing: -.01em; text-align: center; }
.sheet-sub { margin: 4px 0 0; color: var(--muted); text-align: center; }
.sheet-sub:empty { display: none; }
.sheet-fields { display: flex; gap: 24px; margin-top: 14px; flex-wrap: wrap; }
.sheet-fields span { display: flex; align-items: flex-end; gap: 6px; flex: 1; min-width: 140px; }
.sheet-fields i { flex: 1; border-bottom: 1px solid var(--ink); height: 1px; }
.sheet-fields[hidden] { display: none; }

.questions { list-style: none; padding: 0; margin: 0; }
.questions.compact { display: grid; grid-template-columns: repeat(auto-fill, minmax(230px, 1fr)); column-gap: 28px; }
.q {
  display: flex; align-items: center; gap: 14px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}
.questions.compact .q { padding: 6px 0; border-bottom: 1px dashed var(--line); }
.q-num { font-weight: 700; min-width: 34px; font-variant-numeric: tabular-nums; }
.q-text {
  flex: 1; min-width: 0;
  border: 1px solid transparent; border-radius: 6px;
  padding: 4px 6px; margin-left: -6px;
  background: transparent; resize: none; overflow: hidden;
  line-height: 1.4; min-height: 30px;
}
.q-text:hover { border-color: var(--line); }
.q-text:focus { outline: none; border-color: var(--ink); background: #fff; }
.q-text::placeholder { color: #b0aea8; }
.bubbles { display: flex; gap: 8px; flex-shrink: 0; }
.questions.compact .bubbles { margin-left: auto; }

.bubble {
  width: var(--bubble); height: var(--bubble);
  border-radius: 50%;
  border: 1.6px solid var(--ink);
  background: #fff;
  color: var(--ink);
  font-size: 12px; font-weight: 600;
  display: inline-flex; align-items: center; justify-content: center;
  cursor: pointer; padding: 0;
  transition: background .12s, color .12s, transform .08s;
}
.bubble:hover { background: #e9e8e3; }
.bubble:active { transform: scale(.92); }
.bubble:focus-visible { outline: 2px solid #4a6cf7; outline-offset: 2px; }
.bubble[aria-pressed="true"] { background: var(--ink); color: #fff; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translate(-50%, 20px);
  background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 8px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* On-screen A4 pages — positioned in mm from the same layout as the PDF */
.pages { display: flex; flex-direction: column; align-items: center; gap: 24px; --z: 1; }
.a4 {
  position: relative; flex-shrink: 0;
  width: 210mm; height: 297mm;
  zoom: var(--z);
  background: #fff;
  box-shadow: 0 1px 2px rgba(0,0,0,.06), 0 8px 28px rgba(0,0,0,.12);
  font-family: Helvetica, Arial, sans-serif;
  color: rgb(20,20,20);
  overflow: hidden;
}
.a4 .pt { position: absolute; white-space: pre; line-height: 1; }
.a4 .pt.b { font-weight: 700; }
.a4 .pt.a-center { transform: translateX(-50%); }
.a4 .pt.a-right { transform: translateX(-100%); }
.a4 .pl { position: absolute; }
.a4 .bub {
  position: absolute; padding: 0; margin: 0;
  border: .35mm solid rgb(20,20,20); border-radius: 50%;
  background: #fff; cursor: pointer;
  transition: background .1s;
}
.a4 .bub:hover { background: #e4e4e4; }
.a4 .bub[aria-pressed="true"] { background: rgb(15,15,15); }
.a4 .bub:focus-visible { outline: 2px solid #4a6cf7; outline-offset: 1px; }
.a4 .pq {
  position: absolute; padding: 0 1mm; border-radius: 1mm;
  white-space: pre-wrap; overflow-wrap: break-word;
  outline: none; cursor: text;
}
.a4 .pq:hover { background: rgba(0,0,0,.035); }
.a4 .pq:focus { background: #fff; box-shadow: 0 0 0 1px rgb(20,20,20); z-index: 2; }
.a4 .pq:empty::before { content: attr(data-placeholder); color: #b0aea8; }

/* Editable title on the sheet */
.a4 .pt-title { cursor: text; border-radius: 1.5mm; padding: 0 2mm; margin-left: 0; outline: none; transition: background .12s, box-shadow .12s; }
.a4 .pt-title:hover, .a4 .pt-title:focus-visible { background: rgba(0,0,0,.05); box-shadow: 0 0 0 1px rgba(0,0,0,.18); }
.a4 .title-edit {
  position: absolute; z-index: 3; box-sizing: border-box;
  font-family: Helvetica, Arial, sans-serif; font-weight: 700; color: rgb(20,20,20);
  text-align: center; line-height: 1.3; padding: .5mm 2mm;
  border: 0; border-radius: 1.5mm; background: #fff;
  box-shadow: 0 0 0 1.5px rgb(20,20,20), 0 2px 10px rgba(0,0,0,.12); outline: none;
}

/* Size controls */
.size-box { margin-top: 12px; padding: 10px; border: 1px solid var(--line); border-radius: 10px; }
.size-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; }
.size-title { font-weight: 600; font-size: 13px; }
.seg-full { display: flex; width: 100%; margin-bottom: 10px; }
.seg-full button { flex: 1; font-size: 13px; padding: 6px 8px; }
.range { display: block; font-size: 12px; color: var(--muted); margin-bottom: 8px; }
.range span { display: flex; justify-content: space-between; }
.range output { color: var(--ink); font-variant-numeric: tabular-nums; font-weight: 600; }
.range input { width: 100%; margin: 4px 0 0; accent-color: var(--ink); }
.range.off { opacity: .45; }
.size-box .hint { margin-top: 2px; }
.small-check { font-size: 12px; margin: -2px 0 10px; color: var(--muted); }
.small-check input { width: 14px; height: 14px; }
.size-group { font-size: 11px; font-weight: 600; color: var(--ink); margin: 12px 0 6px; padding-top: 10px; border-top: 1px solid var(--line); }

/* A4 PDF preview */
body.modal-open { overflow: hidden; }
.preview {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(30, 30, 34, .82);
  display: flex; flex-direction: column;
}
.preview[hidden] { display: none; }
.preview-bar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; background: var(--surface); border-bottom: 1px solid var(--line);
}
.preview-info { color: var(--muted); font-variant-numeric: tabular-nums; }
.preview-actions { display: flex; gap: 6px; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 6px 14px; cursor: pointer; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--line-strong); }
.seg button.on { background: var(--ink); color: #fff; }
.preview-pages {
  flex: 1; overflow: auto;
  display: flex; flex-direction: column; align-items: center; gap: 20px;
  padding: 24px 16px 48px;
}
.a4-page {
  display: block; background: #fff;
  width: min(210mm, 100%); height: auto; aspect-ratio: 210 / 297;
  box-shadow: 0 4px 18px rgba(0,0,0,.35);
}
.preview-msg { color: #fff; margin-top: 40px; }

/* Responsive */
@media (max-width: 860px) {
  body { height: auto; display: block; }
  .layout { grid-template-columns: 1fr; grid-template-rows: auto; }
  .sidebar { position: static; max-height: none; border-right: 0; border-bottom: 1px solid var(--line); }
  body.no-sidebar .layout { grid-template-columns: 1fr; }
  body.no-sidebar .sidebar { display: none; }
  .main { height: auto; overflow: visible; }
  .split { flex-direction: column; height: auto; }
  .sheet-pane { padding: 16px; overflow: visible; }
  .divider { display: none; }
  .doc-pane { flex: none; height: 80vh; }
  .sheet { padding: 22px 16px 28px; aspect-ratio: auto; }
  .q { flex-wrap: wrap; }
  .questions:not(.compact) .bubbles { width: 100%; padding-left: 48px; }
  .topbar { padding: 10px 16px; }
}

/* Print fallback (used if the PDF library can't load) */
@page { size: A4 portrait; margin: 0; }
@media print {
  body { background: #fff; }
  .topbar, .sidebar, .toast, .preview, .doc-pane, .divider { display: none !important; }
  .main, .split, .pane { height: auto !important; overflow: visible !important; display: block; padding: 0 !important; }
  .pages { gap: 0; --z: 1 !important; }
  .a4 { box-shadow: none; break-after: page; zoom: 1; }
  .a4 .pq:empty::before { content: none; }
  body.print-blank .bub[aria-pressed="true"] { background: #fff; }
  .bub { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  .layout { display: block; }
  .main { padding: 0; }
  .sheet { border: 0; box-shadow: none; padding: 0; max-width: none; }
  .q { break-inside: avoid; }
  .q-text { border: 0 !important; }
  body.print-blank .bubble[aria-pressed="true"] { background: #fff; color: var(--ink); }
  .bubble { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}
