:root {
  color-scheme: light dark;
  --bg: #f6f6f7;
  --surface: #ffffff;
  --surface-2: #f0f0f2;
  --canvas: #e4e4e8;
  --border: #dcdce1;
  --text: #17171a;
  --muted: #6b6b76;
  --accent: #2f6df6;
  --accent-soft: rgba(47, 109, 246, 0.12);
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.08), 0 8px 24px rgba(0, 0, 0, 0.12);
  --board-bg: repeating-conic-gradient(#fff 0 25%, #f0f0f2 0 50%) 0 0 / 16px 16px;
  --font: ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Geist, system-ui, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  /* one spacing scale and one control size for every button, field and list row */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --radius: 8px;
  --radius-sm: 6px;
  --control-h: 32px;
  --row-h: 30px;
  --tree-h: 26px;
  --icon: 16px;
  --font-size: 13px;
  --field-font-size: 13px;
  --topbar: calc(var(--control-h) + var(--space-3) * 2 - 8px);
}
/* touch screens: 40px targets, and 16px field text so iOS does not zoom when a field is focused */
@media (pointer: coarse) {
  :root {
    --control-h: 40px;
    --row-h: 40px;
    --tree-h: 36px;
    --font-size: 14px;
    --field-font-size: 16px;
  }
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #131316;
    --surface: #1b1b1f;
    --surface-2: #232328;
    --canvas: #101012;
    --border: #2d2d34;
    --text: #ececf0;
    --muted: #9a9aa6;
    --accent: #6d97ff;
    --accent-soft: rgba(109, 151, 255, 0.18);
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.5), 0 8px 24px rgba(0, 0, 0, 0.5);
    --board-bg: repeating-conic-gradient(#2a2a30 0 25%, #222227 0 50%) 0 0 / 16px 16px;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; overflow: hidden; }
body { background: var(--bg); color: var(--text); font: var(--font-size)/1.45 var(--font); -webkit-font-smoothing: antialiased; }
button, input, select { font: inherit; color: inherit; }
button { cursor: pointer; -webkit-tap-highlight-color: transparent; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.pad { padding: var(--space-3) var(--space-2); margin: 0; }

.app {
  position: relative;
  height: 100dvh;
  display: grid;
  grid-template-columns: 264px minmax(0, 1fr) 300px;
  grid-template-rows: var(--topbar) minmax(0, 1fr);
  grid-template-areas: "top top top" "side main insp";
}

/* controls: buttons, the document select and the filter field all share --control-h, --radius and the border */
.icon { width: var(--icon); height: var(--icon); flex: none; fill: none; stroke: currentColor; stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: var(--control-h); min-width: var(--control-h); padding: 0 var(--space-3);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  line-height: 1; white-space: nowrap; flex: none;
  transition: background-color 0.12s, border-color 0.12s;
}
.btn:hover { background: var(--surface-2); }
.btn:active { background: var(--border); }
.btn[aria-expanded="true"] { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.btn-icon { padding: 0; width: var(--control-h); }
.btn-icon-label { padding: 0 var(--space-3) 0 10px; }

.select { position: relative; display: inline-flex; min-width: 0; }
.select select {
  appearance: none; -webkit-appearance: none;
  height: var(--control-h); width: 100%; min-width: 0; padding: 0 calc(var(--space-3) + 18px) 0 var(--space-3);
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  font-size: var(--field-font-size); font-weight: 600; text-overflow: ellipsis; cursor: pointer;
  transition: background-color 0.12s;
}
.select select:hover { background: var(--surface-2); }
.select::after {
  content: ""; position: absolute; right: var(--space-3); top: 50%; width: 6px; height: 6px; margin-top: -5px;
  border-right: 1.5px solid var(--muted); border-bottom: 1.5px solid var(--muted); transform: rotate(45deg); pointer-events: none;
}
.select option { font-weight: 400; }

.field {
  width: 100%; height: var(--control-h); padding: 0 var(--space-3);
  background: var(--surface-2); border: 1px solid transparent; border-radius: var(--radius);
  font-size: var(--field-font-size);
}
.field:hover { border-color: var(--border); }
.field:focus { background: var(--surface); border-color: var(--accent); outline: none; box-shadow: 0 0 0 3px var(--accent-soft); }

/* top bar */
.topbar { grid-area: top; display: flex; align-items: center; gap: var(--space-2); padding: 0 var(--space-3); background: var(--surface); border-bottom: 1px solid var(--border); min-width: 0; }
.brand { display: inline-flex; align-items: center; gap: var(--space-2); color: inherit; text-decoration: none; flex: none; padding-right: var(--space-2); border-radius: var(--radius-sm); }
.logo { width: 24px; height: 24px; flex: none; }
.logo-bg { fill: var(--accent); }
.logo-guides { stroke: #fff; stroke-width: 2; stroke-linecap: round; fill: none; }
.logo-board { fill: #fff; }
.brand-name { font-weight: 650; letter-spacing: -0.01em; }
.doc-picker { flex: 0 1 220px; }
.spacer { flex: 1; }
.crumb { color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; flex: 0 1 auto; }
#toggle-sidebar, #toggle-inspector { display: none; }

/* sidebar */
.sidebar { grid-area: side; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-right: 1px solid var(--border); }
.pages { max-height: 50%; overflow: auto; padding: var(--space-2); border-bottom: 1px solid var(--border); display: flex; flex-direction: column; gap: 2px; flex: none; }
.page-item, .board-item { display: flex; align-items: center; justify-content: space-between; gap: var(--space-2); width: 100%; min-height: var(--row-h); text-align: left; background: none; border: 0; border-radius: var(--radius-sm); padding: 0 var(--space-2); flex: none; }
.page-item:hover, .board-item:hover, .tree-label:hover { background: var(--surface-2); }
.page-item[aria-current], .board-item[aria-current] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.page-name, .board-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
.count, .dim { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; flex: none; font-weight: 400; }
.filter { padding: var(--space-2); flex: none; }
.boards { flex: 1; overflow: auto; padding: 0 var(--space-2) var(--space-3); min-height: 0; display: flex; flex-direction: column; gap: 2px; }
.group > summary { cursor: pointer; display: flex; align-items: center; gap: 6px; min-height: var(--row-h); padding: 0 var(--space-2); border-radius: var(--radius-sm); color: var(--muted); font-weight: 600; font-size: 12px; list-style: none; }
.group > summary::-webkit-details-marker { display: none; }
.group > summary::before { content: ""; width: 5px; height: 5px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(-45deg); transition: transform 0.1s; flex: none; }
.group[open] > summary::before { transform: rotate(45deg); }
.group > summary:hover { background: var(--surface-2); }
.group { display: flex; flex-direction: column; gap: 2px; }
.group > .group, .group > .board-item { margin-left: var(--space-3); width: calc(100% - var(--space-3)); }

/* canvas */
.canvas-wrap { grid-area: main; position: relative; min-width: 0; min-height: 0; background: var(--canvas); }
.viewport { position: absolute; inset: 0; overflow: auto; outline: none; overscroll-behavior: contain; cursor: default; }
.viewport.pan-ready, .viewport.pan-ready * { cursor: grab; }
.viewport.panning, .viewport.panning * { cursor: grabbing; user-select: none; }
.canvas-pad { min-width: 100%; min-height: 100%; display: flex; padding: 24px; width: max-content; }
.stage { position: relative; margin: auto; flex: none; background: var(--board-bg); box-shadow: var(--shadow); }
.stage svg { position: absolute; inset: 0; display: block; }
.stage .overlay { pointer-events: none; }
.status { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px; text-align: center; padding: 24px; color: var(--muted); pointer-events: none; }
.status[data-kind="error"] { color: var(--text); background: color-mix(in srgb, var(--canvas) 85%, transparent); }
.status[data-kind="error"] .btn, .status .btn { pointer-events: auto; }
.status p { margin: 0; max-width: 40ch; }
.status[data-kind="loading"] { background: color-mix(in srgb, var(--canvas) 70%, transparent); }
.warnings { position: absolute; left: var(--space-3); bottom: var(--space-3); max-width: calc(100% - 240px); background: var(--surface); border: 1px solid var(--border); border-radius: 6px; padding: 4px 10px; font-size: 12px; color: var(--muted); box-shadow: var(--shadow); }

.zoom {
  position: absolute; right: var(--space-3); bottom: var(--space-3); z-index: 5;
  display: flex; align-items: center; gap: 2px; padding: var(--space-1);
  background: var(--surface); border: 1px solid var(--border); border-radius: calc(var(--radius) + var(--space-1)); box-shadow: var(--shadow);
}
.zoom .btn { border-color: transparent; background: none; }
.zoom .btn:hover { background: var(--surface-2); }
.zoom-level { min-width: 60px; padding: 0 var(--space-2); font-variant-numeric: tabular-nums; }
.zoom-sep { width: 1px; align-self: stretch; margin: var(--space-1) 2px; background: var(--border); }

/* inspector */
.inspector { grid-area: insp; display: flex; flex-direction: column; min-height: 0; background: var(--surface); border-left: 1px solid var(--border); }
.panel { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.panel + .panel { border-top: 1px solid var(--border); }
.panel-title { margin: 0; padding: var(--space-3) var(--space-3) var(--space-2); font-size: 11px; letter-spacing: 0.06em; text-transform: uppercase; color: var(--muted); font-weight: 600; flex: none; }
.tree, .props { overflow: auto; flex: 1; min-height: 0; }
.tree { padding: 0 var(--space-2) var(--space-2); }
.tree ul { list-style: none; margin: 0; padding: 0; }
.tree-row { display: flex; align-items: center; min-width: 0; }
.twisty { width: 20px; height: var(--tree-h); flex: none; background: none; border: 0; padding: 0; color: var(--muted); }
.twisty:not(.twisty-empty)::before { content: ""; display: inline-block; width: 5px; height: 5px; border-right: 1.5px solid currentColor; border-bottom: 1.5px solid currentColor; transform: rotate(-45deg); transition: transform 0.1s; }
.twisty[aria-expanded="true"]::before { transform: rotate(45deg); }
.tree-label { display: flex; align-items: center; gap: 6px; flex: 1; min-width: 0; background: none; border: 0; text-align: left; min-height: var(--tree-h); padding: 0 6px; border-radius: var(--radius-sm); }
.tree-label[aria-current] { background: var(--accent-soft); color: var(--accent); }
.tree-label .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tree-label .master { font-size: 11px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 40%; }
.kind { flex: none; width: 16px; height: 16px; border-radius: 4px; background: var(--surface-2); border: 1px solid var(--border); font-size: 10px; display: grid; place-items: center; color: var(--muted); }
.kind-symbolInstance, .kind-symbolMaster { color: #a24dd6; border-color: currentColor; }
.kind-text { color: var(--accent); }
.prop-head { padding: 0 var(--space-3) var(--space-2); display: flex; align-items: baseline; gap: 8px; }
.prop-head h2 { margin: 0; font-size: 14px; overflow-wrap: anywhere; }
.badge { font-size: 11px; color: var(--muted); border: 1px solid var(--border); border-radius: 10px; padding: 0 7px; flex: none; }
.prop-section { padding: var(--space-2) var(--space-3) var(--space-3); border-top: 1px solid var(--border); }
.prop-section h3 { margin: 0 0 var(--space-1); font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.05em; }
.prop-section dl { margin: 0; }
.prop { display: grid; grid-template-columns: minmax(0, 38%) minmax(0, 1fr); gap: var(--space-2); padding: 2px 0; }
.prop dt { color: var(--muted); overflow-wrap: anywhere; }
.prop dd { margin: 0; overflow-wrap: anywhere; font-variant-numeric: tabular-nums; }
.prop dt:empty { display: none; }
.prop:has(dt:empty) { grid-template-columns: 1fr; }
.swatch { display: inline-flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.swatch code { font-family: var(--mono); font-size: 12px; }
.chip { width: 14px; height: 14px; border-radius: 3px; border: 1px solid var(--border); flex: none; background: color-mix(in srgb, var(--chip) calc(var(--chip-a, 1) * 100%), transparent); }
.chip-grad { background: linear-gradient(135deg, #888, #ddd); }
.chip-img { background: repeating-conic-gradient(#999 0 25%, #ddd 0 50%) 0 0 / 6px 6px; }
.text-sample { white-space: pre-wrap; }

.scrim { display: none; }

/* tablet: the inspector is hidden until toggled, then docks as a side panel (no backdrop over the canvas) */
@media (max-width: 1100px) {
  .app { grid-template-columns: 240px minmax(0, 1fr); grid-template-areas: "top top" "side main"; }
  #toggle-inspector { display: inline-flex; }
  .inspector { display: none; }
  .app.show-inspector { grid-template-columns: 220px minmax(0, 1fr) minmax(260px, 300px); grid-template-areas: "top top top" "side main insp"; }
  .app.show-inspector .inspector { display: flex; }
  .crumb { display: none; }
}
/* tablet portrait: with the inspector open, the boards list becomes a drawer so the canvas keeps its width */
@media (max-width: 900px) {
  .app, .app.show-inspector { grid-template-columns: minmax(0, 1fr); grid-template-areas: "top" "main"; }
  .app.show-inspector { grid-template-columns: minmax(0, 1fr) minmax(280px, 320px); grid-template-areas: "top top" "main insp"; }
  #toggle-sidebar { display: inline-flex; }
  .sidebar { position: fixed; top: var(--topbar); left: 0; bottom: 0; width: min(320px, 88vw); z-index: 20; transform: translateX(-100%); visibility: hidden; transition: transform 0.18s, visibility 0s 0.18s; box-shadow: var(--shadow); }
  .app.show-sidebar .sidebar { transform: none; visibility: visible; transition: transform 0.18s; }
  .app.show-sidebar .scrim { display: block; position: fixed; inset: var(--topbar) 0 0 0; z-index: 15; background: rgba(0, 0, 0, 0.3); }
}
/* phone: the inspector becomes a bottom sheet that shares the screen with the canvas */
@media (max-width: 720px) {
  .app.show-inspector { grid-template-columns: minmax(0, 1fr); grid-template-rows: var(--topbar) minmax(0, 1fr) minmax(0, 45dvh); grid-template-areas: "top" "main" "insp"; }
  .topbar { padding: 0 var(--space-2); }
  .brand { padding-right: 0; }
  .brand-name, .btn-label, #copy-svg, .spacer { display: none; }
  .btn-icon-label { padding: 0; width: var(--control-h); }
  .doc-picker { flex: 1 1 auto; }
  .inspector { border-left: 0; border-top: 1px solid var(--border); box-shadow: 0 -4px 16px rgba(0, 0, 0, 0.12); }
  .warnings { max-width: calc(100% - var(--space-3) * 2); bottom: calc(var(--control-h) + var(--space-3) * 2 + var(--space-2)); }
}
@media (prefers-reduced-motion: reduce) { .sidebar, .inspector, .btn, .group > summary::before, .twisty::before { transition: none !important; } }
