/* ═══════════════════════════════════════════════════════════════
   GST Invoice Generator — Main Application Styles
   Premium billing software look, responsive from 320px to 4K
   ═══════════════════════════════════════════════════════════════ */

/* ── CSS Custom Properties ─────────────────────────────────── */
:root {
  --primary:        #1e40af;
  --primary-light:  #2563eb;
  --primary-dark:   #1e3a8a;
  --primary-bg:     #eff6ff;
  --primary-border: #bfdbfe;

  --success:        #16a34a;
  --success-bg:     #dcfce7;
  --warning:        #d97706;
  --warning-bg:     #fef3c7;
  --error:          #dc2626;
  --error-bg:       #fee2e2;

  --text-dark:      #111827;
  --text-mid:       #374151;
  --text-grey:      #6b7280;
  --text-light:     #9ca3af;

  --bg-page:        #f3f4f6;
  --bg-card:        #ffffff;
  --bg-stripe:      #f8faff;

  --border:         #e5e7eb;
  --border-dark:    #d1d5db;
  --radius:         8px;
  --radius-sm:      4px;
  --radius-lg:      12px;

  --shadow-sm:      0 1px 3px rgba(0,0,0,0.08);
  --shadow:         0 4px 12px rgba(0,0,0,0.08);
  --shadow-lg:      0 8px 24px rgba(0,0,0,0.12);

  --font:           "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  --font-mono:      "Consolas", "Courier New", monospace;

  --header-h:       60px;
  --sidebar-w:      320px;
  --transition:     0.18s ease;
}

/* ── Reset & Base ───────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

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

body {
  font-family: var(--font);
  font-size: 14px;
  color: var(--text-mid);
  background: var(--bg-page);
  min-height: 100vh;
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

ul, ol { list-style: none; }

img { max-width: 100%; height: auto; display: block; }

/* ── Typography ─────────────────────────────────────────────── */
h1, h2, h3, h4 { color: var(--text-dark); font-weight: 700; line-height: 1.25; }
h1 { font-size: 1.75rem; }
h2 { font-size: 1.375rem; }
h3 { font-size: 1.125rem; }
h4 { font-size: 0.9375rem; }

/* ── Utility ────────────────────────────────────────────────── */
.hidden { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0,0,0,0); }
.text-right  { text-align: right; }
.right       { text-align: right; }
.text-center { text-align: center; }
.text-blue   { color: var(--primary); }
.text-grey   { color: var(--text-grey); }
.font-bold   { font-weight: 700; }
.font-mono   { font-family: var(--font-mono); }
.mt-2 { margin-top: 0.5rem; }
.mt-4 { margin-top: 1rem; }
.mb-4 { margin-bottom: 1rem; }
.gap-2 { gap: 0.5rem; }

/* ═══════════════════════════════════════════════════════════════
   LOGIN SCREEN
   ═══════════════════════════════════════════════════════════════ */
#login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1e3a8a 0%, #1e40af 50%, #1d4ed8 100%);
  padding: 1rem;
}

.login-card {
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  padding: 2.5rem;
  width: 100%;
  max-width: 400px;
  text-align: center;
}

.login-logo {
  width: 72px;
  height: 72px;
  margin: 0 auto 1rem;
  border-radius: 50%;
  background: var(--primary-bg);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 2rem;
}

.login-brand {
  font-size: 1.75rem;
  font-weight: 800;
  color: var(--primary);
  letter-spacing: -0.3px;
}

.login-tagline {
  font-size: 0.875rem;
  color: var(--text-grey);
  margin-top: 0.25rem;
  margin-bottom: 2rem;
}

.login-divider {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1.5rem 0;
}

.login-footer {
  font-size: 0.75rem;
  color: var(--text-light);
  margin-top: 1.5rem;
}

#google-signin-btn {
  display: flex;
  justify-content: center;
  margin: 0 auto;
}

/* ═══════════════════════════════════════════════════════════════
   APP HEADER
   ═══════════════════════════════════════════════════════════════ */
#app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  height: var(--header-h);
  background: linear-gradient(90deg, #1e40af 0%, #1d4ed8 60%, #2563eb 100%);
  display: flex;
  align-items: center;
  padding: 0 1.25rem;
  gap: 1rem;
  box-shadow: 0 2px 8px rgba(30,64,175,0.3);
}

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

.header-logo {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  object-fit: contain;
  background: rgba(255,255,255,0.15);
  padding: 2px;
}

.header-logo-placeholder {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: rgba(255,255,255,0.15);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.25rem;
  flex-shrink: 0;
}

.header-title {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

.header-brand-name {
  font-size: 1.0625rem;
  font-weight: 700;
  color: #fff;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-company {
  font-size: 0.6875rem;
  color: rgba(255,255,255,0.7);
}

.header-user {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  flex-shrink: 0;
}

.header-user-info {
  text-align: right;
}

#user-name  { font-size: 0.8125rem; font-weight: 600; color: #fff; display: block; }
#user-email { font-size: 0.6875rem; color: rgba(255,255,255,0.7); display: block; }

#user-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.4);
  object-fit: cover;
  background: rgba(255,255,255,0.2);
}

#btn-logout {
  background: rgba(255,255,255,0.15);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.3);
  border-radius: var(--radius-sm);
  padding: 0.375rem 0.75rem;
  font-size: 0.75rem;
  cursor: pointer;
  transition: background var(--transition);
}
#btn-logout:hover { background: rgba(255,255,255,0.25); }

/* ═══════════════════════════════════════════════════════════════
   TAB BAR
   ═══════════════════════════════════════════════════════════════ */
#tab-bar {
  background: #fff;
  border-bottom: 2px solid var(--border);
  display: flex;
  padding: 0 1.25rem;
  gap: 0;
}

.tab-btn {
  padding: 0.75rem 1.25rem;
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--text-grey);
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  cursor: pointer;
  transition: color var(--transition), border-color var(--transition);
  display: flex;
  align-items: center;
  gap: 0.375rem;
}

.tab-btn:hover { color: var(--primary); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-btn .tab-icon { font-size: 1rem; }

/* ═══════════════════════════════════════════════════════════════
   MAIN LAYOUT (Invoice tab)
   ═══════════════════════════════════════════════════════════════ */
#app-main {
  display: flex;
  flex-direction: column;
  min-height: calc(100vh - var(--header-h) - 46px);
}

.invoice-layout {
  display: flex;
  gap: 0;
  flex: 1;
  min-height: 0;
}

/* ── Cards & Sections ───────────────────────────────────────── */
.card {
  background: var(--bg-card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-sm);
  border: 1px solid var(--border);
  overflow: hidden;
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.75rem 1rem;
  background: var(--primary-bg);
  border-bottom: 1px solid var(--primary-border);
}

.card-header h3 {
  font-size: 0.875rem;
  font-weight: 700;
  color: var(--primary);
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.card-body { padding: 1rem; }

/* ── Invoice Form Area (left/main) ──────────────────────────── */
#invoice-form-area {
  flex: 1;
  min-width: 0;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 1rem;
  /* overflow-y applied only on desktop — see @media (min-width:1025px) below */
}

/* Prevent cards from shrinking inside the bounded scroll container.
   Without this, flex children squeeze to fit the viewport height instead
   of overflowing and triggering overflow-y: auto. */
#invoice-form-area > * {
  flex-shrink: 0;
}

/* ── Totals Sidebar (right, desktop) ────────────────────────── */
#totals-sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: #fff;
  border-left: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  position: sticky;
  top: calc(var(--header-h) + 46px);
  height: calc(100vh - var(--header-h) - 46px);
  overflow-y: auto;
}

/* ═══════════════════════════════════════════════════════════════
   FORM FIELDS
   ═══════════════════════════════════════════════════════════════ */
.form-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 0.75rem;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.form-group label {
  font-size: 0.75rem;
  font-weight: 600;
  color: var(--text-grey);
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.form-group label .required {
  color: var(--error);
  margin-left: 2px;
}

.form-control {
  height: 40px;
  padding: 0 0.75rem;
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.875rem;
  color: var(--text-dark);
  background: #fff;
  transition: border-color var(--transition), box-shadow var(--transition);
  width: 100%;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,0.12);
}

.form-control:disabled { background: #f9fafb; color: var(--text-grey); cursor: not-allowed; }

select.form-control { cursor: pointer; }

textarea.form-control {
  height: auto;
  min-height: 72px;
  padding: 0.5rem 0.75rem;
  resize: vertical;
}

.field-error {
  font-size: 0.6875rem;
  color: var(--error);
  margin-top: 1px;
}

/* ── Customer search ──────────────────────────────────────────── */
.customer-search-wrapper {
  position: relative;
}

#customer-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  right: 0;
  background: #fff;
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  z-index: 200;
  max-height: 280px;
  overflow-y: auto;
  margin-top: 2px;
}

.customer-option {
  padding: 0.625rem 0.875rem;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  gap: 2px;
  border-bottom: 1px solid var(--border);
  transition: background var(--transition);
}

.customer-option:last-child { border-bottom: none; }
.customer-option:hover,
.customer-option.focused { background: var(--primary-bg); }

.customer-option.loading,
.customer-option.error { color: var(--text-grey); font-style: italic; }
.customer-option.error { color: var(--error); }

.customer-name { font-size: 0.875rem; font-weight: 600; color: var(--text-dark); }
.customer-meta { font-size: 0.75rem; color: var(--text-grey); }

.customer-option.create-new .create-label { color: var(--primary); font-weight: 600; font-size: 0.875rem; }

/* ═══════════════════════════════════════════════════════════════
   PRODUCT TABLE
   ═══════════════════════════════════════════════════════════════ */
.invoice-table-wrapper {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table.invoice-items {
  width: 100%;
  border-collapse: collapse;
  min-width: 900px;
  font-size: 0.8125rem;
}

table.invoice-items thead tr {
  background: linear-gradient(90deg, var(--primary) 0%, var(--primary-light) 100%);
}

table.invoice-items th {
  padding: 0.5rem 0.4375rem;
  font-size: 0.6875rem;
  font-weight: 700;
  color: #fff;
  text-align: left;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  border-right: 1px solid rgba(255,255,255,0.15);
  white-space: nowrap;
}
table.invoice-items th:last-child { border-right: none; }
table.invoice-items th.right { text-align: right; }
table.invoice-items th.center { text-align: center; }

table.invoice-items td {
  padding: 0.375rem 0.4375rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table.invoice-items tbody tr:nth-child(even) td { background: var(--bg-stripe); }
table.invoice-items tbody tr:hover td { background: var(--primary-bg); }

table.invoice-items td.text-right { text-align: right; }
table.invoice-items td.center { text-align: center; }

/* Table input fields */
table.invoice-items .product-name-input { min-width: 160px; }
table.invoice-items .product-hsn-input  { width: 72px; text-align: center; }
table.invoice-items .product-qty-input  { width: 62px; text-align: center; }
table.invoice-items .product-unit-input { width: 72px; text-align: center; }
table.invoice-items .product-rate-input { width: 80px; text-align: right; }
table.invoice-items .product-disc-input { width: 72px; text-align: right; }
table.invoice-items .product-gst-select { width: 64px; padding: 0.25rem; }

table.invoice-items input,
table.invoice-items select {
  height: 32px;
  padding: 0.1875rem 0.375rem;
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.8125rem;
  color: var(--text-dark);
  background: #fff;
  transition: border-color var(--transition);
}

table.invoice-items input:focus,
table.invoice-items select:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 2px rgba(37,99,235,0.1);
}

/* ── Billing mode toggle ────────────────────────────────────────────────────── */
/* simple mode: hide all pharma-specific UI (customer type, scheme badges, price hints) */
body[data-billing-mode="simple"] .pharma-only { display: none !important; }

/* Price hint below rate input */
.price-hint {
  display: block;
  font-size: 0.68rem;
  color: var(--text-muted, #6b7280);
  margin-top: 2px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 120px;
}

/* Single-value select — looks like a plain text field, no dropdown arrow */
table.invoice-items select.single-value {
  -webkit-appearance: none;
  appearance: none;
  pointer-events: none;
  cursor: default;
  background-image: none;
  padding-right: 0.375rem;
}
table.invoice-items select.single-value:focus {
  border-color: var(--border-dark);
  box-shadow: none;
}

/* Scheme hints — available offers listed below product name */
.scheme-hints {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  margin-top: 3px;
}
.scheme-hint-pill {
  font-size: 0.65rem;
  padding: 1px 5px;
  border-radius: 3px;
  background: #f3f4f6;
  color: #6b7280;
  border: 1px solid #e5e7eb;
  white-space: nowrap;
  cursor: default;
}
.scheme-hint-pill.applied {
  background: #dcfce7;
  color: #166534;
  border-color: #86efac;
  font-weight: 600;
}
.scheme-hint-pill.qualifies {
  background: #fef3c7;
  color: #92400e;
  border-color: #fde68a;
}

/* Inline multi-offer picker (radio buttons below product name) */
.scheme-picker {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-top: 4px;
  align-items: center;
}
.scheme-pick-label {
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--text-grey);
  white-space: nowrap;
}
.scheme-pick-option {
  display: flex;
  align-items: center;
  gap: 3px;
  font-size: 0.68rem;
  padding: 2px 6px;
  border-radius: 3px;
  background: #f3f4f6;
  border: 1px solid #e5e7eb;
  cursor: pointer;
  user-select: none;
}
.scheme-pick-option:hover { background: #e0f2fe; border-color: #7dd3fc; }
.scheme-pick-option input[type="radio"] { width: 11px; height: 11px; margin: 0; cursor: pointer; }

/* Per-invoice & global offers toggle */
.offers-toggle-wrapper {
  display: flex;
  align-items: center;
  gap: 0.4rem;
  font-size: 0.8rem;
  color: var(--text-mid);
}
.offers-toggle-wrapper input[type="checkbox"] { cursor: pointer; }

/* Offers table — status badges */
.status-active   { color: #166534; font-weight: 700; font-size: 0.78rem; }
.status-archived { color: #b91c1c; font-weight: 700; font-size: 0.78rem; }

/* Ghost button colour variants for offers table actions */
.btn-danger-ghost  { color: var(--error, #dc2626) !important; }
.btn-danger-ghost:hover  { background: #fef2f2; border-color: #fca5a5; }
.btn-success-ghost { color: #166534 !important; }
.btn-success-ghost:hover { background: #f0fdf4; border-color: #86efac; }

/* Offers applied section in bill summary */
#sum-offers-section {
  border-top: 1px solid var(--border);
  padding: 0.5rem 0 0.25rem;
  margin-top: 0.25rem;
}
.sum-offers-label {
  font-size: 0.7rem;
  font-weight: 700;
  color: var(--text-muted, #6b7280);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  margin-bottom: 4px;
}
.sum-offer-item {
  font-size: 0.75rem;
  color: #166534;
  padding: 1px 0;
  display: flex;
  align-items: flex-start;
  gap: 4px;
}

.taxable-value,
.cgst-value,
.sgst-value,
.igst-value { font-size: 0.8125rem; }

.row-total {
  color: var(--primary);
  font-weight: 700;
}

.btn-remove-row {
  width: 26px;
  height: 26px;
  border-radius: 50%;
  border: 1px solid var(--error);
  background: transparent;
  color: var(--error);
  font-size: 0.75rem;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition), color var(--transition);
}
.btn-remove-row:hover { background: var(--error); color: #fff; }

/* Product dropdown in row */
.product-search-wrapper { position: relative; }

.product-dropdown {
  position: absolute;
  top: 100%;
  left: 0;
  min-width: 280px;
  background: #fff;
  border: 1px solid var(--border-dark);
  border-radius: var(--radius-sm);
  box-shadow: var(--shadow-lg);
  z-index: 300;
  max-height: 220px;
  overflow-y: auto;
  margin-top: 2px;
}

.product-option {
  padding: 0.5rem 0.75rem;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 2px;
  transition: background var(--transition);
}
.product-option:last-child { border-bottom: none; }
.product-option:hover,
.product-option.focused { background: var(--primary-bg); }
.product-option.no-results { color: var(--text-grey); font-style: italic; cursor: default; }
.product-option-name { font-size: 0.8125rem; font-weight: 600; color: var(--text-dark); }
.product-option-meta { font-size: 0.6875rem; color: var(--text-grey); }

/* Add row button */
.btn-add-row {
  display: inline-flex;
  align-items: center;
  gap: 0.375rem;
  padding: 0.5rem 1rem;
  background: var(--primary-bg);
  color: var(--primary);
  border: 1px dashed var(--primary-border);
  border-radius: var(--radius-sm);
  font-size: 0.8125rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition);
  margin-top: 0.5rem;
}
.btn-add-row:hover { background: #dbeafe; }

/* ═══════════════════════════════════════════════════════════════
   TOTALS SIDEBAR
   ═══════════════════════════════════════════════════════════════ */
.totals-header {
  padding: 0.875rem 1rem;
  background: var(--primary);
  color: #fff;
  font-size: 0.8125rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}

.totals-body {
  flex: 1;
  padding: 0.25rem 0;
  overflow-y: auto;
}

.sum-section-label {
  padding: 0.375rem 1rem 0.25rem;
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--text-light);
  background: var(--bg-page);
  border-bottom: 1px solid var(--border);
  border-top: 1px solid var(--border);
  margin-top: 0.25rem;
}

.sum-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.4375rem 1rem;
  font-size: 0.8125rem;
  border-bottom: 1px solid var(--border);
}

.sum-row .s-label { color: var(--text-grey); }
.sum-row .s-value { font-variant-numeric: tabular-nums; color: var(--text-mid); }

.sum-row.discount .s-value { color: var(--error); }
.sum-row.grand-total {
  background: var(--primary);
  padding: 0.75rem 1rem;
  border-bottom: none;
}
.sum-row.grand-total .s-label { color: rgba(255,255,255,0.8); font-weight: 700; }
.sum-row.grand-total .s-value { color: #fff; font-size: 1.0625rem; font-weight: 800; }

.sum-row.round-off .s-value { color: var(--text-grey); font-size: 0.75rem; }

.amount-words-panel {
  padding: 0.75rem 1rem;
  background: var(--primary-bg);
  border-top: 1px solid var(--primary-border);
  border-bottom: 1px solid var(--primary-border);
}

.amount-words-label {
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
  color: var(--text-light);
  margin-bottom: 0.25rem;
}

#sum-amount-words {
  font-size: 0.75rem;
  font-style: italic;
  color: var(--primary-dark);
  font-weight: 600;
  line-height: 1.4;
  border-left: 3px solid var(--primary);
  padding-left: 0.5rem;
}

.totals-actions {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  border-top: 1px solid var(--border);
  background: #fff;
}

/* ═══════════════════════════════════════════════════════════════
   BUTTONS
   ═══════════════════════════════════════════════════════════════ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.375rem;
  padding: 0.5625rem 1.125rem;
  border: 1px solid transparent;
  border-radius: var(--radius-sm);
  font-family: var(--font);
  font-size: 0.875rem;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--transition), box-shadow var(--transition), transform 0.1s;
  text-decoration: none;
  user-select: none;
  white-space: nowrap;
}
.btn:active { transform: scale(0.98); }
.btn:disabled { opacity: 0.55; cursor: not-allowed; transform: none; }
.btn.loading { cursor: wait; opacity: 0.8; }

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

.btn-secondary {
  background: #fff;
  color: var(--primary);
  border-color: var(--primary-border);
}
.btn-secondary:hover:not(:disabled) { background: var(--primary-bg); }

.btn-success {
  background: var(--success);
  color: #fff;
  border-color: var(--success);
}
.btn-success:hover:not(:disabled) { background: #15803d; }

.btn-danger {
  background: var(--error);
  color: #fff;
  border-color: var(--error);
}

.btn-ghost {
  background: transparent;
  color: var(--text-grey);
  border-color: var(--border-dark);
}
.btn-ghost:hover:not(:disabled) { background: var(--bg-page); }

.btn-sm { padding: 0.3125rem 0.75rem; font-size: 0.8125rem; }
.btn-lg { padding: 0.75rem 1.5rem; font-size: 1rem; }
.btn-full { width: 100%; }

/* ═══════════════════════════════════════════════════════════════
   HISTORY TAB
   ═══════════════════════════════════════════════════════════════ */
#tab-history {
  padding: 1rem;
}

.history-filters {
  display: flex;
  flex-wrap: wrap;
  gap: 0.625rem;
  margin-bottom: 1rem;
  padding: 0.875rem;
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  position: sticky;
  top: calc(var(--header-h) + 46px);
  z-index: 10;
}

.history-table-wrapper {
  background: #fff;
  border-radius: var(--radius);
  border: 1px solid var(--border);
  overflow-x: auto;
  overflow-y: scroll;
  height: calc(100vh - 240px);
  min-height: 300px;
}

/* Always-visible scrollbar for table wrappers */
.history-table-wrapper::-webkit-scrollbar {
  width: 12px;
  height: 12px;
}
.history-table-wrapper::-webkit-scrollbar-track {
  background: #cbd5e1;
  border-left: 1px solid #94a3b8;
}
.history-table-wrapper::-webkit-scrollbar-thumb {
  background: #64748b;
  border-radius: 6px;
  border: 2px solid #cbd5e1;
  min-height: 40px;
}
.history-table-wrapper::-webkit-scrollbar-thumb:hover {
  background: #334155;
}
.history-table-wrapper::-webkit-scrollbar-corner {
  background: #cbd5e1;
}

table.history-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 700px;
}

table.history-table th {
  padding: 0.625rem 0.875rem;
  font-size: 0.75rem;
  font-weight: 700;
  text-align: left;
  color: var(--text-grey);
  text-transform: uppercase;
  letter-spacing: 0.3px;
  background: var(--bg-page);
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  position: sticky;
  top: 0;
  z-index: 2;
}

table.history-table td {
  padding: 0.625rem 0.875rem;
  font-size: 0.8125rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
  font-variant-numeric: tabular-nums;
}

table.history-table tbody tr:hover td { background: var(--primary-bg); }

table.history-table th.right,
table.history-table th.text-right { text-align: right; }
table.history-table td.right,
table.history-table td.text-right { text-align: right; }

.badge {
  display: inline-block;
  padding: 0.1875rem 0.5625rem;
  border-radius: 20px;
  font-size: 0.6875rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}
.badge-paid     { background: var(--success-bg); color: var(--success); }
.badge-partial  { background: var(--warning-bg); color: var(--warning); }
.badge-unpaid   { background: var(--error-bg);   color: var(--error); }
.badge-overpaid { background: #fef3c7;            color: #d97706; }

/* Customer type tier badge — shown next to customer name when selected */
.customer-type-badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 12px;
  font-size: 0.72rem;
  font-weight: 700;
  letter-spacing: 0.03em;
  background: #eff6ff;
  color: #1e40af;
  border: 1px solid #bfdbfe;
  white-space: nowrap;
  flex-shrink: 0;
}

.customer-type-badge--sm {
  padding: 1px 5px;
  font-size: 0.65rem;
  border-radius: 8px;
  vertical-align: middle;
  margin-left: 4px;
}

/* Customer search results modal table */
#cust-search-results table tbody tr {
  border-bottom: 1px solid var(--border);
  transition: background 0.12s;
  cursor: pointer;
}
#cust-search-results table tbody tr:hover { background: var(--primary-bg); }
#cust-search-results table tbody tr:last-child { border-bottom: none; }

/* Offer / scheme free-qty badge — shown inside invoice product row */
.offer-badge {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-weight: 700;
  background: #dcfce7;
  color: #166534;
  border: 1px solid #bbf7d0;
  margin-left: 4px;
  white-space: nowrap;
}

/* ═══════════════════════════════════════════════════════════════
   MODALS
   ═══════════════════════════════════════════════════════════════ */
.modal {
  position: fixed;
  inset: 0;
  z-index: 500;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: rgba(0,0,0,0.5);
  padding: 1rem;
  overflow-y: auto;
}

.modal-dialog,
.modal-box {
  background: #fff;
  border-radius: var(--radius-lg);
  box-shadow: var(--shadow-lg);
  width: 100%;
  max-width: 520px;
  margin: auto;
  animation: modal-in 0.2s ease;
}

.modal-dialog.modal-lg { max-width: 900px; }

@keyframes modal-in {
  from { opacity: 0; transform: scale(0.96) translateY(-12px); }
  to   { opacity: 1; transform: scale(1)    translateY(0); }
}

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

.modal-header h3 {
  font-size: 1rem;
  color: var(--primary);
}

.modal-close {
  width: 32px;
  height: 32px;
  border: none;
  background: var(--bg-page);
  border-radius: 50%;
  cursor: pointer;
  font-size: 1.125rem;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-grey);
  transition: background var(--transition);
}
.modal-close:hover { background: var(--border-dark); }

.modal-body { padding: 1.25rem; }
.modal-footer {
  padding: 1rem 1.25rem;
  border-top: 1px solid var(--border);
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
}

/* Preview modal */
#preview-content {
  max-height: calc(100vh - 160px);
  overflow-y: auto;
  padding: 1rem;
  background: #f8fafc;
}

/* ═══════════════════════════════════════════════════════════════
   LOADING OVERLAY
   ═══════════════════════════════════════════════════════════════ */
#loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 900;
  background: rgba(0,0,0,0.45);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1rem;
}

.loading-spinner {
  width: 44px;
  height: 44px;
  border: 4px solid rgba(255,255,255,0.25);
  border-top-color: #fff;
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#loading-message {
  color: #fff;
  font-size: 0.9375rem;
  font-weight: 600;
}

/* ═══════════════════════════════════════════════════════════════
   NOTIFICATIONS
   ═══════════════════════════════════════════════════════════════ */
#notification-bar {
  position: fixed;
  top: 1rem;
  right: 1rem;
  z-index: 800;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  max-width: 360px;
  pointer-events: none;
}

.notification {
  display: flex;
  align-items: flex-start;
  gap: 0.625rem;
  padding: 0.75rem 1rem;
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  font-size: 0.875rem;
  font-weight: 500;
  opacity: 0;
  transform: translateX(20px);
  transition: opacity 0.25s, transform 0.25s;
  pointer-events: auto;
}
.notification.visible { opacity: 1; transform: translateX(0); }
.notification.hiding  { opacity: 0; transform: translateX(20px); }

.notification-success { background: #fff; border-left: 4px solid var(--success); }
.notification-warning { background: #fff; border-left: 4px solid var(--warning); }
.notification-error   { background: #fff; border-left: 4px solid var(--error); }
.notification-info    { background: #fff; border-left: 4px solid var(--primary); }

.notif-icon   { font-size: 1rem; flex-shrink: 0; margin-top: 1px; }
.notif-message { flex: 1; color: var(--text-dark); }
.notif-close  { border: none; background: none; cursor: pointer; color: var(--text-grey); font-size: 1.125rem; padding: 0; line-height: 1; }

.notification-success .notif-icon { color: var(--success); }
.notification-warning .notif-icon { color: var(--warning); }
.notification-error   .notif-icon { color: var(--error); }
.notification-info    .notif-icon { color: var(--primary); }

/* ═══════════════════════════════════════════════════════════════
   MOBILE BOTTOM DRAWER (Totals)
   ═══════════════════════════════════════════════════════════════ */
#mobile-totals-trigger {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--primary);
  color: #fff;
  padding: 0.875rem 1.25rem;
  font-weight: 700;
  font-size: 0.9375rem;
  cursor: pointer;
  z-index: 400;
  justify-content: space-between;
  align-items: center;
  border-top: 2px solid var(--primary-dark);
}

#mobile-totals-drawer {
  display: none;
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: #fff;
  border-top: 2px solid var(--border);
  z-index: 450;
  max-height: 65vh;
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.3s ease;
  border-radius: var(--radius-lg) var(--radius-lg) 0 0;
  box-shadow: 0 -4px 20px rgba(0,0,0,0.15);
}
#mobile-totals-drawer.open { transform: translateY(0); }

/* ═══════════════════════════════════════════════════════════════
   GSTIN BADGE
   ═══════════════════════════════════════════════════════════════ */
.gstin-badge {
  display: inline-block;
  background: var(--primary-bg);
  border: 1px solid var(--primary-border);
  color: var(--primary);
  font-size: 0.6875rem;
  font-weight: 700;
  padding: 1px 6px;
  border-radius: 3px;
  letter-spacing: 0.5px;
  font-family: var(--font-mono);
}

/* ═══════════════════════════════════════════════════════════════
   INVOICE PREVIEW (in modal — matches PDF layout)
   ═══════════════════════════════════════════════════════════════ */
.invoice-preview-doc {
  background: #fff;
  font-family: "Segoe UI", "Helvetica Neue", Arial, sans-serif;
  font-size: 11px;
  color: #1f2937;
  max-width: 794px;
  margin: 0 auto;
  box-shadow: 0 2px 16px rgba(0,0,0,0.1);
}

.preview-top-bar {
  background: linear-gradient(135deg, #1e40af 0%, #1d4ed8 60%, #2563eb 100%);
  height: 7px;
}

.preview-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 12px 16px;
  border-bottom: 1px solid #e5e7eb;
  gap: 16px;
}

.preview-header-left { display: flex; gap: 12px; align-items: flex-start; flex: 1; }
.preview-logo { width: 56px; max-height: 56px; object-fit: contain; flex-shrink: 0; }
.preview-brand { font-size: 16px; font-weight: 800; color: #1e40af; }
.preview-company { font-size: 9px; color: #6b7280; margin-top: 1px; }
.preview-address { font-size: 9px; color: #374151; line-height: 1.5; margin-top: 3px; }
.preview-gstin-badge { display: inline-block; background: #eff6ff; border: 1px solid #bfdbfe; color: #1e40af; font-size: 8px; font-weight: 700; padding: 1px 5px; border-radius: 3px; margin-top: 3px; }

.preview-header-right { text-align: right; min-width: 180px; flex-shrink: 0; }
.preview-invoice-title { font-size: 18px; font-weight: 900; color: #1e40af; letter-spacing: 1.5px; }
.preview-original { font-size: 7.5px; color: #9ca3af; letter-spacing: 0.8px; text-transform: uppercase; margin-top: 2px; }
.preview-inv-label { font-size: 7.5px; color: #9ca3af; margin-top: 6px; text-transform: uppercase; }
.preview-inv-no { font-size: 11px; font-weight: 700; color: #111827; }
.preview-meta { font-size: 9px; color: #374151; margin-top: 3px; }

.preview-info-row {
  display: flex;
  gap: 8px;
  padding: 8px 16px;
  background: #f9fafb;
  border-bottom: 1px solid #e5e7eb;
}

.preview-bill-box, .preview-bank-box, .preview-upi-box {
  border: 1px solid #e5e7eb;
  border-radius: 5px;
  overflow: hidden;
  background: #fff;
}
.preview-bill-box { flex: 1; }
.preview-bank-box, .preview-upi-box { flex: 0 0 180px; }

.preview-box-header {
  background: #1e40af;
  color: #fff;
  font-size: 7.5px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.8px;
  padding: 3px 8px;
}

.preview-box-body { padding: 6px 8px; font-size: 9px; color: #374151; line-height: 1.6; }

.preview-items {
  width: 100%;
  border-collapse: collapse;
  margin: 0 16px;
  width: calc(100% - 32px);
  margin: 10px 16px 0;
}

.preview-items thead tr { background: linear-gradient(90deg, #1e40af 0%, #2563eb 100%); }
.preview-items th { color: #fff; padding: 5px 4px; font-size: 7.5px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.3px; }
.preview-items td { padding: 4.5px 4px; font-size: 9px; border-bottom: 1px solid #f3f4f6; vertical-align: top; }
.preview-items tbody tr:nth-child(even) td { background: #f8faff; }
.preview-items tfoot td { background: #eff6ff; font-weight: 700; color: #1e40af; border-top: 2px solid #1e40af; padding: 5px 4px; font-size: 9px; }
.preview-items .total-cell { font-weight: 700; color: #1e40af; }
.preview-items .right { text-align: right; }
.preview-items .center { text-align: center; }

.preview-bottom {
  display: flex;
  gap: 8px;
  padding: 10px 16px;
  align-items: flex-start;
}
.preview-words-box { flex: 1; }
.preview-words-label { font-size: 7.5px; font-weight: 700; text-transform: uppercase; color: #9ca3af; margin-bottom: 3px; }
.preview-words-text { font-size: 9.5px; font-weight: 600; font-style: italic; color: #1f2937; line-height: 1.4; background: #f9fafb; border: 1px solid #e5e7eb; border-left: 3px solid #1e40af; border-radius: 4px; padding: 5px 8px; }

.preview-summary { width: 220px; flex-shrink: 0; border: 1px solid #e5e7eb; border-radius: 6px; overflow: hidden; }
.preview-sum-header { background: #f3f4f6; padding: 3px 8px; font-size: 7.5px; font-weight: 700; text-transform: uppercase; color: #9ca3af; border-bottom: 1px solid #e5e7eb; }
.preview-sum-row { display: flex; justify-content: space-between; padding: 3.5px 8px; font-size: 9px; border-bottom: 1px solid #f3f4f6; color: #374151; }
.preview-grand-total { background: #1e40af; color: #fff !important; padding: 6px 8px; font-size: 11px; font-weight: 800; }

.preview-terms {
  margin: 0 16px 8px;
  border: 1px solid #e5e7eb;
  border-radius: 5px;
  overflow: hidden;
}
.preview-terms-header { background: #f3f4f6; padding: 4px 8px; font-size: 7.5px; font-weight: 700; text-transform: uppercase; color: #6b7280; border-bottom: 1px solid #e5e7eb; }
.preview-terms ol { margin: 0; padding: 5px 8px 5px 20px; color: #6b7280; font-size: 8px; line-height: 1.6; }

.preview-footer {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin: 8px 16px 0;
  padding-top: 8px;
  border-top: 1px solid #e5e7eb;
  font-size: 10px;
}
.preview-signatory { border: 1px solid #e5e7eb; border-radius: 5px; width: 150px; text-align: center; padding: 4px 8px 6px; font-size: 8px; color: #9ca3af; }
.preview-sig-line { border-bottom: 1px solid #d1d5db; height: 24px; margin-bottom: 3px; }
.preview-disclaimer { text-align: center; font-size: 7.5px; color: #d1d5db; padding: 6px 16px 10px; }

/* ═══════════════════════════════════════════════════════════════
   RESPONSIVE
   ═══════════════════════════════════════════════════════════════ */

/* Desktop only: fix #app-main to viewport so the form area scrolls
   independently while the sidebar stays sticky. On mobile/tablet
   the whole page scrolls naturally (min-height keeps the layout open). */
@media (min-width: 1025px) {
  /* Bind the main area to the viewport so child flex containers can scroll */
  #app-main {
    height: calc(100vh - var(--header-h) - 46px);
    overflow: hidden;
  }
  /* flex:1 + min-height:0 lets the row fill the remaining height reliably */
  .invoice-layout {
    flex: 1;
    min-height: 0;
    overflow: hidden;
  }
  /* Now the form area is bounded → overflow-y:auto actually scrolls */
  #invoice-form-area {
    overflow-y: auto;
    min-height: 0;
  }
  /* Inventory tab: fill the bounded #app-main and scroll vertically —
     same pattern as #tab-reports/#tab-users; overflow:hidden on #app-main
     would otherwise clip table rows below the fold */
  #tab-inventory {
    height: 100%;
    overflow-y: auto;
    min-height: 0;
    box-sizing: border-box;
    padding: 0.75rem;
  }

  /* Reports tab: fill the bounded #app-main and scroll vertically —
     without this, overflow:hidden on #app-main clips charts below the fold */
  #tab-reports {
    height: 100%;
    overflow-y: auto;
    min-height: 0;
    box-sizing: border-box;
    padding: 0.75rem;
  }

  /* Users tab: same treatment — two stacked table wrappers need the tab
     itself to scroll so the permissions matrix is never clipped by #app-main */
  #tab-users {
    height: 100%;
    overflow-y: auto;
    min-height: 0;
    box-sizing: border-box;
    padding: 0.75rem;
  }

  /* Order Book tab: fill the bounded #app-main and scroll vertically —
     without this, overflow:hidden on #app-main clips the product list */
  #tab-orderbook {
    height: 100%;
    overflow-y: auto;
    min-height: 0;
    box-sizing: border-box;
    padding: 0.75rem;
  }
  /* Override the class-level height so each table wrapper sizes to its max-height
     rather than filling the full viewport (which it would do via calc(100vh-240px)) */
  #tab-users .history-table-wrapper {
    height: auto;
  }
}

@media (max-width: 1024px) {
  #totals-sidebar {
    display: none;
  }
  #mobile-totals-trigger,
  #mobile-totals-drawer {
    display: flex;
  }
  #invoice-form-area {
    padding-bottom: 72px; /* space for mobile trigger */
  }
}

@media (max-width: 640px) {
  :root { --header-h: 52px; }
  #app-header { padding: 0 0.75rem; gap: 0.5rem; }
  .header-company { display: none; }
  #user-email    { display: none; }
  #user-name     { display: none; }  /* show only avatar + buttons on mobile */
  #user-avatar   { display: none; }
  /* WA settings accessible via tab/settings; hide on mobile to save header space */
  #btn-wa-settings { display: none; }

  /* Wrap tabs onto multiple rows on mobile */
  #tab-bar {
    flex-wrap: wrap;
    padding: 0.25rem 0.375rem;
    gap: 0.125rem;
  }
  .tab-btn {
    flex: 1 1 auto;
    min-width: 80px;
    justify-content: center;
    padding: 0.5rem 0.625rem;
    font-size: 0.75rem;
    border-bottom: 2px solid transparent;
    border-radius: 6px 6px 0 0;
  }
  .tab-btn.active {
    background: var(--primary-bg, #eff6ff);
    border-bottom-color: var(--primary);
  }

  /* History filters stack vertically on mobile — not sticky (saves screen space) */
  .history-filters {
    position: static;
    flex-direction: column;
  }
  .history-filters .form-group { width: 100%; }

  .card-body { padding: 0.75rem; }
  #invoice-form-area { padding: 0.75rem; gap: 0.75rem; }
  .form-grid { grid-template-columns: 1fr; }
  .btn-lg { padding: 0.625rem 1rem; font-size: 0.9375rem; }

  /* Modal: top-anchored scrollable (not bottom-sheet) so first field is always visible */
  .modal {
    padding: 0.5rem;
    align-items: flex-start;
    overflow-y: auto;
  }
  .modal-dialog, .modal-box {
    border-radius: var(--radius-lg);
    max-width: 100%;
    margin: 0;
    max-height: calc(100dvh - 1rem);
    display: flex;
    flex-direction: column;
  }
  .modal-body {
    overflow-y: auto;
    flex: 1;
  }

  #notification-bar { top: auto; bottom: 5rem; left: 0.5rem; right: 0.5rem; max-width: none; }
}

/* Landscape phone — compact tabs in single row, modal uses available height */
@media (max-width: 900px) and (orientation: landscape) and (max-height: 500px) {
  :root { --header-h: 44px; }
  .header-brand-name { font-size: 0.9rem; }
  .header-logo, .header-logo-placeholder { width: 28px; height: 28px; font-size: 1rem; }
  #user-name, #user-email, #user-avatar, #btn-wa-settings { display: none; }
  #tab-bar {
    flex-wrap: nowrap;
    overflow-x: auto;
    padding: 0 0.25rem;
    scrollbar-width: none;
  }
  #tab-bar::-webkit-scrollbar { display: none; }
  .tab-btn { flex-shrink: 0; padding: 0.375rem 0.75rem; font-size: 0.7rem; }
  .modal {
    padding: 0.25rem;
    align-items: flex-start;
    overflow-y: auto;
  }
  .modal-dialog, .modal-box {
    max-height: calc(100dvh - 0.5rem);
    display: flex;
    flex-direction: column;
  }
  .modal-body { overflow-y: auto; flex: 1; }
}

/* ── Dark mode (respects OS preference) ────────────────────── */
@media (prefers-color-scheme: dark) {
  :root {
    --bg-page:     #111827;
    --bg-card:     #1f2937;
    --bg-stripe:   #1a2332;
    --border:      #374151;
    --border-dark: #4b5563;
    --text-dark:   #f9fafb;
    --text-mid:    #d1d5db;
    --text-grey:   #9ca3af;
    --text-light:  #6b7280;
    --shadow-sm:   0 1px 3px rgba(0,0,0,0.3);
    --shadow:      0 4px 12px rgba(0,0,0,0.3);
    --shadow-lg:   0 8px 24px rgba(0,0,0,0.4);
  }
  body { background: var(--bg-page); }
  .form-control, table.invoice-items input, table.invoice-items select {
    background: #1f2937;
    color: var(--text-dark);
    border-color: var(--border-dark);
  }
  #customer-dropdown, .product-dropdown { background: #1f2937; border-color: var(--border-dark); }
  .customer-option:hover, .customer-option.focused,
  .product-option:hover, .product-option.focused { background: rgba(37,99,235,0.15); }
  .notification { background: #1f2937; }
  .modal-dialog, .modal-box { background: #1f2937; }
  .invoice-preview-doc { background: #fff; } /* Always white for invoice */
}
