:root {
  --bg: #f6f7f9; --surface: #ffffff; --surface-2: #eef0f4; --text: #161a22; --muted: #667085; --border: #e1e5ec;
  --accent: #4f46e5; --accent-ink: #ffffff; --accent-soft: #e8e7fd; --danger: #dc2626; --canvas: #d9dde5;
  --shadow: 0 1px 2px rgba(16,24,40,.08), 0 4px 14px rgba(16,24,40,.08);
  --radius: 12px; color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1218; --surface: #181c25; --surface-2: #222733; --text: #e8ebf2; --muted: #8b93a7; --border: #2a3040;
    --accent: #818cf8; --accent-ink: #0f1218; --accent-soft: #272a52; --danger: #f87171; --canvas: #0a0c10;
    --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.4); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1218; --surface: #181c25; --surface-2: #222733; --text: #e8ebf2; --muted: #8b93a7; --border: #2a3040;
  --accent: #818cf8; --accent-ink: #0f1218; --accent-soft: #272a52; --danger: #f87171; --canvas: #0a0c10;
  --shadow: 0 1px 2px rgba(0,0,0,.4), 0 6px 18px rgba(0,0,0,.4); color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); overscroll-behavior: none; }
body { font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, "Noto Sans", sans-serif; -webkit-tap-highlight-color: transparent; }
#app, #lib-root, #editor-root { height: 100vh; height: 100dvh; }
button { font: inherit; color: inherit; cursor: pointer; }
a { color: inherit; text-decoration: none; }
.grow { flex: 1 1 auto; }
.ic { display: inline-flex; align-items: center; justify-content: center; flex: none; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

#bluelight { position: fixed; inset: 0; background: #ff9a2a; mix-blend-mode: multiply; pointer-events: none; z-index: 9999; opacity: 0; }

/* generic controls */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); font-weight: 500; }
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { background: var(--danger); color: #fff; border-color: transparent; }
.icon-btn { display: inline-grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; border: 0; background: transparent; flex: none; }
.icon-btn:hover:not(:disabled) { background: var(--surface-2); }
.icon-btn:disabled { opacity: .35; cursor: default; }
.icon-btn.sm { width: 28px; height: 28px; }
.icon-btn.danger:hover { color: var(--danger); }
.input { width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--border); background: var(--surface); color: var(--text); font: inherit; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 5px 11px; border-radius: 999px; border: 1px solid var(--border); background: var(--surface); font-size: 13px; white-space: nowrap; }
.chip.toggle.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.chip.big { font-size: 18px; padding: 4px 12px; }
.hint { color: var(--muted); font-size: 13px; margin: 0; }
.stack { display: grid; gap: 12px; }
.row { display: flex; gap: 8px; flex-wrap: wrap; }
.row3 { display: grid; grid-template-columns: 1fr 1fr 90px; gap: 10px; }
.field { display: grid; gap: 6px; } .field > span { font-size: 13px; font-weight: 600; color: var(--muted); }
.check { display: flex; gap: 10px; align-items: flex-start; font-size: 14px; }
.check input { margin-top: 3px; width: 18px; height: 18px; accent-color: var(--accent); }
hr { border: 0; border-top: 1px solid var(--border); width: 100%; margin: 4px 0; }
.howto { padding-left: 18px; margin: 0; display: grid; gap: 8px; }
.fatal { padding: 40px; max-width: 520px; margin: 0 auto; }

/* modal */
dialog.modal { border: 0; padding: 0; border-radius: 16px; background: var(--surface); color: var(--text); width: min(460px, calc(100vw - 24px)); max-height: calc(100dvh - 24px); box-shadow: 0 20px 60px rgba(0,0,0,.35); overflow: hidden; }
dialog.modal[open] { display: flex; flex-direction: column; }
dialog.modal.wide { width: min(720px, calc(100vw - 24px)); }
dialog.modal::backdrop { background: rgba(8,10,16,.5); backdrop-filter: blur(2px); }
.m-head { padding: 16px 20px 0; } .m-head h3 { margin: 0; font-size: 17px; }
.m-body { padding: 14px 20px; overflow: auto; flex: 1 1 auto; }
.m-foot { padding: 12px 20px 16px; display: flex; justify-content: flex-end; gap: 8px; }
.m-msg { margin: 0; }
.settings .slider { display: flex; align-items: center; gap: 10px; } .settings .slider input { flex: 1; accent-color: var(--accent); }

/* popup menu */
.popmenu { position: fixed; z-index: 1000; min-width: 210px; max-width: calc(100vw - 16px); max-height: calc(100dvh - 16px); overflow: auto; padding: 6px; background: var(--surface); border: 1px solid var(--border); border-radius: 12px; box-shadow: var(--shadow); }
.pm-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 9px 10px; border: 0; background: transparent; border-radius: 8px; text-align: left; }
.pm-item:hover, .pm-item:focus-visible { background: var(--surface-2); }
.pm-item.danger { color: var(--danger); } .pm-label { flex: 1; } .ic-gap { width: 18px; }
.pm-sep { height: 1px; background: var(--border); margin: 5px 4px; }
.pm-title { padding: 6px 10px 2px; font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.emoji-menu { min-width: 0; }
.emoji-grid { display: grid; grid-template-columns: repeat(8, 38px); gap: 2px; }
.emoji-grid button { width: 38px; height: 38px; font-size: 22px; border: 0; background: transparent; border-radius: 8px; }
.emoji-grid button:hover { background: var(--surface-2); }

/* toasts */
#toasts { position: fixed; left: 0; right: 0; bottom: 18px; display: grid; justify-items: center; gap: 8px; z-index: 2000; pointer-events: none; padding: 0 12px; }
.toast { pointer-events: auto; display: flex; gap: 14px; align-items: center; padding: 10px 16px; border-radius: 12px; background: #1c2230; color: #fff; box-shadow: var(--shadow); max-width: 520px; font-size: 14px; }
.toast-act { background: transparent; border: 0; color: #a5b4fc; font-weight: 600; padding: 0; }

/* ---------- library ---------- */
.lib { display: grid; grid-template-columns: 250px 1fr; height: 100%; }
.side { display: flex; flex-direction: column; gap: 2px; padding: 14px 10px; border-right: 1px solid var(--border); background: var(--surface); overflow: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 4px 8px 14px; font-size: 18px; }
.nav { display: flex; align-items: center; gap: 10px; padding: 9px 12px; border-radius: 10px; border: 0; background: transparent; width: 100%; text-align: left; }
.nav:hover { background: var(--surface-2); } .nav.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.nav small { color: var(--muted); } .nav.sub { padding-top: 6px; padding-bottom: 6px; font-size: 14px; }
.nav-title { padding: 14px 12px 4px; font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); font-weight: 600; }
.side-scrim { display: none; }
.lib-main { display: flex; flex-direction: column; min-width: 0; height: 100%; }
.lib-top { display: flex; align-items: center; gap: 8px; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.menu-btn { display: none; }
.crumbs { display: flex; align-items: center; gap: 6px; font-size: 17px; min-width: 0; white-space: nowrap; overflow: hidden; }
.crumbs a { color: var(--muted); } .crumbs a:hover { color: var(--text); } .crumbs .sep { color: var(--muted); }
.search { display: flex; align-items: center; gap: 8px; padding: 0 12px; height: 38px; border-radius: 10px; background: var(--surface-2); width: min(280px, 36vw); }
.search input { border: 0; background: transparent; outline: 0; color: var(--text); font: inherit; width: 100%; min-width: 0; }
.lib-body { flex: 1; overflow: auto; padding: 18px 18px 90px; align-content: start; }
.lib-body.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 16px; }
.lib-body.list { display: grid; gap: 6px; }
.card { position: relative; border-radius: var(--radius); background: var(--surface); border: 1px solid var(--border); overflow: hidden; cursor: pointer; transition: box-shadow .15s, transform .15s; }
.card:hover { box-shadow: var(--shadow); }
.thumb { aspect-ratio: 3 / 4; background: var(--surface-2); display: grid; place-items: center; position: relative; overflow: hidden; color: var(--muted); }
.thumb img { width: 100%; height: 100%; object-fit: cover; object-position: top; background: #fff; }
.thumb.folder { color: var(--accent); background: var(--accent-soft); }
.badge { position: absolute; top: 8px; display: grid; place-items: center; width: 24px; height: 24px; border-radius: 50%; background: rgba(0,0,0,.55); color: #fff; }
.badge.star { left: 8px; color: #facc15; } .badge.star svg { fill: currentColor; } .badge.lock { right: 8px; }
.card-info { padding: 10px 40px 11px 12px; min-width: 0; }
.card-title { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-more { position: absolute; right: 4px; bottom: 6px; width: 32px; height: 32px; }
.list .card { display: flex; align-items: center; }
.list .thumb { aspect-ratio: auto; width: 54px; height: 64px; flex: none; }
.list .card-info { flex: 1; padding: 8px 48px 8px 14px; }
.list .card-more { bottom: auto; top: 50%; transform: translateY(-50%); }
.list .badge { display: none; }
.empty { display: grid; place-items: center; gap: 10px; padding: 70px 20px; color: var(--muted); text-align: center; grid-column: 1 / -1; }
.empty p { margin: 0; }

/* template picker */
.paper-row { display: flex; gap: 8px; margin-bottom: 12px; flex-wrap: wrap; }
.paper-sw { width: 30px; height: 30px; border-radius: 50%; border: 2px solid var(--border); padding: 0; }
.paper-sw.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.tpl-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(92px, 1fr)); gap: 10px; max-height: 300px; overflow: auto; padding: 2px; }
.tpl-item { display: grid; justify-items: center; gap: 4px; padding: 6px; border: 2px solid transparent; background: transparent; border-radius: 10px; font-size: 12px; }
.tpl-item canvas { border: 1px solid var(--border); border-radius: 3px; }
.tpl-item.on { border-color: var(--accent); background: var(--accent-soft); }
.pick-list { display: grid; gap: 2px; max-height: 50vh; overflow: auto; }
.pick { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 0; background: transparent; border-radius: 8px; text-align: left; }
.pick:hover { background: var(--surface-2); } .pick.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------- editor ---------- */
.editor { position: relative; display: flex; flex-direction: column; height: 100%; background: var(--bg); }
.ed-top { position: relative; z-index: 30; display: flex; align-items: center; gap: 4px; padding: 6px 8px; background: var(--surface); border-bottom: 1px solid var(--border); padding-top: max(6px, env(safe-area-inset-top)); }
.ed-title { border: 0; background: transparent; font-weight: 600; font-size: 16px; padding: 6px 8px; border-radius: 8px; max-width: 38vw; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ed-title:hover { background: var(--surface-2); }
.ed-saved { color: var(--muted); font-size: 12px; margin-left: 4px; }
.ed-top .chip { margin: 0 2px; }
.ed-tools { display: flex; align-items: center; gap: 2px; padding: 5px 8px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.ed-tools::-webkit-scrollbar { display: none; }
.tool { display: grid; place-items: center; width: 42px; height: 38px; border: 0; background: transparent; border-radius: 10px; flex: none; }
.tool:hover { background: var(--surface-2); } .tool.on { background: var(--accent); color: var(--accent-ink); }
.tool-sep { width: 1px; height: 24px; background: var(--border); margin: 0 6px; flex: none; }
.ed-tools .chip { flex: none; margin-left: 4px; }
.ed-props { display: flex; align-items: center; gap: 14px; padding: 6px 12px; min-height: 44px; background: var(--surface); border-bottom: 1px solid var(--border); overflow-x: auto; scrollbar-width: none; }
.ed-props > * { flex: none; }
.swatches { display: flex; align-items: center; gap: 6px; }
.sw { width: 24px; height: 24px; border-radius: 50%; border: 2px solid var(--border); padding: 0; }
.sw.on { outline: 2px solid var(--accent); outline-offset: 2px; }
.sw-custom { width: 28px; height: 28px; padding: 0; border: 0; background: none; border-radius: 50%; overflow: hidden; }
.slider { display: flex; align-items: center; gap: 8px; font-size: 13px; color: var(--muted); }
.slider input[type=range] { width: 140px; accent-color: var(--accent); }
.slider .val { min-width: 34px; color: var(--text); font-variant-numeric: tabular-nums; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 10px; overflow: hidden; }
.seg button { border: 0; background: var(--surface); padding: 5px 11px; font-size: 13px; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }

.ed-vp { flex: 1 1 auto; min-height: 0; overflow: auto; background: var(--canvas); touch-action: none; position: relative; outline: none; -webkit-user-select: none; user-select: none; }
.ed-inner { width: max-content; min-width: 100%; padding: 12px; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.pg { position: relative; flex: none; background: #fff; box-shadow: 0 2px 10px rgba(0,0,0,.28); }
.cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
.cv.live { pointer-events: none; }
.ed-vp[data-tool="pen"] .pg, .ed-vp[data-tool="hl"] .pg, .ed-vp[data-tool="shape"] .pg { cursor: crosshair; }
.ed-vp[data-tool="eraser"] .pg { cursor: none; }
.ed-vp[data-tool="text"] .pg { cursor: text; }
.ed-vp[data-tool="sticker"] .pg { cursor: copy; }
.ed-vp[data-tool="hand"] .pg, .ed-vp.space .pg { cursor: grab; }
.ed-vp.panning .pg { cursor: grabbing; }
.ed-vp.ro .pg { cursor: default; }

.sel-box { position: absolute; border: 1.5px dashed var(--accent); border-radius: 4px; pointer-events: none; z-index: 4; }
.sel-bar { position: absolute; left: 0; top: -48px; display: flex; gap: 2px; padding: 4px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; box-shadow: var(--shadow); pointer-events: auto; }
.sel-box.bar-below .sel-bar { top: auto; bottom: -48px; }
.sel-bar button { width: 34px; height: 34px; display: grid; place-items: center; border: 0; background: transparent; border-radius: 8px; }
.sel-bar button:hover { background: var(--surface-2); } .sel-bar .danger { color: var(--danger); }
.sel-handle { position: absolute; right: -10px; bottom: -10px; width: 20px; height: 20px; border-radius: 50%; background: var(--accent); border: 2px solid #fff; pointer-events: auto; cursor: nwse-resize; touch-action: none; }
.txt-ed { position: absolute; z-index: 6; background: rgba(255,255,255,.55); border: 1px dashed var(--accent); outline: none; resize: none; overflow: hidden; padding: 0; margin: 0; }

.ed-panel { position: absolute; right: 0; top: 52px; bottom: 0; width: 210px; background: var(--surface); border-left: 1px solid var(--border); z-index: 20; display: flex; flex-direction: column; box-shadow: var(--shadow); }
.panel-head { display: flex; align-items: center; padding: 8px 8px 8px 14px; border-bottom: 1px solid var(--border); }
.panel-list { overflow: auto; padding: 10px; display: grid; gap: 12px; align-content: start; }
.pcard { border: 2px solid transparent; border-radius: 10px; padding: 4px; }
.pcard.on { border-color: var(--accent); }
.pcard-img { display: block; width: 100%; border: 0; padding: 0; background: var(--surface-2); border-radius: 6px; overflow: hidden; min-height: 60px; }
.pthumb canvas { width: 100%; height: auto; display: block; }
.pcard-bar { display: flex; align-items: center; gap: 0; font-size: 12px; color: var(--muted); padding: 4px 2px 0; }

@media (max-width: 820px) {
  .lib { grid-template-columns: 1fr; }
  .side { position: fixed; z-index: 60; inset: 0 auto 0 0; width: 270px; transform: translateX(-102%); transition: transform .2s; box-shadow: var(--shadow); }
  .lib.side-open .side { transform: none; }
  .lib.side-open .side-scrim { display: block; position: fixed; inset: 0; background: rgba(0,0,0,.4); z-index: 55; }
  .menu-btn { display: inline-grid; }
  .hide-sm { display: none; }
  .search { width: auto; flex: 1; }
  .crumbs { display: none; }
  .lib-body.grid { grid-template-columns: repeat(auto-fill, minmax(138px, 1fr)); gap: 12px; padding: 12px 12px 90px; }
  .ed-title { max-width: 30vw; }
  .ed-saved { display: none; }
  .slider input[type=range] { width: 110px; }
  .row3 { grid-template-columns: 1fr; }
}
@media print { body { display: none; } }
