/* ════════════════════════════════════════════════════════════════
   FLOW · Mermaid Studio — structural component layer.
   Brand and colour styling is supplied by clean-theme.css.
   ════════════════════════════════════════════════════════════════ */

:root {
  --font-mono: 'JetBrains Mono', ui-monospace, 'SF Mono', Menlo, monospace;
  --font-display: 'Fraunces', Georgia, serif;

  --r-sm: 6px;
  --r-md: 10px;
  --r-lg: 16px;
  --speed: 0.18s;
  --ease: cubic-bezier(0.22, 1, 0.36, 1);
}

/* Legacy token fallback (overridden by clean-theme.css). */
[data-theme="blueprint"] {
  --bg: #0b1013;
  --bg-2: #0f1519;
  --surface: #131b20;
  --surface-2: #18222a;
  --surface-hi: #1e2a33;
  --line: #25343d;
  --line-soft: #1a262e;
  --grid: rgba(120, 178, 196, 0.045);
  --grid-major: rgba(120, 178, 196, 0.08);

  --ink: #e4ecef;
  --ink-soft: #9fb0b8;
  --ink-faint: #61737c;

  --brass: #e8a13a;
  --brass-soft: #f3c179;
  --cyan: #5fc4d6;
  --cyan-deep: #2b8294;
  --red: #ef6f6f;
  --green: #66c98c;

  --accent: var(--brass);
  --accent-ink: #1a1206;
  --shadow: 0 18px 50px -22px rgba(0, 0, 0, 0.85);
  --stage-bg: #0a0f12;
  color-scheme: dark;
}

/* ── DRAFTING (light) ── paper ── */
[data-theme="drafting"] {
  --bg: #ece6d8;
  --bg-2: #e4ddcc;
  --surface: #f6f1e6;
  --surface-2: #efe8d8;
  --surface-hi: #e6dcc6;
  --line: #cfc4ac;
  --line-soft: #ddd3bd;
  --grid: rgba(60, 90, 110, 0.06);
  --grid-major: rgba(60, 90, 110, 0.1);

  --ink: #2b2a24;
  --ink-soft: #5f5b4d;
  --ink-faint: #8c8675;

  --brass: #b96a13;
  --brass-soft: #c98a3e;
  --cyan: #1f7d92;
  --cyan-deep: #145567;
  --red: #c0392b;
  --green: #2f7d4f;

  --accent: var(--brass);
  --accent-ink: #fff7ea;
  --shadow: 0 18px 44px -24px rgba(70, 55, 25, 0.5);
  --stage-bg: #efe9dc;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  font-family: var(--font-mono);
  background: var(--bg);
  color: var(--ink);
  font-size: 13px;
  line-height: 1.5;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  -webkit-font-smoothing: antialiased;
}

/* Blueprint grid behind everything */
.grid-backdrop {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-color: var(--bg);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px),
    linear-gradient(var(--grid-major) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid-major) 1px, transparent 1px);
  background-size: 24px 24px, 24px 24px, 120px 120px, 120px 120px;
  background-position: -1px -1px;
}
.grid-backdrop::after {
  content: '';
  position: absolute;
  inset: 0;
  background: radial-gradient(120% 90% at 80% -10%, transparent 40%, var(--bg) 100%);
}

button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
  color: inherit;
}
button { cursor: pointer; border: none; background: none; }

/* ════════════════ TOP BAR ════════════════ */
.topbar {
  position: relative;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 16px;
  height: 58px;
  background: linear-gradient(180deg, var(--surface) 0%, var(--bg-2) 100%);
  border-bottom: 1px solid var(--line);
  box-shadow: 0 1px 0 rgba(255,255,255,0.02), var(--shadow);
}

.brand { display: flex; align-items: center; gap: 11px; flex-shrink: 0; }
.brand-mark {
  width: 30px; height: 30px;
  stroke: var(--accent);
  stroke-width: 2.2;
  fill: none;
  stroke-linecap: round;
  stroke-linejoin: round;
  filter: drop-shadow(0 0 10px color-mix(in srgb, var(--accent) 40%, transparent));
}
.brand-text h1 {
  font-family: var(--font-display);
  font-weight: 900;
  font-size: 22px;
  line-height: 0.9;
  margin: 0;
  letter-spacing: -0.5px;
}
.brand-sub {
  font-size: 9.5px;
  letter-spacing: 1.5px;
  text-transform: uppercase;
  color: var(--ink-faint);
}

.doc-title-wrap {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
  justify-content: center;
}
.doc-title {
  background: transparent;
  border: 1px solid transparent;
  border-radius: var(--r-sm);
  padding: 6px 12px;
  text-align: center;
  max-width: 360px;
  width: 100%;
  font-family: var(--font-display);
  font-style: italic;
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.2px;
  transition: border-color var(--speed), background var(--speed);
}
.doc-title:hover { border-color: var(--line); }
.doc-title:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--bg);
}
.save-state {
  font-size: 9px;
  text-transform: uppercase;
  letter-spacing: 1px;
  color: var(--ink-faint);
  border: 1px solid var(--line-soft);
  padding: 2px 7px;
  border-radius: 20px;
  white-space: nowrap;
}
.save-state.dirty { color: var(--brass); border-color: color-mix(in srgb, var(--brass) 45%, transparent); }
.save-state.saved { color: var(--green); border-color: color-mix(in srgb, var(--green) 40%, transparent); }

.topbar-actions { display: flex; align-items: center; gap: 8px; flex-shrink: 0; }

.seg { display: flex; align-items: center; gap: 6px; }
.seg-label { font-size: 9px; text-transform: uppercase; letter-spacing: 1px; color: var(--ink-faint); }

.select {
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  padding: 6px 8px;
  border-radius: var(--r-sm);
  cursor: pointer;
  transition: border-color var(--speed);
}
.select:hover { border-color: var(--accent); }
.select:focus { outline: none; border-color: var(--accent); }

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 13px;
  border-radius: var(--r-sm);
  font-weight: 500;
  font-size: 12px;
  border: 1px solid transparent;
  transition: all var(--speed) var(--ease);
  white-space: nowrap;
  text-decoration: none;
}
.btn-ghost {
  background: var(--surface-2);
  border-color: var(--line);
  color: var(--ink-soft);
}
.btn-ghost:hover { border-color: var(--accent); color: var(--ink); }
.btn-ghost .dot {
  width: 7px; height: 7px; border-radius: 50%;
  background: var(--ink-faint);
  transition: all var(--speed);
}
.btn-ghost.is-on { color: var(--ink); border-color: color-mix(in srgb, var(--green) 50%, var(--line)); }
.btn-ghost.is-on .dot {
  background: var(--green);
  box-shadow: 0 0 9px var(--green);
  animation: pulse 2.4s infinite;
}
@keyframes pulse { 50% { opacity: 0.45; } }

.btn-accent {
  background: linear-gradient(180deg, var(--brass-soft), var(--brass));
  color: var(--accent-ink);
  font-weight: 700;
  box-shadow: 0 4px 14px -6px var(--brass);
}
.btn-accent:hover { filter: brightness(1.07); transform: translateY(-1px); }
.btn-accent:active { transform: translateY(0); }

.btn-icon {
  width: 34px; height: 34px;
  padding: 0;
  display: grid; place-items: center;
  background: var(--surface-2);
  border: 1px solid var(--line);
  border-radius: var(--r-sm);
  font-size: 16px;
  color: var(--ink-soft);
}
.btn-icon:hover { border-color: var(--accent); color: var(--ink); }

.divider { width: 1px; height: 24px; background: var(--line); margin: 0 2px; }

/* ── dropdown menu ── */
.menu { position: relative; }
.menu-pop {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  width: 250px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--r-md);
  box-shadow: var(--shadow);
  padding: 6px;
  opacity: 0;
  transform: translateY(-6px) scale(0.98);
  transform-origin: top right;
  pointer-events: none;
  transition: opacity var(--speed), transform var(--speed) var(--ease);
  z-index: 40;
}
.menu.open .menu-pop { opacity: 1; transform: none; pointer-events: auto; }
.menu-item {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 1px;
  width: 100%;
  text-align: left;
  padding: 8px 10px;
  border-radius: var(--r-sm);
  color: var(--ink);
  transition: background var(--speed);
}
.menu-item:hover { background: var(--surface-hi); }
.menu-item b { font-weight: 700; font-size: 12px; }
.menu-item span { font-size: 10px; color: var(--ink-faint); }
.menu-sep { height: 1px; background: var(--line-soft); margin: 5px 8px; }
.menu-row {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 10px;
}
.menu-row > label { font-size: 10px; text-transform: uppercase; letter-spacing: 1px; color: var(--ink-faint); }
.seg-btns { display: flex; gap: 3px; }
.seg-btns button {
  padding: 4px 8px;
  font-size: 10px;
  border-radius: 5px;
  background: var(--surface-2);
  border: 1px solid var(--line-soft);
  color: var(--ink-soft);
  transition: all var(--speed);
}
.seg-btns button:hover { color: var(--ink); }
.seg-btns button.active { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 700; }

/* ════════════════ WORKSPACE ════════════════ */
.workspace {
  position: relative;
  z-index: 10;
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ── LEFT RAIL ── */
.rail {
  width: 232px;
  flex-shrink: 0;
  background: color-mix(in srgb, var(--surface) 70%, transparent);
  backdrop-filter: blur(6px);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.rail-tabs { display: flex; border-bottom: 1px solid var(--line); }
.rail-tab {
  flex: 1;
  padding: 11px 8px;
  font-size: 10.5px;
  text-transform: uppercase;
  letter-spacing: 1.5px;
  color: var(--ink-faint);
  border-bottom: 2px solid transparent;
  transition: all var(--speed);
}
.rail-tab:hover { color: var(--ink-soft); }
.rail-tab.active { color: var(--accent); border-bottom-color: var(--accent); }

.rail-panel { flex: 1; min-height: 0; display: flex; flex-direction: column; padding: 10px; }
.rail-panel[hidden] { display: none; }
.rail-new {
  width: 100%;
  padding: 9px;
  margin-bottom: 10px;
  border: 1px dashed var(--line);
  border-radius: var(--r-sm);
  color: var(--ink-soft);
  font-size: 11px;
  letter-spacing: 0.5px;
  transition: all var(--speed);
}
.rail-new:hover { border-color: var(--accent); color: var(--accent); border-style: solid; }

.rail-list { flex: 1; overflow-y: auto; display: flex; flex-direction: column; gap: 5px; margin: -3px; padding: 3px; }

.rail-card {
  position: relative;
  padding: 9px 10px;
  border-radius: var(--r-sm);
  border: 1px solid var(--line-soft);
  background: var(--surface-2);
  cursor: pointer;
  transition: all var(--speed) var(--ease);
  overflow: hidden;
}
.rail-card:hover { border-color: var(--accent); transform: translateX(2px); }
.rail-card.active { border-color: var(--accent); background: var(--surface-hi); }
.rail-card.active::before {
  content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 3px; background: var(--accent);
}
.rail-card .rc-name { font-size: 12px; font-weight: 500; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.rail-card .rc-meta { font-size: 9.5px; color: var(--ink-faint); margin-top: 2px; display: flex; gap: 7px; }
.rail-card .rc-type { color: var(--cyan); }
.rail-card .rc-actions {
  position: absolute; right: 6px; top: 50%; transform: translateY(-50%);
  display: flex; gap: 3px; opacity: 0; transition: opacity var(--speed);
  background: var(--surface-hi); padding: 3px; border-radius: 5px;
}
.rail-card:hover .rc-actions { opacity: 1; }
.rc-actions button {
  width: 22px; height: 22px; display: grid; place-items: center;
  border-radius: 4px; color: var(--ink-soft); font-size: 12px;
}
.rc-actions button:hover { background: var(--surface); color: var(--accent); }
.rc-actions button.danger:hover { color: var(--red); }

.rail-empty { font-size: 11px; color: var(--ink-faint); line-height: 1.7; padding: 18px 6px; text-align: center; }
.rail-empty b { color: var(--accent); }

.tmpl-card { display: flex; flex-direction: column; gap: 3px; }
.tmpl-card .tc-icon { font-size: 15px; }

/* ── PANES ── */
.pane {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  position: relative;
}
.pane-editor { width: 42%; }
.pane-preview { flex: 1; }

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 12px;
  height: 36px;
  flex-shrink: 0;
  background: var(--bg-2);
  border-bottom: 1px solid var(--line);
}
.pane-label { font-size: 9.5px; text-transform: uppercase; letter-spacing: 2px; color: var(--ink-faint); }
.pane-head-actions { display: flex; align-items: center; gap: 5px; }
.mini-btn {
  padding: 4px 9px;
  border-radius: 5px;
  font-size: 10.5px;
  color: var(--ink-soft);
  border: 1px solid transparent;
  transition: all var(--speed);
}
.mini-btn:hover { background: var(--surface-2); color: var(--ink); border-color: var(--line); }
.zoom-readout { min-width: 46px; text-align: center; font-variant-numeric: tabular-nums; }

/* ── EDITOR ── */
.editor-host { flex: 1; min-height: 0; position: relative; }
.CodeMirror {
  height: 100% !important;
  background: transparent !important;
  font-family: var(--font-mono) !important;
  font-size: 13.5px;
  line-height: 1.65;
  color: var(--ink);
}
.CodeMirror-gutters { background: var(--bg-2) !important; border-right: 1px solid var(--line-soft) !important; }
.CodeMirror-linenumber { color: var(--ink-faint); font-size: 11px; }
.CodeMirror-cursor { border-left: 2px solid var(--accent) !important; }
.CodeMirror-activeline-background { background: color-mix(in srgb, var(--accent) 6%, transparent) !important; }
.CodeMirror-selected { background: color-mix(in srgb, var(--cyan) 22%, transparent) !important; }
.CodeMirror-matchingbracket { color: var(--accent) !important; border-bottom: 1px solid var(--accent); font-weight: 700; }
.CodeMirror-scrollbar-filler { background: transparent !important; }

/* mermaid syntax highlighting tokens */
.cm-mm-keyword { color: var(--brass); font-weight: 700; }
.cm-mm-type    { color: var(--cyan); font-weight: 700; }
.cm-mm-arrow   { color: var(--green); font-weight: 700; }
.cm-mm-node    { color: var(--ink); }
.cm-mm-string  { color: var(--brass-soft); }
.cm-mm-label   { color: var(--cyan); }
.cm-mm-comment { color: var(--ink-faint); font-style: italic; }
.cm-mm-number  { color: var(--green); }

/* ── STATUS BAR ── */
.statusbar {
  flex-shrink: 0;
  height: 26px;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
  background: var(--bg-2);
  border-top: 1px solid var(--line);
  font-size: 10px;
  color: var(--ink-faint);
  letter-spacing: 0.4px;
}
.stat-right { margin-left: auto; }
.stat { white-space: nowrap; }
#stat-type { color: var(--cyan); text-transform: uppercase; letter-spacing: 1px; }

/* ── SPLITTER ── */
.splitter {
  width: 7px;
  flex-shrink: 0;
  cursor: col-resize;
  background: var(--bg-2);
  border-left: 1px solid var(--line);
  border-right: 1px solid var(--line);
  position: relative;
  transition: background var(--speed);
}
.splitter::after {
  content: '';
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  width: 3px; height: 34px;
  border-radius: 3px;
  background: var(--line);
  transition: background var(--speed);
}
.splitter:hover::after, .splitter:focus::after { background: var(--accent); }
.splitter:hover { background: var(--surface); }

/* ── PREVIEW ── */
.pane-preview { background: var(--stage-bg); }
.preview-stage {
  flex: 1;
  min-height: 0;
  position: relative;
  overflow: hidden;
  cursor: grab;
  background:
    radial-gradient(circle at center, transparent 0, var(--stage-bg) 90%),
    repeating-linear-gradient(0deg, transparent 0 23px, var(--grid) 23px 24px),
    repeating-linear-gradient(90deg, transparent 0 23px, var(--grid) 23px 24px);
}
.preview-stage.panning { cursor: grabbing; }
.preview-canvas {
  position: absolute;
  top: 50%; left: 50%;
  transform-origin: center center;
  transition: transform 0.06s linear;
  will-change: transform;
}
.diagram-holder { display: inline-block; }
.diagram-holder svg { display: block; max-width: none !important; height: auto; }

.preview-hint {
  position: absolute;
  bottom: 12px; left: 50%;
  transform: translateX(-50%);
  font-size: 9.5px;
  letter-spacing: 1px;
  text-transform: uppercase;
  color: var(--ink-faint);
  background: color-mix(in srgb, var(--bg) 70%, transparent);
  padding: 5px 12px;
  border-radius: 20px;
  border: 1px solid var(--line-soft);
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.4s;
}
.preview-stage:hover .preview-hint { opacity: 0.8; }

.diagram-holder.rendering { opacity: 0.4; transition: opacity 0.2s; }

/* rendering shimmer when empty */
.diagram-empty {
  display: grid; place-items: center; gap: 14px;
  padding: 50px;
  color: var(--ink-faint);
  text-align: center;
}
.diagram-empty svg { width: 54px; height: 54px; stroke: var(--line); stroke-width: 1.6; fill: none; }
.diagram-empty p { margin: 0; font-size: 12px; max-width: 280px; line-height: 1.7; }

/* ── ERROR TRAY ── */
.error-tray {
  flex-shrink: 0;
  max-height: 38%;
  overflow: auto;
  background: color-mix(in srgb, var(--red) 9%, var(--bg-2));
  border-top: 2px solid var(--red);
}
.error-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 7px 12px;
  position: sticky; top: 0;
  background: color-mix(in srgb, var(--red) 12%, var(--bg-2));
}
.error-badge { font-size: 9.5px; text-transform: uppercase; letter-spacing: 1.5px; color: var(--red); font-weight: 700; }
#error-close { color: var(--red); font-size: 18px; line-height: 1; width: 22px; height: 22px; border-radius: 4px; }
#error-close:hover { background: color-mix(in srgb, var(--red) 20%, transparent); }
#error-msg { margin: 0; padding: 4px 14px 14px; font-size: 11.5px; color: var(--ink); white-space: pre-wrap; word-break: break-word; line-height: 1.6; }

/* ── TOAST ── */
.toast {
  position: fixed;
  bottom: 26px; left: 50%;
  transform: translate(-50%, 20px);
  background: var(--surface);
  border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  color: var(--ink);
  padding: 11px 18px;
  border-radius: var(--r-sm);
  font-size: 12px;
  box-shadow: var(--shadow);
  z-index: 100;
  opacity: 0;
  pointer-events: none;
  transition: opacity var(--speed), transform var(--speed) var(--ease);
  max-width: 80vw;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.err { border-left-color: var(--red); }
.toast.ok { border-left-color: var(--green); }

/* ── PRESENT / FULLSCREEN ── */
.preview-stage:fullscreen { background: var(--stage-bg); }
.preview-stage:fullscreen .preview-hint { display: none; }

/* scrollbars */
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--line); border-radius: 6px; border: 2px solid var(--bg); }
::-webkit-scrollbar-thumb:hover { background: var(--accent); }

/* responsive */
@media (max-width: 900px) {
  .rail { position: absolute; z-index: 30; height: calc(100% - 58px); box-shadow: var(--shadow); transform: translateX(-100%); transition: transform var(--speed); }
  .rail.open { transform: none; }
  .doc-title-wrap { display: none; }
}
