:root {
  --bg0: #f3efe6;
  --bg1: #e7f0ef;
  --ink: #1c2422;
  --muted: #5c6b67;
  --accent: #0f6e56;
  --accent-2: #c45c26;
  --panel: rgba(255, 252, 247, 0.92);
  --line: #d5ddd9;
  --shadow: 0 18px 40px rgba(28, 36, 34, 0.08);
  --radius: 14px;
  --font-ui: "Noto Sans TC", "DM Sans", sans-serif;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-ui);
  background:
    radial-gradient(circle at 12% 10%, #dff3ee 0%, transparent 42%),
    radial-gradient(circle at 88% 0%, #f7e2d2 0%, transparent 36%),
    linear-gradient(160deg, var(--bg0), var(--bg1));
}

#app { min-height: 100%; display: flex; flex-direction: column; }
#app.auth-blocked {
  visibility: hidden;
  pointer-events: none;
  user-select: none;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  padding: 1rem 1.5rem;
  padding-top: calc(1rem + env(safe-area-inset-top, 0px));
  border-bottom: 1px solid rgba(28, 36, 34, 0.08);
  backdrop-filter: blur(8px);
  position: sticky;
  top: 0;
  z-index: 10;
  background: rgba(243, 239, 230, 0.85);
}

.topbar-main {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex: 1;
  min-width: 0;
}

.brand-mark {
  font-family: "DM Sans", var(--font-ui);
  font-weight: 700;
  font-size: 1.45rem;
  letter-spacing: -0.03em;
  display: block;
}
.brand-sub { color: var(--muted); font-size: 0.85rem; }

.current-tab-label {
  display: none;
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--ink);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 9.5rem;
}

.nav-menu-btn {
  display: none;
  align-items: center;
  gap: 0.35rem;
  flex-shrink: 0;
  padding: 0.4rem 0.7rem;
  min-height: 44px;
}
.nav-menu-icon {
  display: inline-block;
  width: 1.1rem;
  height: 0.85rem;
  border-top: 2px solid currentColor;
  border-bottom: 2px solid currentColor;
  background:
    linear-gradient(currentColor, currentColor) center / 100% 2px no-repeat;
  vertical-align: middle;
}
.nav-menu-text { font-size: 0.85rem; }

.nav-backdrop {
  display: none;
  pointer-events: none;
  visibility: hidden;
}

.sync-indicator {
  margin-left: auto;
  margin-right: 0.75rem;
  padding: 0.45rem 0.8rem;
  border-radius: 999px;
  border: 1px solid var(--line);
  background: #fff;
  font-size: 0.85rem;
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  white-space: nowrap;
}
.sync-indicator.online { background: #e8f5f0; border-color: #9dceb8; color: var(--accent); }
.sync-indicator.offline { background: #f8e8e4; border-color: #e2b4b4; color: #a33; }
.sync-indicator.syncing { background: #fff6e8; border-color: #e2c48a; color: #8a5a12; }

.tabs { display: flex; gap: 0.4rem; flex-wrap: wrap; }
.tab, button, .primary {
  font: inherit;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  padding: 0.55rem 0.9rem;
  cursor: pointer;
  min-height: 44px;
}
.tab.active, .primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}
.primary:hover { filter: brightness(1.05); }
button:disabled { opacity: 0.5; cursor: not-allowed; }

main {
  padding: 1.25rem 1.5rem 2rem;
  padding-bottom: calc(2rem + env(safe-area-inset-bottom, 0px));
  flex: 1;
}
.view { display: none; animation: fade 0.25s ease; }
.view.active { display: block; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }

.billing-layout {
  display: grid;
  grid-template-columns: minmax(280px, 360px) 1fr;
  gap: 1rem;
}
.panel {
  background: var(--panel);
  border: 1px solid rgba(28, 36, 34, 0.06);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem 1.25rem;
}
.panel h2 { margin: 0 0 0.75rem; font-size: 1.1rem; }
.panel h3 { margin: 0 0 0.5rem; font-size: 1rem; }

.row { display: flex; gap: 0.5rem; margin-bottom: 0.6rem; }
.row.gap { flex-wrap: wrap; }
.item-filter-stack {
  display: grid;
  gap: 0.55rem;
  margin-bottom: 0.6rem;
}
.item-filter-stack .item-category-label,
.item-filter-stack .item-search-label {
  display: grid;
  gap: 0.3rem;
  width: 100%;
  margin: 0;
  font-size: 0.9rem;
  color: var(--muted);
}
.item-filter-stack #item-category,
.item-filter-stack #item-search {
  width: 100%;
  min-height: 44px;
}
.item-filter-hint {
  margin: 0;
  font-size: 0.8rem;
  line-height: 1.35;
}
.grow { flex: 1; }
/* Text fields only — radios/checkboxes must not inherit width/min-height chrome
   (that stretched bilingual pay labels vs single-line FPS/PayMe unevenly). */
input:not([type="radio"]):not([type="checkbox"]),
select,
textarea {
  width: 100%;
  font: inherit;
  padding: 0.55rem 0.7rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  min-height: 44px;
}
input[type="radio"],
input[type="checkbox"] {
  width: 1.15rem;
  height: 1.15rem;
  min-width: 1.15rem;
  min-height: 1.15rem;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 0;
  background: transparent;
  flex-shrink: 0;
  align-self: center;
  accent-color: var(--accent);
  vertical-align: middle;
}
label { display: grid; gap: 0.25rem; font-size: 0.9rem; color: var(--muted); }
label.check { display: flex; align-items: center; gap: 0.5rem; color: var(--ink); }

.result-list {
  list-style: none;
  margin: 0 0 1rem;
  padding: 0;
  max-height: 180px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fff;
}
.result-list li {
  padding: 0.65rem 0.75rem;
  border-bottom: 1px solid var(--line);
  cursor: pointer;
}
.result-list li:hover { background: #eef7f4; }
.result-list li:last-child { border-bottom: 0; }
.selected {
  padding: 0.75rem;
  border-radius: 10px;
  background: #e8f5f0;
  margin-bottom: 1.2rem;
  min-height: 44px;
}
.selected.empty { background: #f4f1ea; color: var(--muted); }

.bill-table, .data-table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 1rem;
}
.bill-table th, .bill-table td,
.data-table th, .data-table td {
  text-align: left;
  padding: 0.55rem 0.4rem;
  border-bottom: 1px solid var(--line);
  vertical-align: top;
}
.bill-table input { min-height: 40px; padding: 0.35rem 0.45rem; }

.bill-controls, .payment-box, .form-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 0.75rem;
  margin-bottom: 1rem;
}
.form-grid .wide { grid-column: 1 / -1; }

.product-settings-form {
  display: grid;
  gap: 0.85rem;
  margin-bottom: 1.25rem;
}
.product-section {
  margin: 0;
  padding: 0.85rem 1rem 1rem;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.55);
}
.product-section legend {
  padding: 0 0.35rem;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--ink);
}
.product-section .form-grid { margin-bottom: 0; }
.product-section .form-note { margin: 0; font-size: 0.8rem; }
.product-form-actions { margin: 0; }
.product-settings-form textarea {
  min-height: 4.5rem;
  resize: vertical;
}

.totals-box {
  background: linear-gradient(135deg, #1c3d35, #0f6e56);
  color: #f7fffb;
  border-radius: 12px;
  padding: 0.9rem 1rem;
  margin-bottom: 1rem;
}
.totals-box div {
  display: flex;
  justify-content: space-between;
  margin: 0.2rem 0;
}
.totals-box .grand { font-size: 1.25rem; margin-top: 0.45rem; }

/* Payment picker: custom dots (native radios stay for a11y/JS but are visually
   hidden). #26 accent-color sizing still left Cash/Card huge vs FPS/PayMe tiny
   in some browsers; drawn controls are equal everywhere. */
.payment-box > h3,
.payment-box > .pay-methods,
.payment-box > #package-pay,
.payment-box > #split-payments {
  grid-column: 1 / -1;
}
.pay-methods {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem 0.85rem;
  margin-bottom: 0.75rem;
  align-items: stretch;
}
.pay-methods .pay-option {
  display: inline-flex;
  align-items: center;
  gap: 0.45rem;
  color: var(--ink);
  grid: none;
  min-height: 44px;
  min-width: 6.5rem;
  padding: 0.15rem 0.25rem;
  margin: 0;
  cursor: pointer;
  line-height: 1.2;
  font-size: 0.9rem;
}
.pay-methods .pay-option input[type="radio"] {
  position: absolute;
  width: 1px;
  height: 1px;
  min-width: 0;
  min-height: 0;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
  appearance: none;
  -webkit-appearance: none;
  accent-color: unset;
}
.pay-methods .pay-dot {
  box-sizing: border-box;
  width: 17px;
  height: 17px;
  min-width: 17px;
  min-height: 17px;
  border-radius: 50%;
  border: 2px solid #8a9490;
  background: #fff;
  flex-shrink: 0;
  display: inline-block;
  position: relative;
}
.pay-methods .pay-option input[type="radio"]:checked + .pay-dot {
  border-color: var(--accent);
  background: var(--accent);
  box-shadow: inset 0 0 0 3px #fff;
}
.pay-methods .pay-option:focus-within .pay-dot {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}
.pay-methods .pay-label {
  display: flex;
  flex-direction: column;
  gap: 0.05rem;
  color: var(--ink);
}
.pay-methods .pay-zh {
  font-size: 0.92rem;
  color: var(--ink);
}
.pay-methods .pay-en {
  font-size: 0.78rem;
  color: var(--muted);
}
.split.hidden { display: none; }
.actions { display: flex; gap: 0.6rem; flex-wrap: wrap; }
.status { min-height: 1.4em; color: var(--accent); }
.status.error { color: #a33; }
.file-btn {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  padding: 0.45rem 0.85rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
  font: inherit;
  color: var(--ink);
}
.file-btn:hover { border-color: var(--accent); color: var(--accent); }
.muted { color: var(--muted); font-size: 0.95rem; }
.badge-warn {
  display: inline-block;
  margin-left: 0.35rem;
  padding: 0.1rem 0.35rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: #6b4a00;
  background: #f5e6b8;
  border-radius: 0.25rem;
}
.badge.muted {
  display: inline-block;
  margin-left: 0.25rem;
  padding: 0.05rem 0.3rem;
  font-size: 0.7rem;
  font-weight: 600;
  color: var(--muted);
  background: #eee;
  border-radius: 0.25rem;
  vertical-align: middle;
}
pre.invoice-detail {
  white-space: pre-wrap;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 0.85rem;
  max-height: 420px;
  overflow: auto;
}

.invoice-detail {
  margin-top: 1rem;
  padding: 1rem;
  border-radius: 12px;
  background: #fff;
  border: 1px solid var(--line);
}
.invoice-detail.hidden { display: none; }

.customer-history-section {
  margin-top: 1.25rem;
  padding-top: 0.75rem;
  border-top: 1px solid var(--line);
  scroll-margin-top: 5rem;
}
.customer-history-section.is-active {
  outline: 2px solid var(--accent);
  outline-offset: 6px;
  border-radius: 8px;
}

.history-dialog {
  position: fixed;
  inset: 0;
  z-index: 80;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 1rem;
  background: rgba(28, 36, 34, 0.45);
}
.history-dialog:not(.hidden) {
  display: flex;
}
.history-dialog-sheet {
  width: min(920px, 100%);
  max-height: min(85vh, 720px);
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  padding: 1rem 1.1rem 1.25rem;
}
.history-dialog-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  margin-bottom: 0.35rem;
}
.history-dialog-head h2 {
  margin: 0;
  font-size: 1.15rem;
}
.history-dialog-head button {
  min-width: 44px;
  font-size: 1.4rem;
  line-height: 1;
}
.history-dialog-body {
  overflow: auto;
  margin-top: 0.5rem;
  -webkit-overflow-scrolling: touch;
}
@media (min-width: 768px) {
  .history-dialog {
    align-items: center;
  }
  .history-dialog-sheet {
    border-radius: var(--radius);
  }
}
.danger { color: #a33; border-color: #e2b4b4; }

.post-checkout-dialog .post-checkout-sheet {
  width: min(560px, 100%);
  max-height: min(85vh, 640px);
}
.post-checkout-invoice {
  margin: 0.25rem 0 0.5rem;
  font-size: 1.15rem;
  font-weight: 650;
}
.post-checkout-hint {
  margin: 0 0 0.75rem;
  line-height: 1.45;
}
.post-checkout-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  margin-top: 0.5rem;
}
.post-checkout-actions button {
  min-height: 44px;
}

/* Touch targets: keep ≥44px (already on buttons/text inputs); radios stay fixed size */
.tab,
button,
.primary,
input:not([type="radio"]):not([type="checkbox"]),
select,
textarea,
.result-list li {
  min-height: 44px;
}

.hidden { display: none !important; }

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1rem;
  background:
    radial-gradient(circle at 20% 20%, #dff3ee 0%, transparent 45%),
    linear-gradient(160deg, #f3efe6, #e7f0ef);
}
.auth-gate-card {
  width: min(420px, 100%);
  background: var(--panel);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.5rem;
  display: grid;
  gap: 0.75rem;
}
.auth-gate-card h1 {
  margin: 0;
  font-family: "DM Sans", var(--font-ui);
  font-size: 1.8rem;
  letter-spacing: -0.03em;
}
.auth-gate-card .btn-google {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0.65rem 1rem;
  border-radius: var(--radius);
  border: 1px solid var(--line);
  background: #fff;
  color: var(--ink);
  text-decoration: none;
  font-weight: 600;
  font-family: "DM Sans", var(--font-ui);
}
.auth-gate-card .btn-google:hover {
  border-color: var(--accent);
}
#gate-password-form {
  display: grid;
  gap: 0.75rem;
}
#btn-logout { display: none; }
body.vps-mode #btn-logout { display: inline-flex; align-items: center; }
body.vps-mode .sync-indicator,
body.vps-mode .tab[data-view="import"],
body.vps-mode .tab[data-view="paper"],
body.vps-mode .tab[data-view="sync"],
body.vps-mode .tab[data-view="billing"],
body.vps-mode .tab[data-view="customers"],
body.vps-mode .tab[data-view="items"],
body.vps-mode .tab[data-view="calendar"],
body.vps-mode .tab[data-view="reports"] {
  display: none;
}

.invoice-list-filters {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 0.55rem 0.75rem;
  margin: 0 0 0.75rem;
}
.invoice-list-filters label {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
  font-size: 0.85rem;
  min-width: 0;
}
.invoice-list-filters input[type="date"],
.invoice-list-filters input[type="search"] {
  min-height: 40px;
  padding: 0.35rem 0.55rem;
  border: 1px solid var(--line);
  border-radius: 8px;
  font: inherit;
  background: #fff;
}
.invoice-list-filters .invoice-list-search {
  flex: 1 1 12rem;
}
.invoice-list-filters .invoice-list-search input {
  width: 100%;
  box-sizing: border-box;
}
.invoice-list-filters #btn-invoices-search {
  min-height: 40px;
  flex: 0 0 auto;
}

.invoice-list-toolbar,
.invoice-list-pager {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.55rem 0.75rem;
  margin: 0 0 0.75rem;
}
.invoice-list-toolbar--bottom {
  margin: 0.85rem 0 0.35rem;
  padding-top: 0.65rem;
  border-top: 1px solid var(--line);
}
.invoice-list-toolbar .invoice-list-meta,
.invoice-list-pager .invoice-list-meta {
  margin: 0;
  flex: 1 1 auto;
  min-width: 0;
}
.invoice-list-nav {
  display: inline-flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.45rem;
  flex: 0 0 auto;
}
.invoice-page-indicator {
  font-size: 0.9rem;
  min-width: 5.5rem;
  text-align: center;
}
.btn-invoices-page:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

/* Desktop: sticky bottom pager so Prev/Next stay in view while scrolling the table */
@media (min-width: 601px) {
  #view-invoices.active .invoice-list-toolbar--bottom {
    position: sticky;
    bottom: 0;
    z-index: 5;
    background: rgba(255, 252, 247, 0.97);
    backdrop-filter: blur(6px);
    margin: 0.5rem 0 0;
    padding: 0.65rem 0.35rem;
    border-top: 1px solid var(--line);
    box-shadow: 0 -4px 12px rgba(28, 36, 34, 0.06);
  }
  #view-invoices.active .invoice-list-toolbar--bottom .btn-invoices-page:not(:disabled) {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
  }
}

.inv-actions-primary {
  display: inline-flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  align-items: center;
}
.inv-actions {
  position: relative;
  display: inline-block;
  vertical-align: top;
}
.inv-actions > summary {
  list-style: none;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  gap: 0.35rem;
  min-height: 44px;
  padding: 0.45rem 0.75rem;
  border-radius: 10px;
  border: 1px solid var(--line);
  background: #fff;
  font: inherit;
  user-select: none;
}
.inv-actions > summary::-webkit-details-marker { display: none; }
.inv-actions > summary::after {
  content: "▾";
  font-size: 0.75rem;
  color: var(--muted);
}
.inv-actions[open] > summary::after { content: "▴"; }
.inv-actions-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin-top: 0.4rem;
}
#invoice-table td:last-child {
  min-width: 11rem;
}
#invoice-table td:last-child button {
  margin: 0.15rem 0.15rem 0.15rem 0;
}

/* Desktop / tablet: keep 更多 as a real disclosure (do not dump every secondary button). */
@media (min-width: 601px) {
  .inv-actions-cell {
    display: flex;
    flex-wrap: wrap;
    gap: 0.35rem;
    align-items: flex-start;
  }
  .inv-actions > summary {
    min-height: 2.25rem;
    padding: 0.35rem 0.7rem;
  }
  .inv-actions-panel {
    position: absolute;
    z-index: 5;
    right: 0;
    min-width: 11rem;
    margin-top: 0.35rem;
    padding: 0.45rem;
    border: 1px solid var(--line);
    border-radius: 10px;
    background: #fff;
    box-shadow: 0 8px 24px rgba(28, 36, 34, 0.12);
    display: none;
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 0.3rem;
  }
  .inv-actions[open] > .inv-actions-panel {
    display: flex;
  }
  .inv-actions-panel button {
    width: 100%;
    margin: 0;
    justify-content: flex-start;
  }
}

/* Tablet 600–1024 */
@media (max-width: 1024px) {
  .billing-layout { grid-template-columns: 1fr; }
  .bill-controls, .payment-box, .form-grid { grid-template-columns: 1fr 1fr; }
  .topbar { flex-wrap: wrap; }
  .tabs { width: 100%; }
}

/* Phone <600 — compact sticky bar; tabs in drawer (not sticky grid) */
@media (max-width: 600px) {
  main {
    padding: 0.85rem;
    padding-bottom: calc(0.85rem + env(safe-area-inset-bottom, 0px) + 3.25rem);
  }

  .post-checkout-actions button {
    flex: 1 1 calc(50% - 0.5rem);
  }
  .post-checkout-actions #post-checkout-done {
    flex: 1 1 100%;
  }

  .topbar {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    padding: 0.45rem 0.65rem;
    padding-top: calc(0.45rem + env(safe-area-inset-top, 0px));
    gap: 0.4rem;
  }

  .topbar-main {
    width: 100%;
    gap: 0.4rem;
  }

  .brand-mark { font-size: 1.1rem; line-height: 1.15; }
  .brand-sub { display: none; }

  .current-tab-label {
    display: inline-block;
    flex: 1 1 auto;
    min-width: 0;
    max-width: none;
    padding: 0.2rem 0.45rem;
    border-radius: 8px;
    background: rgba(15, 110, 86, 0.08);
    border: 1px solid rgba(15, 110, 86, 0.18);
    font-size: 0.78rem;
    line-height: 1.25;
  }

  .sync-indicator {
    margin: 0;
    margin-left: 0;
    margin-right: 0;
    width: auto;
    flex-shrink: 0;
    padding: 0.3rem 0.55rem;
    min-height: 36px;
    font-size: 0.75rem;
    border-radius: 8px;
  }

  #btn-logout {
    min-height: 36px;
    padding: 0.3rem 0.55rem;
    font-size: 0.78rem;
  }

  .nav-menu-btn {
    display: inline-flex;
  }

  /*
   * Drawer lives inside .topbar (sticky z-index:10 stacking context).
   * Elevate the whole topbar above the backdrop when open so tab buttons
   * and 選單 receive taps; closed backdrop must never capture clicks.
   */
  body.nav-open .topbar {
    z-index: 50;
  }

  /* Drawer: full tab list off sticky header */
  .tabs {
    display: none;
    position: fixed;
    top: calc(env(safe-area-inset-top, 0px) + 3.1rem);
    left: 0.65rem;
    right: 0.65rem;
    width: auto;
    max-height: min(70vh, calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 5rem));
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
    grid-template-columns: 1fr 1fr;
    gap: 0.35rem;
    padding: 0.55rem;
    background: rgba(255, 252, 247, 0.98);
    border: 1px solid var(--line);
    border-radius: 12px;
    box-shadow: 0 12px 28px rgba(28, 36, 34, 0.14);
    z-index: 2;
    pointer-events: none;
  }

  body.nav-open .tabs {
    display: grid;
    pointer-events: auto;
  }

  body.nav-open .nav-backdrop {
    display: block;
    visibility: visible;
    pointer-events: auto;
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    margin: 0;
    background: rgba(28, 36, 34, 0.28);
    z-index: 40; /* below elevated topbar (50); above page content */
    border: 0;
    padding: 0;
    -webkit-tap-highlight-color: transparent;
  }

  body.nav-open .nav-menu-btn {
    background: var(--accent);
    border-color: var(--accent);
    color: #fff;
    position: relative;
    z-index: 3;
  }

  .tab {
    padding: 0.45rem 0.55rem;
    font-size: 0.88rem;
    min-height: 42px;
  }

  .bill-controls, .payment-box, .form-grid { grid-template-columns: 1fr; }
  .bill-table { font-size: 0.9rem; display: block; overflow-x: auto; }
  .data-table { width: 100%; }

  /* Invoices: full-width labeled cards (thead is sibling of #invoice-table tbody) */
  #view-invoices .data-table {
    display: block;
    width: 100%;
    border-collapse: separate;
  }
  #view-invoices .data-table > thead {
    display: none;
  }
  #invoice-table {
    display: block;
    width: 100%;
  }
  #invoice-table tr.invoice-row,
  #invoice-table tr {
    display: block;
    width: 100%;
    max-width: 100%;
    box-sizing: border-box;
    margin: 0 0 0.85rem;
    padding: 0.85rem 0.9rem;
    border: 1px solid var(--line);
    border-radius: 12px;
    background: #fff;
  }
  #invoice-table td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 0.75rem;
    width: 100%;
    box-sizing: border-box;
    border: 0;
    border-bottom: 1px solid rgba(28, 36, 34, 0.06);
    padding: 0.4rem 0;
    text-align: right;
    font-size: 0.95rem;
  }
  #invoice-table td:last-child {
    border-bottom: 0;
  }
  #invoice-table td::before {
    content: attr(data-label);
    flex: 0 0 auto;
    color: var(--muted);
    font-size: 0.78rem;
    font-weight: 500;
    text-align: left;
  }
  #invoice-table td.inv-actions-cell {
    display: flex;
    flex-direction: column;
    align-items: stretch;
    gap: 0.45rem;
    min-width: 0;
    margin-top: 0.35rem;
    padding-top: 0.55rem;
    text-align: left;
  }
  #invoice-table td.inv-actions-cell::before {
    margin-bottom: 0.15rem;
  }
  #invoice-table .inv-actions-primary {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 0.45rem;
    width: 100%;
  }
  #invoice-table .inv-actions-primary button:first-child {
    grid-column: 1 / -1;
  }
  #invoice-table .inv-actions-primary button,
  #invoice-table .inv-actions > summary,
  #invoice-table .inv-actions-panel button {
    width: 100%;
    margin: 0;
    min-height: 44px;
    padding: 0.55rem 0.65rem;
    font-size: 0.92rem;
    justify-content: center;
  }
  #invoice-table .inv-actions {
    display: block;
    width: 100%;
  }
  #invoice-table .inv-actions > summary {
    width: 100%;
    box-sizing: border-box;
  }
  #invoice-table .inv-actions-panel {
    display: grid;
    grid-template-columns: 1fr;
    gap: 0.4rem;
    margin-top: 0.45rem;
  }
  #invoice-table .inv-actions-panel button.danger {
    color: #a33;
  }

  /* Sticky filter + meta under compact topbar while scrolling invoices */
  #view-invoices.active .invoice-list-filters {
    position: sticky;
    top: calc(env(safe-area-inset-top, 0px) + 3.05rem);
    z-index: 7;
    background: rgba(255, 252, 247, 0.96);
    backdrop-filter: blur(6px);
    padding: 0.45rem 0.35rem 0.35rem;
    margin: 0 -0.35rem 0.45rem;
    border-bottom: 1px solid var(--line);
  }
  #view-invoices.active .invoice-list-toolbar--top {
    position: sticky;
    top: calc(env(safe-area-inset-top, 0px) + 3.05rem + 5.6rem);
    z-index: 6;
    background: rgba(255, 252, 247, 0.96);
    backdrop-filter: blur(6px);
    padding: 0.35rem 0.35rem;
    margin: 0 -0.35rem 0.65rem;
    border-bottom: 1px solid var(--line);
    border-radius: 0;
  }
  #view-invoices.active .invoice-list-filters label {
    flex: 1 1 calc(50% - 0.4rem);
  }
  #view-invoices.active .invoice-list-filters .invoice-list-search {
    flex: 1 1 100%;
  }
  #view-invoices.active .invoice-list-filters #btn-invoices-search {
    flex: 1 1 100%;
    min-height: 44px;
  }
  #view-invoices.active .invoice-list-nav {
    width: 100%;
    justify-content: space-between;
  }
  #view-invoices.active .btn-invoices-page {
    min-height: 44px;
    padding: 0.45rem 0.85rem;
  }
  /* Bottom pager remains in document flow on phone (top sticky covers mid-scroll) */
  #view-invoices.active .invoice-list-toolbar--bottom {
    position: static;
    box-shadow: none;
  }
}

/* Prefer landscape / short phone: keep bar compact, drawer scrollable */
@media (max-width: 600px) and (max-height: 480px) {
  .tabs {
    max-height: calc(100dvh - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px) - 3.5rem);
  }
}
