:root {
  --bg: #f5f6f8;
  --surface: #ffffff;
  --surface-2: #fafbfc;
  --border: #e6e8ec;
  --border-strong: #d6d9df;
  --text: #16181d;
  --muted: #6b7280;
  --muted-2: #9aa1ad;
  --accent: #2563eb;
  --accent-weak: #eaf0fe;
  --danger: #dc2626;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --shadow-lg: 0 12px 32px rgba(16, 24, 40, 0.14);
  --ui: "Inter", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --checker: #eef0f3;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0f1115;
    --surface: #181b21;
    --surface-2: #1e222a;
    --border: #272c35;
    --border-strong: #333a45;
    --text: #eceef2;
    --muted: #9aa1ad;
    --muted-2: #6b7280;
    --accent: #4f83ff;
    --accent-weak: #1b2740;
    --checker: #22262e;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 16px 40px rgba(0, 0, 0, 0.55);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: var(--ui);
  color: var(--text);
  background: var(--bg);
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
  display: flex;
  flex-direction: column;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 24px;
  padding: 12px 20px;
  background: color-mix(in srgb, var(--surface) 88%, transparent);
  backdrop-filter: saturate(1.2) blur(10px);
  border-bottom: 1px solid var(--border);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-shrink: 0;
}

.brand-mark {
  width: 24px;
  height: 24px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 1.8;
  stroke-linecap: round;
}

.brand-name {
  font-weight: 600;
  letter-spacing: -0.01em;
  font-size: 15px;
}
.brand-name .amp { color: var(--muted-2); font-weight: 500; }

.toolbar {
  display: flex;
  align-items: flex-end;
  gap: 16px;
  flex: 1;
  min-width: 0;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  min-width: 0;
}
.field label {
  font-size: 11px;
  font-weight: 500;
  color: var(--muted);
  letter-spacing: 0.02em;
  text-transform: uppercase;
}
.field--compact { width: 92px; flex-shrink: 0; }
#filename { width: 240px; }

input[type="text"],
input[type="number"],
select {
  font: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  outline: none;
  transition: border-color .12s, box-shadow .12s;
  width: 100%;
}
input:focus, select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-weak);
}
select { cursor: pointer; appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%236b7280' stroke-width='2.5' stroke-linecap='round'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat; background-position: right 10px center; padding-right: 28px;
}

.toolbar-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
}

/* ---------- Buttons ---------- */
.btn {
  font: inherit;
  font-weight: 500;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  padding: 9px 14px;
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, border-color .12s, color .12s, opacity .12s;
}
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn--primary { background: var(--accent); color: #fff; }
.btn--primary:not(:disabled):hover { background: color-mix(in srgb, var(--accent) 88%, #000); }
.btn--ghost { background: var(--surface); border-color: var(--border-strong); color: var(--text); }
.btn--ghost:hover { background: var(--surface-2); border-color: var(--muted-2); }
.linklike {
  background: none; border: none; padding: 0; margin: 0;
  color: var(--accent); font: inherit; font-weight: 500; cursor: pointer; text-decoration: underline;
  text-underline-offset: 2px;
}

/* ---------- Main / dropzone ---------- */
.main { flex: 1; padding: 24px 20px 64px; }

.dropzone {
  max-width: 640px;
  margin: 8vh auto 0;
  border: 1.5px dashed var(--border-strong);
  border-radius: 16px;
  background: var(--surface);
  padding: 56px 32px;
  text-align: center;
  cursor: pointer;
  transition: border-color .15s, background .15s, transform .15s;
  outline: none;
}
.dropzone:hover, .dropzone:focus-visible { border-color: var(--accent); background: var(--surface-2); }
.dropzone.dragover { border-color: var(--accent); background: var(--accent-weak); transform: scale(1.004); }
.dropzone-inner { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.dropzone-icon { width: 40px; height: 40px; fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; margin-bottom: 8px; }
.dropzone h1 { font-size: 18px; font-weight: 600; margin: 0; letter-spacing: -0.01em; }
.dropzone p { margin: 0; color: var(--muted); }
.dropzone-hint { font-size: 12px; color: var(--muted-2); margin-top: 10px; }

/* ---------- Board ---------- */
.board {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 18px;
  max-width: 1400px;
  margin: 0 auto;
  align-items: start;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: opacity .15s, border-color .15s;
}
.card.deselected { opacity: .5; }
.card.deselected .card-stage { pointer-events: none; }

.card-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.card-check { display: inline-flex; align-items: center; cursor: pointer; }
.card-check input { position: absolute; opacity: 0; width: 0; height: 0; }
.card-check .box {
  width: 18px; height: 18px; border-radius: 5px;
  border: 1.5px solid var(--border-strong); display: inline-flex;
  align-items: center; justify-content: center; transition: background .12s, border-color .12s;
}
.card-check input:checked + .box { background: var(--accent); border-color: var(--accent); }
.card-check .box svg { width: 12px; height: 12px; stroke: #fff; stroke-width: 3; fill: none; opacity: 0; }
.card-check input:checked + .box svg { opacity: 1; }
.card-check input:focus-visible + .box { box-shadow: 0 0 0 3px var(--accent-weak); }

.card-titles { display: flex; flex-direction: column; min-width: 0; flex: 1; }
.card-title { font-weight: 600; font-size: 13px; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-dims { font-size: 11.5px; color: var(--muted); font-variant-numeric: tabular-nums; }

.icon-btn {
  width: 28px; height: 28px; flex-shrink: 0;
  display: inline-flex; align-items: center; justify-content: center;
  border-radius: 6px; border: none; background: transparent; cursor: pointer; color: var(--muted);
  transition: background .12s, color .12s;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn.danger:hover { color: var(--danger); background: color-mix(in srgb, var(--danger) 10%, transparent); }
.icon-btn svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

.card-stage {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background:
    conic-gradient(var(--checker) 25%, transparent 0 50%, var(--checker) 0 75%, transparent 0) 0 0/16px 16px,
    var(--surface-2);
  min-height: 120px;
}
.stage-canvas {
  display: block;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.06), var(--shadow);
  cursor: grab;
  touch-action: none;
}
.stage-canvas:active { cursor: grabbing; }

.card-foot {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 14px 12px;
  border-top: 1px solid var(--border);
}
.zoom {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 8px;
}
.zoom svg { width: 14px; height: 14px; stroke: var(--muted); fill: none; stroke-width: 1.8; flex-shrink: 0; }
input[type="range"] {
  -webkit-appearance: none; appearance: none;
  width: 100%; height: 4px; border-radius: 4px; background: var(--border-strong); outline: none; cursor: pointer;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; appearance: none;
  width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); box-shadow: var(--shadow);
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; border-radius: 50%; background: var(--accent); border: 2px solid var(--surface); box-shadow: var(--shadow);
}
.filename-preview {
  font-size: 11px;
  color: var(--muted-2);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 45%;
}

/* ---------- Dialog ---------- */
.dialog {
  border: none;
  border-radius: var(--radius);
  padding: 0;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  width: min(420px, calc(100vw - 32px));
}
.dialog::backdrop { background: rgba(16, 24, 40, .35); backdrop-filter: blur(2px); }
.dialog-form { padding: 22px; display: flex; flex-direction: column; gap: 16px; }
.dialog-form h2 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.dialog-row { display: flex; align-items: flex-end; gap: 10px; }
.dialog-row .times { color: var(--muted-2); padding-bottom: 9px; }
.dialog-suffix { font-size: 12px; color: var(--muted); margin: 0; }
.dialog-suffix code { background: var(--surface-2); border: 1px solid var(--border); padding: 2px 6px; border-radius: 5px; font-size: 11.5px; word-break: break-all; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(8px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 16px;
  border-radius: 999px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: opacity .18s, transform .18s;
  pointer-events: none;
  z-index: 60;
}
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

/* ---------- Design editor ---------- */
.editor {
  border: none;
  padding: 0;
  border-radius: 14px;
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-lg);
  width: min(1120px, calc(100vw - 32px));
  max-width: calc(100vw - 32px);
  max-height: calc(100vh - 32px);
  overflow: hidden;
}
.editor::backdrop { background: rgba(16, 24, 40, .45); backdrop-filter: blur(3px); }

.editor-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.editor-titles { display: flex; align-items: baseline; gap: 10px; min-width: 0; }
.editor-titles h2 { margin: 0; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.editor-dims { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; flex-shrink: 0; }
.editor-head-actions { display: flex; gap: 8px; flex-shrink: 0; }

.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  padding: 10px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--surface-2);
}
.tool-group { display: flex; align-items: center; gap: 6px; }
.tool {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  padding: 7px 11px;
  cursor: pointer;
  transition: background .12s, border-color .12s;
}
.tool:hover { background: var(--accent-weak); border-color: var(--accent); }
.tool svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 1.7; stroke-linecap: round; stroke-linejoin: round; }
.tool rect { fill: none; }

.overlay-group { gap: 10px; }
.overlay-label { font-size: 12px; font-weight: 500; color: var(--muted); }
.overlay-group input[type="range"] { width: 110px; }
.overlay-val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; width: 34px; text-align: right; }

input[type="color"] {
  width: 34px; height: 30px; padding: 2px;
  border: 1px solid var(--border-strong); border-radius: 7px; background: var(--surface); cursor: pointer;
}
input[type="color"]::-webkit-color-swatch { border: none; border-radius: 4px; }
input[type="color"]::-webkit-color-swatch-wrapper { padding: 0; }
input[type="color"]:disabled { opacity: .4; cursor: not-allowed; }

.editor-body {
  display: grid;
  grid-template-columns: 1fr 288px;
  min-height: 0;
  height: min(64vh, 560px);
}
.editor-stage {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow: auto;
  background:
    conic-gradient(var(--checker) 25%, transparent 0 50%, var(--checker) 0 75%, transparent 0) 0 0/18px 18px,
    var(--surface-2);
}
#editorCanvas {
  display: block;
  border-radius: 4px;
  box-shadow: 0 0 0 1px rgba(0,0,0,.08), var(--shadow-lg);
  outline: none;
  touch-action: none;
}

.editor-props {
  border-left: 1px solid var(--border);
  padding: 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.props-empty { font-size: 12.5px; color: var(--muted); line-height: 1.5; margin: 0; }
.props-head { display: flex; align-items: center; justify-content: space-between; }
.props-type { font-size: 13px; font-weight: 600; }
.props-order { display: flex; gap: 2px; }
.props-fields { display: flex; flex-direction: column; gap: 11px; }
.prop { display: flex; flex-direction: column; gap: 5px; }
.prop > span { font-size: 11px; font-weight: 500; color: var(--muted); text-transform: uppercase; letter-spacing: 0.02em; }
.prop textarea {
  font: inherit; resize: vertical; min-height: 44px;
  color: var(--text); background: var(--surface);
  border: 1px solid var(--border-strong); border-radius: 7px; padding: 7px 9px; outline: none;
}
.prop textarea:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-weak); }
.color-toggle { display: flex; align-items: center; gap: 8px; }

/* Color control: brand swatches + custom picker + hex */
.colorfield { display: flex; flex-direction: column; gap: 7px; }
.swatches { display: flex; flex-wrap: wrap; gap: 5px; }
.swatch {
  width: 22px; height: 22px; border-radius: 6px; padding: 0; cursor: pointer;
  border: 1px solid rgba(0, 0, 0, .18);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, .12);
  transition: transform .1s, box-shadow .1s;
}
.swatch:hover { transform: scale(1.08); }
.swatch.active { box-shadow: 0 0 0 2px var(--surface), 0 0 0 4px var(--accent); }
.swatch--none {
  background:
    linear-gradient(45deg, transparent 45%, var(--danger) 45%, var(--danger) 55%, transparent 55%),
    var(--surface);
  background-size: 100% 100%;
}
.colorfield-row { display: flex; align-items: center; gap: 6px; }
.swatch-custom { width: 32px; height: 28px; flex-shrink: 0; }
.hex-input {
  width: 92px; font-variant-numeric: tabular-nums; text-transform: uppercase;
  padding: 6px 8px; font-size: 12.5px;
}

.pct-range { display: flex; align-items: center; gap: 9px; }
.pct-range input[type="range"] { flex: 1; }
.pct-val { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; width: 34px; text-align: right; }

/* Overlay control sits inline in the editor toolbar */
#overlayColorMount .colorfield { flex-direction: row; align-items: center; gap: 8px; }
#overlayColorMount .swatches { flex-wrap: nowrap; }
#overlayColorMount .swatch { width: 18px; height: 18px; }
#overlayColorMount .hex-input { width: 80px; }

@media (max-width: 720px) {
  .editor-body { grid-template-columns: 1fr; height: auto; }
  .editor-props { border-left: none; border-top: 1px solid var(--border); }
  .topbar { flex-wrap: wrap; gap: 12px; }
  .toolbar { flex-wrap: wrap; }
  #filename { width: 100%; }
  .field { flex: 1; }
  .toolbar-actions { width: 100%; margin-left: 0; }
  .toolbar-actions .btn { flex: 1; }
}
