/* ---------- Design tokens ---------- */
:root {
  --bg: #f4f4f2;
  --surface: #ffffff;
  --ink: #1a1a1a;
  --ink-2: #2c2c2c;
  --muted: #8a8a8a;
  --muted-2: #6b6b6b;
  --line: #e6e5e1;
  --line-2: #d4d3cd;
  --accent-link: #2a6fc7;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow-sm: 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 8px 24px -12px rgba(0,0,0,0.12);
  --font-sans: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}
button, input, textarea, select { font: inherit; color: inherit; }

/* ---------- App shell ---------- */
.app-header {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
  padding: 12px 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { width: 34px; height: 34px; color: var(--ink); flex-shrink: 0; }
.brand h1 { margin: 0; font-size: 16px; font-weight: 600; letter-spacing: -0.01em; }
.brand .subtitle { margin: 0; font-size: 12px; color: var(--muted-2); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: flex-end; }
.actions .btn { white-space: nowrap; }
@media (max-width: 640px) {
  .brand .subtitle { display: none; }
}

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 9px 14px;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.15s ease;
  background: none;
  text-decoration: none;
}
.btn-primary { background: var(--ink); color: #fff; border-color: var(--ink); }
.btn-primary:hover { background: #000; }
.btn-ghost { background: transparent; color: var(--ink); border-color: var(--line-2); }
.btn-ghost:hover { background: var(--bg); }
.btn-sm { padding: 6px 10px; font-size: 12px; }
.btn-add {
  width: 100%;
  justify-content: center;
  margin-top: 10px;
  border-style: dashed;
}
.icon-btn {
  background: transparent;
  border: none;
  color: var(--muted);
  cursor: pointer;
  padding: 6px;
  border-radius: 4px;
  transition: all 0.15s ease;
}
.icon-btn:hover { color: #c8361c; background: rgba(200,54,28,0.08); }

/* ---------- Main layout ---------- */
.app-main {
  display: grid;
  grid-template-columns: minmax(440px, 1fr) minmax(560px, 1.15fr);
  gap: 24px;
  padding: 24px 28px;
  max-width: 1700px;
  margin: 0 auto;
  align-items: start;
}
@media (max-width: 1024px) {
  .app-main { grid-template-columns: 1fr; }
}

/* Mobile layout rules live at the end of the file (see bottom) so they
   take precedence over later base rules with equal specificity. */

/* ---------- Editor ---------- */
.editor { display: flex; flex-direction: column; gap: 16px; }
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 18px;
}
.card-title {
  margin: 0 0 14px 0;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.05em;
  text-transform: uppercase;
  color: var(--muted-2);
}

.grid-2 {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}
[hidden] { display: none !important; }
.field { display: flex; flex-direction: column; gap: 6px; }
.field.full { grid-column: 1 / -1; }
.field label {
  font-size: 12px;
  font-weight: 500;
  color: var(--ink-2);
}
.field input, .field textarea, .field select {
  width: 100%;
  padding: 9px 11px;
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  font-size: 13px;
  color: var(--ink);
  outline: none;
  transition: border-color 0.15s ease, background 0.15s ease;
  resize: vertical;
  font-family: inherit;
}
.field input:focus, .field textarea:focus {
  border-color: var(--ink);
  background: var(--surface);
}
.field-hint {
  font-size: 11px;
  color: var(--muted);
  margin: 4px 0 0;
}

/* Company tabs */
.company-tabs {
  display: flex;
  gap: 4px;
  padding: 4px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
}
.company-tab {
  flex: 1;
  padding: 8px 10px;
  background: transparent;
  border: none;
  border-radius: 4px;
  font-size: 12px;
  font-weight: 500;
  color: var(--muted-2);
  cursor: pointer;
  transition: all 0.15s ease;
}
.company-tab:hover { color: var(--ink); }
.company-tab.active {
  background: var(--surface);
  color: var(--ink);
  box-shadow: var(--shadow-sm);
}

/* Logo uploader */
.logo-uploader {
  display: flex;
  gap: 14px;
  align-items: center;
  padding: 12px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
}
.logo-preview {
  width: 72px;
  height: 72px;
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  background: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  flex-shrink: 0;
}
.logo-preview img { max-width: 100%; max-height: 100%; object-fit: contain; }
.logo-preview .logo-placeholder {
  font-size: 10px;
  color: var(--muted);
  letter-spacing: 0.08em;
}
.logo-controls { display: flex; flex-direction: column; gap: 6px; align-items: flex-start; }
.logo-controls > div:first-of-type { display: flex; gap: 6px; }
.logo-controls .field-hint { margin: 2px 0 0; }
.logo-controls label.btn { cursor: pointer; }

/* Items */
.items-header {
  display: grid;
  grid-template-columns: 1fr 100px 60px 100px 32px;
  gap: 8px;
  padding: 0 4px 8px 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: var(--muted-2);
  border-bottom: 1px solid var(--line);
}
.item-row {
  display: grid;
  grid-template-columns: 1fr 100px 60px 100px 32px;
  gap: 8px;
  padding: 8px 0;
  align-items: start;
  border-bottom: 1px solid var(--line);
}
.item-row:last-child { border-bottom: none; }
.item-row textarea, .item-row input {
  padding: 7px 9px;
  background: var(--bg);
  border: 1px solid transparent;
  border-radius: 4px;
  font-size: 13px;
  outline: none;
  transition: all 0.15s ease;
  font-family: inherit;
  width: 100%;
  resize: vertical;
  min-height: 36px;
}
.item-row textarea { min-height: 36px; }
.item-row input:focus, .item-row textarea:focus {
  border-color: var(--ink);
  background: var(--surface);
}
.item-amount {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  text-align: right;
  padding: 8px 6px 0 0;
  color: var(--ink-2);
  font-size: 13px;
}
.item-row .icon-btn { margin-top: 6px; }

/* ---------- Preview ---------- */
.preview-wrap {
  position: sticky;
  top: 84px;
}
.preview-scroll {
  max-height: calc(100vh - 108px);
  overflow-y: auto;
  border-radius: var(--radius);
}

/* ---------- Invoice (sample-matching) ---------- */
.invoice {
  background: #fff;
  color: #1a1a1a;
  padding: 48px 52px 56px;
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  font-size: 12px;
  line-height: 1.55;
  font-family: var(--font-sans);
}

/* Top band */
.inv-top {
  display: grid;
  grid-template-columns: 170px 1fr 150px;
  gap: 24px;
  align-items: start;
  padding-bottom: 22px;
}
.inv-logo-box {
  width: 170px;
  height: 130px;
  background: transparent;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
}
.inv-logo-box img { max-width: 100%; max-height: 100%; object-fit: contain; }
.inv-logo-box .logo-placeholder {
  font-size: 11px;
  color: #b8b8b8;
  letter-spacing: 0.15em;
}

.inv-company { padding-top: 2px; }
.inv-company-name {
  font-size: 19px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 8px;
  letter-spacing: -0.01em;
}
.inv-company-line {
  font-size: 12px;
  color: #2c2c2c;
  line-height: 1.5;
}
.inv-company-address { white-space: pre-line; }
.inv-company-line:empty { display: none; }
.inv-company-link {
  font-size: 12px;
  color: var(--accent-link);
  text-decoration: underline;
  display: block;
  word-break: break-all;
}
.inv-company-link:empty { display: none; }

.inv-meta {
  text-align: right;
  padding-top: 2px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.inv-meta-row {
  display: flex;
  flex-direction: column;
  gap: 2px;
  align-items: flex-end;
}
.inv-meta-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  color: #8a8a8a;
  text-transform: uppercase;
}
.inv-meta-value {
  font-size: 13px;
  font-weight: 500;
  color: #1a1a1a;
}
.inv-meta-balance .inv-meta-value {
  font-weight: 600;
}

/* Divider */
.inv-divider {
  height: 1px;
  background: #e1e0dc;
  margin: 6px 0 22px;
}

/* Bill to */
.inv-billto {
  margin-bottom: 28px;
}
.inv-section-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: #8a8a8a;
  text-transform: uppercase;
  margin-bottom: 10px;
}
.inv-client-name {
  font-size: 16px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 10px;
  letter-spacing: -0.01em;
}
.inv-client-line {
  font-size: 12px;
  color: #2c2c2c;
  line-height: 1.55;
}
.inv-client-line:empty { display: none; }

/* Items table */
.inv-items {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 24px;
}
.inv-items thead th {
  padding: 12px 8px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.1em;
  color: #8a8a8a;
  text-transform: uppercase;
  border-top: 1px solid #d4d3cd;
  border-bottom: 1px solid #d4d3cd;
  text-align: left;
}
.inv-items th.col-rate, .inv-items th.col-qty, .inv-items th.col-amt,
.inv-items td.col-rate, .inv-items td.col-qty, .inv-items td.col-amt {
  text-align: right;
}
.inv-items th.col-desc { padding-left: 4px; }
.inv-items th.col-amt { padding-right: 4px; }
.inv-items tbody td {
  padding: 14px 8px;
  border-bottom: 1px solid #eceae4;
  vertical-align: top;
  font-size: 12px;
  color: #1a1a1a;
}
.inv-items tbody td.col-desc { padding-left: 4px; white-space: pre-line; line-height: 1.6; }
.inv-items tbody td.col-amt { padding-right: 4px; font-variant-numeric: tabular-nums; font-weight: 500; }
.inv-items tbody td.col-rate { font-variant-numeric: tabular-nums; }
.inv-items tbody tr:last-child td { border-bottom: none; }
.inv-items .col-rate { width: 110px; }
.inv-items .col-qty { width: 60px; }
.inv-items .col-amt { width: 120px; }
.inv-items .empty-row td {
  padding: 20px 0;
  color: #8a8a8a;
  font-style: italic;
  text-align: center;
}

/* Bottom: payment + totals */
.inv-bottom {
  display: grid;
  grid-template-columns: 1fr 320px;
  gap: 32px;
  margin-top: 8px;
  padding-top: 12px;
  border-top: 1px solid #d4d3cd;
}
.inv-payment-title {
  font-size: 17px;
  font-weight: 600;
  color: #1a1a1a;
  margin-bottom: 14px;
  letter-spacing: -0.01em;
}
.inv-payment-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: #8a8a8a;
  text-transform: uppercase;
  margin-bottom: 8px;
}
.inv-payment-line {
  font-size: 12px;
  color: #1a1a1a;
  line-height: 1.7;
}
.inv-payment-line:empty { display: none; }
.inv-payment-method { margin-top: 14px; }
.inv-payment-method-label { margin-bottom: 6px; }
.inv-payment-method:has(.inv-payment-line:empty) { display: none; }

/* Totals */
.inv-totals {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.inv-total-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding-bottom: 14px;
  border-bottom: 1px solid #d4d3cd;
  font-size: 12px;
}
.inv-total-row span:first-child {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: #8a8a8a;
  text-transform: uppercase;
}
.inv-total-row span:last-child {
  font-variant-numeric: tabular-nums;
  font-weight: 500;
  color: #1a1a1a;
}
.inv-balance {
  text-align: right;
}
.inv-balance-label {
  font-size: 10px;
  font-weight: 600;
  letter-spacing: 0.12em;
  color: #8a8a8a;
  text-transform: uppercase;
  margin-bottom: 6px;
}
.inv-balance-value {
  font-size: 20px;
  font-weight: 700;
  color: #1a1a1a;
  font-variant-numeric: tabular-nums;
  letter-spacing: -0.01em;
}

/* Other */
.inv-other {
  margin-top: 40px;
  padding-top: 22px;
  border-top: 1px solid #e1e0dc;
}
.inv-other-text {
  font-size: 12px;
  color: #2c2c2c;
  white-space: pre-line;
  line-height: 1.6;
}

/* ---------- Scrollbar polish ---------- */
.preview-scroll::-webkit-scrollbar { width: 8px; }
.preview-scroll::-webkit-scrollbar-track { background: transparent; }
.preview-scroll::-webkit-scrollbar-thumb { background: var(--line-2); border-radius: 4px; }
.preview-scroll::-webkit-scrollbar-thumb:hover { background: var(--muted); }

/* PDF render tweaks */
.invoice.pdf-render {
  box-shadow: none;
  border-radius: 0;
}

/* ==========================================================
   Modal / Overlay
   ========================================================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 20, 22, 0.55);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  overflow-y: auto;
}
.modal-overlay[hidden] { display: none; }

.modal {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  width: 100%;
  max-width: 620px;
  padding: 28px 28px 20px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.18);
  max-height: calc(100vh - 48px);
  overflow-y: auto;
}
.modal-sm { max-width: 400px; }

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
}

.modal-title {
  font-size: 20px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--ink);
  margin: 0 0 6px;
}
.modal-sub {
  color: var(--muted);
  margin: 0 0 18px;
  font-size: 13.5px;
}

.btn-block { width: 100%; margin-top: 8px; }

.login-error {
  color: #b3261e;
  background: #fdecea;
  border: 1px solid #f5c2bc;
  padding: 8px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 10px;
}

.settings-section {
  padding: 18px 0;
  border-top: 1px solid var(--line-soft);
}
.settings-section:first-of-type { border-top: 0; padding-top: 4px; }
.settings-section h3 {
  font-size: 14px;
  font-weight: 600;
  color: var(--ink);
  margin: 0 0 10px;
  letter-spacing: -0.005em;
}
.settings-section .btn { margin-top: 6px; }
.admin-section { background: rgba(255, 200, 60, 0.06); border: 1px solid rgba(255, 200, 60, 0.25); border-radius: 10px; padding: 14px 16px; margin-top: 14px; }
.admin-section h3 { display: flex; align-items: center; gap: 8px; }
.admin-tag { font-size: 10px; font-weight: 600; letter-spacing: 0.04em; text-transform: uppercase; background: rgba(200, 120, 0, 0.18); color: #8a5500; padding: 2px 7px; border-radius: 999px; }
.admin-company-tabs { flex-wrap: wrap; }

/* ==========================================================
   Client autocomplete
   ========================================================== */
.client-field { position: relative; }
.client-suggestions {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  margin-top: 4px;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.08);
  max-height: 240px;
  overflow-y: auto;
  z-index: 10;
}
.client-suggestions[hidden] { display: none; }
.client-suggestion {
  padding: 10px 14px;
  cursor: pointer;
  border-bottom: 1px solid var(--line-soft);
  font-size: 13.5px;
}
.client-suggestion:last-child { border-bottom: 0; }
.client-suggestion:hover,
.client-suggestion.active { background: var(--bg); }
.client-suggestion-name { font-weight: 600; color: var(--ink); }
.client-suggestion-meta { font-size: 12px; color: var(--muted); margin-top: 2px; }

/* Log list */
.log-list {
  max-height: 260px;
  overflow-y: auto;
  border: 1px solid var(--line-soft);
  border-radius: 8px;
}
.log-row {
  display: grid;
  grid-template-columns: 90px 1fr 1fr auto;
  gap: 10px;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line-soft);
  font-size: 12.5px;
  align-items: center;
}
.log-row:last-child { border-bottom: 0; }
.log-row .log-date { color: var(--muted); font-variant-numeric: tabular-nums; }
.log-row .log-num { font-weight: 600; color: var(--ink); }
.log-row .log-client { color: var(--ink); }
.log-row .log-total { color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; text-align: right; }
.log-empty { padding: 14px; text-align: center; color: var(--muted); font-size: 13px; }

/* ========================================================
   LOGIN — dark theme (scoped to login overlay only)
   ======================================================== */
.login-overlay-dark { background: #000; }
.login-modal-dark {
  background: #0a0a0a;
  color: #fff;
  border: 1px solid #1f1f1f;
  box-shadow: 0 20px 60px rgba(0,0,0,0.5);
  padding: 28px 24px;
  max-width: 360px;
  width: 92%;
}
.login-logo {
  display: block;
  width: 100%;
  max-width: 220px;
  height: auto;
  margin: 0 auto 14px;
}
.login-modal-dark .modal-sub {
  color: #b8b8b8;
  text-align: center;
  font-size: 13px;
  margin-bottom: 18px;
}
.login-modal-dark .field label {
  color: #d4d4d4;
  font-size: 12px;
  font-weight: 500;
}
.login-modal-dark .field input {
  background: #1a1a1a;
  color: #fff;
  border: 1px solid #333;
  border-radius: 6px;
  padding: 11px 13px;
  font-size: 15px;
  transition: border-color 0.15s, background 0.15s;
  /* Prevent iOS auto-zoom on focus */
  -webkit-appearance: none;
  appearance: none;
}
.login-modal-dark .field input::placeholder { color: #666; }
.login-modal-dark .field input:focus {
  outline: none;
  border-color: #fff;
  background: #141414;
}
/* Autofill background fix — browsers force yellow by default */
.login-modal-dark .field input:-webkit-autofill,
.login-modal-dark .field input:-webkit-autofill:hover,
.login-modal-dark .field input:-webkit-autofill:focus {
  -webkit-text-fill-color: #fff;
  -webkit-box-shadow: 0 0 0 1000px #1a1a1a inset;
  caret-color: #fff;
}
.login-modal-dark .off-network-tag {
  color: #f5b949;
  font-weight: 500;
  font-size: 11px;
}
.login-modal-dark .off-network-hint {
  color: #a0a0a0;
  margin-top: 6px;
  font-size: 12px;
}
.login-modal-dark .login-error {
  color: #ff6b6b;
  background: rgba(255,107,107,0.08);
  border: 1px solid rgba(255,107,107,0.2);
  padding: 9px 12px;
  border-radius: 6px;
  font-size: 13px;
  margin: 10px 0;
}
/* Wrap the button so we can center a narrow pill inside the form */
.login-modal-dark .login-btn-dark {
  background: #fff;
  color: #000;
  border: none;
  padding: 10px 28px;
  font-weight: 600;
  font-size: 14px;
  border-radius: 6px;
  margin: 10px auto 0;
  cursor: pointer;
  transition: background 0.15s, transform 0.1s;
  display: flex;
  align-items: center;
  justify-content: center;
  width: auto;
  min-width: 140px;
}
.login-btn-dark:hover { background: #e8e8e8; }
.login-btn-dark:active { transform: translateY(1px); }

/* ========================================================
   MOBILE (phones) — kept at end so it wins over base rules
   ======================================================== */
@media (max-width: 640px) {
  html, body { overflow-x: hidden; max-width: 100vw; }

  .app-main {
    grid-template-columns: minmax(0, 1fr);
    padding: 14px 12px;
    gap: 16px;
  }
  .card { padding: 14px; }

  /* Header */
  .app-header { padding: 8px 12px; gap: 8px; }
  .brand h1 { font-size: 14px; }
  .brand .subtitle { display: none; }
  .logo { width: 28px; height: 28px; }
  .actions { gap: 6px; width: auto; flex-wrap: wrap; justify-content: flex-end; }
  .actions .btn { padding: 7px 10px; font-size: 12px; }
  .actions .btn-primary { flex: 1 1 100%; order: 99; justify-content: center; }

  /* Two-column grids collapse to one column */
  .grid-2 { grid-template-columns: 1fr !important; gap: 10px; }
  .field input, .field textarea, .field select { width: 100%; max-width: 100%; box-sizing: border-box; }

  /* Company tabs: single row, equal width, no overflow */
  .company-tabs { gap: 2px; padding: 3px; }
  .company-tab {
    padding: 7px 4px;
    font-size: 11px;
    flex: 1 1 0;
    min-width: 0;
    text-overflow: ellipsis;
    overflow: hidden;
    white-space: nowrap;
  }

  /* Items editor: stack description full width, rate+qty on next line */
  .items-header { display: none; }
  .item-row {
    display: grid !important;
    grid-template-columns: 1fr auto !important;
    grid-template-areas:
      "desc   remove"
      "rate   qty"
      "amount amount" !important;
    gap: 8px !important;
    padding: 12px 0 !important;
    align-items: center;
  }
  .item-row > textarea[data-k="description"] { grid-area: desc; min-height: 60px; width: 100%; box-sizing: border-box; }
  .item-row > input[data-k="rate"] { grid-area: rate; width: 100%; box-sizing: border-box; }
  .item-row > input[data-k="qty"]  { grid-area: qty;  width: 100%; box-sizing: border-box; }
  .item-row > .item-amount { grid-area: amount; text-align: right; font-weight: 600; font-size: 14px; padding-top: 4px; border-top: 1px dashed var(--line); }
  .item-row > .icon-btn { grid-area: remove; margin-top: 0; justify-self: end; }

  /* Rate/qty labels using ::before so users know which is which */
  .item-row > input[data-k="rate"]::placeholder { color: var(--muted); }
  .item-row > input[data-k="qty"]::placeholder  { color: var(--muted); }

  /* Preview: not sticky, invoice shrinks to fit */
  .preview-wrap { position: static; }
  .preview-scroll { max-height: none; overflow: visible; }
  .invoice {
    padding: 20px 16px 24px !important;
    font-size: 10.5px;
    width: 100%;
    box-sizing: border-box;
    overflow: hidden;
  }

  /* Top band: logo + company info stack, meta block goes below */
  .inv-top {
    display: grid !important;
    grid-template-columns: 84px 1fr !important;
    gap: 10px !important;
    padding-bottom: 14px !important;
  }
  .inv-logo-box { width: 84px !important; height: 60px !important; }
  .inv-company { padding-top: 0 !important; min-width: 0; }
  .inv-company-name { font-size: 14px !important; }
  .inv-company-line { font-size: 10px !important; word-break: break-word; }
  .inv-company-link { font-size: 10px !important; word-break: break-all; }

  /* Meta (invoice #, date, due, balance) wraps below logo+company as a full-width row */
  .inv-meta {
    grid-column: 1 / -1 !important;
    display: grid !important;
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 8px 14px !important;
    text-align: left !important;
    border-top: 1px solid #e5e4de;
    padding-top: 12px;
    margin-top: 4px;
  }
  .inv-meta-row { display: flex !important; flex-direction: column !important; gap: 2px !important; }
  .inv-meta-label { font-size: 9px !important; }
  .inv-meta-value { font-size: 12px !important; }
  .inv-meta-balance .inv-meta-value { font-size: 13px !important; }

  /* Invoice items table scales down and wraps description */
  .inv-items { font-size: 10px; table-layout: fixed; width: 100%; }
  .inv-items thead th { padding: 8px 2px !important; font-size: 8px !important; letter-spacing: 0.06em !important; }
  .inv-items tbody td { padding: 8px 2px !important; font-size: 10px !important; word-break: break-word; }
  .inv-items .col-rate { width: 52px !important; }
  .inv-items .col-qty  { width: 28px !important; }
  .inv-items .col-amt  { width: 62px !important; }

  /* Totals */
  .inv-totals { font-size: 11px !important; }
  .inv-totals-row { gap: 10px !important; }

  /* Payment / bank blocks */
  .inv-payment { font-size: 10px !important; }
  .inv-payment strong, .inv-payment b { font-size: 11px !important; }
}

/* ---------- Products admin (Settings) ---------- */
.products-list { display: flex; flex-direction: column; gap: 6px; margin-bottom: 10px; }
.product-row {
  display: grid;
  grid-template-columns: 1fr 120px auto auto;
  gap: 8px;
  align-items: center;
  padding: 8px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
}
.product-row input {
  padding: 7px 9px;
  background: var(--surface);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  font-size: 13px;
}
.product-row .icon-btn { color: var(--muted); }
.product-row .icon-btn:hover { color: #c8361c; background: rgba(200,54,28,0.08); }

.product-add-row {
  display: grid;
  grid-template-columns: 1fr 120px auto;
  gap: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--line);
}
.product-add-row input {
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  font-size: 13px;
}

/* Catalog picker inside Items */
.item-row select.item-picker {
  grid-column: 1 / -1;
  padding: 8px 10px;
  background: var(--bg);
  border: 1px solid var(--line-2);
  border-radius: var(--radius-sm);
  font-size: 13px;
  margin-bottom: 4px;
  cursor: pointer;
}

@media (max-width: 640px) {
  .product-row { grid-template-columns: 1fr auto; grid-template-areas: "name delete" "price save"; }
  .product-row .product-name { grid-area: name; }
  .product-row .product-price { grid-area: price; }
  .product-row .product-save { grid-area: save; justify-self: end; }
  .product-row .product-delete { grid-area: delete; justify-self: end; }

  .product-add-row { grid-template-columns: 1fr 1fr; grid-template-areas: "name name" "price add"; }
  .product-add-row #newProductName { grid-area: name; }
  .product-add-row #newProductPrice { grid-area: price; }
  .product-add-row #addProductBtn { grid-area: add; }

  .item-row select.item-picker { grid-column: 1 / -1 !important; grid-area: picker; }
  .item-row {
    grid-template-areas:
      "picker picker"
      "desc   remove"
      "rate   qty"
      "amount amount" !important;
  }
}

/* Hide invoice preview on phones — users download the PDF instead */
@media (max-width: 640px) {
  .preview-wrap { display: none !important; }
}

/* Mobile: compact Invoice Details card */
@media (max-width: 640px) {
  .card { padding: 12px; }
  .card-title { font-size: 11px !important; }
  .field label { font-size: 11px; }
  .field input, .field textarea, .field select {
    padding: 7px 9px;
    font-size: 13px;
  }
  .grid-2 { gap: 8px !important; }
  .editor { gap: 10px !important; }
}

/* Normalize date input — iOS Safari adds extra internal padding that makes it taller */
input[type="date"] {
  -webkit-appearance: none;
  appearance: none;
  min-height: 0;
  height: auto;
  line-height: 1.3;
  background-clip: padding-box;
}
input[type="date"]::-webkit-date-and-time-value {
  text-align: left;
  min-height: 0;
  line-height: 1.3;
}
input[type="date"]::-webkit-calendar-picker-indicator {
  padding: 0;
  margin: 0;
}

@media (max-width: 640px) {
  input[type="date"] {
    padding: 7px 9px;
    font-size: 13px;
  }
}
