:root {
  --accent: #6965db; --accent-hover: #5b57d1; --accent-light: #e3e2fe; --accent-text: #fff;
  --island-bg: #ffffff; --island-shadow: 0 0 0 1px rgba(0,0,0,.04), 0 1px 4px rgba(0,0,0,.08), 0 2px 10px rgba(0,0,0,.06);
  --text: #1b1b1f; --text-muted: #6f6f78; --border: #e5e5ea; --btn-hover: #f1f0ff; --btn-active: #e0dfff; --btn-selected: #e0dfff;
  --input-bg: #f5f5f9; --danger: #db6965; --danger-bg: #fdeeee; --overlay: rgba(0,0,0,.35);
  --radius: 10px; --radius-sm: 6px;
  --font: "Assistant", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}
.theme--dark {
  --island-bg: #232329; --island-shadow: 0 0 0 1px rgba(255,255,255,.04), 0 1px 4px rgba(0,0,0,.4), 0 2px 10px rgba(0,0,0,.3);
  --text: #ced4da; --text-muted: #8d8d99; --border: #34343f; --btn-hover: #31303b; --btn-active: #403e6a; --btn-selected: #403e6a;
  --input-bg: #1b1b1f; --accent-light: #403e6a; --danger-bg: #3a2222; --overlay: rgba(0,0,0,.55);
}
@font-face { font-family: "Virgil"; src: url("../fonts/Virgil.woff2") format("woff2"); font-display: swap; }
@font-face { font-family: "Excalifont"; src: url("../fonts/Excalifont.woff2") format("woff2"); font-display: swap; }
@font-face { font-family: "Cascadia"; src: url("../fonts/Cascadia.woff2") format("woff2"); font-display: swap; }
@font-face { font-family: "Assistant"; src: url("../fonts/Assistant-Regular.woff2") format("woff2"); font-display: swap; }

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; }
body { font-family: var(--font); font-size: 14px; color: var(--text); background: #fff; -webkit-font-smoothing: antialiased; }
.theme--dark body, body.theme--dark { background: #121212; }
button { font-family: inherit; font-size: inherit; color: inherit; }
input, textarea, select { font-family: inherit; }

#app { position: fixed; inset: 0; overflow: hidden; touch-action: none; background: #fff; }
#app.theme--dark { background: #121212; }
.canvas-wrap { position: absolute; inset: 0; }
.canvas-wrap canvas { position: absolute; left: 0; top: 0; display: block; }
.theme--dark .canvas-wrap canvas { filter: invert(93%) hue-rotate(180deg); }
#interactive-canvas { z-index: 2; }

.text-editor {
  position: absolute; z-index: 20; margin: 0; padding: 0; border: 0; outline: none; background: transparent; resize: none; overflow: hidden;
  transform-origin: center center; white-space: pre; min-height: 1em; caret-color: var(--text);
}
.theme--dark .text-editor { filter: invert(93%) hue-rotate(180deg); }

/* ---------- UI chrome ---------- */
.ui { position: absolute; inset: 0; z-index: 10; pointer-events: none; }
.ui > * { pointer-events: auto; }
.island { background: var(--island-bg); box-shadow: var(--island-shadow); border-radius: var(--radius); padding: 4px; }
.top-bar { position: absolute; left: 12px; right: 12px; top: 12px; display: grid; grid-template-columns: 1fr auto 1fr; gap: 10px; align-items: start; pointer-events: none; }
.top-bar > * { pointer-events: auto; }
.top-left { display: flex; gap: 8px; align-items: center; }
.top-right { display: flex; gap: 8px; align-items: center; justify-content: flex-end; }
.toolbar { display: flex; align-items: center; gap: 2px; padding: 4px; max-width: calc(100vw - 24px); overflow-x: auto; scrollbar-width: none; }
.toolbar::-webkit-scrollbar { display: none; }
.toolbar .sep { width: 1px; height: 24px; background: var(--border); margin: 0 4px; flex: none; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; border: 0; background: transparent; border-radius: var(--radius-sm); padding: 0; height: 36px; min-width: 36px; cursor: pointer; position: relative; color: var(--text); transition: background .1s; flex: none; }
.btn:hover { background: var(--btn-hover); }
.btn:active { background: var(--btn-active); }
.btn.selected, .btn[aria-pressed="true"] { background: var(--btn-selected); color: var(--accent); }
.theme--dark .btn.selected, .theme--dark .btn[aria-pressed="true"] { color: #d5d3ff; }
.btn:disabled { opacity: .35; cursor: default; background: transparent; }
.btn .icon { display: inline-flex; width: 20px; height: 20px; }
.btn .icon svg { width: 20px; height: 20px; }
.btn .badge { position: absolute; right: 3px; bottom: 2px; font-size: 9px; color: var(--text-muted); line-height: 1; }
.btn.selected .badge { color: inherit; }
.btn.primary { background: var(--accent); color: var(--accent-text); padding: 0 14px; height: 36px; font-weight: 600; }
.btn.primary:hover { background: var(--accent-hover); }
.btn.text { padding: 0 10px; height: 32px; font-weight: 500; }
.btn.danger { color: var(--danger); }
.btn.danger.primary { background: var(--danger); color: #fff; }
.btn.outline { border: 1px solid var(--border); padding: 0 12px; height: 34px; }
.btn.sm { height: 28px; min-width: 28px; }
.btn.sm .icon svg { width: 16px; height: 16px; }
.icon-btn-label { font-size: 13px; }

/* properties panel */
.props { position: absolute; left: 12px; top: 64px; width: 212px; max-height: calc(100vh - 150px); overflow-y: auto; padding: 10px 12px 6px; display: flex; flex-direction: column; gap: 10px; scrollbar-width: thin; }
.props h4 { margin: 0 0 4px; font-size: 12px; font-weight: 500; color: var(--text-muted); }
.props .section { display: flex; flex-direction: column; }
.props .row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.props .row .btn { height: 32px; min-width: 32px; }
.swatch { width: 22px; height: 22px; border-radius: 5px; border: 1px solid rgba(0,0,0,.12); cursor: pointer; padding: 0; position: relative; flex: none; background-clip: padding-box; }
.theme--dark .swatch { border-color: rgba(255,255,255,.18); }
.swatch.selected { outline: 2px solid var(--accent); outline-offset: 1px; }
.swatch.transparent, .color-current.transparent { background-image: linear-gradient(45deg, #ddd 25%, transparent 25%), linear-gradient(-45deg, #ddd 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #ddd 75%), linear-gradient(-45deg, transparent 75%, #ddd 75%); background-size: 8px 8px; background-position: 0 0, 0 4px, 4px -4px, -4px 0; background-color: #fff; }
.color-row { display: flex; gap: 6px; align-items: center; }
.color-row .divider { width: 1px; height: 22px; background: var(--border); margin: 0 2px; }
.color-current { width: 26px; height: 26px; border-radius: 6px; border: 1px solid var(--border); cursor: pointer; padding: 0; flex: none; }
.color-current.open { outline: 2px solid var(--accent); outline-offset: 1px; }
.opacity { width: 100%; accent-color: var(--accent); margin: 2px 0 0; }
.props .actions-row { display: flex; gap: 6px; flex-wrap: wrap; }

/* popover */
.popover { position: fixed; z-index: 60; background: var(--island-bg); box-shadow: var(--island-shadow); border-radius: var(--radius); padding: 10px; min-width: 180px; }
.color-picker { width: 220px; }
.color-picker .grid { display: grid; grid-template-columns: repeat(5, 1fr); gap: 6px; margin-bottom: 8px; }
.color-picker .shades { display: flex; gap: 4px; margin-bottom: 8px; }
.color-picker .shades .swatch { width: 18px; height: 22px; border-radius: 4px; }
.color-picker .hex { display: flex; align-items: center; gap: 6px; }
.color-picker input[type=text] { flex: 1; min-width: 0; height: 30px; border: 1px solid var(--border); border-radius: 6px; padding: 0 8px; background: var(--input-bg); color: var(--text); font-family: monospace; }
.color-picker label { font-size: 11px; color: var(--text-muted); display: block; margin-bottom: 4px; }
.popover .menu-item, .dropdown .menu-item { display: flex; align-items: center; gap: 10px; width: 100%; padding: 7px 10px; border: 0; background: transparent; border-radius: var(--radius-sm); cursor: pointer; text-align: left; font-size: 13.5px; color: var(--text); }
.menu-item:hover, .menu-item.active { background: var(--btn-hover); }
.menu-item:disabled { opacity: .4; cursor: default; background: transparent; }
.menu-item .icon { width: 18px; height: 18px; display: inline-flex; color: var(--text-muted); }
.menu-item .icon svg { width: 18px; height: 18px; }
.menu-item .label { flex: 1; }
.menu-item kbd, .shortcuts kbd { font-family: inherit; font-size: 11px; color: var(--text-muted); border: 1px solid var(--border); border-radius: 4px; padding: 1px 5px; background: var(--input-bg); }
.menu-item .check { width: 16px; display: inline-flex; color: var(--accent); }
.menu-sep { height: 1px; background: var(--border); margin: 6px 0; }
.dropdown { position: fixed; z-index: 60; background: var(--island-bg); box-shadow: var(--island-shadow); border-radius: var(--radius); padding: 6px; min-width: 230px; max-height: calc(100vh - 80px); overflow-y: auto; }
.dropdown .section-title { font-size: 11.5px; color: var(--text-muted); padding: 8px 10px 4px; }
.dropdown .theme-row { display: flex; align-items: center; justify-content: space-between; padding: 6px 10px; }
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg .btn { border-radius: 0; height: 30px; min-width: 34px; }
.canvas-bg-row { display: flex; gap: 6px; padding: 6px 10px 8px; align-items: center; }

/* bottom */
.bottom-left { position: absolute; left: 12px; bottom: 12px; display: flex; gap: 8px; align-items: center; }
.bottom-right { position: absolute; right: 12px; bottom: 12px; display: flex; gap: 8px; align-items: center; }
.zoom-island { display: flex; align-items: center; }
.zoom-island .zoom-value { min-width: 56px; text-align: center; font-size: 13px; cursor: pointer; height: 36px; display: inline-flex; align-items: center; justify-content: center; border-radius: var(--radius-sm); }
.zoom-island .zoom-value:hover { background: var(--btn-hover); }
.hint { position: absolute; left: 50%; bottom: 14px; transform: translateX(-50%); font-size: 12.5px; color: var(--text-muted); white-space: nowrap; pointer-events: none; max-width: calc(100vw - 340px); overflow: hidden; text-overflow: ellipsis; }

/* stats */
.stats { position: absolute; right: 12px; bottom: 60px; width: 220px; padding: 10px 12px; font-size: 12.5px; }
.stats h4 { margin: 0 0 6px; font-size: 12px; color: var(--text-muted); font-weight: 600; display: flex; justify-content: space-between; align-items: center; }
.stats table { width: 100%; border-collapse: collapse; }
.stats td { padding: 2px 0; }
.stats td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
.stats input { width: 70px; text-align: right; height: 24px; border: 1px solid var(--border); border-radius: 4px; background: var(--input-bg); color: var(--text); padding: 0 6px; }

/* dialogs */
.dialog-backdrop { position: fixed; inset: 0; background: var(--overlay); z-index: 100; display: flex; align-items: center; justify-content: center; padding: 20px; }
.dialog { background: var(--island-bg); color: var(--text); border-radius: 14px; box-shadow: 0 10px 40px rgba(0,0,0,.3); width: min(100%, 560px); max-height: calc(100vh - 40px); display: flex; flex-direction: column; overflow: hidden; }
.dialog.wide { width: min(100%, 860px); }
.dialog header { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px 8px; }
.dialog header h2 { margin: 0; font-size: 20px; font-weight: 700; }
.dialog .body { padding: 8px 20px 20px; overflow-y: auto; }
.dialog .body p { margin: 0 0 12px; color: var(--text-muted); line-height: 1.45; }
.dialog .footer { display: flex; gap: 8px; justify-content: flex-end; padding: 12px 20px 16px; border-top: 1px solid var(--border); }
.dialog label.field { display: block; font-size: 12.5px; color: var(--text-muted); margin: 10px 0 4px; }
.dialog input[type=text], .dialog input[type=url] { width: 100%; height: 38px; border: 1px solid var(--border); border-radius: 8px; background: var(--input-bg); color: var(--text); padding: 0 10px; font-size: 14px; }
.dialog .link-row { display: flex; gap: 8px; align-items: center; }
.dialog .link-row input { flex: 1; min-width: 0; }
.check-row { display: flex; align-items: center; gap: 8px; margin: 8px 0; font-size: 13.5px; cursor: pointer; }
.check-row input { accent-color: var(--accent); width: 16px; height: 16px; }
.export-layout { display: grid; grid-template-columns: 1fr 240px; gap: 20px; }
.export-preview { background-image: linear-gradient(45deg, #eee 25%, transparent 25%), linear-gradient(-45deg, #eee 25%, transparent 25%), linear-gradient(45deg, transparent 75%, #eee 75%), linear-gradient(-45deg, transparent 75%, #eee 75%); background-size: 16px 16px; background-position: 0 0, 0 8px, 8px -8px, -8px 0; border-radius: 10px; min-height: 260px; display: flex; align-items: center; justify-content: center; overflow: hidden; border: 1px solid var(--border); }
.export-preview img, .export-preview canvas { max-width: 100%; max-height: 420px; }
.export-buttons { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.export-buttons .btn { flex: 1; }
.scale-row { display: flex; gap: 6px; }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 14px; }
.tabs .tab { padding: 8px 12px; border: 0; background: transparent; cursor: pointer; border-bottom: 2px solid transparent; font-weight: 600; color: var(--text-muted); }
.tabs .tab.active { color: var(--accent); border-bottom-color: var(--accent); }
.collab-status { display: flex; align-items: center; gap: 8px; padding: 10px 12px; background: var(--input-bg); border-radius: 8px; font-size: 13px; margin-bottom: 12px; }
.collab-status .dot { width: 8px; height: 8px; border-radius: 50%; background: #2f9e44; }
.collab-status .dot.err { background: var(--danger); }
.avatars { display: flex; gap: -4px; }
.avatar { width: 30px; height: 30px; border-radius: 50%; display: inline-flex; align-items: center; justify-content: center; color: #fff; font-weight: 700; font-size: 13px; border: 2px solid var(--island-bg); margin-left: -6px; cursor: default; }
.avatar:first-child { margin-left: 0; }

/* help */
.shortcuts { display: grid; grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); gap: 18px; }
.shortcuts h3 { font-size: 14px; margin: 0 0 8px; color: var(--text); }
.shortcuts .row { display: flex; justify-content: space-between; align-items: center; gap: 10px; padding: 4px 0; font-size: 13px; border-bottom: 1px solid var(--border); }
.shortcuts .row span { color: var(--text-muted); }
.shortcuts .keys { display: flex; gap: 4px; flex: none; }

/* command palette */
.palette { width: min(100%, 560px); }
.palette input { width: 100%; height: 44px; border: 0; border-bottom: 1px solid var(--border); background: transparent; color: var(--text); padding: 0 16px; font-size: 16px; outline: none; }
.palette .list { max-height: 50vh; overflow-y: auto; padding: 6px; }
.palette .menu-item { font-size: 14px; }

/* context menu */
.context-menu { position: fixed; z-index: 90; min-width: 220px; }

/* sidebar */
.sidebar { position: absolute; right: 12px; top: 64px; bottom: 12px; width: 300px; display: flex; flex-direction: column; padding: 0; overflow: hidden; }
.sidebar header { display: flex; align-items: center; justify-content: space-between; padding: 10px 12px; border-bottom: 1px solid var(--border); font-weight: 700; }
.sidebar .lib-actions { display: flex; gap: 6px; padding: 8px 10px; flex-wrap: wrap; border-bottom: 1px solid var(--border); }
.sidebar .lib-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; padding: 10px; overflow-y: auto; flex: 1; align-content: start; }
.lib-item { position: relative; aspect-ratio: 1; border: 1px solid var(--border); border-radius: 8px; display: flex; align-items: center; justify-content: center; cursor: pointer; background: #fff; overflow: hidden; }
.theme--dark .lib-item { background: #fff; }
.lib-item:hover { border-color: var(--accent); }
.lib-item .remove { position: absolute; top: 2px; right: 2px; width: 20px; height: 20px; border-radius: 50%; background: rgba(255,255,255,.9); border: 1px solid #ddd; display: none; align-items: center; justify-content: center; padding: 0; cursor: pointer; color: #333; }
.lib-item:hover .remove { display: inline-flex; }
.lib-item .remove svg { width: 12px; height: 12px; }
.lib-empty { padding: 20px; color: var(--text-muted); font-size: 13px; text-align: center; line-height: 1.5; }

/* toasts */
.toasts { position: absolute; left: 50%; bottom: 56px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; pointer-events: none; }
.toast { background: var(--text); color: var(--island-bg); padding: 8px 14px; border-radius: 8px; font-size: 13px; box-shadow: 0 4px 12px rgba(0,0,0,.2); animation: toast-in .15s ease-out; }
@keyframes toast-in { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }

/* link editor / frame name */
.link-editor { position: absolute; z-index: 30; display: flex; gap: 6px; align-items: center; padding: 6px; }
.link-editor input { width: 240px; height: 30px; border: 1px solid var(--border); border-radius: 6px; background: var(--input-bg); color: var(--text); padding: 0 8px; }
.frame-name-input { position: absolute; z-index: 30; border: 1px solid var(--accent); background: var(--island-bg); color: var(--text); padding: 2px 6px; font-size: 14px; border-radius: 4px; outline: none; }

/* modes */
.zen .props, .zen .hint, .zen .stats, .zen .top-right, .zen .bottom-right, .zen .top-left { display: none !important; }
.viewmode .props, .viewmode .toolbar, .viewmode .hint, .viewmode .top-right .lib-btn, .viewmode .bottom-left .undo-island { display: none !important; }

/* mobile */
.mobile-props-toggle { display: none; }
@media (max-width: 760px) {
  .top-bar { grid-template-columns: auto 1fr auto; gap: 6px; }
  .toolbar { gap: 0; }
  .toolbar .btn { height: 34px; min-width: 34px; }
  .toolbar .badge { display: none; }
  .props { left: 8px; right: 8px; top: auto; bottom: 60px; width: auto; max-height: 40vh; display: none; }
  .props.open { display: flex; }
  .mobile-props-toggle { display: inline-flex; }
  .hint { display: none; }
  .sidebar { left: 8px; right: 8px; width: auto; top: 60px; }
  .export-layout { grid-template-columns: 1fr; }
  .stats { display: none; }
  .top-right .icon-btn-label { display: none; }
  .top-right .btn.primary { padding: 0 10px; }
  .avatars { display: none; }
  .bottom-left { bottom: 8px; left: 8px; }
  .bottom-right { bottom: 8px; right: 8px; }
}
.find-box { position: absolute; left: 50%; top: 70px; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; padding: 8px; width: min(420px, calc(100vw - 24px)); z-index: 40; }
.find-box .row { display: flex; gap: 6px; align-items: center; }
.find-box input { flex: 1; min-width: 0; height: 34px; border: 1px solid var(--border); border-radius: 8px; background: var(--input-bg); color: var(--text); padding: 0 10px; font-size: 14px; }
.find-box .count { font-size: 12px; color: var(--text-muted); white-space: nowrap; }
.find-box .results { max-height: 240px; overflow-y: auto; display: flex; flex-direction: column; }
.find-box .results .menu-item { font-size: 13px; }
