:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --border: #d9dce1;
  --text: #22252a;
  --muted: #6b7280;
  --accent: #2f5d8a;
  --button: #ffffff;
  --button-hover: #eef2f7;
  --gutter-bg: #f3f3f3;
  --gutter-fg: #9a9a9a;
  --guide: #d4d4d4;
  --warn: #a15c00;
  --selection: rgb(47 93 138 / 0.28);
  --tok-comment: #8a8f98;
  --tok-key: #7b3fa8;
  --tok-value: #1f6f4a;
  --tok-likely: #b3261e;
  --code-font: Consolas, "Cascadia Mono", Menlo, "DejaVu Sans Mono", monospace;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1b1d21;
    --panel: #22252a;
    --border: #3a3e45;
    --text: #e4e6ea;
    --muted: #9aa1ab;
    --accent: #8fb6de;
    --button: #2b2f35;
    --button-hover: #353a42;
    --gutter-bg: #1f2125;
    --gutter-fg: #6b717b;
    --guide: #40444c;
    --warn: #e0a54a;
    --selection: rgb(143 182 222 / 0.3);
    --tok-comment: #7c838d;
    --tok-key: #c99cf0;
    --tok-value: #8fd1a9;
    --tok-likely: #ff8a80;
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 "Segoe UI", system-ui, -apple-system, Arial, sans-serif;
}

/* ---------------------------------------------------------------- toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 14px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; color: var(--accent); margin-right: 4px; }
.group { display: flex; align-items: center; gap: 4px; }
button {
  font: inherit;
  color: var(--text);
  background: var(--button);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 4px 10px;
  cursor: pointer;
}
button:hover { background: var(--button-hover); }
button:focus-visible, .splitter:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.zoom-label { min-width: 3.2em; text-align: center; color: var(--muted); font-variant-numeric: tabular-nums; }
.check { display: flex; align-items: center; gap: 5px; color: var(--muted); cursor: pointer; }

/* ---------------------------------------------------------------- panes */
.panes { flex: 1; display: flex; min-height: 0; }
.splitter { width: 6px; cursor: col-resize; background: var(--border); flex: none; }

.editor {
  flex: 0 0 34%;
  min-width: 220px;
  display: flex;
  background: var(--panel);
  overflow: hidden;
  --line: 20px;
  --pad: 8px;
}
.gutter {
  flex: none;
  overflow: hidden;
  background: var(--gutter-bg);
  border-right: 1px solid var(--border);
}
.gutter-inner {
  margin: 0;
  padding: var(--pad) 10px var(--pad) 12px;
  min-width: 4ch;
  text-align: right;
  font: 14px/var(--line) var(--code-font);
  color: var(--gutter-fg);
  user-select: none;
}
.code { position: relative; flex: 1; min-width: 0; }
.guides { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.guides i { position: absolute; width: 1px; background: var(--guide); }
.code textarea {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  border: 0;
  outline: none;
  resize: none;
  padding: var(--pad) 10px;
  background: transparent;
  color: var(--text);
  font: 14px/var(--line) var(--code-font);
  white-space: pre;
  overflow: auto;
  tab-size: 4;
}

.preview {
  flex: 1;
  min-width: 0;
  overflow: auto;
  background: #ffffff; /* the diagram is always drawn on white paper */
}
.preview.transparent {
  background: repeating-conic-gradient(#eeeeee 0 25%, #ffffff 0 50%) 0 0 / 16px 16px;
}
.stage { display: inline-block; padding: 12px; }
.stage svg { display: block; }
.stage [data-line] { cursor: pointer; }
.stage .hl:not(.hit) { filter: drop-shadow(0 0 2px #f9a825) drop-shadow(0 0 1px #f9a825); }

/* ---------------------------------------------------------------- style panel */
.style-panel {
  flex: 0 0 270px;
  overflow-y: auto;
  background: var(--panel);
  border-left: 1px solid var(--border);
  padding: 10px 14px 16px;
}
.panel-head { display: flex; justify-content: space-between; align-items: center; }
.panel-note { margin: 6px 0 4px; color: var(--muted); font-size: 12px; }
.panel-note code { font-size: 11px; }
button.icon { border: 0; background: none; padding: 2px 6px; font-size: 14px; }
.style-panel fieldset { border: 0; border-top: 1px solid var(--border); margin: 10px 0 0; padding: 8px 0 0; }
.style-panel legend { font-weight: 600; padding: 0 6px 0 0; }
.style-panel .row {
  display: grid;
  grid-template-columns: 1fr auto auto;
  align-items: center;
  gap: 6px;
  min-height: 30px;
}
.style-panel .row > span { color: var(--text); }
.style-panel .row.overridden > span::after { content: " •"; color: var(--accent); }
.style-panel select { font: inherit; color: var(--text); background: var(--button); border: 1px solid var(--border); border-radius: 5px; padding: 2px 4px; max-width: 130px; }
.style-panel input[type="color"] { width: 44px; height: 24px; padding: 0 2px; border: 1px solid var(--border); border-radius: 4px; background: var(--button); cursor: pointer; }
.style-panel input[type="range"] { width: 100px; }
.style-panel output { min-width: 3.2em; text-align: right; color: var(--muted); font-variant-numeric: tabular-nums; }
.style-panel .reset { padding: 0 6px; visibility: hidden; }
.style-panel .row.overridden .reset { visibility: visible; }
.panel-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 14px; }

/* ---------------------------------------------------------------- branch color popover */
.popover {
  position: fixed;
  z-index: 10;
  width: 232px;
  padding: 10px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 6px 24px rgb(0 0 0 / 0.18);
}
.pop-title { font-weight: 600; margin-bottom: 8px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.swatches { display: grid; grid-template-columns: repeat(6, 1fr); gap: 6px; }
.swatches button { aspect-ratio: 1; padding: 0; border: 2px solid transparent; border-radius: 50%; }
.swatches button.current { border-color: var(--text); }
.pop-row { display: flex; flex-wrap: wrap; align-items: center; gap: 6px; margin-top: 10px; }
.pop-row .custom { display: flex; align-items: center; gap: 4px; color: var(--muted); }
.pop-row input[type="color"] { width: 32px; height: 24px; padding: 0 2px; border: 1px solid var(--border); border-radius: 4px; }
.popover { max-height: calc(100vh - 16px); overflow-y: auto; }

/* Gantt task editor in the popover: a label column and the inputs. */
.popover:has(.task-form:not([hidden])) { width: 384px; }
.popover:has(.task-form:not([hidden])) .swatches { grid-template-columns: repeat(12, 1fr); gap: 5px; }
.task-form { display: grid; gap: 6px; margin-bottom: 12px; padding-bottom: 10px; border-bottom: 1px solid var(--border); }
.task-form[hidden] { display: none; }
.tf-title { color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.04em; }
.tf-row { display: grid; grid-template-columns: 64px 1fr; align-items: center; gap: 6px; }
.tf-row[hidden] { display: none; }
.tf-label { color: var(--muted); }
.tf-field { display: flex; flex-wrap: wrap; align-items: center; gap: 4px; min-width: 0; }
.tf-field [hidden] { display: none; }
.task-form input, .task-form select {
  font: inherit; font-size: 12px; color: var(--text); background: var(--panel);
  border: 1px solid var(--border); border-radius: 4px; padding: 2px 4px; min-width: 0;
}
.task-form .tf-name, .task-form input[list] { flex: 1 1 120px; }
.task-form input[type="date"] { width: 118px; }
.task-form input[type="time"] { width: 80px; }
.task-form .tf-short { width: 72px; }
.task-form button { font-size: 12px; padding: 2px 6px; }
.task-form .invalid { border-color: #d32f2f; box-shadow: 0 0 0 1px #d32f2f; }

/* ---------------------------------------------------------------- status */
.status {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 12px;
  background: var(--panel);
  border-top: 1px solid var(--border);
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
}
.status #status { overflow: hidden; text-overflow: ellipsis; }
.status .warn { color: var(--warn); }
.filename { flex: none; }

.drop-hint {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgb(47 93 138 / 0.18);
  border: 3px dashed var(--accent);
  font-size: 20px;
  font-weight: 600;
  color: var(--accent);
  pointer-events: none;
}

/* ---------------------------------------------------------------- narrow screens */
@media (max-width: 760px) {
  .style-panel { position: fixed; inset: auto 0 0 0; max-height: 60vh; flex: none; border-left: 0; border-top: 1px solid var(--border); z-index: 5; }
  .panes { flex-direction: column; }
  .splitter { display: none; }
  .editor { flex: 0 0 40%; min-width: 0; }
  .preview { flex: 1; }
}

/* ---------------------------------------------------------------- help dialog */
.help {
  width: min(620px, calc(100vw - 32px));
  max-height: calc(100vh - 48px);
  padding: 14px 18px 16px;
  color: var(--text);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: 0 10px 40px rgb(0 0 0 / 0.25);
}
.help::backdrop { background: rgb(0 0 0 / 0.3); }
.help h3 { margin: 14px 0 4px; font-size: 13px; color: var(--accent); }
.help p { margin: 4px 0; }
.help pre {
  margin: 0;
  padding: 6px 10px;
  overflow-x: auto;
  background: var(--gutter-bg);
  border-radius: 5px;
  font: 12.5px/1.55 var(--code-font);
}
.help pre span { color: var(--muted); font-family: "Segoe UI", system-ui, sans-serif; font-size: 12px; }
.help kbd {
  padding: 0 4px;
  border: 1px solid var(--border);
  border-radius: 3px;
  background: var(--button);
  font: 11.5px var(--code-font);
}

/* ---------------------------------------------------------------- new-diagram templates */
.templates { display: grid; gap: 8px; margin-top: 8px; }
.templates button {
  display: grid;
  gap: 2px;
  text-align: left;
  padding: 8px 12px;
}
.templates button strong { color: var(--accent); }
.templates button span { color: var(--muted); font-size: 12px; }
.share-link {
  width: 100%;
  margin-top: 8px;
  padding: 6px 8px;
  font: 12px var(--code-font);
  color: var(--text);
  background: var(--gutter-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
}
.status-row .pop-label { color: var(--muted); margin-right: 2px; }
.pop-row button.current { border-color: var(--accent); background: var(--button-hover); }

/* ---------------------------------------------------------------- editor syntax colors */
/* The textarea's text is invisible; .highlight draws the same lines in color behind it. */
.code textarea { color: transparent; caret-color: var(--text); z-index: 1; }
.code textarea::selection { background: var(--selection); color: transparent; }
.highlight { position: absolute; inset: 0; overflow: hidden; pointer-events: none; }
.hl-line {
  position: absolute;
  white-space: pre;
  font: 14px/var(--line) var(--code-font);
  tab-size: 4;
  color: var(--text);
}
.hl-line.warn { text-decoration: underline wavy var(--warn); text-underline-offset: 3px; }
.gutter-inner .warn { color: var(--warn); }
.tok-comment { color: var(--tok-comment); font-style: italic; }
.tok-key, .tok-legend { color: var(--tok-key); }
.tok-legend { text-decoration: underline; text-underline-offset: 3px; }
.tok-value { color: var(--tok-value); }
.tok-legend-title { color: var(--text); }
.tok-head { color: var(--accent); text-decoration: underline; text-underline-offset: 3px; }
.tok-tag { text-decoration: underline; text-decoration-thickness: 2px; text-underline-offset: 3px; }
.tok-likely-mark { color: var(--tok-likely); }
.tok-likely { color: var(--tok-likely); text-decoration: underline; text-underline-offset: 3px; }
.tok-ruled { color: var(--muted); text-decoration: line-through; }
@media (prefers-color-scheme: dark) {
  .tok-tag { filter: brightness(1.7) saturate(0.85); }
}

/* ---------------------------------------------------------------- tool switcher */
.brand { display: flex; align-items: center; gap: 6px; }
.brand span { color: var(--muted); font-weight: 400; }
.brand a { color: var(--muted); text-decoration: none; font-weight: 600; padding: 2px 4px; border-radius: 4px; }
.brand a:hover { color: var(--text); background: var(--button-hover); }
.brand a[aria-current="page"] { color: var(--accent); font-weight: 700; }

/* ---------------------------------------------------------------- Gantt timing words in the editor */
.tok-date { color: var(--tok-value); }
.tok-length { color: var(--tok-key); }
.tok-pct { color: var(--tok-key); }
.tok-dep { color: var(--accent); }

/* ---------------------------------------------------------------- "Save as" name dialog (browsers without a Save window) */
.save-dialog { width: min(440px, calc(100vw - 32px)); }
.save-row { display: grid; gap: 6px; margin-top: 10px; color: var(--muted); }
.save-row input {
  font: 14px var(--code-font);
  color: var(--text);
  background: var(--gutter-bg);
  border: 1px solid var(--border);
  border-radius: 5px;
  padding: 6px 8px;
}
.save-note { margin: 8px 0 0; color: var(--muted); font-size: 12px; }
.save-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; }
.save-actions button[type="submit"] { background: var(--accent); color: #ffffff; border-color: var(--accent); }

/* ---------------------------------------------------------------- frozen name column (Gantt) */
/* Both SVGs share one grid cell; the pinned one sticks to the preview's left edge. */
.stage.has-pin { display: inline-grid; }
.stage.has-pin > svg, .stage.has-pin > .pin, .stage.has-pin > .pin-top, .stage.has-pin > .pin-corner { grid-area: 1 / 1; }
.stage .pin {
  position: sticky;
  left: 0;
  z-index: 1;
  align-self: start;
  justify-self: start;
  transition: box-shadow 0.15s;
}
.stage .pin.scrolled { box-shadow: 1px 0 0 rgb(0 0 0 / 0.18), 6px 0 10px -4px rgb(0 0 0 / 0.25); }
/* Frozen date header (sticks to the top) and the blank corner where it meets the names. */
.stage .pin-top, .stage .pin-corner {
  position: sticky;
  top: 0;
  align-self: start;
  justify-self: start;
}
.stage .pin-top { z-index: 2; transition: box-shadow 0.15s; }
.stage .pin-corner { left: 0; z-index: 3; }
.stage .pin-top.scrolled { box-shadow: 0 1px 0 rgb(0 0 0 / 0.18), 0 6px 10px -4px rgb(0 0 0 / 0.25); }
