* { box-sizing: border-box; }
[hidden] { display: none !important; }

:root {
    --bg: #f1f5f9;
    --panel: #ffffff;
    --border: #e2e8f0;
    --text: #1e293b;
    --muted: #64748b;
    --primary: #2563eb;
    --primary-dark: #1d4ed8;
    --danger: #dc2626;
    --accent: #0ea5e9;
}

html, body {
    margin: 0;
    height: 100%;
    font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
    color: var(--text);
    background: var(--bg);
    font-size: 14px;
}

/* Topbar */
.topbar {
    height: 52px;
    background: #0f172a;
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 18px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 600; font-size: 16px; }
.logo { font-size: 20px; }
.status { font-size: 12px; color: #94a3b8; min-height: 16px; }
.status.ok { color: #4ade80; }
.status.err { color: #f87171; }

/* Layout */
.layout { display: flex; height: calc(100vh - 52px); }

/* Sidebar */
.sidebar {
    width: 280px;
    background: var(--panel);
    border-right: 1px solid var(--border);
    display: flex;
    flex-direction: column;
    overflow: hidden;
}
.sidebar-section { padding: 12px 14px; border-bottom: 1px solid var(--border); }
.flows-section { flex: 1; overflow-y: auto; }
.sidebar-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 8px; }
.sidebar-head h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 0; }
.head-btns { display: flex; gap: 4px; }

.btn-icon {
    width: 24px; height: 24px;
    border: 1px solid var(--border);
    background: #fff; border-radius: 6px;
    cursor: pointer; font-size: 15px; line-height: 1;
    color: var(--muted);
}
.btn-icon:hover:not(:disabled) { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-icon:disabled { opacity: .4; cursor: default; }

.module-list, .flow-list { list-style: none; margin: 0; padding: 0; }
.module-list li, .flow-list li {
    padding: 8px 10px; border-radius: 7px; cursor: pointer;
    display: flex; align-items: center; justify-content: space-between; gap: 6px;
}
.module-list li:hover, .flow-list li:hover { background: #f1f5f9; }
.module-list li.active, .flow-list li.active { background: #dbeafe; color: var(--primary-dark); font-weight: 600; }
.item-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; }
.badge { background: #e2e8f0; color: var(--muted); border-radius: 10px; padding: 1px 7px; font-size: 11px; }
.row-actions { display: none; gap: 2px; }
.module-list li:hover .row-actions, .flow-list li:hover .row-actions { display: flex; }
.row-actions button {
    border: none; background: none; cursor: pointer; color: var(--muted);
    font-size: 12px; padding: 2px 4px; border-radius: 4px;
}
.row-actions button:hover { background: #cbd5e1; color: var(--text); }

/* Main */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.empty-state { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); }

.editor-wrap { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.editor-toolbar {
    display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
    padding: 10px 14px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.flow-name-input {
    font-size: 15px; font-weight: 600; border: 1px solid transparent;
    padding: 5px 8px; border-radius: 6px; min-width: 180px;
}
.flow-name-input:hover, .flow-name-input:focus { border-color: var(--border); outline: none; }
.sep { width: 1px; height: 22px; background: var(--border); margin: 0 4px; }

.tool {
    border: 1px solid var(--border); background: #fff; color: var(--text);
    padding: 6px 10px; border-radius: 6px; cursor: pointer; font-size: 13px;
}
.tool:hover { background: #f1f5f9; }
.tool.primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.tool.primary:hover { background: var(--primary-dark); }
.tool.active { background: var(--accent); color: #fff; border-color: var(--accent); }

.editor-body { flex: 1; display: flex; overflow: hidden; }
.canvas-holder { flex: 1; overflow: auto; background:
    linear-gradient(90deg, #e9eef5 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(#e9eef5 1px, transparent 1px) 0 0 / 24px 24px,
    #f8fafc;
}
#canvas { width: 2000px; height: 1400px; display: block; }

/* Nodes */
.node { cursor: grab; }
.node:active { cursor: grabbing; }
.node .shape { fill: #fff; stroke: #475569; stroke-width: 1.5; }
.node.type-start .shape { fill: #dcfce7; stroke: #16a34a; }
.node.type-process .shape { fill: #dbeafe; stroke: #2563eb; }
.node.type-decision .shape { fill: #fef9c3; stroke: #ca8a04; }
.node.type-io .shape { fill: #f3e8ff; stroke: #9333ea; }
.node.selected .shape { stroke-width: 3; stroke-dasharray: 4 2; }
.node text { font-size: 13px; fill: #1e293b; pointer-events: none; user-select: none; }

.edge-path { stroke: #475569; stroke-width: 2; fill: none; }
.edge-hit { stroke: transparent; stroke-width: 14; fill: none; cursor: pointer; }
.edge-label-bg { fill: #f8fafc; }
.edge-label { font-size: 12px; fill: #334155; }
.edge.selected .edge-path { stroke: var(--accent); stroke-width: 3; }

/* Attachments panel */
.attach-panel { width: 230px; border-left: 1px solid var(--border); background: var(--panel); padding: 14px; overflow-y: auto; }
.attach-panel h3 { margin: 0 0 10px; font-size: 13px; }
.attach-list { list-style: none; margin: 0 0 12px; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.attach-list li { display: flex; align-items: center; gap: 6px; font-size: 12px; background: #f1f5f9; padding: 6px 8px; border-radius: 6px; }
.attach-list a { color: var(--primary); text-decoration: none; flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attach-list button { border: none; background: none; color: var(--danger); cursor: pointer; font-size: 14px; }
.upload-btn { display: inline-block; border: 1px dashed var(--border); border-radius: 6px; padding: 8px 12px; cursor: pointer; color: var(--primary); text-align: center; width: 100%; }
.upload-btn:hover { background: #f1f5f9; }
.hint { color: var(--muted); font-size: 11px; margin: 8px 0 0; }
.editor-hint { padding: 6px 14px; font-size: 12px; color: var(--muted); background: var(--panel); border-top: 1px solid var(--border); }

/* Inline label editor (replaces native prompt) */
.node-edit-input {
    position: absolute;
    z-index: 30;
    font: inherit;
    font-size: 13px;
    text-align: center;
    padding: 4px 6px;
    border: 2px solid var(--accent);
    border-radius: 6px;
    background: #fff;
    outline: none;
    box-shadow: 0 4px 14px rgba(0,0,0,.18);
}

/* Modal */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(15,23,42,.5);
    display: flex; align-items: center; justify-content: center; z-index: 100;
}
.modal { background: #fff; border-radius: 10px; width: 380px; max-width: 90vw; overflow: hidden; box-shadow: 0 20px 40px rgba(0,0,0,.2); }
.modal-head { padding: 14px 18px; font-weight: 600; border-bottom: 1px solid var(--border); }
.modal-body { padding: 18px; display: flex; flex-direction: column; gap: 12px; }
.modal-body label { font-size: 12px; color: var(--muted); display: block; margin-bottom: 4px; }
.modal-body input, .modal-body textarea {
    width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit;
}
.modal-foot { padding: 12px 18px; display: flex; justify-content: flex-end; gap: 8px; border-top: 1px solid var(--border); }
.modal-wide { width: 860px; max-width: 94vw; }
.modal-head { display: flex; align-items: center; justify-content: space-between; }
.modal-x { border: none; background: none; font-size: 22px; line-height: 1; cursor: pointer; color: var(--muted); }
.modal-x:hover { color: var(--text); }
.modal-body label.chk { display: flex; align-items: center; gap: 8px; flex-direction: row; }
.modal-body select { width: 100%; padding: 8px 10px; border: 1px solid var(--border); border-radius: 6px; font: inherit; }

/* hide edit-only controls for viewers */
.hide-edit { display: none !important; }

/* Auth pages */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; background: #0f172a; }
.auth-card { background: #fff; width: 360px; max-width: 92vw; padding: 28px; border-radius: 12px; box-shadow: 0 20px 50px rgba(0,0,0,.35); display: flex; flex-direction: column; gap: 12px; }
.auth-card h1 { font-size: 20px; margin: 0; }
.auth-sub { margin: 0 0 6px; color: var(--muted); font-size: 13px; }
.auth-card label { font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 4px; }
.auth-card input { padding: 9px 11px; border: 1px solid var(--border); border-radius: 7px; font: inherit; }
.auth-btn { margin-top: 6px; padding: 10px; }
.auth-err { color: var(--danger); font-size: 13px; min-height: 16px; }

/* Topbar identity */
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.user-chip { display: flex; align-items: center; gap: 8px; color: #e2e8f0; font-size: 13px; }
.role-badge { text-transform: capitalize; font-size: 11px; padding: 2px 8px; border-radius: 10px; background: #334155; color: #e2e8f0; }
.role-badge.role-admin  { background: #7c3aed; }
.role-badge.role-editor { background: #2563eb; }
.role-badge.role-viewer { background: #475569; }
.topbar .tool { padding: 5px 10px; font-size: 13px; }

/* Data tables (history, users) */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th { text-align: left; color: var(--muted); font-size: 11px; text-transform: uppercase; letter-spacing: .04em; padding: 6px 8px; border-bottom: 1px solid var(--border); }
.data-table td { padding: 8px; border-bottom: 1px solid #f1f5f9; }
.data-table .vno { font-weight: 600; }
.data-table .muted { color: var(--muted); }
.vrow-actions { display: flex; gap: 6px; }
.vrow-actions button { border: 1px solid var(--border); background: #fff; border-radius: 6px; padding: 3px 9px; cursor: pointer; font-size: 12px; }
.vrow-actions button:hover { background: #f1f5f9; }
.users-head { margin-bottom: 12px; }
.ok-dot { color: #16a34a; }

/* Version viewer + compare */
.viewer-toolbar { display: flex; align-items: center; gap: 16px; margin-bottom: 10px; }
.cmp-label { font-size: 13px; color: var(--muted); }
.cmp-label select { width: auto; display: inline-block; }
.cmp-legend { display: flex; gap: 12px; font-size: 12px; }
.cmp-legend .lg::before { content: '●'; margin-right: 4px; }
.lg-added { color: #16a34a; }
.lg-removed { color: #dc2626; }
.lg-changed { color: #d97706; }
.viewer-canvas { height: 460px; overflow: auto; border: 1px solid var(--border); border-radius: 8px; background:
    linear-gradient(90deg, #eef2f7 1px, transparent 1px) 0 0 / 24px 24px,
    linear-gradient(#eef2f7 1px, transparent 1px) 0 0 / 24px 24px, #f8fafc; }
#viewerSvg { width: 1400px; height: 1000px; display: block; }

/* Diff highlight states */
.node.hl-added .shape   { stroke: #16a34a !important; stroke-width: 3 !important; }
.node.hl-removed .shape { stroke: #dc2626 !important; stroke-width: 2.5 !important; stroke-dasharray: 5 3; }
.node.hl-removed        { opacity: .55; }
.node.hl-changed .shape { stroke: #d97706 !important; stroke-width: 3 !important; }
.edge.hl-added .edge-path   { stroke: #16a34a; stroke-width: 3; }
.edge.hl-removed .edge-path { stroke: #dc2626; stroke-width: 2.5; stroke-dasharray: 6 4; opacity: .7; }
.edge.hl-changed .edge-path { stroke: #d97706; stroke-width: 3; }
