:root {
  --primary: #1a3a5c;
  --primary-light: #2563a8;
  --primary-dark: #0f2340;
  --accent: #e8a020;
  --sidebar-width: 260px;
  --header-height: 56px;
  --bg: #f0f2f5;
  --card-bg: #ffffff;
  --border: #dee2e6;
  --text: #212529;
  --text-muted: #6c757d;
  --sparte-strom: #f0a500;
  --sparte-gas: #0d7a3e;
  --sparte-wasser: #0d6efd;
  --sparte-waerme: #dc3545;
  --radius: 8px;
}

* { box-sizing: border-box; }

body {
  font-family: 'Segoe UI', system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
  margin: 0;
  font-size: 14px;
  line-height: 1.5;
}

/* ===== LOGIN ===== */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, var(--primary-dark) 0%, var(--primary) 60%, var(--primary-light) 100%);
}

.login-card {
  background: var(--card-bg);
  border-radius: 12px;
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}

.login-logo {
  text-align: center;
  margin-bottom: 2rem;
}

.login-logo .logo-icon {
  width: 64px;
  height: 64px;
  background: var(--primary);
  border-radius: 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 28px;
  margin-bottom: 0.75rem;
}

.login-logo h1 {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--primary);
  margin: 0;
}

.login-logo small {
  color: var(--text-muted);
  font-size: 0.8rem;
}

/* ===== APP SHELL ===== */
#app-shell { display: none; }
#app-shell.active { display: flex; flex-direction: column; min-height: 100vh; }

/* Header */
.app-header {
  height: var(--header-height);
  background: var(--primary);
  color: white;
  display: flex;
  align-items: center;
  padding: 0 1.25rem;
  gap: 1rem;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  box-shadow: 0 2px 8px rgba(0,0,0,0.2);
}

.app-header .brand {
  font-size: 1.1rem;
  font-weight: 700;
  letter-spacing: 0.02em;
  white-space: nowrap;
}

.app-header .header-spacer { flex: 1; }

.user-info {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-size: 0.85rem;
  opacity: 0.9;
}

.user-avatar {
  width: 32px;
  height: 32px;
  background: var(--accent);
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 700;
  font-size: 0.8rem;
  color: var(--primary);
}

/* Sidebar */
.app-body {
  display: flex;
  padding-top: var(--header-height);
  min-height: 100vh;
}

.sidebar {
  width: var(--sidebar-width);
  background: var(--primary-dark);
  color: #cdd3da;
  position: fixed;
  top: var(--header-height);
  left: 0;
  bottom: 0;
  overflow-y: auto;
  z-index: 900;
  padding: 1rem 0;
}

.sidebar-section {
  padding: 0.25rem 0;
  margin-bottom: 0.25rem;
}

.sidebar-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: #8899aa;
  padding: 0.5rem 1.25rem 0.25rem;
}

.nav-link {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  padding: 0.6rem 1.25rem;
  color: #b0bec5;
  text-decoration: none;
  border-radius: 0;
  transition: all 0.15s;
  cursor: pointer;
  border: none;
  background: none;
  width: 100%;
  font-size: 0.875rem;
}

.nav-link:hover { background: rgba(255,255,255,0.07); color: white; }
.nav-link.active { background: var(--primary-light); color: white; border-left: 3px solid var(--accent); }
.nav-link i { width: 18px; text-align: center; opacity: 0.8; }

/* Main Content */
.main-content {
  margin-left: var(--sidebar-width);
  flex: 1;
  padding: 1.5rem;
  min-height: calc(100vh - var(--header-height));
}

/* Pages */
.page { display: none; }
.page.active { display: block; }

/* Page Header */
.page-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 1.5rem;
}

.page-title {
  font-size: 1.4rem;
  font-weight: 700;
  color: var(--primary);
  margin: 0;
}

/* Cards */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
  margin-bottom: 1rem;
}

.card-header {
  padding: 0.875rem 1.25rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-weight: 600;
}

.card-body { padding: 1.25rem; }

/* Stats Cards */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 1rem;
  margin-bottom: 1.5rem;
}

.stat-card {
  background: var(--card-bg);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  padding: 1.25rem;
  display: flex;
  align-items: center;
  gap: 1rem;
  box-shadow: 0 1px 4px rgba(0,0,0,0.06);
}

.stat-icon {
  width: 48px;
  height: 48px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.3rem;
  flex-shrink: 0;
}

.stat-icon.blue { background: #dbeafe; color: var(--primary-light); }
.stat-icon.yellow { background: #fef3c7; color: var(--accent); }
.stat-icon.green { background: #d1fae5; color: #059669; }
.stat-icon.red { background: #fee2e2; color: #dc2626; }

.stat-value { font-size: 1.8rem; font-weight: 700; color: var(--primary); line-height: 1; }
.stat-label { font-size: 0.8rem; color: var(--text-muted); margin-top: 2px; }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th {
  background: #f8f9fa;
  font-weight: 600;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
  padding: 0.65rem 1rem;
  border-bottom: 2px solid var(--border);
  text-align: left;
  white-space: nowrap;
}
td {
  padding: 0.75rem 1rem;
  border-bottom: 1px solid #f0f0f0;
  vertical-align: middle;
}
tr:last-child td { border-bottom: none; }
tr:hover td { background: #fafbfc; }

/* Badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  padding: 0.25rem 0.6rem;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  white-space: nowrap;
}

.badge-strom { background: #fef3c7; color: #92400e; }
.badge-gas { background: #d1fae5; color: #065f46; }
.badge-wasser { background: #dbeafe; color: #1e40af; }
.badge-waerme { background: #fee2e2; color: #991b1b; }
.badge-slp { background: #ede9fe; color: #5b21b6; }
.badge-rlm { background: #fce7f3; color: #9d174d; }
.badge-imsys { background: #e0f2fe; color: #0369a1; }
.badge-einspeiser { background: #f0fdf4; color: #14532d; }
.badge-aktiv { background: #d1fae5; color: #065f46; }
.badge-entwurf { background: #fef3c7; color: #92400e; }
.badge-inaktiv { background: #f1f5f9; color: #475569; }
.badge-admin { background: var(--primary); color: white; }
.badge-editor { background: #dbeafe; color: #1e40af; }
.badge-viewer { background: #f1f5f9; color: #475569; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.5rem 1rem;
  border-radius: 6px;
  font-size: 0.875rem;
  font-weight: 500;
  border: 1px solid transparent;
  cursor: pointer;
  text-decoration: none;
  transition: all 0.15s;
  white-space: nowrap;
}

.btn-primary { background: var(--primary-light); color: white; border-color: var(--primary-light); }
.btn-primary:hover { background: var(--primary); border-color: var(--primary); }
.btn-secondary { background: white; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: #f8f9fa; }
.btn-danger { background: #dc3545; color: white; border-color: #dc3545; }
.btn-danger:hover { background: #b02a37; }
.btn-sm { padding: 0.3rem 0.65rem; font-size: 0.8rem; }
.btn-icon { padding: 0.35rem 0.5rem; }

/* Filter Bar */
.filter-bar {
  display: flex;
  gap: 0.75rem;
  align-items: center;
  flex-wrap: wrap;
  margin-bottom: 1rem;
  background: var(--card-bg);
  padding: 0.875rem 1.25rem;
  border-radius: var(--radius);
  border: 1px solid var(--border);
}

.filter-bar input,
.filter-bar select {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.4rem 0.75rem;
  font-size: 0.875rem;
  background: #f8f9fa;
  color: var(--text);
  outline: none;
  transition: border-color 0.15s;
}

.filter-bar input:focus,
.filter-bar select:focus { border-color: var(--primary-light); background: white; }
.filter-bar input { min-width: 200px; }

/* Form Controls */
.form-group { margin-bottom: 1rem; }
.form-label { display: block; font-weight: 600; font-size: 0.85rem; margin-bottom: 0.35rem; color: var(--text); }
.form-control {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 0.5rem 0.75rem;
  font-size: 0.875rem;
  color: var(--text);
  background: white;
  transition: border-color 0.15s, box-shadow 0.15s;
  outline: none;
}
.form-control:focus { border-color: var(--primary-light); box-shadow: 0 0 0 3px rgba(37,99,168,0.1); }
textarea.form-control { resize: vertical; min-height: 80px; }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 2000;
  display: none;
  align-items: center;
  justify-content: center;
  padding: 1rem;
}
.modal-overlay.active { display: flex; }

.modal-box {
  background: white;
  border-radius: 12px;
  width: 100%;
  max-width: 560px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 25px 80px rgba(0,0,0,0.3);
  animation: modalIn 0.2s ease;
}

.modal-box.modal-lg { max-width: 900px; }
.modal-box.modal-xl { max-width: 1200px; }

@keyframes modalIn {
  from { transform: translateY(-20px) scale(0.97); opacity: 0; }
  to { transform: none; opacity: 1; }
}

.modal-header {
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title { font-size: 1.1rem; font-weight: 700; color: var(--primary); margin: 0; }
.modal-close {
  background: none;
  border: none;
  font-size: 1.3rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 0.25rem;
  border-radius: 4px;
  line-height: 1;
}
.modal-close:hover { background: #f0f0f0; color: var(--text); }
.modal-body { padding: 1.5rem; }

/* Fullscreen modal */
.modal-fullscreen {
  width: 100vw !important;
  max-width: 100vw !important;
  height: 100vh !important;
  max-height: 100vh !important;
  border-radius: 0 !important;
  margin: 0;
  display: flex;
  flex-direction: column;
}
/* Editor Toolbar */
.editor-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 .5rem;
  height: 44px;
  background: #f1f5f9;
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
.editor-toolbar-tabs { padding: 0; background: none; }
.editor-toolbar-actions { display: flex; align-items: center; gap: .25rem; }
.toolbar-btn {
  display: inline-flex; align-items: center; gap: .35rem;
  padding: .3rem .65rem;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: white;
  color: var(--text);
  font-size: .82rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background .15s;
}
.toolbar-btn:hover { background: #e8edf3; }
.toolbar-btn-primary { background: var(--primary); color: white; border-color: var(--primary); }
.toolbar-btn-primary:hover { background: #1a4a80; }
.toolbar-sep { width: 1px; height: 22px; background: var(--border); margin: 0 .15rem; }

.modal-fullscreen .modal-body {
  flex: 1;
  overflow: hidden;
  padding: 0;
  display: flex;
  flex-direction: column;
}
.modal-fullscreen .diagram-workspace {
  flex: 1;
  height: auto !important;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 0.75rem;
}

/* Diagram Editor */
.diagram-editor {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 1rem;
  height: 500px;
}

.editor-pane, .preview-pane {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
  display: flex;
  flex-direction: column;
}

.pane-header {
  background: #f8f9fa;
  padding: 0.5rem 0.875rem;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
  font-size: 0.8rem;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.editor-textarea {
  flex: 1;
  width: 100%;
  border: none;
  padding: 0.875rem;
  font-family: 'Cascadia Code', 'Fira Code', 'Courier New', monospace;
  font-size: 0.8rem;
  line-height: 1.6;
  resize: none;
  outline: none;
  background: #1e2127;
  color: #abb2bf;
  tab-size: 2;
}

.preview-pane .preview-inner {
  flex: 1;
  overflow: auto;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background: white;
}

.preview-inner svg { max-width: 100%; max-height: 100%; }

/* ===== VISUAL DIAGRAM EDITOR ===== */
.diagram-workspace {
  display: grid;
  grid-template-columns: 130px 1fr 180px;
  gap: 0;
  height: 520px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.shape-palette {
  background: #f8f9fa;
  border-right: 1px solid var(--border);
  padding: 0.5rem;
  overflow-y: auto;
  min-height: 0;       /* Grid-Item darf schrumpfen → Scroll aktiviert */
  height: 100%;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  box-sizing: border-box;
}

.palette-label {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.4rem 0.25rem 0.1rem;
}

.shape-btn {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.5rem;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: white;
  cursor: pointer;
  font-size: 0.75rem;
  color: var(--text);
  transition: all 0.15s;
  text-align: left;
  width: 100%;
}
.shape-btn:hover { background: #e8f0fe; border-color: #2563a8; color: #2563a8; }
.shape-btn.active { background: #2563a8; color: white; border-color: #2563a8; }
.shape-btn .shape-icon { font-size: 1rem; flex-shrink: 0; }

.diagram-canvas-wrap {
  background: white;
  overflow: hidden;
  position: relative;
}

.diagram-canvas-wrap .toolbar-strip {
  position: absolute;
  top: 8px;
  left: 8px;
  z-index: 10;
  display: flex;
  gap: 0.35rem;
}

.properties-panel {
  background: #f8f9fa;
  border-left: 1px solid var(--border);
  overflow-y: auto;
  padding: 0.5rem;
}

.prop-section {
  font-size: 0.65rem;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  padding: 0.5rem 0 0.2rem;
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
}
.prop-section:first-child { border-top: none; margin-top: 0; }

.prop-row {
  margin-bottom: 0.45rem;
}
.prop-row label {
  display: block;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 0.15rem;
}
.prop-row input[type="text"],
.prop-row input[type="number"],
.prop-row select {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.25rem 0.4rem;
  font-size: 0.78rem;
  background: white;
  outline: none;
}
.prop-row input[type="color"] {
  width: 100%;
  height: 28px;
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 1px;
  cursor: pointer;
  background: white;
}
.prop-hint {
  font-size: 0.75rem;
  color: var(--text-muted);
  text-align: center;
  padding: 1rem 0.5rem;
  line-height: 1.6;
}

/* Detail View */
.detail-grid {
  display: grid;
  grid-template-columns: 200px 1fr;
  gap: 0.5rem 1rem;
  margin-bottom: 1rem;
}
.detail-label { font-weight: 600; font-size: 0.8rem; color: var(--text-muted); text-transform: uppercase; padding-top: 2px; }
.detail-value { font-size: 0.9rem; }

/* Version Timeline */
.version-list { list-style: none; padding: 0; margin: 0; }
.version-item {
  display: flex;
  gap: 1rem;
  padding: 0.75rem 0;
  border-bottom: 1px solid #f0f0f0;
  align-items: flex-start;
}
.version-item:last-child { border-bottom: none; }
.version-badge {
  background: var(--primary);
  color: white;
  border-radius: 6px;
  padding: 0.2rem 0.5rem;
  font-size: 0.75rem;
  font-weight: 700;
  white-space: nowrap;
}

/* Alert */
.alert {
  padding: 0.75rem 1rem;
  border-radius: 6px;
  font-size: 0.875rem;
  margin-bottom: 1rem;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}
.alert-danger { background: #fee2e2; color: #991b1b; border: 1px solid #fecaca; }
.alert-success { background: #d1fae5; color: #065f46; border: 1px solid #a7f3d0; }

/* Toast */
.toast-container {
  position: fixed;
  bottom: 1.5rem;
  right: 1.5rem;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.toast {
  background: #1e293b;
  color: white;
  padding: 0.75rem 1.25rem;
  border-radius: 8px;
  font-size: 0.875rem;
  box-shadow: 0 8px 24px rgba(0,0,0,0.2);
  display: flex;
  align-items: center;
  gap: 0.75rem;
  animation: toastIn 0.3s ease;
  max-width: 360px;
}

.toast.toast-success { border-left: 4px solid #10b981; }
.toast.toast-error { border-left: 4px solid #ef4444; }

@keyframes toastIn {
  from { transform: translateX(100%); opacity: 0; }
  to { transform: none; opacity: 1; }
}

/* Tabs */
.tab-nav {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  margin-bottom: 1.25rem;
}

.tab-btn {
  padding: 0.6rem 1.25rem;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  font-size: 0.875rem;
  font-weight: 500;
  color: var(--text-muted);
  transition: all 0.15s;
}

.tab-btn.active { color: var(--primary-light); border-bottom-color: var(--primary-light); }
.tab-btn:hover:not(.active) { color: var(--text); }

/* Helpers */
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 0.8rem; }
.mt-1 { margin-top: 0.25rem; }
.mt-2 { margin-top: 0.5rem; }
.mb-0 { margin-bottom: 0; }
.d-flex { display: flex; }
.align-center { align-items: center; }
.gap-2 { gap: 0.5rem; }
.gap-3 { gap: 0.75rem; }
.flex-1 { flex: 1; }
.fw-bold { font-weight: 700; }
.empty-state {
  text-align: center;
  padding: 3rem;
  color: var(--text-muted);
}
.empty-state i { font-size: 3rem; opacity: 0.3; margin-bottom: 1rem; display: block; }

/* Responsive */
@media (max-width: 768px) {
  .sidebar { display: none; }
  .main-content { margin-left: 0; }
  .diagram-editor { grid-template-columns: 1fr; height: auto; }
  .editor-pane, .preview-pane { height: 300px; }
}
