:root {
  --bg: #f4f5f7;
  --surface: #ffffff;
  --surface-2: #f0f1f4;
  --border: #e2e4e9;
  --border-strong: #cdd1d8;
  --text: #1d2129;
  --muted: #6b7280;
  --accent: #3a6df0;
  --accent-soft: rgba(58, 109, 240, 0.12);
  --danger: #d93025;
  --danger-soft: rgba(217, 48, 37, 0.1);
  --dep-in: #0ea5e9;
  --dep-out: #f59e0b;
  --grid-dot: #d0d4db;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-lg: 0 10px 28px rgba(16, 24, 40, 0.16);
  --radius: 8px;
  --font: Roboto, system-ui, -apple-system, "Segoe UI", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
  color-scheme: light;
}

[data-theme="dark"] {
  --bg: #15171c;
  --surface: #1e2127;
  --surface-2: #272a32;
  --border: #2f333b;
  --border-strong: #3e434d;
  --text: #e6e8eb;
  --muted: #9aa1ad;
  --accent: #6b93ff;
  --accent-soft: rgba(107, 147, 255, 0.16);
  --danger: #f28b82;
  --danger-soft: rgba(242, 139, 130, 0.12);
  --grid-dot: #2c3039;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 10px 28px rgba(0, 0, 0, 0.55);
  color-scheme: dark;
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--font);
}

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

#app {
  display: grid;
  grid-template-columns: 240px 1fr auto;
  grid-template-rows: 48px 1fr;
  height: 100vh;
}

/* ---------- Верхняя панель ---------- */

.topbar {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 12px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}

.brand {
  width: 216px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-weight: 700;
  font-size: 15px;
}
.brand-mark { color: var(--accent); font-size: 18px; }

.sheet-name {
  min-width: 240px;
  height: 30px;
  padding: 0 8px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  font-weight: 600;
  font-size: 14px;
}
.sheet-name:hover { border-color: var(--border); }
.sheet-name:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.spacer { flex: 1; }
.status { font-size: 12px; color: var(--muted); }
.status.warn { color: #b45309; }
.status.error { color: var(--danger); }
[data-theme="dark"] .status.warn { color: #f5b041; }

/* ---------- Кнопки ---------- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 30px;
  padding: 0 12px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--danger); }
.btn.danger:hover { background: var(--danger-soft); }
.btn.small { height: 24px; padding: 0 8px; font-size: 12px; }
.btn.icon { width: 30px; padding: 0; }

.icon-btn {
  display: inline-grid;
  place-items: center;
  width: 22px;
  height: 22px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 13px;
  flex: none;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }

/* ---------- Боковая панель ---------- */

.sidebar {
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
  padding: 6px 0;
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 12px 6px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.list { list-style: none; margin: 0; padding: 0 6px 8px; }

.list-item {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 4px 6px 4px 8px;
  border-radius: 6px;
  cursor: pointer;
}
.list-item:hover { background: var(--surface-2); }
.list-item.active { background: var(--accent-soft); color: var(--accent); font-weight: 500; }

.item-icon { color: var(--muted); flex: none; }
.list-item.active .item-icon { color: var(--accent); }
.item-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.item-actions { display: flex; gap: 2px; opacity: 0; }
.list-item:hover .item-actions, .list-item:focus-within .item-actions { opacity: 1; }
.list .hint { padding: 4px 8px; }

.count { font-size: 11px; color: var(--muted); min-width: 16px; text-align: right; }

.plain-input {
  flex: 1;
  min-width: 0;
  padding: 2px 4px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: transparent;
}
.plain-input:hover { border-color: var(--border); }
.plain-input:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.swatch {
  flex: none;
  width: 14px;
  height: 14px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: none;
  cursor: pointer;
  overflow: hidden;
}
.swatch::-webkit-color-swatch-wrapper { padding: 0; }
.swatch::-webkit-color-swatch { border: none; border-radius: 50%; }
.swatch::-moz-color-swatch { border: none; border-radius: 50%; }

.cat-dot { flex: none; width: 10px; height: 10px; border-radius: 50%; background: var(--cat); }

/* ---------- Меню ---------- */

.menu-wrap { position: relative; }
.menu {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  min-width: 200px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  text-transform: none;
  letter-spacing: normal;
  font-size: 13px;
  font-weight: 400;
  color: var(--text);
}
.menu.right { left: auto; right: 0; }
.menu-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 0;
  border-radius: 4px;
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.menu-item:hover { background: var(--surface-2); }
.menu-note { padding: 7px 10px; color: var(--muted); }
.menu-sep { height: 1px; margin: 4px 0; background: var(--border); }

/* ---------- Холст ---------- */

.viewport {
  position: relative;
  overflow: hidden;
  background-color: var(--bg);
  background-image: radial-gradient(circle, var(--grid-dot) 1px, transparent 1.2px);
  background-size: var(--grid, 20px) var(--grid, 20px);
  cursor: grab;
  touch-action: none;
}
.viewport.panning { cursor: grabbing; }
.world { position: absolute; left: 0; top: 0; transform-origin: 0 0; }

.canvas-toolbar {
  position: absolute;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 4px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}
.canvas-toolbar .btn { border-color: transparent; }
.canvas-toolbar .btn.primary { border-color: var(--accent); }
.top-left { left: 12px; top: 12px; }
.bottom-left { left: 12px; bottom: 12px; }
.zoom-label { min-width: 42px; text-align: center; font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }

.empty-state {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  color: var(--muted);
  text-align: center;
  pointer-events: none;
}

/* ---------- Блоки ---------- */

.block {
  position: absolute;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: default;
}
.block::before {
  content: "";
  position: absolute;
  top: -1px;
  bottom: -1px;
  left: -1px;
  width: 4px;
  border-radius: var(--radius) 0 0 var(--radius);
  background: var(--cat);
}
.block.selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), var(--shadow-lg); z-index: 5; }
.block.dragging { z-index: 10; box-shadow: var(--shadow-lg); opacity: 0.94; }

.block-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 7px 6px 7px 14px;
  border-bottom: 1px solid var(--border);
  cursor: move;
  user-select: none;
}
.block-title { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-weight: 600; }
.block-cat {
  flex: none;
  max-width: 45%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 1px 7px;
  border-radius: 10px;
  font-size: 11px;
  color: var(--cat);
  background: color-mix(in srgb, var(--cat) 14%, transparent);
}
.block-body { padding: 4px 8px 8px 12px; }
.block-empty { padding: 10px 4px 4px; color: var(--muted); font-size: 12px; }
.block-resize { position: absolute; top: 0; right: -3px; bottom: 0; width: 7px; cursor: ew-resize; }

/* ---------- Поля ---------- */

.field {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(64px, 42%) 12px;
  align-items: center;
  gap: 6px;
  min-height: 28px;
  padding: 2px 4px;
  border-radius: 4px;
}
.field-label { min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
.field.type-checkbox .field-label { color: var(--muted); font-size: 12px; }
.field.type-textarea { grid-template-columns: minmax(0, 1fr) 12px; }
.field.type-textarea textarea { grid-column: 1 / -1; grid-row: 2; }
.field.type-checkbox input { justify-self: end; margin: 0 4px; accent-color: var(--accent); }

.field input[type="text"], .field textarea {
  width: 100%;
  min-width: 0;
  padding: 3px 6px;
  border: 1px solid transparent;
  border-radius: 4px;
  background: var(--surface-2);
}
.field textarea { resize: vertical; min-height: 54px; }
.field.type-number input { text-align: right; font-variant-numeric: tabular-nums; }
.field input:focus, .field textarea:focus { outline: none; border-color: var(--accent); background: var(--surface); }

.field.computed input, .field.computed textarea { background: transparent; font-weight: 600; cursor: default; }
.field.computed input:focus { border-color: transparent; }
.fx { color: var(--accent); font: italic 13px Georgia, serif; text-align: center; }

.field.has-error input, .field.has-error textarea { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }
.field.has-error .fx { color: var(--danger); }

.field.selected { background: var(--accent-soft); }
.field.dep-in { background: color-mix(in srgb, var(--dep-in) 13%, transparent); box-shadow: inset 2px 0 0 var(--dep-in); }
.field.dep-out { background: color-mix(in srgb, var(--dep-out) 15%, transparent); box-shadow: inset 2px 0 0 var(--dep-out); }

/* ---------- Инспектор ---------- */

.inspector {
  width: 320px;
  overflow-y: auto;
  background: var(--surface);
  border-left: 1px solid var(--border);
}
.insp-head {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 12px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.insp-head .title { flex: 1; }
.insp-body { display: flex; flex-direction: column; gap: 14px; padding: 14px 16px 24px; }
.fieldset > label {
  display: block;
  margin-bottom: 5px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: var(--muted);
}
.input {
  width: 100%;
  height: 32px;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: 6px;
  background: var(--surface);
}
textarea.input { height: auto; min-height: 72px; padding: 6px 8px; resize: vertical; }
.input:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.mono { font-family: var(--mono); font-size: 12px; }

.hint { margin-top: 5px; font-size: 12px; color: var(--muted); }
.hint.error { color: var(--danger); }
.error-box { padding: 8px 10px; border-radius: 6px; background: var(--danger-soft); color: var(--danger); font-size: 12px; }
.ok-box { padding: 8px 10px; border-radius: 6px; background: var(--surface-2); font-size: 12px; }

code, .ref { font-family: var(--mono); font-size: 12px; }
.ref {
  display: inline-block;
  margin: 2px 4px 2px 0;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  cursor: pointer;
}
.ref.in { box-shadow: inset 2px 0 0 var(--dep-in); }
.ref.out { box-shadow: inset 2px 0 0 var(--dep-out); }

.insp-section-title { font-size: 11px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); }
.insp-fields { list-style: none; margin: -6px 0 0; padding: 0; }
.insp-fields .list-item { padding-left: 6px; }
.insp-fields code { color: var(--muted); }
.insp-actions { display: flex; flex-wrap: wrap; gap: 6px; }

.help { font-size: 12px; color: var(--muted); }
.help summary { cursor: pointer; color: var(--text); }
.help ul { margin: 6px 0 0; padding-left: 18px; }
.help li { margin: 3px 0; }

/* ---------- Уведомления ---------- */

.toast {
  position: fixed;
  right: 16px;
  bottom: 16px;
  z-index: 100;
  max-width: 420px;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--text);
  color: var(--bg);
  box-shadow: var(--shadow-lg);
}
.toast.error { background: var(--danger); color: #fff; }
.toast ul { margin: 6px 0 0; padding-left: 18px; font-size: 12px; }
