:root {
  --bg: #0e0f13;
  --panel: #16181f;
  --panel-2: #1d2029;
  --line: #2a2e3a;
  --text: #e7e9ef;
  --muted: #8b91a3;
  --accent: #7c6cff;
  --accent-2: #22d3ee;
  --danger: #ff5d6c;
  --radius: 10px;
  --check-a: #262a34;
  --check-b: #1c1f27;
  --stage-h: 62vh;
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root:not([data-theme="dark"]) {
    --bg: #f3f4f7;
    --panel: #ffffff;
    --panel-2: #f5f6f9;
    --line: #dfe2ea;
    --text: #171a22;
    --muted: #697084;
    --check-a: #e6e8ee;
    --check-b: #f7f8fa;
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { margin: 0; }

body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  min-height: 100vh;
}

h2 { font-size: 12px; font-weight: 600; letter-spacing: .06em; text-transform: uppercase; margin: 0; color: var(--muted); }

button, select, textarea, input[type="text"], input[type="number"] {
  font: inherit;
  color: var(--text);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 6px 10px;
}
textarea { resize: vertical; width: 100%; min-height: 34px; line-height: 1.35; }
button { cursor: pointer; white-space: nowrap; }
button:hover { border-color: var(--accent); }
button:disabled { opacity: .4; cursor: default; border-color: var(--line); }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
button.primary:hover { filter: brightness(1.1); }
button.danger:hover { border-color: var(--danger); color: var(--danger); }
button.small { padding: 3px 8px; font-size: 12px; }
input[type="color"] { width: 100%; height: 30px; padding: 2px; border: 1px solid var(--line); border-radius: 7px; background: var(--panel-2); cursor: pointer; }
input[type="range"] { width: 100%; accent-color: var(--accent); }
input[type="checkbox"] { accent-color: var(--accent); }

.sub { color: var(--muted); font-size: 11px; font-weight: 400; text-transform: none; letter-spacing: 0; }
.hint { color: var(--muted); font-size: 11px; margin: 8px 0 0; }
.grow { flex: 1; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap;
  padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--panel);
  position: sticky; top: 0; z-index: 10;
}
.brand { font-weight: 800; font-size: 16px; letter-spacing: .02em; }
.brand-mark {
  background: linear-gradient(135deg, var(--accent), var(--accent-2));
  color: #fff; padding: 2px 7px; border-radius: 6px; margin-right: 2px;
}
.topbar-actions { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.sep { width: 1px; height: 22px; background: var(--line); margin: 0 4px; }

/* Workspace */
.workspace {
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr) 300px;
  gap: 16px;
  padding: 16px;
  align-items: start;
}
.side { display: flex; flex-direction: column; gap: 16px; min-width: 0; }
.panel {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 12px;
  min-width: 0;
}
.panel-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 10px; }
details.panel > summary { list-style: none; cursor: pointer; }
details.panel > summary::-webkit-details-marker { display: none; }
details.panel > summary h2::before { content: '▸ '; }
details.panel[open] > summary h2::before { content: '▾ '; }
details.panel:not([open]) > summary { margin-bottom: 0; }

/* Templates */
.template-list { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.template {
  display: flex; flex-direction: column; gap: 4px; padding: 4px; text-align: left;
  font-size: 11px; color: var(--muted);
}
.template canvas { width: 100%; aspect-ratio: 1200 / 630; border-radius: 5px; display: block; }

/* Layers */
.add-row { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin-bottom: 10px; }
.add-row button { padding: 6px 4px; }
.layer-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.layer {
  display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: 8px;
  border: 1px solid transparent; background: var(--panel-2); cursor: pointer; user-select: none;
}
.layer:hover { border-color: var(--line); }
.layer.selected { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 14%, var(--panel-2)); }
.layer.hidden-layer .layer-name, .layer.hidden-layer canvas { opacity: .4; }
.layer.drag-over-top { box-shadow: 0 -2px 0 var(--accent-2); }
.layer.drag-over-bottom { box-shadow: 0 2px 0 var(--accent-2); }
.layer canvas {
  width: 44px; height: 32px; border-radius: 5px; flex: none;
  background: repeating-conic-gradient(var(--check-a) 0 25%, var(--check-b) 0 50%) 0 0 / 8px 8px;
}
.layer-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer-name > div { overflow: hidden; text-overflow: ellipsis; }
.layer-meta { color: var(--muted); font-size: 10px; text-transform: uppercase; }
.layer-meta .ov { color: var(--accent-2); }
.icon-btn { background: none; border: none; padding: 3px; color: var(--muted); line-height: 0; border-radius: 5px; }
.icon-btn:hover { color: var(--text); }
.icon-btn.on { color: var(--accent-2); }
.icon-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* Stage */
.stage { display: flex; flex-direction: column; align-items: center; min-width: 0; }
.format-tabs {
  display: flex; gap: 6px; margin-bottom: 8px; max-width: 100%; overflow-x: auto; padding-bottom: 2px;
  scrollbar-width: thin;
}
.format-tabs button { display: flex; flex-direction: column; align-items: flex-start; gap: 1px; padding: 5px 10px; position: relative; }
.format-tabs button span { font-size: 10px; color: var(--muted); }
.format-tabs button.on { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 18%, var(--panel-2)); }
.format-tabs button.has-ov::after {
  content: ''; position: absolute; top: 5px; right: 5px; width: 6px; height: 6px; border-radius: 50%; background: var(--accent-2);
}
.stage-toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 10px; color: var(--muted); width: 100%; }
.stage-toolbar label { display: flex; gap: 6px; align-items: center; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 7px; overflow: hidden; }
.seg button { border: none; border-radius: 0; padding: 4px 10px; font-size: 12px; background: var(--panel-2); color: var(--muted); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: #fff; }
.seg button[data-scope="format"].on { background: #0e9fb8; }
.canvas-wrap {
  position: relative; width: min(100%, calc(var(--stage-h) * 1.905)); aspect-ratio: 1200 / 630;
  border-radius: 8px; overflow: hidden; border: 1px solid var(--line);
  background: repeating-conic-gradient(var(--check-a) 0 25%, var(--check-b) 0 50%) 0 0 / 24px 24px;
  touch-action: none;
}
.canvas-wrap.scoped { outline: 2px solid #0e9fb8; outline-offset: 2px; }
#editor { width: 100%; height: 100%; display: block; cursor: default; }
.drop-hint {
  position: absolute; inset: 0; display: none; place-items: center; font-size: 16px; font-weight: 600;
  background: color-mix(in srgb, var(--accent) 35%, transparent); color: #fff; pointer-events: none;
}
.canvas-wrap.dragging .drop-hint { display: grid; }

/* Properties */
.props { display: flex; flex-direction: column; gap: 10px; }
.props .empty { color: var(--muted); }
.field { display: flex; flex-direction: column; gap: 4px; }
.field > span { color: var(--muted); font-size: 11px; display: flex; justify-content: space-between; gap: 6px; }
.field.ovr > span::after, .check.ovr::after { content: '● this size'; color: var(--accent-2); font-size: 10px; }
.field-row { display: grid; grid-template-columns: 1fr 64px; gap: 8px; align-items: center; }
.field-row input[type="number"] { padding: 4px 6px; width: 100%; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.grid-2 > * { min-width: 0; }
.group { border-top: 1px solid var(--line); padding-top: 10px; display: flex; flex-direction: column; gap: 10px; }
.group-title { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; font-weight: 600; }
.btn-row { display: flex; gap: 6px; flex-wrap: wrap; }
.check { display: flex; gap: 6px; align-items: center; cursor: pointer; }
.props select, .props input[type="text"] { width: 100%; }
.tokens { color: var(--muted); font-size: 10px; }
.tokens code { font: 10px ui-monospace, "SF Mono", Menlo, monospace; background: var(--panel-2); padding: 0 3px; border-radius: 3px; cursor: pointer; }
.tokens code:hover { color: var(--accent-2); }
.scope-note { font-size: 11px; color: #0e9fb8; margin: 0; }

/* Outputs */
.outputs { padding: 0 16px 24px; }
.outputs-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(0, 1fr); gap: 16px; }
.previews-panel, .snippet-panel { grid-column: 1 / -1; }
.output-list { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 8px; }
.output {
  display: flex; flex-direction: column; gap: 4px;
  padding: 8px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  cursor: pointer; min-width: 0;
}
.output:hover, .output:focus-visible { border-color: var(--accent); outline: none; }
.output.current { box-shadow: inset 0 0 0 1px var(--accent); }
.output .thumb { height: 116px; display: grid; place-items: center; }
.output canvas {
  border-radius: 4px;
  background: repeating-conic-gradient(var(--check-a) 0 25%, var(--check-b) 0 50%) 0 0 / 8px 8px;
}
.output .o-name { font-size: 11px; font-weight: 600; word-break: break-all; }
.output .o-size { font-size: 10px; color: var(--muted); }
.output .o-foot { display: flex; justify-content: space-between; align-items: center; gap: 6px; }
.output .o-bytes { font-size: 10px; color: var(--muted); font-variant-numeric: tabular-nums; }
.output .o-bytes.warn { color: var(--danger); font-weight: 600; }

.settings { display: flex; flex-direction: column; gap: 10px; }
.settings label:not(.check) { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 11px; }
.settings input[type="text"], .settings select { width: 100%; }
.format-checks { display: flex; flex-direction: column; gap: 5px; padding-bottom: 6px; border-bottom: 1px solid var(--line); }
.format-checks .check { justify-content: space-between; }
.format-checks .check > span:first-of-type { flex: 1; }
.format-checks .dims { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }

.snippet {
  margin: 0; padding: 12px; background: var(--panel-2); border: 1px solid var(--line); border-radius: 8px;
  font: 12px/1.6 ui-monospace, "SF Mono", Menlo, monospace; overflow-x: auto; white-space: pre;
}

/* Link preview mockups (fixed app colors, independent of theme) */
.previews { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 16px; align-items: start; }
.pv { margin: 0; display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.pv figcaption { font-size: 11px; color: var(--muted); }
.pv canvas { display: block; width: 100%; height: auto; }
.pv .pv-title, .pv .pv-desc, .pv .pv-site, .pv .pv-domain { overflow: hidden; display: -webkit-box; -webkit-box-orient: vertical; }

.pv-fb { background: #fff; border: 1px solid #dadde1; font-family: Helvetica, Arial, sans-serif; color: #050505; }
.pv-fb .pv-body { background: #f0f2f5; padding: 10px 12px; border-top: 1px solid #dadde1; }
.pv-fb .pv-domain { font-size: 12px; color: #65676b; -webkit-line-clamp: 1; }
.pv-fb .pv-title { font-size: 16px; font-weight: 600; line-height: 1.25; margin-top: 2px; -webkit-line-clamp: 2; }
.pv-fb .pv-desc { font-size: 14px; color: #65676b; margin-top: 2px; -webkit-line-clamp: 1; }

.pv-x { font-family: -apple-system, "Segoe UI", Roboto, sans-serif; }
.pv-x-img { position: relative; border-radius: 16px; overflow: hidden; border: 1px solid #cfd9de; }
.pv-x-pill {
  position: absolute; left: 12px; bottom: 12px; max-width: calc(100% - 24px);
  background: rgba(0,0,0,.77); color: #fff; font-size: 13px; padding: 1px 6px; border-radius: 4px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.pv-x-from { font-size: 13px; color: var(--muted); margin-top: 4px; }

.pv-li { background: #fff; border-radius: 8px; overflow: hidden; border: 1px solid #e0dfdc; font-family: -apple-system, "Segoe UI", Roboto, sans-serif; color: rgba(0,0,0,.9); }
.pv-li .pv-body { padding: 8px 12px 10px; }
.pv-li .pv-title { font-size: 14px; font-weight: 600; line-height: 1.3; -webkit-line-clamp: 2; }
.pv-li .pv-domain { font-size: 12px; color: rgba(0,0,0,.6); margin-top: 2px; -webkit-line-clamp: 1; }

.pv-im { background: #e9e9eb; border-radius: 18px; overflow: hidden; max-width: 300px; font-family: -apple-system, system-ui, sans-serif; color: #000; }
.pv-im .pv-body { padding: 8px 12px 10px; }
.pv-im .pv-title { font-size: 13px; font-weight: 600; line-height: 1.25; -webkit-line-clamp: 2; }
.pv-im .pv-domain { font-size: 12px; color: #8a8a8e; -webkit-line-clamp: 1; }

.pv-slack {
  background: #fff; color: #1d1c1d; padding: 2px 0 2px 12px; border-left: 4px solid #dddddd;
  font-family: Lato, -apple-system, "Segoe UI", sans-serif; font-size: 14px; line-height: 1.4;
  border-radius: 2px; display: flex; flex-direction: column; gap: 2px;
}
.pv-slack .pv-site { font-weight: 700; -webkit-line-clamp: 1; }
.pv-slack .pv-title { color: #1264a3; font-weight: 700; -webkit-line-clamp: 2; }
.pv-slack .pv-desc { -webkit-line-clamp: 3; }
.pv-slack canvas { max-width: 300px; border-radius: 8px; margin-top: 4px; border: 1px solid rgba(29,28,29,.13); }

.pv-dc {
  --embed: #6d5dfc;
  background: #2b2d31; color: #dbdee1; padding: 10px 14px 14px 12px; border-left: 4px solid var(--embed);
  border-radius: 4px; font-family: "gg sans", "Noto Sans", Helvetica, Arial, sans-serif; font-size: 14px; line-height: 1.375;
  display: flex; flex-direction: column; gap: 6px;
}
.pv-dc .pv-site { font-size: 12px; -webkit-line-clamp: 1; }
.pv-dc .pv-title { color: #00a8fc; font-weight: 600; font-size: 15px; -webkit-line-clamp: 2; }
.pv-dc .pv-desc { -webkit-line-clamp: 3; }
.pv-dc canvas { border-radius: 4px; margin-top: 6px; }

.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(20px);
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-weight: 600;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; z-index: 20;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 1200px) {
  .workspace { grid-template-columns: 260px minmax(0, 1fr); }
  .props-panel { grid-column: 1 / -1; }
}
@media (max-width: 860px) {
  .outputs-grid { grid-template-columns: 1fr; }
}
@media (max-width: 720px) {
  .workspace { grid-template-columns: 1fr; padding: 12px 16px; }
  .stage { order: -1; }
  :root { --stage-h: 52vh; }
}
