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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: #f5f5f5;
  color: #333;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

header {
  background: #1a1a2e;
  color: #fff;
  padding: 1rem 1.2rem;
  position: sticky;
  top: 0;
  z-index: 100;
}
.header-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
}
.header-logo { height: 36px; width: auto; filter: brightness(0) invert(1); }
header h1 { font-size: 1.2rem; font-weight: 600; }
header .subtitle { font-size: 0.75rem; opacity: 0.7; margin-top: 0.15rem; }

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem;
}

.step { display: none; }
.step.active { display: block; }
.hidden { display: none !important; }

/* ========== Upload Step ========== */
#dropzone {
  border: 2px dashed #bbb;
  border-radius: 16px;
  padding: 2.5rem 1.5rem;
  text-align: center;
  cursor: pointer;
  transition: border-color 0.2s, background 0.2s;
  -webkit-tap-highlight-color: transparent;
}
#dropzone.dragover { border-color: #4a6cf7; background: #eef2ff; }
#dropzone:active { background: #eef2ff; }

.upload-icon { margin-bottom: 0.8rem; }
.upload-icon svg { opacity: 0.7; }
.upload-title { font-size: 1.2rem; font-weight: 600; color: #333; margin-bottom: 0.3rem; }
#dropzone .small { font-size: 0.85rem; color: #888; }

.upload-buttons {
  display: flex;
  gap: 0.75rem;
  margin-top: 1rem;
}

.btn-upload {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.9rem 1rem;
  font-size: 1rem;
  font-weight: 500;
  border-radius: 12px;
  min-height: 56px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.upload-hint {
  text-align: center;
  font-size: 0.75rem;
  color: #aaa;
  margin-top: 0.8rem;
}

#upload-status {
  text-align: center;
  padding: 3rem 1rem;
}
#upload-status p { color: #555; }
#upload-status .small { color: #999; font-size: 0.8rem; margin-top: 0.3rem; }

.spinner {
  width: 40px; height: 40px;
  border: 3px solid #ddd;
  border-top-color: #4a6cf7;
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
  margin: 0 auto 1rem;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ========== Buttons ========== */
.btn {
  display: inline-block;
  padding: 0.6rem 1.2rem;
  border: none;
  border-radius: 8px;
  font-size: 0.95rem;
  cursor: pointer;
  text-decoration: none;
  transition: background 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.btn:active { transform: scale(0.97); }
.btn-primary { background: #4a6cf7; color: #fff; }
.btn-primary:hover { background: #3a5cd7; }
.btn-primary:disabled { background: #99aaf7; cursor: not-allowed; }
.btn-secondary { background: #e0e0e0; color: #333; }
.btn-secondary:hover { background: #d0d0d0; }
.btn-small { padding: 0.4rem 1rem; font-size: 0.85rem; }
.btn-danger { background: #e74c3c; color: #fff; font-size: 0.8rem; padding: 0.3rem 0.6rem; border-radius: 6px; }
.btn-danger:hover { background: #c0392b; }

.btn-add-line { width: 100%; text-align: center; margin-top: 0.5rem; }
.btn-submit { width: 100%; padding: 0.9rem; font-size: 1.05rem; font-weight: 600; min-height: 52px; }

/* ========== Form ========== */
fieldset {
  border: 1px solid #ddd;
  border-radius: 12px;
  padding: 1rem;
  margin-bottom: 1rem;
  background: #fff;
}
legend { font-weight: 600; padding: 0 0.5rem; font-size: 0.95rem; }

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.7rem;
}
.field.full-width { grid-column: 1 / -1; }

label {
  display: block;
  font-size: 0.78rem;
  font-weight: 500;
  margin-bottom: 0.15rem;
  color: #555;
}

input, textarea, select {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid #ccc;
  border-radius: 8px;
  font-size: 16px; /* prevents iOS zoom */
  font-family: inherit;
  -webkit-appearance: none;
  appearance: none;
}
input:focus, textarea:focus { outline: none; border-color: #4a6cf7; box-shadow: 0 0 0 3px rgba(74,108,247,0.15); }
input.readonly { background: #f0f0f0; }

/* ========== Table (Desktop) ========== */
.table-wrap { overflow-x: auto; margin-bottom: 0.5rem; -webkit-overflow-scrolling: touch; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.85rem;
}
th, td {
  padding: 0.4rem 0.4rem;
  border-bottom: 1px solid #eee;
  text-align: left;
}
th { font-weight: 600; font-size: 0.7rem; color: #666; text-transform: uppercase; }
td input {
  padding: 0.3rem 0.4rem;
  font-size: 0.85rem;
  border: 1px solid #ddd;
  border-radius: 4px;
}
td input[type="number"] { width: 75px; }
td input.article-code { width: 110px; }
td textarea.description {
  width: 260px;
  min-height: 28px;
  resize: none;
  overflow: hidden;
  font-family: inherit;
  font-size: 0.85rem;
  padding: 0.3rem 0.4rem;
  border: 1px solid #ddd;
  border-radius: 4px;
  line-height: 1.3;
}
.line-card textarea {
  resize: none;
  overflow: hidden;
  line-height: 1.3;
}
td .line-total { font-weight: 600; white-space: nowrap; }

/* ========== Line Cards (Mobile) ========== */
.line-card {
  background: #fafafa;
  border: 1px solid #e8e8e8;
  border-radius: 10px;
  padding: 0.8rem;
  margin-bottom: 0.6rem;
  position: relative;
}
.line-card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 0.6rem;
}
.line-card-num {
  font-weight: 700;
  font-size: 0.8rem;
  color: #4a6cf7;
}
.line-card-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.5rem;
}
.line-card-grid .field-full { grid-column: 1 / -1; }
.line-card label { font-size: 0.72rem; }
.line-card input {
  padding: 0.45rem 0.6rem;
  font-size: 15px;
  border-radius: 6px;
}
.line-card-total {
  text-align: right;
  font-weight: 700;
  font-size: 0.95rem;
  margin-top: 0.5rem;
  color: #333;
}

/* ========== Totals ========== */
.totals {
  display: flex;
  gap: 1rem;
  justify-content: flex-end;
  align-items: baseline;
  padding: 0.8rem 0;
  font-size: 0.85rem;
  flex-wrap: wrap;
}
.total-main { font-size: 1.05rem; }

/* ========== Form Actions ========== */
.form-actions {
  display: flex;
  gap: 0.8rem;
  padding: 0.8rem 0;
}
.form-actions .btn-secondary { flex: 0 0 auto; }
.form-actions .btn-submit { flex: 1; }

/* ========== Warnings / Errors / Success ========== */
.warning-box {
  background: #fff8e1;
  border: 1px solid #ffc107;
  border-radius: 8px;
  padding: 0.6rem 0.8rem;
  margin-bottom: 0.5rem;
  font-size: 0.82rem;
  color: #795548;
}
.error-box {
  background: #fdecea;
  border: 1px solid #e74c3c;
  border-radius: 8px;
  padding: 0.8rem 1rem;
  font-size: 0.9rem;
  color: #c0392b;
  margin-bottom: 0.8rem;
}
.success-box {
  background: #e8f5e9;
  border: 1px solid #4caf50;
  border-radius: 12px;
  padding: 1.5rem;
  margin-bottom: 1rem;
  text-align: center;
}
.success-box h2 { color: #2e7d32; font-size: 1.3rem; margin-bottom: 0.5rem; }

/* ========== Raw text ========== */
details { margin-top: 1rem; }
summary { cursor: pointer; font-size: 0.82rem; color: #666; padding: 0.5rem 0; }
pre {
  background: #f8f8f8;
  border: 1px solid #ddd;
  border-radius: 8px;
  padding: 0.8rem;
  max-height: 250px;
  overflow: auto;
  font-size: 0.75rem;
  white-space: pre-wrap;
  word-break: break-word;
  margin-top: 0.5rem;
  -webkit-overflow-scrolling: touch;
}

/* ========== Confirm Modal ========== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  z-index: 1000;
  padding: 1rem;
}
.modal {
  background: #fff;
  border-radius: 16px;
  padding: 1.5rem;
  max-width: 450px;
  width: 100%;
  animation: slideUp 0.25s ease-out;
}
@keyframes slideUp {
  from { transform: translateY(30px); opacity: 0; }
  to { transform: translateY(0); opacity: 1; }
}
.modal h3 { margin-bottom: 0.8rem; font-size: 1.1rem; }
.modal p { font-size: 0.9rem; color: #555; margin-bottom: 0.8rem; }
.modal-actions {
  display: flex;
  gap: 0.8rem;
}
.modal-actions .btn {
  flex: 1;
  text-align: center;
  padding: 0.8rem;
  min-height: 48px;
}

/* ========== Supplier Panel ========== */
.supplier-panel {
  background: #fff3e0;
  border: 1px solid #ff9800;
  border-radius: 10px;
  padding: 1rem;
  margin-bottom: 1rem;
}
.supplier-panel-header {
  font-weight: 600;
  font-size: 0.9rem;
  color: #e65100;
  margin-bottom: 0.7rem;
}
.supplier-search-row {
  display: flex;
  gap: 0.5rem;
  margin-bottom: 0.5rem;
}
.supplier-search-row input { flex: 1; }
.supplier-search-row .btn { flex-shrink: 0; }

.supplier-searching,
.supplier-no-results {
  padding: 0.6rem;
  font-size: 0.85rem;
  color: #666;
  text-align: center;
}

.supplier-result {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.5rem 0.6rem;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  background: #fff;
  margin-bottom: 0.4rem;
  gap: 0.5rem;
}
.supplier-result-info {
  display: flex;
  flex-direction: column;
  min-width: 0;
  flex: 1;
}
.supplier-result-info strong {
  font-size: 0.85rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.supplier-result-nif {
  font-size: 0.72rem;
  color: #888;
}

.supplier-create-divider {
  text-align: center;
  margin: 0.6rem 0;
  position: relative;
  font-size: 0.8rem;
  color: #999;
}
.supplier-create-divider::before,
.supplier-create-divider::after {
  content: '';
  position: absolute;
  top: 50%;
  width: 40%;
  height: 1px;
  background: #ddd;
}
.supplier-create-divider::before { left: 0; }
.supplier-create-divider::after { right: 0; }

/* ========== Responsive: Show/hide table vs cards ========== */
.mobile-only { display: none; }
.desktop-only { display: block; }

/* ========== Mobile (< 768px) ========== */
@media (max-width: 767px) {
  header { padding: 0.8rem 1rem; }
  header h1 { font-size: 1.1rem; }

  main { padding: 0.8rem; }

  .form-grid { grid-template-columns: 1fr; }

  .desktop-only { display: none !important; }
  .mobile-only { display: block !important; }

  .totals { justify-content: space-between; gap: 0.5rem; }

  .modal-overlay { align-items: flex-end; padding: 0; }
  .modal { border-radius: 16px 16px 0 0; max-width: 100%; }

  #step-result .btn-upload { width: 100%; text-align: center; }
}

/* ========== Tablet (768-1024px) ========== */
@media (min-width: 768px) and (max-width: 1024px) {
  .form-grid { grid-template-columns: 1fr 1fr; }
  .upload-buttons { max-width: 500px; margin: 1rem auto 0; }
  #dropzone { max-width: 500px; margin: 0 auto; }
}

/* ========== Desktop (> 1024px) ========== */
@media (min-width: 1025px) {
  main { padding: 1.5rem 2rem; margin-top: 1rem; }

  header { padding: 1.2rem 2rem; }
  header h1 { font-size: 1.4rem; }

  .form-grid { grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); }

  #dropzone { max-width: 600px; margin: 0 auto; }
  .upload-buttons { max-width: 600px; margin: 1rem auto 0; }

  .btn-submit { width: auto; min-width: 240px; }
  .btn-add-line { width: auto; }

  .modal-overlay { align-items: center; }
  .modal { border-radius: 16px; }
}
