:root {
  --bg: #fffaf7; --surface: #fff; --peach: #fff0e6; --peach2: #ffd8c2;
  --ink: #17211c; --muted: #66736c; --emerald: #0f5132; --emerald2: #197149;
  --line: #d8dbd8; --shadow: 4px 5px 0 #17211c; --radius: 4px; --grid: #eadfd8;
  --danger: #b3311f;
  /* Canvas background/grid start out matching the theme's own --bg/--grid,
     but are kept as separate variables (rather than the canvas simply
     reading --bg/--grid directly) so a user's custom canvas colour choice
     never leaks into the rest of the app chrome (topbar, sidebars), and can
     be reverted independently of light/dark theme. */
  --canvasBg: #fffaf7; --canvasGrid: #eadfd8;
}
* { box-sizing: border-box; }
html, body, #app { height: 100%; margin: 0; }
body { font: 13px/1.35 Inter, ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif; color: var(--ink); background: var(--bg); overflow: hidden; }
button, input, textarea, select { font: inherit; color: inherit; }
button { border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); min-height: 30px; cursor: pointer; }
button:hover { border-color: var(--emerald); background: var(--peach); }
button:disabled { opacity: .45; cursor: not-allowed; }
button:focus-visible, input:focus-visible, textarea:focus-visible, select:focus-visible, [tabindex]:focus-visible { outline: 3px solid #f4a77c; outline-offset: 1px; }
select { border: 1px solid var(--line); background: var(--bg); padding: 5px; border-radius: var(--radius); min-height: 30px; }
hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; }
.hidden { display: none !important; }
.skip { position: fixed; left: 8px; top: -50px; z-index: 200; background: #fff; padding: 8px; }
.skip:focus { top: 8px; }

/* ---------------------------------------------------------------- shell */
.app { display: flex; flex-direction: column; height: 100%; }
.topbar { height: 48px; border-bottom: 2px solid var(--ink); display: flex; align-items: center; padding: 0 8px; background: #fff; z-index: 10; }
.brand { display: flex; align-items: center; gap: 7px; margin-right: 20px; text-decoration: none; color: inherit; cursor: pointer; }
.brandMark { display: grid; place-items: center; width: 30px; height: 30px; background: var(--emerald); color: white; border: 2px solid var(--ink); box-shadow: 2px 2px 0 var(--peach2); font-weight: 900; }
.menus, .topActions { display: flex; gap: 3px; }
.menus button { border-color: transparent; }
.topActions { margin-left: auto; }
.iconBtn { width: 30px; padding: 0; }
.command { min-width: 105px; }
.primary { background: var(--emerald) !important; color: #fff !important; border-color: var(--ink) !important; font-weight: 700; }
.primary:hover { background: var(--emerald2) !important; }
.wide { width: 100%; margin-bottom: 4px; }

.workspace { position: relative; display: grid; grid-template-columns: 190px minmax(360px, 1fr) 240px; flex: 1; min-height: 0; }
.sidebar { background: #fff; overflow: auto; border-color: var(--ink); border-style: solid; }
.sidebar.left { border-width: 0 2px 0 0; }
.sidebar.right { border-width: 0 0 0 2px; }
.panelHead { height: 38px; display: flex; align-items: center; justify-content: space-between; padding: 4px 7px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: #fff; z-index: 2; }
.sidebar section { padding: 5px 7px; border-bottom: 1px solid var(--line); }
h3 { font-size: 11px; text-transform: uppercase; letter-spacing: .09em; margin: 6px 0; color: var(--muted); }
.search { margin: 7px; display: flex; border: 1px solid var(--line); align-items: center; padding: 0 5px; background: var(--bg); }
.search input { border: 0; background: transparent; width: 100%; padding: 6px; outline: 0; }

/* ---------------------------------------------------------------- shapes */
.shapeGrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; }
.shapeGrid button { height: 58px; padding: 4px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; }
.glyph { display: grid; place-items: center; width: 30px; height: 23px; border: 2px solid var(--emerald); background: var(--peach); }
.glyph.circle { border-radius: 50%; }
.glyph.diamond { transform: rotate(45deg); width: 22px; height: 22px; }
.glyph.diamond > i { transform: rotate(-45deg); }
.textGlyph { border: 0; background: none; font-weight: 800; font-size: 20px; }
.noteGlyph { border-width: 1px; background: #fff2a9; }
.dbGlyph { border-radius: 50%; font-weight: bold; }

/* ---------------------------------------------------------------- editor */
.editor { display: flex; min-width: 0; flex-direction: column; }
.toolbar { height: 42px; background: #fff; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 3px; padding: 4px; overflow-x: auto; }
.toolbar button { min-width: 31px; }
.toolbar .active, #formatPainterToolBtn.active { background: var(--peach2); border-color: var(--ink); }
.divider { height: 22px; border-left: 1px solid var(--line); margin: 0 3px; flex: none; }
.spacer { flex: 1; }
.toolbar output { min-width: 40px; text-align: center; color: var(--muted); }

.canvasWrap { position: relative; flex: 1; overflow: hidden; background-color: var(--canvasBg); background-image: linear-gradient(var(--canvasGrid) 1px, transparent 1px), linear-gradient(90deg, var(--canvasGrid) 1px, transparent 1px); background-size: 20px 20px; cursor: default; touch-action: none; }
.canvasWrap.noGrid { background-image: none; }
.canvasTransform { position: absolute; inset: 0; transform-origin: 0 0; }
#canvas { width: 100%; height: 100%; overflow: visible; touch-action: none; }
body.hideRulers .rulerH, body.hideRulers .rulerV, body.hideRulers .rulerCorner { display: none; }
.rulerCorner { position: absolute; top: 0; left: 0; width: 22px; height: 22px; z-index: 5; background: var(--surface); border-right: 1px solid var(--line); border-bottom: 1px solid var(--line); }
.rulerH { position: absolute; top: 0; left: 0; right: 0; height: 22px; z-index: 3; background: rgba(255, 255, 255, .82); border-bottom: 1px solid var(--line); pointer-events: none; }
.rulerV { position: absolute; top: 0; left: 0; bottom: 0; width: 22px; z-index: 3; background: rgba(255, 255, 255, .82); border-right: 1px solid var(--line); pointer-events: none; }

.node { cursor: move; }
.node .shape { vector-effect: non-scaling-stroke; }
.node.selected .shape { stroke: #e77736 !important; stroke-width: 3 !important; }
.node.locked { cursor: not-allowed; }
.node .groupBox { pointer-events: none; }
.node .hitLayer { pointer-events: all; }
.node .imgBg { }
.node .cellText { pointer-events: none; fill: var(--ink); font-size: 12px; }
.node .cellHit { pointer-events: all; cursor: text; }
.node .laneTitle { fill: var(--ink); font-size: 12px; pointer-events: none; }
.edge { fill: none; stroke: var(--emerald); stroke-width: 2; cursor: pointer; vector-effect: non-scaling-stroke; }
.edge.selected { stroke: #e77736; stroke-width: 4; }
.pageBounds { fill: #fff; stroke: var(--line); stroke-width: 2; filter: drop-shadow(0 2px 6px rgba(0, 0, 0, .12)); }
.node text, .nodeLabel { pointer-events: none; user-select: none; font-size: 14px; fill: #17211c; }
.edgeLabel { font-size: 12px; fill: var(--emerald); pointer-events: none; }
.handle { fill: #fff; stroke: #e77736; stroke-width: 2; cursor: nwse-resize; vector-effect: non-scaling-stroke; }
.handleEdge { cursor: ns-resize; }
.rotateHandle { fill: #fff; stroke: #e77736; stroke-width: 2; cursor: grab; }
.rotateStem { stroke: #e77736; stroke-width: 1.5; stroke-dasharray: 3 2; }
.multiBox { fill: none; stroke: #e77736; stroke-width: 2; stroke-dasharray: 6 4; }
.marqueeBox { fill: rgba(231, 119, 54, .12); stroke: #e77736; stroke-width: 1.5; stroke-dasharray: 4 3; }
.guideLine { stroke: #e77736; stroke-width: 1; stroke-dasharray: 5 4; }
.connectorPreview { stroke: #e77736; stroke-width: 2; stroke-dasharray: 5 4; fill: none; pointer-events: none; }
.endpointHandle { fill: #fff; stroke: #e77736; stroke-width: 2; cursor: grab; }
.waypointHandle { fill: #e77736; stroke: #fff; stroke-width: 1.5; cursor: grab; }
.portDot { fill: var(--peach2); stroke: var(--emerald); stroke-width: 1.5; cursor: crosshair; }

.emptyState { position: absolute; inset: 0; display: grid; place-content: center; text-align: center; pointer-events: none; }
.emptyState > * { pointer-events: auto; }
.emptyState > span { font-size: 47px; color: var(--emerald); }
.emptyState h2 { margin: 0; font-family: Georgia, serif; font-size: 25px; }
.emptyState p { color: var(--muted); }
.emptyState div { display: flex; gap: 4px; justify-content: center; }
.minimap { position: absolute; right: 8px; bottom: 8px; width: 132px; height: 88px; border: 2px solid var(--ink); background: rgba(255, 255, 255, .9); box-shadow: 2px 2px 0 var(--peach2); z-index: 4; }
.minimap svg { width: 100%; height: 100%; }
.statusbar, .pages { min-height: 25px; background: #fff; border-top: 1px solid var(--line); display: flex; align-items: center; gap: 14px; padding: 2px 7px; color: var(--muted); font-size: 11px; }
.pages { justify-content: center; padding: 2px; }
.page { min-height: 22px; border: 0; border-bottom: 2px solid transparent; }
.page.active { border-bottom-color: var(--emerald); font-weight: bold; }

/* ---------------------------------------------------------------- inspector */
.inspectorEmpty { padding: 14px; color: var(--muted); }
.sidebar label { display: flex; flex-direction: column; gap: 3px; margin-bottom: 7px; color: var(--muted); font-size: 11px; }
.sidebar label.checkboxLabel { flex-direction: row; align-items: center; gap: 6px; }
.sidebar input, .sidebar textarea, .sidebar select { border: 1px solid var(--line); padding: 5px; background: var(--bg); width: 100%; color: var(--ink); }
.fieldRow, .fourFields { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.fourFields { grid-template-columns: repeat(2, 1fr); }
.segmented, .buttonGrid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 3px; }
.segmented4 { grid-template-columns: repeat(4, 1fr); }
.buttonGrid { grid-template-columns: 1fr 1fr; }
.layer { display: grid; grid-template-columns: 18px 1fr 26px 26px 22px 22px 22px; align-items: center; gap: 2px; background: var(--peach); padding: 3px; margin-bottom: 3px; }
.layer.active { outline: 2px solid var(--emerald); }
.layer button { border: 0; background: transparent; padding: 0; min-height: 22px; }
.layerName { cursor: text; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12px; }
.layerDot { cursor: pointer; }
.layers { padding-bottom: 12px; }

/* ---------------------------------------------------------------- popovers / menus */
.popover { position: fixed; top: 43px; left: 85px; z-index: 50; background: #fff; border: 2px solid var(--ink); box-shadow: var(--shadow); width: 230px; max-height: 70vh; overflow: auto; padding: 4px; }
.popover button { display: flex; width: 100%; justify-content: space-between; gap: 10px; border: 0; text-align: left; }
.popover kbd { color: var(--muted); font-size: 10px; }
#contextMenu { top: auto; left: auto; }

/* ---------------------------------------------------------------- dialogs */
dialog { border: 2px solid var(--ink); box-shadow: 8px 8px 0 rgba(15, 81, 50, .3); padding: 0; background: #fff; color: var(--ink); }
dialog::backdrop { background: rgba(16, 31, 24, .35); }
.codeDialog { width: min(850px, 90vw); height: min(650px, 86vh); }
.dialogHead { height: 48px; border-bottom: 1px solid var(--line); padding: 7px; display: flex; align-items: center; justify-content: space-between; }
.dialogHead div { display: flex; flex-direction: column; }
.dialogHead small { color: var(--muted); }
.dialogHead button { font-size: 20px; }
.codeBody { height: calc(100% - 98px); display: flex; flex-direction: column; padding: 7px; gap: 5px; }
.codeEditorWrap { position: relative; flex: 1; min-height: 0; background: #17211c; }
.codeEditorWrap textarea, .codeEditorWrap pre { position: absolute; inset: 0; margin: 0; padding: 12px; font: 13px/1.6 ui-monospace, SFMono-Regular, Consolas, monospace; tab-size: 2; white-space: pre-wrap; word-break: break-word; overflow: auto; }
.codeBody textarea { resize: none; background: transparent; color: transparent; caret-color: #e8f4ec; border: 0; z-index: 2; }
.mermaidHighlight { color: #cddccf; pointer-events: none; background: transparent; z-index: 1; }
.tokKeyword { color: #f4a77c; font-weight: 700; }
.tokArrow { color: #7dd9ae; }
.tokLabel { color: #ffd8c2; }
.tokShape { color: #9fd9c4; }
.tokComment { color: #6d7b74; font-style: italic; }
.codeWarnings { min-height: 20px; padding: 3px 4px; color: #b3311f; font-size: 11px; }
.codeBody > div#codeDiagnostics { min-height: 25px; padding: 4px; background: var(--peach); color: var(--muted); }
.codeBody > div#codeDiagnostics.diagError { background: #fde3df; color: var(--danger); }
.dialogActions { height: 50px; border-top: 1px solid var(--line); display: flex; gap: 5px; align-items: center; padding: 7px; }
.commandDialog { width: min(570px, 90vw); top: -25vh; }
.commandSearch { display: flex; align-items: center; padding: 8px; border-bottom: 1px solid var(--line); }
.commandSearch input { font-size: 18px; border: 0; flex: 1; padding: 7px; outline: 0; }
.commandDialog #commandList { max-height: 330px; overflow: auto; padding: 5px; }
.commandItem { display: flex; width: 100%; justify-content: space-between; border: 0; text-align: left; padding: 7px; }
.commandItem kbd { color: var(--muted); }
.exportGrid { padding: 10px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 7px; width: 560px; max-width: 90vw; }
.exportGrid button { height: 78px; display: flex; flex-direction: column; justify-content: center; }
.exportGrid b { font-size: 16px; color: var(--emerald); }
.exportGrid small { color: var(--muted); }
.exportOnlyRow { padding: 8px 12px 0; }
.csvBody { padding: 12px; display: flex; flex-direction: column; gap: 8px; max-height: 60vh; overflow: auto; }
.csvBody label { display: flex; flex-direction: column; gap: 3px; color: var(--muted); font-size: 11px; }

.treeViewDialog { width: min(560px, 92vw); max-height: 80vh; }
.treeViewList { max-height: min(500px, 60vh); overflow: auto; padding: 6px; }
.treeItem { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; }
.treeItem.active { background: var(--peach2); font-weight: 700; }
.treeLayer { padding: 6px 4px 2px; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .06em; }
.historyItem { display: block; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line); border-radius: 0; }
.historyItem small { color: var(--muted); float: right; }
.historyItem.futureItem { opacity: .5; }
.snapshotRow, .shortcutRow { display: flex; align-items: center; gap: 6px; padding: 6px 4px; border-bottom: 1px solid var(--line); }
.shortcutKey { min-width: 130px; }

/* ---------------------------------------------------------------- misc */
.inlineEditor { position: fixed; z-index: 60; border: 2px solid #e77736; background: #fff; color: var(--ink); padding: 4px 6px; resize: none; font-family: Inter, sans-serif; line-height: 1.25; box-shadow: var(--shadow); }
.inlineEditorSmall { text-align: center; }
.toast { position: fixed; left: 50%; bottom: 38px; transform: translateX(-50%) translateY(80px); opacity: 0; background: var(--ink); color: #fff; padding: 8px 13px; border: 2px solid #fff; transition: .2s; z-index: 100; }
.toast.show { transform: translateX(-50%) translateY(0); opacity: 1; }
.collapsedLeft { grid-template-columns: 0 minmax(360px, 1fr) 240px; }
.collapsedRight { grid-template-columns: 190px minmax(360px, 1fr) 0; }
.collapsedLeft .left, .collapsedRight .right { visibility: hidden; }

/* Collapsing a sidebar hides its own collapse button along with it, so
   these small edge tabs are the only way back -- shown only while the
   matching sidebar is collapsed, positioned outside the grid column that
   just went to 0 width so they stay clickable. */
.edgeReveal {
  position: absolute; top: 50%; transform: translateY(-50%);
  z-index: 6; width: 16px; height: 44px; padding: 0;
  display: none; align-items: center; justify-content: center;
  background: #fff; border: 2px solid var(--ink);
  box-shadow: 2px 2px 0 var(--peach2);
}
.edgeReveal--left { left: 0; border-radius: 0 6px 6px 0; border-left-width: 0; }
.edgeReveal--right { right: 0; border-radius: 6px 0 0 6px; border-right-width: 0; }
.collapsedLeft .edgeReveal--left, .collapsedRight .edgeReveal--right { display: flex; }
.dark .edgeReveal { background: var(--surface); }
.presenting .topbar, .presenting .sidebar, .presenting .toolbar, .presenting .statusbar, .presenting .pages, .presenting .minimap, .presenting .rulerH, .presenting .rulerV, .presenting .rulerCorner { display: none; }
.presenting .workspace { display: block; height: 100%; }
.presenting .editor, .presenting .canvasWrap { height: 100%; }

.dark { --bg: #16231d; --surface: #1c2d25; --peach: #3b3029; --peach2: #674b3c; --ink: #f5f2ed; --muted: #b4c0ba; --emerald: #7dd9ae; --emerald2: #55bb8d; --line: #516059; --grid: #2b3932; --canvasBg: #16231d; --canvasGrid: #2b3932; }
.dark .topbar, .dark .toolbar, .dark .sidebar, .dark .statusbar, .dark .pages, .dark dialog, .dark .popover, .dark .rulerCorner, .dark .rulerH, .dark .rulerV, .dark .inlineEditor { background: var(--surface); }
.dark .node text, .dark .nodeLabel { fill: #f5f2ed; }
.dark .primary { color: #10231b !important; }
.dark .brandMark { color: #10231b; }
.dark .codeBody textarea { background: #0d1712; }
.dark .pageBounds { fill: #1c2d25; }
.dark .codeBody > div#codeDiagnostics { background: var(--peach); color: var(--muted); }

/* ---------------------------------------------------------------- reduced motion & touch targets */
.reduceMotion, .reduceMotion * { transition-duration: .001ms !important; animation-duration: .001ms !important; }
@media (prefers-reduced-motion: reduce) { .toast { transition: none; } }
body.largeTouchTargets button, body.largeTouchTargets .tool, body.largeTouchTargets .iconBtn { min-height: 44px; min-width: 44px; }

/* ---------------------------------------------------------------- print */
.printArea { display: none; }
body.printing #app { display: none; }
body.printing #printArea { display: block; }
@media print {
  body.printing #app, body.printing .toast { display: none !important; }
  #printArea svg { width: 100%; height: auto; }
}

/* ---------------------------------------------------------------- responsive */
@media (max-width: 850px) {
  .workspace { grid-template-columns: 150px minmax(340px, 1fr); }
  .right { display: none; }
  .menus { display: none; }
  .minimap { display: none; }
}
@media (max-width: 600px) {
  .workspace { display: block; }
  .left { display: none; }
  .topActions .command, .topActions #presentBtn { display: none; }
  .toolbar { overflow-x: auto; }
  .brand { margin-right: auto; }
}

/* ---------------------------------------------------------------- forced-colors / high contrast */
@media (forced-colors: active) {
  .handle, .rotateHandle, .endpointHandle, .waypointHandle, .portDot { forced-color-adjust: none; }
}
