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

:root {
  --red:          #D32F26;
  --yellow:       #F4C20D;
  --bg-app:       #F4F2EF;
  --bg-card:      #FFFFFF;
  --bg-input:     #FBFAF8;
  --bg-metric:    #F4F2EF;
  --text-primary: #1C1B1A;
  --text-sec:     #6E6A64;
  --text-ter:     #8C867F;
  --text-label:   #9A948D;
  --border-card:  #ECE8E3;
  --border-input: #E4E0DA;
  --border-row:   #F0ECE7;
  --font-sans:    'IBM Plex Sans', system-ui, sans-serif;
  --font-head:    'Archivo', system-ui, sans-serif;
  --font-mono:    'IBM Plex Mono', 'Menlo', monospace;
  --r-input:      9px;
  --r-card:       16px;
  --r-block:      11px;
}

body {
  font-family: var(--font-sans);
  background: var(--bg-app);
  color: var(--text-primary);
  font-size: 14px;
  line-height: 1.5;
  min-height: 100vh;
  padding: 2rem 1rem;
}

.app {
  max-width: 760px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.mono { font-family: var(--font-mono); }

/* ── Top bar ───────────────────────────────────────────────────────────────── */

.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-bottom: 4px;
  gap: 12px;
}

.top-bar-brand {
  display: flex;
  align-items: center;
  gap: 12px;
}

.logo-chip {
  border-radius: 11px;
  border: 1px solid var(--border-card);
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  padding: 6px 10px;
}
.logo-chip img { width: 120px; height: 55px; object-fit: contain; }

.brand-name {
  font-family: var(--font-head);
  font-weight: 800;
  font-size: 17px;
  color: var(--text-primary);
  line-height: 1.2;
}
.brand-sub {
  font-size: 12.5px;
  color: var(--text-ter);
  margin-top: 1px;
}


/* ── Cards ─────────────────────────────────────────────────────────────────── */

.card {
  background: var(--bg-card);
  border: 1px solid var(--border-card);
  border-radius: var(--r-card);
  padding: 22px;
  box-shadow: 0 1px 2px rgba(0,0,0,0.03);
}

.card-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
}
.card-dot {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  background: var(--red);
  flex-shrink: 0;
}
.card-title {
  font-family: var(--font-head);
  font-size: 15px;
  font-weight: 700;
  color: var(--text-primary);
}
.card-header .badge {
  margin-left: auto;
  display: flex;
  gap: 6px;
  flex-shrink: 0;
}

/* Badge pills */
.badge-pill {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 11px;
  padding: 3px 8px;
  border-radius: 8px;
  background: var(--bg-app);
  color: var(--text-ter);
  border: 1px solid #E7E3DE;
}
.badge-input {
  border: none;
  background: transparent;
  font-size: 11px;
  color: var(--text-ter);
  font-family: var(--font-mono);
  outline: none;
  padding: 0;
  cursor: text;
}
.badge-input:focus { color: var(--text-primary); }
#invoice-number-input { width: 28px; }
#date-input           { width: 104px; }

/* ── Form fields ───────────────────────────────────────────────────────────── */

.field-row {
  display: flex;
  gap: 10px;
  margin-bottom: 10px;
}
.field-grow { flex: 1; }
.field-nif  { width: 170px; flex-shrink: 0; }

.field {
  display: flex;
  flex-direction: column;
  gap: 5px;
}
.field label {
  font-size: 11.5px;
  color: var(--text-ter);
  font-weight: 600;
  letter-spacing: 0.03em;
}
.field input,
.field textarea {
  border-radius: var(--r-input);
  border: 1px solid var(--border-input);
  background: var(--bg-input);
  padding: 0 13px;
  font-size: 14px;
  color: var(--text-primary);
  font-family: var(--font-sans);
  outline: none;
  transition: border-color 0.15s, background 0.15s;
  width: 100%;
}
.field input { height: 38px; }
.field textarea {
  padding: 10px 13px;
  resize: vertical;
  line-height: 1.5;
  min-height: 62px;
  max-height: 120px;
  overflow-y: auto;
}
.field input:focus,
.field textarea:focus {
  border-color: var(--red);
  background: #fff;
}
.field input::placeholder,
.field textarea::placeholder { color: #C4BEB6; }
.field input.field-invalid    { border-color: var(--red); }
.field input.mono             { font-family: var(--font-mono); }

/* ── Line items ────────────────────────────────────────────────────────────── */

.line-headers {
  display: grid;
  grid-template-columns: 1fr 56px 78px 86px 26px;
  gap: 9px;
  padding-bottom: 8px;
  margin-bottom: 4px;
  border-bottom: 1px solid var(--border-row);
}
.line-headers span {
  font-size: 11px;
  color: var(--text-label);
  font-weight: 600;
}

.line-item {
  display: grid;
  grid-template-columns: 1fr 56px 78px 86px 26px;
  gap: 9px;
  align-items: center;
  margin-bottom: 6px;
}
.line-item input {
  height: 34px;
  border-radius: var(--r-input);
  border: 1px solid var(--border-input);
  background: var(--bg-input);
  padding: 0 8px;
  font-size: 13px;
  color: var(--text-primary);
  font-family: var(--font-sans);
  outline: none;
  width: 100%;
  transition: border-color 0.15s, background 0.15s;
}
.line-item input:focus { border-color: var(--red); background: #fff; }
.line-item input::placeholder { color: #C4BEB6; }
.line-item input.mono {
  font-family: var(--font-mono);
  font-size: 13px;
}
.line-item input[type="number"] { text-align: center; }
.line-item .line-price          { text-align: right; }
.line-item input[readonly] {
  font-family: var(--font-mono);
  font-weight: 600;
  font-size: 13px;
  text-align: right;
  color: var(--text-primary);
  background: transparent;
  border-color: transparent;
  cursor: default;
}

.btn-remove {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--border-card);
  background: none;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #C4BEB6;
  transition: color 0.15s, background 0.15s, border-color 0.15s;
  flex-shrink: 0;
}
.btn-remove:hover { color: var(--red); background: #FBEBEA; border-color: var(--red); }
.btn-remove svg { width: 12px; height: 12px; }

.btn-add-line {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  padding: 10px 14px;
  margin-top: 6px;
  font-family: var(--font-sans);
  font-size: 13px;
  color: #5C5750;
  cursor: pointer;
  background: none;
  border: 1.5px dashed #D8D3CC;
  border-radius: var(--r-input);
  transition: color 0.15s, border-color 0.15s;
}
.btn-add-line:hover { color: var(--red); border-color: var(--red); }

/* ── Extras row ────────────────────────────────────────────────────────────── */

.extras-row {
  display: flex;
  gap: 10px;
  margin-top: 16px;
  padding-top: 16px;
  border-top: 1px solid var(--border-row);
}
.extras-row .field-nif { width: 180px; flex-shrink: 0; }

/* ── Totals ────────────────────────────────────────────────────────────────── */

.totals-grid {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.metric {
  background: var(--bg-metric);
  border-radius: var(--r-block);
  padding: 13px 15px;
}
.metric .m-label {
  font-size: 11.5px;
  color: var(--text-ter);
  margin-bottom: 4px;
}
.metric .m-value {
  font-size: 16px;
  font-weight: 600;
  color: var(--text-primary);
}
.metric-total {
  background: var(--red);
}
.metric-total .m-label { color: rgba(255,255,255,0.82); }
.metric-total .m-value { color: #fff; font-weight: 700; }

/* ── Action buttons ────────────────────────────────────────────────────────── */

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 40px;
  border-radius: 10px;
  font-size: 13px;
  font-weight: 600;
  font-family: var(--font-sans);
  cursor: pointer;
  border: none;
  white-space: nowrap;
  transition: background 0.15s, border-color 0.15s, opacity 0.15s;
}
.btn svg { width: 16px; height: 16px; flex-shrink: 0; }
.btn-secondary {
  flex: 1 1 120px;
  background: #fff;
  border: 1px solid var(--border-input);
  color: #5C5750;
}
.btn-secondary:hover { border-color: #C4BEB6; background: var(--bg-input); }
.btn-primary {
  flex: 2 1 160px;
  background: var(--text-primary);
  color: #fff;
}
.btn-primary:hover { background: #000; }

.footer-note {
  text-align: center;
  font-size: 11.5px;
  color: #A8A29A;
  margin-top: 12px;
}

/* ── Responsive ────────────────────────────────────────────────────────────── */

@media (max-width: 540px) {
  .top-bar         { flex-wrap: wrap; }
  .field-row       { flex-direction: column; }
  .field-nif       { width: 100%; }
  .extras-row      { flex-direction: column; }
  .extras-row .field-nif { width: 100%; }
  .totals-grid     { grid-template-columns: 1fr; }
  .line-headers,
  .line-item       { grid-template-columns: 1fr 44px 70px 72px 26px; }

  /* Secondary buttons share one row; primary takes the full second row */
  .btn-secondary   { flex: 1 1 0; }
  .btn-primary     { flex: 1 1 100%; order: 3; }
}
