:root {
  --ink: #1d1b2e;
  --ink-2: #4b4766;
  --muted: #8a87a0;
  --line: #e6e4ef;
  --bg: #f4f3f9;
  --surface: #ffffff;
  --accent: #7c5cff;
  --accent-2: #5b3ff0;
  --accent-soft: #efeaff;
  --danger: #e5484d;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(29, 27, 46, .06), 0 6px 24px rgba(29, 27, 46, .08);
  --font: Inter, system-ui, -apple-system, "Segoe UI", sans-serif;
  color-scheme: light;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.4 var(--font); color: var(--ink); background: var(--bg); overflow: hidden; }
[hidden] { display: none !important; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.skip { position: absolute; left: -999px; top: 0; z-index: 99; background: var(--surface); padding: 8px; }
.skip:focus { left: 8px; }
.muted { color: var(--muted); font-weight: 400; }
.spacer { flex: 1; }

/* ---------- buttons ---------- */
.btn { display: inline-flex; align-items: center; gap: 6px; height: 36px; padding: 0 14px; border-radius: 8px;
  border: 1px solid transparent; background: var(--surface); font-weight: 600; white-space: nowrap; }
.btn.primary { background: var(--accent); color: #fff; }
.btn.primary:hover { background: var(--accent-2); }
.btn.ghost { background: transparent; border-color: var(--line); }
.btn.ghost:hover { background: var(--accent-soft); }
.btn.small { height: 28px; padding: 0 10px; font-size: 12px; }
.btn.danger { color: var(--danger); }
.icon-btn { display: inline-grid; place-items: center; width: 34px; height: 34px; border-radius: 8px;
  border: 0; background: transparent; }
.icon-btn:hover:not(:disabled) { background: var(--accent-soft); }
.icon-btn.on { background: var(--accent-soft); color: var(--accent-2); }
.icon-btn:disabled { opacity: .35; cursor: default; }

/* ---------- brand ---------- */
.brand { display: flex; align-items: center; gap: 10px; font-weight: 800; font-size: 20px; letter-spacing: -.02em; }
.brand small { font-size: 11px; font-weight: 600; color: var(--accent-2); background: var(--accent-soft);
  padding: 2px 8px; border-radius: 99px; letter-spacing: 0; }
.logo { width: 28px; height: 28px; border-radius: 8px; background:
  linear-gradient(135deg, var(--accent) 0 50%, transparent 50%), repeating-linear-gradient(45deg, #ff6fae 0 4px, #ffd166 4px 8px); }
.brand.small .logo { width: 24px; height: 24px; }

/* ---------- home ---------- */
.home { height: 100%; overflow: auto; }
.home-top { display: flex; align-items: center; justify-content: space-between; padding: 16px 32px;
  background: var(--surface); border-bottom: 1px solid var(--line); position: sticky; top: 0; z-index: 2; }
.home-actions { display: flex; gap: 8px; }
.hero { padding: 48px 32px 32px; text-align: center;
  background: radial-gradient(1200px 300px at 50% 0, #e8e0ff, transparent), var(--bg); }
.hero h1 { font-size: 40px; letter-spacing: -.03em; margin: 0 0 28px; }
.preset-row { display: flex; gap: 14px; justify-content: center; flex-wrap: wrap; }
.preset { display: flex; flex-direction: column; align-items: center; gap: 8px; width: 120px; padding: 12px 8px;
  border-radius: 12px; border: 0; background: transparent; }
.preset:hover { background: var(--surface); box-shadow: var(--shadow); }
.preset .shape { background: linear-gradient(135deg, #c9b8ff, #ffc2dc); border-radius: 6px; }
.preset span { font-size: 12px; font-weight: 600; }
.preset small { font-size: 11px; color: var(--muted); }
.home-section { padding: 8px 32px 32px; }
.home-section h2 { font-size: 18px; margin: 12px 0 14px; }
.tpl-grid, .recent-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 18px; }
.card { position: relative; border: 0; padding: 0; background: transparent; text-align: left; }
.card .thumb { aspect-ratio: 4/3; border-radius: 10px; background: #e9e7f2; display: grid; place-items: center;
  overflow: hidden; box-shadow: var(--shadow); transition: transform .15s; }
.card:hover .thumb { transform: translateY(-2px); }
.card .thumb img, .card .thumb canvas { max-width: 92%; max-height: 92%; box-shadow: 0 2px 8px rgba(0,0,0,.15); }
.card .name { display: block; font-weight: 600; margin-top: 8px; font-size: 13px; }
.card .meta { font-size: 11px; color: var(--muted); }
.card .del { position: absolute; right: 6px; top: 6px; opacity: 0; background: var(--surface); }
.card:hover .del, .card:focus-within .del { opacity: 1; }
.empty { color: var(--muted); padding: 24px; border: 2px dashed var(--line); border-radius: 12px; text-align: center; }

/* ---------- editor layout ---------- */
.editor { display: grid; height: 100%;
  grid-template: "top top top" 56px "rail panel work" 1fr / 76px 320px 1fr; }
.editor.panel-closed { grid-template-columns: 76px 0 1fr; }
.topbar { grid-area: top; display: flex; align-items: center; gap: 6px; padding: 0 12px;
  background: linear-gradient(90deg, #2a1f63, #5b3ff0 60%, #8f5cff); color: #fff; }
.topbar .icon-btn:hover:not(:disabled) { background: rgba(255,255,255,.15); }
.topbar .btn.ghost { color: #fff; border-color: rgba(255,255,255,.35); }
.topbar .btn.ghost:hover { background: rgba(255,255,255,.15); }
.topbar .btn.primary { background: #fff; color: var(--accent-2); }
.doc-name { background: transparent; border: 1px solid transparent; border-radius: 6px; color: #fff; padding: 4px 8px;
  font-weight: 600; width: 260px; }
.doc-name:hover, .doc-name:focus { border-color: rgba(255,255,255,.4); outline: none; }
.save-state { font-size: 12px; opacity: .75; min-width: 60px; }
.rail { grid-area: rail; background: var(--surface); border-right: 1px solid var(--line); display: flex;
  flex-direction: column; gap: 2px; padding: 8px 6px; }
.rail-btn { display: flex; flex-direction: column; align-items: center; gap: 4px; padding: 10px 2px; border: 0;
  background: transparent; border-radius: 10px; font-size: 11px; font-weight: 500; color: var(--ink-2); }
.rail-btn:hover { background: var(--bg); }
.rail-btn.active { background: var(--accent-soft); color: var(--accent-2); }
.panel { grid-area: panel; background: var(--surface); border-right: 1px solid var(--line); overflow: auto; padding: 16px; }
.editor.panel-closed .panel { display: none; }
.panel h3 { font-size: 13px; margin: 18px 0 10px; color: var(--ink-2); }
.panel h3:first-child { margin-top: 0; }
.search { width: 100%; height: 38px; border: 1px solid var(--line); border-radius: 8px; padding: 0 12px; background: var(--bg); }
.grid-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.tile { aspect-ratio: 1; border: 0; border-radius: 8px; background: var(--bg); display: grid; place-items: center; padding: 10px; }
.tile:hover { background: var(--accent-soft); }
.tile svg { width: 100%; height: 100%; }
.tile img { max-width: 100%; max-height: 100%; object-fit: contain; }
.text-preset { display: block; width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface);
  border-radius: 8px; padding: 12px 14px; margin-bottom: 8px; }
.text-preset:hover { border-color: var(--accent); }
.tpl-tile { border: 0; padding: 0; background: transparent; text-align: left; }
.tpl-tile .thumb { border-radius: 8px; overflow: hidden; background: var(--bg); display: grid; place-items: center; aspect-ratio: 1; }
.tpl-tile canvas { max-width: 100%; max-height: 100%; }
.tpl-tile span { font-size: 12px; font-weight: 500; }
.swatches { display: flex; flex-wrap: wrap; gap: 6px; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 1px solid rgba(0,0,0,.12); padding: 0; }
.swatch.lg { width: 44px; height: 44px; border-radius: 8px; }
.swatch.sel { outline: 2px solid var(--accent); outline-offset: 2px; }
.swatch.add { display: grid; place-items: center; background: conic-gradient(red, yellow, lime, cyan, blue, magenta, red); position: relative; overflow: hidden; }
.swatch.add input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.drop-hint { border: 2px dashed var(--line); border-radius: 10px; padding: 20px; text-align: center; color: var(--muted); }
.layer-row { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border-radius: 8px; border: 1px solid transparent;
  background: transparent; width: 100%; text-align: left; }
.layer-row:hover { background: var(--bg); }
.layer-row.sel { border-color: var(--accent); background: var(--accent-soft); }
.layer-row .lname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 13px; }
.layer-row .kind { font-size: 10px; color: var(--muted); text-transform: uppercase; width: 38px; }
.field { display: flex; align-items: center; gap: 8px; margin: 8px 0; font-size: 12px; color: var(--ink-2); }
.field > span { width: 84px; }
.field input[type=range] { flex: 1; accent-color: var(--accent); }
.field input[type=number], .field select, .num { width: 70px; height: 30px; border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; background: var(--surface); }

/* ---------- workspace ---------- */
.workspace { grid-area: work; position: relative; display: flex; flex-direction: column; min-width: 0; min-height: 0; }
.ctxbar { height: 52px; flex: none; display: flex; align-items: center; gap: 4px; padding: 0 12px; background: var(--surface);
  border-bottom: 1px solid var(--line); flex-wrap: nowrap; overflow-x: auto; overflow-y: hidden; scrollbar-width: thin; }
.ctxbar > * { flex: none; }
.ctxbar .spacer { flex: 1 0 8px; }
.ctxbar .sep { width: 1px; height: 24px; background: var(--line); margin: 0 6px; }
.ctxbar select { height: 32px; border: 1px solid var(--line); border-radius: 6px; padding: 0 6px; background: var(--surface); }
.ctxbar .font-select { width: 160px; }
.color-chip { width: 32px; height: 32px; border-radius: 8px; border: 1px solid var(--line); position: relative; overflow: hidden; padding: 0; }
.color-chip input { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; }
.color-chip.text-chip { background: var(--surface) !important; display: grid; place-items: center; font-weight: 800; }
.color-chip.text-chip i { position: absolute; bottom: 4px; left: 6px; right: 6px; height: 4px; border-radius: 2px; }
.ctx-hint { color: var(--muted); font-size: 13px; }
.stage { flex: 1; overflow: auto; position: relative; background: var(--bg); outline: none; padding: 40px 60px 120px; }
.stage.drop { box-shadow: inset 0 0 0 3px var(--accent); }
.pages { display: flex; flex-direction: column; align-items: center; gap: 22px; min-width: min-content; margin: 0 auto; }
.page-head { display: flex; align-items: center; gap: 4px; align-self: stretch; font-size: 12px; color: var(--ink-2); }
.page-head b { font-weight: 600; }
.page-head .icon-btn { width: 28px; height: 28px; }
.page-wrap { position: relative; box-shadow: 0 2px 10px rgba(29,27,46,.12); flex: none; }
.page-wrap.active { box-shadow: 0 0 0 2px var(--accent), 0 2px 10px rgba(29,27,46,.12); }
.page { position: absolute; left: 0; top: 0; transform-origin: 0 0; overflow: hidden; }
.el { position: absolute; transform-origin: 50% 50%; }
.el .inner { position: absolute; inset: 0; }
.el svg { position: absolute; inset: 0; overflow: visible; }
.el img { width: 100%; height: 100%; object-fit: cover; display: block; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.el.text .inner { white-space: pre-wrap; overflow-wrap: break-word; word-break: normal; }
.el.text.editing .inner { outline: none; cursor: text; user-select: text; }
.el.locked { cursor: not-allowed; }
.add-page { margin-top: 4px; }

/* overlay (screen-space, unscaled) */
.overlay { position: absolute; inset: 0; pointer-events: none; }
.sel-box { position: absolute; border: 2px solid var(--accent); transform-origin: 50% 50%; }
.sel-box.multi { border-style: dashed; }
.sel-box.hover { border-width: 1.5px; opacity: .6; }
.sel-box.locked { border-color: var(--muted); }
.handle { position: absolute; width: 12px; height: 12px; margin: -6px 0 0 -6px; background: #fff; border: 1.5px solid var(--accent);
  border-radius: 50%; pointer-events: auto; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.handle.edge { border-radius: 6px; }
.handle.edge.n, .handle.edge.s { width: 22px; height: 8px; margin: -4px 0 0 -11px; }
.handle.edge.e, .handle.edge.w { width: 8px; height: 22px; margin: -11px 0 0 -4px; }
.handle.rot { width: 22px; height: 22px; margin: -11px 0 0 -11px; display: grid; place-items: center; cursor: grab; }
.handle.rot::after { content: "⟳"; font-size: 13px; color: var(--accent-2); line-height: 1; }
.guide { position: absolute; background: #ff3d8b; }
.guide.x { width: 1px; top: 0; bottom: 0; }
.guide.y { height: 1px; left: 0; right: 0; }
.marquee { position: absolute; border: 1px solid var(--accent); background: rgba(124,92,255,.08); }
.badge { position: absolute; background: var(--ink); color: #fff; font-size: 11px; padding: 2px 6px; border-radius: 4px; white-space: nowrap; transform: translate(-50%, 0); }

.bottombar { height: 44px; display: flex; align-items: center; gap: 6px; padding: 0 12px; background: var(--surface);
  border-top: 1px solid var(--line); }
.bottombar input[type=range] { width: 140px; accent-color: var(--accent); }
.page-info { font-size: 12px; color: var(--ink-2); }

/* ---------- modal / toast / menu ---------- */
.modal-back { position: fixed; inset: 0; background: rgba(20,18,35,.45); display: grid; place-items: center; z-index: 50; }
.modal { background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); width: min(520px, 92vw); max-height: 86vh;
  overflow: auto; padding: 22px; }
.modal h2 { margin: 0 0 14px; font-size: 18px; }
.modal .row { display: flex; gap: 10px; align-items: center; margin: 10px 0; flex-wrap: wrap; }
.modal .row label { min-width: 70px; font-size: 13px; color: var(--ink-2); }
.modal input[type=number], .modal select { height: 36px; border: 1px solid var(--line); border-radius: 8px; padding: 0 10px; }
.modal .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 18px; }
.modal table { width: 100%; border-collapse: collapse; font-size: 13px; }
.modal td { padding: 4px 6px; border-bottom: 1px solid var(--line); }
.modal kbd, .modal code { background: var(--bg); border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-size: 12px; font-family: "Roboto Mono", monospace; }
.toast { position: fixed; left: 50%; bottom: 64px; transform: translateX(-50%) translateY(20px); background: var(--ink); color: #fff;
  padding: 10px 16px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; z-index: 60; }
.toast.show { opacity: 1; transform: translateX(-50%); }
.menu { position: fixed; z-index: 55; background: var(--surface); border-radius: 10px; box-shadow: var(--shadow);
  border: 1px solid var(--line); padding: 6px; min-width: 200px; }
.menu button { display: flex; justify-content: space-between; width: 100%; border: 0; background: transparent; padding: 7px 10px;
  border-radius: 6px; text-align: left; font-size: 13px; }
.menu button:hover { background: var(--accent-soft); }
.menu kbd { color: var(--muted); font-size: 11px; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }

@media (max-width: 900px) {
  .editor { grid-template-columns: 64px 0 1fr; }
  .editor .panel { display: none; }
  .editor.panel-open-mobile { grid-template-columns: 64px 260px 1fr; }
  .editor.panel-open-mobile .panel { display: block; }
  .doc-name { width: 120px; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
