/* ==========================================================
   Invoice Generator — "Paper & Ledger" design system
   Plain CSS, no preprocessor. Fonts are loaded via <link> tags
   in each page's <head> (Bricolage Grotesque, Public Sans,
   IBM Plex Mono from Google Fonts) — see index.html.

   Motion policy: every animation/transition listed in the brief
   (hero rise, row enter/exit, paper settle, template flip,
   number roll, paid stamp, button morph, button lift) is defined
   ONLY inside `@media (prefers-reduced-motion: no-preference)`
   blocks. Users who prefer reduced motion get the same end
   states instantly, with no transform/opacity animation at all.
   Only `transform` and `opacity` are animated anywhere in this
   file — with one deliberate, documented exception: collapsing a
   deleted line item's vertical space needs *some* size-adjacent
   property, so that one transition also animates
   `grid-template-rows` (a modern, non-layout-thrashing way to
   animate toward "auto height" — see ".item-row-wrap").
   ========================================================== */

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

/* ==========================================================
   Design tokens — every color below is derived from the five
   brief-mandated brand tokens via color-mix().
   ========================================================== */
:root {
  /* Brand tokens */
  --ink: #11161c;
  --paper: #fcfbf8;
  --ledger: #e4e1d8;
  --accent: #2f6f5e;
  --stamp: #c2413a;

  /* Derived surfaces & text */
  --paper-shade: color-mix(in srgb, var(--paper) 92%, var(--ink) 8%);
  --paper-deep: color-mix(in srgb, var(--paper) 82%, var(--ink) 18%);
  --ink-muted: color-mix(in srgb, var(--ink) 65%, var(--paper) 35%);
  --ink-faint: color-mix(in srgb, var(--ink) 38%, var(--paper) 62%);
  --ledger-line: var(--ledger);
  --ledger-strong: color-mix(in srgb, var(--ledger) 55%, var(--ink) 45%);
  --accent-soft: color-mix(in srgb, var(--accent) 13%, var(--paper) 87%);
  --accent-strong: color-mix(in srgb, var(--accent) 75%, var(--ink) 25%);
  --stamp-soft: color-mix(in srgb, var(--stamp) 6%, var(--paper) 94%);
  --stamp-strong: color-mix(in srgb, var(--stamp) 15%, var(--ink) 85%);

  /* Shadows — numeric equivalent of --ink (17, 22, 28) at low alpha,
     since box-shadow can't consume a hex custom property directly */
  --shadow-sm: 0 1px 2px rgba(17, 22, 28, 0.07);
  --shadow-md: 0 3px 8px rgba(17, 22, 28, 0.08), 0 1px 2px rgba(17, 22, 28, 0.06);
  --shadow-paper:
    0 1px 1px rgba(17, 22, 28, 0.04),
    0 8px 16px rgba(17, 22, 28, 0.07),
    0 28px 56px rgba(17, 22, 28, 0.12);

  --radius: 10px;
  --radius-sm: 7px;
  --radius-lg: 16px;

  /* Typography */
  --font-display: "Bricolage Grotesque", "Segoe UI", Georgia, serif;
  --font-sans: "Public Sans", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: "IBM Plex Mono", ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;

  /* Motion durations, named per the brief */
  --dur-row: 200ms;
  --dur-roll: 300ms;
  --dur-stamp: 380ms;
  --dur-flip: 200ms;
}

/* ==========================================================
   Base
   ========================================================== */
html {
  font-size: 16px;
}

body {
  margin: 0;
  font-family: var(--font-sans);
  background: var(--paper-shade);
  color: var(--ink);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1, h2, h3, p {
  margin: 0;
}

button {
  font-family: inherit;
}

/* Locks background scroll while the mobile preview drawer is open */
body.drawer-open-lock {
  overflow: hidden;
}

/* Numeric values line up in columns everywhere they appear */
input[type="number"],
.item-amount,
#invoice-number,
.preview-table .col-num,
.totals-row span,
#prev-invoice-number,
.history-cell--number,
.history-cell--amount,
.btn-download__count {
  font-family: var(--font-mono);
  font-variant-numeric: tabular-nums;
}

/* ==========================================================
   Focus — visible in the accent color on every interactive
   element, everywhere, no exceptions.
   ========================================================== */
a:focus-visible,
button:focus-visible,
input:focus-visible,
select:focus-visible,
textarea:focus-visible,
[tabindex]:focus-visible {
  outline: 2.5px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

/* ==========================================================
   Tool toolbar
   The site-wide nav header and footer live in assets/css/site.css
   (shared across every page). This is just the small strip
   directly above the invoice form/preview.
   ========================================================== */
.tool-toolbar {
  background: var(--paper);
  border-bottom: 1px solid var(--ledger-strong);
  padding: 1.5rem 1.5rem 1.25rem;
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  flex-wrap: wrap;
}

.tool-toolbar__subtitle {
  color: var(--ink-muted);
  font-size: 0.95rem;
}

/* ==========================================================
   Layout
   ========================================================== */
.app-layout {
  max-width: 1400px;
  margin: 0 auto;
  padding: 1.75rem 1.5rem 4rem;
  display: grid;
  grid-template-columns: minmax(0, 480px) minmax(0, 1fr);
  gap: 1.75rem;
  align-items: start;
}

@media (max-width: 960px) {
  .app-layout {
    grid-template-columns: 1fr;
    padding-bottom: 6rem;
  }
}

.form-column,
.preview-column {
  min-width: 0;
}

.preview-column {
  position: sticky;
  top: 1.75rem;
  perspective: 1800px;
}

@media (max-width: 960px) {
  /* On mobile the preview becomes a bottom drawer, opened with the
     floating "Preview Invoice" button — see .preview-fab below. */
  .preview-column {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    top: auto;
    max-height: 82vh;
    margin: 0;
    padding: 1.1rem 1.1rem calc(1.25rem + env(safe-area-inset-bottom, 0px));
    background: var(--paper-shade);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-paper);
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    z-index: 60;
    transform: translateY(105%);
  }

  .preview-column.is-drawer-open {
    transform: translateY(0);
  }

  @media (prefers-reduced-motion: no-preference) {
    .preview-column {
      transition: transform 320ms cubic-bezier(0.22, 0.8, 0.28, 1);
    }
  }
}

/* ==========================================================
   Form: cards, fields
   ========================================================== */
.card {
  /* Fieldsets default to min-width: min-content in most browsers, which
     stops them shrinking below their widest child (e.g. the line-item
     table). Overriding it here is what lets .items-scroll do the
     scrolling instead of the whole page overflowing horizontally. */
  min-width: 0;
  background: var(--paper);
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  padding: 1.25rem;
  margin: 0 0 1.25rem;
}

.card legend {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1rem;
  padding: 0 0.4rem;
  color: var(--ink);
}

.field {
  margin-bottom: 0.85rem;
}

.field:last-child {
  margin-bottom: 0;
}

.field-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 0.85rem;
}

.field-row .field {
  margin-bottom: 0.85rem;
}

.field--inline {
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.field--inline label {
  margin-bottom: 0;
}

label {
  display: block;
  font-size: 0.78rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--ink-muted);
  margin-bottom: 0.35rem;
}

input[type="text"],
input[type="email"],
input[type="tel"],
input[type="number"],
input[type="date"],
select,
textarea {
  width: 100%;
  padding: 0.55rem 0.7rem;
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink);
  font-size: 0.92rem;
  font-family: inherit;
  /* Only border-color transitions — Chromium fails to interpolate a
     transitioned box-shadow whose color is a color-mix() result
     (reproducible: box-shadow silently computes to transparent).
     The focus ring below appears instantly instead, which is fine
     for a 3px ring and arguably better for a clear focus cue. */
  transition: border-color 0.15s ease;
}

textarea {
  resize: vertical;
  min-height: 2.5rem;
}

input:focus,
select:focus,
textarea:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

/* Checkbox field (mark as paid) */
.field--checkbox {
  margin-top: 0.2rem;
}

.checkbox-label {
  display: flex;
  align-items: center;
  gap: 0.55rem;
  font-size: 0.88rem;
  font-weight: 600;
  color: var(--ink);
  cursor: pointer;
  margin-bottom: 0;
}

.checkbox-label input[type="checkbox"] {
  width: 1.05rem;
  height: 1.05rem;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Logo upload */
.logo-upload-row {
  display: flex;
  align-items: center;
  gap: 0.6rem;
  flex-wrap: wrap;
}

.logo-input {
  flex: 1 1 auto;
  min-width: 0;
  font-size: 0.82rem;
  color: var(--ink-muted);
}

/* Template switcher */
.template-switcher {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 0.5rem;
}

.template-option {
  padding: 0.55rem 0.4rem;
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--ink-muted);
  font-size: 0.84rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

.template-option:hover {
  border-color: var(--accent);
}

.template-option.is-active {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-strong);
}

input[type="color"] {
  width: 2.75rem;
  height: 2.2rem;
  padding: 0.15rem;
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  cursor: pointer;
}

/* ==========================================================
   Line items table (form side)
   ========================================================== */
.items-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin-bottom: 0.85rem;
}

.items-table {
  min-width: 480px;
}

.items-head {
  display: grid;
  grid-template-columns: minmax(0, 2.4fr) 0.7fr 0.9fr 0.9fr 1.7rem;
  gap: 0.5rem;
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--ink-muted);
  padding: 0 0 0.5rem;
  border-bottom: 1px solid var(--ledger-strong);
  margin-bottom: 0.5rem;
}

#items-body {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

/* Wraps each .item-row so a deleted row can collapse its rendered
   height via grid-template-rows (see file header note on motion). */
.item-row-wrap {
  display: grid;
  grid-template-rows: 1fr;
  opacity: 1;
  transform-origin: top center;
}

.item-row-wrap > .item-row {
  overflow: hidden;
  min-height: 0;
}

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 2.4fr) 0.7fr 0.9fr 0.9fr 1.7rem;
  gap: 0.5rem;
  align-items: center;
}

.item-row input {
  padding: 0.5rem 0.6rem;
  font-size: 0.88rem;
}

.item-row.is-invalid .item-desc {
  border-color: var(--stamp);
  box-shadow: 0 0 0 3px var(--stamp-soft);
}

.item-amount {
  font-size: 0.9rem;
  font-weight: 600;
  text-align: right;
  padding: 0.5rem 0.4rem;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.delete-row-btn {
  width: 1.7rem;
  height: 1.7rem;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius-sm);
  background: var(--paper);
  color: var(--stamp);
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.delete-row-btn:hover {
  background: var(--stamp-soft);
  border-color: var(--stamp);
}

.delete-row-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
}

.delete-row-btn:disabled:hover {
  background: var(--paper);
  border-color: var(--ledger-strong);
}

@media (prefers-reduced-motion: no-preference) {
  .item-row-wrap {
    transition:
      grid-template-rows var(--dur-row) ease,
      opacity var(--dur-row) ease,
      transform var(--dur-row) ease;
    animation: row-enter var(--dur-row) ease-out both;
  }

  .item-row-wrap.is-removing {
    grid-template-rows: 0fr;
    opacity: 0;
    transform: scaleY(0.94);
  }

  @keyframes row-enter {
    from {
      opacity: 0;
      transform: translateY(-10px);
    }
    to {
      opacity: 1;
      transform: translateY(0);
    }
  }
}

/* ==========================================================
   Validation message
   ========================================================== */
.validation-message {
  background: var(--stamp-soft);
  border: 1px solid var(--stamp);
  color: var(--stamp-strong);
  border-radius: var(--radius-sm);
  padding: 0.65rem 0.85rem;
  font-size: 0.85rem;
  font-weight: 600;
  margin-bottom: 1rem;
}

/* ==========================================================
   Buttons
   ========================================================== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  padding: 0.6rem 1.1rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease;
}

@media (prefers-reduced-motion: no-preference) {
  .btn {
    transition: background 0.15s ease, border-color 0.15s ease, color 0.15s ease, transform 0.1s ease;
  }

  .btn:not(:disabled):hover {
    transform: translateY(-1px);
  }

  .btn:not(:disabled):active {
    transform: translateY(1px);
  }
}

.btn:disabled {
  cursor: not-allowed;
  opacity: 0.7;
}

.btn--primary {
  background: var(--ink);
  color: var(--paper);
}

.btn--primary:not(:disabled):hover {
  background: var(--accent-strong);
}

.btn--secondary {
  background: var(--accent-soft);
  color: var(--accent-strong);
  border-color: transparent;
  width: 100%;
}

.btn--secondary:hover {
  background: color-mix(in srgb, var(--accent-soft) 70%, var(--accent) 30%);
}

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

.btn--ghost:hover {
  background: var(--stamp-soft);
  color: var(--stamp-strong);
  border-color: var(--stamp);
}

.btn--small {
  padding: 0.4rem 0.75rem;
  font-size: 0.8rem;
}

.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.75rem;
  margin-top: 0.25rem;
}

/* ---- Download button: idle -> loading -> success -> idle ---- */
.btn-download {
  position: relative;
}

.btn-download__spinner {
  display: none;
  width: 0.95rem;
  height: 0.95rem;
  border-radius: 50%;
  border: 2px solid color-mix(in srgb, var(--paper) 35%, transparent);
  border-top-color: var(--paper);
  flex: none;
}

.btn-download__check {
  display: none;
  flex: none;
  color: var(--paper);
}

.btn-download[data-state="loading"] .btn-download__spinner {
  display: block;
}

.btn-download[data-state="loading"] .btn-download__label,
.btn-download[data-state="success"] .btn-download__label {
  /* keep layout stable; text still swaps, icons are the primary cue */
}

.btn-download[data-state="success"] .btn-download__check {
  display: block;
}

@media (prefers-reduced-motion: no-preference) {
  .btn-download__spinner {
    animation: btn-spin 700ms linear infinite;
  }

  .btn-download__check {
    transform: scale(0.4);
    opacity: 0;
  }

  .btn-download[data-state="success"] .btn-download__check {
    animation: btn-check-in 260ms cubic-bezier(0.2, 0.9, 0.3, 1.2) forwards;
  }

  @keyframes btn-spin {
    to {
      transform: rotate(360deg);
    }
  }

  @keyframes btn-check-in {
    from {
      opacity: 0;
      transform: scale(0.4);
    }
    to {
      opacity: 1;
      transform: scale(1);
    }
  }
}

/* ---- Mobile "Preview Invoice" floating action button ---- */
.preview-fab {
  display: none;
  position: fixed;
  right: 1.1rem;
  bottom: 1.1rem;
  z-index: 65;
  background: var(--ink);
  color: var(--paper);
  border: none;
  border-radius: 999px;
  padding: 0.85rem 1.4rem;
  font-weight: 700;
  font-size: 0.88rem;
  box-shadow: var(--shadow-md);
  cursor: pointer;
}

@media (max-width: 960px) {
  .preview-fab {
    display: inline-flex;
    align-items: center;
    gap: 0.4rem;
  }
}

@media (prefers-reduced-motion: no-preference) {
  .preview-fab:hover {
    transform: translateY(-1px);
  }

  .preview-fab:active {
    transform: translateY(1px);
  }
}

.drawer-backdrop {
  display: none;
}

@media (max-width: 960px) {
  .drawer-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(17, 22, 28, 0.4);
    opacity: 0;
    pointer-events: none;
    z-index: 55;
  }

  .drawer-backdrop.is-visible {
    opacity: 1;
    pointer-events: auto;
  }

  @media (prefers-reduced-motion: no-preference) {
    .drawer-backdrop {
      transition: opacity 320ms ease;
    }
  }
}

/* ==========================================================
   Preview sheet — the signature element: a physical A4 page
   ========================================================== */
.preview-sheet {
  position: relative;
  background: var(--paper);
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius);
  box-shadow: var(--shadow-paper);
  padding: 2.25rem;
  max-width: 740px;
  margin: 0 auto;
  overflow: hidden;
  backface-visibility: hidden;
}

/* Correct A4 proportions (210 x 297mm) on desktop, where the sheet is
   the hero visual. On mobile it lives in a capped, scrollable drawer,
   so it sizes to its content instead of forcing extra blank space. */
@media (min-width: 961px) {
  .preview-sheet {
    min-height: 1046px; /* 740px * (297/210) */
  }
}

@media (prefers-reduced-motion: no-preference) {
  .preview-sheet {
    animation: paper-settle 650ms cubic-bezier(0.2, 0.7, 0.2, 1) both;
  }

  @keyframes paper-settle {
    from {
      opacity: 0;
      transform: rotate(-2.25deg) translateY(16px) scale(0.98);
    }
    to {
      opacity: 1;
      transform: rotate(0deg) translateY(0) scale(1);
    }
  }
}

.preview-logo {
  display: none;
  max-width: 160px;
  max-height: 70px;
  object-fit: contain;
  margin-bottom: 0.6rem;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  padding-bottom: 1.25rem;
  border-bottom: 2px solid var(--invoice-accent, var(--accent));
  flex-wrap: wrap;
}

.preview-business-name {
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 700;
}

.preview-invoice-title {
  text-align: right;
}

.preview-invoice-title h2 {
  font-family: var(--font-display);
  font-size: 2rem;
  font-weight: 800;
  letter-spacing: 0.03em;
  color: var(--invoice-accent, var(--accent));
  margin-bottom: 0.4rem;
}

.preview-line {
  font-size: 0.88rem;
  color: var(--ink-muted);
  margin-top: 0.15rem;
}

.preview-line .label {
  color: var(--ink-muted);
  font-weight: 600;
  margin-right: 0.25rem;
}

.preview-strong {
  color: var(--ink);
  font-weight: 700;
  font-size: 0.95rem;
}

.preview-address {
  white-space: pre-line;
}

.preview-parties {
  display: flex;
  justify-content: space-between;
  gap: 1.5rem;
  padding: 1.25rem 0;
  flex-wrap: wrap;
}

.preview-section-label {
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.07em;
  color: var(--invoice-accent, var(--accent));
  margin-bottom: 0.4rem;
}

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.preview-table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
  margin-top: 0.5rem;
  font-size: 0.88rem;
}

.preview-table th {
  text-align: left;
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-muted);
  border-bottom: 1px solid var(--ledger-strong);
  padding: 0.55rem 0.4rem;
}

.preview-table td {
  padding: 0.6rem 0.4rem;
  border-bottom: 1px solid var(--ledger-line);
  vertical-align: top;
  word-break: break-word;
}

.preview-table .col-num {
  text-align: right;
  white-space: nowrap;
}

.preview-table tbody tr:last-child td {
  border-bottom: 1px solid var(--ledger-strong);
}

.preview-table .empty-row td {
  color: var(--ink-faint);
  font-style: italic;
  text-align: center;
  font-family: var(--font-sans);
}

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

.totals-box {
  width: 100%;
  max-width: 260px;
}

.totals-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: 0.9rem;
  padding: 0.35rem 0;
  color: var(--ink-muted);
}

.totals-row span:first-child {
  font-family: var(--font-sans);
}

.totals-row--final {
  border-top: 2px solid var(--invoice-accent, var(--accent));
  margin-top: 0.35rem;
  padding-top: 0.55rem;
  font-size: 1.15rem;
  font-weight: 800;
  color: var(--ink);
}

.totals-row--final span:first-child {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 1rem;
}

.preview-footer {
  display: flex;
  gap: 2rem;
  margin-top: 2rem;
  padding-top: 1.25rem;
  border-top: 1px solid var(--ledger-strong);
  flex-wrap: wrap;
}

.preview-footer__block {
  flex: 1 1 200px;
  min-width: 0;
}

@media (max-width: 640px) {
  .preview-sheet {
    padding: 1.25rem;
  }

  .preview-invoice-title {
    text-align: left;
  }

  .preview-header {
    flex-direction: column;
  }
}

/* ---- Template flip: driven by inline styles set in script.js so the
   two-phase rotation (out, swap content, in) can be sequenced exactly.
   These base properties just make sure the 3D transform renders
   correctly whenever script.js sets it. ---- */
@media (prefers-reduced-motion: no-preference) {
  .preview-sheet {
    transform-style: preserve-3d;
  }
}

/* ==========================================================
   Paid stamp — diagonal watermark, stamps in like rubber on paper
   ========================================================== */
.paid-stamp {
  display: none;
  position: absolute;
  top: 42%;
  left: 50%;
  transform: translate(-50%, -50%) rotate(-20deg);
  border: 4px solid var(--stamp);
  color: var(--stamp);
  font-family: var(--font-display);
  font-size: 2.75rem;
  font-weight: 800;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  padding: 0.2rem 1.5rem;
  opacity: 0.6;
  pointer-events: none;
  z-index: 2;
}

.paid-stamp.is-visible {
  display: block;
}

@media (prefers-reduced-motion: no-preference) {
  .paid-stamp.is-stamping {
    animation: stamp-in var(--dur-stamp) cubic-bezier(0.2, 0.9, 0.25, 1.15) both;
  }

  @keyframes stamp-in {
    0% {
      opacity: 0;
      transform: translate(-50%, -50%) scale(1.4) rotate(-6deg);
    }
    55% {
      opacity: 0.6;
      transform: translate(-50%, -50%) scale(0.94) rotate(-22deg);
    }
    100% {
      opacity: 0.6;
      transform: translate(-50%, -50%) scale(1) rotate(-20deg);
    }
  }
}

/* ==========================================================
   Templates — Classic / Modern / Minimal
   Only visual styling changes here; invoice data is untouched.
   ========================================================== */

/* ---- Classic: black & white, bordered table, traditional ---- */
.preview-sheet[data-template="classic"] .preview-header {
  border-bottom: 2px solid var(--ink);
}

.preview-sheet[data-template="classic"] .preview-table {
  border: 1px solid var(--ink);
}

.preview-sheet[data-template="classic"] .preview-table th,
.preview-sheet[data-template="classic"] .preview-table td {
  border: 1px solid var(--ink-muted);
}

.preview-sheet[data-template="classic"] .preview-table th {
  color: var(--ink);
  background: var(--paper-shade);
}

.preview-sheet[data-template="classic"] .totals-row--final {
  border-top: 2px solid var(--ink);
}

/* ---- Modern: colored header band, borderless table, whitespace ---- */
.preview-sheet[data-template="modern"] {
  padding-top: 0;
}

.preview-sheet[data-template="modern"] .preview-header {
  background: var(--invoice-accent, var(--accent));
  border-bottom: none;
  margin: 0 -2.25rem 1.5rem;
  padding: 2.25rem 2.25rem 1.75rem;
}

.preview-sheet[data-template="modern"] .preview-business-name,
.preview-sheet[data-template="modern"] .preview-invoice-title h2 {
  color: var(--paper);
}

.preview-sheet[data-template="modern"] .preview-header .preview-line {
  color: rgba(252, 251, 248, 0.82);
}

.preview-sheet[data-template="modern"] .preview-header .preview-line .label {
  color: rgba(252, 251, 248, 0.68);
}

.preview-sheet[data-template="modern"] .preview-table th,
.preview-sheet[data-template="modern"] .preview-table td {
  border-bottom: none;
}

.preview-sheet[data-template="modern"] .preview-table thead tr {
  border-bottom: 2px solid var(--ledger-strong);
}

.preview-sheet[data-template="modern"] .preview-table tbody tr:not(:last-child) {
  border-bottom: 1px solid var(--paper-shade);
}

@media (max-width: 640px) {
  .preview-sheet[data-template="modern"] .preview-header {
    margin: 0 -1.25rem 1.25rem;
    padding: 1.75rem 1.25rem;
  }
}

/* ---- Minimal: no band, thin lines, small type, lots of space ---- */
.preview-sheet[data-template="minimal"] {
  padding: 3rem;
}

.preview-sheet[data-template="minimal"] .preview-header {
  border-bottom: 1px solid var(--ledger-strong);
  padding-bottom: 1.75rem;
}

.preview-sheet[data-template="minimal"] .preview-business-name {
  font-size: 1rem;
}

.preview-sheet[data-template="minimal"] .preview-invoice-title h2 {
  font-size: 1.4rem;
  font-weight: 600;
}

.preview-sheet[data-template="minimal"] .preview-line,
.preview-sheet[data-template="minimal"] .preview-table {
  font-size: 0.8rem;
}

.preview-sheet[data-template="minimal"] .preview-table th,
.preview-sheet[data-template="minimal"] .preview-table td {
  border-bottom: 1px solid var(--ledger-strong);
  padding: 0.75rem 0.4rem;
}

.preview-sheet[data-template="minimal"] .preview-parties,
.preview-sheet[data-template="minimal"] .preview-footer {
  padding-top: 1.75rem;
  padding-bottom: 1.75rem;
}

.preview-sheet[data-template="minimal"] .totals-row--final {
  border-top: 1px solid var(--invoice-accent, var(--accent));
}

@media (max-width: 640px) {
  .preview-sheet[data-template="minimal"] {
    padding: 1.5rem;
  }
}

/* ==========================================================
   History modal
   ========================================================== */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(17, 22, 28, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.5rem;
  z-index: 100;
}

.modal-overlay[hidden] {
  display: none;
}

.modal {
  background: var(--paper);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
  width: 100%;
  max-width: 720px;
  max-height: 85vh;
  display: flex;
  flex-direction: column;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 1.25rem 1.5rem;
  border-bottom: 1px solid var(--ledger-strong);
}

.modal-header h2 {
  font-family: var(--font-display);
  font-size: 1.2rem;
  font-weight: 700;
}

.modal-close {
  background: transparent;
  border: none;
  font-size: 1.5rem;
  line-height: 1;
  color: var(--ink-muted);
  cursor: pointer;
  padding: 0.2rem 0.4rem;
}

.modal-close:hover {
  color: var(--stamp);
}

.modal-body {
  padding: 1.25rem 1.5rem;
  overflow-y: auto;
}

.modal-footer {
  padding: 1rem 1.5rem;
  border-top: 1px solid var(--ledger-strong);
  display: flex;
  justify-content: flex-end;
}

.history-empty {
  color: var(--ink-muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 1.5rem 0;
}

.history-empty[hidden] {
  display: none;
}

.history-list {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.history-row {
  display: grid;
  grid-template-columns: 1fr 1.2fr 0.9fr 0.9fr auto;
  gap: 0.75rem;
  align-items: center;
  padding: 0.6rem 0.7rem;
  border: 1px solid var(--ledger-strong);
  border-radius: var(--radius-sm);
  font-size: 0.85rem;
}

.history-cell--number {
  font-weight: 700;
}

.history-cell--amount {
  font-weight: 600;
  text-align: right;
}

.history-cell--actions {
  display: flex;
  gap: 0.4rem;
  justify-self: end;
}

@media (max-width: 640px) {
  .history-row {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "number amount"
      "client date"
      "actions actions";
  }

  .history-cell--number { grid-area: number; }
  .history-cell--amount { grid-area: amount; text-align: right; }
  .history-cell--actions { grid-area: actions; justify-self: stretch; }
}

/* ==========================================================
   Print — only the invoice preview should print
   ========================================================== */
@media print {
  body * {
    visibility: hidden;
  }

  #invoice-preview,
  #invoice-preview * {
    visibility: visible;
  }

  #invoice-preview {
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    max-width: 100%;
    margin: 0;
    box-shadow: none;
    border: none;
    min-height: 0;
    transform: none !important;
    animation: none !important;
  }

  .site-nav,
  .site-footer,
  .tool-toolbar,
  .form-column,
  .modal-overlay,
  .page-hero,
  .content-section,
  .preview-fab,
  .drawer-backdrop {
    display: none !important;
  }
}
