:root {
  --bg: #0f0f13;
  --surface: #1a1a24;
  --surface2: #222230;
  --border: #2e2e3e;
  --text: #e8e8f0;
  --text-muted: #7878a0;
  --primary: #7c5cfc;
  --primary-hover: #9176fd;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --info: #3b82f6;
  --radius: 12px;
  --radius-sm: 8px;
}

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

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* Layout */
#app { min-height: 100vh; display: flex; flex-direction: column; }

.page { display: none; flex: 1; flex-direction: column; }
.page.active { display: flex; }

/* Nav */
.nav {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 0 16px;
  padding-top: env(safe-area-inset-top);
  padding-left: max(16px, env(safe-area-inset-left));
  padding-right: max(16px, env(safe-area-inset-right));
  height: calc(56px + env(safe-area-inset-top));
  display: flex;
  align-items: flex-end;
  gap: 12px;
  position: sticky;
  top: 0;
  z-index: 100;
}
.nav-title { font-weight: 700; font-size: 18px; flex: 1; }
.nav-title span { color: var(--primary); }
.nav-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  cursor: pointer;
  padding: 8px;
  border-radius: var(--radius-sm);
  font-size: 20px;
  display: flex;
  align-items: center;
  transition: color 0.15s, background 0.15s;
}
.nav-btn:hover, .nav-btn.active { color: var(--text); background: var(--surface2); }

/* Main content */
.content { flex: 1; padding: 16px; padding-bottom: max(80px, calc(env(safe-area-inset-bottom) + 80px)); max-width: 800px; width: 100%; margin: 0 auto; }

/* Cards */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
}

/* Forms */
.form-group { margin-bottom: 16px; }
label { display: block; font-size: 14px; color: var(--text-muted); margin-bottom: 6px; font-weight: 500; }
input, textarea, select {
  width: 100%;
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  color: var(--text);
  padding: 12px 14px;
  font-size: 16px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
input:focus, textarea:focus, select:focus { border-color: var(--primary); }
textarea { resize: vertical; min-height: 120px; }

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-size: 15px;
  font-weight: 600;
  transition: opacity 0.15s, background 0.15s;
  touch-action: manipulation;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover:not(:disabled) { background: var(--primary-hover); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-ghost { background: var(--surface2); color: var(--text); }
.btn-ghost:hover:not(:disabled) { background: var(--border); }
.btn-block { width: 100%; }
.btn-sm { padding: 8px 14px; font-size: 13px; }

/* Status badges */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  border-radius: 20px;
  font-size: 12px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}
.badge-pending { background: rgba(245,158,11,0.15); color: var(--warning); }
.badge-running { background: rgba(59,130,246,0.15); color: var(--info); }
.badge-completed { background: rgba(34,197,94,0.15); color: var(--success); }
.badge-failed { background: rgba(239,68,68,0.15); color: var(--danger); }
.badge-cancelled { background: rgba(120,120,160,0.15); color: var(--text-muted); }

/* Running pulse */
.badge-running::before {
  content: '';
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--info);
  animation: pulse 1.5s ease infinite;
}
@keyframes pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.5; transform: scale(0.8); }
}

/* Job list */
.job-item {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  display: flex;
  align-items: flex-start;
  gap: 12px;
}
.job-item:hover { border-color: var(--primary); background: var(--surface2); }
.job-item-body { flex: 1; min-width: 0; }
.job-item-prompt {
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  margin-bottom: 6px;
}
.job-item-meta { font-size: 12px; color: var(--text-muted); display: flex; gap: 12px; flex-wrap: wrap; }
.job-drag-handle { color: var(--text-muted); padding: 4px; cursor: grab; font-size: 18px; }

/* Filter tabs */
.filter-tabs {
  display: flex;
  gap: 6px;
  margin-bottom: 16px;
  overflow-x: auto;
  padding-bottom: 4px;
  scrollbar-width: none;
}
.filter-tabs::-webkit-scrollbar { display: none; }
.filter-tab {
  padding: 7px 14px;
  border-radius: 20px;
  border: 1px solid var(--border);
  background: none;
  color: var(--text-muted);
  cursor: pointer;
  font-size: 13px;
  white-space: nowrap;
  transition: all 0.15s;
}
.filter-tab.active { background: var(--primary); border-color: var(--primary); color: #fff; }

/* FAB */
.fab {
  position: fixed;
  bottom: max(24px, calc(env(safe-area-inset-bottom) + 16px));
  right: max(24px, env(safe-area-inset-right));
  width: 56px;
  height: 56px;
  border-radius: 28px;
  background: var(--primary);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 20px rgba(124,92,252,0.4);
  transition: transform 0.15s, box-shadow 0.15s;
  z-index: 200;
  touch-action: manipulation;
}
.fab:hover { transform: scale(1.08); box-shadow: 0 6px 24px rgba(124,92,252,0.5); }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.7);
  z-index: 300;
  display: none;
  align-items: flex-end;
  justify-content: center;
}
.modal-backdrop.open { display: flex; }
.modal {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 20px 16px 32px;
  width: 100%;
  max-width: 600px;
  max-height: 90vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  align-items: center;
  margin-bottom: 20px;
  gap: 12px;
}
.modal-title { font-size: 18px; font-weight: 700; flex: 1; }
.modal-close { background: none; border: none; color: var(--text-muted); cursor: pointer; font-size: 24px; }

/* Folder browser */
.folder-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.1s;
  font-size: 15px;
}
.folder-item:hover { background: var(--surface2); }
.folder-item.selected { background: rgba(124,92,252,0.15); color: var(--primary); }
.folder-icon { font-size: 18px; }
.folder-path-display {
  font-size: 12px;
  color: var(--text-muted);
  padding: 8px 12px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  margin-bottom: 12px;
  word-break: break-all;
  font-family: monospace;
}

/* Image upload */
.image-upload-area {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  padding: 20px;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  color: var(--text-muted);
}
.image-upload-area:hover, .image-upload-area.drag-over {
  border-color: var(--primary);
  background: rgba(124,92,252,0.05);
}
.image-previews {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}
.image-preview-item {
  position: relative;
  width: 72px;
  height: 72px;
}
.image-preview-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: var(--radius-sm);
}
.image-preview-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  border: none;
  cursor: pointer;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Output viewer */
.output-viewer {
  background: #0a0a0e;
  border-radius: var(--radius-sm);
  padding: 16px;
  font-family: 'SF Mono', 'Fira Code', 'Consolas', monospace;
  font-size: 13px;
  line-height: 1.6;
  max-height: 500px;
  overflow-y: auto;
  border: 1px solid var(--border);
}
.output-line { margin-bottom: 2px; white-space: pre-wrap; word-break: break-word; }
.output-line.stderr { color: #ff8080; }
.output-line.system { color: var(--text-muted); font-style: italic; }
.output-line.stdout { color: #e0e0f0; }

/* Usage bar */
.usage-bar-container { margin-top: 4px; }
.usage-bar-track {
  height: 6px;
  background: var(--surface2);
  border-radius: 3px;
  overflow: hidden;
}
.usage-bar-fill {
  height: 100%;
  border-radius: 3px;
  transition: width 0.5s ease;
}
.usage-bar-fill.low { background: var(--success); }
.usage-bar-fill.mid { background: var(--warning); }
.usage-bar-fill.high { background: var(--danger); }

/* Login page */
.login-container {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.login-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 32px 24px;
  width: 100%;
  max-width: 380px;
}
.login-logo { text-align: center; margin-bottom: 28px; }
.login-logo h1 { font-size: 24px; font-weight: 800; }
.login-logo h1 span { color: var(--primary); }
.login-logo p { color: var(--text-muted); font-size: 14px; margin-top: 4px; }

/* Toast */
.toast-container {
  position: fixed;
  top: 70px;
  right: 16px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.toast {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 14px;
  max-width: 320px;
  animation: slideIn 0.2s ease;
  box-shadow: 0 4px 16px rgba(0,0,0,0.3);
}
.toast.error { border-color: var(--danger); }
.toast.success { border-color: var(--success); }
@keyframes slideIn { from { transform: translateX(20px); opacity: 0; } }

/* Settings page */
.settings-section { margin-bottom: 24px; }
.settings-section h3 { font-size: 14px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 12px; }

/* Misc */
.divider { border: none; border-top: 1px solid var(--border); margin: 16px 0; }
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 13px; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }

/* Empty state */
.empty-state { text-align: center; padding: 48px 24px; color: var(--text-muted); }
.empty-state-icon { font-size: 48px; margin-bottom: 16px; }
.empty-state p { font-size: 15px; }

/* Detail page */
.detail-section { margin-bottom: 24px; }
.detail-label { font-size: 12px; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.08em; margin-bottom: 8px; }
.detail-value { font-size: 15px; white-space: pre-wrap; word-break: break-word; }

.image-grid { display: flex; flex-wrap: wrap; gap: 8px; }
.image-grid img { width: 90px; height: 90px; object-fit: cover; border-radius: var(--radius-sm); cursor: pointer; }

/* QR code */
.qr-container { text-align: center; padding: 16px; }
.qr-container img { max-width: 200px; border-radius: var(--radius-sm); background: #fff; padding: 8px; }

/* Recovery codes */
.recovery-codes {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-family: monospace;
  font-size: 14px;
}
.recovery-code {
  background: var(--surface2);
  padding: 8px 12px;
  border-radius: var(--radius-sm);
  text-align: center;
  letter-spacing: 0.1em;
}

/* Drag & drop reordering */
.job-item.dragging { opacity: 0.4; }
.job-item.drag-over { border-color: var(--primary); }

@media (min-width: 600px) {
  .modal { border-radius: var(--radius); margin: 20px; max-height: 80vh; }
  .modal-backdrop { align-items: center; }
}
