:root {
  --navy: #1b2a4a;
  --navy-hi: #24365c;
  --orange: #e8600a;
  --ink: #1b2a4a;
  --muted: #6a7280;
  --line: #dde1e8;
  --bg: #f5f6f8;
  --danger: #b91c1c;
  --danger-bg: #fdecec;
  --ok: #1a7f37;
  --ok-bg: #e7f6ec;
  --wa: #25d366;
  --tap: 44px;
  --shadow: 0 1px 2px rgba(27, 42, 74, .06), 0 4px 16px rgba(27, 42, 74, .05);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }   /* author display rules must not defeat it */
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 16px; line-height: 1.5;
  color: var(--ink); background: var(--bg);
}

/* ---------- chrome ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 16px; background: var(--navy); color: #fff;
}
.brand { display: flex; align-items: center; gap: 10px; color: #fff; text-decoration: none; }
.brand-logo { width: 30px; height: 30px; border-radius: 6px; background: #fff; object-fit: contain; padding: 2px; }
.brand-name { font-weight: 700; letter-spacing: .01em; }
.brand-name span { color: var(--orange); font-weight: 600; }
.topbar form { margin: 0; }
.signout {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; border-radius: 8px;
  background: none; border: 1px solid transparent; color: #b9c2d6; cursor: pointer;
}
.signout:hover { color: #fff; border-color: rgba(255, 255, 255, .25); }

.wrap { max-width: 820px; margin: 0 auto; padding: 24px 16px 56px; }
h1 { font-size: 22px; margin: 0 0 4px; letter-spacing: -0.01em; }
h2 { font-size: 15px; margin: 26px 0 10px; text-transform: none; color: var(--ink); }

.page-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 18px; }
.crumb { display: inline-flex; align-items: center; gap: 4px; color: var(--muted); text-decoration: none; font-size: 14px; font-weight: 600; }
.crumb:hover { color: var(--navy); }

.flash {
  max-width: 820px; margin: 14px auto 0;
  display: flex; align-items: center; gap: 10px;
  padding: 12px 16px; border-radius: 8px; font-weight: 500;
}
.flash .ico { width: 18px; }
.flash-success { background: var(--ok-bg); color: var(--ok); }
.flash-error { background: var(--danger-bg); color: var(--danger); }

/* ---------- cards ---------- */
.card { background: #fff; border: 1px solid var(--line); border-radius: 12px; padding: 22px; box-shadow: var(--shadow); }
.card-narrow { max-width: 400px; margin: 56px auto; }
/* share card at the top of the invoice form */
.share-card { display: flex; align-items: center; justify-content: space-between; gap: 18px; margin-bottom: 22px; }
.share-card-main h2 { margin: 0 0 2px; }
.share-card .actions { flex-wrap: nowrap; }

.login-card { text-align: center; }
.login-card h1 { margin-bottom: 2px; }
.login-card .form-error { text-align: left; }
.login-mark {
  width: 46px; height: 46px; margin: 0 auto 12px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 12px; background: var(--navy); color: #fff;
}
.login-mark .ico { width: 22px; height: 22px; }
.stack { display: flex; flex-direction: column; gap: 4px; margin-top: 18px; text-align: left; }
.stack .btn { width: 100%; margin-top: 8px; }

.list-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 16px; }
.filter { display: block; margin-bottom: 14px; }
.filter input { margin-top: 0; }

.empty {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  text-align: center; color: var(--muted);
  background: #fff; border: 1px dashed var(--line); border-radius: 12px; padding: 40px 24px;
}
.empty .ico { width: 34px; height: 34px; color: var(--line); }
.empty strong { color: var(--ink); }

/* ---------- form controls ---------- */
label { display: block; margin-bottom: 16px; font-size: 13px; font-weight: 600; color: var(--muted); }
label .opt { font-weight: 400; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; }
input, select {
  display: block; width: 100%; margin-top: 5px;
  min-height: var(--tap); padding: 10px 12px;
  font-size: 16px; font-family: inherit;
  border: 1px solid var(--line); border-radius: 8px;
  background: #fff; color: var(--ink);
  transition: border-color .12s ease, box-shadow .12s ease;
}
input:hover, select:hover { border-color: #c7ccd6; }
input:focus, select:focus { outline: 0; border-color: var(--orange); box-shadow: 0 0 0 3px rgba(232, 96, 10, .15); }
input:focus-visible, select:focus-visible, .btn:focus-visible, .icon-btn:focus-visible, .crumb:focus-visible, .signout:focus-visible {
  outline: 2px solid var(--orange); outline-offset: 2px;
}
input[readonly], input:disabled, select:disabled { background: #f1f2f4; color: var(--muted); cursor: not-allowed; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 0 20px; }

/* ---------- data tables ---------- */
table.grid { width: 100%; border-collapse: collapse; background: #fff; border: 1px solid var(--line); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
table.grid th, table.grid td { padding: 12px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
table.grid th { background: #fafbfc; font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); font-weight: 700; }
table.grid tr:last-child td { border-bottom: 0; }
table.invoice-list tbody tr:hover td { background: #fafbff; }
.num { text-align: right; }
.inv-no { font-weight: 700; color: var(--navy); text-decoration: none; }
.inv-no:hover { color: var(--orange); }
.total-cell { font-weight: 700; }
.row-actions { display: flex; gap: 16px; align-items: center; justify-content: center; }
.row-actions form { display: inline-flex; margin: 0; }
table.invoice-list th:last-child,
table.invoice-list td.row-actions { width: 220px; text-align: center; }

.line-items input { margin-top: 0; }
.line-items .qty { max-width: 84px; }
.line-items .price { max-width: 120px; }
.line-items .qty, .line-items .price { margin-left: auto; }
.line-items td { vertical-align: top; }

.li-controls { display: flex; flex-wrap: wrap; gap: 12px; align-items: end; margin: 16px 0 4px; }
.li-controls label { margin-bottom: 0; flex: 1 1 200px; }

.actions { display: flex; gap: 10px; flex-wrap: wrap; }

/* sticky Save bar — pinned to the viewport bottom while the form scrolls */
.form-sticky {
  position: sticky; bottom: 0; z-index: 5;
  margin: 26px -16px -56px; padding: 14px 16px;
  display: flex; align-items: center; justify-content: flex-end; gap: 14px;
  background: rgba(245, 246, 248, .94);
  border-top: 1px solid var(--line);
  -webkit-backdrop-filter: blur(4px); backdrop-filter: blur(4px);
}
.form-sticky .btn { min-width: 190px; }
.save-hint { color: var(--orange); font-size: 13px; font-weight: 600; }

/* ---------- buttons ---------- */
.ico { width: 18px; height: 18px; flex: none; display: block; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: var(--tap); padding: 10px 18px;
  border: 1px solid var(--navy); border-radius: 8px;
  background: #fff; color: var(--navy);
  font-size: 15px; font-weight: 600; font-family: inherit;
  text-decoration: none; cursor: pointer; white-space: nowrap;
  transition: background .12s ease, border-color .12s ease, color .12s ease, transform .04s ease;
}
.btn:hover { background: #eef1f6; }
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .5; cursor: not-allowed; pointer-events: none; }
.btn-primary { background: var(--navy); color: #fff; }
.btn-primary:hover { background: var(--navy-hi); }
.btn-wa { background: var(--wa); border-color: var(--wa); color: #05391a; }
.btn-wa:hover { background: #1fc25d; border-color: #1fc25d; }
.btn-ghost { border-color: var(--line); color: var(--navy); }
.btn-ghost:hover { border-color: #c7ccd6; background: #f4f6f9; }

.icon-btn {
  display: inline-flex; align-items: center; justify-content: center;
  width: 40px; height: 40px; padding: 0;
  border: 1px solid var(--line); border-radius: 8px;
  background: #fff; color: var(--navy); cursor: pointer;
  transition: background .12s ease, border-color .12s ease, color .12s ease;
}
.icon-btn:hover { background: #eef1f6; border-color: #c7ccd6; }
.icon-btn--danger:hover { background: var(--danger-bg); border-color: #eab8b8; color: var(--danger); }
.btn-danger { background: var(--danger); border-color: var(--danger); color: #fff; }
.btn-danger:hover { background: #a11616; border-color: #a11616; }

/* ---------- modals (native <dialog>) ---------- */
.modal {
  width: min(440px, calc(100vw - 24px));
  border: 1px solid var(--line); border-radius: 14px; padding: 22px;
  box-shadow: 0 12px 48px rgba(27, 42, 74, .22);
  color: var(--ink);
}
.modal::backdrop { background: rgba(27, 42, 74, .38); }
.modal[open] { animation: modal-in .14s ease; }
@keyframes modal-in { from { opacity: 0; transform: translateY(6px); } }
.modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.modal-head h2 { margin: 0; font-size: 17px; }
.modal-head .icon-btn { width: 34px; height: 34px; border-color: transparent; }
.modal .copy-row { display: flex; gap: 8px; margin-top: 5px; }
.modal .copy-row input { margin-top: 0; }
.modal .copy-row .btn { flex: none; }
.modal-or { text-align: center; color: var(--muted); font-size: 12px; text-transform: uppercase;
  letter-spacing: .08em; margin: 14px 0; }
.modal #wa-send { width: 100%; }
.modal-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }
.modal-actions .btn { min-width: 96px; }

.linkbtn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 40px;
  background: none; border: 0; padding: 4px 2px;
  color: var(--danger); font-size: 14px; font-weight: 600; cursor: pointer;
}
.linkbtn:hover { text-decoration: underline; }

.field-error { color: var(--danger); font-size: 13px; display: block; margin-top: 5px; font-weight: 500; }
.form-error { display: flex; align-items: center; gap: 8px; color: var(--danger); background: var(--danger-bg); padding: 10px 14px; border-radius: 8px; font-weight: 500; margin-bottom: 14px; }
.form-error .ico { width: 18px; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
code { background: #eef0f3; padding: 3px 6px; border-radius: 5px; word-break: break-all; font-size: 13px; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}

/* ==================== mobile ==================== */
@media (max-width: 640px) {
  .wrap { padding: 18px 12px 40px; }
  h1 { font-size: 20px; }
  .page-head { flex-direction: column; align-items: flex-start; gap: 6px; }

  .list-head { flex-direction: column; align-items: stretch; }
  .list-head .btn { width: 100%; }

  .two-col { grid-template-columns: 1fr; }

  /* tables -> stacked cards */
  table.grid, table.grid tbody, table.grid tr, table.grid td { display: block; width: 100%; }
  table.grid thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  table.grid { border: 0; background: transparent; box-shadow: none; }
  table.grid tr {
    background: #fff; border: 1px solid var(--line); border-radius: 12px;
    padding: 16px; margin-bottom: 12px; box-shadow: var(--shadow);
  }
  table.grid tr:last-child td { border-bottom: 0; }
  table.grid td { border: 0; padding: 5px 0; text-align: left; }
  table.grid td[data-label]::before {
    content: attr(data-label) "\00a0\00a0";
    display: inline-block; min-width: 92px;
    font-size: 11px; text-transform: uppercase; letter-spacing: .05em;
    color: var(--muted); font-weight: 700;
  }
  table.invoice-list td[data-label="Invoice"] { padding-bottom: 8px; }
  table.invoice-list td[data-label="Invoice"]::before { display: none; }
  table.invoice-list td[data-label="Invoice"] .inv-no { font-size: 18px; }
  table.invoice-list td[data-label="Total"] { font-size: 17px; padding-top: 8px; }
  /* out-specify `table.grid td { display:block }` above so flex centring applies */
  table.invoice-list td.row-actions {
    display: flex; flex-wrap: wrap; justify-content: center; gap: 18px;
    width: auto; padding-top: 14px; border-top: 1px solid var(--line); margin-top: 10px;
  }
  .row-actions .icon-btn { width: 44px; height: 44px; }

  /* line-item card: description full width, qty + price side by side */
  table.line-items tr.li-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
  table.line-items td { padding: 0; width: auto; }
  table.line-items td[data-cell="desc"] { grid-column: 1 / -1; }
  table.line-items td[data-label] { align-self: end; }
  table.line-items td[data-label]::before { display: block; min-width: 0; margin-bottom: 4px; }
  table.line-items .qty, table.line-items .price { max-width: none; width: 100%; margin-left: 0; }
  table.line-items td[data-cell="remove"] { grid-column: 1 / -1; }

  .li-controls { flex-direction: column; align-items: stretch; gap: 10px; }
  .li-controls label { flex: 0 0 auto; }
  .li-controls .btn { width: 100%; }

  .share-card { flex-direction: column; align-items: stretch; }
  .share-card .actions { flex-wrap: wrap; }
  .share-card .actions .btn { flex: 1 1 140px; }

  .form-sticky { margin: 22px -12px -40px; padding: 12px; }
  .form-sticky .btn { width: 100%; min-width: 0; }
}
