/* ===== VARIABLES ===== */
:root {
  --primary: #1565C0;
  --primary-dark: #0D47A1;
  --primary-light: #E3F2FD;
  --success: #2E7D32;
  --warning: #F57F17;
  --danger: #C62828;
  --text: #212121;
  --text-secondary: #757575;
  --text-muted: #9E9E9E;
  --bg: #F5F5F5;
  --surface: #FFFFFF;
  --border: #E0E0E0;
  --shadow: 0 2px 8px rgba(0,0,0,0.1);
  --shadow-lg: 0 4px 16px rgba(0,0,0,0.15);
  --radius: 12px;
  --radius-sm: 8px;
}

/* ===== RESET ===== */
*, *::before, *::after { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

input, select, textarea, button {
  font-family: inherit;
  font-size: 16px; /* 16px prevents iOS zoom on focus */
}

/* ===== APP CONTAINER ===== */
#app {
  max-width: 480px;
  margin: 0 auto;
}

/* ===== SCREENS : simple show/hide ===== */
.screen { display: none; }
.screen.active { display: block; }

/* ===== HEADER ===== */
.app-header {
  display: flex;
  align-items: center;
  height: 56px;
  padding: 0 12px;
  background: var(--primary);
  color: white;
  gap: 8px;
  position: sticky;
  top: 0;
  z-index: 50;
}

.app-header h1 {
  font-size: 1.1rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-left, .header-right { flex-shrink: 0; }
.header-center { flex: 1; min-width: 0; }

.app-logo {
  font-weight: 800;
  font-size: 1.3rem;
  background: white;
  color: var(--primary);
  padding: 2px 8px;
  border-radius: 6px;
}

.icon-btn {
  background: none;
  border: none;
  color: white;
  font-size: 1.4rem;
  padding: 8px;
  cursor: pointer;
  position: relative;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  min-width: 40px;
  min-height: 40px;
}

.icon-btn:active { background: rgba(255,255,255,0.2); }
.back-arrow { font-size: 1.5rem; }
.sync-icon { font-size: 1.5rem; }

.badge {
  position: absolute;
  top: 2px; right: 2px;
  background: var(--danger);
  color: white;
  font-size: 0.65rem;
  font-weight: 700;
  min-width: 18px; height: 18px;
  border-radius: 9px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0 4px;
}

/* ===== BODY CONTENT ===== */
.screen-body {
  padding: 12px;
}

/* ===== ALERTS ===== */
.alert {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 12px 16px;
  border-radius: var(--radius-sm);
  margin: 8px 12px;
  font-size: 0.9rem;
}

.alert-warning {
  background: #FFF8E1;
  border: 1px solid #FFD54F;
  color: #F57F17;
}

.alert-content { flex: 1; }
.alert-content strong { display: block; margin-bottom: 4px; }
.alert-content p { font-size: 0.85rem; margin: 4px 0; }
.alert-icon { font-size: 1.3rem; flex-shrink: 0; }
.alert-actions { display: flex; gap: 8px; margin-top: 8px; }

/* ===== EMPTY STATE ===== */
.empty-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 60px 20px;
  color: var(--text-secondary);
}

.empty-icon { font-size: 4rem; margin-bottom: 16px; opacity: 0.5; }

/* ===== FAB ===== */
.fab {
  position: fixed;
  bottom: 24px; right: 20px;
  width: 60px; height: 60px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  font-size: 2rem;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--shadow-lg);
  cursor: pointer;
  z-index: 40;
}

.fab:active { transform: scale(0.92); }

/* ===== NDF LIST ===== */
.ndf-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ndf-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  cursor: pointer;
}

.ndf-item:active { transform: scale(0.98); }
.ndf-status-icon { font-size: 1.3rem; flex-shrink: 0; }
.ndf-info { flex: 1; min-width: 0; }

.ndf-info-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
}

.ndf-establishment {
  font-weight: 600;
  font-size: 0.95rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 60%;
}

.ndf-amount {
  font-weight: 700;
  font-size: 1rem;
  color: var(--primary-dark);
}

.ndf-info-bottom {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 2px;
}

.ndf-date, .ndf-category {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.ndf-category {
  background: var(--primary-light);
  color: var(--primary);
  padding: 1px 8px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 500;
}

/* ===== FORM ===== */
.form-container {
  padding-bottom: 80px; /* espace pour le bouton VALIDER sticky */
}

.form-section {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
  margin-bottom: 8px;
}

.section-header {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 16px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
}

.section-header:active { background: #FAFAFA; }
.section-icon { font-size: 1.2rem; flex-shrink: 0; }
.section-title { flex: 1; font-weight: 600; font-size: 0.95rem; }
.section-status { font-size: 0.75rem; color: var(--success); font-weight: 500; }

.section-chevron {
  font-size: 0.7rem;
  color: var(--text-muted);
  transition: transform 0.25s ease;
}

.form-section.open .section-chevron {
  transform: rotate(180deg);
}

/* Section body : simple display toggle */
.section-body {
  display: none;
  padding: 4px 16px 16px;
}

.form-section.open .section-body {
  display: block;
}

/* ===== FIELDS ===== */
.field { margin-bottom: 14px; }
.field:last-child { margin-bottom: 0; }

.field label {
  display: block;
  font-size: 0.82rem;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
}

.field input[type="text"],
.field input[type="number"],
.field input[type="date"],
.field input[type="email"],
.field select,
.field textarea {
  width: 100%;
  padding: 10px 12px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--surface);
  color: var(--text);
  font-size: 16px;
  -webkit-appearance: none;
  appearance: none;
}

.field input:focus,
.field select:focus,
.field textarea:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-light);
}

.field textarea { resize: vertical; min-height: 48px; }

.field select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 12 12'%3E%3Cpath d='M6 8L1 3h10z' fill='%23757575'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 36px;
}

.char-count {
  display: block;
  text-align: right;
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

/* ===== RADIO GROUP ===== */
.radio-group { display: flex; flex-wrap: wrap; gap: 8px; }
.radio-option { display: flex; cursor: pointer; }
.radio-option input { display: none; }

.radio-label {
  padding: 8px 16px;
  border: 1.5px solid var(--border);
  border-radius: 20px;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-secondary);
}

.radio-option input:checked + .radio-label {
  background: var(--primary);
  color: white;
  border-color: var(--primary);
}

/* ===== TOGGLE ===== */
.toggle-label {
  display: flex !important;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  text-transform: none !important;
  font-size: 0.95rem !important;
  font-weight: 500 !important;
  color: var(--text) !important;
}

.toggle-label input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  width: 44px; height: 24px;
  background: #BDBDBD;
  border-radius: 12px;
  position: relative;
  cursor: pointer;
  flex-shrink: 0;
}

.toggle-label input[type="checkbox"]::after {
  content: '';
  position: absolute;
  top: 2px; left: 2px;
  width: 20px; height: 20px;
  background: white;
  border-radius: 50%;
  transition: transform 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.2);
}

.toggle-label input[type="checkbox"]:checked { background: var(--primary); }
.toggle-label input[type="checkbox"]:checked::after { transform: translateX(20px); }

/* ===== INPUT WITH SUFFIX ===== */
.input-with-suffix { display: flex; gap: 8px; }
.input-with-suffix input { flex: 1; }

.currency-select {
  width: 80px;
  padding: 10px 8px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 0.9rem;
  font-weight: 600;
  text-align: center;
  background: #FAFAFA;
}

/* ===== VAT LINES ===== */
.vat-line { padding: 10px 0; border-bottom: 1px solid var(--border); }
.vat-line:last-child { border-bottom: none; }

.vat-line-fields {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr 1fr;
  gap: 8px;
}

.field-sm label { font-size: 0.7rem !important; margin-bottom: 4px !important; }
.field-sm input, .field-sm select { padding: 8px !important; font-size: 0.85rem !important; }

.vat-total {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0 0;
  font-size: 0.9rem;
}

/* ===== SCAN ===== */
.scan-empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px 0;
  gap: 12px;
}

.scan-preview { margin-bottom: 12px; }

.scan-pages {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding: 8px 0;
}

.scan-page {
  position: relative;
  width: 100px; height: 140px;
  flex-shrink: 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 2px solid var(--border);
}

.scan-page img { width: 100%; height: 100%; object-fit: cover; }

.scan-page-remove {
  position: absolute; top: 4px; right: 4px;
  width: 22px; height: 22px;
  border-radius: 50%;
  background: var(--danger);
  color: white; border: none;
  font-size: 0.7rem;
  display: flex; align-items: center; justify-content: center;
  cursor: pointer;
}

.scan-page-number {
  position: absolute; bottom: 4px; left: 4px;
  background: rgba(0,0,0,0.6);
  color: white; font-size: 0.65rem;
  padding: 1px 6px; border-radius: 8px;
}

.scan-actions { display: flex; gap: 8px; margin-top: 8px; }

/* ===== OCR ===== */
.ocr-field-wrapper { display: flex; gap: 8px; align-items: center; }
.ocr-field-wrapper input { flex: 1; }

.confidence-badge {
  padding: 3px 8px; border-radius: 10px;
  font-size: 0.7rem; font-weight: 700;
  white-space: nowrap; flex-shrink: 0;
}

.confidence-high { background: #E8F5E9; color: var(--success); }
.confidence-medium { background: #FFF8E1; color: var(--warning); }
.confidence-low { background: #FFEBEE; color: var(--danger); }

.ocr-loading {
  display: flex; flex-direction: column;
  align-items: center; padding: 30px;
  gap: 12px; color: var(--text-secondary);
}

.spinner {
  width: 36px; height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ===== ATTACHMENTS ===== */
.attachments-grid { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }

.attachment-item {
  position: relative;
  width: 80px; height: 80px;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1.5px solid var(--border);
  display: flex; align-items: center; justify-content: center;
  background: #FAFAFA;
}

.attachment-item img { width: 100%; height: 100%; object-fit: cover; }
.attachment-item .att-pdf { font-size: 0.7rem; font-weight: 600; color: var(--danger); text-align: center; padding: 4px; }

.attachment-remove {
  position: absolute; top: 2px; right: 2px;
  width: 20px; height: 20px; border-radius: 50%;
  background: var(--danger); color: white; border: none;
  font-size: 0.65rem; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

/* ===== GEO ===== */
.geo-result { padding: 12px; background: #F5F5F5; border-radius: var(--radius-sm); margin-top: 8px; }
.geo-address { font-weight: 600; font-size: 0.95rem; margin-bottom: 4px; }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px; padding: 10px 20px; border: none;
  border-radius: var(--radius-sm); font-size: 0.9rem;
  font-weight: 600; cursor: pointer;
}

.btn:active { transform: scale(0.96); }
.btn-primary { background: var(--primary); color: white; }
.btn-primary:disabled { background: #BDBDBD; cursor: not-allowed; }
.btn-outline { background: transparent; border: 1.5px solid var(--primary); color: var(--primary); }
.btn-outline.btn-danger { border-color: var(--danger); color: var(--danger); }
.btn-large { padding: 14px 28px; font-size: 1rem; }
.btn-sm { padding: 6px 14px; font-size: 0.8rem; }
.btn-full { width: 100%; }

/* ===== FORM FOOTER - sticky en bas de scroll ===== */
.form-footer {
  position: sticky;
  bottom: 0;
  padding: 12px 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  z-index: 40;
}

/* ===== CHIPS ===== */
.chips-container {
  display: flex; flex-wrap: wrap; gap: 6px;
  padding: 8px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  min-height: 44px; align-items: center;
}

.chip {
  display: flex; align-items: center; gap: 4px;
  padding: 4px 10px;
  background: var(--primary-light); color: var(--primary-dark);
  border-radius: 16px; font-size: 0.82rem; font-weight: 500;
}

.chip-remove { background: none; border: none; color: var(--primary); font-size: 1rem; cursor: pointer; padding: 0 2px; }

.chip-input {
  border: none !important; outline: none;
  flex: 1; min-width: 120px;
  padding: 4px !important; font-size: 0.9rem !important;
  box-shadow: none !important;
}

/* ===== STATUS CHIP ===== */
.status-chip { font-size: 0.75rem; padding: 3px 10px; border-radius: 12px; font-weight: 600; }
.status-draft { background: #E3F2FD; color: #1565C0; }
.status-synced { background: #E8F5E9; color: #2E7D32; }
.status-error { background: #FFEBEE; color: #C62828; }

/* ===== CAMERA MODAL ===== */
.modal {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.85);
  z-index: 100;
}

.camera-modal-content {
  width: 100%; height: 100%;
  display: flex; flex-direction: column;
  background: black;
}

.camera-header {
  display: flex; justify-content: space-between; align-items: center;
  padding: 12px 16px; color: white; font-weight: 600;
  background: rgba(0,0,0,0.7);
  position: absolute; top: 0; left: 0; right: 0; z-index: 2;
}

.camera-viewport { flex: 1; position: relative; overflow: hidden; }
.camera-viewport video { width: 100%; height: 100%; object-fit: cover; }

.camera-overlay {
  position: absolute; inset: 0;
  display: flex; align-items: center; justify-content: center;
  pointer-events: none;
}

.camera-frame {
  width: 85%; height: 70%;
  border: 2px dashed rgba(255,255,255,0.5);
  border-radius: 12px;
}

.camera-controls {
  display: flex; justify-content: center;
  padding: 20px; background: rgba(0,0,0,0.7);
  position: absolute; bottom: 0; left: 0; right: 0; z-index: 2;
}

.camera-btn {
  width: 72px; height: 72px; border-radius: 50%;
  border: 4px solid white; background: transparent; cursor: pointer;
  display: flex; align-items: center; justify-content: center;
}

.camera-btn:active { transform: scale(0.9); }
.camera-shutter { width: 56px; height: 56px; border-radius: 50%; background: white; }

/* ===== TOAST ===== */
.toast {
  position: fixed; bottom: 90px; left: 50%;
  transform: translateX(-50%) translateY(100px);
  background: #323232; color: white;
  padding: 12px 24px; border-radius: 24px;
  font-size: 0.9rem; font-weight: 500;
  z-index: 200; transition: transform 0.3s ease;
  text-align: center; max-width: calc(100% - 40px);
  box-shadow: var(--shadow-lg);
}

.toast.show { transform: translateX(-50%) translateY(0); }

/* ===== DETAIL ===== */
.detail-card {
  background: var(--surface); border-radius: var(--radius);
  padding: 16px; margin-bottom: 10px; box-shadow: var(--shadow);
}

.detail-row {
  display: flex; justify-content: space-between;
  padding: 8px 0; border-bottom: 1px solid #F5F5F5;
}

.detail-row:last-child { border-bottom: none; }
.detail-label { font-size: 0.82rem; color: var(--text-muted); font-weight: 500; }
.detail-value { font-weight: 600; text-align: right; max-width: 60%; word-break: break-word; }

.detail-amount {
  font-size: 1.8rem; font-weight: 800;
  color: var(--primary-dark); text-align: center; padding: 16px 0;
}

.detail-receipt { width: 100%; border-radius: var(--radius-sm); margin-top: 8px; }

/* ===== UTILITIES ===== */
/* ===== LOGIN ===== */
.login-container {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
  background: var(--bg);
}

.login-logo {
  text-align: center;
  margin-bottom: 40px;
}

.login-form {
  width: 100%;
  max-width: 360px;
  background: var(--surface);
  padding: 24px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
}

.login-form .field { margin-bottom: 16px; }

.login-error {
  background: #FFEBEE;
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
  margin-bottom: 12px;
}

/* ===== SYNC PROGRESS ===== */
.sync-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,0.6);
  z-index: 80;
  display: flex;
  align-items: center;
  justify-content: center;
}

.sync-dialog {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 32px 24px;
  text-align: center;
  max-width: 320px;
  width: 90%;
  box-shadow: var(--shadow-lg);
}

.sync-dialog .spinner {
  margin: 0 auto 16px;
}

.sync-progress-text {
  font-size: 0.9rem;
  color: var(--text-secondary);
  margin-top: 8px;
}

.sync-result {
  font-size: 0.9rem;
  margin-top: 12px;
  line-height: 1.6;
}

.sync-result .success { color: var(--success); font-weight: 600; }
.sync-result .error { color: var(--danger); font-weight: 600; }

/* ===== USER INFO IN HEADER ===== */
.user-chip {
  font-size: 0.7rem;
  background: rgba(255,255,255,0.2);
  padding: 2px 8px;
  border-radius: 10px;
  color: white;
}

/* ===== UTILITIES ===== */
.hidden { display: none !important; }
.text-muted { color: var(--text-muted); }
.text-center { text-align: center; }
.text-sm { font-size: 0.8rem; }
.text-danger { color: var(--danger); }
