/* ---------------------------------------------------------------
   Aesthetic Invoice
   The invoice *is* the form: every field is an input styled to read
   as document text, so what you edit is exactly what prints.
   --------------------------------------------------------------- */

:root {
  color-scheme: light dark;

  --canvas: #edece7;
  --paper: #ffffff;
  --ink: #16160f;
  --muted: #78776e;
  --faint: #a3a29a;
  --line: #e4e3dd;
  --rule: #16160f;
  --accent: #2f7d5d;
  --accent-soft: rgba(47, 125, 93, 0.09);
  --hover: rgba(22, 22, 15, 0.045);
  --shadow: 0 1px 2px rgba(22, 22, 15, .05), 0 12px 32px -12px rgba(22, 22, 15, .18);

  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --font-doc: ui-serif, Georgia, "Iowan Old Style", "Times New Roman", serif;

  --sheet-pad: 4rem;
}

@media (prefers-color-scheme: dark) {
  :root {
    --canvas: #0e0e0c;
    --paper: #1a1a16;
    --ink: #eeede1;
    --muted: #8e8d83;
    --faint: #67665e;
    --line: #2c2c25;
    --rule: #eeede1;
    --accent: #5fbf92;
    --accent-soft: rgba(95, 191, 146, 0.12);
    --hover: rgba(238, 237, 225, 0.05);
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 12px 32px -12px rgba(0, 0, 0, .6);
  }
}

* { box-sizing: border-box; }

/* Author `display` rules otherwise beat the UA stylesheet's [hidden]. */
[hidden] { display: none !important; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--canvas);
  color: var(--ink);
  font: 15px/1.5 var(--font-ui);
  -webkit-font-smoothing: antialiased;
}

/* ---------------------------------------------------------- toolbar */

.bar {
  position: sticky;
  top: 0;
  z-index: 10;
  /* Three tracks so the brand centres on the viewport, not on the space the
     tools happen to leave over. */
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 1rem;
  padding: 0.75rem 1.25rem;
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--canvas) 82%, transparent);
  backdrop-filter: blur(12px);
}

.bar__brand {
  grid-column: 2;
  display: flex;
  align-items: center;
  gap: 0.6rem;
  font-family: var(--font-doc);
  font-size: 1.5rem;
  letter-spacing: -0.01em;
}

.bar__mark {
  width: 0.7rem;
  height: 0.7rem;
  border-radius: 50%;
  background: var(--accent);
}

.bar__tools {
  grid-column: 3;
  justify-self: end;
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.tool {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  margin-right: 0.25rem;
}

.tool__label {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.tool__input {
  font: inherit;
  font-size: 0.875rem;
  color: var(--ink);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0.3rem 0.4rem;
}

.btn {
  font: inherit;
  font-size: 0.875rem;
  font-weight: 500;
  white-space: nowrap;
  padding: 0.4rem 0.85rem;
  border-radius: 7px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: background .15s, border-color .15s, opacity .15s;
}

.btn--ghost {
  color: var(--muted);
  background: transparent;
  border-color: var(--line);
}
.btn--ghost:hover { color: var(--ink); background: var(--hover); }

.btn--solid {
  color: #fff;
  background: var(--accent);
}
.btn--solid:hover { opacity: .88; }

@media (prefers-color-scheme: dark) {
  .btn--solid { color: #0e0e0c; }
}

:where(.btn, .tool__input, .f, .addrow, .row__del, .logo):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ------------------------------------------------------------ stage */

.stage {
  padding: 2.5rem 1.25rem 4rem;
}

.sheet {
  width: 100%;
  max-width: 46rem;
  margin: 0 auto;
  padding: var(--sheet-pad);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 6px;
  box-shadow: var(--shadow);
}

.fineprint {
  max-width: 46rem;
  margin: 1.5rem auto 0;
  font-size: 0.8125rem;
  line-height: 1.6;
  color: var(--faint);
  text-align: center;
}
.fineprint strong { color: var(--muted); font-weight: 600; }

/* ------------------------------------------------- editable fields */

.f {
  font: inherit;
  color: inherit;
  width: 100%;
  background: transparent;
  border: 0;
  border-radius: 4px;
  padding: 0.1rem 0.3rem;
  margin-left: -0.3rem;
  resize: none;
  transition: background .12s;
}
.f:hover { background: var(--hover); }
.f:focus { background: var(--accent-soft); outline: none; }
.f::placeholder { color: var(--faint); }

/* Auto-growing textarea, done in CSS rather than by measuring scrollHeight:
   a hidden mirror holds the same text in the same grid cell and dictates the
   height. A JS-set pixel height goes stale the moment the column changes
   width — which is exactly what print does, leaving a gap under the text. */
.grow { display: grid; }

.grow__mirror,
.grow > .f {
  grid-area: 1 / 1;
  /* width:100% is load-bearing on the mirror: as a block its auto width would
     grow by the negative margin, wrap later than the textarea, and under-
     measure the height — clipping the last line. */
  width: 100%;
  padding: 0.1rem 0.3rem;
  margin-left: -0.3rem;
  font: inherit;
  line-height: inherit;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.grow__mirror {
  visibility: hidden;
  pointer-events: none;
}

.grow > textarea.f { overflow: hidden; }

.grow--muted { color: var(--muted); line-height: 1.55; }

.f--issuer {
  font-family: var(--font-doc);
  font-size: 1.375rem;
  letter-spacing: -0.015em;
}

.f--strong { font-weight: 600; }
.f--right { text-align: right; }

.f--date {
  font-variant-numeric: tabular-nums;
  text-align: right;
}
/* Native date fields render wider than their text; keep them on the grid. */
.f--date::-webkit-calendar-picker-indicator {
  opacity: .35;
  cursor: pointer;
}
.f--date::-webkit-calendar-picker-indicator:hover { opacity: .8; }

.eyebrow {
  display: block;
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.11em;
  text-transform: uppercase;
  color: var(--faint);
}
.eyebrow--right { text-align: right; }

.stack {
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

/* -------------------------------------------------------- sheet head */

.sheet__head {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 2rem;
  margin-bottom: 3rem;
}

.issuer { flex: 1; min-width: 0; }

.logo {
  --logo-scale: 1;
  display: grid;
  place-items: center;
  width: max-content;
  min-width: 4.5rem;
  height: 3.25rem;
  margin-bottom: 0.9rem;
  padding: 0 0.75rem;
  border: 1px dashed var(--line);
  border-radius: 5px;
  cursor: pointer;
  transition: border-color .15s, background .15s;
}
.logo:hover { border-color: var(--accent); background: var(--accent-soft); }
.logo:has(img:not([hidden])) {
  border: 0;
  padding: 0;
  min-width: 0;
  height: auto;
  max-width: 100%;
}
/* The slider scales both caps, so the aspect ratio still comes from whichever
   one binds first. min(…, 100%) keeps a scaled-up logo inside the issuer
   column instead of shoving the invoice title off the sheet. */
.logo img {
  max-height: calc(3.25rem * var(--logo-scale));
  max-width: min(calc(13rem * var(--logo-scale)), 100%);
  display: block;
}
.logo__hint {
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--faint);
  white-space: nowrap;
}
.logo:has(img:not([hidden])) .logo__hint { display: none; }

/* Sits outside the .logo label on purpose: dragging the slider inside it
   would trip the file picker on every click. */
.logo__tools {
  display: flex;
  align-items: center;
  gap: 0.9rem;
  margin: -0.55rem 0 0.75rem;
}

.logo__size {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  cursor: pointer;
}

.logo__size input[type="range"] {
  width: 6.5rem;
  height: 1rem;
  accent-color: var(--accent);
  cursor: pointer;
}

.logo__clear {
  font: inherit;
  font-size: 0.6875rem;
  color: var(--faint);
  background: none;
  border: 0;
  padding: 0;
  cursor: pointer;
}
.logo__clear:hover { color: var(--accent); text-decoration: underline; }

.docmeta {
  width: 12rem;
  flex: none;
  text-align: right;
}

.docmeta__title {
  margin: 0 0 1.1rem;
  font-family: var(--font-doc);
  font-size: 2.5rem;
  font-weight: 400;
  line-height: 1;
  letter-spacing: -0.03em;
}

.docmeta .eyebrow { text-align: right; }

/* ---------------------------------------------------------- parties */

/* Date columns are sized to fit a full native date field — including the
   picker icon — or the year silently clips. */
.parties {
  display: grid;
  grid-template-columns: 1fr 9.5rem 9.5rem;
  gap: 1.5rem;
  align-items: start;
  margin-bottom: 2.5rem;
}

.parties__to { max-width: 22rem; }

.parties .stack:not(.parties__to) .eyebrow { text-align: right; }

/* ------------------------------------------------------- line items */

.row {
  display: grid;
  grid-template-columns: 1fr 3.5rem 6rem 7rem;
  gap: 0.75rem;
  align-items: center;
}

/* Descriptions wrap to as many lines as they need — an <input> would clip
   the overflow on paper, where there is no scrolling to rescue it. */
.row--item .grow { min-width: 0; }

/* On paper and desktop the qty/rate wrapper dissolves into the row's own
   grid; on mobile it becomes a "1 × 4200" line under the description. */
.row__calc { display: contents; }
.row__x { display: none; }

.row--head {
  padding-bottom: 0.6rem;
  border-bottom: 1px solid var(--rule);
}

/* The delete button lives outside the sheet's text column so it never
   shifts the numbers when it appears. */
.row--item {
  position: relative;
  padding: 0.55rem 0;
  border-bottom: 1px solid var(--line);
  align-items: start;
}

/* Matches the fields' own padding so the amount sits on the description's
   first line when the description wraps. */
.row__amount {
  padding: 0.1rem 0.3rem 0.1rem 0;
  text-align: right;
  font-variant-numeric: tabular-nums;
  color: var(--muted);
}

.row__del {
  position: absolute;
  top: 50%;
  left: calc(100% + 0.6rem);
  transform: translateY(-50%);
  display: grid;
  place-items: center;
  width: 1.4rem;
  height: 1.4rem;
  padding: 0;
  font: inherit;
  font-size: 1rem;
  line-height: 1;
  color: var(--faint);
  background: none;
  border: 0;
  border-radius: 4px;
  cursor: pointer;
  opacity: 0;
  transition: opacity .12s, color .12s, background .12s;
}
.row--item:hover .row__del,
.row__del:focus-visible { opacity: 1; }
.row__del:hover { color: #c2413a; background: var(--hover); }

.addrow {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  margin-top: 0.9rem;
  padding: 0.3rem 0.55rem 0.3rem 0.35rem;
  font: inherit;
  font-size: 0.8125rem;
  color: var(--muted);
  background: none;
  border: 0;
  border-radius: 5px;
  cursor: pointer;
  transition: color .12s, background .12s;
}
.addrow:hover { color: var(--accent); background: var(--accent-soft); }
.addrow span { font-size: 1rem; line-height: 1; }

/* ----------------------------------------------------------- totals */

.totals {
  display: flex;
  justify-content: flex-end;
  margin-top: 2rem;
}

.totals__inner {
  width: 19rem;
  display: flex;
  flex-direction: column;
  gap: 0.55rem;
}

.tot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.9375rem;
  color: var(--muted);
}

.tot__label {
  display: inline-flex;
  align-items: center;
  gap: 0.15rem;
}

.tot__val {
  font-variant-numeric: tabular-nums;
}

.f--pct {
  width: 2.6rem;
  margin-left: 0.25rem;
  padding: 0.05rem 0.2rem;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.tot__pct { color: var(--faint); }

.tot--grand {
  margin-top: 0.35rem;
  padding-top: 0.85rem;
  border-top: 1px solid var(--rule);
  color: var(--ink);
  font-family: var(--font-doc);
  font-size: 1.5rem;
  letter-spacing: -0.02em;
}
.tot--grand .tot__label { font-size: 1.125rem; }

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

.notes {
  margin-top: 3rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}
.notes .grow { font-size: 0.875rem; max-width: 34rem; }

/* ----------------------------------------------------------- mobile */

/* Centring the brand costs twice the tools' width in side tracks (~1020px of
   viewport). Below that, stack brand over tools so it stays centred rather
   than drifting off as the tools squeeze their track. */
@media (max-width: 1020px) {
  .bar {
    grid-template-columns: 1fr;
    justify-items: center;
    gap: 0.6rem;
  }
  .bar__brand { grid-column: 1; }
  .bar__tools { grid-column: 1; justify-self: center; flex-wrap: wrap; justify-content: center; }
}

@media (max-width: 720px) {
  :root { --sheet-pad: 1.5rem; }

  .bar { padding: 0.65rem 1rem; }
  .bar__brand { font-size: 1.25rem; }

  .stage { padding: 1.5rem 0.75rem 3rem; }

  .sheet__head { flex-direction: column; gap: 1.75rem; margin-bottom: 2rem; }
  .docmeta { width: 100%; text-align: left; }
  .docmeta .eyebrow, .f--right { text-align: left; }
  .docmeta__title { font-size: 2rem; margin-bottom: 0.75rem; }

  .parties { grid-template-columns: 1fr 1fr; gap: 1.25rem; }
  .parties__to { grid-column: 1 / -1; }
  .parties .stack:not(.parties__to) .eyebrow, .f--date { text-align: left; }

  /* Four columns leave the description barely 80px on a phone. Give it the
     full width and drop qty/rate onto a second line under it. */
  .row--head { display: none; }

  .row--item {
    grid-template-columns: 1fr auto 1.4rem;
    grid-template-areas:
      "desc desc   del"
      "calc amount amount";
    gap: 0.3rem 0.5rem;
    align-items: center;
    padding: 0.7rem 0;
  }

  .row--item .grow { grid-area: desc; }
  .row__amount { grid-area: amount; padding-right: 0; }
  .row__del { grid-area: del; position: static; transform: none; opacity: 1; justify-self: end; }

  .row__calc {
    grid-area: calc;
    display: flex;
    align-items: center;
    gap: 0.15rem;
  }
  .row__x { display: inline; color: var(--faint); font-size: 0.75rem; }
  .row__calc [data-f="qty"] { width: 2.25rem; text-align: right; }
  .row__calc [data-f="rate"] { width: 5rem; text-align: left; }

  .totals__inner { width: 100%; }
}

/* ------------------------------------------------------------ print */

@media print {
  :root {
    --canvas: #fff;
    --paper: #fff;
    --ink: #000;
    --muted: #444;
    --faint: #666;
    --line: #d8d8d3;
    --rule: #000;
    --accent: #2f7d5d;
    --shadow: none;
    --sheet-pad: 0;
  }

  @page { margin: 16mm; }

  body { background: #fff; font-size: 11pt; }

  .bar, .fineprint, .addrow, .row__del, .logo__hint, .logo__tools { display: none !important; }

  .stage { padding: 0; }

  .sheet {
    max-width: none;
    border: 0;
    border-radius: 0;
    box-shadow: none;
  }

  .logo { border: 0; padding: 0; min-width: 0; height: auto; }

  /* Inputs must read as plain text on paper. A field that happened to be
     focused when the user hit print would otherwise carry its highlight
     onto the page. Field metrics are deliberately left alone: the mirror
     that sizes each textarea has to keep matching it exactly. */
  .f { background: transparent !important; }
  .f::placeholder { color: transparent; }
  .f--date::-webkit-calendar-picker-indicator { display: none; }

  .row--item, .notes, .totals { break-inside: avoid; }
  .sheet__head { break-after: avoid; }
}
