:root {
  --bg: #0b0f17;
  --bg-elevated: #111827;
  --bg-card: #141c2b;
  --bg-input: #0e1420;
  --border: #232f46;
  --border-strong: #324363;
  --text: #e6edf7;
  --text-muted: #8fa1bc;
  --accent: #6366f1;
  --accent-hover: #7c7fff;
  --accent-soft: rgba(99, 102, 241, 0.14);
  --success: #22c55e;
  --success-soft: rgba(34, 197, 94, 0.14);
  --warning: #f59e0b;
  --warning-soft: rgba(245, 158, 11, 0.15);
  --danger: #ef4444;
  --danger-soft: rgba(239, 68, 68, 0.14);
  --radius: 12px;
  --radius-sm: 8px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

a { color: #93a8ff; text-decoration: none; }
a:hover { text-decoration: underline; }

code {
  font-family: var(--mono);
  background: rgba(255, 255, 255, 0.06);
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 12.5px;
}

.app-shell { display: flex; min-height: 100vh; }

.sidebar {
  width: 260px;
  flex-shrink: 0;
  background: var(--bg-elevated);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  padding: 20px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 4px 8px 20px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}

.brand-icon {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  background: linear-gradient(135deg, #6366f1, #8b5cf6);
  display: grid;
  place-items: center;
  font-size: 20px;
  box-shadow: 0 6px 20px rgba(99, 102, 241, 0.35);
}

.brand-title { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 12px; color: var(--text-muted); }

.nav { display: flex; flex-direction: column; gap: 6px; flex: 1; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  font-size: 14px;
  font-weight: 500;
  text-align: left;
  transition: all 0.15s ease;
}
.nav-item:hover { background: rgba(255, 255, 255, 0.04); color: var(--text); }
.nav-item.active {
  background: var(--accent-soft);
  color: #c7d2fe;
  border-color: rgba(99, 102, 241, 0.35);
}
.nav-icon { font-size: 16px; }

.badge {
  margin-left: auto;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text-muted);
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 999px;
}
.badge.has-online { background: var(--success-soft); color: var(--success); }

.sidebar-footer { padding-top: 14px; border-top: 1px solid var(--border); }

.quick-tip {
  background: rgba(255, 255, 255, 0.025);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  font-size: 12px;
  color: var(--text-muted);
}
.quick-tip-title { color: var(--text); font-weight: 600; margin-bottom: 6px; }
.quick-tip ol { margin: 0; padding-left: 16px; }
.quick-tip li { margin-bottom: 4px; }

.main { flex: 1; min-width: 0; padding: 24px 28px 56px; max-width: 1440px; }

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 22px;
  flex-wrap: wrap;
}
.topbar h1 { margin: 0; font-size: 22px; font-weight: 700; }
.topbar p { margin: 3px 0 0; color: var(--text-muted); font-size: 13px; }
.topbar-actions { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }

.profile-pill {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg-card);
  border: 1px solid var(--border);
  padding: 6px 10px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--text-muted);
}
.profile-pill select {
  background: transparent;
  border: none;
  color: var(--text);
  font-weight: 600;
  padding: 2px 4px;
  min-width: 160px;
}

.tab-panel { display: none; flex-direction: column; gap: 18px; }
.tab-panel.active { display: flex; }

.card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
}
.launch-hero {
  border-color: rgba(99, 102, 241, 0.35);
  background: linear-gradient(180deg, rgba(99, 102, 241, 0.06), var(--bg-card) 42%);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 14px;
  flex-wrap: wrap;
}
.card-header h2 { margin: 0; font-size: 15.5px; font-weight: 600; }

.muted { color: var(--text-muted); font-size: 12.5px; }
.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.stack-col { display: flex; flex-direction: column; gap: 18px; }
.inline-controls { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }

.section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 4px;
  flex-wrap: wrap;
}
.section-header h2 { margin: 0; font-size: 16px; }

/* OAuth Panel & Accounts */
.oauth-panel {
  background: var(--bg-input);
  border: 1px solid rgba(99, 102, 241, 0.35);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  margin-bottom: 14px;
}
.oauth-steps { display: flex; flex-direction: column; gap: 14px; }
.oauth-step { display: flex; gap: 12px; align-items: flex-start; }
.step-num {
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: var(--accent);
  color: #fff;
  font-weight: 700;
  font-size: 12px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
  margin-top: 2px;
}
.step-body { flex: 1; }
.step-title { font-weight: 600; margin-bottom: 4px; }
.oauth-finish-row { display: grid; grid-template-columns: 2fr 1.2fr auto; gap: 8px; margin-top: 6px; }

.accounts-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 12px;
}
.account-card {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.account-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.account-email { font-weight: 700; font-size: 13.5px; word-break: break-all; }
.account-stats {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 12px;
  color: var(--text-muted);
}
.account-stats strong { color: var(--text); }

/* Launch Form */
.launch-form {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}
.launch-options-row {
  grid-column: 1 / -1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  flex-wrap: wrap;
  padding-top: 4px;
}
.btn-launch { padding: 10px 20px; font-size: 14px; }

/* Live Job Terminal */
.live-job-box {
  margin-top: 16px;
  background: #080c14;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  overflow: hidden;
}
.job-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 14px;
  background: rgba(255, 255, 255, 0.03);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
}
.job-title-wrap { display: flex; align-items: center; gap: 10px; }

.drive-auth-banner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  background: var(--warning-soft);
  border-bottom: 1px solid rgba(245, 158, 11, 0.35);
  font-size: 13px;
  flex-wrap: wrap;
}

.terminal-log {
  margin: 0;
  padding: 12px 14px;
  max-height: 320px;
  overflow-y: auto;
  font-family: var(--mono);
  font-size: 12px;
  line-height: 1.55;
  color: #cbd5e1;
  white-space: pre-wrap;
  word-break: break-word;
}

/* Cloud VMs & Instances */
.vms-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(340px, 1fr));
  gap: 12px;
}
.vm-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 16px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.vm-card-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }

.instances-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(380px, 1fr));
  gap: 16px;
}
.instance-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.instance-card.online { border-color: rgba(34, 197, 94, 0.38); }
.inst-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 10px; }
.inst-title { font-weight: 700; font-size: 15px; display: flex; align-items: center; gap: 7px; flex-wrap: wrap; }
.inst-url { font-family: var(--mono); font-size: 12px; color: var(--text-muted); word-break: break-all; margin-top: 3px; }
.inst-actions { display: flex; flex-wrap: wrap; gap: 7px; }

.metrics-box {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  font-size: 12.5px;
}
.metric-row { display: flex; justify-content: space-between; gap: 8px; }
.progress-track { width: 100%; height: 6px; background: rgba(255, 255, 255, 0.07); border-radius: 999px; overflow: hidden; }
.progress-fill { height: 100%; background: linear-gradient(90deg, #6366f1, #22c55e); border-radius: 999px; transition: width 0.3s ease; }

/* Forms */
.form-stack { display: flex; flex-direction: column; gap: 12px; }
.form-grid { display: grid; grid-template-columns: 1fr 1.6fr 1fr auto; gap: 12px; }
.form-row-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.form-row-4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12.5px; color: var(--text-muted); font-weight: 500; }
.field-align-end { justify-content: flex-end; }

input[type="text"], input[type="url"], input[type="password"], input[type="number"], select, textarea {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 8px 11px;
  font-size: 13.5px;
  font-family: inherit;
  outline: none;
  width: 100%;
}
textarea { resize: vertical; font-family: var(--mono); font-size: 12.5px; }
input:focus, select:focus, textarea:focus { border-color: var(--accent); }

.toggle-inline { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 13px; user-select: none; }

/* Buttons & Badges */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 8px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 600;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
  color: var(--text);
  text-decoration: none !important;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--accent-hover); }
.btn-secondary { background: rgba(255, 255, 255, 0.07); border-color: var(--border-strong); }
.btn-secondary:hover:not(:disabled) { background: rgba(255, 255, 255, 0.12); }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-muted); }
.btn-ghost:hover:not(:disabled) { color: var(--text); border-color: var(--border-strong); background: rgba(255, 255, 255, 0.04); }
.btn-danger-ghost { background: transparent; border-color: rgba(239, 68, 68, 0.35); color: #fca5a5; }
.btn-danger-ghost:hover:not(:disabled) { background: var(--danger-soft); border-color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-block { width: 100%; }

.status-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
}
.status-pill::before { content: ""; width: 7px; height: 7px; border-radius: 50%; }
.status-pill.online { background: var(--success-soft); color: var(--success); }
.status-pill.online::before { background: var(--success); box-shadow: 0 0 8px var(--success); }
.status-pill.offline { background: rgba(148, 163, 184, 0.12); color: var(--text-muted); }
.status-pill.offline::before { background: #64748b; }
.status-pill.checking { background: var(--warning-soft); color: var(--warning); }
.status-pill.checking::before { background: var(--warning); }
.status-pill.failed { background: var(--danger-soft); color: #fca5a5; }
.status-pill.failed::before { background: var(--danger); }

.tag {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 7px;
  border-radius: 5px;
  background: var(--accent-soft);
  color: #c7d2fe;
}
.tag.gpu { background: rgba(34, 197, 94, 0.14); color: #86efac; }

/* Builder & Lists */
.builder-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.builder-toolbar-left, .builder-toolbar-right { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.builder-toolbar select { width: auto; min-width: 220px; }

.chips, .preset-bar { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 4px; }
.preset-bar { margin-bottom: 12px; }
.chip {
  background: rgba(255, 255, 255, 0.04);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.chip:hover { border-color: var(--accent); color: #c7d2fe; background: var(--accent-soft); }

.add-row { display: grid; grid-template-columns: 1fr 2fr auto; gap: 8px; margin-bottom: 12px; }
.model-add-row { grid-template-columns: 1fr 160px 1.1fr 2fr auto; }
.item-list { display: flex; flex-direction: column; gap: 8px; }
.item-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
}
.item-row.disabled { opacity: 0.55; }
.item-main { display: flex; align-items: center; gap: 10px; min-width: 0; flex: 1; }
.item-info { min-width: 0; }
.item-name { font-weight: 600; font-size: 13.5px; display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.item-sub { font-family: var(--mono); font-size: 11.5px; color: var(--text-muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 700px; }

/* Export & Studio */
.export-hero, .studio-bar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.export-actions, .studio-bar-left { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.studio-bar select { width: auto; min-width: 240px; }
.view-switch { display: flex; gap: 8px; }
.cells-preview { display: flex; flex-direction: column; gap: 12px; }
.cell-card, .code-card { background: var(--bg-card); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.cell-head { display: flex; justify-content: space-between; align-items: center; padding: 8px 14px; background: rgba(255, 255, 255, 0.03); border-bottom: 1px solid var(--border); font-size: 12.5px; color: var(--text-muted); }
.cell-card pre, .code-card pre { margin: 0; padding: 14px 16px; overflow-x: auto; font-family: var(--mono); font-size: 12.5px; color: #dbeafe; background: #0a0f1a; }

.mini-tabs { display: flex; gap: 4px; background: var(--bg-input); padding: 3px; border-radius: var(--radius-sm); border: 1px solid var(--border); }
.mini-tab { border: none; background: transparent; color: var(--text-muted); padding: 5px 10px; border-radius: 6px; font-size: 12px; font-weight: 600; cursor: pointer; }
.mini-tab.active { background: var(--accent); color: #fff; }

.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.gallery-item { background: var(--bg-input); border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.gallery-item img { width: 100%; display: block; aspect-ratio: 1 / 1; object-fit: cover; }
.gallery-cap { padding: 8px 10px; display: flex; justify-content: space-between; font-size: 12px; color: var(--text-muted); }

.remote-models-view { display: flex; flex-direction: column; gap: 12px; max-height: 360px; overflow-y: auto; }
.model-group-title { font-size: 12px; font-weight: 700; color: #c7d2fe; margin-bottom: 6px; }
.model-pill-list { display: flex; flex-wrap: wrap; gap: 6px; }
.model-pill { background: var(--bg-input); border: 1px solid var(--border); padding: 3px 8px; border-radius: 6px; font-family: var(--mono); font-size: 11.5px; }

.empty-state {
  text-align: center;
  padding: 24px 16px;
  color: var(--text-muted);
  border: 1px dashed var(--border);
  border-radius: var(--radius-sm);
  grid-column: 1 / -1;
}

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.72);
  display: grid;
  place-items: center;
  z-index: 9000;
  padding: 20px;
}
.modal-card {
  width: 100%;
  max-width: 780px;
  background: var(--bg-card);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.6);
}

/* Toast */
.toast-container { position: fixed; right: 20px; bottom: 20px; display: flex; flex-direction: column; gap: 8px; z-index: 9999; }
.toast {
  background: #1e293b;
  border: 1px solid var(--border-strong);
  color: var(--text);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  box-shadow: 0 10px 25px rgba(0, 0, 0, 0.4);
}
.toast.success { border-left: 3px solid var(--success); }
.toast.error { border-left: 3px solid var(--danger); }
.toast.info { border-left: 3px solid var(--accent); }

@media (max-width: 1024px) {
  .app-shell { flex-direction: column; }
  .sidebar { width: 100%; height: auto; position: static; }
  .launch-form, .grid-2, .oauth-finish-row, .model-add-row, .add-row, .form-grid {
    grid-template-columns: 1fr;
  }
  .form-row-4 { grid-template-columns: repeat(2, 1fr); }
}
