:root {
  --bg: #f4f2ee;
  --grid: rgba(0, 0, 0, .06);
  --ink: #1e1e1e;
  --muted: #6b6b6b;
  --bar: #ffffff;
  --line: #d9d5cd;
  --accent: #2f6fde;
  --danger: #c0392b;
}

* { box-sizing: border-box; }

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

body {
  background: var(--bg);
  color: var(--ink);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

/* ---------- toolbar ---------- */
.toolbar {
  position: sticky; top: 0; z-index: 100000;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
  padding: 8px 16px;
  background: var(--bar);
  border-bottom: 1px solid var(--line);
}
.brand { margin-right: auto; }
.version { margin-left: 4px; font-size: 12px; font-weight: normal; color: var(--muted); }
.tools { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.fmt { display: inline-flex; gap: 2px; }

button {
  font: inherit; font-size: 14px;
  padding: 4px 10px; min-width: 32px;
  border: 1px solid var(--line); border-radius: 6px;
  background: #fff; color: var(--ink); cursor: pointer;
}
button:hover { border-color: var(--accent); }
.icon-btn {
  display: inline-grid; place-items: center;
  width: 32px; height: 32px; min-width: 0; padding: 0;
}
.icon-btn svg { display: block; }
.icon-btn .no { display: none; }
.icon-btn.off .no { display: inline; }
.icon-btn.off svg > circle, .icon-btn.off svg > path { opacity: .55; }
.toggle.on { background: var(--accent); border-color: var(--accent); color: #fff; }
.status { font-size: 13px; color: var(--muted); min-width: 6em; }
.status.err { color: var(--danger); }

.edit-only { display: none; }
body.editing .edit-only { display: flex; }

/* ---------- canvas ---------- */
.canvas {
  position: relative;
  min-height: calc(100vh - 50px);
}
body.editing .canvas {
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 16px 16px;
}

/* ---------- boxes ---------- */
.box {
  position: absolute;
  display: flex; flex-direction: column;
  border-radius: 8px;
  box-shadow: 0 1px 3px rgba(0,0,0,.12), 0 4px 14px rgba(0,0,0,.06);
  overflow: hidden;
}
.box-head {
  display: none;
  align-items: center; gap: 4px;
  height: 26px; padding: 0 4px 0 8px;
  background: rgba(0,0,0,.06);
  cursor: grab; touch-action: none; user-select: none;
  flex: none;
}
.box-head .grip { flex: 1; font-size: 12px; color: var(--muted); letter-spacing: 2px; }
.box-head button { padding: 0 6px; min-width: 22px; height: 20px; font-size: 12px; line-height: 1; }
.box-head input[type=color] { width: 22px; height: 20px; padding: 0; border: 1px solid var(--line); border-radius: 4px; background: none; cursor: pointer; }
.box-head .del:hover { border-color: var(--danger); color: var(--danger); }

.box-body {
  flex: 1; overflow: auto;
  padding: 10px 12px;
  outline: none;
  overflow-wrap: anywhere;
}
.box-body > :first-child { margin-top: 0; }
.box-body > :last-child { margin-bottom: 0; }
.box-body img { max-width: 100%; height: auto; }

.box-body.drop-target { box-shadow: inset 0 0 0 2px var(--accent); background: rgba(47,111,222,.06); }

.img-frame {
  position: fixed; z-index: 99999;
  outline: 2px solid var(--accent);
  pointer-events: none;
}
.img-frame[hidden] { display: none; }
.img-handle {
  position: absolute; right: -7px; bottom: -7px;
  width: 14px; height: 14px;
  background: var(--accent); border: 2px solid #fff; border-radius: 3px;
  cursor: nwse-resize; pointer-events: auto; touch-action: none;
}
.img-size {
  position: absolute; left: 0; top: -22px;
  padding: 1px 6px; border-radius: 4px;
  background: var(--accent); color: #fff;
  font-size: 11px; white-space: nowrap;
}

.handle { display: none; position: absolute; touch-action: none; }
.handle.e  { top: 0; right: 0; width: 8px; height: 100%; cursor: ew-resize; }
.handle.s  { left: 0; bottom: 0; height: 8px; width: 100%; cursor: ns-resize; }
.handle.se { right: 0; bottom: 0; width: 16px; height: 16px; cursor: nwse-resize;
  background: linear-gradient(135deg, transparent 50%, rgba(0,0,0,.25) 50%); }

body.editing .box { outline: 1px dashed rgba(0,0,0,.2); }
body.editing .box.selected { outline: 2px solid var(--accent); }
body.editing .box-head { display: flex; }
body.editing .handle { display: block; }
body.editing .box-body:focus { box-shadow: inset 0 0 0 2px rgba(47,111,222,.25); }

.box.dragging { opacity: .9; }
.box.dragging .box-head { cursor: grabbing; }
body.busy, body.busy * { user-select: none; }

.empty {
  position: absolute; inset: 0;
  display: grid; place-items: center;
  color: var(--muted);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161616; --grid: rgba(255,255,255,.05); --ink: #e8e8e8; --muted: #9a9a9a;
    --bar: #1f1f1f; --line: #3a3a3a;
  }
  button { background: #2a2a2a; }
  /* box colors are user-chosen backgrounds, so keep box text dark */
  .box { color: #1e1e1e; }
}
