:root {
  --ink: #17212f; --muted: #5d6b7e; --line: #dfe4eb; --bg: #f3f5f8; --card: #fff;
  --brand: #1f4e79; --brand-2: #e8f0f8; --ok: #15803d; --warn: #b45309; --danger: #b91c1c;
  --radius: 10px; --top: 58px;
  font-family: 'Inter', system-ui, -apple-system, 'Segoe UI', Roboto, Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }

/* top bar */
.topbar { position: sticky; top: 0; z-index: 20; height: var(--top); display: flex; align-items: center; justify-content: space-between;
  padding: 0 18px; background: #fff; border-bottom: 1px solid var(--line); gap: 12px; }
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 17px; }
.logo { width: 30px; height: 30px; border-radius: 8px; background: var(--brand); color: #fff; display: grid; place-items: center; font-weight: 800; }
.save-state { font-weight: 400; color: var(--muted); font-size: 12px; }
.save-state.err { color: var(--danger); }
.top-actions { display: flex; gap: 8px; align-items: center; }
.menu { position: relative; }
.menu-list { position: absolute; right: 0; top: calc(100% + 6px); background: #fff; border: 1px solid var(--line); border-radius: var(--radius);
  box-shadow: 0 10px 30px rgba(15,23,42,.12); padding: 6px; min-width: 220px; display: flex; flex-direction: column; }
.menu-list[hidden] { display: none; }
.menu-list button, .menu-list label { text-align: left; background: none; border: 0; padding: 9px 10px; border-radius: 7px; cursor: pointer; }
.menu-list button:hover, .menu-list label:hover { background: var(--bg); }
.menu-list hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: #fff; padding: 8px 14px; border-radius: 8px; cursor: pointer; font-weight: 600; white-space: nowrap; }
.btn:hover { border-color: #b9c3d0; }
.btn.primary { background: var(--brand); border-color: var(--brand); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; }
.btn.danger, .icon.danger { color: var(--danger); }
.btn[disabled] { opacity: .6; cursor: progress; }
.icon { border: 1px solid transparent; background: none; width: 28px; height: 28px; border-radius: 6px; cursor: pointer; color: var(--muted); }
.icon:hover:not([disabled]) { background: var(--bg); border-color: var(--line); }
.icon[disabled] { opacity: .3; cursor: default; }

/* layout */
.layout { display: grid; grid-template-columns: minmax(380px, 520px) 1fr; height: calc(100vh - var(--top)); }
.editor { overflow-y: auto; padding: 16px 16px 80px; border-right: 1px solid var(--line); background: var(--bg); }
.preview-pane { display: flex; flex-direction: column; min-width: 0; background: #e7ebf0; }
.mobile-tabs { display: none; }

/* panels */
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); margin-bottom: 10px; }
.panel > summary { list-style: none; cursor: pointer; padding: 13px 16px; font-weight: 650; display: flex; justify-content: space-between; align-items: center; }
.panel > summary::-webkit-details-marker { display: none; }
.panel > summary::after { content: '▸'; color: var(--muted); transition: transform .15s; margin-left: 8px; }
.panel[open] > summary::after { transform: rotate(90deg); }
.panel > summary span:first-child { flex: 1; }
.badge { background: var(--brand-2); color: var(--brand); border-radius: 999px; padding: 1px 9px; font-size: 12px; font-weight: 700; }
.panel-body { padding: 0 16px 16px; }
.grid { display: flex; flex-wrap: wrap; gap: 10px 12px; }
.fld { flex: 1 1 100%; display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.fld.half { flex-basis: calc(50% - 6px); }
.fld.third { flex-basis: calc(33.33% - 8px); }
.fld label { font-size: 12.5px; color: var(--muted); font-weight: 600; }
.fld input, .fld select, .fld textarea { border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; background: #fff; width: 100%; }
.fld textarea { resize: vertical; line-height: 1.45; }
.fld input:focus, .fld select:focus, .fld textarea:focus { outline: 2px solid color-mix(in srgb, var(--brand) 35%, transparent); border-color: var(--brand); }
.check { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.fld.check { flex-direction: row; align-items: center; }
.hint { color: var(--muted); font-size: 12px; line-height: 1.4; }
.hint.full { flex-basis: 100%; }
.warn { color: var(--warn); font-size: 12px; display: none; }
.warn.show { display: block; margin: 8px 0 0; }
.entries { display: flex; flex-direction: column; gap: 10px; }
.entry-card { border: 1px solid var(--line); border-radius: 9px; padding: 10px 12px 12px; background: #fbfcfd; }
.entry-bar { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 8px; }
.entry-name { font-weight: 650; font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entry-actions { display: flex; flex: none; }
.add { margin-top: 10px; width: 100%; border: 1px dashed #b7c2cf; background: #fff; border-radius: 8px; padding: 9px; cursor: pointer; color: var(--brand); font-weight: 650; }
.add:hover { background: var(--brand-2); }
.sub { margin-top: 14px; padding-top: 12px; border-top: 1px solid var(--line); }
.sub .check { margin-bottom: 10px; }
.starters { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.starters small { color: var(--muted); width: 100%; }
.chip { border: 1px solid var(--line); background: #fff; border-radius: 999px; padding: 5px 10px; cursor: pointer; font-size: 12px; text-align: left; }
.chip:hover { border-color: var(--brand); color: var(--brand); }
.mode-cards { display: flex; flex-direction: column; gap: 8px; margin-bottom: 8px; }
.mode-card { display: block; border: 1.5px solid var(--line); border-radius: 9px; padding: 10px 12px 10px 36px; cursor: pointer; position: relative; }
.mode-card input { position: absolute; left: 12px; top: 13px; }
.mode-card small { display: block; color: var(--muted); margin-top: 2px; line-height: 1.4; }
.mode-card.active { border-color: var(--brand); background: var(--brand-2); }
.order { list-style: none; padding: 0; margin: 0 0 10px; counter-reset: o; }
.order li { display: flex; justify-content: space-between; align-items: center; padding: 5px 4px; border-bottom: 1px solid var(--line); }
.photo-row { display: flex; gap: 14px; align-items: center; margin-bottom: 12px; }
.photo-thumb { width: 84px; height: 84px; background: var(--bg); border: 1px dashed #b7c2cf; display: grid; place-items: center; overflow: hidden; flex: none; color: var(--muted); font-size: 12px; }
.photo-thumb.aspect-passport { height: 112px; }
.photo-thumb.shape-circle { border-radius: 50%; }
.photo-thumb.shape-rounded { border-radius: 12px; }
.photo-thumb img { width: 100%; height: 100%; object-fit: cover; }
.photo-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.tips { margin: 12px 0 0; padding-left: 18px; color: var(--muted); font-size: 12.5px; line-height: 1.6; }

/* design bar + preview */
.design-bar { display: flex; flex-wrap: wrap; gap: 10px 16px; align-items: center; padding: 10px 16px; background: #fff; border-bottom: 1px solid var(--line); }
.tpl-btn { display: flex; align-items: center; gap: 8px; border: 1px solid var(--line); background: #fff; border-radius: 8px; padding: 6px 12px; cursor: pointer; }
.tpl-btn:hover { border-color: var(--brand); }
.tpl-label { color: var(--muted); font-size: 12px; }
.pill { font-size: 11px; font-weight: 700; border-radius: 999px; padding: 2px 8px; }
.pill.ok { background: #e7f6ec; color: var(--ok); }
.pill.mid { background: #fff4e0; color: var(--warn); }
.pill.no { background: #fde8e8; color: var(--danger); }
.pill.info { background: var(--brand-2); color: var(--brand); }
.swatches { display: flex; gap: 6px; }
.swatch { width: 22px; height: 22px; border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px var(--line); cursor: pointer; padding: 0; }
.swatch.active { box-shadow: 0 0 0 2px var(--ink); }
.mini { display: flex; align-items: center; gap: 6px; font-size: 12.5px; color: var(--muted); }
.mini select { border: 1px solid var(--line); border-radius: 7px; padding: 5px 8px; background: #fff; color: var(--ink); }
.meter { display: flex; align-items: center; gap: 10px; padding: 8px 16px; font-size: 12.5px; color: var(--muted); }
.meter-bar { width: 140px; height: 6px; border-radius: 99px; background: #d4dbe4; overflow: hidden; }
.meter-bar span { display: block; height: 100%; background: var(--ok); width: 0; transition: width .2s; }
.meter.over .meter-bar span { background: var(--warn); }
.meter.over .meter-text { color: var(--warn); font-weight: 600; }
.stage { flex: 1; overflow: auto; padding: 8px 24px 40px; }
.scaler { transform-origin: top left; }
.page-wrap { position: relative; width: 210mm; box-shadow: 0 6px 24px rgba(15,23,42,.14); background: #fff; }
.page-break { position: absolute; left: -12px; right: -12px; border-top: 2px dashed #e67e22; pointer-events: none; }
.page-break span { position: absolute; right: 0; top: -20px; background: #e67e22; color: #fff; font-size: 11px; padding: 1px 6px; border-radius: 4px; font-family: system-ui, sans-serif; }

/* dialogs */
.dialog { border: 0; border-radius: 14px; padding: 22px; max-width: 460px; width: calc(100% - 32px); box-shadow: 0 30px 80px rgba(15,23,42,.3); }
.dialog.wide { max-width: 1080px; }
.dialog::backdrop { background: rgba(15,23,42,.45); }
.dialog h3 { margin: 0 0 6px; }
.dialog-head { display: flex; justify-content: space-between; align-items: center; }
.muted { color: var(--muted); margin: 0 0 14px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.crop-stage { display: grid; place-items: center; background: repeating-conic-gradient(#eef1f5 0 25%, #fff 0 50%) 0 0/16px 16px; border-radius: 10px; padding: 14px; }
.crop-stage canvas { cursor: grab; touch-action: none; box-shadow: 0 0 0 2px var(--brand); max-width: 100%; }
.crop-stage canvas:active { cursor: grabbing; }
.crop-tools { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; margin-top: 12px; }
.crop-tools input[type=range] { flex: 1; min-width: 120px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; background: #fff; padding: 7px 10px; cursor: pointer; font-size: 13px; }
.seg button.active { background: var(--brand); color: #fff; }
.steps { padding-left: 20px; line-height: 1.7; }
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; max-height: 70vh; overflow-y: auto; padding: 4px; }
.tpl-card { border: 2px solid var(--line); border-radius: 12px; padding: 10px; cursor: pointer; background: #fff; text-align: left; display: flex; flex-direction: column; gap: 8px; }
.tpl-card:hover { border-color: #9fb3c8; }
.tpl-card.active { border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-2); }
.tpl-thumb { height: 290px; overflow: hidden; border: 1px solid var(--line); border-radius: 6px; position: relative; background: #fff; }
.tpl-thumb > .resume { transform: scale(.25); transform-origin: top left; position: absolute; top: 0; left: 0; pointer-events: none; }
.tpl-card h4 { margin: 0; font-size: 15px; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.tpl-card p { margin: 0; color: var(--muted); font-size: 12.5px; line-height: 1.4; }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(120px); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 9px; transition: transform .25s; z-index: 50; max-width: 90vw; }
.toast.show { transform: translateX(-50%) translateY(0); }

@media (max-width: 900px) {
  .topbar { padding: 0 10px; }
  .save-state { display: none; }
  #btn-docx, #btn-pdf { padding: 8px 10px; }
  .mobile-tabs { display: flex; position: sticky; top: var(--top); z-index: 15; background: #fff; border-bottom: 1px solid var(--line); }
  .mobile-tabs button { flex: 1; border: 0; background: none; padding: 10px; font-weight: 650; color: var(--muted); border-bottom: 2px solid transparent; }
  .mobile-tabs button.active { color: var(--brand); border-bottom-color: var(--brand); }
  .layout { grid-template-columns: 1fr; height: auto; }
  .layout[data-view=edit] .preview-pane, .layout[data-view=preview] .editor { display: none; }
  .editor { border-right: 0; }
  .preview-pane { min-height: calc(100vh - var(--top) - 42px); }
  .stage { padding: 8px 10px 40px; }
  .fld.third { flex-basis: calc(50% - 6px); }
}
@media (max-width: 520px) {
  .brand span:not(.logo) { display: none; }
  .fld.half, .fld.third { flex-basis: 100%; }
}
@media print { .topbar, .mobile-tabs, .layout, .toast, dialog { display: none !important; } }
