:root {
  --bg: #f0f2f7;
  --surface: #ffffff;
  --surface-2: #f7f8fc;
  --border: #e4e8f0;
  --text-primary: #0f172a;
  --text-secondary: #64748b;
  --text-muted: #94a3b8;
  --accent: #3b5bdb;
  --accent-light: #e8ecfd;
  --accent-hover: #2f4ac7;
  --green: #10b981;
  --green-light: #d1fae5;
  --yellow: #f59e0b;
  --yellow-light: #fef3c7;
  --red: #ef4444;
  --red-light: #fee2e2;
  --blue: #3b82f6;
  --blue-light: #dbeafe;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow: 0 4px 16px rgba(0,0,0,0.07), 0 1px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.09), 0 2px 8px rgba(0,0,0,0.05);
  --radius: 14px;
  --radius-sm: 8px;
  --transition: 0.22s ease;
  --font: 'DM Sans', sans-serif;
  --mono: 'DM Mono', monospace;
}

[data-theme="dark"] {
  --bg: #0a0f1e;
  --surface: #111827;
  --surface-2: #1a2234;
  --border: #1e2a3d;
  --text-primary: #f1f5f9;
  --text-secondary: #94a3b8;
  --text-muted: #475569;
  --accent: #4f6ef7;
  --accent-light: #1a2350;
  --accent-hover: #6179f8;
  --green: #10b981;
  --green-light: #064e3b;
  --yellow: #f59e0b;
  --yellow-light: #451a03;
  --red: #ef4444;
  --red-light: #450a0a;
  --blue: #3b82f6;
  --blue-light: #1e3a5f;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.3);
  --shadow: 0 4px 16px rgba(0,0,0,0.35);
  --shadow-lg: 0 8px 32px rgba(0,0,0,0.4);
}

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

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text-primary);
  min-height: 100vh;
  font-size: 14px;
  line-height: 1.5;
  transition: background var(--transition), color var(--transition);
}

.navbar {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 28px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  box-shadow: var(--shadow-sm);
  transition: background var(--transition), border-color var(--transition);
}

.nav-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 18px;
  font-weight: 700;
  color: var(--text-primary);
  letter-spacing: -0.3px;
}

.nav-brand .logo-icon {
  width: 32px;
  height: 32px;
  background: linear-gradient(135deg, var(--accent), #818cf8);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  color: white;
  font-size: 14px;
}

.nav-brand span { color: var(--accent); }

.nav-center,
.nav-right {
  display: flex;
  align-items: center;
  gap: 12px;
}

.sim-status {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 12px;
  font-weight: 500;
  color: var(--text-secondary);
  padding: 5px 12px;
  background: var(--surface-2);
  border-radius: 20px;
  border: 1px solid var(--border);
}

.status-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  transition: background var(--transition);
}

.status-dot.running { background: var(--green); animation: pulse 1.5s infinite; }
.status-dot.paused { background: var(--yellow); }

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 7px 16px;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  border: none;
  transition: all var(--transition);
  white-space: nowrap;
}

.btn-primary { background: var(--accent); color: white; }
.btn-primary:hover { background: var(--accent-hover); transform: translateY(-1px); box-shadow: 0 4px 12px rgba(59,91,219,0.35); }

.btn-secondary {
  background: var(--surface-2);
  color: var(--text-primary);
  border: 1px solid var(--border);
}

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

.btn-success { background: var(--green); color: white; }
.btn-success:hover { background: #0d9e71; transform: translateY(-1px); box-shadow: 0 4px 12px rgba(16,185,129,0.35); }

.btn-warning { background: var(--yellow); color: white; }
.btn-warning:hover { background: #d97706; }

.btn-ghost {
  background: transparent;
  color: var(--text-secondary);
  border: 1px solid var(--border);
  padding: 7px 12px;
}

.btn-ghost:hover { background: var(--surface-2); color: var(--text-primary); }

.btn:disabled { opacity: 0.5; cursor: not-allowed; transform: none !important; }
.btn:active { transform: scale(0.97) !important; }

.app-body {
  display: grid;
  grid-template-columns: 300px 1fr;
  gap: 20px;
  padding: 24px 28px;
  max-width: 1600px;
  margin: 0 auto;
  min-height: calc(100vh - 60px);
}

.sidebar { display: flex; flex-direction: column; gap: 16px; }

.card {
  background: var(--surface);
  border-radius: var(--radius);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  overflow: hidden;
  transition: background var(--transition), border-color var(--transition), box-shadow var(--transition);
}

.card:hover { box-shadow: var(--shadow-lg); }

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

.card-title {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-primary);
  letter-spacing: 0.1px;
}

.card-title .icon {
  width: 28px;
  height: 28px;
  border-radius: 7px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
}

.icon-blue { background: var(--blue-light); color: var(--blue); }
.icon-green { background: var(--green-light); color: var(--green); }
.icon-accent { background: var(--accent-light); color: var(--accent); }
.icon-yellow { background: var(--yellow-light); color: var(--yellow); }
.icon-red { background: var(--red-light); color: var(--red); }

.card-body { padding: 16px 20px; }

.form-group { margin-bottom: 14px; }
.form-group:last-child { margin-bottom: 0; }

label {
  display: block;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

input[type="number"],
select {
  width: 100%;
  padding: 9px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text-primary);
  font-family: var(--font);
  font-size: 13px;
  transition: border-color var(--transition), box-shadow var(--transition), background var(--transition);
  appearance: none;
}

input[type="number"]:focus,
select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(59,91,219,0.12);
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
  cursor: pointer;
  height: 4px;
}

.range-row { display: flex; align-items: center; gap: 10px; }

.range-val {
  font-size: 12px;
  font-weight: 600;
  color: var(--accent);
  font-family: var(--mono);
  min-width: 24px;
  text-align: right;
}

.demand-group {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 6px;
}

.demand-btn {
  padding: 7px 8px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  background: var(--surface-2);
  color: var(--text-secondary);
  font-family: var(--font);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all var(--transition);
  text-align: center;
}

.demand-btn.active-low { background: var(--green-light); color: var(--green); border-color: var(--green); }
.demand-btn.active-med { background: var(--yellow-light); color: var(--yellow); border-color: var(--yellow); }
.demand-btn.active-high { background: var(--red-light); color: var(--red); border-color: var(--red); }
.demand-btn:hover { border-color: var(--accent); color: var(--accent); }

.algo-select-wrap { position: relative; }

.algo-select-wrap::after {
  content: '\f107';
  font-family: 'Font Awesome 6 Free';
  font-weight: 900;
  position: absolute;
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
  font-size: 11px;
}

select { padding-right: 30px; cursor: pointer; }

.btn-row { display: flex; gap: 8px; margin-top: 4px; }
.btn-row .btn { flex: 1; justify-content: center; }

.main-section { display: flex; flex-direction: column; gap: 16px; }

.metrics-row { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }

.metric-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: all var(--transition);
  position: relative;
  overflow: hidden;
}

.metric-card::before {
  content: '';
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  height: 3px;
}

.metric-card.m-blue::before { background: var(--blue); }
.metric-card.m-green::before { background: var(--green); }
.metric-card.m-yellow::before { background: var(--yellow); }
.metric-card.m-red::before { background: var(--red); }

.metric-card:hover { transform: translateY(-2px); box-shadow: var(--shadow-lg); }

.metric-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.6px;
}

.metric-value {
  font-size: 30px;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1;
  letter-spacing: -1px;
  font-family: var(--mono);
}

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

.charts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.chart-card { position: relative; }
.chart-wrap { position: relative; height: 200px; padding: 4px 0; }

.vm-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 12px;
  padding: 4px 0;
}

.vm-card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  transition: all var(--transition);
  position: relative;
}

.vm-card:hover { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-light); }

.vm-card.vm-high { border-color: rgba(239,68,68,0.4); }
.vm-card.vm-med { border-color: rgba(245,158,11,0.4); }
.vm-card.vm-low { border-color: rgba(16,185,129,0.3); }

.vm-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

.vm-name {
  font-size: 12px;
  font-weight: 700;
  color: var(--text-primary);
  font-family: var(--mono);
}

.vm-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 10px;
  font-family: var(--mono);
}

.badge-green { background: var(--green-light); color: var(--green); }
.badge-yellow { background: var(--yellow-light); color: var(--yellow); }
.badge-red { background: var(--red-light); color: var(--red); }

.vm-metric { margin-bottom: 8px; }
.vm-metric:last-child { margin-bottom: 0; }

.vm-metric-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 4px;
}

.vm-metric-label {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.vm-metric-val {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-secondary);
  font-family: var(--mono);
}

.progress-bar {
  height: 5px;
  background: var(--border);
  border-radius: 3px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.4s ease, background 0.4s ease;
}

.fill-green { background: var(--green); }
.fill-yellow { background: var(--yellow); }
.fill-red { background: var(--red); }

.vm-tasks {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--border);
  font-size: 10px;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  gap: 5px;
}

.task-log {
  max-height: 140px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.task-log::-webkit-scrollbar { width: 4px; }
.task-log::-webkit-scrollbar-track { background: transparent; }
.task-log::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.log-entry {
  font-size: 11px;
  font-family: var(--mono);
  padding: 5px 10px;
  border-radius: 6px;
  background: var(--surface-2);
  color: var(--text-secondary);
  border-left: 3px solid var(--border);
  animation: slideIn 0.2s ease;
}

@keyframes slideIn {
  from { opacity: 0; transform: translateX(-8px); }
  to { opacity: 1; transform: translateX(0); }
}

.log-entry.log-ok { border-color: var(--green); }
.log-entry.log-warn { border-color: var(--yellow); }
.log-entry.log-err { border-color: var(--red); }

.speed-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 14px;
  gap: 10px;
}

.speed-label { font-size: 12px; color: var(--text-secondary); font-weight: 500; white-space: nowrap; }

.dark-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border-radius: 8px;
  background: var(--surface-2);
  border: 1px solid var(--border);
  cursor: pointer;
  color: var(--text-secondary);
  font-size: 14px;
  transition: all var(--transition);
}

.dark-toggle:hover { background: var(--accent-light); color: var(--accent); }

.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-muted);
  font-size: 13px;
}

.empty-state i {
  font-size: 32px;
  margin-bottom: 12px;
  opacity: 0.3;
  display: block;
}

.divider { height: 1px; background: var(--border); margin: 12px 0; }

.tooltip-wrap { position: relative; display: inline-flex; }

.tooltip-text {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text-primary);
  color: var(--surface);
  font-size: 11px;
  padding: 4px 9px;
  border-radius: 5px;
  white-space: nowrap;
  pointer-events: none;
  z-index: 200;
}

.tooltip-wrap:hover .tooltip-text { display: block; }

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 20px;
  font-family: var(--mono);
}

.algo-badge {
  font-size: 10px;
  font-weight: 600;
  padding: 3px 8px;
  border-radius: 5px;
  background: var(--accent-light);
  color: var(--accent);
  font-family: var(--mono);
}

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

.vm-scroll-area { overflow-x: auto; }
.card-full { grid-column: 1 / -1; }

#toast {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  background: var(--text-primary);
  color: var(--surface);
  padding: 10px 18px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transform: translateY(10px);
  transition: all 0.25s ease;
  pointer-events: none;
}

#toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 1100px) {
  .app-body { grid-template-columns: 270px 1fr; padding: 16px; }
  .metrics-row { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 800px) {
  .app-body { grid-template-columns: 1fr; }
  .charts-grid { grid-template-columns: 1fr; }
}