:root {
  --accent: #2f5bea;
  --accent-dark: #2447bd;
  --paper: #f6f4ef;
  --side: #ece8df;
  --ink: #1b2430;
  --muted: #5b6472;
  --hair: #d9d4c7;
  --danger: #b3261e;
  --ok: #1d7a4d;
  --radius: 12px;
  --serif: ui-serif, 'New York', Georgia, 'Times New Roman', serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font: 16px/1.4 -apple-system, 'Helvetica Neue', Helvetica, Arial, sans-serif;
  -webkit-text-size-adjust: 100%;
}
h1, h2, h3 { margin: 0; }
a { color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--hair); width: 100%; margin: 4px 0; }
button { font: inherit; color: inherit; cursor: pointer; }
button:disabled { opacity: 0.5; cursor: default; }
:focus-visible { outline: 3px solid color-mix(in srgb, var(--accent) 60%, white); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------- controls ---------- */
.btn {
  min-height: 44px; padding: 0 16px; border: 1px solid var(--hair); border-radius: var(--radius);
  background: #fff; font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  text-decoration: none; color: var(--ink);
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover:not(:disabled) { background: var(--accent-dark); }
.btn.danger, button.danger { color: var(--danger); }
.btn.small { min-height: 36px; padding: 0 12px; font-size: 14px; }
.btn.ghost { background: transparent; border-style: dashed; color: var(--accent); }
.btn.wide { width: 100%; }
.btn.on, .tool.on { background: var(--ink); border-color: var(--ink); color: #fff; }
.icon { min-width: 44px; min-height: 44px; border: 0; border-radius: var(--radius); background: transparent; font-size: 20px; }
.icon:hover:not(:disabled) { background: rgba(0, 0, 0, 0.06); }
.row-btns { display: flex; gap: 10px; flex-wrap: wrap; }
.row-btns.end { justify-content: flex-end; }
.spacer { flex: 1; }

input[type='text'], input[type='email'], input[type='password'], input[type='url'], input[type='search'], input[type='number'], select, textarea {
  font: inherit; color: inherit; min-height: 44px; width: 100%; padding: 0 12px;
  border: 1px solid var(--hair); border-radius: var(--radius); background: #fff;
}
input[type='color'] { width: 44px; height: 44px; padding: 2px; border: 1px solid var(--hair); border-radius: var(--radius); background: #fff; cursor: pointer; }
input[type='checkbox'] { width: 22px; height: 22px; accent-color: var(--accent); }
input[type='range'] { width: 110px; accent-color: var(--accent); }
.field { display: grid; gap: 6px; font-size: 14px; font-weight: 600; color: var(--muted); }
.check { display: flex; align-items: center; gap: 10px; font-size: 15px; min-height: 44px; }
.stack { display: grid; gap: 14px; }
.hint { color: var(--muted); font-size: 14px; margin: 0; }
.error { color: var(--danger); margin: 0; font-size: 15px; }
.error:empty { display: none; }
.success { color: var(--ok); margin: 0; }
.success:empty { display: none; }
.tagline { color: var(--muted); margin: 4px 0 16px; }
.brand { font: 600 28px var(--serif); }

/* ---------- sign in ---------- */
.auth { min-height: 100dvh; display: grid; place-items: center; padding: 24px 16px; }
.auth-card { width: min(440px, 100%); background: #fff; border: 1px solid var(--hair); border-radius: 18px; padding: 28px; display: grid; gap: 14px; box-shadow: 0 10px 40px rgba(27, 36, 48, 0.08); }
.center-card { min-height: 100dvh; display: grid; place-content: center; justify-items: center; gap: 14px; text-align: center; padding: 16px; }
.spinner { width: 32px; height: 32px; border-radius: 50%; border: 3px solid var(--hair); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.tabs { display: flex; gap: 4px; flex-wrap: wrap; border-bottom: 1px solid var(--hair); padding-bottom: 8px; }
.tabs button { border: 0; background: transparent; padding: 8px 12px; border-radius: 10px; min-height: 40px; color: var(--muted); font-weight: 500; }
.tabs button[aria-selected='true'] { background: var(--ink); color: #fff; }
.tabs.small { border: 0; padding: 0; }
.server summary { cursor: pointer; color: var(--muted); font-size: 14px; padding: 6px 0; }
.recovery { font: 600 20px/1.7 ui-monospace, Menlo, Consolas, monospace; letter-spacing: 0.05em; background: var(--paper); border: 1px dashed var(--muted); border-radius: var(--radius); padding: 14px; word-break: break-all; user-select: all; }

/* ---------- library ---------- */
.library, .admin { min-height: 100dvh; display: flex; flex-direction: column; }
.lib-top { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 10px 16px; border-bottom: 1px solid var(--hair); background: var(--paper); position: sticky; top: 0; z-index: 5; }
.lib-top input[type='search'] { width: min(280px, 100%); flex: 1 1 160px; }
.sync-status { color: var(--muted); font-size: 14px; margin-right: auto; }
.sync-status.bad, .ed-status.bad { color: var(--danger); }
.lib-body { display: flex; flex: 1; min-height: 0; }
.folders { width: 240px; flex: none; background: var(--side); padding: 14px 10px; display: grid; align-content: start; gap: 4px; }
.folder { display: flex; align-items: center; border-radius: 10px; }
.folder.on { background: #fff; }
.folder-name { flex: 1; display: flex; justify-content: space-between; gap: 8px; align-items: center; min-height: 44px; padding: 0 12px; border: 0; background: transparent; text-align: left; border-radius: 10px; }
.folder.on .folder-name { font-weight: 600; }
.count { color: var(--muted); font-size: 14px; }
.folder .icon { min-width: 36px; min-height: 36px; font-size: 18px; }
.notes { flex: 1; min-width: 0; padding: 18px 20px 40px; }
.section { font: 600 30px var(--serif); margin-bottom: 16px; }
.grid, .tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 20px; }
.card { position: relative; }
.card-open, .tpl { width: 100%; border: 0; background: transparent; padding: 0; text-align: left; display: grid; gap: 8px; border-radius: 14px; }
.thumb-wrap { border: 1px solid var(--hair); border-radius: 12px; overflow: hidden; background: #fff; aspect-ratio: 794 / 1123; box-shadow: 0 1px 3px rgba(27, 36, 48, 0.08); }
.thumb-wrap canvas { width: 100%; height: 100%; display: block; }
.thumb { background: #fff; }
.tpl.on .thumb-wrap { border: 3px solid var(--accent); }
.card-title { font-weight: 600; padding: 0 4px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.card-meta { color: var(--muted); font-size: 13px; padding: 0 4px; }
.card-more { position: absolute; top: 6px; right: 6px; background: rgba(255, 255, 255, 0.9); min-width: 36px; min-height: 36px; border: 1px solid var(--hair); font-size: 18px; }
.empty { display: grid; justify-items: center; gap: 12px; padding: 60px 0; color: var(--muted); }
.tpl-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); max-height: 56vh; overflow: auto; padding: 4px; }

/* ---------- menus, toasts, dialogs ---------- */
.menu { position: fixed; z-index: 50; min-width: 220px; max-height: 70vh; overflow: auto; background: #fff; border: 1px solid var(--hair); border-radius: 14px; padding: 6px; box-shadow: 0 12px 36px rgba(27, 36, 48, 0.2); display: grid; }
.menu button { border: 0; background: transparent; text-align: left; padding: 0 12px; min-height: 42px; border-radius: 8px; white-space: pre; }
.menu button:hover:not(:disabled) { background: var(--paper); }
#toasts { position: fixed; left: 50%; bottom: 20px; transform: translateX(-50%); display: grid; gap: 8px; z-index: 100; width: min(520px, calc(100vw - 24px)); }
.toast { background: var(--ink); color: #fff; padding: 12px 16px; border-radius: 12px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.25); }
.overlay { position: fixed; inset: 0; background: rgba(27, 36, 48, 0.45); z-index: 60; display: grid; place-items: center; padding: 16px; overflow: auto; }
.modal { width: min(480px, 100%); max-height: calc(100dvh - 32px); overflow: auto; background: var(--paper); border-radius: 18px; padding: 20px; display: grid; gap: 14px; }
.modal.wide { width: min(780px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: center; }
.modal-head h2 { font: 600 24px var(--serif); }

/* ---------- admin ---------- */
.admin-body { padding: 20px; display: grid; gap: 20px; max-width: 960px; width: 100%; margin: 0 auto; }
.panel { background: #fff; border: 1px solid var(--hair); border-radius: 16px; padding: 20px; display: grid; gap: 14px; }
.panel h2 { font: 600 22px var(--serif); }
.users { width: 100%; border-collapse: collapse; font-size: 15px; }
.users th { text-align: left; color: var(--muted); font-weight: 600; padding: 6px 8px; border-bottom: 1px solid var(--hair); }
.users td { padding: 8px; border-bottom: 1px solid var(--hair); }
.users .actions { display: flex; gap: 6px; flex-wrap: wrap; }

/* ---------- editor ---------- */
.editor { display: flex; flex-direction: column; height: 100dvh; }
.ed-top { background: var(--paper); border-bottom: 1px solid var(--hair); padding: 8px 12px; display: grid; gap: 8px; z-index: 5; }
.ed-row { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.ed-title { flex: 1 1 200px; min-width: 140px; width: auto; font: 600 18px var(--serif); border-color: transparent; background: transparent; }
.ed-title:focus { background: #fff; border-color: var(--hair); }
.ed-status { color: var(--muted); font-size: 14px; min-width: 70px; }
.tool { min-height: 44px; padding: 0 14px; border: 1px solid var(--hair); border-radius: var(--radius); background: #fff; font-weight: 500; }
.swatches { display: flex; gap: 6px; align-items: center; }
.swatch { width: 32px; height: 32px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 0 0 1px var(--hair); padding: 0; }
.swatch.on { box-shadow: 0 0 0 3px var(--accent); }
.size { display: flex; align-items: center; gap: 8px; font-size: 14px; color: var(--muted); }
.ed-inspector { display: flex; gap: 10px; align-items: end; flex-wrap: wrap; padding: 8px 10px; background: #fff; border: 1px solid var(--hair); border-radius: var(--radius); }
.ed-inspector .field { font-size: 12px; gap: 2px; }
.ed-inspector select, .ed-inspector input[type='number'], .ed-inspector input[type='text'] { min-height: 36px; width: auto; max-width: 160px; }
.ed-inspector input[type='color'] { height: 36px; width: 44px; }
.ed-inspector strong { align-self: center; }

.ed-scroll { flex: 1; overflow: auto; background: #e9e5db; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; }
.ed-pages { display: flex; flex-direction: column; align-items: center; gap: 20px; padding: 20px 12px 80px; }
.page { position: relative; flex: none; background: #fff; box-shadow: 0 2px 12px rgba(27, 36, 48, 0.15); border-radius: 2px; }
.scaler { position: absolute; left: 0; top: 0; width: 794px; height: 1123px; transform-origin: 0 0; }
.scaler canvas { position: absolute; left: 0; top: 0; width: 794px; height: 1123px; }
.scaler .live { touch-action: none; cursor: crosshair; }
.editing .scaler .live { pointer-events: none; }
.items { position: absolute; inset: 0; display: none; }
.items.on { display: block; }
.item { position: absolute; }
.item:not(.selected) { outline: 1px dashed rgba(47, 91, 234, 0.55); }
.item.selected { outline: 2px solid var(--accent); }
.handle { display: none; position: absolute; align-items: center; justify-content: center; background: var(--accent); color: #fff; touch-action: none; user-select: none; font-size: 14px; z-index: 2; }
.item.selected .handle { display: flex; }
.handle.move { top: -36px; right: 0; width: 48px; height: 30px; border-radius: 15px; cursor: grab; }
.handle.resize { right: -16px; bottom: -16px; width: 30px; height: 30px; border-radius: 50%; cursor: nwse-resize; }
.item.img img { width: 100%; height: 100%; display: block; user-select: none; -webkit-user-drag: none; touch-action: none; cursor: grab; }
.obj-text { display: block; width: 100%; min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent; resize: none; overflow: hidden; line-height: 1.25; outline: none; }
.sticky { width: 100%; height: 100%; padding: 12px; border-radius: 6px; box-shadow: 0 2px 5px rgba(0, 0, 0, 0.18); }
.sticky .obj-text { height: 100%; }
.checklist { width: 100%; }
.checklist .row { display: flex; align-items: center; gap: 10px; min-height: 0; }
.checklist input[type='checkbox'] { flex: none; margin: 0; }
.cl-text { flex: 1; min-width: 0; min-height: 0; padding: 0; border: 0; border-radius: 0; background: transparent; font: inherit; color: inherit; outline: none; }
.table { display: grid; width: 100%; }
.table input { min-width: 0; min-height: 0; padding: 0 6px; border: 1px solid; border-radius: 0; background: transparent; font: inherit; color: inherit; outline: none; }
.stamp { display: flex; align-items: center; justify-content: center; width: 100%; height: 100%; border: 3px solid; border-radius: 8px; overflow: hidden; }

/* ---------- small screens ---------- */
@media (max-width: 720px) {
  .lib-body { flex-direction: column; }
  .folders { width: 100%; display: flex; overflow-x: auto; padding: 8px; }
  .folder { flex: none; }
  .notes { padding: 14px 12px 40px; }
  .grid { grid-template-columns: repeat(2, 1fr); gap: 14px; }
}

/* ---------- printing ---------- */
#print-root { display: none; }
@media print {
  @page { size: A4; margin: 0; }
  body.printing > :not(#print-root) { display: none !important; }
  body.printing #print-root { display: block; }
  #print-root img { display: block; width: 210mm; height: 297mm; break-after: page; page-break-after: always; }
}
