/*
 * The palette is shared with temp/ and todo/ so the three feel like siblings.
 * The layout is not: this page is a two-pane workbench, not a single column.
 */
:root {
  /* Lets native form controls and scrollbars follow the theme instead of
     staying light-mode bright against a dark page. */
  color-scheme: light dark;

  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #e3e6ea;
  --text: #1c1f23;
  --muted: #7a828c;
  --accent: #2f6fed;
  --danger: #d1443c;
  --ok: #2c9c62;
  --radius: 10px;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 8px 24px rgba(16, 24, 40, 0.06);

  /* The canvas is deliberately a shade off the page, so the diagram reads as
     something sitting on a surface rather than floating on the background. */
  --canvas: #fbfbfc;
  --grid: rgba(28, 31, 35, 0.07);

  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
  --code-size: 13px;
  --code-line: 20px;
  --code-pad: 14px;

  /* Owned by the splitter; persisted in localStorage. */
  --editor-width: 42%;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #14161a;
    --panel: #1c1f24;
    --border: #2b3038;
    --text: #e7eaee;
    --muted: #949ca7;
    --accent: #6f9dff;
    --danger: #f0736a;
    --ok: #5ac48b;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 8px 24px rgba(0, 0, 0, 0.3);

    --canvas: #16191d;
    --grid: rgba(231, 234, 238, 0.07);
  }
}

* {
  box-sizing: border-box;
}

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

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  /* The preview pans by drag; without this a drag selects the whole page. */
  overscroll-behavior: none;
}

.hidden {
  display: none !important;
}

.muted,
.dot {
  color: var(--muted);
  font-size: 12px;
}

/* Layout — full bleed. A diagram wants every pixel it can get. */
.app {
  display: flex;
  flex-direction: column;
  height: 100%;
  padding: 12px 16px 16px;
}

/* Topbar */
.topbar {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px 12px;
  padding-bottom: 12px;
}

.topbar__title {
  font-size: 16px;
  font-weight: 600;
  margin: 0;
}

.topbar__meta {
  align-items: center;
  display: flex;
  flex: 1;
  flex-wrap: wrap;
  gap: 6px;
}

.topbar__actions {
  display: flex;
  gap: 6px;
}

/* Status */
.status {
  font-size: 12px;
  color: var(--muted);
}

.status--live {
  color: var(--ok);
}

/* Pulsing dot: shows at a glance that the realtime stream is connected. */
.status--live::before {
  animation: pulse 2s ease-in-out infinite;
  background: var(--ok);
  border-radius: 50%;
  content: '';
  display: inline-block;
  height: 7px;
  margin-right: 5px;
  vertical-align: baseline;
  width: 7px;
}

.status--error {
  color: var(--danger);
}

@keyframes pulse {
  50% {
    opacity: 0.35;
  }
}

@media (prefers-reduced-motion: reduce) {
  .status--live::before {
    animation: none;
  }
}

/* Buttons */
.btn {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 13px;
  padding: 6px 12px;
  white-space: nowrap;
}

.btn:hover {
  border-color: var(--muted);
}

.btn:disabled {
  cursor: default;
  opacity: 0.5;
}

.btn:disabled:hover {
  border-color: var(--border);
}

.btn--select {
  padding-right: 8px;
}

/* Panes */
.panes {
  display: grid;
  flex: 1;
  grid-template-columns: var(--editor-width) 10px 1fr;
  min-height: 0;
}

.pane {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
}

/* Splitter */
.splitter {
  background: none;
  border: none;
  cursor: col-resize;
  padding: 0;
  position: relative;
}

/* A 10px hit area, but only a hairline is drawn. */
.splitter::after {
  background: var(--border);
  border-radius: 1px;
  content: '';
  inset: 0 4px;
  position: absolute;
  transition: background 0.12s ease;
}

.splitter:hover::after,
.splitter:focus-visible::after,
.splitter.is-dragging::after {
  background: var(--accent);
}

.splitter:focus-visible {
  outline: none;
}

/* While dragging, the cursor must not flicker as it crosses the panes. */
body.is-resizing {
  cursor: col-resize;
  user-select: none;
}

/* Editor */
.editor {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  display: flex;
  flex: 1;
  min-height: 0;
  overflow: hidden;
}

.editor:focus-within {
  border-color: var(--accent);
}

/* Line numbers. Scrolled in step with the textarea by app.js — the two share a
   font and line-height, and the textarea does not wrap, so every logical line is
   exactly one visual line and the columns stay aligned. */
.editor__gutter {
  border-right: 1px solid var(--border);
  color: var(--muted);
  flex-shrink: 0;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-line);
  /* Fixed room for three digits, so crossing line 10 or 100 does not shunt the
     whole editor sideways. */
  min-width: 44px;
  overflow: hidden;
  padding: var(--code-pad) 8px var(--code-pad) 12px;
  tab-size: 2;
  text-align: right;
  user-select: none;
  white-space: pre;
}

/* The line Mermaid blamed for a parse error. */
.editor__gutter b {
  color: var(--danger);
  font-weight: 600;
}

.editor__input {
  background: none;
  border: none;
  color: var(--text);
  flex: 1;
  font-family: var(--mono);
  font-size: var(--code-size);
  line-height: var(--code-line);
  min-width: 0;
  outline: none;
  padding: var(--code-pad) 14px;
  resize: none;
  tab-size: 2;
  white-space: pre;
}

/* Only while the first load is in flight. */
.editor__input:disabled {
  opacity: 0.6;
}

/* Error — sits under the editor and only takes space when there is one.
   Mermaid quotes the offending source back with a caret under it, so the text is
   monospaced and pre-wrapped to keep that caret pointing at the right column. */
.error {
  background: color-mix(in srgb, var(--danger) 10%, transparent);
  border: 1px solid color-mix(in srgb, var(--danger) 30%, transparent);
  border-radius: 8px;
  color: var(--danger);
  flex-shrink: 0;
  font-family: var(--mono);
  font-size: 12px;
  margin: 8px 0 0;
  max-height: 8em;
  overflow-y: auto;
  padding: 8px 10px;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Preview */
/* The zoom controls and the placeholder are positioned against this. */
.pane--preview {
  position: relative;
}

.viewport {
  background: var(--canvas);
  /* A dot grid, so panning an empty area still reads as movement. */
  background-image: radial-gradient(var(--grid) 1px, transparent 1px);
  background-size: 20px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: grab;
  flex: 1;
  min-height: 0;
  overflow: hidden;
  position: relative;
  touch-action: none; /* we handle drag-to-pan ourselves */
  /* Without this, dragging to pan sweeps a text selection across every label
     it crosses. */
  user-select: none;
}

.viewport.is-panning {
  cursor: grabbing;
}

/* Panned by viewport.js as translate(x, y). Zoom is not a transform — it resizes
   the SVG inside, so the drawing stays sharp instead of being a stretched
   bitmap. */
.stage {
  left: 0;
  position: absolute;
  top: 0;
  will-change: transform;
}

.stage svg {
  display: block;
}

/* Keep the last good drawing on screen while the source is mid-edit and broken,
   greyed back so it is obvious it no longer matches the text. */
.stage.is-stale {
  opacity: 0.35;
  transition: opacity 0.15s ease;
}

/* Shown before the first successful render, and if Mermaid fails to load. */
.placeholder {
  color: var(--muted);
  font-size: 13px;
  left: 50%;
  margin: 0;
  max-width: 80%;
  pointer-events: none;
  position: absolute;
  text-align: center;
  top: 50%;
  transform: translate(-50%, -50%);
}

/* Zoom controls — float over the canvas, bottom right. */
.zoom {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
  bottom: 12px;
  box-shadow: var(--shadow);
  display: flex;
  overflow: hidden;
  position: absolute;
  right: 12px;
}

.zoom__btn {
  background: none;
  border: none;
  color: var(--text);
  cursor: pointer;
  font-family: inherit;
  font-size: 14px;
  line-height: 1;
  padding: 7px 11px;
}

.zoom__btn:hover {
  background: var(--bg);
}

.zoom__level {
  border-left: 1px solid var(--border);
  border-right: 1px solid var(--border);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  min-width: 58px;
}

/* Toast */
.toast {
  background: var(--text);
  border-radius: 8px;
  bottom: 24px;
  color: var(--panel);
  font-size: 13px;
  left: 50%;
  max-width: calc(100% - 32px);
  opacity: 0;
  padding: 8px 16px;
  pointer-events: none;
  position: fixed;
  text-align: center;
  transform: translate(-50%, 8px);
  transition: opacity 0.18s ease, transform 0.18s ease;
  z-index: 10;
}

.toast--show {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* Narrow screens: stack the panes instead of splitting them. Side by side, two
   columns on a phone leaves neither one usable. */
@media (max-width: 820px) {
  .app {
    padding: 10px 12px 12px;
  }

  .panes {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(140px, 2fr) 3fr;
    row-gap: 10px;
  }

  .splitter {
    display: none;
  }
}
