/* ERD Board — dark by default, light via [data-theme="light"]. */

:root {
  /* Tells the browser to paint scrollbars, form controls and the textarea
     caret dark. Without it the native scrollbars stay light on a dark page. */
  color-scheme: dark;
  --bg: #0d1117;
  --stage: #0a0e14;
  --panel: #11161d;
  --panel-2: #161c25;
  --card: #161b22;
  --card-head: #1c2330;
  --line: #262d38;
  --line-soft: #1c222c;
  --text: #e6edf3;
  --text-dim: #8b98a9;
  --text-faint: #5d6979;
  --accent: #6366f1;
  --accent-soft: #6366f133;
  --edge: #4a5768;
  --edge-hot: #7c8cf8;
  --grid: #1a2029;
  --shadow: 0 1px 2px #0008, 0 8px 24px -8px #000a;
  --radius: 8px;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", Vazirmatn, Roboto, sans-serif;
}

[data-theme="light"] {
  color-scheme: light;
  --bg: #ffffff;
  --stage: #f4f6f9;
  --panel: #ffffff;
  --panel-2: #f7f9fb;
  --card: #ffffff;
  --card-head: #f2f5f9;
  --line: #dde3ea;
  --line-soft: #e9edf2;
  --text: #16202b;
  --text-dim: #5b6876;
  --text-faint: #8c98a6;
  --accent: #4f46e5;
  --accent-soft: #4f46e522;
  --edge: #9aa8ba;
  --edge-hot: #4f46e5;
  --grid: #e4e9ef;
  --shadow: 0 1px 2px #0f172a12, 0 8px 20px -10px #0f172a26;
}

* { box-sizing: border-box; }

/* Scrollbars, themed. Firefox takes the two-value form; WebKit needs the
   pseudo-elements. Both are driven by the same tokens as everything else. */
* { scrollbar-width: thin; scrollbar-color: var(--line) transparent; }
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb {
  background: var(--line);
  border-radius: 6px;
  border: 2px solid transparent;
  background-clip: content-box;
}
::-webkit-scrollbar-thumb:hover { background: var(--edge); background-clip: content-box; }
::-webkit-scrollbar-corner { background: transparent; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.45 var(--sans);
  -webkit-font-smoothing: antialiased;
}

button, input, select, textarea { font: inherit; color: inherit; }

kbd {
  font: 11px var(--mono);
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 4px;
  padding: 0 4px;
  color: var(--text-dim);
}

/* ── top bar ───────────────────────────────────────────────── */

.topbar {
  height: 48px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  position: relative;
  z-index: 40;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 650;
  letter-spacing: -0.01em;
  margin-right: 4px;
}
.brand span { display: none; }
@media (min-width: 900px) { .brand span { display: inline; } }

.spacer { flex: 1; }

.btn {
  height: 30px;
  padding: 0 10px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  color: var(--text-dim);
  cursor: pointer;
  white-space: nowrap;
  transition: background .12s, color .12s, border-color .12s;
}
.btn:hover { background: var(--card-head); color: var(--text); border-color: var(--edge); }
.btn.icon { width: 30px; padding: 0; display: grid; place-items: center; font-size: 15px; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.1); }
.btn.wide { width: 100%; }
.btn.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }

.toolgroup { display: flex; gap: 2px; }

.check {
  display: flex; align-items: center; gap: 6px;
  height: 30px; padding: 0 10px;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel-2); color: var(--text-dim);
  cursor: pointer; white-space: nowrap; user-select: none;
}
.check:hover { color: var(--text); border-color: var(--edge); }
.check input { accent-color: var(--accent); margin: 0; cursor: pointer; }
.check:has(input:checked) { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }
.zoomlabel { min-width: 54px; font-variant-numeric: tabular-nums; font-size: 12px; }

.picker {
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  padding: 0 8px;
  max-width: 190px;
  cursor: pointer;
}

.pill {
  display: inline-block;
  min-width: 17px;
  padding: 0 5px;
  border-radius: 9px;
  background: var(--line);
  color: var(--text-dim);
  font: 600 11px/16px var(--sans);
  text-align: center;
}
.pill.hot { background: var(--accent); color: #fff; }

.savestate {
  font-size: 11px;
  color: var(--text-faint);
  min-width: 58px;
  text-align: right;
}

/* ── search ────────────────────────────────────────────────── */

.search { position: relative; width: min(300px, 30vw); }
.search input {
  width: 100%;
  height: 30px;
  border-radius: 6px;
  border: 1px solid var(--line);
  background: var(--panel-2);
  padding: 0 10px;
  outline: none;
}
.search input:focus { border-color: var(--accent); }
.search input::placeholder { color: var(--text-faint); }

.search-results {
  position: absolute;
  top: 36px;
  left: 0;
  right: 0;
  max-height: 340px;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 4px;
  z-index: 50;
}
.search-results .res {
  padding: 6px 8px;
  border-radius: 5px;
  cursor: pointer;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}
.search-results .res:hover, .search-results .res.sel { background: var(--accent-soft); }
.search-results .res b { font: 500 12px var(--mono); }
.search-results .res span { color: var(--text-faint); font-size: 11px; }
.search-results .empty { padding: 10px; color: var(--text-faint); }

/* ── layout ────────────────────────────────────────────────── */

.layout { display: flex; height: calc(100% - 48px); }

.sidebar {
  width: 232px;
  flex: none;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width .16s ease, border-width .16s;
}
.sidebar.collapsed { width: 0; border-right-width: 0; }
@media (max-width: 780px) { .sidebar { display: none; } }

/* Both panels collapse; the toggle glyph flips to point the way it will move. */
#sidebarToggle, #notesToggle { transition: none; }

.side-section { display: flex; flex-direction: column; min-height: 0; border-bottom: 1px solid var(--line); }
.side-section.grow { flex: 1; border-bottom: none; }

.side-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 9px 12px 7px;
  font: 600 11px/1 var(--sans);
  letter-spacing: .07em;
  text-transform: uppercase;
  color: var(--text-faint);
}
.linkbtn {
  background: none; border: none; padding: 0 0 0 8px;
  color: var(--text-dim); cursor: pointer; font-size: 11px; text-transform: none; letter-spacing: 0;
}
.linkbtn:hover { color: var(--accent); }
.muted { color: var(--text-faint); font-weight: 400; letter-spacing: 0; text-transform: none; }

.grouplist { padding: 0 8px 10px; max-height: 34vh; overflow-y: auto; }
.grp {
  display: flex; align-items: center; gap: 8px;
  padding: 5px 6px; border-radius: 5px; cursor: pointer; user-select: none;
}
.grp:hover { background: var(--panel-2); }
.grp.off { opacity: .38; }
.grp .swatch { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.grp .n { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.grp .c { color: var(--text-faint); font: 11px var(--mono); }

.tablelist { flex: 1; overflow-y: auto; padding: 0 8px 10px; }
.tbl-item {
  display: flex; align-items: center; gap: 7px;
  padding: 4px 6px; border-radius: 5px; cursor: pointer;
  font: 12px var(--mono);
  color: var(--text-dim);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.tbl-item:hover { background: var(--panel-2); color: var(--text); }
.tbl-item.sel { background: var(--accent-soft); color: var(--text); }
.tbl-item .dot { width: 7px; height: 7px; border-radius: 50%; flex: none; }
.tbl-item .badge { margin-left: auto; font: 600 10px var(--sans); color: var(--accent); }

.side-foot { padding: 9px 12px; border-top: 1px solid var(--line); color: var(--text-faint); font-size: 11px; display: grid; gap: 4px; }

/* ── stage / canvas ────────────────────────────────────────── */

.stage { flex: 1; position: relative; overflow: hidden; background: var(--stage); }

.viewport {
  position: absolute;
  inset: 0;
  overflow: hidden;
  cursor: default;
  background-image: radial-gradient(var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  background-position: 0 0;
}
.viewport.panning { cursor: grabbing; }
.viewport.pannable { cursor: grab; }

.world { position: absolute; top: 0; left: 0; transform-origin: 0 0; will-change: transform; }
.areas { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; }
.edges { position: absolute; top: 0; left: 0; overflow: visible; pointer-events: none; }
.edges path { pointer-events: stroke; }
.tables { position: absolute; top: 0; left: 0; }
.annos { position: absolute; top: 0; left: 0; }

/* domain grouping frames — drawn behind everything, follow the tables */
.areas .frame { stroke-width: 1.5; stroke-dasharray: 7 5; rx: 16; }
.areas .frame-label {
  font: 600 13px var(--sans);
  letter-spacing: .04em;
  text-transform: uppercase;
}
.areas.off { display: none; }

/* table card */
.tnode {
  position: absolute;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: grab;
  user-select: none;
  transition: opacity .13s, box-shadow .13s, border-color .13s;
}
.tnode:active { cursor: grabbing; }
.tnode .thead {
  display: flex; align-items: center; gap: 6px;
  padding: 0 9px;
  background: var(--card-head);
  border-bottom: 1px solid var(--line);
  border-left: 3px solid var(--gc, var(--accent));
  font: 600 12.5px/1 var(--sans);
  overflow: hidden;
}
.tnode .thead .tname { flex: 1; font-family: var(--mono); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tnode .thead .tcount { color: var(--text-faint); font: 400 10px var(--mono); }
.tnode .thead .tnote {
  background: var(--accent); color: #fff; border-radius: 8px;
  font: 600 9.5px/13px var(--sans); padding: 0 5px;
}
.tnode .trow {
  display: flex; align-items: center; gap: 8px;
  padding: 0 9px;
  font: 11.5px/1 var(--mono);
  color: var(--text-dim);
  white-space: nowrap;
}
.tnode .trow .cname { flex: none; color: var(--text); }
.tnode .trow .ctype { margin-left: auto; color: var(--text-faint); font-size: 10.5px; }
.tnode .trow .key { font-size: 9px; letter-spacing: .04em; padding: 0 3px; border-radius: 3px; flex: none; }
.tnode .trow .key.pk { background: #eab30833; color: #eab308; }
.tnode .trow .key.fk { background: #38bdf833; color: #38bdf8; }
.tnode .trow.hot { background: var(--accent-soft); }
.tnode .trow.nn .cname::after { content: "*"; color: #f4707033; }

.tnode.dim { opacity: .16; }
.tnode.sel { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), var(--shadow); }
.tnode.near { border-color: var(--edge-hot); }
.tnode.hidden { display: none; }

[data-theme="light"] .tnode .trow .key.pk { background: #f5d20e40; color: #a07800; }
[data-theme="light"] .tnode .trow .key.fk { background: #38bdf833; color: #0b7fb0; }

/* annotation cards — titles, descriptions, explanations authored in the JSON */
.anode {
  position: absolute;
  background: var(--card);
  border: 1px dashed var(--line);
  border-left: 3px solid var(--gc, var(--accent));
  border-radius: var(--radius);
  padding: 9px 12px 11px;
  box-shadow: var(--shadow);
  cursor: grab;
  user-select: none;
  transition: opacity .13s, border-color .13s;
}
.anode:active { cursor: grabbing; }
.anode .atitle {
  font: 700 13px/1.3 var(--sans);
  color: var(--text);
  margin-bottom: 5px;
  letter-spacing: -0.005em;
}
.anode .abody {
  font: 12px/1.55 var(--sans);
  color: var(--text-dim);
  white-space: pre-wrap;
}
.anode .abody strong { color: var(--text); font-weight: 600; }
.anode.dim { opacity: .16; }
.anode.hidden { display: none; }

/* edges */
.edges .link { fill: none; stroke: var(--edge); stroke-width: 1.4; transition: stroke .12s, opacity .12s; }
.edges .link.dim { opacity: .1; }
.edges .link.hot { stroke: var(--edge-hot); stroke-width: 2; }
.edges .link.self { stroke-dasharray: 4 3; }
.edges .cap { fill: none; stroke: var(--edge); stroke-width: 1.4; }
.edges .cap.dim { opacity: .1; }
.edges .cap.hot { stroke: var(--edge-hot); stroke-width: 2; }
.edges .hit { stroke: transparent; stroke-width: 12; fill: none; cursor: pointer; }

/* minimap */
.minimap {
  position: absolute; right: 12px; bottom: 12px;
  width: 200px; height: 140px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  cursor: pointer;
  opacity: .92;
}
.minimap canvas { width: 100%; height: 100%; display: block; }
.minimap-view {
  position: absolute;
  border: 1.5px solid var(--accent);
  background: var(--accent-soft);
  border-radius: 3px;
  pointer-events: none;
}
@media (max-width: 780px) { .minimap { display: none; } }

.hint {
  position: absolute; left: 12px; bottom: 12px;
  padding: 5px 9px; border-radius: 6px;
  background: var(--panel); border: 1px solid var(--line);
  color: var(--text-dim); font: 11.5px var(--mono);
  pointer-events: none; opacity: 0; transition: opacity .15s;
  max-width: 60ch; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.hint.on { opacity: 1; }

/* ── notes panel ───────────────────────────────────────────── */

.notes {
  width: 300px; flex: none;
  background: var(--panel);
  border-left: 1px solid var(--line);
  display: flex; flex-direction: column; overflow: hidden;
}
/* `display: flex` above outranks the user-agent rule for [hidden], so the
   attribute needs saying again or the panel can never be closed. */
.notes[hidden] { display: none; }
@media (max-width: 980px) {
  .notes { position: absolute; right: 0; top: 48px; bottom: 0; z-index: 30; box-shadow: var(--shadow); }
}

.note-compose { padding: 0 12px 12px; border-bottom: 1px solid var(--line); display: grid; gap: 7px; }
.note-target { font: 12px var(--mono); color: var(--accent); }
.note-compose textarea {
  width: 100%; resize: vertical;
  background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px;
  padding: 7px 9px; outline: none; min-height: 62px;
}
.note-compose textarea:focus { border-color: var(--accent); }

.notelist { flex: 1; overflow-y: auto; padding: 8px; display: grid; gap: 7px; align-content: start; }
.note {
  background: var(--panel-2); border: 1px solid var(--line);
  border-left: 3px solid var(--accent);
  border-radius: 6px; padding: 7px 9px;
}
.note.done { opacity: .5; border-left-color: var(--text-faint); }
.note.done .body { text-decoration: line-through; }
.note .meta { display: flex; align-items: center; gap: 6px; margin-bottom: 4px; }
.note .meta .where { font: 11px var(--mono); color: var(--accent); cursor: pointer; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note .meta .where:hover { text-decoration: underline; }
.note .body { white-space: pre-wrap; word-break: break-word; font-size: 12.5px; }
.note .acts { display: flex; gap: 8px; margin-top: 5px; }
.note .acts button { background: none; border: none; padding: 0; cursor: pointer; color: var(--text-faint); font-size: 11px; }
.note .acts button:hover { color: var(--accent); }
.notelist .empty { color: var(--text-faint); text-align: center; padding: 24px 12px; font-size: 12px; }
