/* ============ Invoice Maker ============ */

:root {
  --bg: #f2f1ec;
  --surface: #ffffff;
  --ink: #1d2833;
  --ink-soft: #3d4750;
  --muted: #6d7680;
  --faint: #9aa2ab;
  --line: #e2dfd6;
  --line-strong: #cdc8bc;
  --accent: #17594a;
  --accent-deep: #124a3d;
  --accent-soft: #eaf2ee;
  --danger: #b0423c;
  --danger-soft: #fdf2f1;
  --shadow-sheet: 0 1px 2px rgba(29, 40, 51, 0.06), 0 14px 36px rgba(29, 40, 51, 0.13);
  --font-ui: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Noto Sans", sans-serif;
  --font-serif: Georgia, "Iowan Old Style", "Times New Roman", Times, serif;
  --topbar-h: 57px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--font-ui);
  font-size: 14.5px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

svg { display: block; }

/* ---------- Top bar ---------- */

.topbar {
  position: sticky;
  top: 0;
  z-index: 40;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--line);
}

.topbar-inner {
  max-width: 1480px;
  margin: 0 auto;
  padding: 10px 28px;
  display: flex;
  align-items: center;
  gap: 16px;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }
.brand-text { display: grid; gap: 2px; }
.brand-name { font-size: 15px; font-weight: 700; letter-spacing: -0.01em; line-height: 1.2; }

.save-hint { display: flex; align-items: center; gap: 6px; font-size: 11.5px; color: var(--muted); line-height: 1.2; }
.save-dot {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--accent);
  opacity: 0.25;
  transition: opacity 250ms ease-out;
}
.save-dot.on { opacity: 1; }

.topbar-spacer { flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 10px; }

/* ---------- Buttons ---------- */

.btn {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid transparent;
  border-radius: 9px;
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 15px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  white-space: nowrap;
  transition: background 130ms ease-out, border-color 130ms ease-out, color 130ms ease-out, transform 120ms ease-out;
}
.btn:active { transform: scale(0.97); }
.btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(23, 89, 74, 0.28); }

.btn-primary { background: var(--accent); color: #ffffff; box-shadow: 0 1px 2px rgba(18, 74, 61, 0.28); }
.btn-primary:hover { background: var(--accent-deep); }

.btn-ghost { background: var(--surface); border-color: var(--line); color: var(--ink-soft); }
.btn-ghost:hover { border-color: var(--line-strong); background: #faf9f5; }
.btn-ghost.armed { border-color: var(--danger); background: var(--danger-soft); color: var(--danger); }

.label-short { display: none; }

/* ---------- Layout ---------- */

.app {
  max-width: 1480px;
  margin: 0 auto;
  padding: 28px 28px 64px;
  display: grid;
  grid-template-columns: minmax(0, 460px) minmax(0, 1fr);
  gap: 34px;
  align-items: start;
}

.form-col { display: grid; gap: 16px; min-width: 0; }
.preview-col { min-width: 0; }

.preview-sticky {
  position: sticky;
  top: calc(var(--topbar-h) + 18px);
}

.preview-frame { display: flex; }
.sheet-hug { margin-inline: auto; flex: none; }

@media (max-width: 1023px) {
  .app { grid-template-columns: minmax(0, 1fr); gap: 30px; }
  .preview-sticky { position: static; top: auto; }
}

/* ---------- Cards & fields ---------- */

.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 20px;
  display: grid;
  gap: 14px;
}

.card-title {
  margin: 0;
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.hint-label { font-weight: 500; letter-spacing: 0; text-transform: none; color: var(--faint); }
.micro-hint { font-size: 11.5px; color: var(--faint); }

.field { display: grid; gap: 6px; min-width: 0; }
.field-label { font-size: 12px; font-weight: 600; color: #4c565f; line-height: 1.3; }

.input, .textarea, .select {
  font: inherit;
  font-size: 14px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid #d8d4c9;
  border-radius: 8px;
  width: 100%;
  transition: border-color 130ms ease-out, box-shadow 130ms ease-out;
}
.input, .select { height: 38px; padding: 0 11px; }
.textarea { padding: 9px 11px; resize: vertical; min-height: 66px; line-height: 1.45; }
.input::placeholder, .textarea::placeholder { color: var(--faint); }
.input:hover, .textarea:hover, .select:hover { border-color: #c3beb1; }
.input:focus, .textarea:focus, .select:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(23, 89, 74, 0.13);
}

.select {
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg%20xmlns='http://www.w3.org/2000/svg'%20width='14'%20height='14'%20viewBox='0%200%2024%2024'%20fill='none'%20stroke='%236d7680'%20stroke-width='2.2'%20stroke-linecap='round'%20stroke-linejoin='round'%3E%3Cpath%20d='m6%209%206%206%206-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 32px;
  cursor: pointer;
}

.grid-2 { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; }

/* number inputs without spinners */
input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }
input[type="number"] { -moz-appearance: textfield; appearance: textfield; }

/* combo: discount type + value */
.combo {
  display: flex;
  height: 38px;
  border: 1px solid #d8d4c9;
  border-radius: 8px;
  background: var(--surface);
  transition: border-color 130ms ease-out, box-shadow 130ms ease-out;
}
.combo:hover { border-color: #c3beb1; }
.combo:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(23, 89, 74, 0.13);
}
.combo-select {
  width: 84px;
  flex: none;
  border: none;
  border-right: 1px solid #d8d4c9;
  border-radius: 7px 0 0 7px;
  background-color: transparent;
  background-position: right 7px center;
  padding-right: 24px;
  height: 100%;
  box-shadow: none;
}
.combo-select:focus, .combo-input:focus { outline: none; box-shadow: none; }
.combo-input {
  flex: 1;
  min-width: 0;
  border: none;
  border-radius: 0 7px 7px 0;
  height: 100%;
  box-shadow: none;
}

/* ---------- Line items ---------- */

.items-head {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 74px 112px 36px;
  gap: 8px;
}
.items-head span {
  font-size: 10.5px;
  font-weight: 700;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--faint);
}
.items-head span:first-child { padding-left: 12px; }
.items-head span:nth-child(2), .items-head span:nth-child(3) { text-align: right; padding-right: 12px; }

.item-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 74px 112px 36px;
  gap: 8px;
  align-items: center;
}
.item-row + .item-row { margin-top: 8px; }
.item-qty, .item-price { text-align: right; }

@media (max-width: 560px) {
  .items-head { display: none; }
  .item-row {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr) 36px;
    grid-template-areas: "desc desc" "qty price del";
    row-gap: 8px;
  }
  .cell-desc { grid-area: desc; }
  .cell-qty { grid-area: qty; }
  .cell-price { grid-area: price; }
  .cell-del { grid-area: del; justify-self: end; }
}

.icon-btn {
  width: 38px;
  height: 38px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--faint);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex: none;
  transition: background 130ms ease-out, color 130ms ease-out, transform 120ms ease-out;
}
.icon-btn:hover { background: var(--danger-soft); color: var(--danger); }
.icon-btn:active { transform: scale(0.92); }
.icon-btn:focus-visible { outline: none; border-color: var(--danger); box-shadow: 0 0 0 3px rgba(176, 66, 60, 0.2); }

.add-btn {
  margin-top: 2px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 10px 12px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: color 130ms ease-out, border-color 130ms ease-out, background 130ms ease-out;
}
.add-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }
.add-btn:active { transform: scale(0.99); }
.add-btn:focus-visible { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px rgba(23, 89, 74, 0.13); }

/* ---------- Logo upload ---------- */

.logo-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; }
.logo-thumb {
  height: 38px;
  max-width: 130px;
  object-fit: contain;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  padding: 2px;
}
.upload-btn {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border: 1px dashed var(--line-strong);
  border-radius: 8px;
  background: transparent;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: color 130ms ease-out, border-color 130ms ease-out, background 130ms ease-out;
}
.upload-btn:hover { color: var(--accent); border-color: var(--accent); background: var(--accent-soft); }

.link-btn {
  appearance: none;
  border: none;
  background: none;
  padding: 0;
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  color: var(--danger);
  cursor: pointer;
}
.link-btn:hover { text-decoration: underline; }
.link-btn:focus-visible { outline: none; box-shadow: 0 0 0 3px rgba(176, 66, 60, 0.2); border-radius: 4px; }

/* ---------- Invoice sheet ---------- */

.sheet {
  width: 794px;
  min-height: 1123px; /* A4 at 96dpi */
  background: #ffffff;
  border-radius: 7px;
  box-shadow: var(--shadow-sheet);
  transform-origin: top left;
  position: relative;
  overflow: hidden;
  padding: 52px 56px 64px;
  font-family: var(--font-ui);
  font-size: 13.5px;
  line-height: 1.45;
  color: #17202a;
}

.sheet .ph { color: #a6adb4; font-weight: 400; }

.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 48px;
  padding-bottom: 22px;
  border-bottom: 1.5px solid #17202a;
}
.sheet-left { min-width: 0; }
.sheet-logo {
  display: block;
  max-height: 64px;
  max-width: 200px;
  object-fit: contain;
  margin-bottom: 14px;
}
.sheet-biz {
  margin: 0;
  font-family: var(--font-serif);
  font-size: 28px;
  line-height: 1.12;
  font-weight: 600;
  letter-spacing: -0.015em;
  color: #131c25;
}
.sheet-biz-meta { margin-top: 8px; font-size: 12px; color: #5b666f; line-height: 1.55; white-space: pre-wrap; }

.sheet-right { flex: none; text-align: right; }
.sheet-inv-label { font-size: 11px; font-weight: 700; letter-spacing: 0.22em; text-transform: uppercase; color: #5b666f; }
.sheet-inv-number { margin-top: 3px; font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
.sheet-dates { margin-top: 14px; display: grid; gap: 4px; }
.sheet-dates .d-row { display: flex; justify-content: flex-end; gap: 10px; font-size: 12px; line-height: 1.4; }
.sheet-dates .d-label { color: #8a939b; }
.sheet-dates .d-value { color: #17202a; font-weight: 500; font-variant-numeric: tabular-nums; }

.sheet-parties { display: flex; justify-content: space-between; gap: 48px; padding: 26px 0 28px; }
.bill-to { max-width: 58%; }
.mini-label { font-size: 10px; font-weight: 700; letter-spacing: 0.14em; text-transform: uppercase; color: #7d868f; margin-bottom: 7px; }
.bill-to-name { font-size: 15px; font-weight: 600; color: #131c25; }
.bill-to-meta { margin-top: 4px; font-size: 12.5px; color: #5b666f; line-height: 1.55; white-space: pre-wrap; }

.amount-due { text-align: right; flex: none; }
.amount-due-total {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: -0.01em;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
  line-height: 1.15;
}
.amount-due-sub { margin-top: 3px; font-size: 12px; color: #5b666f; }

.sheet-table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
.sheet-table thead th {
  font-size: 10px;
  font-weight: 700;
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: #7d868f;
  text-align: left;
  padding: 0 0 9px;
  border-bottom: 1px solid #d3d8dd;
}
.sheet-table thead th.num { text-align: right; }
.sheet-table td { padding: 11px 0; border-bottom: 1px solid #e7eaf0; vertical-align: top; }
.sheet-table td.num { text-align: right; font-variant-numeric: tabular-nums; }
.sheet-table td.desc { font-weight: 500; color: #17202a; padding-right: 28px; }
.sheet-table td.qty { color: #3f4a54; padding-right: 20px; }
.sheet-table td.up { color: #3f4a54; padding-right: 24px; }
.sheet-table td.amt { color: #17202a; font-weight: 600; }
.sheet-table th:nth-child(2) { width: 72px; }
.sheet-table th:nth-child(3) { width: 118px; }
.sheet-table th:nth-child(4) { width: 128px; }
.sheet-table tbody tr:last-child td { border-bottom: 1.5px solid #17202a; }
.sheet-empty { color: #a6adb4; padding: 14px 0; }

.sheet-totals { margin: 16px 0 0 auto; width: 264px; }
.t-row { display: flex; justify-content: space-between; align-items: baseline; padding: 6px 0; font-size: 13px; }
.t-label { color: #5b666f; }
.t-val { color: #17202a; font-weight: 500; font-variant-numeric: tabular-nums; }
.t-total { border-top: 1.5px solid #17202a; margin-top: 5px; padding-top: 11px; }
.t-total .t-label { font-weight: 700; color: #131c25; font-size: 14.5px; }
.t-total .t-val { font-weight: 700; color: var(--accent); font-size: 17px; }

.sheet-notes { margin-top: 40px; max-width: 62%; }
.sheet-notes-text { font-size: 13px; color: #3f4a54; line-height: 1.6; white-space: pre-wrap; }

.sheet-strip { position: absolute; left: 0; right: 0; bottom: 0; height: 8px; background: var(--accent); }

/* one-time entrance for the sheet */
@keyframes sheet-in {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}
@media (prefers-reduced-motion: no-preference) {
  .sheet-hug { animation: sheet-in 300ms cubic-bezier(0.23, 1, 0.32, 1) both; }
}

/* ---------- Compact top bar on small screens ---------- */

@media (max-width: 599px) {
  .topbar-inner { padding: 9px 16px; }
  .save-hint { display: none; }
  .btn { font-size: 13px; padding: 8px 12px; }
  .label-full { display: none; }
  .label-short { display: inline; }
  .app { padding: 20px 16px 48px; }
}

@media (max-width: 560px) {
  .grid-2 { grid-template-columns: 1fr; }
}

/* ---------- Print: only the sheet, true A4 ---------- */

@media print {
  @page { size: A4; margin: 0; }

  .screen-only { display: none !important; }

  html, body {
    background: #ffffff;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .app { display: block; padding: 0; margin: 0; max-width: none; }
  .preview-col { position: static; }
  .preview-sticky { position: static; top: auto; max-height: none; overflow: visible; }
  .preview-frame { display: block; overflow: visible; padding: 0; }
  .sheet-hug { width: auto !important; height: auto !important; margin: 0; animation: none; }

  .sheet {
    transform: none !important;
    width: 210mm !important;
    min-height: 297mm !important;
    border-radius: 0 !important;
    box-shadow: none !important;
    padding: 16mm 18mm 22mm !important;
    print-color-adjust: exact;
    -webkit-print-color-adjust: exact;
  }

  .sheet-table tr,
  .sheet-head, .sheet-parties, .sheet-totals, .sheet-notes {
    break-inside: avoid;
    page-break-inside: avoid;
  }
}
