:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --border: #dfe1e5;
  --ink: #202124;
  --muted: #5f6368;
  --accent: #1a73e8;
  --accent-soft: #e8f0fe;
  --danger: #d93025;
  --warn: #b06000;
  --ok: #137333;
  --radius: 6px;
  font-family: Inter, "Segoe UI", Helvetica, Arial, sans-serif;
  font-size: 13px;
  color: var(--ink);
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; background: var(--bg); overflow: hidden; }

button, input, select, textarea { font: inherit; color: inherit; }
button {
  border: 1px solid var(--border);
  background: var(--panel);
  border-radius: var(--radius);
  padding: 5px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { background: #f1f3f4; }
button:disabled { opacity: 0.45; cursor: default; }
button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
button.primary:hover { background: #1765cc; }
button.danger { color: var(--danger); }
button.link { border: none; background: none; padding: 0; color: var(--accent); }
button.icon { padding: 2px 7px; line-height: 1.2; }
input[type="text"], input[type="number"], input[type="search"], select, textarea {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 5px 7px;
  background: #fff;
  min-width: 0;
}
input:focus, select:focus, textarea:focus, button:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
kbd { border: 1px solid var(--border); border-bottom-width: 2px; border-radius: 3px; padding: 0 4px; font-size: 11px; background: #fff; }
code { background: #f1f3f4; padding: 0 3px; border-radius: 3px; font-size: 12px; }

/* Toolbar */
.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 10px;
  padding: 8px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--border);
  z-index: 5;
}
.brand span { white-space: nowrap; }
@media (max-width: 1500px) { .brand span { display: none; } }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 14px; }
.title-input { border: 1px solid transparent; border-radius: var(--radius); padding: 5px 8px; font-size: 14px; font-weight: 600; min-width: 140px; flex: 1 1 160px; max-width: 360px; background: transparent; }
.title-input:hover { border-color: var(--border); }
.group { display: flex; align-items: center; gap: 4px; }
.options label { display: flex; align-items: center; gap: 5px; color: var(--muted); margin-left: 6px; }

.menu { position: relative; }
.menu summary { list-style: none; border: 1px solid var(--border); border-radius: var(--radius); padding: 5px 10px; cursor: pointer; background: #fff; }
.menu summary::-webkit-details-marker { display: none; }
.menu summary::after { content: " ▾"; color: var(--muted); }
.menu-items { position: absolute; top: calc(100% + 4px); left: 0; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); box-shadow: 0 4px 16px rgba(0,0,0,.12); display: flex; flex-direction: column; padding: 4px; min-width: 190px; z-index: 20; white-space: nowrap; }
.menu-items button { border: none; text-align: left; }

/* Layout */
main { flex: 1; display: flex; min-height: 0; }
.sidebar { width: 280px; flex: none; background: var(--panel); overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.sidebar.left { border-right: 1px solid var(--border); }
.sidebar.right { border-left: 1px solid var(--border); width: 340px; }
h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 0; font-weight: 700; }
h3 { font-size: 12px; margin: 14px 0 6px; color: var(--muted); font-weight: 700; display: flex; justify-content: space-between; align-items: center; }
.count { font-weight: 400; color: var(--muted); }
.panel-head { display: flex; justify-content: space-between; align-items: center; }
.help { color: var(--muted); margin: 6px 0; line-height: 1.45; font-size: 12px; }

.entity-list { list-style: none; margin: 0; padding: 0; flex: 1; overflow-y: auto; min-height: 80px; }
.entity-list li { display: flex; align-items: center; gap: 8px; padding: 5px 6px; border-radius: var(--radius); cursor: pointer; }
.entity-list li:hover { background: #f1f3f4; }
.entity-list li.selected { background: var(--accent-soft); }
.entity-list .swatch { width: 10px; height: 10px; border-radius: 2px; flex: none; border: 1px solid; }
.entity-list .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.entity-list .status-tag { font-size: 9.5px; font-weight: 700; text-transform: uppercase; white-space: nowrap; max-width: 80px; overflow: hidden; text-overflow: ellipsis; }
.entity-list .jur { color: var(--muted); font-size: 11px; white-space: nowrap; }

.bulk summary { cursor: pointer; font-weight: 600; color: var(--muted); }
.bulk textarea { width: 100%; font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; resize: vertical; margin-bottom: 6px; }

/* Canvas */
.canvas-wrap { flex: 1; position: relative; min-width: 0; background: #fbfbfc; background-image: radial-gradient(#e3e5e8 1px, transparent 1px); background-size: 20px 20px; }
#canvas { width: 100%; height: 100%; display: block; cursor: grab; user-select: none; touch-action: none; }
#canvas.panning { cursor: grabbing; }
#canvas .node { cursor: pointer; }
#canvas .node.dragging { cursor: grabbing; }
#canvas .edge, #canvas .edge-label { cursor: pointer; }
/* Shift held: Shift+drag selects an area (or links from a box) */
#canvas.shift-held, #canvas.shift-held .node, #canvas.shift-held .edge, #canvas.shift-held .edge-label { cursor: crosshair; }
#canvas:focus { outline: none; }
.app-version { position: absolute; right: 12px; bottom: 10px; color: var(--muted); font-size: 11px; background: rgba(255,255,255,.85); padding: 3px 8px; border-radius: var(--radius); cursor: default; }
.hint { position: absolute; left: 12px; bottom: 10px; max-width: calc(100% - 210px); color: var(--muted); font-size: 11.5px; background: rgba(255,255,255,.85); padding: 4px 8px; border-radius: var(--radius); pointer-events: none; }
.empty { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; color: var(--muted); pointer-events: none; }
.empty[hidden] { display: none; }
.empty .muted { font-size: 12px; margin-top: 0; }
.empty button { pointer-events: auto; }

/* Collapsible side panels */
.collapse-btn {
  position: absolute;
  top: 10px;
  z-index: 4;
  width: 24px;
  height: 40px;
  padding: 0;
  font-size: 16px;
  line-height: 1;
  color: var(--muted);
  box-shadow: 0 1px 3px rgba(0,0,0,.08);
}
.collapse-btn.left { left: 0; border-radius: 0 var(--radius) var(--radius) 0; border-left: none; }
.collapse-btn.right { right: 0; border-radius: var(--radius) 0 0 var(--radius); border-right: none; }
body.left-collapsed .sidebar.left,
body.right-collapsed .sidebar.right { display: none; }

/* Chart switcher */
/* Tabs of open charts */
.tabbar { display: flex; align-items: flex-end; gap: 6px; padding: 6px 12px 0; background: var(--bg); border-bottom: 1px solid var(--border); }
.tabs { display: flex; gap: 2px; overflow-x: auto; flex: 1; min-width: 0; scrollbar-width: thin; }
.tab { display: flex; align-items: center; gap: 4px; max-width: 220px; padding: 5px 6px 5px 12px; border: 1px solid var(--border); border-bottom: none; border-radius: var(--radius) var(--radius) 0 0; background: #eceef1; color: var(--muted); cursor: pointer; white-space: nowrap; user-select: none; }
.tab:hover { background: #f3f4f6; }
.tab.active { background: var(--panel); color: var(--ink); font-weight: 600; margin-bottom: -1px; padding-bottom: 6px; }
.tab .tab-title { overflow: hidden; text-overflow: ellipsis; }
.tab .tab-close { border: none; background: transparent; padding: 0 4px; line-height: 1; font-size: 14px; color: var(--muted); border-radius: 4px; }
.tab .tab-close:hover { background: #dadce0; color: var(--ink); }
.tab-new { border: 1px dashed var(--border); border-bottom: none; border-radius: var(--radius) var(--radius) 0 0; background: transparent; color: var(--muted); padding: 5px 10px; }
.all-charts { margin-bottom: 4px; }
.all-charts summary { padding: 3px 10px; font-size: 12px; }
.menu-items.right { left: auto; right: 0; max-height: 60vh; overflow-y: auto; }
.menu-items button.current { font-weight: 600; }
.menu-items .meta { color: var(--muted); font-size: 11px; margin-left: 8px; }

/* Inspector */
.field { display: grid; grid-template-columns: 92px 1fr; align-items: center; gap: 6px; margin-bottom: 6px; }
.field > span { color: var(--muted); }
.field input, .field select { width: 100%; }
.row-actions { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }

table.links { width: 100%; border-collapse: collapse; }
table.links td { padding: 3px 2px; vertical-align: middle; }
table.links td.who { max-width: 120px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
table.links input.pct { width: 64px; text-align: right; }
table.links input.cls { width: 100%; }
table.links tr.total td { border-top: 1px solid var(--border); color: var(--muted); padding-top: 5px; }
table.links tr.total.bad td { color: var(--danger); font-weight: 600; }
.kind-tag { font-size: 10px; padding: 1px 5px; border-radius: 8px; background: #f1f3f4; color: var(--muted); }
.adder { display: flex; gap: 4px; margin-top: 6px; }
.adder select { flex: 1; }
.adder input { width: 64px; text-align: right; }
.muted { color: var(--muted); }
.sel-list { list-style: none; margin: 0; padding: 0; max-height: 240px; overflow-y: auto; }
.sel-list li { display: flex; justify-content: space-between; align-items: center; gap: 6px; padding: 2px 0; }
.sel-list .link { text-align: left; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.subchart { background: #f8f9fa; border: 1px solid var(--border); border-radius: var(--radius); padding: 2px 10px 10px; margin-top: 8px; }
.subchart h3 { margin-top: 8px; }
.subchart select { width: 100%; }
.check { display: flex; align-items: center; gap: 6px; margin-top: 8px; color: var(--muted); cursor: pointer; }
.check:has(input:disabled) { opacity: .5; cursor: default; }
.empty-note { color: var(--muted); font-style: italic; margin: 2px 0; }

table.eff { width: 100%; border-collapse: collapse; }
table.eff td { padding: 3px 2px; }
table.eff td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
table.eff .bar { height: 4px; background: var(--accent-soft); border-radius: 2px; margin-top: 2px; }
table.eff .bar > div { height: 100%; background: var(--accent); border-radius: 2px; }

.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; margin: 8px 0; }
.stats div { background: #f8f9fa; border-radius: var(--radius); padding: 8px; }
.stats b { display: block; font-size: 18px; }
.stats span { color: var(--muted); font-size: 11px; }

/* Issues */
.issues { border-top: 1px solid var(--border); padding-top: 10px; margin-top: auto; }
.issues ul { list-style: none; padding: 0; margin: 6px 0 0; max-height: 220px; overflow-y: auto; }
.issues li { display: flex; gap: 6px; padding: 4px 4px; border-radius: var(--radius); cursor: pointer; line-height: 1.35; }
.issues li:hover { background: #f1f3f4; }
.issues li .dot { flex: none; width: 8px; height: 8px; border-radius: 50%; margin-top: 5px; }
.issues li.error .dot { background: var(--danger); }
.issues li.warning .dot { background: #f9ab00; }
.issues li.info .dot { background: #9aa0a6; }
.issues li.ok { cursor: default; color: var(--ok); }

.toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%) translateY(20px); background: #323232; color: #fff; padding: 8px 14px; border-radius: var(--radius); opacity: 0; transition: all .2s; pointer-events: none; z-index: 50; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }

@media (max-width: 900px) {
  main { flex-direction: column; overflow-y: auto; }
  .sidebar, .sidebar.right { width: auto; border: none; max-height: none; }
  .canvas-wrap { min-height: 60vh; order: -1; }
  .entity-list { max-height: 240px; }
  .collapse-btn { display: none; }
}

/* Print dialog */
.print-dialog { border: 1px solid var(--border); border-radius: 10px; padding: 18px 20px; width: min(520px, 92vw); box-shadow: 0 12px 40px rgba(0,0,0,.2); }
.print-dialog::backdrop { background: rgba(32,33,36,.35); }
.print-dialog h2 { font-size: 15px; text-transform: none; letter-spacing: 0; color: var(--ink); margin-bottom: 4px; }
.print-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 12px 0; }
.print-grid label { display: flex; flex-direction: column; gap: 4px; color: var(--muted); font-size: 12px; }
.print-grid label.check { flex-direction: row; grid-column: 1 / -1; align-items: center; }
.pr-summary { font-weight: 600; margin: 6px 0; }
.pr-summary.warn { color: var(--warn); }
.pr-pages { margin: 0 0 8px; padding-left: 22px; max-height: 220px; overflow-y: auto; }
.pr-pages li { padding: 2px 0; }
.pr-pages .pt { color: var(--muted); font-variant-numeric: tabular-nums; }
.pr-pages .pt.small { color: var(--warn); font-weight: 600; }
.row-actions.end { justify-content: flex-end; }
