:root {
    --bg: #f4f5f8;
    --surface: #ffffff;
    --surface-2: #eef0f5;
    --border: #dde1e9;
    --text: #1b2230;
    --muted: #6a7487;
    --accent: #3a6cf4;
    --accent-soft: rgba(58, 108, 244, .12);
    --danger: #d64545;
    --node-fill: #ffffff;
    --node-stroke: #6f7b90;
    --edge: #5b667a;
    --grid: #c9cfda;
    --shadow: 0 4px 18px rgba(20, 30, 55, .12);
    --boundary: rgba(58, 108, 244, .05);
    --font: "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
    --mono: ui-monospace, "Cascadia Code", Consolas, monospace;
}

:root[data-theme="dark"] {
    --bg: #11141a;
    --surface: #1a1f28;
    --surface-2: #222934;
    --border: #2d3441;
    --text: #e4e8f0;
    --muted: #8d97aa;
    --accent: #6f95ff;
    --accent-soft: rgba(111, 149, 255, .16);
    --danger: #ff6b6b;
    --node-fill: #232b38;
    --node-stroke: #8190a8;
    --edge: #9aa6bb;
    --grid: #343c4b;
    --shadow: 0 4px 18px rgba(0, 0, 0, .45);
    --boundary: rgba(111, 149, 255, .07);
}

* { box-sizing: border-box; }

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

body {
    font-family: var(--font);
    font-size: 14px;
    color: var(--text);
    background: var(--bg);
    overflow: hidden;
}

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

button {
    border: 1px solid var(--border);
    background: var(--surface);
    border-radius: 7px;
    padding: 5px 10px;
    cursor: pointer;
}
button:hover:not(:disabled) { background: var(--surface-2); }
button:disabled { opacity: .45; cursor: default; }
button:focus-visible, input:focus-visible, select:focus-visible, textarea:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 1px;
}
button.primary {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    font-weight: 600;
}
button.primary:hover:not(:disabled) { filter: brightness(1.08); background: var(--accent); }
button.danger:hover:not(:disabled) { color: var(--danger); }

input[type="text"], select, textarea {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 7px;
    padding: 5px 8px;
}

kbd {
    font-family: var(--mono);
    font-size: 11px;
    color: var(--muted);
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 4px;
    padding: 0 5px;
    white-space: nowrap;
}

.visually-hidden {
    position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap;
}

.boot { padding: 40px; color: var(--muted); }

/* ------------------------------------------------------------ layout */

.app { display: flex; height: 100%; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.content { flex: 1; min-height: 0; display: flex; }
.content > .board { flex: 1 1 55%; min-width: 0; }
.content > .mermaid-pane { flex: 1 1 45%; min-width: 0; border-left: 1px solid var(--border); }
.content.mermaid > .mermaid-pane { border-left: 0; }

/* ------------------------------------------------------------ command palette */

.palette {
    width: 268px;
    flex: none;
    display: flex;
    flex-direction: column;
    background: var(--surface);
    border-right: 1px solid var(--border);
}

.brand {
    display: flex; align-items: center; gap: 9px;
    padding: 13px 16px 8px;
    font-weight: 700; font-size: 16px; letter-spacing: .2px;
}
.brand .logo { color: var(--accent); font-size: 20px; }

.search { position: relative; padding: 6px 12px 10px; }
.search input { width: 100%; padding-right: 64px; }
.search kbd { position: absolute; right: 22px; top: 12px; pointer-events: none; }

.cmd-list { flex: 1; overflow-y: auto; padding: 0 8px 14px; }

.group-head {
    display: flex; align-items: center; gap: 6px; width: 100%;
    border: 0; background: none; border-radius: 6px;
    padding: 7px 8px 5px;
    font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .7px;
    color: var(--muted);
}
.group-head:hover:not(:disabled) { background: var(--surface-2); }
.group-head .count { margin-left: auto; font-weight: 500; opacity: .7; }
.chev { display: inline-block; transition: transform .12s; font-size: 14px; line-height: 1; }
.chev.open { transform: rotate(90deg); }

.cmd {
    display: flex; align-items: center; gap: 10px; width: 100%;
    border: 0; background: none; text-align: left;
    padding: 6px 9px; border-radius: 7px;
}
.cmd .icon { width: 20px; text-align: center; color: var(--muted); font-size: 15px; flex: none; }
.cmd .label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.cmd.active { background: var(--accent-soft); color: var(--accent); font-weight: 600; }
.cmd.active .icon { color: var(--accent); }
.no-match { color: var(--muted); padding: 12px; }

/* ------------------------------------------------------------ top bar */

.topbar {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 14px;
    background: var(--surface);
    border-bottom: 1px solid var(--border);
    min-height: 52px;
}
.topbar button, .topbar select, .pane-head button { white-space: nowrap; }
.topbar .title {
    font-size: 15px; font-weight: 600;
    border-color: transparent; background: transparent;
    width: clamp(90px, 18vw, 320px); min-width: 0; flex: 0 1 auto;
}
.topbar .title:hover { border-color: var(--border); }
.kind-badge {
    font-size: 11px; font-weight: 600; color: var(--accent);
    background: var(--accent-soft); border-radius: 99px; padding: 3px 10px; white-space: nowrap;
}
.spacer { flex: 1; }

.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.seg button { border: 0; border-radius: 0; background: var(--surface); padding: 6px 14px; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent); color: #fff; font-weight: 600; }
.seg.small button { padding: 4px 12px; }

.copy-group { display: inline-flex; gap: 0; }
.copy-group button { border-radius: 8px 0 0 8px; }
.copy-group select { border-radius: 0 8px 8px 0; border-left: 0; max-width: 130px; text-overflow: ellipsis; }
.icon-btn { width: 34px; padding: 5px 0; }

/* ------------------------------------------------------------ board */

.board { position: relative; overflow: hidden; background: var(--bg); }
.canvas { width: 100%; height: 100%; display: block; touch-action: none; user-select: none; outline: none; cursor: default; }
.board[data-tool="pan"] .canvas { cursor: grab; }
.board[data-tool="pan"] .canvas.panning { cursor: grabbing; }
.board[data-tool="connect"] .canvas { cursor: crosshair; }

.grid-dot { fill: var(--grid); }

.node { cursor: move; }
.board[data-tool="connect"] .node { cursor: crosshair; }
.board[data-tool="pan"] .node { cursor: grab; }

.node-shape {
    fill: var(--node-fill);
    stroke: var(--node-stroke);
    stroke-width: 1.6;
    transition: stroke .1s;
}
.node-shape.detail { fill: none; }
.node-text {
    font-size: 14px;
    fill: var(--text);
    pointer-events: none;
}
.node.selected .node-shape { stroke: var(--accent); stroke-width: 2.4; }
.node.drop .node-shape { stroke: var(--accent); fill: var(--accent-soft); stroke-width: 3; }
.node.source .node-shape { stroke: var(--accent); }
.selection-ring { fill: none; stroke: var(--accent); stroke-width: 1.4; stroke-dasharray: 4 3; pointer-events: none; }

.node.actor .node-shape { fill: #fff4e0; stroke: #c77700; }
.node.actor .actor-body { stroke: #c77700; stroke-width: 1.8; fill: none; stroke-linecap: round; }
:root[data-theme="dark"] .node.actor .node-shape { fill: #4a3410; }
.actor-hit { fill: transparent; }

.boundary-box { fill: var(--boundary); stroke: var(--accent); stroke-width: 1.5; stroke-dasharray: 7 5; pointer-events: stroke; }
.boundary-head { fill: transparent; pointer-events: all; }
.boundary-title { font-size: 13px; font-weight: 700; fill: var(--accent); pointer-events: none; }
.node.boundary { cursor: move; }
.node.boundary.selected .boundary-box { stroke-width: 2.4; }
.resize-handle { fill: var(--accent); cursor: nwse-resize; }

.handle { cursor: crosshair; }
.handle circle { fill: var(--accent); stroke: var(--surface); stroke-width: 2; }
.handle path { stroke: #fff; stroke-width: 1.8; stroke-linecap: round; }

/* mind map */
.node.depth0 .node-shape { fill: var(--text); stroke: var(--text); }
.node.depth0 .node-text { fill: var(--surface); font-weight: 700; font-size: 16px; }
.node.depth1 .node-shape { fill: var(--bc); stroke: var(--bc); }
.node.depth1 .node-text { fill: #fff; font-weight: 600; }
.node.depthn .node-shape { fill: color-mix(in srgb, var(--bc) 14%, var(--surface)); stroke: var(--bc); }
.node.depth0.selected .node-shape, .node.depth1.selected .node-shape, .node.depthn.selected .node-shape { stroke: var(--accent); stroke-width: 3; }

/* edges */
.edge-hit { fill: none; stroke: transparent; stroke-width: 16; cursor: pointer; }
.edge-line { fill: none; stroke: var(--edge); }
.edge.dotted .edge-line { stroke-dasharray: 6 5; }
.edge.selected .edge-line { stroke: var(--accent); }
.arrow-head { fill: var(--edge); }
.arrow-head.sel { fill: var(--accent); }
.edge-label-bg { fill: var(--bg); stroke: var(--border); stroke-width: 1; }
.edge-label { font-size: 12px; fill: var(--text); pointer-events: none; }
.connect-preview { stroke: var(--accent); stroke-width: 2; stroke-dasharray: 5 4; pointer-events: none; }
.marquee { fill: var(--accent-soft); stroke: var(--accent); stroke-width: 1; pointer-events: none; }

.empty-hint {
    position: absolute; left: 50%; top: 42%; transform: translate(-50%, -50%);
    text-align: center; max-width: 320px; color: var(--muted); pointer-events: none;
    display: flex; flex-direction: column; gap: 6px;
}
.empty-hint strong { font-size: 18px; color: var(--text); }

.board-controls {
    position: absolute; bottom: 14px;
    display: flex; align-items: center; gap: 2px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
    padding: 3px; box-shadow: var(--shadow);
}
.board-controls.left { left: 14px; }
.board-controls.right { right: 14px; }
.board-controls button { border: 0; background: transparent; width: 32px; padding: 5px 0; }
.zoom-readout { min-width: 46px; text-align: center; font-size: 12px; color: var(--muted); }

.inspector {
    position: absolute; top: 12px; left: 50%; transform: translateX(-50%);
    display: flex; align-items: center; gap: 8px;
    background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
    padding: 6px 8px; box-shadow: var(--shadow);
    max-width: calc(100% - 28px);
}
.inspector .tag {
    font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .6px;
    color: var(--muted); padding: 0 4px;
}
.inspector input[type="text"], .inspector textarea { width: 220px; resize: none; line-height: 1.3; }
.inspector select { max-width: 140px; }
.inspector .check { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); font-size: 13px; }

/* ------------------------------------------------------------ mermaid / markdown pane */

.mermaid-pane { display: flex; flex-direction: column; background: var(--surface); }
.pane-head {
    display: flex; align-items: center; gap: 10px;
    padding: 8px 14px; border-bottom: 1px solid var(--border); min-height: 48px;
}
.pane-head .hint { color: var(--muted); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; min-width: 0; }
.pane-head .dirty { color: #b86e00; font-size: 12.5px; font-weight: 600; white-space: nowrap; }

.md-view { flex: 1; overflow: auto; padding: 22px 30px 40px; line-height: 1.6; }
.md-view h1 { font-size: 24px; margin: 0 0 18px; }
.md-view h2 { font-size: 19px; margin: 26px 0 10px; }
.md-view p { margin: 0 0 12px; }
.md-view code { font-family: var(--mono); background: var(--surface-2); border-radius: 4px; padding: 1px 5px; }
.md-view pre { background: var(--surface-2); border-radius: 8px; padding: 12px 14px; overflow: auto; }
.md-view pre.mermaid, .md-view div.mermaid { margin: 8px 0 20px; text-align: center; background: none; padding: 0; overflow: visible; }
.md-view pre.mermaid:not(.rendered):not(.failed) { color: transparent; position: relative; min-height: 140px; }
.md-view pre.mermaid:not(.rendered):not(.failed)::before {
    content: "Rendering diagram…"; position: absolute; inset: 0; display: grid; place-items: center;
    color: var(--muted); font-family: var(--font);
}
.md-view .mermaid svg { max-width: 100%; height: auto; }
.mermaid-error { text-align: left; border: 1px solid var(--danger); border-radius: 8px; padding: 12px 14px; color: var(--danger); }
.mermaid-error pre { color: var(--text); white-space: pre-wrap; margin: 8px 0 0; }

.source {
    flex: 1; width: 100%; resize: none; border: 0; border-radius: 0;
    font-family: var(--mono); font-size: 13px; line-height: 1.55; padding: 16px 18px;
    background: var(--surface); tab-size: 2; white-space: pre; overflow: auto;
}
.source:focus-visible { outline: none; }

/* ------------------------------------------------------------ toast + errors */

.toast {
    position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%);
    background: var(--text); color: var(--surface);
    padding: 10px 18px; border-radius: 10px; box-shadow: var(--shadow);
    max-width: min(560px, calc(100vw - 32px)); z-index: 50; animation: toast-in .16s ease-out;
}
.toast.error { background: var(--danger); color: #fff; }
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

#blazor-error-ui {
    background: #fff3cd; color: #5c4400; bottom: 0; display: none; left: 0; padding: .6rem 1.25rem .7rem;
    position: fixed; width: 100%; z-index: 1000;
}
#blazor-error-ui .dismiss { cursor: pointer; position: absolute; right: .75rem; top: .5rem; }

@media (max-width: 1150px) {
    .topbar .kind-badge { display: none; }
}

@media (max-width: 900px) {
    .palette { width: 220px; }
    .content { flex-direction: column; }
    .content > .mermaid-pane { border-left: 0; border-top: 1px solid var(--border); }
    .topbar .kind-badge, .pane-head .hint { display: none; }
}
