:root {
  --bg: #f3f4f6;
  --panel: #ffffff;
  --border: #d1d5db;
  --text: #111827;
  --muted: #6b7280;
  --accent: #1d4ed8;
  --select: #7c3aed;
  --select-bg: #ede9fe;
  --danger: #b91c1c;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { display: flex; flex-direction: column; background: var(--bg); }
button, select, input, textarea { font: inherit; }
button {
  border: 1px solid var(--border); background: #fff; border-radius: 4px;
  padding: 3px 10px; cursor: pointer;
}
button:hover:not(:disabled) { background: #f9fafb; border-color: #9ca3af; }
button:disabled { opacity: .45; cursor: default; }
button.primary { background: var(--accent); color: #fff; border-color: var(--accent); }
button.primary:hover:not(:disabled) { background: #1e40af; }
button.danger { color: var(--danger); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.spacer { flex: 1; }
.sep { width: 1px; align-self: stretch; background: var(--border); margin: 0 4px; }

.toolbar {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 6px 10px; background: var(--panel); border-bottom: 1px solid var(--border);
}
.brand { margin-right: 8px; color: var(--accent); letter-spacing: .05em; }
.system-pick { display: flex; align-items: center; gap: 6px; }
.system-pick select { min-width: 220px; max-width: 360px; padding: 2px 4px; }

.panes {
  flex: 1; min-height: 0; display: grid;
  grid-template-columns: minmax(220px, 25%) 1fr minmax(240px, 25%);
  gap: 6px; padding: 6px;
}
.pane {
  background: var(--panel); border: 1px solid var(--border); border-radius: 6px;
  display: flex; flex-direction: column; min-width: 0; min-height: 0;
}
.pane-head {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  padding: 6px 8px; border-bottom: 1px solid var(--border);
}
.pane-head h2 { font-size: 13px; margin: 0; text-transform: uppercase; color: var(--muted); }
.tree-actions { display: flex; flex-wrap: wrap; gap: 4px; margin-left: auto; }
.tree-actions button { padding: 1px 6px; font-size: 12px; }

.tree { overflow: auto; flex: 1; padding: 4px 0; outline: none; }
.tree ul { list-style: none; margin: 0; padding-left: 14px; }
.tree > ul { padding-left: 4px; }
.tree .row {
  display: flex; align-items: center; gap: 4px; padding: 1px 6px; cursor: pointer;
  white-space: nowrap; border-radius: 3px;
}
.tree .row:hover { background: #f3f4f6; }
.tree .row.selected { background: var(--select-bg); outline: 1px solid var(--select); }
.tree .twisty { width: 12px; text-align: center; color: var(--muted); flex: none; }
.tree .badge {
  font-size: 10px; padding: 0 4px; border-radius: 3px; color: #fff; flex: none;
}
.badge.system { background: #1d4ed8; }
.badge.context { background: #047857; }
.badge.device { background: #b45309; }
.badge.component { background: #4b5563; }
.badge.concern { background: #9ca3af; }
.badge.concern.hard { background: #b91c1c; }
.tree .meta { color: var(--muted); font-size: 12px; }

.center-pane { overflow: hidden; }
.tabs { display: flex; gap: 2px; padding: 4px 6px 0; border-bottom: 1px solid var(--border); }
.tab { border-radius: 4px 4px 0 0; border-bottom: none; background: #f9fafb; }
.tab.active { background: #fff; font-weight: 600; position: relative; top: 1px; }
.tab-body { flex: 1; min-height: 0; display: flex; flex-direction: column; overflow: auto; }
#tab-layers3d { overflow: hidden; }
.zoom-bar { display: flex; align-items: center; gap: 4px; padding: 4px 6px; flex-wrap: wrap; }
.zoom-bar button { white-space: nowrap; }
.legend { display: flex; align-items: center; gap: 4px; color: var(--muted); font-size: 12px; }
.dot { display: inline-block; width: 12px; height: 12px; border-radius: 50%; border: 1.5px solid #6b7280; }
.dot.hard { background: #fca5a5; border-color: #b91c1c; }
.dot.soft { background: #fff; border-style: dashed; margin-left: 6px; }
.layers-scroll { flex: 1; overflow: auto; background: #fff; border-top: 1px solid var(--border); }
.layers-scroll svg { display: block; user-select: none; touch-action: none; }
.layers-scroll .hit { cursor: pointer; }
.layers-scroll .hit[data-kind="system"] { cursor: default; }
.layers3d { flex: 1; min-height: 0; overflow: hidden; background: #fff; border-top: 1px solid var(--border); }
.layers3d canvas { display: block; touch-action: none; cursor: grab; }
.hint3d { font-size: 12px; padding: 0 8px 4px; }
.check { display: flex; align-items: center; gap: 3px; margin-left: 6px; font-size: 13px; }
.tree .row.drop-ok { background: #ede9fe; outline: 2px dashed var(--select); }
.tree .row[draggable=true] { cursor: grab; }

.props { overflow: auto; flex: 1; padding: 8px; }
.props label { display: block; font-size: 12px; color: var(--muted); margin: 8px 0 2px; }
.props input[type=text], .props input[type=number], .props textarea, .props select {
  width: 100%; padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px;
}
.props textarea { min-height: 70px; resize: vertical; }
.props .invalid { border-color: var(--danger); background: #fef2f2; }
.props .kind-line { font-size: 12px; color: var(--muted); margin-bottom: 4px; }
.props .row2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.props table { width: 100%; border-collapse: collapse; margin-top: 4px; }
.props td, .props th { padding: 2px; font-size: 12px; text-align: left; }
.props td input { width: 100%; padding: 2px 4px; border: 1px solid var(--border); border-radius: 3px; }
.props .actions { margin-top: 12px; display: flex; gap: 6px; flex-wrap: wrap; }

.behaviour { padding: 10px; display: grid; gap: 16px; }
.behaviour h3 { margin: 0 0 4px; font-size: 14px; }
.grid-table { border-collapse: collapse; width: 100%; }
.grid-table th, .grid-table td { border: 1px solid var(--border); padding: 3px 5px; text-align: left; }
.grid-table th { background: #f9fafb; font-size: 12px; }
.grid-table input, .grid-table select { width: 100%; border: 1px solid transparent; padding: 2px; background: transparent; }
.grid-table input:focus, .grid-table select:focus { border-color: var(--accent); background: #fff; }
.matrix td.cell { text-align: center; cursor: pointer; min-width: 64px; user-select: none; }
.matrix td.cell.off { background: #f3f4f6; color: var(--muted); }
.matrix td.cell.override { background: #dbeafe; font-weight: 600; }
.matrix td.owner { color: var(--muted); font-size: 12px; }
.matrix .hint { color: var(--muted); font-size: 12px; margin: 0 0 6px; }

.status {
  padding: 4px 10px; background: var(--panel); border-top: 1px solid var(--border);
  font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.status.error { color: var(--danger); background: #fef2f2; }
.status.dirty::before { content: "● "; color: #d97706; }

dialog { width: min(1000px, 92vw); max-height: 85vh; padding: 0; border: 1px solid var(--border); border-radius: 6px; }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 12px; border-bottom: 1px solid var(--border); }
.dialog-head h2 { margin: 0; font-size: 16px; }
.log-body { overflow: auto; max-height: calc(85vh - 50px); padding: 8px 12px; }
.log-body td { font-size: 12px; vertical-align: top; }

/* --- accounts: user box, read-only, login and user pages --- */
.ro-badge { display: none; padding: 1px 8px; border-radius: 10px; background: #fef3c7; color: #92400e; font-size: 12px; font-weight: 600; }
.read-only .ro-badge { display: inline-block; }
.read-only .matrix td.cell { cursor: default; pointer-events: none; }
.user-box { display: inline-flex; align-items: center; gap: 6px; }
.user-box[hidden], [hidden] { display: none !important; }
.button-link {
  border: 1px solid var(--border); background: #fff; border-radius: 4px; padding: 3px 10px;
  color: var(--text); text-decoration: none;
}
.button-link:hover { background: #f9fafb; border-color: #9ca3af; }
.small-dialog { width: min(420px, 92vw); }
.stack { display: flex; flex-direction: column; gap: 10px; padding: 16px; }
.stack h1, .stack h2 { margin: 0 0 4px; font-size: 18px; }
.stack label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.stack input, .stack select { padding: 6px 8px; border: 1px solid var(--border); border-radius: 4px; color: var(--text); }
.stack input:focus, .stack select:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.row-end { display: flex; justify-content: flex-end; gap: 8px; }
.error-text { color: var(--danger); margin: 0; min-height: 1.2em; font-size: 13px; }
.login-page { align-items: center; justify-content: center; padding: 16px; }
.login-card { width: min(360px, 100%); background: var(--panel); border: 1px solid var(--border); border-radius: 8px; }
.login-card .brand { font-size: 20px; margin: 0; }
.login-card button { padding: 6px 10px; }
.page { flex: 1; overflow: auto; padding: 12px; }
.card { background: var(--panel); border: 1px solid var(--border); border-radius: 6px; padding: 12px; margin-bottom: 12px; max-width: 1000px; }
.card h2 { margin: 0 0 10px; font-size: 15px; }
.inline-form { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 8px; }
.inline-form label { display: flex; flex-direction: column; gap: 3px; font-size: 12px; color: var(--muted); }
.inline-form input, .inline-form select { padding: 4px 6px; border: 1px solid var(--border); border-radius: 4px; }
.users-table { border-collapse: collapse; width: 100%; }
.users-table th, .users-table td { border-bottom: 1px solid var(--border); padding: 6px; text-align: left; vertical-align: middle; }
.users-table th { font-size: 12px; color: var(--muted); font-weight: 600; }
.users-table td.actions { white-space: nowrap; text-align: right; }
.users-table td.actions button { margin-left: 4px; }
.users-table tr.disabled td:not(.actions) { color: var(--muted); text-decoration: line-through; }
.users-table .you { color: var(--accent); font-size: 12px; margin-left: 4px; }
.table-scroll { overflow-x: auto; }
.secret { display: flex; gap: 8px; align-items: center; }
.secret code { flex: 1; padding: 6px 8px; background: #f3f4f6; border-radius: 4px; font-size: 15px; word-break: break-all; user-select: all; }
.roles-help { margin: 8px 0 0; padding-left: 18px; color: var(--muted); font-size: 13px; }

@media (max-width: 900px) {
  .panes { grid-template-columns: 1fr; grid-auto-rows: minmax(300px, auto); overflow: auto; }
}
