/* Editor chrome, following Apple HIG conventions:
   system font (SF Pro on Apple platforms), 13 px body / 11 px captions,
   8-pt spacing grid, ≥ 28 px controls, clear 3 px focus rings, semantic
   colours with light & dark appearance, and reduced-motion support. */

:root {
  color-scheme: light dark;
  --font: -apple-system, BlinkMacSystemFont, 'SF Pro Text', 'Segoe UI Variable Text', 'Segoe UI', system-ui, sans-serif;
  --mono: ui-monospace, 'SF Mono', Menlo, Consolas, monospace;

  --bg: #ececec;
  --panel: #f6f6f7;
  --panel-raised: #ffffff;
  --label: #1d1d1f;
  --label-2: #6e6e73;
  --label-3: #8e8e93;
  --separator: rgba(0, 0, 0, 0.1);
  --fill: rgba(120, 120, 128, 0.12);
  --fill-hover: rgba(120, 120, 128, 0.2);
  --accent: #007aff;
  --accent-text: #ffffff;
  --accent-soft: rgba(0, 122, 255, 0.12);
  --danger: #d70015;
  --focus: rgba(0, 122, 255, 0.55);
  --stage: #dcdcdf;
  --seg-on: #ffffff;
  --radius: 8px;
  --radius-s: 6px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1e1e1e;
    --panel: #262628;
    --panel-raised: #323234;
    --label: #f5f5f7;
    --label-2: #a1a1a6;
    --label-3: #7c7c80;
    --separator: rgba(255, 255, 255, 0.1);
    --fill: rgba(120, 120, 128, 0.24);
    --fill-hover: rgba(120, 120, 128, 0.34);
    --accent: #0a84ff;
    --accent-soft: rgba(10, 132, 255, 0.2);
    --danger: #ff453a;
    --focus: rgba(10, 132, 255, 0.7);
    --stage: #141415;
    --seg-on: #636366; /* macOS dark selected-segment grey: clearly visible */
  }
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 13px/1.38 var(--font);
  color: var(--label);
  background: var(--bg);
  display: grid;
  grid-template-rows: auto 1fr;
  overflow: hidden;
  -webkit-font-smoothing: antialiased;
}
body:not(.ready) .workspace { opacity: 0; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; margin: -1px; padding: 0;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}
.skip-link { position: absolute; left: 8px; top: -40px; z-index: 10; background: var(--accent); color: #fff; padding: 8px 12px; border-radius: var(--radius-s); }
.skip-link:focus { top: 8px; }

/* Focus: always visible for keyboard users. */
:focus { outline: none; }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 1px; border-radius: var(--radius-s); }
input:focus-visible, select:focus-visible, textarea:focus-visible { outline-offset: 0; }
.seg-item input:focus-visible + label { outline: 3px solid var(--focus); outline-offset: 0; z-index: 1; }

code { font: 12px var(--mono); background: var(--fill); padding: 1px 4px; border-radius: 4px; }

.icon { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; flex: none; }

/* ---------- toolbar ---------- */
.toolbar {
  display: flex; align-items: center; gap: 16px;
  padding: 8px 16px; min-height: 52px;
  background: var(--panel); border-bottom: 1px solid var(--separator);
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 15px; font-weight: 600; letter-spacing: -0.01em; }
.brand-mark { width: 20px; height: 20px; border-radius: 6px; background: linear-gradient(135deg, #34c759, #007aff); }
.badge { font-size: 11px; font-weight: 500; color: var(--label-2); background: var(--fill); padding: 2px 6px; border-radius: 4px; }
.tool-group { display: flex; align-items: center; gap: 4px; }
.spacer { flex: 1; }
.zoom-value { min-width: 48px; text-align: center; font-variant-numeric: tabular-nums; color: var(--label-2); }

/* ---------- buttons ---------- */
button { font: inherit; color: inherit; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  min-height: 28px; padding: 4px 12px; border-radius: var(--radius-s);
  border: 1px solid var(--separator); background: var(--panel-raised);
  font-weight: 500; cursor: pointer; white-space: nowrap;
  box-shadow: 0 0.5px 1px rgba(0, 0, 0, 0.08);
}
.btn:hover:not(:disabled) { background: var(--fill-hover); }
.btn.primary { background: var(--accent); color: var(--accent-text); border-color: transparent; }
.btn.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
.btn.subtle { background: transparent; border-color: transparent; box-shadow: none; color: var(--accent); }
.btn.subtle:hover:not(:disabled) { background: var(--accent-soft); }
.btn.small { min-height: 24px; padding: 2px 8px; font-size: 12px; }
.btn.full { width: 100%; justify-content: flex-start; overflow: hidden; text-overflow: ellipsis; display: flex; }
.btn.full > span { overflow: hidden; text-overflow: ellipsis; }
.btn:disabled, .icon-btn:disabled { opacity: 0.4; cursor: default; }
.icon-btn {
  display: inline-grid; place-items: center; width: 28px; height: 28px;
  border: 0; border-radius: var(--radius-s); background: transparent; cursor: pointer; color: var(--label-2);
}
.icon-btn:hover:not(:disabled) { background: var(--fill-hover); color: var(--label); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 8px; }

/* ---------- form controls ---------- */
input, select, textarea { font: inherit; color: var(--label); }
input[type=number], input[type=text], select, textarea {
  min-height: 28px; padding: 4px 8px; border-radius: var(--radius-s);
  border: 1px solid var(--separator); background: var(--panel-raised); width: 100%;
}
textarea { resize: vertical; min-height: 56px; line-height: 1.4; }
input[aria-invalid=true] { border-color: var(--danger); box-shadow: 0 0 0 1px var(--danger); }
.compact-select { width: auto; }
.num { font-variant-numeric: tabular-nums; }
.num-wrap { position: relative; display: flex; align-items: center; }
.num-wrap .num { padding-right: 24px; min-width: 0; }
.unit { position: absolute; right: 8px; color: var(--label-3); font-size: 11px; pointer-events: none; }
/* Arrow keys (Shift = ×10 in most browsers) still step the value. */
input[type=number] { -moz-appearance: textfield; appearance: textfield; }
input[type=number]::-webkit-inner-spin-button, input[type=number]::-webkit-outer-spin-button { -webkit-appearance: none; margin: 0; }

input[type=range] { width: 100%; accent-color: var(--accent); min-height: 28px; margin: 0; }
.slider { display: grid; grid-template-columns: 1fr 76px; gap: 8px; align-items: center; }

.color { display: grid; grid-template-columns: 28px 1fr; gap: 8px; align-items: center; }
.swatch { width: 28px; height: 28px; padding: 0; border: 1px solid var(--separator); border-radius: var(--radius-s); background: none; cursor: pointer; }
.swatch::-webkit-color-swatch-wrapper { padding: 2px; }
.swatch::-webkit-color-swatch { border: 0; border-radius: 4px; }
.hex { font-family: var(--mono); text-transform: uppercase; }

.switch {
  appearance: none; width: 38px; height: 22px; border-radius: 11px; background: var(--fill-hover);
  position: relative; cursor: pointer; transition: background 0.15s; flex: none; margin: 0;
}
.switch::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 18px; height: 18px; border-radius: 50%;
  background: #fff; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3); transition: transform 0.15s;
}
.switch:checked { background: #34c759; }
.switch:checked::after { transform: translateX(16px); }

fieldset { border: 0; padding: 0; margin: 0; min-width: 0; }
.segmented { display: flex; background: var(--fill); border-radius: var(--radius-s); padding: 2px; gap: 2px; }
.seg-item { flex: 1; display: flex; position: relative; }
.seg-item label {
  flex: 1; display: grid; place-items: center; min-height: 24px; padding: 2px 8px;
  border-radius: 5px; cursor: pointer; font-weight: 500; color: var(--label-2);
}
.seg-item input:checked + label { background: var(--seg-on); color: var(--label); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.12), 0 0 0 0.5px rgba(0, 0, 0, 0.06); }

/* ---------- layout ---------- */
.workspace { display: grid; grid-template-columns: 260px 1fr 300px; min-height: 0; transition: opacity 0.2s; }
.sidebar { background: var(--panel); overflow-y: auto; min-height: 0; }
.sidebar.left { border-right: 1px solid var(--separator); }
.sidebar.right { border-left: 1px solid var(--separator); }

.section { padding: 12px 16px 16px; border-bottom: 1px solid var(--separator); display: grid; gap: 8px; }
.section-title { margin: 0 0 2px; font-size: 11px; font-weight: 600; color: var(--label-2); letter-spacing: 0.02em; text-transform: uppercase; }
.row { display: grid; grid-template-columns: 96px 1fr; align-items: center; gap: 8px; }
.row.stacked { grid-template-columns: 1fr; gap: 4px; }
.row-label { color: var(--label-2); }
.seg-row { grid-template-columns: 96px 1fr; }
.seg-row legend { float: left; padding: 0; }
.pair { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.mini { display: grid; grid-template-columns: auto 1fr; gap: 6px; align-items: center; }
.mini-label { color: var(--label-2); min-width: 14px; }
.hint { margin: 0; color: var(--label-2); font-size: 12px; }
.stop { display: grid; gap: 8px; padding: 8px; background: var(--fill); border-radius: var(--radius); }

.inspector-head { padding: 16px 16px 8px; }
.inspector-head h1 { margin: 0; font-size: 17px; font-weight: 600; letter-spacing: -0.01em; }
.inspector-head p { margin: 2px 0 0; color: var(--label-2); }

.asset-preview { display: grid; grid-template-columns: 64px 1fr; gap: 12px; align-items: center; }
.asset-preview img { width: 64px; height: 64px; object-fit: contain; border-radius: var(--radius-s); background: var(--panel-raised); border: 1px solid var(--separator); }
.asset-preview.checker img {
  background: conic-gradient(#e6e6e6 25%, #fff 0 50%, #e6e6e6 0 75%, #fff 0) 0 0 / 12px 12px;
}
.asset-preview small { color: var(--label-2); }

.shortcuts { margin: 0; display: grid; grid-template-columns: auto 1fr; gap: 4px 12px; font-size: 12px; }
.shortcuts dt { color: var(--label-2); }
.shortcuts dd { margin: 0; text-align: right; font-family: var(--mono); font-size: 11px; }

/* ---------- left: templates & layers ---------- */
.template-list { display: grid; gap: 8px; }
.template-card {
  display: grid; gap: 6px; text-align: left; padding: 8px; border-radius: var(--radius);
  border: 1px solid var(--separator); background: var(--panel-raised); cursor: pointer;
}
.template-card[aria-pressed=true] { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.template-card .thumb { width: 100%; height: auto; border-radius: 4px; box-shadow: 0 0 0 1px var(--separator); background: #fff; }
.template-name { font-weight: 600; }
.template-meta { color: var(--label-2); font-size: 12px; text-transform: capitalize; }

.layer-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 1px; }
.layer { display: flex; align-items: center; border-radius: var(--radius-s); }
.layer:hover { background: var(--fill); }
.layer.selected { background: var(--accent-soft); }
.layer.child .layer-main { padding-left: 28px; }
.layer.muted .layer-name { color: var(--label-3); }
.layer-main {
  flex: 1; display: flex; align-items: center; gap: 8px; min-height: 28px; min-width: 0;
  padding: 4px 8px; border: 0; background: none; text-align: left; cursor: pointer; border-radius: var(--radius-s);
}
.layer-main.static { cursor: default; }
.layer-main .icon { color: var(--label-2); }
.layer.selected .layer-main .icon { color: var(--accent); }
.layer-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.layer .icon-btn { width: 24px; height: 24px; }
.plain-list { list-style: none; padding: 0; margin: 0; display: grid; gap: 2px; }

/* ---------- stage ---------- */
.stage { position: relative; background: var(--stage); min-width: 0; min-height: 0; overflow: auto; }
.surface { min-width: 100%; min-height: 100%; display: grid; place-items: center; padding: 48px; width: max-content; }
.surface:focus-visible { outline-offset: -4px; border-radius: 0; }
.surface.drop .frame { outline: 3px dashed var(--accent); outline-offset: 6px; }
.frame { position: relative; box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15), 0 8px 24px rgba(0, 0, 0, 0.12); }
.banner-canvas { display: block; }
.overlay { position: absolute; inset: 0; cursor: default; touch-action: none; }
.surface.dragging .overlay { cursor: grabbing; }
.size-badge {
  position: sticky; left: 12px; bottom: 12px; float: left; margin-top: -36px;
  font-size: 11px; color: var(--label-2); background: var(--panel); padding: 4px 8px;
  border-radius: var(--radius-s); border: 1px solid var(--separator); font-variant-numeric: tabular-nums;
}

.box { position: absolute; transform-origin: center; pointer-events: none; }
.box.selection { outline: 1.5px solid var(--accent); }
.box.selection.group { outline-style: dashed; }
.box.placeholder {
  border: 1.5px dashed #8e8e93; background: rgba(142, 142, 147, 0.12);
  display: grid; place-items: center; overflow: hidden;
}
.box.placeholder span { font-size: 10px; color: #6e6e73; text-align: center; padding: 2px; }
.handle {
  position: absolute; width: 10px; height: 10px; background: #fff; border: 1.5px solid var(--accent);
  border-radius: 2px; pointer-events: auto; margin: -5px;
}
.handle::before { content: ''; position: absolute; inset: -8px; } /* larger hit area */
.handle.nw { left: 0; top: 0; cursor: nwse-resize; }
.handle.ne { left: 100%; top: 0; cursor: nesw-resize; }
.handle.se { left: 100%; top: 100%; cursor: nwse-resize; }
.handle.sw { left: 0; top: 100%; cursor: nesw-resize; }
.handle.w { left: 0; top: 50%; cursor: ew-resize; }
.handle.e { left: 100%; top: 50%; cursor: ew-resize; }
.handle.rotate { left: 50%; top: -24px; border-radius: 50%; cursor: grab; }
.rotate-stem { position: absolute; left: 50%; top: -19px; height: 19px; border-left: 1.5px solid var(--accent); }
.sel-label {
  position: absolute; left: -1px; bottom: calc(100% + 4px); white-space: nowrap;
  font-size: 11px; font-weight: 500; color: #fff; background: var(--accent); padding: 1px 6px; border-radius: 4px;
}
.box.selection:has(.rotate) .sel-label { bottom: auto; top: calc(100% + 6px); }

/* ---------- dialog & toasts ---------- */
dialog {
  border: 0; border-radius: 12px; padding: 0; max-width: 560px; width: calc(100vw - 32px);
  background: var(--panel-raised); color: var(--label);
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.3), 0 0 0 0.5px rgba(0, 0, 0, 0.2);
}
dialog::backdrop { background: rgba(0, 0, 0, 0.3); }
.dialog-body { padding: 24px; display: grid; gap: 16px; }
.dialog-body h1 { margin: 0; font-size: 20px; font-weight: 600; }
.dialog-body h2 { margin: 0 0 4px; font-size: 13px; }
.lead { margin: 0; color: var(--label-2); }
.steps { margin: 0; padding-left: 20px; display: grid; gap: 8px; }
.dialog-actions { display: flex; gap: 8px; flex-wrap: wrap; }
.dialog-footer { display: flex; justify-content: flex-end; }
.status { margin: 0; min-height: 18px; color: var(--label-2); }
.notes { background: var(--fill); padding: 12px; border-radius: var(--radius); }
.notes ul { margin: 0; padding-left: 18px; }

.toasts { position: fixed; bottom: 16px; left: 50%; transform: translateX(-50%); display: grid; gap: 8px; z-index: 50; }
.toast {
  background: rgba(30, 30, 32, 0.92); color: #fff; padding: 8px 14px; border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.25); max-width: 480px; transition: opacity 0.3s;
}
.toast.error { background: var(--danger); }
.toast.out { opacity: 0; }

/* Controls may shrink inside narrow sidebars instead of overflowing them. */
.sidebar { overflow-x: hidden; }
.row, .seg-row { grid-template-columns: 96px minmax(0, 1fr); }
.row > *, .pair > *, .slider > *, .color > * { min-width: 0; }
.seg-item label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.toolbar { flex-wrap: wrap; row-gap: 8px; }

@media (max-width: 1100px) {
  .workspace { grid-template-columns: 220px minmax(0, 1fr) 272px; }
  .row, .seg-row { grid-template-columns: 84px minmax(0, 1fr); }
  .section { padding: 12px; }
  .badge { display: none; }
}
@media (max-width: 860px) {
  /* Narrow windows: stage on top, panels side by side below it. */
  body { overflow: auto; }
  .workspace { grid-template-columns: 1fr 1fr; grid-template-rows: minmax(260px, 45vh) auto; }
  .stage { grid-column: 1 / -1; grid-row: 1; }
  .sidebar.left { grid-row: 2; border-right: 1px solid var(--separator); }
  .sidebar.right { grid-row: 2; border-left: 0; }
  .sidebar { overflow-y: visible; }
  .toolbar .tool-group[aria-label=Zoom] { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}
