/* ===== Reset & Base ===== */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { font-size: 16px; -webkit-text-size-adjust: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--cream);
  color: var(--espresso);
  min-height: 100dvh;
  overscroll-behavior: none;
}

/* ===== Design Tokens ===== */
:root {
  --espresso: #2B2523;
  --cream: #F9F7F4;
  --warm-linen: #EBE8E3;
  --burnt-sienna: #B85C3A;
  --forest-green: #2D5144;
  --warm-gray: #918A85;
  --taupe: #6B6360;
  --amber: #D4A574;

  --status-pending: var(--warm-linen);
  --status-delivered: #E8F5E9;
  --status-delivered-text: #2D5144;
  --status-pending-sync: #FFFFFF;
  --status-pending-sync-border: #2D5144;
  --status-conflict: #FFF3E0;
  --status-conflict-text: #B85C3A;
  --status-failed: #FDECEA;
  --status-failed-text: #922B21;

  --radius: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --gap: 12px;
}

/* ===== Typography ===== */
h1 { font-family: 'Montserrat', sans-serif; font-size: 1.5rem; font-weight: 700; }
h2 { font-family: 'Montserrat', sans-serif; font-size: 1.125rem; font-weight: 600; }
h3 { font-family: 'Montserrat', sans-serif; font-size: 1rem; font-weight: 600; }
.text-sm { font-size: 0.875rem; }
.text-xs { font-size: 0.75rem; }
.text-muted { color: var(--taupe); }
.text-center { text-align: center; }

/* ===== Layout ===== */
#app {
  max-width: 480px;
  margin: 0 auto;
  padding: 16px;
  padding-bottom: 80px; /* room for nav */
}
.screen { display: none; }
.screen.active { display: block; }

/* ===== Header ===== */
.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
  margin-bottom: 8px;
}
.app-header h1 { font-size: 1.25rem; }
.header-badge {
  font-size: 0.75rem;
  padding: 4px 10px;
  border-radius: 20px;
  font-weight: 600;
}
.header-badge.planning { background: var(--warm-linen); color: var(--taupe); }
.header-badge.in_progress { background: #E3F2FD; color: #1565C0; }
.header-badge.completed { background: var(--status-delivered); color: var(--status-delivered-text); }

/* ===== Sync Bar ===== */
.sync-bar {
  display: none;
  align-items: center;
  gap: 8px;
  padding: 10px 14px;
  background: #FFF8E1;
  border-radius: var(--radius);
  margin-bottom: var(--gap);
  font-size: 0.875rem;
  color: #8D6E00;
  font-weight: 500;
}
.sync-bar.visible { display: flex; }
.sync-spinner {
  width: 16px; height: 16px;
  border: 2px solid #D4A574;
  border-top-color: transparent;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ===== Needs Review ===== */
.needs-review {
  display: none;
  padding: 12px 14px;
  background: var(--status-conflict);
  border-radius: var(--radius);
  margin-bottom: var(--gap);
  font-size: 0.875rem;
  color: var(--status-conflict-text);
  cursor: pointer;
}
.needs-review.visible { display: block; }

/* ===== Progress ===== */
.progress-section {
  margin-bottom: 16px;
}
.progress-bar-track {
  height: 8px;
  background: var(--warm-linen);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 6px;
}
.progress-bar-fill {
  height: 100%;
  background: var(--forest-green);
  border-radius: 4px;
  transition: width 0.3s ease;
}
.progress-stats {
  display: flex;
  justify-content: space-between;
  font-size: 0.75rem;
  color: var(--taupe);
}

/* ===== Stop Cards ===== */
.stop-list { display: flex; flex-direction: column; gap: var(--gap); }
.stop-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 14px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.1s ease;
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 72px;
  border: 2px solid transparent;
}
.stop-card:active { transform: scale(0.98); }
.stop-card.delivered { background: var(--status-delivered); }
.stop-card.pending-sync {
  background: var(--status-pending-sync);
  border-color: var(--status-pending-sync-border);
  border-style: dashed;
}
.stop-card.conflict { background: var(--status-conflict); }
.stop-card.failed { background: var(--status-failed); }

.stop-order {
  width: 32px; height: 32px;
  border-radius: 50%;
  background: var(--warm-linen);
  display: flex; align-items: center; justify-content: center;
  font-weight: 700; font-size: 0.875rem;
  flex-shrink: 0;
  color: var(--taupe);
}
.stop-card.delivered .stop-order { background: var(--forest-green); color: #fff; }
.stop-card.failed .stop-order { background: var(--status-failed-text); color: #fff; }

.stop-info { flex: 1; min-width: 0; }
.stop-name { font-weight: 600; font-size: 0.9375rem; }
.stop-postcode { font-size: 0.8125rem; color: var(--taupe); }
.stop-meta {
  display: flex; gap: 8px; margin-top: 4px;
  font-size: 0.75rem; color: var(--warm-gray);
}
.stop-badge {
  font-size: 0.6875rem;
  padding: 2px 8px;
  border-radius: 10px;
  font-weight: 600;
  white-space: nowrap;
}
.stop-badge.pending-sync { background: var(--forest-green); color: #fff; }
.stop-badge.conflict { background: var(--status-conflict-text); color: #fff; }

/* ===== Stop Detail Screen ===== */
.detail-header {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 16px;
}
.back-btn {
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  background: var(--warm-linen);
  border: none; border-radius: var(--radius);
  font-size: 1.25rem;
  cursor: pointer;
  flex-shrink: 0;
}
.detail-card {
  background: #fff;
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  margin-bottom: var(--gap);
}
.detail-row {
  display: flex; justify-content: space-between;
  align-items: flex-start;
  padding: 8px 0;
}
.detail-row + .detail-row { border-top: 1px solid var(--warm-linen); }
.detail-label { font-size: 0.8125rem; color: var(--taupe); }
.detail-value { font-size: 0.9375rem; font-weight: 500; text-align: right; }

.action-links {
  display: flex; gap: var(--gap); margin-bottom: var(--gap);
}
.action-link {
  flex: 1;
  display: flex; align-items: center; justify-content: center;
  gap: 6px;
  padding: 14px;
  background: #fff;
  border: 1px solid var(--warm-linen);
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--espresso);
  text-decoration: none;
  min-height: 48px;
}
.action-link:active { background: var(--warm-linen); }

/* ===== Buttons ===== */
.btn {
  display: block;
  width: 100%;
  padding: 16px;
  border: none;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  min-height: 48px;
  transition: opacity 0.2s;
}
.btn:disabled { opacity: 0.5; cursor: not-allowed; }
.btn-primary { background: var(--forest-green); color: #fff; }
.btn-primary:active:not(:disabled) { opacity: 0.85; }
.btn-danger { background: var(--status-failed-text); color: #fff; }
.btn-danger:active:not(:disabled) { opacity: 0.85; }
.btn-secondary { background: var(--warm-linen); color: var(--espresso); }
.btn-outline {
  background: transparent;
  border: 2px solid var(--warm-linen);
  color: var(--espresso);
}
.btn + .btn { margin-top: var(--gap); }

/* ===== Delivery Flow ===== */
.method-options {
  display: flex; flex-direction: column; gap: 8px;
  margin-bottom: 16px;
}
.method-option {
  display: flex; align-items: center; gap: 12px;
  padding: 14px;
  background: #fff;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  cursor: pointer;
  min-height: 48px;
  font-size: 0.9375rem;
}
.method-option.selected { border-color: var(--forest-green); background: #F0FAF4; }
.method-radio {
  width: 22px; height: 22px;
  border: 2px solid var(--warm-gray);
  border-radius: 50%;
  flex-shrink: 0;
  display: flex; align-items: center; justify-content: center;
}
.method-option.selected .method-radio {
  border-color: var(--forest-green);
}
.method-option.selected .method-radio::after {
  content: '';
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--forest-green);
}
.safe-place-input {
  display: none;
  margin-top: 8px;
}
.safe-place-input.visible { display: block; }
.safe-place-input textarea {
  width: 100%;
  padding: 12px;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  font-family: inherit;
  resize: vertical;
  min-height: 80px;
}
.safe-place-input textarea:focus { outline: none; border-color: var(--forest-green); }

/* ===== Failure Flow ===== */
.reason-options {
  display: flex; flex-direction: column; gap: 8px;
  margin-bottom: 16px;
}
.reason-option {
  padding: 14px;
  background: #fff;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  cursor: pointer;
  min-height: 48px;
  font-size: 0.9375rem;
}
.reason-option.selected { border-color: var(--status-failed-text); background: #FFF5F5; }
.reason-other-input {
  display: none; margin-top: 8px;
}
.reason-other-input.visible { display: block; }
.reason-other-input textarea {
  width: 100%; padding: 12px;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  font-size: 0.9375rem; font-family: inherit;
  resize: vertical; min-height: 80px;
}
.reason-other-input textarea:focus { outline: none; border-color: var(--status-failed-text); }

/* ===== Photo Button ===== */
.photo-section {
  margin-bottom: 16px;
}
.photo-preview {
  display: none;
  width: 100%;
  max-height: 200px;
  object-fit: cover;
  border-radius: var(--radius);
  margin-top: 8px;
}
.photo-preview.visible { display: block; }

/* ===== Plan Screen ===== */
.order-select-list { display: flex; flex-direction: column; gap: 8px; }
.order-select-card {
  padding: 14px;
  background: #fff;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  cursor: pointer;
}
.order-select-card.selected { border-color: var(--forest-green); background: #F0FAF4; }
.plan-controls {
  display: flex; gap: var(--gap);
  margin-bottom: 16px;
}
.plan-controls input, .plan-controls select {
  flex: 1; padding: 12px;
  border: 2px solid var(--warm-linen);
  border-radius: var(--radius);
  font-size: 0.9375rem;
  background: #fff;
}

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 48px 24px;
  color: var(--taupe);
}
.empty-state h2 { margin-bottom: 8px; color: var(--espresso); }
.empty-state p { margin-bottom: 24px; }

/* ===== Nav ===== */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  background: #fff;
  border-top: 1px solid var(--warm-linen);
  display: flex;
  max-width: 480px;
  margin: 0 auto;
  z-index: 100;
}
.nav-item {
  flex: 1;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  padding: 10px 0;
  font-size: 0.6875rem;
  color: var(--warm-gray);
  text-decoration: none;
  cursor: pointer;
  min-height: 56px;
  border: none; background: none;
}
.nav-item.active { color: var(--forest-green); font-weight: 600; }
.nav-icon { font-size: 1.25rem; margin-bottom: 2px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: 72px;
  left: 50%; transform: translateX(-50%);
  padding: 12px 24px;
  border-radius: var(--radius);
  font-size: 0.875rem;
  font-weight: 500;
  z-index: 200;
  opacity: 0;
  transition: opacity 0.3s;
  pointer-events: none;
  max-width: 400px;
}
.toast.visible { opacity: 1; }
.toast.success { background: var(--forest-green); color: #fff; }
.toast.error { background: var(--status-failed-text); color: #fff; }

/* ===== Loading ===== */
.loading {
  display: flex; align-items: center; justify-content: center;
  padding: 48px;
}
.loading-spinner {
  width: 32px; height: 32px;
  border: 3px solid var(--warm-linen);
  border-top-color: var(--forest-green);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

/* ===== Confirmation overlay ===== */
.confirm-overlay {
  display: none;
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.4);
  z-index: 150;
  align-items: flex-end;
  justify-content: center;
}
.confirm-overlay.visible { display: flex; }
.confirm-sheet {
  background: #fff;
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 24px;
  width: 100%;
  max-width: 480px;
}
.confirm-sheet h3 { margin-bottom: 8px; }
.confirm-sheet p { color: var(--taupe); margin-bottom: 20px; font-size: 0.9375rem; }
