/* ==========================================================================
   SAMPLE — invoice builder, and the document it prints
   ==========================================================================
   Every other sample here is designed for a screen. This one is designed for
   paper, which is a different medium with different rules: fixed page height,
   no scrolling, no hover, no JavaScript, and a reader who may only ever see
   the printout.

   The interesting CSS is all in the @media print block at the bottom.

   Scoped under .inv.
   ========================================================================== */

.inv {
  --bg:     #eceef1;
  --panel:  #ffffff;
  --ink:    #14181d;
  --ink-2:  #4d5761;
  --ink-3:  #646f7a;   /* 5.1:1 on white; #6f7a85 was 4.38 and failed */
  --line:   #dde2e7;
  --line-2: #c3ccd4;
  --brand:  #1d4ed8;
  --brand-w:#eaf0fe;
  --go:     #047857;

  --sans: "Inter", -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: "JetBrains Mono", ui-monospace, Consolas, monospace;

  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-font-smoothing: antialiased;
  min-height: 100vh;
}
.inv *, .inv *::before, .inv *::after { box-sizing: border-box; }
.inv a { color: var(--brand); }
.inv :focus-visible { outline-color: var(--brand); }

.inv-wrap { width: 100%; max-width: 84rem; margin-inline: auto; padding: 0 clamp(1rem, 3vw, 2rem) 5rem; }

.inv-hero { padding-block: clamp(2rem, 5vw, 3rem) 1.25rem; max-width: 44rem; }
.inv-hero h1 { margin: 0; font-size: clamp(1.9rem, 5vw, 3rem); letter-spacing: -0.03em; line-height: 1.05; font-weight: 800; }
.inv-hero h1 em { font-style: normal; color: var(--brand); }
.inv-hero p { margin: .9rem 0 0; color: var(--ink-2); line-height: 1.7; }

.inv-cols { display: grid; gap: 1.25rem; grid-template-columns: minmax(0, 1fr); align-items: start; }
@media (min-width: 68rem) { .inv-cols { grid-template-columns: minmax(0, 22rem) minmax(0, 1fr); } }

/* ------------------------------------------------------------- the form --- */

.icard { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.icard__head { padding: .7rem .9rem; border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: .6rem; }
.icard__head h2 { margin: 0; font-size: .7rem; font-weight: 700; letter-spacing: .12em; text-transform: uppercase; color: var(--ink-3); }
.icard__head .sp { margin-left: auto; }
.icard__body { padding: .9rem; display: grid; grid-template-columns: minmax(0, 1fr); gap: .7rem; }

.ifield { display: grid; grid-template-columns: minmax(0, 1fr); gap: .25rem; min-width: 0; }
.ifield label { font-size: .68rem; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-3); }
.ifield input, .ifield select, .ifield textarea {
  font: inherit; font-size: .85rem;
  padding: .45rem .6rem; border: 1px solid var(--line-2); border-radius: 8px;
  background: #fff; color: var(--ink); width: 100%;
  /* Without this an input keeps its intrinsic ~20-character min-content and
     refuses to shrink, which is what widened the column in the first place. */
  min-width: 0;
}
.ifield textarea { resize: vertical; min-height: 3.4rem; }

.iline { display: grid; grid-template-columns: 1fr 3.4rem 5.5rem auto; gap: .4rem; align-items: end; }
.iline input { font-size: .8rem; }
.iline .num { text-align: right; font-family: var(--mono); }
.ix {
  width: 30px; height: 30px; flex: none;
  border: 1px solid var(--line-2); border-radius: 7px;
  background: #fff; color: var(--ink-3); cursor: pointer;
  display: grid; place-items: center; font-size: 1rem; line-height: 1;
}
.ix:hover { border-color: #ef4444; color: #ef4444; }

.ibtn {
  font: inherit; font-size: .8rem; font-weight: 600;
  padding: .5rem .9rem; border-radius: 8px;
  border: 1px solid var(--line-2); background: #fff; color: var(--ink);
  cursor: pointer; transition: background .15s, border-color .15s, transform .1s;
}
.ibtn:hover { background: #f2f5f8; border-color: var(--ink-3); }
.ibtn:active { transform: scale(.97); }
.ibtn--go { background: var(--brand); border-color: var(--brand); color: #fff; }
.ibtn--go:hover { background: #1a44bd; border-color: #1a44bd; }
.inv-btns { display: flex; flex-wrap: wrap; gap: .5rem; }

/* --------------------------------------------------------- the document --- */

/* The page is 794px because A4 is 794px, and shrinking it would make the
   preview lie about where the lines break. On a phone it therefore scrolls
   inside its own container rather than widening the document. */
.inv-stage {
  display: grid; gap: .8rem; justify-items: center;
  min-width: 0;
  overflow-x: auto;
  padding-bottom: .4rem;
}

/*
 * A4 at 96dpi is 794 x 1123 CSS pixels. The preview is drawn at exactly that
 * size so what is on screen is what comes out of the printer — a preview at
 * "roughly A4" is worse than no preview, because it lies about line breaks.
 */
.page {
  width: 794px; max-width: 100%;
  min-height: 1123px;
  background: #fff;
  color: #14181d;
  padding: 56px 60px;
  box-shadow: 0 20px 50px -24px rgba(20, 24, 29, .45);
  border-radius: 2px;
  display: flex; flex-direction: column;
}

.doc-head { display: flex; justify-content: space-between; gap: 2rem; align-items: flex-start; padding-bottom: 22px; border-bottom: 2px solid #14181d; }
.doc-from b { display: block; font-size: 1.05rem; letter-spacing: -0.01em; }
.doc-from span { display: block; font-size: .78rem; color: #4d5761; line-height: 1.6; }
.doc-title { text-align: right; }
.doc-title h2 { margin: 0; font-size: 1.9rem; letter-spacing: -0.03em; font-weight: 800; }
.doc-title dl { margin: .5rem 0 0; font-size: .76rem; display: grid; gap: .15rem; }
.doc-title dl div { display: flex; gap: .6rem; justify-content: flex-end; }
.doc-title dt { color: #646f7a; }
.doc-title dd { margin: 0; font-family: var(--mono); }

.doc-to { margin-top: 22px; font-size: .82rem; }
.doc-to b { display: block; font-size: .68rem; letter-spacing: .12em; text-transform: uppercase; color: #646f7a; margin-bottom: .3rem; }
.doc-to span { display: block; line-height: 1.6; }

table.doc-items { width: 100%; border-collapse: collapse; margin-top: 24px; font-size: .82rem; }
.doc-items th {
  text-align: left; padding: .5rem .4rem;
  border-bottom: 1px solid #14181d;
  font-size: .66rem; letter-spacing: .1em; text-transform: uppercase; color: #4d5761;
}
.doc-items td { padding: .55rem .4rem; border-bottom: 1px solid #e6eaee; vertical-align: top; }
.doc-items .num { text-align: right; font-family: var(--mono); font-variant-numeric: tabular-nums; white-space: nowrap; }
.doc-items tbody tr { break-inside: avoid; }

.doc-foot { margin-top: auto; padding-top: 24px; }
.doc-sum { margin-left: auto; width: 16rem; font-size: .84rem; }
.doc-sum div { display: flex; justify-content: space-between; padding: .28rem 0; }
.doc-sum dt { color: #4d5761; }
.doc-sum dd { margin: 0; font-family: var(--mono); font-variant-numeric: tabular-nums; }
.doc-sum .total { margin-top: .35rem; padding-top: .5rem; border-top: 2px solid #14181d; font-size: 1.05rem; font-weight: 800; }
.doc-terms { margin-top: 22px; padding-top: 14px; border-top: 1px solid #e6eaee; font-size: .72rem; color: #4d5761; line-height: 1.7; white-space: pre-line; }

/* --------------------------------------------------------------- notes --- */

.inv-note { margin-top: 1.5rem; background: var(--panel); border: 1px solid var(--line); border-radius: 12px; padding: clamp(1.1rem, 3vw, 2rem); }
.inv-note h2 { margin: 0 0 .8rem; font-size: 1.25rem; letter-spacing: -0.02em; }
.inv-note h3 { margin: 1.6rem 0 .4rem; font-size: .95rem; }
.inv-note p  { margin: 0 0 .7rem; color: var(--ink-2); line-height: 1.7; max-width: 46rem; }
.inv-note code { font-family: var(--mono); font-size: .86em; background: #eef1f5; border-radius: 4px; padding: .1em .35em; color: #1a44bd; }
.inv-note pre {
  margin: .9rem 0; padding: .9rem 1rem; overflow-x: auto;
  background: #14181d; color: #dde3ea; border-radius: 10px;
  font-family: var(--mono); font-size: .76rem; line-height: 1.7;
}
.inv-note pre .c { color: #8b97a3; }
.inv-note pre .k { color: #93c5fd; }

/* ========================================================== FOR PAPER =====
   The whole reason this sample exists. Everything above is the editor; this
   is the deliverable. */

@media print {
  /* A named page with real margins. Without this the browser applies its own
     and the design silently shifts on every machine. */
  @page { size: A4; margin: 14mm 15mm; }

  /* Nothing but the document. Hiding the chrome is not a nicety — a printed
     page cannot scroll away from a sidebar. */
  .sflag, .inv-hero, .inv-form, .inv-note, .inv-btns, .site-footer, .site-header { display: none !important; }

  .inv, .inv-wrap, .inv-cols, .inv-stage { display: block !important; background: #fff !important; padding: 0 !important; margin: 0 !important; max-width: none !important; }

  .page {
    width: auto !important; min-height: 0 !important;
    box-shadow: none !important; border-radius: 0 !important;
    padding: 0 !important;
  }

  /* Repeat the column headers on every sheet. A second page of unlabelled
     numbers is the classic printed-table failure. */
  .doc-items thead { display: table-header-group; }
  .doc-items tfoot { display: table-footer-group; }

  /* Never split a line item, and never let the totals block start a page on
     its own — an invoice whose total is orphaned onto page 2 gets queried. */
  .doc-items tr, .doc-foot, .doc-sum { break-inside: avoid; }
  .doc-foot { break-before: avoid; }

  /* Print the URL behind a link, since a reader on paper cannot click it. */
  .doc-terms a::after { content: " (" attr(href) ")"; font-size: .9em; color: #4d5761; }

  /* Backgrounds are dropped by default in print. Anything that carries
     meaning has to survive as a border or as text. */
  .doc-items th { border-bottom: 1px solid #000 !important; }
}

@media (prefers-reduced-motion: reduce) {
  .inv *, .inv *::before, .inv *::after { transition-duration: .01ms !important; animation-duration: .01ms !important; }
}
