/* === RESET & BASE === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --primary: #1a3a5c;
  --primary-light: #2a5298;
  --accent: #e8a020;
  --accent-light: #f5c060;
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface2: #eef1f6;
  --border: #d0d7e3;
  --text: #1a2740;
  --text-muted: #6b7a99;
  --danger: #c0392b;
  --danger-light: #fdf0ef;
  --success: #27ae60;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 2px 12px rgba(26,58,92,.10);
  --shadow-md: 0 4px 24px rgba(26,58,92,.16);
  --header-h: 60px;
  --font: 'Segoe UI', system-ui, -apple-system, sans-serif;
}

html { font-size: 16px; -webkit-font-smoothing: antialiased; }

body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  min-height: 100dvh;
  overflow-x: hidden;
}

/* === VIEWS === */
.view { display: none; min-height: 100dvh; flex-direction: column; }
.view.active { display: flex; }

/* === HEADER === */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 0 16px;
  box-shadow: 0 2px 8px rgba(0,0,0,.2);
}

.app-header h1 {
  flex: 1;
  font-size: 1.1rem;
  font-weight: 600;
  letter-spacing: .02em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-logo {
  height: 36px;
  width: auto;
  object-fit: contain;
  filter: brightness(0) invert(1);
  flex-shrink: 0;
}

.btn-icon {
  width: 40px; height: 40px;
  border-radius: 50%;
  background: transparent;
  border: none;
  color: #fff;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}
.btn-icon:hover { background: rgba(255,255,255,.15); }
.btn-icon svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-icon.danger:hover { background: rgba(192,57,43,.3); }

.header-actions { display: flex; gap: 4px; }

/* === SEARCH BAR === */
.search-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  padding: 10px 16px;
}
.search-bar svg { width: 18px; height: 18px; fill: none; stroke: var(--text-muted); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }
.search-bar input { flex: 1; border: none; background: transparent; font-size: .95rem; color: var(--text); outline: none; }
.search-bar input::placeholder { color: var(--text-muted); }

/* === CARD LIST === */
.card-list {
  flex: 1;
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.albaran-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: box-shadow .15s, transform .1s;
  display: flex;
  align-items: center;
  gap: 12px;
  border-left: 4px solid var(--accent);
}
.albaran-card:hover { box-shadow: var(--shadow-md); transform: translateY(-1px); }

.card-badge {
  background: var(--primary);
  color: #fff;
  border-radius: 6px;
  padding: 4px 8px;
  font-size: .75rem;
  font-weight: 700;
  white-space: nowrap;
  flex-shrink: 0;
}

.card-info { flex: 1; min-width: 0; }
.card-empresa { font-size: .95rem; font-weight: 600; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.card-meta { font-size: .8rem; color: var(--text-muted); margin-top: 2px; }

.card-arrow { color: var(--border); flex-shrink: 0; }
.card-arrow svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* === EMPTY STATE === */
.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 40px 20px;
  color: var(--text-muted);
  text-align: center;
}
.empty-state svg { width: 64px; height: 64px; fill: none; stroke: var(--border); stroke-width: 1.5; stroke-linecap: round; stroke-linejoin: round; }
.empty-state p { font-size: .95rem; line-height: 1.6; }
.hidden { display: none !important; }

/* === FORM BODY === */
.form-body {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding-bottom: 32px;
}

.form-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.section-title {
  font-size: .8rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--primary-light);
  border-bottom: 1px solid var(--surface2);
  padding-bottom: 8px;
  margin-bottom: 4px;
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
.form-grid.three-col { grid-template-columns: 1fr 1fr; }

.field { display: flex; flex-direction: column; gap: 5px; }
.field.full-width { grid-column: 1 / -1; }
.field label { font-size: .8rem; font-weight: 600; color: var(--text-muted); text-transform: uppercase; letter-spacing: .04em; }
.field label .req { color: var(--accent); }

input, textarea, select {
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 9px 12px;
  font-size: .95rem;
  font-family: var(--font);
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color .15s, box-shadow .15s;
  width: 100%;
}
input:focus, textarea:focus, select:focus {
  border-color: var(--primary-light);
  box-shadow: 0 0 0 3px rgba(42,82,152,.12);
}
textarea { resize: vertical; min-height: 80px; }
.readonly-field { background: var(--surface2); color: var(--text-muted); cursor: default; }

/* === AUTOCOMPLETE === */
.autocomplete-wrapper { position: relative; }
.dropdown {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--surface);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-md);
  max-height: 240px;
  overflow-y: auto;
  z-index: 200;
  list-style: none;
}
.dropdown li {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--surface2);
  transition: background .1s;
}
.dropdown li:last-child { border-bottom: none; }
.dropdown li:hover, .dropdown li.active { background: var(--surface2); }
.dropdown .dd-name { font-size: .9rem; font-weight: 600; color: var(--text); }
.dropdown .dd-addr { font-size: .78rem; color: var(--text-muted); margin-top: 2px; }

/* === OPERARIOS === */
.operario-row {
  display: grid;
  grid-template-columns: 1fr 90px 90px auto;
  gap: 8px;
  align-items: end;
}
.operario-row select { padding: 9px 8px; }

.btn-remove-op {
  width: 36px; height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--danger-light);
  color: var(--danger);
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  transition: background .15s;
  flex-shrink: 0;
}
.btn-remove-op:hover { background: #f5c0bc; }
.btn-remove-op svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* === BUTTONS === */
.btn-primary, .btn-secondary, .btn-ghost, .btn-danger {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 11px 20px;
  border-radius: var(--radius-sm);
  border: none;
  font-size: .9rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background .15s, box-shadow .15s, transform .1s;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-light); box-shadow: var(--shadow); }
.btn-secondary { background: var(--surface2); color: var(--primary); border: 1.5px solid var(--border); }
.btn-secondary:hover { background: var(--border); }
.btn-ghost { background: transparent; color: var(--text-muted); }
.btn-ghost:hover { background: var(--surface2); }
.btn-danger { background: var(--danger); color: #fff; }
.btn-danger:hover { background: #a93226; }
.btn-primary svg, .btn-secondary svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-secondary svg { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.btn-add-operario-full { width: 100%; }

#btn-add-operario {
  align-self: flex-start;
  gap: 6px;
}
#btn-add-operario svg { width: 14px; height: 14px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; }

.form-actions {
  display: flex;
  gap: 10px;
  padding: 16px;
  background: var(--surface);
  border-top: 1px solid var(--border);
  position: sticky;
  bottom: 0;
  z-index: 50;
}
.form-actions .btn-primary, .form-actions .btn-secondary { flex: 1; }

/* === SIGNATURE === */
.signature-container {
  border: 2px dashed var(--border);
  border-radius: var(--radius-sm);
  overflow: hidden;
  background: #fafbfc;
}
#signature-canvas {
  display: block;
  width: 100%;
  height: 160px;
  cursor: crosshair;
  touch-action: none;
}
.signature-actions {
  display: flex;
  justify-content: flex-end;
  padding: 6px 10px;
  border-top: 1px solid var(--border);
  background: var(--surface2);
}

/* === DETAIL VIEW === */
.detail-body {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding-bottom: 32px;
}

.detail-section {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}

.detail-section h3 {
  font-size: .78rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .08em;
  color: var(--primary-light);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--surface2);
}

.detail-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-bottom: 10px;
}
.detail-row:last-child { margin-bottom: 0; }
.detail-row .dr-label { font-size: .75rem; font-weight: 600; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.detail-row .dr-value { font-size: .95rem; color: var(--text); word-break: break-word; white-space: pre-wrap; }

.detail-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 16px; }

.op-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--surface2);
  border-radius: 20px;
  padding: 5px 12px;
  font-size: .85rem;
  margin: 3px;
  color: var(--primary);
  font-weight: 500;
}
.op-chip .op-hours { color: var(--text-muted); font-size: .78rem; }

.sig-preview {
  max-width: 280px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  display: block;
  margin-top: 6px;
}

/* === TOAST === */
.toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%) translateY(20px);
  background: var(--text);
  color: #fff;
  padding: 10px 20px;
  border-radius: 24px;
  font-size: .9rem;
  z-index: 9999;
  opacity: 0;
  transition: opacity .25s, transform .25s;
  white-space: nowrap;
  max-width: 90vw;
  text-align: center;
  pointer-events: none;
}
.toast.show {
  opacity: 1;
  transform: translateX(-50%) translateY(0);
}
.toast.success { background: var(--success); }
.toast.error { background: var(--danger); }

/* === MODAL === */
.modal-overlay {
  position: fixed; inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 1000;
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 360px;
  width: 100%;
  box-shadow: var(--shadow-md);
}
.modal h3 { font-size: 1.1rem; margin-bottom: 8px; }
.modal p { color: var(--text-muted); font-size: .9rem; margin-bottom: 20px; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; }

/* === MATERIAL ROWS === */
.material-row {
  display: grid;
  grid-template-columns: 72px 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.material-row input { min-width: 0; }
.material-row input[type="number"] { padding: 9px 8px; }

/* === CLIENTE ROW (empresa nueva manual) === */
.cliente-row {
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--surface2);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px;
  margin-bottom: 8px;
  position: relative;
}
.cliente-row-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.cliente-row .btn-remove-op {
  position: absolute;
  top: 8px;
  right: 8px;
}
.btn-save-cliente {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  margin-top: 4px;
  padding: 9px 14px;
  background: var(--primary);
  color: #fff;
  border: none;
  border-radius: var(--radius-sm);
  font-size: .85rem;
  font-weight: 600;
  font-family: var(--font);
  cursor: pointer;
  transition: background .15s;
  width: 100%;
  justify-content: center;
}
.btn-save-cliente:hover { background: var(--primary-light); }
.btn-save-cliente svg { width: 15px; height: 15px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }

/* === LOGIN === */
#view-login.active {
  align-items: center;
  justify-content: center;
  background: var(--bg);
}
.login-container {
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  padding: 36px 28px 32px;
  width: 100%;
  max-width: 360px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}
.login-logo {
  width: 120px;
  object-fit: contain;
}
.login-title {
  font-size: 1.2rem;
  font-weight: 700;
  color: var(--primary);
  text-align: center;
}
.login-container form {
  width: 100%;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.login-btn {
  width: 100%;
  margin-top: 4px;
  padding: 13px;
  font-size: 1rem;
}
.login-error {
  color: var(--danger);
  font-size: .85rem;
  text-align: center;
  padding: 6px 10px;
  background: var(--danger-light);
  border-radius: var(--radius-sm);
}

/* === CHECKBOX LABEL === */
.checkbox-label {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  cursor: pointer;
  font-size: 1rem;
  color: var(--text);
  padding: 8px 0;
}
.checkbox-label input[type="checkbox"] {
  width: 20px;
  height: 20px;
  accent-color: var(--primary);
  cursor: pointer;
}

/* === HISTORY DROPDOWN === */
.history-dropdown {
  top: 100%;
  margin-top: 2px;
}

/* === LOADING OVERLAY === */
.loading-overlay {
  position: fixed; inset: 0;
  background: rgba(255,255,255,.85);
  z-index: 2000;
  display: flex; flex-direction: column;
  align-items: center; justify-content: center;
  gap: 16px;
}
.loading-spinner {
  width: 40px; height: 40px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.loading-overlay p { font-size: .9rem; color: var(--text-muted); }

/* === RESPONSIVE === */
@media (max-width: 400px) {
  .form-grid { grid-template-columns: 1fr; }
  .operario-row { grid-template-columns: 1fr 80px 80px auto; }
  .material-row { grid-template-columns: 60px 1fr auto; }
  .detail-grid { grid-template-columns: 1fr; }
}
