:root {
  --bg: #f8f9fb;
  --surface: #fff;
  --surface2: #f1f3f5;
  --border: #e5e8ec;
  --text: #1a1d21;
  --text-muted: #6b7280;
  --accent: #2563eb;
  --accent-hover: #1d4ed8;
  --success: #059669;
  --error: #dc2626;
  --radius: 14px;
  --radius-sm: 10px;
  --font: 'DM Sans', system-ui, sans-serif;
  --shadow: 0 1px 3px rgba(0,0,0,0.06);
  --shadow-lg: 0 4px 14px rgba(0,0,0,0.08);
}

* { box-sizing: border-box; }

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: -webkit-fill-available;
  font-family: var(--font);
  font-size: 15px;
  line-height: 1.5;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
  overflow-x: hidden;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px 40px;
  padding-bottom: calc(40px + env(safe-area-inset-bottom));
}

/* Header */
.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 28px;
}

.logo {
  font-size: 1.35rem;
  font-weight: 700;
  letter-spacing: -0.03em;
  color: var(--text);
  min-width: 0;
}

.status-pills {
  display: flex;
  gap: 8px;
}

.pill {
  font-size: 0.75rem;
  font-weight: 500;
  padding: 4px 10px;
  border-radius: 999px;
  background: var(--surface2);
  color: var(--text-muted);
}

.pill.ready {
  background: rgba(5, 150, 105, 0.12);
  color: var(--success);
}

.pill.error {
  background: rgba(220, 38, 38, 0.1);
  color: var(--error);
}

/* Tabs */
.tabs {
  display: flex;
  gap: 4px;
  margin-bottom: 20px;
  padding: 4px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  width: 100%;
  max-width: 320px;
}

.tab {
  flex: 1;
  min-height: 44px;
  padding: 10px 12px;
  border: none;
  border-radius: 8px;
  font-family: var(--font);
  font-size: 0.9rem;
  font-weight: 500;
  color: var(--text-muted);
  background: transparent;
  cursor: pointer;
  transition: color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.tab:hover {
  color: var(--text);
}

.tab.active {
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
}

/* Panels */
.panel {
  display: none;
}

.panel.active {
  display: block;
}

/* Steps */
.step {
  margin-bottom: 24px;
}

.step-label {
  margin: 0 0 10px;
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-muted);
}

.drop {
  padding: 28px 20px;
  min-height: 100px;
  background: var(--surface);
  border: 2px dashed var(--border);
  border-radius: var(--radius);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.drop:hover,
.drop.dragover {
  border-color: var(--accent);
  background: rgba(37, 99, 235, 0.04);
}

.drop-main {
  margin: 0 0 4px;
  font-size: 1.05rem;
  font-weight: 500;
  color: var(--text);
}

.drop-sub {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.drop.dual {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  padding: 16px;
}

.drop-half {
  padding: 20px 12px;
  min-height: 80px;
  background: var(--surface2);
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.drop-half:hover,
.drop-half.dragover {
  border-color: var(--accent);
  background: rgba(37, 99, 235, 0.06);
}

.drop-half .drop-main { font-size: 0.95rem; }
.drop-half .drop-sub { font-size: 0.8rem; }

/* Template grid */
.template-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  gap: 10px;
}

.template-card {
  padding: 0;
  border: 2px solid var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface);
  cursor: pointer;
  transition: border-color 0.2s, box-shadow 0.2s;
  font-family: var(--font);
  text-align: left;
  box-shadow: var(--shadow);
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  min-height: 44px;
}

.template-card:hover {
  border-color: var(--accent);
}

.template-card.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(37, 99, 235, 0.25);
}

.template-card img {
  width: 100%;
  aspect-ratio: 16/10;
  object-fit: cover;
  display: block;
}

.template-name {
  padding: 6px 8px;
  font-size: 0.7rem;
  color: var(--text-muted);
  line-height: 1.3;
}

.loading {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
}

/* Slider */
.ai-mode-label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  cursor: pointer;
}
.ai-mode-label input { margin: 0; }
.ai-mode-label small { font-size: 0.7rem; color: var(--text-muted); }
.ai-mode-label.single-ai { margin-bottom: 8px; }

.slider-label {
  display: flex;
  align-items: center;
  gap: 12px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.slider-label span { min-width: 60px; }

.slider-label input[type="range"] {
  width: 120px;
  min-width: 80px;
  height: 8px;
  accent-color: var(--accent);
  touch-action: none;
}

.title-inline {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.title-inline span { min-width: 52px; }

.title-inline input {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-family: var(--font);
  font-size: 16px;
  min-width: 0;
  flex: 1;
}

.single-title { margin-bottom: 12px; }

/* Step actions */
.step-actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 16px;
  margin-top: 20px;
}

/* Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 12px 20px;
  min-height: 44px;
  min-width: 44px;
  border: none;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.95rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.btn:active { transform: scale(0.98); }
.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: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
}

.btn.secondary:hover:not(:disabled) {
  background: var(--border);
}

.btn.ghost {
  background: transparent;
  color: var(--text-muted);
}

.btn.ghost:hover:not(:disabled) {
  color: var(--text);
  background: var(--surface2);
}

.btn.outline {
  background: transparent;
  color: var(--accent);
  border: 1px solid var(--accent);
}

.btn.outline:hover:not(:disabled) {
  background: rgba(37, 99, 235, 0.08);
}

.spinner {
  width: 18px;
  height: 18px;
  border: 2px solid rgba(255,255,255,0.35);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.6s linear infinite;
}

.btn.ghost .spinner,
.btn.outline .spinner {
  border-color: rgba(37, 99, 235, 0.3);
  border-top-color: var(--accent);
}

@keyframes spin { to { transform: rotate(360deg); } }

/* Preview block */
.preview-block {
  margin-top: 24px;
  padding: 20px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.preview-split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.preview-cell {
  position: relative;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  overflow: hidden;
  min-height: 140px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.preview-cell img {
  width: 100%;
  height: 140px;
  object-fit: contain;
  display: block;
}

.preview-placeholder {
  position: absolute;
  font-size: 0.85rem;
  color: var(--text-muted);
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  align-items: center;
}

/* Options */
.options-details {
  margin-top: 16px;
  padding: 14px 18px;
  background: var(--surface2);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
}

.options-details summary {
  cursor: pointer;
  font-weight: 500;
  color: var(--text-muted);
  padding: 4px 0;
  min-height: 44px;
  display: flex;
  align-items: center;
  -webkit-tap-highlight-color: transparent;
}

.options-details summary:hover {
  color: var(--text);
}

.options-inner {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-top: 12px;
}

.field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.field span {
  color: var(--text-muted);
  min-width: 60px;
}

.field input,
.field select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  font-family: var(--font);
  font-size: 16px; /* prevents iOS zoom on focus */
}

@media (min-width: 480px) {
  .field input,
  .field select { font-size: 0.9rem; }
}

.field input[type="number"] { width: 72px; }
.field input[type="text"] { min-width: 100px; }
.field select { min-width: 90px; }

/* Result block */
.result-block {
  margin-top: 28px;
  padding: 24px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}

.result-block.simple {
  padding: 16px 24px;
}

.result-meta {
  margin: 0 0 16px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.result-image {
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: var(--surface2);
  margin-bottom: 12px;
}

.result-image img {
  max-width: 100%;
  max-height: 380px;
  object-fit: contain;
  display: block;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
  margin: 16px 0;
}

.result-grid a,
.batch-result-item {
  display: block;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--surface2);
}

.result-grid img,
.batch-result-item img {
  width: 100%;
  aspect-ratio: 16/10;
  object-fit: cover;
  display: block;
}

.link {
  display: inline-block;
  margin-bottom: 16px;
  padding: 4px 0;
  font-size: 0.9rem;
  color: var(--accent);
  text-decoration: none;
  word-break: break-all;
  min-height: 44px;
  line-height: 1.4;
}

.link:hover { text-decoration: underline; }

.link-btn {
  background: none;
  border: none;
  padding: 12px 0;
  min-height: 44px;
  font-family: var(--font);
  font-size: 0.9rem;
  color: var(--accent);
  cursor: pointer;
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}

.link-btn:hover { text-decoration: underline; }

.quick-upload {
  margin-top: 24px;
  margin-bottom: 0;
}

/* Plate row */
.plate-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  margin-bottom: 16px;
}

.plate-name {
  font-size: 0.85rem;
  color: var(--text-muted);
}

/* Toast */
.toast {
  position: fixed;
  left: 16px;
  right: 16px;
  bottom: calc(24px + env(safe-area-inset-bottom));
  max-width: 400px;
  margin: 0 auto;
  transform: translateY(100px);
  padding: 14px 20px;
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 500;
  background: var(--text);
  color: var(--surface);
  box-shadow: var(--shadow-lg);
  opacity: 0;
  transition: transform 0.25s, opacity 0.25s;
  z-index: 100;
}

.toast.show {
  transform: translateY(0);
  opacity: 1;
}

.toast.success { background: var(--success); }
.toast.error { background: var(--error); }

/* Footer */
.footer {
  margin-top: 40px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
  color: var(--text-muted);
}

.footer p { margin: 0; }

/* Vehicles dashboard */
.dashboard-title {
  margin: 0 0 6px;
  font-size: 1.25rem;
  font-weight: 600;
  color: var(--text);
}

.dashboard-sub {
  margin: 0 0 20px;
  font-size: 0.9rem;
  color: var(--text-muted);
}

.vehicles-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 16px;
}

.vehicle-card {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
}

.vehicle-card-preview {
  aspect-ratio: 16/10;
  background: var(--surface2);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}

.vehicle-card-preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.vehicle-card-preview .no-preview {
  font-size: 0.85rem;
  color: var(--text-muted);
}

.vehicle-card-body {
  padding: 12px 14px;
  flex: 1;
}

.vehicle-card-title {
  margin: 0 0 4px;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.vehicle-card-meta {
  margin: 0 0 10px;
  font-size: 0.8rem;
  color: var(--text-muted);
}

.btn-danger {
  color: var(--error);
  font-size: 0.85rem;
}

.btn-danger:hover:not(:disabled) {
  color: var(--error);
  background: rgba(220, 38, 38, 0.08);
}

.vehicles-empty {
  margin: 0;
  padding: 24px;
  text-align: center;
  font-size: 0.95rem;
  color: var(--text-muted);
  grid-column: 1 / -1;
}

/* Mobile-first responsive */
@media (max-width: 480px) {
  .app { padding: 16px 12px 32px; padding-bottom: calc(32px + env(safe-area-inset-bottom)); }
  .header { margin-bottom: 20px; flex-wrap: wrap; gap: 10px; }
  .logo { font-size: 1.2rem; }
  .tabs { max-width: none; }
  .tab { font-size: 0.85rem; padding: 10px 8px; }
  .step { margin-bottom: 18px; }
  .step-label { font-size: 0.75rem; }
  .drop { padding: 24px 16px; min-height: 88px; }
  .drop-main { font-size: 1rem; }
  .drop-sub { font-size: 0.85rem; }
  .template-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .template-name { padding: 6px 6px; font-size: 0.65rem; }
  .step-actions { flex-direction: column; align-items: stretch; gap: 12px; margin-top: 16px; }
  .slider-label { flex-wrap: wrap; }
  .slider-label input[type="range"] { flex: 1; max-width: 200px; }
  .title-inline { flex-direction: column; align-items: stretch; gap: 6px; }
  .title-inline span { min-width: 0; }
  .preview-block { padding: 16px; margin-top: 20px; }
  .preview-cell { min-height: 120px; }
  .preview-cell img { height: 120px; }
  .actions { flex-direction: column; align-items: stretch; gap: 8px; }
  .actions .btn { width: 100%; justify-content: center; }
  .options-details { padding: 12px 14px; margin-top: 12px; }
  .options-inner { flex-direction: column; gap: 12px; margin-top: 10px; }
  .field { flex-wrap: wrap; }
  .field input[type="number"], .field input[type="text"], .field select { width: 100%; max-width: 140px; }
  .result-block { padding: 18px 16px; margin-top: 22px; }
  .result-block.simple { padding: 14px 16px; }
  .result-image img { max-height: 280px; }
  .result-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .plate-row { flex-direction: column; align-items: stretch; gap: 8px; }
  .plate-row .btn { width: 100%; }
  .vehicles-list { grid-template-columns: 1fr; gap: 12px; }
  .vehicle-card-body { padding: 10px 12px; }
  .vehicle-card-title { font-size: 0.9rem; white-space: normal; }
  .dashboard-title { font-size: 1.1rem; }
  .dashboard-sub { margin-bottom: 16px; font-size: 0.85rem; }
}

@media (max-width: 560px) {
  .drop.dual { grid-template-columns: 1fr; padding: 14px; }
  .drop-half { min-height: 72px; }
  .preview-split { grid-template-columns: 1fr; gap: 10px; }
  .step-actions { flex-direction: column; align-items: stretch; }
}

@media (min-width: 481px) and (max-width: 640px) {
  .template-grid { grid-template-columns: repeat(3, 1fr); }
  .vehicles-list { grid-template-columns: repeat(2, 1fr); }
}
