:root {
  --ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "Cascadia Mono", Consolas, "SF Mono", Menlo, monospace;
  --bg: #f3f4f6; --panel: #ffffff; --surface: #ffffff; --canvas: #eef0f3; --grid: #d3d7de;
  --border: #d0d5dd; --line: #e6e9ee; --text: #1b2330; --muted: #677384; --hover: #eef3fb;
  --accent: #2563eb; --accent-text: #ffffff; --accent-soft: #dbe7ff; --edge: #7b8797;
  --pk: #b45309; --fk: #2563eb; --danger: #c0262d; --input: #ffffff; --shadow: 0 8px 30px rgba(15, 23, 42, .18);
  color-scheme: light;
}
:root[data-theme="dark"] {
  --bg: #15181d; --panel: #1b1f26; --surface: #20252d; --canvas: #14171b; --grid: #2a3039;
  --border: #363d49; --line: #2c323c; --text: #e7eaef; --muted: #94a0b0; --hover: #29303b;
  --accent: #5b9dff; --accent-text: #0b1220; --accent-soft: #1f3a66; --edge: #7a8697;
  --pk: #f0b35a; --fk: #7fb2ff; --danger: #ff7b81; --input: #161a20; --shadow: 0 8px 30px rgba(0, 0, 0, .5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; font: 13px/1.4 var(--ui); color: var(--text); background: var(--bg); overflow: hidden; }
code { font-family: var(--mono); font-size: 12px; }
[hidden] { display: none !important; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  height: 30px; padding: 0 11px; border: 1px solid var(--border); border-radius: 6px; background: var(--panel);
  cursor: pointer; white-space: nowrap;
}
button:hover:not(:disabled) { background: var(--hover); }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); font-weight: 600; }
button.primary:hover:not(:disabled) { background: var(--accent); filter: brightness(1.08); }
button.danger { color: var(--danger); }
button.sq { width: 30px; padding: 0; font-size: 16px; }
button.ghost { width: 100%; border-style: dashed; color: var(--muted); background: transparent; margin-top: 6px; }
button.icon { width: 24px; height: 28px; padding: 0; border: 0; background: transparent; color: var(--muted); font-size: 16px; border-radius: 4px; }
input[type="text"], input[type="search"], input[type="number"], select, textarea {
  height: 28px; padding: 0 7px; border: 1px solid var(--border); border-radius: 5px; background: var(--input); min-width: 0; width: 100%;
}
select { padding: 0 3px; }
textarea { height: auto; padding: 6px 8px; resize: vertical; font-family: var(--mono); font-size: 12px; line-height: 1.5; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
input:disabled { opacity: .4; }
input[type="color"] { width: 34px; height: 28px; padding: 2px; border: 1px solid var(--border); border-radius: 5px; background: var(--input); cursor: pointer; }
input[type="checkbox"], input[type="radio"] { accent-color: var(--accent); margin: 0; }

/* ---------- top bar ---------- */
#bar { display: flex; align-items: center; gap: 6px; height: 48px; padding: 0 10px; background: var(--panel); border-bottom: 1px solid var(--border); flex: none; overflow-x: auto; }
.brand { display: flex; align-items: center; flex: none; }
.name-input { width: 200px !important; flex: none; height: 30px !important; font-weight: 600; border-color: transparent !important; background: transparent !important; }
.name-input:hover, .name-input:focus { border-color: var(--border) !important; background: var(--input) !important; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; flex: none; }
.grow { flex: 1; min-width: 8px; }
.save-state { color: var(--muted); font-size: 12px; white-space: nowrap; margin-right: 6px; }
.save-state.bad { color: var(--danger); font-weight: 600; }
.group { display: flex; flex: none; }
.group button { border-radius: 0; margin-left: -1px; min-width: 30px; }
.group button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.group button:last-child { border-radius: 0 6px 6px 0; }
#btn-zoom-reset { width: 54px; padding: 0; font-variant-numeric: tabular-nums; }

/* ---------- layout ---------- */
main { flex: 1; display: flex; min-height: 0; }
#side { width: 430px; flex: none; display: flex; flex-direction: column; background: var(--panel); border-right: 1px solid var(--border); min-height: 0; }
#stage { flex: 1; position: relative; min-width: 0; }

/* ---------- canvas ---------- */
#canvas { display: block; width: 100%; height: 100%; background: var(--canvas); cursor: grab; user-select: none; -webkit-user-select: none; touch-action: none; }
#canvas.panning { cursor: grabbing; }
.grid-dot { fill: var(--grid); }
.tbl { cursor: move; }
.tbl-box { fill: var(--surface); stroke: var(--border); stroke-width: 1; }
.tbl.sel .tbl-box { stroke: var(--accent); stroke-width: 2.5; }
.tbl-name { fill: #fff; font: 600 13px var(--ui); dominant-baseline: central; }
.row-bg { fill: transparent; }
.row:hover .row-bg { fill: var(--hover); }
.row.drop .row-bg { fill: var(--accent-soft); }
.row-line { stroke: var(--line); stroke-width: 1; }
.col-name { fill: var(--text); font: 13px var(--ui); dominant-baseline: central; }
.col-name.pk { font-weight: 600; }
.col-type { fill: var(--muted); font: 12px var(--mono); dominant-baseline: central; text-anchor: end; }
.badge { font: 700 9px var(--ui); dominant-baseline: central; fill: var(--muted); letter-spacing: .3px; }
.badge.pk { fill: var(--pk); }
.badge.fk { fill: var(--fk); }
.handle { fill: var(--surface); stroke: var(--accent); stroke-width: 1.5; opacity: 0; cursor: crosshair; }
.tbl:hover .handle { opacity: .5; }
.row:hover .handle, .handle:hover { opacity: 1; }
.handle:hover { fill: var(--accent); }
.edge { cursor: pointer; }
.edge-hit { fill: none; stroke: transparent; stroke-width: 14; }
.edge-line, .edge-mark { fill: none; stroke: var(--edge); stroke-width: 1.5; }
.edge:hover .edge-line, .edge:hover .edge-mark { stroke: var(--accent); }
.edge.sel .edge-line, .edge.sel .edge-mark { stroke: var(--accent); stroke-width: 2.5; }
.link-line { fill: none; stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; pointer-events: none; }

#hint { position: absolute; left: 12px; bottom: 10px; right: 12px; color: var(--muted); font-size: 11.5px; pointer-events: none; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#empty { position: absolute; left: 50%; top: 44%; transform: translate(-50%, -50%); width: min(460px, 90%); text-align: center; background: var(--panel); border: 1px solid var(--border); border-radius: 12px; padding: 26px 24px; box-shadow: var(--shadow); }
#empty h2 { margin: 0 0 6px; font-size: 18px; }
#empty p { margin: 0 0 16px; color: var(--muted); }
#empty .row { justify-content: center; }

/* ---------- sidebar ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--border); flex: none; }
.tab { flex: 1; height: 38px; border: 0; border-radius: 0; background: transparent; color: var(--muted); border-bottom: 2px solid transparent; font-weight: 600; }
.tab.on { color: var(--text); border-bottom-color: var(--accent); }
.side-tools { display: flex; gap: 6px; padding: 8px 10px; flex: none; }
.list { flex: 1; overflow-y: auto; padding: 0 10px 40px; }
.empty { color: var(--muted); padding: 14px 4px; margin: 0; }
.item { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; background: var(--surface); }
.item.open { border-color: var(--accent); }
.item-head { display: flex; align-items: center; gap: 8px; height: 36px; padding: 0 10px; cursor: pointer; border-radius: 8px; }
.item-head:hover { background: var(--hover); }
.dot { width: 10px; height: 10px; border-radius: 3px; flex: none; }
.item-title { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.count { color: var(--muted); font-size: 12px; margin-left: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 55%; }
.chev { color: var(--muted); width: 10px; flex: none; }
.item-body { padding: 4px 10px 12px; border-top: 1px solid var(--line); }
.sec { margin: 14px 0 6px; font-size: 11px; font-weight: 700; letter-spacing: .5px; text-transform: uppercase; color: var(--muted); }
.row { display: flex; gap: 8px; align-items: flex-end; flex-wrap: wrap; }
.row.end { justify-content: flex-end; margin-top: 14px; }
.field { display: flex; flex-direction: column; gap: 3px; flex: 1; min-width: 0; margin-top: 6px; }
.field > span { font-size: 11px; color: var(--muted); }
.field.narrow { flex: 0 0 90px; }
.field.tiny { flex: 0 0 34px; }
.field.inline { flex: 0 0 auto; flex-direction: row; align-items: center; gap: 5px; margin-top: 0; }
.field.inline input { width: 64px; }
.chk { display: inline-flex; align-items: center; gap: 5px; height: 28px; cursor: pointer; white-space: nowrap; }
.hint, .warn { margin: 4px 0; font-size: 12px; color: var(--muted); }
.warn { color: var(--danger); }

.col-head, .col-main { display: grid; grid-template-columns: minmax(0, 1.1fr) minmax(0, 1.1fr) 50px 26px 28px 20px 20px; gap: 4px; align-items: center; }
.col-head { font-size: 11px; color: var(--muted); padding: 0 1px 3px; }
.col-head span:nth-child(4), .col-head span:nth-child(5) { text-align: center; }
.col { padding: 2px 0; border-radius: 6px; }
.col.open { background: var(--hover); padding: 4px; margin: 2px -4px; }
.col-more { padding: 2px 2px 6px; }
.col-more .row { margin-top: 4px; }
.computed { font: 12px var(--mono); color: var(--muted); padding: 0 6px; }
.tog { height: 28px; padding: 0; font-size: 11px; font-weight: 700; color: var(--muted); background: var(--input); }
.tog.on { background: var(--accent-soft); border-color: var(--accent); color: var(--text); }

.card { border: 1px solid var(--border); border-radius: 6px; padding: 8px; margin-top: 6px; background: var(--panel); }
.card-head { display: flex; align-items: center; gap: 6px; }
.tag { font-size: 10px; font-weight: 700; letter-spacing: .4px; padding: 3px 6px; border-radius: 4px; background: var(--line); color: var(--muted); white-space: nowrap; }
.tag.pk { color: var(--pk); }
.keycols { color: var(--muted); font-family: var(--mono); font-size: 12px; line-height: 28px; margin-left: auto; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chips { display: flex; flex-wrap: wrap; align-items: center; gap: 5px; margin-top: 6px; }
.chips .lbl { font-size: 11px; color: var(--muted); width: 52px; }
.chip { display: inline-flex; align-items: center; gap: 2px; height: 26px; padding-left: 8px; border: 1px solid var(--border); border-radius: 13px; background: var(--input); font-size: 12px; }
.chip button { height: 22px; border: 0; background: transparent; padding: 0 5px; color: var(--muted); border-radius: 11px; }
.chip .ord { font-size: 10px; font-weight: 700; }
.chips select.add { width: 92px; height: 26px; color: var(--muted); }
.chk-row { display: grid; grid-template-columns: 120px minmax(0, 1fr) 22px; gap: 4px; margin-top: 4px; }
.pair { display: grid; grid-template-columns: minmax(0, 1fr) 16px minmax(0, 1fr) 22px; gap: 4px; align-items: center; margin-top: 4px; }
.arrow { color: var(--muted); text-align: center; }

/* ---------- dialogs, menus, toasts ---------- */
dialog { width: min(760px, 94vw); max-height: 90vh; padding: 20px; border: 1px solid var(--border); border-radius: 12px; background: var(--panel); color: var(--text); box-shadow: var(--shadow); }
dialog[open] { display: flex; flex-direction: column; }
dialog::backdrop { background: rgba(10, 14, 20, .45); }
dialog h3 { margin: 0 0 4px; font-size: 16px; }
dialog .sub { margin: 0 0 10px; color: var(--muted); }
dialog textarea { flex: 1; min-height: 300px; resize: none; white-space: pre; overflow: auto; }
dialog .opts { margin: 8px 0; align-items: center; gap: 14px; }
#imp-result { max-height: 150px; overflow: auto; }
#imp-result p { margin: 4px 0; }
.warnings { margin: 4px 0; padding-left: 18px; color: var(--muted); font-size: 12px; }
#dg-list { overflow-y: auto; min-height: 80px; margin-top: 6px; }
.dg { display: flex; align-items: center; gap: 8px; padding: 8px 10px; border: 1px solid var(--border); border-radius: 8px; margin-bottom: 6px; }
.dg.cur { border-color: var(--accent); }
.dg-info { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.dg-info span { color: var(--muted); font-size: 12px; }
.menu { position: fixed; z-index: 50; min-width: 170px; padding: 4px; background: var(--panel); border: 1px solid var(--border); border-radius: 8px; box-shadow: var(--shadow); display: flex; flex-direction: column; }
.menu button { border: 0; text-align: left; background: transparent; }
.menu hr { border: 0; border-top: 1px solid var(--line); margin: 4px 0; width: 100%; }
#toasts { position: fixed; bottom: 34px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; z-index: 100; pointer-events: none; }
.toast { padding: 8px 14px; border-radius: 8px; background: #1b2330; color: #fff; box-shadow: var(--shadow); transition: opacity .3s; }
.toast.error { background: #a11d24; }
.toast.out { opacity: 0; }

@media (max-width: 900px) { #side { width: 340px; } #hint { display: none; } }
