/* ============================================================
   TOKENS
   ============================================================ */
:root{
  --ink: #111111;
  --ink-soft: #3a3a3a;
  --muted: #6f6f6c;
  --line: #e4e3df;
  --line-strong: #cfceca;
  --paper: #ffffff;
  --panel: #fbfbfa;
  --accent: #14532d;      /* used sparingly: paid state */
  --accent-warn: #7a2b0a; /* used sparingly: unpaid/overdue state */
  --radius: 2px;
  --font-display: 'Space Grotesk', sans-serif;
  --font-body: 'Inter', sans-serif;
  --font-mono: 'JetBrains Mono', monospace;
}

*{ box-sizing: border-box; }

html{
  /* FIX 1: stop sideways scroll caused by any child that slightly
     overflows the viewport (sticky panels, wide tables, etc). */
  overflow-x: hidden;
}

html,body{
  margin:0;
  padding:0;
  width: 100%;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-font-smoothing: antialiased;
}

h1,h2,h3,h4{ font-family: var(--font-display); margin:0; }

button, input, select, textarea{ font-family: inherit; }

img{ max-width: 100%; height: auto; } /* FIX 2: any image (incl. logo) scales, never overflows */

/* ============================================================
   TOP BAR
   ============================================================ */
.topbar{
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  background: rgba(255,255,255,0.92);
  backdrop-filter: blur(6px);
  z-index: 50;
}
.topbar-inner{
  max-width: 1360px;
  margin: 0 auto;
  padding: 14px 28px;
  display:flex;
  align-items:center;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}
.brand{ display:flex; align-items:center; gap:12px; min-width: 0; }
.brand-mark{
  width: 34px; height: 34px;
  flex-shrink: 0;
  border: 1px solid var(--ink);
  display:flex; align-items:center; justify-content:center;
  font-family: var(--font-mono);
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 0.5px;
}
.brand-text{ display:flex; flex-direction: column; line-height: 1.15; min-width: 0; }
.brand-text strong{
  font-family: var(--font-display); font-size: 15px; letter-spacing: -0.01em;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap; /* FIX 3: long brand names truncate cleanly */
}
.brand-text span{ font-size: 11px; color: var(--muted); }

.topbar-actions{ display:flex; gap: 8px; flex-wrap: wrap; }

.btn{
  border: 1px solid var(--ink);
  background: var(--paper);
  color: var(--ink);
  padding: 9px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  border-radius: var(--radius);
  transition: transform 0.12s ease, background 0.12s ease, color 0.12s ease;
  min-height: 40px; /* FIX 4: keeps tap targets close to the ~40-44px touch minimum */
}
.btn:hover{ transform: translateY(-1px); }
.btn:active{ transform: translateY(0); }
.btn-ghost{ background: transparent; border-color: var(--line-strong); color: var(--ink-soft); }
.btn-ghost:hover{ border-color: var(--ink); color: var(--ink); }
.btn-dark{ background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn-dark:hover{ background: #000; }
.btn-solid{ background: var(--ink); color: var(--paper); border-color: var(--ink); }
.btn-solid:hover{ background: #000; }

/* ============================================================
   HERO
   ============================================================ */
.hero{
  max-width: 1360px;
  margin: 0 auto;
  padding: 56px 28px 32px;
}
.hero h1{
  font-size: clamp(28px, 3.4vw, 42px);
  line-height: 1.08;
  max-width: 720px;
  letter-spacing: -0.015em;
}
.hero p{
  margin: 16px 0 22px;
  max-width: 560px;
  color: var(--muted);
  font-size: 15px;
  line-height: 1.55;
}
.hero-tags{ display:flex; flex-wrap: wrap; gap: 8px; }
.hero-tags span{
  font-family: var(--font-mono);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  border: 1px solid var(--line-strong);
  padding: 6px 10px;
  color: var(--ink-soft);
}

/* ============================================================
   WORKSPACE GRID
   ============================================================ */
.workspace{
  max-width: 1360px;
  margin: 0 auto;
  padding: 0 28px 64px;
  display: grid;
  grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
  gap: 32px;
  align-items: start;
}

.panel{ min-width: 0; }

/* ---------- FORM ---------- */
.field-card{
  border: 1px solid var(--line);
  padding: 22px;
  margin-bottom: 18px;
  background: var(--panel);
}
.field-card h2{
  font-size: 14px;
  font-weight: 600;
  display:flex;
  align-items:center;
  gap: 10px;
  margin-bottom: 16px;
  letter-spacing: -0.01em;
}
.field-card h2 .num{
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  border: 1px solid var(--line-strong);
  padding: 2px 6px;
}

label{
  display:flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 14px;
  font-weight: 500;
}
label:last-child{ margin-bottom: 0; }

input[type="text"], input[type="date"], input[type="number"], select, textarea{
  border: none;
  border-bottom: 1px solid var(--line-strong);
  background: transparent;
  padding: 8px 2px;
  font-size: 14px;
  color: var(--ink);
  outline: none;
  resize: vertical;
  font-family: var(--font-body);
  width: 100%; /* FIX 5: guarantees inputs never exceed their container on narrow screens */
}
input:focus, select:focus, textarea:focus{
  border-bottom: 1.5px solid var(--ink);
}
input[type="file"]{
  font-size: 12px;
  padding: 6px 0;
}

.row{ display:grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.row-3{ grid-template-columns: 1fr 1fr 1fr; }
.row-4{ grid-template-columns: repeat(4, 1fr); }

/* FIX 6: row-4 used to jump straight from 4 columns to 1 at 560px,
   cramming fields together on tablets. Add a 2-column mid-step. */
@media (max-width: 760px){
  .row-4{ grid-template-columns: 1fr 1fr; }
}
@media (max-width: 560px){
  .row, .row-3, .row-4{ grid-template-columns: 1fr; }
}

/* ---------- ITEMS ---------- */
.items-table-wrap{
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* FIX 7: smooth momentum scroll on iOS for the item grid */
}
.items-head{
  display: grid;
  grid-template-columns: 1fr 56px 84px 90px 26px;
  gap: 8px;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  border-bottom: 1px solid var(--line-strong);
  padding-bottom: 8px;
  margin-bottom: 8px;
  min-width: 420px;
}
.item-row{
  display:grid;
  grid-template-columns: 1fr 56px 84px 90px 26px;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
  min-width: 420px;
}
.item-row input{ font-size: 13px; }
.item-row .amt{
  font-family: var(--font-mono);
  font-size: 13px;
  text-align: right;
  color: var(--ink-soft);
}
.item-remove{
  border: none;
  background: none;
  color: var(--muted);
  cursor: pointer;
  font-size: 15px;
  line-height: 1;
  min-width: 32px;
  min-height: 32px; /* FIX 4 cont'd: easier to tap on phones */
}
.item-remove:hover{ color: var(--accent-warn); }

.btn-add{ width: 100%; margin-top: 10px; text-align: center; }

/* ============================================================
   PREVIEW / INVOICE SHEET
   ============================================================ */
.preview-panel{ position: sticky; top: 78px; }
.preview-toolbar{
  display:flex; align-items:center; gap: 8px;
  font-size: 11px; color: var(--muted);
  text-transform: uppercase; letter-spacing: 0.06em;
  margin-bottom: 14px;
}
.preview-toolbar .dot{ color: var(--accent); font-size: 8px; }

.invoice-sheet{
  background: #fff;
  border: 1px solid var(--line);
  box-shadow: 0 24px 48px -24px rgba(0,0,0,0.18);
  padding: 48px 44px;
  position: relative;
  max-width: 780px;
  width: 100%; /* FIX 8: prevents overflow when the fixed max-width doesn't fit a narrow grid track */
  min-height: 900px;
}

.invoice-status{
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  border: 1.5px solid var(--accent-warn);
  color: var(--accent-warn);
  padding: 4px 10px;
  margin-top: 14px;
}
.invoice-status.paid{ border-color: var(--accent); color: var(--accent); }
.invoice-status.draft{ border-color: var(--line-strong); color: var(--muted); }

.inv-header{
  display:flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 28px;
}
.inv-from{ min-width: 0; } /* FIX 9: lets long addresses/emails wrap instead of pushing layout wide */
.inv-from h3{ font-size: 18px; margin-bottom: 6px; letter-spacing: -0.01em; }
.inv-from p{ margin: 2px 0; font-size: 12.5px; overflow-wrap: anywhere; }
.logo-img{ max-height: 46px; max-width: 160px; margin-bottom: 10px; object-fit: contain; }
.logo-img.hidden{ display:none; }

.inv-meta{ text-align: right; min-width: 200px; max-width: 260px; display:flex; flex-direction: column; align-items: flex-end; }
.inv-meta h1, .inv-meta p{ width: 100%; }
.inv-meta h1{
  font-size: 30px;
  letter-spacing: 0.06em;
  margin-bottom: 10px;
  color: var(--ink);
}
.inv-meta p{ margin: 3px 0; font-size: 12.5px; font-family: var(--font-mono); overflow-wrap: anywhere; }
.inv-meta .muted{ font-family: var(--font-body); margin-right: 4px; }

.muted{ color: var(--muted); }

.inv-divider{ height: 1px; background: var(--line-strong); margin: 22px 0; }

.inv-to{ min-width: 0; }
.inv-to .label{
  font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted);
}
.inv-to h4{ font-size: 15px; margin: 6px 0 4px; }
.inv-to p{ margin: 2px 0; font-size: 12.5px; overflow-wrap: anywhere; }

.table-scroll{
  overflow-x: auto;
  -webkit-overflow-scrolling: touch; /* FIX 7 cont'd */
  margin: 26px 0 4px;
}
.inv-table{
  width: 100%;
  min-width: 420px;
  border-collapse: collapse;
  margin: 0;
}
.inv-table thead th{
  text-align: left;
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  border-bottom: 1px solid var(--ink);
  padding: 0 0 10px;
  font-weight: 600;
}
.inv-table th.ta-right, .inv-table td.ta-right{ text-align: right; }
.inv-table tbody td{
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
  font-size: 13px;
  vertical-align: top;
  overflow-wrap: anywhere;
}
.inv-table tbody td:last-child{
  font-family: var(--font-mono);
  text-align: right;
}
.inv-table tbody td:nth-child(2), .inv-table tbody td:nth-child(3){
  font-family: var(--font-mono);
  color: var(--ink-soft);
  text-align: right;
}

.inv-totals{ display:flex; justify-content: flex-end; margin-top: 14px; }
.totals-box{ width: 280px; }
.t-row{
  display:flex; justify-content: space-between;
  font-size: 13px;
  padding: 7px 0;
  font-family: var(--font-mono);
}
.t-row span:first-child{ font-family: var(--font-body); color: var(--muted); }
.t-grand{
  border-top: 1.5px solid var(--ink);
  margin-top: 6px;
  padding-top: 12px;
  font-size: 16px;
  font-weight: 600;
}

.inv-footer{ display:grid; grid-template-columns: 1fr 1fr; gap: 24px; }
.inv-footer .label{ font-size: 10px; text-transform: uppercase; letter-spacing: 0.08em; color: var(--muted); }
.inv-footer p{ font-size: 12.5px; margin: 6px 0 0; white-space: pre-wrap; overflow-wrap: anywhere; }

.inv-thanks{
  text-align: center;
  font-size: 10.5px;
  color: var(--muted);
  margin-top: 40px;
  letter-spacing: 0.03em;
}

/* ============================================================
   FOOTER
   ============================================================ */
.site-footer{
  border-top: 1px solid var(--line);
  padding: 24px 28px;
  text-align: center;
}
.site-footer p{ font-size: 12px; color: var(--muted); margin:0; }

/* ============================================================
   RESPONSIVE
   ============================================================ */

/* ---- Tablet: stack the two-column workspace ---- */
@media (max-width: 980px){
  .workspace{ grid-template-columns: 1fr; }
  .preview-panel{ position: static; }
  .invoice-sheet{ padding: 32px 24px; max-width: 100%; }
  .inv-meta h1{ font-size: 24px; }
}

/* ---- Large phone / small tablet ---- */
@media (max-width: 780px){
  .topbar-inner{ flex-direction: column; align-items: stretch; gap: 12px; padding: 12px 16px; }
  .brand{ justify-content: flex-start; }
  .topbar-actions{ width: 100%; justify-content: stretch; }
  .topbar-actions .btn{ flex: 1 1 auto; }
  .hero{ padding: 40px 20px 26px; }
  .workspace{ padding: 0 16px 48px; gap: 22px; }
}

/* ---- Phones ---- */
@media (max-width: 600px){
  .brand-text strong{ font-size: 14px; }
  .topbar-actions{ flex-wrap: wrap; gap: 6px; }
  .topbar-actions .btn{ flex: 1 1 calc(50% - 6px); font-size: 12.5px; padding: 9px 8px; }

  .hero h1{ font-size: 24px; }
  .hero p{ font-size: 14px; }
  .hero-tags span{ font-size: 10px; padding: 5px 8px; }

  .field-card{ padding: 16px; }
  .field-card h2{ font-size: 13px; }
  .row, .row-3, .row-4{ grid-template-columns: 1fr; }

  /* FIX 10: iOS Safari auto-zooms the page on input focus when the
     field's font-size is under 16px. Bump form fields to 16px only
     on phones, where that zoom-on-focus behavior actually kicks in. */
  input[type="text"], input[type="date"], input[type="number"], select, textarea{
    font-size: 16px;
  }

  .invoice-sheet{ padding: 22px 16px; min-height: 0; }
  .inv-header{ flex-direction: column; gap: 18px; }
  .inv-meta{ align-items: flex-start; text-align: left; min-width: 0; max-width: 100%; width: 100%; }
  .inv-meta h1{ font-size: 22px; text-align: left; }
  .inv-meta p{ text-align: left; }
  .invoice-status{ margin-top: 8px; }

  .inv-footer{ grid-template-columns: 1fr; gap: 18px; }
  .totals-box{ width: 100%; max-width: 320px; }
  .inv-totals{ justify-content: flex-start; }

  .item-row .item-remove{ font-size: 17px; padding: 4px; }
}

/* ---- Small phones ---- */
@media (max-width: 380px){
  .topbar-actions .btn{ flex: 1 1 100%; }
  .brand-text span{ display: none; }
  .hero h1{ font-size: 21px; }
  .invoice-sheet{ padding: 18px 12px; }
  .inv-meta h1{ font-size: 20px; }
}

/* ============================================================
   PRINT
   ============================================================ */
@media print{
  .no-print, .topbar, .hero, .site-footer{ display: none !important; }
  body{ background: #fff; }
  .workspace{ display:block; padding:0; max-width: 100%; }
  .preview-panel{ position: static; }
  .invoice-sheet{
    box-shadow: none;
    border: none;
    max-width: 100%;
    min-height: 0;
    padding: 0;
  }
}