:root {
  --navy: #29256d;
  --navy-deep: #1f1a58;
  --blue: #2aa9d7;
  --teal: #32b4a2;
  --green: #35b95a;
  --bg: #f5f7fb;
  --card: #ffffff;
  --text: #172033;
  --muted: #687386;
  --line: #e5e9f0;
  --danger: #c73f4b;
  --warning: #aa6a00;
  --success: #157d43;
  --shadow: 0 12px 32px rgba(35, 42, 74, .08);
  --radius: 16px;
}

* { box-sizing: border-box; }
html { min-height: 100%; background: var(--bg); }
body { margin: 0; min-height: 100vh; font-family: Inter, ui-sans-serif, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; color: var(--text); background: var(--bg); }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }

.app-shell { display: grid; grid-template-columns: 248px 1fr; min-height: 100vh; }
.sidebar { position: sticky; top: 0; height: 100vh; padding: 24px 18px; background: #fff; border-right: 1px solid var(--line); display: flex; flex-direction: column; z-index: 4; }
.brand-block { padding: 4px 8px 24px; border-bottom: 1px solid var(--line); }
.brand-logo { display: block; width: 100%; height: 58px; object-fit: contain; object-position: left center; }
.brand-block p { margin: 8px 0 0; color: var(--muted); font-size: 12px; font-weight: 700; letter-spacing: .04em; }
.nav-list { display: grid; gap: 8px; margin-top: 22px; }
.nav-btn { border: 0; border-radius: 12px; background: transparent; padding: 12px 13px; text-align: left; color: #505a6d; font-weight: 700; display: flex; gap: 10px; align-items: center; }
.nav-btn:hover { background: #f3f5fa; }
.nav-btn.active { background: rgba(42, 169, 215, .11); color: var(--navy); }
.nav-btn span { width: 22px; text-align: center; font-size: 18px; }
.sidebar-footer { margin-top: auto; padding: 16px 8px 2px; color: var(--muted); line-height: 1.45; }

.main-content { min-width: 0; padding: 0 32px 48px; }
.topbar { height: 104px; display: flex; align-items: center; justify-content: space-between; gap: 24px; }
.topbar h1 { margin: 2px 0 0; font-size: 27px; letter-spacing: -.025em; }
.eyebrow { text-transform: uppercase; letter-spacing: .14em; font-weight: 800; font-size: 10px; color: var(--blue); margin: 0; }
.top-actions { display: flex; gap: 10px; }

.view { display: none; }
.view.active { display: block; }
.panel { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.panel-head { padding: 22px 24px; display: flex; align-items: center; justify-content: space-between; gap: 20px; border-bottom: 1px solid var(--line); }
.panel-head h2, .backup-card h2, .data-note h2 { margin: 0 0 5px; font-size: 18px; }
.panel-head p, .backup-card p, .data-note p { margin: 0; color: var(--muted); font-size: 13px; line-height: 1.55; }

.btn { border: 1px solid transparent; padding: 10px 15px; border-radius: 10px; font-weight: 800; transition: .15s ease; }
.btn:hover { transform: translateY(-1px); }
.btn.primary { color: #fff; background: var(--navy); box-shadow: 0 7px 18px rgba(41, 37, 109, .16); }
.btn.primary:hover { background: var(--navy-deep); }
.btn.secondary { background: #fff; border-color: #dce2ec; color: #364056; }
.btn.ghost { background: #f5f7fb; color: #4a5568; }
.btn.small { padding: 7px 10px; font-size: 12px; }
.btn.danger { background: var(--danger); color: white; }
.btn.danger-outline { border-color: #f0c6cb; background: #fff; color: var(--danger); }
.text-btn { background: none; border: 0; color: var(--navy); font-weight: 800; }
.icon-btn { width: 36px; height: 36px; border-radius: 10px; border: 1px solid var(--line); background: #fff; font-size: 24px; color: #626d80; }

.stats-grid { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 16px; margin-bottom: 18px; }
.stat-card { background: #fff; border: 1px solid var(--line); border-radius: var(--radius); padding: 20px; box-shadow: var(--shadow); }
.stat-label { display: block; color: var(--muted); font-size: 12px; font-weight: 800; text-transform: uppercase; letter-spacing: .05em; }
.stat-card strong { display: block; margin: 9px 0 4px; font-size: 25px; color: var(--navy); }
.stat-card small { color: #8a94a5; }
.dashboard-grid { display: grid; grid-template-columns: minmax(0, 2fr) minmax(280px, .8fr); gap: 18px; align-items: start; }
.quick-panel { padding: 22px; }
.quick-panel h2 { margin: 0 0 16px; font-size: 18px; }
.quick-card { width: 100%; display: flex; gap: 13px; text-align: left; align-items: center; padding: 14px; border: 1px solid var(--line); border-radius: 13px; background: #fff; margin-top: 10px; }
.quick-card:hover { border-color: #bcd7e5; background: #fbfdff; }
.quick-icon { width: 38px; height: 38px; flex: 0 0 38px; border-radius: 11px; display: grid; place-items: center; background: rgba(42, 169, 215, .1); color: var(--navy); font-weight: 900; }
.quick-card strong, .quick-card small { display: block; }
.quick-card small { color: var(--muted); margin-top: 3px; line-height: 1.35; }

.table-wrap { overflow: auto; }
table { border-collapse: collapse; width: 100%; }
th { text-align: left; color: #768196; font-size: 11px; text-transform: uppercase; letter-spacing: .04em; background: #fbfcfe; }
th, td { padding: 12px 16px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { font-size: 13px; }
tbody tr:last-child td { border-bottom: 0; }
.empty-state { text-align: center; padding: 44px 20px; color: var(--muted); }
.empty-state h3 { color: var(--text); margin: 8px 0 5px; }
.empty-state p { margin: 0 0 16px; }
.empty-icon { width: 48px; height: 48px; border-radius: 50%; margin: 0 auto; display: grid; place-items: center; background: #f1f4f9; color: var(--navy); font-size: 22px; }

.status-badge { display: inline-flex; align-items: center; gap: 6px; border-radius: 999px; padding: 5px 9px; font-size: 11px; font-weight: 800; }
.status-badge.paid { background: #e8f7ee; color: var(--success); }
.status-badge.partial { background: #fff4df; color: var(--warning); }
.status-badge.unpaid { background: #fdecee; color: #a53742; }
.status-dot { width: 6px; height: 6px; border-radius: 50%; background: currentColor; }

.invoice-workspace { display: grid; grid-template-columns: minmax(570px, 1.1fr) minmax(460px, .9fr); gap: 18px; align-items: start; }
.form-panel { overflow: hidden; }
.invoice-editor label, .modal-card label { display: grid; gap: 6px; color: #525d70; font-size: 12px; font-weight: 800; }
input, textarea, select { width: 100%; border: 1px solid #d9e0e9; border-radius: 9px; background: #fff; color: var(--text); padding: 10px 11px; outline: none; }
textarea { resize: vertical; }
input:focus, textarea:focus, select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(42, 169, 215, .1); }
.form-section-title { margin: 22px 24px 10px; padding-top: 2px; color: var(--navy); font-weight: 900; font-size: 13px; }
.split-title { display: flex; justify-content: space-between; align-items: center; }
.form-grid { display: grid; gap: 12px; padding: 0 24px 12px; }
.form-grid.cols-4 { grid-template-columns: repeat(4, 1fr); }
.form-grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.form-grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.invoice-editor > .panel > label { margin: 0 24px 12px; }
.line-items-scroll { overflow-x: auto; padding: 0 24px; }
.line-items-table { min-width: 710px; border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.line-items-table th, .line-items-table td { padding: 7px; }
.line-items-table th { font-size: 10px; }
.line-items-table input { padding: 8px; }
.line-items-table .sno { width: 54px; text-align: center; }
.line-items-table .remove-col { width: 42px; }
.line-items-table td:first-child { text-align: center; color: #8993a4; font-weight: 800; }
.remove-line { width: 30px; height: 30px; border: 0; background: #fff3f4; color: var(--danger); border-radius: 8px; font-weight: 900; }
.field-hint { color: #8791a2; font-size: 11px; margin: 8px 24px 0; }
.totals-editor { display: grid; grid-template-columns: 1fr 340px; gap: 24px; padding: 22px 24px 8px; align-items: start; }
.notes-block label { margin: 0 !important; }
.totals-box { border: 1px solid var(--line); border-radius: 12px; padding: 8px 14px; background: #fbfcfe; }
.totals-box > div, .paid-input-row { min-height: 42px; display: flex !important; align-items: center; justify-content: space-between; gap: 15px; border-bottom: 1px solid var(--line); margin: 0 !important; }
.totals-box strong { color: var(--navy); }
.paid-input-row input { width: 130px; text-align: right; padding: 7px 8px; }
.totals-box .grand-total { border-bottom: 0; font-size: 14px; }
.totals-box .grand-total strong { font-size: 18px; }
.form-actions { display: flex; justify-content: flex-end; gap: 10px; padding: 18px 24px 24px; }
.preview-panel { position: sticky; top: 18px; overflow: hidden; }

.invoice-sheet { width: 100%; background: #fff; color: #172033; padding: 30px 34px 28px; font-family: Arial, Helvetica, sans-serif; font-size: 11px; min-height: 720px; }
.invoice-brand-row { display: flex; justify-content: space-between; gap: 30px; align-items: flex-start; padding-bottom: 20px; border-bottom: 2px solid var(--navy); }
.invoice-logo { width: 265px; max-height: 74px; object-fit: contain; object-position: left top; }
.invoice-title-block { text-align: right; }
.invoice-title-block h2 { font-size: 25px; letter-spacing: .13em; color: var(--navy); margin: 0 0 8px; }
.invoice-title-block strong { font-size: 13px; }
.company-meta { color: #5f6a7a; line-height: 1.55; margin-top: 9px; }
.invoice-meta-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 26px; padding: 22px 0; }
.invoice-card-label { color: #7a8495; font-size: 9px; font-weight: 800; text-transform: uppercase; letter-spacing: .07em; margin-bottom: 5px; }
.invoice-card strong { color: var(--navy); font-size: 13px; }
.invoice-card p { margin: 4px 0 0; line-height: 1.5; }
.meta-lines { display: grid; gap: 6px; }
.meta-line { display: grid; grid-template-columns: 115px 1fr; gap: 10px; }
.meta-line span:first-child { color: #7a8495; }
.print-items { margin-top: 4px; width: 100%; border: 1px solid #dfe4ec; }
.print-items th { background: var(--navy); color: white; padding: 9px 8px; }
.print-items td { padding: 9px 8px; }
.print-items th.num, .print-items td.num { text-align: right; }
.print-items th.center, .print-items td.center { text-align: center; }
.print-summary { margin: 18px 0 0 auto; width: 315px; }
.print-summary .sum-row { display: flex; justify-content: space-between; gap: 20px; padding: 7px 2px; border-bottom: 1px solid #e5e8ee; }
.print-summary .sum-row strong { color: var(--navy); }
.print-summary .sum-row.balance { font-size: 14px; padding: 10px 2px; border-bottom: 2px solid var(--navy); }
.print-notes { margin-top: 24px; padding: 12px 14px; background: #f7f9fc; border-left: 3px solid var(--blue); line-height: 1.55; white-space: pre-wrap; }
.print-footer { margin-top: 32px; border-top: 1px solid #dfe4ec; padding-top: 12px; color: #6a7484; display: flex; justify-content: space-between; gap: 30px; font-size: 9.5px; line-height: 1.5; }
.print-footer > div { width: 50%; }

.records-head { flex-wrap: wrap; }
.filters-row { padding: 16px 20px; display: grid; grid-template-columns: minmax(240px, 1fr) 160px 145px 145px auto; gap: 9px; border-bottom: 1px solid var(--line); }
.search-box { position: relative; }
.search-box span { position: absolute; left: 11px; top: 8px; color: #8892a3; font-size: 20px; }
.search-box input { padding-left: 36px; }
.records-table-wrap { min-height: 250px; }
.action-menu { display: flex; gap: 6px; justify-content: flex-end; }
.table-btn { border: 1px solid var(--line); background: #fff; color: #3e4960; border-radius: 8px; padding: 6px 8px; font-size: 11px; font-weight: 800; }
.table-btn.pay { color: var(--success); border-color: #cfe9da; }

.backup-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 18px; }
.backup-card { padding: 26px; }
.backup-card p { max-width: 540px; min-height: 42px; margin-bottom: 18px; }
.backup-icon { width: 48px; height: 48px; border-radius: 13px; background: #eef5fb; color: var(--navy); display: grid; place-items: center; font-weight: 900; margin-bottom: 16px; }
.danger-zone .backup-icon { background: #fff0f1; color: var(--danger); }
.data-note { margin-top: 18px; padding: 24px; }

.modal-backdrop { display: none; position: fixed; inset: 0; z-index: 20; padding: 24px; background: rgba(18, 23, 38, .52); align-items: center; justify-content: center; }
.modal-backdrop.open { display: flex; }
.modal-card { width: min(620px, 100%); max-height: 92vh; overflow: auto; background: #fff; border-radius: 18px; box-shadow: 0 30px 80px rgba(0,0,0,.22); padding: 22px; }
.wide-modal { width: min(820px, 100%); }
.modal-head { display: flex; justify-content: space-between; align-items: flex-start; gap: 15px; margin-bottom: 18px; }
.modal-head h2 { margin: 3px 0 0; }
.modal-card .form-grid { padding: 0 0 12px; }
.modal-card > form > label { margin-bottom: 12px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 9px; padding-top: 8px; }
.multi-actions { border-top: 1px solid var(--line); margin-top: 18px; padding-top: 16px; }
.spacer { flex: 1; }
.payment-context { padding: 13px 14px; border-radius: 11px; background: #f5f7fb; margin-bottom: 16px; display: flex; justify-content: space-between; gap: 15px; font-size: 12px; }
.payment-context strong { color: var(--navy); }
.detail-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-bottom: 18px; }
.detail-kpi { padding: 14px; border: 1px solid var(--line); border-radius: 11px; }
.detail-kpi span { display: block; font-size: 10px; color: var(--muted); text-transform: uppercase; font-weight: 800; }
.detail-kpi strong { display: block; margin-top: 5px; color: var(--navy); }
.payment-history { border: 1px solid var(--line); border-radius: 12px; overflow: hidden; }
.payment-history h3 { margin: 0; padding: 13px 15px; background: #fbfcfe; border-bottom: 1px solid var(--line); font-size: 13px; }
.payment-history table td, .payment-history table th { padding: 9px 12px; }

.toast-container { position: fixed; right: 20px; bottom: 20px; z-index: 50; display: grid; gap: 10px; }
.toast { background: #1f2735; color: #fff; border-radius: 11px; padding: 12px 15px; box-shadow: 0 12px 35px rgba(0,0,0,.18); min-width: 250px; max-width: 380px; font-size: 13px; animation: toastIn .18s ease; }
.toast.success { border-left: 4px solid #48c278; }
.toast.error { border-left: 4px solid #ea6873; }
@keyframes toastIn { from { transform: translateY(8px); opacity: 0 } to { transform: translateY(0); opacity: 1 } }

@media (max-width: 1250px) {
  .invoice-workspace { grid-template-columns: 1fr; }
  .preview-panel { position: static; }
  .form-grid.cols-4 { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 960px) {
  .app-shell { grid-template-columns: 82px 1fr; }
  .sidebar { padding: 18px 10px; }
  .brand-logo { height: 45px; object-fit: cover; object-position: left; width: 52px; }
  .brand-block p, .nav-btn:not(.active) { font-size: 0; }
  .nav-btn { justify-content: center; }
  .nav-btn.active { font-size: 0; }
  .nav-btn span { font-size: 20px; }
  .sidebar-footer { display: none; }
  .main-content { padding: 0 18px 35px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .dashboard-grid { grid-template-columns: 1fr; }
  .filters-row { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 650px) {
  .app-shell { display: block; }
  .sidebar { position: fixed; height: auto; inset: auto 0 0 0; z-index: 10; padding: 8px; border-right: 0; border-top: 1px solid var(--line); }
  .brand-block, .sidebar-footer { display: none; }
  .nav-list { margin: 0; grid-template-columns: repeat(4, 1fr); gap: 4px; }
  .nav-btn { padding: 9px 4px; }
  .main-content { padding: 0 12px 90px; }
  .topbar { height: 88px; }
  .top-actions .secondary { display: none; }
  .stats-grid { grid-template-columns: 1fr; }
  .form-grid.cols-4, .form-grid.cols-3, .form-grid.cols-2, .totals-editor, .backup-grid, .detail-grid { grid-template-columns: 1fr; }
  .filters-row { grid-template-columns: 1fr; }
  .invoice-sheet { padding: 20px; }
  .invoice-brand-row, .invoice-meta-grid, .print-footer { display: block; }
  .invoice-title-block { text-align: left; margin-top: 18px; }
  .print-footer > div { width: auto; margin-bottom: 8px; }
}

@media print {
  @page { size: A4 portrait; margin: 8mm; }
  html, body { background: #fff !important; }
  body * { visibility: hidden !important; }
  #invoicePrintArea, #invoicePrintArea * { visibility: visible !important; }
  #invoicePrintArea { position: absolute; left: 0; top: 0; width: 100%; min-height: 0; padding: 0; }
  .invoice-sheet { box-shadow: none; border: 0; font-size: 10.4px; }
  .print-items th, .print-items td { padding: 7px 6px; }
}

/* Server/auth additions */
.btn:disabled, button:disabled { opacity: .6; cursor: wait; transform: none !important; }
.user-chip { border: 1px solid #dce2ec; background: #fff; color: #364056; border-radius: 10px; padding: 9px 12px; font-weight: 800; display: inline-flex; align-items: center; gap: 8px; }
.user-chip:hover { background: #f8fafc; }
.user-dot { width: 8px; height: 8px; border-radius: 50%; background: var(--green); box-shadow: 0 0 0 3px rgba(53,185,90,.12); }
.logout-btn { text-decoration: none; display: inline-flex; align-items: center; }
.account-user { color: var(--muted); margin: 0 0 18px; font-size: 13px; }

@media (max-width: 980px) {
  .top-actions { flex-wrap: wrap; justify-content: flex-end; }
  .user-chip { max-width: 180px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
}
