:root {
  --bg: #ffffff;
  --panel: #ffffff;
  --line: #e4e4e7;
  --text: #18181b;
  --muted: #71717a;
  --hover: #f1f1f3;
  --accent: #2563eb;
  --accent-soft: #dbe7ff;
  --sel-root: #e4f0ff;
  --sel-root-hover: #daeaff;
  --sel-child: #f1f7ff;
  --sel-child-hover: #e7f2ff;
  --comp: #7c3aed;
  --comp-soft: #f3e8ff;
  --warn: #b45309;
  --ok: #16a34a;
  --error: #dc2626;
  --pin: #f59e0b;
  --track: #f0f0f2;
  --thumb: #ffffff;
  --shadow-icon: #a1a1aa;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #18181b;
    --panel: #18181b;
    --line: #313138;
    --text: #f4f4f5;
    --muted: #a1a1aa;
    --hover: #2a2a30;
    --accent: #60a5fa;
    --accent-soft: #1e3a5f;
    --sel-root: #1e3a5f;
    --sel-root-hover: #244570;
    --sel-child: #17273d;
    --sel-child-hover: #1c304b;
    --comp: #a78bfa;
    --comp-soft: #2e1065;
    --warn: #fbbf24;
    --ok: #4ade80;
    --error: #f87171;
    --track: #27272c;
    --thumb: #3a3a42;
    --shadow-icon: #71717a;
  }
}

* { box-sizing: border-box; }
html, body, #root { height: 100%; margin: 0; overscroll-behavior: none; }
body {
  font: 12px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", sans-serif;
  color: var(--text);
  background: var(--bg);
}
body[data-resizing] { user-select: none; }
body[data-resizing="vertical"] * { cursor: col-resize !important; }
body[data-resizing="horizontal"] * { cursor: row-resize !important; }
button { font: inherit; color: inherit; user-select: none; }
svg.lucide { flex: none; display: block; }

#app {
  display: grid;
  grid-template: 44px minmax(0, 1fr) / auto minmax(0, 1fr) auto;
  grid-template-areas: "bar bar bar" "left stage right";
  height: 100%;
}
#app.panels-hidden #left,
#app.panels-hidden #right { display: none; }

/* Toolbar */
#bar {
  grid-area: bar;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 12px 0 8px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
  min-width: 0;
}
#file { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.spacer { flex: 1; }
#status { display: inline-flex; align-items: center; gap: 5px; color: var(--muted); white-space: nowrap; }
#status > svg { color: var(--ok); }
#status.warn { color: var(--warn); }
#status.warn > svg { color: var(--warn); }
#status.bad { color: var(--error); }
#status.bad > svg { color: var(--error); }
#zoom { font-family: ui-monospace, Menlo, monospace; width: 50px; }
.group { display: flex; border: 1px solid var(--line); border-radius: 6px; overflow: hidden; flex: none; }
.group button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  border: 0;
  background: transparent;
  padding: 5px 10px;
  cursor: pointer;
  min-width: 30px;
}
.group button + button { border-left: 1px solid var(--line); }
.group button:hover { background: var(--hover); }
.group button.on { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.group.plain { border: 0; gap: 4px; overflow: visible; }
.group.plain button { width: 30px; height: 30px; min-width: 0; padding: 0; border-radius: 6px; color: var(--muted); }
.group.plain button + button { border-left: 0; }
.group.plain button:hover { color: var(--text); }
.group.plain button.on { color: var(--accent); }
.group.segmented { border: 0; gap: 2px; padding: 2px; border-radius: 8px; background: var(--track); overflow: visible; }
.group.segmented button { padding: 3px 10px; min-width: 0; border-radius: 6px; color: var(--muted); }
.group.segmented button + button { border-left: 0; }
.group.segmented button:hover { background: transparent; color: var(--text); }
.group.segmented button.on { background: var(--thumb); color: var(--accent); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.1), 0 0 0 0.5px rgba(0, 0, 0, 0.06); }

/* Side panels & resize handles */
.side-panel {
  position: relative;
  width: var(--panel-w, 260px);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--panel);
}
#left { grid-area: left; border-right: 1px solid var(--line); }
#right { grid-area: right; border-left: 1px solid var(--line); }

.resize-handle { position: absolute; z-index: 20; touch-action: none; }
.resize-handle::after {
  content: "";
  position: absolute;
  background: transparent;
  transition: background 0.12s ease;
}
.resize-handle:hover::after,
.resize-handle.is-dragging::after {
  background: var(--accent);
}
.resize-vertical { top: 0; bottom: 0; width: 6px; cursor: col-resize; }
.resize-vertical::after { top: 0; bottom: 0; left: 2px; width: 2px; }
.side-left > .resize-vertical { right: -3px; }
.side-right > .resize-vertical { left: -3px; }

.resize-horizontal { left: 0; right: 0; top: -3px; height: 6px; cursor: row-resize; z-index: 15; }
.resize-horizontal::after { left: 0; right: 0; top: 2px; height: 2px; }

/* Collapsible & stacked sections */
.panel-section {
  position: relative;
  display: flex;
  flex-direction: column;
  min-height: 72px;
  flex: 1 1 0;
  border-top: 1px solid var(--line);
}
.panel-section:first-child { border-top: 0; }
.panel-section.is-resizable { flex: 0 0 var(--section-h, 220px); min-height: 96px; max-height: 75%; }
.panel-section.is-collapsed { flex: 0 0 auto; min-height: 0; max-height: none; }
.panel-section.is-collapsed + .panel-section:not(.is-collapsed) { flex: 1 1 0; max-height: none; }
.panel-section.is-collapsed + .panel-section > .resize-horizontal { display: none; }

.section-header {
  display: flex;
  align-items: center;
  gap: 4px;
  width: 100%;
  padding: 7px 12px 7px 8px;
  border: 0;
  background: transparent;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
  cursor: pointer;
  user-select: none;
  text-align: left;
  flex: none;
}
.section-header:hover { color: var(--text); background: var(--hover); }
/* Header buttons sit over the header's right end; the header leaves room for them. */
.has-actions > .section-header { padding-right: 40px; }
.section-actions { position: absolute; top: 0; right: 6px; height: 30px; display: flex; align-items: center; }
.section-actions .group.plain button { width: 24px; height: 24px; }
.section-chevron { display: inline-grid; place-items: center; width: 14px; height: 14px; flex: none; }
.section-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { font-weight: 400; text-transform: none; letter-spacing: 0; }
.scroll { overflow: auto; min-height: 0; flex: 1 1 0; padding: 2px 0 8px; }
.empty { color: var(--muted); padding: 4px 12px 8px; }

/* Figma-style Layers */
.layer {
  display: flex;
  align-items: center;
  gap: 8px;
  height: 30px;
  margin: 0 6px;
  padding: 0 8px 0 6px;
  border-radius: 6px;
  white-space: nowrap;
  cursor: pointer;
  user-select: none;
}
.layer.artboard { font-weight: 600; margin-top: 4px; }
.layer.artboard:first-child { margin-top: 0; }
.layer .twisty {
  flex: none;
  width: 14px;
  height: 14px;
  display: inline-grid;
  place-items: center;
  color: var(--muted);
  border-radius: 3px;
}
.layer .twisty:not(.empty-twisty):hover { color: var(--text); background: rgba(127, 127, 127, 0.14); }
.layer .layer-icon { flex: none; color: var(--muted); }
.layer.artboard .layer-icon { color: var(--text); }
.layer .type { font-weight: 450; }
.layer.artboard .type { font-weight: 600; }
.layer .detail { color: var(--muted); overflow: hidden; text-overflow: ellipsis; min-width: 0; }

/* Component & component-descendant tint (orthogonal to selection background) */
.layer.in-component .layer-icon,
.layer.in-component .type { color: var(--comp); }
.layer.in-component .twisty { color: var(--comp); opacity: 0.75; }
.layer.component .type { font-weight: 550; }

/* Hover & contiguous parent+children selection block */
.layer:not(.selected):not(.selected-descendant):hover,
.layer:not(.selected):not(.selected-descendant).hover {
  background: var(--hover);
}
.layer.selected { background: var(--sel-root); }
.layer.selected:hover,
.layer.selected.hover { background: var(--sel-root-hover); }
.layer.selected:not(.in-component) .layer-icon { color: var(--text); }

.layer.selected-descendant { background: var(--sel-child); }
.layer.selected-descendant:hover,
.layer.selected-descendant.hover { background: var(--sel-child-hover); }

.layer.sel-single { border-radius: 6px; }
.layer.sel-top { border-radius: 6px 6px 0 0; }
.layer.sel-middle { border-radius: 0; }
.layer.sel-bottom { border-radius: 0 0 6px 6px; }

.layer.failed .layer-icon,
.layer.failed .type { color: var(--error); }

/* Problems and comments */
.item { padding: 6px 12px; cursor: pointer; border-top: 1px solid var(--line); }
.item:first-child { border-top: 0; }
.item:hover { background: var(--hover); }
.item .head { display: flex; gap: 5px; align-items: center; color: var(--muted); }
.item .sev-icon { color: var(--warn); }
.item.error .sev-icon { color: var(--error); }
.item .rule { font-weight: 600; color: var(--warn); }
.item.error .rule { color: var(--error); }
.item .body { margin-top: 2px; overflow-wrap: anywhere; }
.item .actions { display: flex; gap: 10px; margin-top: 4px; }
.item.resolved { opacity: 0.55; }
.link {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  border: 0;
  background: none;
  padding: 0;
  color: var(--accent);
  cursor: pointer;
}
.link:hover { text-decoration: underline; }
.badge { color: var(--warn); }
.num {
  display: inline-grid;
  place-items: center;
  min-width: 18px;
  height: 18px;
  padding: 0 4px;
  border-radius: 9px;
  background: var(--pin);
  color: #1c1917;
  font-weight: 700;
  font-size: 11px;
}

/* Properties */
.prop-head { padding: 4px 12px 2px; }
.prop-head .title {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  font-weight: 600;
}
.prop-head .title .layer-icon { color: var(--muted); }
.prop-head .title.comp-title,
.prop-head .title.comp-title .layer-icon { color: var(--comp); }
.prop-head .source {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 2px;
  margin-top: 2px;
  color: var(--muted);
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
}
.prop-head .owner { display: flex; align-items: center; gap: 4px; margin-top: 2px; color: var(--muted); font-size: 11px; }
.prop-head .owner .link { color: var(--comp); }
.prop-group { padding-bottom: 4px; }
.prop-group-title { display: flex; justify-content: space-between; align-items: center; padding: 10px 12px 4px; font-weight: 600; }
.prop-group .item { border-top: 0; }
.prop-group .note { padding: 3px 12px; color: var(--muted); }
/* First column as wide as a row label, so Y and H line up with the row values below. */
.metrics { display: grid; grid-template-columns: 96px minmax(0, 1fr); gap: 0 8px; padding: 0 12px; }
.metric { display: flex; gap: 8px; padding: 3px 0; font-variant-numeric: tabular-nums; }
.metric .k { color: var(--muted); width: 14px; flex: none; }
.metric .mode { color: var(--muted); }
.rows { padding: 0 12px; }
.row { display: flex; gap: 8px; padding: 3px 0; align-items: baseline; }
.row .k { color: var(--muted); width: 96px; flex: none; }
.row .v { font-family: ui-monospace, Menlo, monospace; font-size: 11px; overflow-wrap: anywhere; min-width: 0; }
.swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  margin-right: 6px;
  border-radius: 2px;
  border: 1px solid var(--line);
  vertical-align: -1px;
}
/* Shadows section: a named entry per shadow, its values indented under the name. */
.shadow { padding: 3px 12px 6px; }
.shadow-head { display: flex; align-items: center; gap: 8px; padding: 2px 0 4px; }
.shadow-icon { width: 12px; height: 12px; margin: 1px; border: 1.5px solid var(--text); border-radius: 3px; background: var(--panel); flex: none; }
.shadow-grid, .shadow-color { margin-left: 22px; }
.shadow-grid { display: grid; grid-template-columns: 44px minmax(0, 1fr) 52px minmax(0, 1fr); gap: 0 8px; font-variant-numeric: tabular-nums; }
.shadow-grid > span { padding: 3px 0; }
.shadow-grid .k { color: var(--muted); }
.shadow-color { display: flex; align-items: center; gap: 8px; padding: 3px 0; font-variant-numeric: tabular-nums; }
.shadow-color .swatch { margin-right: 0; width: 14px; height: 14px; vertical-align: 0; }
.variant { display: flex; gap: 8px; align-items: center; padding: 3px 12px; }
.variant .k { color: var(--muted); width: 96px; flex: none; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.variant .group { flex: 0 1 auto; min-width: 0; }
.variant select { min-width: 0; max-width: 100%; font: inherit; color: inherit; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 3px 6px; }
.text-content { padding: 3px 12px; white-space: pre-wrap; overflow-wrap: anywhere; }

/* Stage */
#stage { grid-area: stage; position: relative; overflow: hidden; min-width: 0; touch-action: none; cursor: default; }
#stage.panning { cursor: grabbing; }
#stage.commenting { cursor: crosshair; }
#stage.playing.clickable { cursor: pointer; }
#notice {
  position: absolute;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--bg);
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.12);
  pointer-events: none;
}
/* Screen-sized and pinned to the window's corner by the renderer; the stage clips it. */
#canvas { position: absolute; left: 0; top: 0; display: block; }
#overlay { position: absolute; inset: 0; pointer-events: none; overflow: hidden; --s: 1; --tx: 0; --ty: 0; }

/* Pinned to canvas point (--x, --y). The renderer updates --s/--tx/--ty on
   #overlay every frame; --shift is an extra screen-space offset. */
.anchored {
  position: absolute;
  left: 0;
  top: 0;
  transform: translate(calc(var(--tx) * 1px + var(--s) * var(--x) * 1px), calc(var(--ty) * 1px + var(--s) * var(--y) * 1px))
    var(--shift, translate(0, 0));
}
#boot { position: absolute; inset: 0; display: grid; place-items: center; color: var(--muted); }
#boot[hidden] { display: none; }

.frame-label {
  padding-bottom: 6px;
  color: var(--muted);
  white-space: nowrap;
  pointer-events: auto;
  cursor: pointer;
}
.frame-label:hover, .frame-label.selected { color: var(--accent); }
/* At most as wide as the artboard on screen, one line tall. Parts that don't
   fit wrap onto a second line, which is clipped away, so they drop out whole. */
.frame-label.above {
  /* The 6px gap above the artboard lives in the offset, not padding, so the clip box is exactly one line. */
  --shift: translateY(calc(-100% - 6px));
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  column-gap: 8px;
  /* Pushes anything that wraps far below the clip, whatever the font's line height. */
  row-gap: 2em;
  max-width: calc(var(--s) * var(--w) * 1px);
  height: 1.45em;
  line-height: 1.45;
  padding-bottom: 0;
  overflow: hidden;
}
.frame-label .frame-name { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.frame-label.above .modified { margin-left: 0; }
.frame-label .modified { display: inline-flex; align-items: center; gap: 3px; margin-left: 8px; color: var(--warn); }
.frame-label .modified button { display: inline-flex; padding: 1px; border: 0; background: none; color: inherit; cursor: pointer; }
.frame-label .modified button:hover { color: var(--text); }
.frame-label.failed { width: calc(var(--s) * var(--w) * 1px); height: calc(var(--s) * var(--h) * 1px); max-width: none; padding: 8px 10px; overflow: hidden; color: var(--error); cursor: default; }
.frame-label .failed-head { display: inline-flex; align-items: center; gap: 5px; font-weight: 600; }
.frame-label .err { display: block; margin-top: 6px; white-space: normal; overflow-wrap: anywhere; font-family: ui-monospace, Menlo, monospace; font-size: 11px; }
/* Matches the canvas selection outline (renderer.ts), which keeps one color in both schemes. */
.size-badge {
  --shift: translate(-50%, 6px);
  padding: 1px 6px;
  border-radius: 4px;
  background: #2563eb;
  color: #fff;
  font-size: 11px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
}
.size-badge.comp { background: #7c3aed; }
/* Distance labels for Option/Alt measuring (renderer.ts draws the lines in the same red).
   Beside a vertical line, below a horizontal one. */
.measure-label {
  padding: 1px 5px;
  border-radius: 4px;
  background: #f24822;
  color: #fff;
  font-size: 11px;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  pointer-events: none;
}
.measure-label.x { --shift: translate(-50%, 4px); }
.measure-label.y { --shift: translate(4px, -50%); }
.pin {
  margin: -22px 0 0 0;
  width: 22px;
  height: 22px;
  border-radius: 11px 11px 11px 2px;
  border: 2px solid #fff;
  background: var(--pin);
  color: #1c1917;
  font-weight: 700;
  font-size: 11px;
  display: grid;
  place-items: center;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35);
  pointer-events: auto;
  cursor: pointer;
}
.pin.detached { background: #a1a1aa; border-style: dashed; }
.popover {
  --shift: translate(8px, 8px);
  width: 260px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--bg);
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.22);
  pointer-events: auto;
}
.popover .target {
  display: flex;
  align-items: center;
  gap: 4px;
  color: var(--muted);
  margin-bottom: 6px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.popover .target-icon { color: var(--comp); }
.popover .target-frame { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; }
.popover .target-node { flex: 0 1 auto; min-width: 0; overflow: hidden; text-overflow: ellipsis; color: var(--text); }
.popover textarea {
  width: 100%;
  height: 72px;
  resize: none;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
}
.popover textarea:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.popover .actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 8px; }
.btn { border: 1px solid var(--line); border-radius: 6px; background: var(--bg); padding: 5px 12px; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn:disabled { opacity: 0.5; cursor: default; }

/* Narrow screens: canvas first, properties as a bottom sheet. */
@media (max-width: 820px) {
  #app { grid-template: 44px minmax(0, 1fr) auto / minmax(0, 1fr); grid-template-areas: "bar" "stage" "right"; }
  #left, #status, #zoom-out, #zoom-in { display: none; }
  #right { width: auto; border-left: 0; border-top: 1px solid var(--line); max-height: 42vh; display: block; overflow: auto; }
  #right > .resize-handle, #right .resize-horizontal { display: none; }
  #right .panel-section { flex: none; max-height: none; min-height: 0; }
  #right .scroll { max-height: none; overflow: visible; }
  #app:not(.has-selection) #right { display: none; }
}
