:root {
  --bg: #f8fafc;
  --surface: #ffffff;
  --surface-alt: #f1f5f9;
  --border: #e2e8f0;
  --text: #0f172a;
  --text-muted: #475569;
  --primary: #2563eb;
  --primary-light: #eff6ff;
  --success: #059669;
  --success-bg: #ecfdf5;
  --danger: #e11d48;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.06);
  --shadow-md: 0 4px 12px rgba(15, 23, 42, 0.08);
  --radius: 12px;
  --font-sans: "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Topbar */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
  z-index: 20;
  box-shadow: var(--shadow-sm);
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  display: grid;
  place-items: center;
  font-size: 20px;
  color: #fff;
  box-shadow: 0 2px 6px rgba(37, 99, 235, 0.3);
}

.brand h1 {
  font-size: 16px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.subtitle {
  font-size: 12px;
  color: var(--text-muted);
}

.tabs {
  display: flex;
  background: var(--surface-alt);
  padding: 4px;
  border-radius: 10px;
  gap: 4px;
}

.tab-btn {
  border: none;
  background: transparent;
  padding: 7px 14px;
  border-radius: 7px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
  cursor: pointer;
  transition: all 0.15s ease;
}

.tab-btn:hover {
  color: var(--text);
}

.tab-btn.active {
  background: var(--surface);
  color: var(--primary);
  box-shadow: var(--shadow-sm);
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.uniqueness-badge {
  display: flex;
  align-items: center;
  gap: 7px;
  background: var(--success-bg);
  color: var(--success);
  border: 1px solid #a7f3d0;
  padding: 5px 11px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
}

.uniqueness-badge.error {
  background: #fff1f2;
  color: var(--danger);
  border-color: #fecdd3;
}

.uniqueness-badge .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: currentColor;
}

.btn-outline {
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 12px;
  border-radius: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}

.btn-outline:hover {
  background: var(--surface-alt);
}

/* Main Workspace */
.workspace {
  flex: 1;
  display: flex;
  min-height: 0;
  position: relative;
}

.canvas-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
  position: relative;
  background: radial-gradient(#cbd5e1 1px, transparent 1px);
  background-size: 22px 22px;
  background-color: #f8fafc;
}

.canvas-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 16px;
  gap: 12px;
  background: rgba(255, 255, 255, 0.92);
  backdrop-filter: blur(6px);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 10;
}

.toolbar-left, .toolbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
  width: 310px;
  max-width: 100%;
}

.search-icon {
  position: absolute;
  left: 10px;
  font-size: 13px;
  pointer-events: none;
}

.search-box input {
  width: 100%;
  padding: 7px 30px 7px 32px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 13px;
  outline: none;
  background: var(--surface);
}

.search-box input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
}

.clear-btn {
  position: absolute;
  right: 8px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
}

.toggle-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  padding: 6px 11px;
  border-radius: 8px;
  cursor: pointer;
  user-select: none;
}

.icon-btn {
  width: 30px;
  height: 30px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 7px;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  display: grid;
  place-items: center;
}

.icon-btn:hover, .btn-small:hover {
  background: var(--surface-alt);
}

.btn-small {
  height: 30px;
  padding: 0 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 7px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
}

.zoom-label {
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  min-width: 44px;
  text-align: center;
}

/* Search Results Dropdown */
.search-results {
  position: absolute;
  top: 56px;
  left: 16px;
  width: 440px;
  max-width: calc(100% - 32px);
  max-height: 360px;
  overflow-y: auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow-md);
  z-index: 30;
  padding: 6px;
}

.search-item {
  padding: 9px 12px;
  border-radius: 7px;
  cursor: pointer;
  border-bottom: 1px solid #f1f5f9;
}

.search-item:last-child {
  border-bottom: none;
}

.search-item:hover {
  background: var(--primary-light);
}

.search-item-title {
  font-size: 12.5px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 3px;
}

.search-item-eq {
  font-size: 13px;
  font-family: "JetBrains Mono", "SFMono-Regular", Consolas, monospace;
  color: var(--text);
}

/* SVG Viewport */
.svg-viewport {
  flex: 1;
  position: relative;
  overflow: hidden;
  cursor: grab;
  user-select: none;
}

.svg-viewport:active {
  cursor: grabbing;
}

#diagramSvg {
  width: 100%;
  height: 100%;
  display: block;
}

/* SVG Interactive Elements */
.svg-node {
  cursor: pointer;
  transition: filter 0.15s ease;
}

.svg-node:hover {
  filter: drop-shadow(0 6px 14px rgba(15, 23, 42, 0.16));
}

.svg-link-group {
  cursor: pointer;
}

.svg-link-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 18px;
}

.svg-link-path {
  fill: none;
  stroke-width: 2.4px;
  transition: stroke-width 0.15s, opacity 0.15s;
}

.svg-link-group:hover .svg-link-path,
.svg-link-group.active .svg-link-path {
  stroke-width: 4px;
}

.svg-link-group.dimmed {
  opacity: 0.18;
}

.svg-node.dimmed {
  opacity: 0.25;
}

.svg-badge-bg {
  transition: transform 0.15s;
}

.svg-link-group:hover .svg-badge-bg,
.svg-link-group.active .svg-badge-bg {
  stroke-width: 2.5px;
}

/* Legend */
.legend-bar {
  background: rgba(255, 255, 255, 0.94);
  border-top: 1px solid var(--border);
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
}

.legend-item {
  display: flex;
  align-items: center;
  gap: 6px;
  font-weight: 600;
  color: var(--text);
}

.legend-item i {
  width: 11px;
  height: 11px;
  border-radius: 3px;
  display: inline-block;
}

.legend-hint {
  margin-left: auto;
  color: var(--primary);
}

/* Inspector Pane */
.inspector-pane {
  width: 410px;
  max-width: 42vw;
  background: var(--surface);
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 15;
}

.inspector-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  background: #fcfdff;
}

.inspector-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--primary-light);
  color: var(--primary);
  margin-bottom: 6px;
}

.inspector-header h2 {
  font-size: 17px;
  font-weight: 700;
  line-height: 1.35;
}

.inspector-body {
  flex: 1;
  overflow-y: auto;
  padding: 18px 20px;
}

.empty-state {
  color: var(--text-muted);
  font-size: 14px;
  line-height: 1.6;
}

.empty-state ul {
  margin-top: 10px;
  padding-left: 18px;
}

.empty-state li {
  margin-bottom: 8px;
}

.rule-box {
  background: var(--surface-alt);
  border-left: 4px solid var(--primary);
  padding: 12px 14px;
  border-radius: 0 8px 8px 0;
  font-size: 13.5px;
  line-height: 1.55;
  color: var(--text);
  margin-bottom: 16px;
}

.eq-section-title {
  font-size: 12.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.eq-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-sm);
}

.eq-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 6px;
}

.eq-num {
  font-size: 11.5px;
  font-weight: 700;
  color: var(--primary);
  background: var(--primary-light);
  padding: 2px 8px;
  border-radius: 999px;
}

.eq-cond {
  font-size: 11.5px;
  color: #b45309;
  background: #fef3c7;
  padding: 2px 8px;
  border-radius: 999px;
  font-weight: 600;
}

.eq-formula {
  font-size: 15.5px;
  font-weight: 600;
  color: var(--text);
  line-height: 1.5;
  padding: 4px 0;
  word-break: break-word;
}

.eq-note {
  font-size: 12.5px;
  color: var(--text-muted);
  margin-top: 4px;
  line-height: 1.45;
}

.connected-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.connected-btn {
  text-align: left;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 10px 12px;
  cursor: pointer;
  transition: all 0.15s;
}

.connected-btn:hover {
  border-color: var(--primary);
  background: var(--primary-light);
}

.connected-btn-title {
  font-size: 13.5px;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 4px;
}

.connected-btn-sub {
  font-size: 12.5px;
  color: var(--text-muted);
}

/* Summary Table Pane */
.table-pane {
  flex: 1;
  overflow-y: auto;
  padding: 24px 32px;
  background: var(--bg);
}

.table-header-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 16px;
  margin-bottom: 20px;
  flex-wrap: wrap;
}

.table-header-bar h2 {
  font-size: 20px;
  font-weight: 700;
}

.table-header-bar p {
  font-size: 13.5px;
  color: var(--text-muted);
  margin-top: 4px;
}

.table-filters select {
  padding: 8px 12px;
  border-radius: 8px;
  border: 1px solid var(--border);
  font-size: 13.5px;
  font-weight: 600;
  background: var(--surface);
}

.summary-group-title {
  font-size: 16px;
  font-weight: 700;
  color: var(--primary);
  margin: 22px 0 10px;
  padding-bottom: 6px;
  border-bottom: 2px solid #dbeafe;
}

.summary-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border-radius: 10px;
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  margin-bottom: 24px;
}

.summary-table th, .summary-table td {
  padding: 11px 14px;
  border: 1px solid var(--border);
  text-align: left;
  vertical-align: top;
  font-size: 13.5px;
}

.summary-table th {
  background: var(--surface-alt);
  font-weight: 700;
  color: var(--text);
}

.summary-eq-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.summary-eq-item {
  font-weight: 600;
  color: var(--text);
}

.summary-eq-cond {
  font-size: 12px;
  color: #b45309;
  font-weight: 500;
  margin-left: 6px;
}

@media (max-width: 960px) {
  .workspace {
    flex-direction: column;
  }
  .inspector-pane {
    width: 100%;
    max-width: 100%;
    height: 42vh;
    border-left: none;
    border-top: 1px solid var(--border);
  }
}

@media print {
  body {
    height: auto;
    overflow: visible;
    background: #fff;
  }
  .topbar-actions, .canvas-toolbar, .tabs, .legend-bar {
    display: none !important;
  }
  .table-pane {
    display: block !important;
    padding: 0;
  }
  .canvas-pane, .inspector-pane {
    display: none !important;
  }
}
