:root {
  --accent: #2f5aa8;
  --accent-dark: #1f3f7a;
  --accent-light: #eaf0fa;
  --ink: #1c2530;
  --muted: #6b7480;
  --line: #d7dce3;
  --paper: #ffffff;
  --bg: #eef1f5;
  --radius: 10px;
}

* { box-sizing: border-box; }

[hidden] { display: none !important; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

/* ---------- Toolbar ---------- */
.toolbar {
  position: sticky;
  top: 0;
  z-index: 10;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}

.toolbar-brand {
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.2px;
  color: var(--accent-dark);
}

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

.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 8px 14px;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
  transition: background .15s ease, border-color .15s ease, transform .05s ease;
}

.btn:hover { background: #f5f7fa; border-color: #c3ccd6; }
.btn:active { transform: translateY(1px); }

.btn-ghost { color: var(--ink); }

.btn-primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.btn-primary:hover { background: var(--accent-dark); border-color: var(--accent-dark); }

/* ---------- Page / Sheet layout ---------- */
.page-wrap {
  display: flex;
  justify-content: center;
  padding: 32px 16px 64px;
}

.sheet {
  width: 210mm;
  min-height: 297mm;
  background: var(--paper);
  box-shadow: 0 4px 24px rgba(20, 30, 50, 0.12), 0 1px 3px rgba(20,30,50,0.08);
  border-radius: var(--radius);
  padding: 20mm 16mm;
  position: relative;
}

/* ---------- Inputs baseline: look like text until focused ---------- */
input {
  font-family: inherit;
  color: inherit;
  border: 1px solid transparent;
  background: transparent;
  border-radius: 4px;
  padding: 2px 4px;
  outline: none;
}

input:hover {
  background: var(--accent-light);
}

input:focus {
  background: var(--accent-light);
  border-color: var(--accent);
}

/* ---------- Header ---------- */
.sheet-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 24px;
  margin-bottom: 28px;
}

.brand-block {
  display: flex;
  gap: 14px;
  align-items: flex-start;
}

.logo-drop {
  width: 64px;
  height: 64px;
  border: 1.5px dashed var(--line);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  cursor: pointer;
  overflow: hidden;
  color: var(--muted);
  font-size: 11px;
  text-align: center;
}
.logo-drop:hover { border-color: var(--accent); color: var(--accent); }
.logo-drop img { width: 100%; height: 100%; object-fit: contain; }

.brand-fields {
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.f-company {
  font-size: 19px;
  font-weight: 800;
  color: var(--accent-dark);
  width: 260px;
  padding-left: 4px;
}

.f-line {
  font-size: 12.5px;
  color: var(--muted);
  width: 260px;
}

.f-line.bold {
  color: var(--ink);
  font-weight: 700;
  font-size: 13.5px;
}

.title-block h1 {
  margin: 0;
  font-size: 34px;
  letter-spacing: 2px;
  color: var(--accent-dark);
  font-weight: 800;
  text-align: right;
}

/* ---------- Info row: Bill To + meta stack side by side ---------- */
.info-row {
  display: flex;
  justify-content: space-between;
  align-items: stretch;
  gap: 20px;
  margin-bottom: 26px;
}

.meta-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 230px;
  flex-shrink: 0;
}

.meta-box {
  display: flex;
  align-items: stretch;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--line);
}

.meta-box-head {
  background: var(--accent);
  color: #fff;
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  padding: 8px 10px;
  display: flex;
  align-items: center;
  white-space: nowrap;
}

.meta-input {
  display: block;
  flex: 1;
  width: auto;
  min-width: 0;
  padding: 8px 10px;
  font-size: 13px;
  font-weight: 600;
  border-radius: 0;
  text-align: right;
}

/* ---------- Bill To ---------- */
.bill-to {
  padding: 12px 14px;
  background: var(--accent-light);
  border-radius: 8px;
  display: inline-flex;
  flex-direction: column;
  gap: 3px;
  min-width: 300px;
  flex: 1;
}

.bill-to-head {
  font-size: 10.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--accent);
  margin-bottom: 4px;
}

.bill-to .f-line,
.bill-to .f-line.bold {
  width: 280px;
  background: transparent;
}

/* ---------- Items table ---------- */
.items-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 8px;
}

.items-table thead th {
  background: var(--accent);
  color: #fff;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  font-weight: 700;
  text-align: left;
  padding: 9px 10px;
}

.items-table th.col-qty,
.items-table th.col-price,
.items-table th.col-amount {
  text-align: right;
}

.items-table th.col-del { width: 32px; }

.items-table td {
  border-bottom: 1px solid var(--line);
  padding: 6px 8px;
  font-size: 13px;
  vertical-align: middle;
}

.items-table td.col-qty,
.items-table td.col-price,
.items-table td.col-amount {
  text-align: right;
}

.items-table input {
  width: 100%;
  text-align: left;
}

.items-table td.col-qty input,
.items-table td.col-price input,
.items-table td.col-amount input {
  text-align: right;
}

.items-table td.col-amount input {
  font-weight: 700;
}

.row-delete-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  width: 24px;
  height: 24px;
  border-radius: 6px;
  display: flex;
  align-items: center;
  justify-content: center;
}
.row-delete-btn:hover { background: #fbe4e4; color: #c62828; }

/* ---------- Bottom: notes + totals ---------- */
.bottom-row {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 20px;
  margin-top: 18px;
}

.notes-block {
  font-size: 12.5px;
  color: var(--accent-dark);
  font-style: italic;
  display: flex;
  flex-direction: column;
  gap: 6px;
  max-width: 320px;
}

.notes-line {
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 4px;
  flex-wrap: wrap;
}

.bank-field {
  display: grid;
  grid-template-columns: 92px 1fr;
  align-items: center;
  gap: 6px;
}

.bank-field span { color: var(--muted); font-weight: 600; font-style: normal; }

.inline-input {
  font-style: italic;
  font-weight: 700;
  color: var(--accent-dark);
  min-width: 40px;
}
.inline-input.wide { width: 100%; }
.inline-input.tiny {
  width: 32px;
  min-width: 0;
  padding: 2px 2px;
  text-align: center;
}

.totals-block {
  width: 260px;
  background: var(--accent-light);
  border-radius: 8px;
  padding: 12px 16px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.totals-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 13px;
}

.totals-row span:first-child { color: var(--muted); font-weight: 600; }
.totals-row span:last-child { font-weight: 700; }

.tax-input {
  width: 70px;
  text-align: right;
  font-weight: 700;
}

.total-final {
  border-top: 1.5px solid var(--accent);
  padding-top: 8px;
  margin-top: 2px;
  font-size: 16px;
}
.total-final span:first-child { color: var(--accent-dark); font-weight: 800; }
.total-final span:last-child { color: var(--accent-dark); font-weight: 800; }

.currency-input {
  border: none;
  font-weight: 800;
  color: var(--accent-dark);
  width: 42px;
  text-align: center;
  padding: 0 2px;
}

/* ---------- Footer ---------- */
.sheet-footer {
  margin-top: 40px;
  text-align: center;
  font-size: 12px;
  color: var(--muted);
}

.sheet-footer .inline-input {
  font-weight: 700;
  color: var(--accent);
  font-style: normal;
  width: 200px;
}

/* ---------- Print ---------- */
@media print {
  .no-print, .toolbar, .row-delete-btn { display: none !important; }

  html, body {
    background: #fff;
  }

  .page-wrap {
    padding: 0;
  }

  .sheet {
    width: auto;
    min-height: 0;
    box-shadow: none;
    border-radius: 0;
    margin: 0;
    padding: 10mm 14mm;
  }

  input {
    border-color: transparent !important;
    background: transparent !important;
  }

  .logo-drop {
    border: none;
  }

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

/* ---------- PDF export snapshot (toggled via JS on #savePdfBtn) ---------- */
body.exporting .no-print,
body.exporting .row-delete-btn {
  display: none !important;
}

body.exporting .sheet {
  box-shadow: none;
  border-radius: 0;
}

body.exporting input {
  border-color: transparent !important;
  background: transparent !important;
}

body.exporting .logo-drop {
  border: none;
}

/* ---------- Passphrase modal ---------- */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(20, 28, 42, 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}

.modal {
  background: #fff;
  border-radius: 12px;
  padding: 28px 26px 22px;
  width: 100%;
  max-width: 360px;
  box-shadow: 0 12px 40px rgba(15, 23, 40, 0.25);
}

.modal h2 {
  margin: 0 0 8px;
  font-size: 18px;
  color: var(--accent-dark);
}

.modal-desc {
  margin: 0 0 16px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.4;
}

.modal-input {
  display: block;
  width: 100%;
  padding: 10px 12px;
  font-size: 14px;
  border: 1px solid var(--line);
  border-radius: 7px;
  margin-bottom: 10px;
  background: #fff;
}
.modal-input:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--accent-light);
}

.modal-error {
  color: #c62828;
  font-size: 12.5px;
  font-weight: 600;
  margin: -2px 0 12px;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 4px;
}

.modal-skip {
  display: block;
  margin: 14px auto 0;
  border: none;
  background: none;
  color: var(--muted);
  font-size: 12.5px;
  text-decoration: underline;
  cursor: pointer;
}
.modal-skip:hover { color: var(--accent-dark); }

@media (max-width: 850px) {
  .sheet { width: 100%; padding: 8mm; }
  .info-row { flex-direction: column; }
  .meta-stack { width: 100%; }
  .toolbar { flex-direction: column; gap: 10px; align-items: stretch; }
  .toolbar-actions { flex-wrap: wrap; justify-content: center; }
}
