:root {
  --bg: #0f172a;
  --panel: #1e293b;
  --panel-2: #273449;
  --text: #e2e8f0;
  --muted: #94a3b8;
  --accent: #38bdf8;
  --accent-2: #0ea5e9;
  --danger: #f87171;
  --canvas: #ffffff;
  --radius: 14px;
  --tap: 52px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-t: env(safe-area-inset-top, 0px);
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font: 15px/1.3 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  height: 100dvh;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
button { font: inherit; color: inherit; background: none; border: 0; cursor: pointer; }
.hidden { display: none !important; }
svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }

/* Top bar */
.topbar {
  display: flex; align-items: center; gap: 4px;
  padding: calc(6px + var(--safe-t)) 8px 6px;
  background: var(--panel);
  flex: 0 0 auto;
}
.topbar .title { flex: 1; font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; padding: 0 6px; }
.icon-btn { width: 44px; height: 44px; border-radius: 12px; display: grid; place-items: center; }
.icon-btn:active { background: var(--panel-2); }
.icon-btn[disabled] { opacity: .35; }

/* Stage */
.stage { position: relative; flex: 1 1 auto; min-height: 0; background: var(--canvas); overflow: hidden; }
#cad { position: absolute; inset: 0; width: 100%; height: 100%; touch-action: none; display: block; }
.hud {
  position: absolute; left: 10px; top: 10px;
  background: rgba(15,23,42,.82); color: #fff;
  padding: 6px 10px; border-radius: 10px; font-size: 13px;
  pointer-events: none; max-width: 70%;
  font-variant-numeric: tabular-nums;
}
.hud:empty { display: none; }
.chips {
  position: absolute; left: 0; right: 0; bottom: 8px;
  display: flex; gap: 6px; padding: 0 8px;
  overflow-x: auto; scrollbar-width: none;
}
.chips::-webkit-scrollbar { display: none; }
.chip-gap { flex: 1; }
.chip {
  flex: 0 0 auto; height: 36px; padding: 0 12px; border-radius: 18px;
  background: rgba(15,23,42,.75); color: #fff; font-size: 13px; font-weight: 600;
  backdrop-filter: blur(6px);
}
.chip.on { background: var(--accent-2); }
.chip.primary { background: #16a34a; }
.zoom { position: absolute; right: 10px; top: 10px; display: flex; flex-direction: column; gap: 6px; }
.zoom button {
  width: 40px; height: 40px; border-radius: 10px; font-size: 22px; font-weight: 600;
  background: rgba(15,23,42,.75); color: #fff;
}

/* Properties */
.props {
  flex: 0 0 auto; background: var(--panel-2);
  padding: 8px 10px; max-height: 38dvh; overflow-y: auto;
  border-top: 1px solid #334155;
}
.props .p-head { display: flex; align-items: center; gap: 8px; margin-bottom: 6px; }
.props .p-head b { flex: 1; }
.props .p-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 6px 10px; }
.props label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.props label span { flex: 0 0 auto; min-width: 20px; }
.props input[type=text], .props input[type=number] {
  flex: 1; min-width: 0; height: 36px; border-radius: 8px; border: 1px solid #475569;
  background: var(--panel); color: var(--text); padding: 0 8px; font: inherit;
  font-variant-numeric: tabular-nums;
}
.props .p-actions { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.props .p-actions button, .small-btn {
  height: 36px; padding: 0 12px; border-radius: 9px; background: var(--panel); font-weight: 600; font-size: 13px;
}
.props .p-actions button.danger { color: var(--danger); }
.swatches { display: flex; gap: 8px; flex-wrap: wrap; padding: 4px 0; }
.swatch { width: 30px; height: 30px; border-radius: 50%; border: 3px solid transparent; }
.swatch.on { border-color: #fff; box-shadow: 0 0 0 2px var(--accent); }

/* Toolbar */
.toolbar {
  flex: 0 0 auto; display: flex; gap: 2px; overflow-x: auto; scrollbar-width: none;
  background: var(--panel); padding: 6px 6px calc(6px + var(--safe-b));
}
.toolbar::-webkit-scrollbar { display: none; }
.tool {
  flex: 0 0 auto; width: 68px; height: 60px; border-radius: 12px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 3px;
  font-size: 11px; color: var(--muted);
}
.tool svg { width: 26px; height: 26px; }
.tool.on { background: var(--accent-2); color: #fff; }
.tool:active { background: var(--panel-2); }

/* Drawer */
.drawer { position: fixed; inset: 0; z-index: 20; }
.drawer-scrim { position: absolute; inset: 0; background: rgba(0,0,0,.5); }
.drawer-panel {
  position: absolute; left: 0; top: 0; bottom: 0; width: min(86vw, 340px);
  background: var(--panel); padding: calc(14px + var(--safe-t)) 14px calc(14px + var(--safe-b));
  overflow-y: auto; display: flex; flex-direction: column; gap: 14px;
  box-shadow: 4px 0 24px rgba(0,0,0,.4);
}
.drawer h2 { margin: 0 0 4px; font-size: 20px; }
.drawer-group { display: flex; flex-direction: column; gap: 4px; border-top: 1px solid #334155; padding-top: 10px; }
.drawer .row { text-align: left; height: 44px; padding: 0 10px; border-radius: 10px; font-size: 15px; }
.drawer .row:active { background: var(--panel-2); }
.drawer .row.danger { color: var(--danger); }
.row-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; padding: 4px 10px 0; }
.row-btns { display: flex; gap: 6px; padding: 4px 10px; flex-wrap: wrap; }
.row-btns button { height: 38px; padding: 0 14px; border-radius: 9px; background: var(--panel-2); font-weight: 600; }
.row-btns button.on { background: var(--accent-2); color: #fff; }
.drawer .swatches { padding: 4px 10px; }
.drawer-foot { margin-top: auto; font-size: 12px; color: var(--muted); }

/* Modal */
.modal { position: fixed; inset: 0; z-index: 30; background: rgba(0,0,0,.55); display: grid; place-items: center; padding: 16px; }
.modal-box { width: min(100%, 420px); background: var(--panel); border-radius: var(--radius); padding: 16px; }
.modal-box h3 { margin: 0 0 12px; font-size: 17px; }
.modal-box input[type=text], .modal-box input[type=number], .modal-box textarea {
  width: 100%; height: 42px; border-radius: 9px; border: 1px solid #475569;
  background: var(--panel-2); color: var(--text); padding: 0 10px; font: inherit;
}
.modal-box textarea { height: 90px; padding: 8px 10px; resize: vertical; }
.modal-btns { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.modal-btns button { height: 40px; padding: 0 16px; border-radius: 9px; background: var(--panel-2); font-weight: 600; }
.modal-btns button.primary { background: var(--accent-2); color: #fff; }
.list { display: flex; flex-direction: column; gap: 4px; max-height: 50dvh; overflow-y: auto; }
.list .item { display: flex; align-items: center; gap: 8px; height: 44px; padding: 0 8px; border-radius: 9px; background: var(--panel-2); }
.list .item b { flex: 1; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.list .item small { color: var(--muted); }
.list .item .del { color: var(--danger); font-size: 18px; width: 32px; }
.help { font-size: 14px; line-height: 1.5; max-height: 60dvh; overflow-y: auto; }
.help p { margin: 0 0 8px; }
.help b { color: var(--accent); }

/* Toast */
.toast {
  position: fixed; left: 50%; bottom: calc(150px + var(--safe-b)); transform: translateX(-50%);
  background: #0f172a; color: #fff; padding: 10px 16px; border-radius: 12px; z-index: 40;
  box-shadow: 0 4px 20px rgba(0,0,0,.4); font-size: 14px; max-width: 90vw; text-align: center;
}

@media (min-width: 700px) {
  .tool { width: 84px; }
  .props { max-height: 30dvh; }
}
