:root {
  --bg: #ffffff;
  --bg-2: #f5f5f5;
  --bg-3: #ebebeb;
  --canvas: #e5e5e5;
  --text: #1e1e1e;
  --muted: #757575;
  --line: #e6e6e6;
  --line-2: #d4d4d4;
  --accent: #0d99ff;
  --accent-weak: #e5f4ff;
  --component: #9747ff;
  --danger: #e03e1a;
  --bar: #2c2c2c;
  --bar-text: #ffffff;
  --bar-hover: #3d3d3d;
  --radius: 6px;
  --font: Inter, system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #2c2c2c;
    --bg-2: #383838;
    --bg-3: #444444;
    --canvas: #1e1e1e;
    --text: #f0f0f0;
    --muted: #a8a8a8;
    --line: #3d3d3d;
    --line-2: #4d4d4d;
    --accent-weak: #1c3a52;
    --bar: #1e1e1e;
    --bar-hover: #383838;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #2c2c2c;
  --bg-2: #383838;
  --bg-3: #444444;
  --canvas: #1e1e1e;
  --text: #f0f0f0;
  --muted: #a8a8a8;
  --line: #3d3d3d;
  --line-2: #4d4d4d;
  --accent-weak: #1c3a52;
  --bar: #1e1e1e;
  --bar-hover: #383838;
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 13px/1.4 var(--font); color: var(--text); background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--accent); }
code { background: var(--bg-2); padding: 1px 4px; border-radius: 4px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.error { color: var(--danger); min-height: 1.2em; margin: 4px 0; }
button.primary {
  background: var(--accent); color: #fff; border: 0; border-radius: var(--radius); padding: 8px 14px; font-weight: 600;
}
button.primary:disabled { opacity: .6; }
button.link { background: none; border: 0; color: var(--accent); padding: 0; }
button.danger-outline { background: none; border: 1px solid var(--danger); color: var(--danger); border-radius: var(--radius); padding: 6px 12px; }
input[type=text], input[type=password], input[type=number], select, textarea {
  background: var(--bg); border: 1px solid var(--line-2); border-radius: var(--radius); padding: 7px 9px; min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }

/* Auth pages */
.auth-page { display: grid; place-items: center; min-height: 100%; background: var(--bg-2); padding: 16px; }
.auth-card {
  width: min(380px, 100%); background: var(--bg); border: 1px solid var(--line); border-radius: 12px; padding: 28px;
  display: flex; flex-direction: column; gap: 10px;
}
.auth-card h1 { font-size: 20px; margin: 8px 0 0; }
.auth-card label { display: flex; flex-direction: column; gap: 4px; font-weight: 500; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 15px; }
.brand a { display: flex; align-items: center; gap: 8px; color: inherit; text-decoration: none; }

/* Home */
.home { background: var(--bg-2); min-height: 100%; }
.home-bar { display: flex; justify-content: space-between; align-items: center; padding: 12px 20px; background: var(--bg); border-bottom: 1px solid var(--line); }
.home-bar nav { display: flex; gap: 16px; align-items: center; }
.home-main { max-width: 1200px; margin: 0 auto; padding: 24px 16px; }
.home-main.narrow { max-width: 680px; }
.home-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.home-head h1 { font-size: 22px; }
.file-grid { list-style: none; padding: 0; display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 16px; }
.file-card { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.file-card a { display: block; color: inherit; text-decoration: none; padding: 0 0 8px; }
.file-card .thumb { aspect-ratio: 16/10; background: var(--bg-3); display: grid; place-items: center; overflow: hidden; }
.file-card .thumb img { width: 100%; height: 100%; object-fit: contain; }
.file-card .file-name { font-weight: 600; padding: 8px 12px 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.file-card .muted { padding: 0 12px; }
.file-actions { display: flex; gap: 12px; padding: 0 12px 10px; }
.card { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 18px; margin: 16px 0; }
.card h2 { font-size: 16px; margin-top: 0; }
.card label { display: flex; flex-direction: column; gap: 4px; margin: 8px 0; }
.secret-row { display: flex; gap: 8px; margin: 8px 0; }
.secret-row input { flex: 1; font-family: ui-monospace, monospace; }
.secret-row button { border: 1px solid var(--line-2); background: var(--bg); border-radius: var(--radius); padding: 6px 10px; }

/* Editor */
body.editor { overflow: hidden; user-select: none; -webkit-user-select: none; background: var(--canvas); }
.editor-shell { display: grid; grid-template-rows: 44px 1fr; height: 100vh; height: 100dvh; }
.topbar { display: flex; align-items: center; gap: 4px; background: var(--bar); color: var(--bar-text); padding: 0 8px; min-width: 0; }
.topbar .tb-btn {
  background: none; border: 0; color: var(--bar-text); height: 32px; min-width: 32px; padding: 0 6px; border-radius: 5px;
  display: inline-flex; align-items: center; justify-content: center; gap: 4px;
}
.topbar .tb-btn:hover { background: var(--bar-hover); }
.topbar .tb-btn.active { background: var(--accent); }
.topbar .tb-btn svg { width: 18px; height: 18px; }
.topbar .sep { width: 1px; height: 24px; background: #444; margin: 0 6px; }
.topbar .title { flex: 1; text-align: center; min-width: 0; display: flex; justify-content: center; }
.topbar .title input {
  background: transparent; border: 1px solid transparent; color: var(--bar-text); text-align: center; font-weight: 600; width: min(320px, 100%);
}
.topbar .title input:hover { border-color: #555; }
.topbar .status { font-size: 12px; color: #bbb; white-space: nowrap; }
.topbar .zoom { font-size: 12px; width: 56px; }
.workspace { display: grid; grid-template-columns: 240px 1fr 288px; min-height: 0; }
.panel { background: var(--bg); border-color: var(--line); border-style: solid; border-width: 0; overflow: auto; min-height: 0; }
.panel.left { border-right-width: 1px; display: flex; flex-direction: column; }
.panel.right { border-left-width: 1px; }
.panel-section { border-bottom: 1px solid var(--line); padding: 8px 0; }
.panel-section:last-child { border-bottom: 0; }
.section-head { display: flex; align-items: center; justify-content: space-between; padding: 2px 12px 4px; font-weight: 600; font-size: 12px; }
.section-head .icon-btn { margin-right: -6px; }
.icon-btn {
  background: none; border: 0; border-radius: 4px; width: 26px; height: 26px; display: inline-grid; place-items: center; color: inherit; padding: 0;
}
.icon-btn:hover { background: var(--bg-3); }
.icon-btn.on { background: var(--accent-weak); color: var(--accent); }
.icon-btn svg { width: 16px; height: 16px; }

.pages-list, .layers-list { list-style: none; margin: 0; padding: 0; }
.pages-list li { padding: 5px 12px 5px 28px; cursor: default; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.pages-list li:hover { background: var(--bg-2); }
.pages-list li.current { font-weight: 600; }
.pages-list li.current::before { content: '✓'; position: absolute; margin-left: -16px; }
.layers { flex: 1; min-height: 0; overflow: auto; }
.layer-row {
  display: flex; align-items: center; height: 28px; padding-right: 6px; gap: 4px; cursor: default; white-space: nowrap; position: relative;
}
.layer-row:hover { background: var(--bg-2); }
.layer-row.selected { background: var(--accent-weak); }
.layer-row.child-of-selected { background: color-mix(in srgb, var(--accent-weak) 45%, transparent); }
.layer-row .twisty { width: 16px; flex: none; text-align: center; color: var(--muted); font-size: 9px; }
.layer-row .ltype { width: 16px; flex: none; color: var(--muted); display: grid; place-items: center; }
.layer-row .ltype svg { width: 14px; height: 14px; }
.layer-row.component .ltype, .layer-row.component .lname, .layer-row.instance .ltype, .layer-row.instance .lname { color: var(--component); }
.layer-row .lname { flex: 1; overflow: hidden; text-overflow: ellipsis; }
.layer-row .lname input { width: 100%; padding: 1px 4px; }
.layer-row .ltoggles { display: none; }
.layer-row:hover .ltoggles, .layer-row .ltoggles.pinned { display: flex; }
.layer-row.hidden-layer .lname, .layer-row.hidden-layer .ltype { opacity: .45; }
.layer-row.drop-before::before, .layer-row.drop-after::after {
  content: ''; position: absolute; left: 0; right: 0; height: 2px; background: var(--accent);
}
.layer-row.drop-before::before { top: -1px; }
.layer-row.drop-after::after { bottom: -1px; }
.layer-row.drop-inside { outline: 2px solid var(--accent); outline-offset: -2px; }

.viewport { position: relative; overflow: hidden; background: var(--page-bg, var(--canvas)); touch-action: none; outline: none; }
.viewport svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.viewport .overlay { pointer-events: none; }
.viewport .overlay .hit-handle { pointer-events: all; }
.viewport.tool-hand, .viewport.panning { cursor: grab; }
.viewport.panning:active { cursor: grabbing; }
.viewport.tool-create { cursor: crosshair; }
.viewport.tool-text { cursor: text; }
.frame-label { font: 11px var(--font); fill: var(--muted); cursor: default; pointer-events: all; }
.frame-label.selected { fill: var(--accent); }
.frame-label.component { fill: var(--component); }
.text-editor {
  position: absolute; transform-origin: 0 0; outline: none; white-space: pre-wrap; word-break: break-word; caret-color: var(--accent);
  user-select: text; -webkit-user-select: text; min-width: 2px; background: transparent;
}
.size-label { font: 600 11px var(--font); fill: #fff; }

/* Properties */
.props { padding-bottom: 40px; }
.prop-tabs { display: flex; gap: 4px; padding: 8px 12px; border-bottom: 1px solid var(--line); }
.prop-tabs button { background: none; border: 0; padding: 4px 8px; border-radius: 5px; font-weight: 600; color: var(--muted); }
.prop-tabs button.active { color: var(--text); background: var(--bg-2); }
.row { display: flex; align-items: center; gap: 6px; padding: 3px 12px; }
.row > * { min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; padding: 3px 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; padding: 3px 12px; }
.field {
  display: flex; align-items: center; height: 28px; border: 1px solid transparent; border-radius: 5px; background: var(--bg-2); min-width: 0;
}
.field:hover { border-color: var(--line-2); }
.field:focus-within { border-color: var(--accent); }
.field .flabel { width: 24px; flex: none; text-align: center; color: var(--muted); font-size: 11px; cursor: ew-resize; }
.field input, .field select { border: 0; background: transparent; padding: 0 6px 0 0; height: 26px; width: 100%; outline: none; }
.field select { padding-left: 6px; }
.field .suffix { color: var(--muted); padding-right: 6px; font-size: 11px; }
.swatch-field { display: flex; align-items: center; gap: 6px; flex: 1; height: 28px; background: var(--bg-2); border-radius: 5px; padding: 0 6px; }
.swatch {
  width: 18px; height: 18px; border-radius: 3px; border: 1px solid rgba(0,0,0,.15); flex: none; position: relative; overflow: hidden;
  background-image: linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%), linear-gradient(45deg, #ccc 25%, transparent 25%, transparent 75%, #ccc 75%);
  background-size: 8px 8px; background-position: 0 0, 4px 4px;
}
.swatch > span { position: absolute; inset: 0; }
.swatch input[type=color] { position: absolute; inset: 0; opacity: 0; cursor: pointer; width: 100%; height: 100%; border: 0; padding: 0; }
.swatch-field { min-width: 0; }
.swatch-field input.hex { width: 54px; min-width: 0; border: 0; background: transparent; text-transform: uppercase; padding: 0; outline: none; }
.swatch-field input.pct { width: 34px; min-width: 0; border: 0; background: transparent; padding: 0; text-align: right; outline: none; }
.seg { display: inline-flex; background: var(--bg-2); border-radius: 5px; padding: 2px; gap: 2px; }
.seg button { background: none; border: 0; height: 24px; min-width: 26px; border-radius: 4px; display: grid; place-items: center; color: var(--muted); }
.seg button.on { background: var(--bg); color: var(--text); box-shadow: 0 0 0 1px var(--line-2); }
.seg button svg { width: 15px; height: 15px; }
.check { display: flex; align-items: center; gap: 6px; }
.mixed { color: var(--muted); font-style: italic; }
.prop-empty { padding: 16px 12px; color: var(--muted); }
.export-row { display: flex; gap: 6px; padding: 3px 12px; }
.export-row .field { flex: 1; }
.btn-secondary { border: 1px solid var(--line-2); background: var(--bg); border-radius: 5px; padding: 5px 10px; width: calc(100% - 24px); margin: 4px 12px; }
.btn-secondary:hover { background: var(--bg-2); }

/* Menus and toasts */
.menu {
  position: fixed; z-index: 50; background: #1e1e1e; color: #fff; border-radius: 8px; padding: 6px 0; min-width: 200px;
  box-shadow: 0 8px 24px rgba(0,0,0,.25); font-size: 12px;
}
.menu button { display: flex; justify-content: space-between; gap: 24px; width: 100%; background: none; border: 0; color: inherit; padding: 5px 14px; text-align: left; }
.menu button:hover:not(:disabled) { background: var(--accent); }
.menu button:disabled { opacity: .4; }
.menu .kbd { color: #aaa; }
.menu hr { border: 0; border-top: 1px solid #444; margin: 5px 0; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: #1e1e1e; color: #fff; padding: 8px 14px;
  border-radius: 8px; z-index: 60; box-shadow: 0 4px 16px rgba(0,0,0,.25); max-width: calc(100% - 32px);
}
.popover {
  position: fixed; z-index: 40; background: var(--bg); border: 1px solid var(--line-2); border-radius: 8px; padding: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,.18); width: 240px;
}

@media (max-width: 900px) {
  .workspace { grid-template-columns: 0 1fr 0; }
  .workspace.show-left { grid-template-columns: 220px 1fr 0; }
  .workspace.show-right { grid-template-columns: 0 1fr 240px; }
  .topbar .hide-narrow { display: none; }
}
.row > svg, .btn-secondary svg { width: 16px; height: 16px; flex: none; }
.row > b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
