/* ── Reset & Base ── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #f5f7fa;
  --surface: #ffffff;
  --border: #e8ecf1;
  --text: #1a2332;
  --text-secondary: #64748b;
  --primary: #3b82f6;
  --primary-hover: #2563eb;
  --green: #22c55e;
  --green-bg: #f0fdf4;
  --yellow: #eab308;
  --yellow-bg: #fefce8;
  --red: #ef4444;
  --red-bg: #fef2f2;
  --gray: #94a3b8;
  --gray-bg: #f8fafc;
  --shadow-sm: 0 1px 3px rgba(0,0,0,.06);
  --shadow-md: 0 4px 12px rgba(0,0,0,.08);
  --radius: 8px;
  --radius-sm: 4px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  --mono: 'SF Mono', 'Fira Code', 'Consolas', monospace;
}

body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

/* ── Layout ── */
.app-header {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 24px;
  height: 56px;
  display: flex;
  align-items: center;
  gap: 24px;
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: var(--shadow-sm);
}

.app-header h1 {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}

.app-header .logo {
  display: flex;
  align-items: center;
  gap: 8px;
}

.app-header .logo-icon {
  width: 24px;
  height: 24px;
  background: var(--primary);
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-weight: 700;
  font-size: 12px;
}

.breadcrumb {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--text-secondary);
}

.breadcrumb a {
  color: var(--primary);
  text-decoration: none;
  cursor: pointer;
}

.breadcrumb a:hover { text-decoration: underline; }
.breadcrumb .sep { color: var(--gray); }

.container {
  max-width: 1280px;
  margin: 0 auto;
  padding: 24px;
}

/* ── Cards ── */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-header h2 {
  font-size: 15px;
  font-weight: 600;
}

.card-body { padding: 0; }
.card-body.padded { padding: 20px; }

/* ── Table ── */
.data-table {
  width: 100%;
  border-collapse: collapse;
}

.data-table th {
  text-align: left;
  padding: 10px 16px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: .5px;
  background: var(--gray-bg);
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.data-table td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

.data-table tr:last-child td { border-bottom: none; }
.data-table tr:hover td { background: #f8fafc; }

.data-table .cell-id {
  font-family: var(--mono);
  font-size: 13px;
  font-weight: 500;
  color: var(--primary);
  cursor: pointer;
}

.data-table .cell-id:hover { text-decoration: underline; }

.data-table .cell-secondary {
  font-size: 12px;
  color: var(--text-secondary);
}

.data-table .cell-mono {
  font-family: var(--mono);
  font-size: 12px;
}

/* ── Status Badge ── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 4px 10px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

.badge::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  flex-shrink: 0;
}

.badge-running { background: var(--green-bg); color: #16a34a; }
.badge-running::before { background: var(--green); }

.badge-ready { background: var(--gray-bg); color: var(--text-secondary); }
.badge-ready::before { background: var(--gray); }

.badge-paused { background: var(--yellow-bg); color: #a16207; }
.badge-paused::before { background: var(--yellow); }

.badge-unreachable, .badge-stopping { background: var(--red-bg); color: var(--red); }
.badge-unreachable::before, .badge-stopping::before { background: var(--red); }

/* ── Source/Target pills ── */
.endpoint-stack {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.endpoint-pill {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 12px;
  color: var(--text-secondary);
}

.endpoint-pill .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--gray);
}

.endpoint-pill.source .dot { background: #8b5cf6; }
.endpoint-pill.target .dot { background: #06b6d4; }

.endpoint-arrow {
  font-size: 10px;
  color: var(--gray);
  margin-left: 13px;
}

.endpoint-arrow-inline {
  font-size: 11px;
  color: var(--gray);
  margin: 0 4px;
}

/* ── Metric Display ── */
.metric {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.metric-value {
  font-size: 13px;
  font-weight: 500;
  font-family: var(--mono);
}

.metric-label {
  font-size: 11px;
  color: var(--text-secondary);
}

.metric-value.green { color: var(--green); }
.metric-value.yellow { color: var(--yellow); }
.metric-value.red { color: var(--red); }

/* ── Buttons ── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  background: var(--surface);
  color: var(--text);
  transition: all .15s;
  white-space: nowrap;
}

.btn:hover { background: var(--gray-bg); border-color: #cbd5e1; }
.btn:disabled { opacity: .5; cursor: not-allowed; }

.btn-primary {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
}

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

.btn-danger {
  color: var(--red);
  border-color: var(--red);
}

.btn-danger:hover { background: var(--red-bg); }

.btn-success {
  color: #16a34a;
  border-color: var(--green);
}

.btn-success:hover { background: var(--green-bg); }

.btn-group {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

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

.action-link {
  font-size: 13px;
  color: var(--primary);
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}

.action-link:hover { text-decoration: underline; }

.action-sep {
  color: var(--border);
  user-select: none;
}

/* ── Tabs (detail view) ── */
.tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
  margin-bottom: 20px;
}

.tab {
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  transition: all .15s;
}

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

/* ── Rules Editor ── */
.rules-table {
  width: 100%;
  border-collapse: collapse;
}

.rules-table th {
  text-align: left;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  background: var(--gray-bg);
  border-bottom: 1px solid var(--border);
}

.rules-table td {
  padding: 6px 12px;
  border-bottom: 1px solid var(--border);
}

.rules-table input {
  width: 100%;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-family: var(--mono);
  outline: none;
  transition: border-color .15s;
}

.rules-table input:focus { border-color: var(--primary); }

.rules-table input[readonly] {
  background: transparent;
  border-color: transparent;
  cursor: default;
  color: var(--text);
}

.rules-table input[readonly]:focus {
  border-color: transparent;
  outline: none;
}

.rules-table .row-actions {
  width: 40px;
  text-align: center;
}

.rules-table .btn-remove {
  color: var(--red);
  cursor: pointer;
  background: none;
  border: none;
  font-size: 16px;
  line-height: 1;
  padding: 4px;
}

.rules-table .btn-remove:hover { background: var(--red-bg); border-radius: 4px; }

/* ── Monitor: Pipeline bar ── */
.pipeline-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
}

.pipeline-card {
  padding: 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--gray-bg);
}

.pipeline-card .pipe-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 12px;
  font-weight: 600;
}

.pipeline-card .pipe-bar {
  height: 6px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}

.pipeline-card .pipe-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width .3s;
}

.pipe-bar-fill.low { background: var(--green); }
.pipe-bar-fill.mid { background: var(--yellow); }
.pipe-bar-fill.high { background: var(--red); }

.pipeline-card .pipe-detail {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 4px;
}

/* ── Monitor: Table group ── */
.rule-group {
  margin-bottom: 16px;
}

.rule-group-header {
  padding: 10px 16px;
  background: var(--gray-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  font-size: 13px;
  font-weight: 600;
  font-family: var(--mono);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.rule-group-header .toggle {
  transition: transform .2s;
  font-size: 12px;
  color: var(--text-secondary);
}

.rule-group-header .toggle.collapsed { transform: rotate(-90deg); }

.rule-group-body {
  border: 1px solid var(--border);
  border-top: none;
  border-radius: 0 0 var(--radius) var(--radius);
  overflow: hidden;
}

/* ── Modal ── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 90%;
  max-width: 700px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  font-size: 15px;
  font-weight: 600;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.modal-header .close-btn {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-secondary);
  cursor: pointer;
  padding: 4px;
  line-height: 1;
}

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

.modal-footer {
  padding: 16px 20px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Diff display */
.diff-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.diff-panel h4 {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: .5px;
}

.diff-panel pre {
  background: var(--gray-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  font-size: 12px;
  font-family: var(--mono);
  line-height: 1.6;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 300px;
  overflow-y: auto;
}

.diff-added { background: #dcfce7; }
.diff-removed { background: #fee2e2; }

/* ── Metric cards row ── */
.metrics-row {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.metric-card {
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
}

.metric-card .mc-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-bottom: 4px;
}

.metric-card .mc-value {
  font-size: 20px;
  font-weight: 600;
  font-family: var(--mono);
  word-break: break-all;
  overflow-wrap: break-word;
}

.metric-card .mc-value-sm {
  font-size: 13px;
  font-weight: 500;
  font-family: var(--mono);
  word-break: break-all;
  overflow-wrap: break-word;
  color: var(--text);
}

.metric-card .mc-sub {
  font-size: 11px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ── Utilities ── */
.hidden { display: none !important; }
.text-muted { color: var(--text-secondary); }
.text-center { text-align: center; }
.mt-16 { margin-top: 16px; }
.mb-16 { margin-bottom: 16px; }
.gap-8 { gap: 8px; }

.empty-state {
  padding: 48px 24px;
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
}

.spinner {
  display: inline-block;
  width: 16px;
  height: 16px;
  border: 2px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ── Toast ── */
.toast-container {
  position: fixed;
  top: 64px;
  right: 24px;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 10px 16px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: slideIn .2s ease-out;
  max-width: 360px;
}

.toast-success { background: var(--green); color: #fff; }
.toast-error { background: var(--red); color: #fff; }

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