:root {
  --bg: #f4f5f9;
  --panel: #ffffff;
  --border: #e4e6ee;
  --text: #1f2430;
  --muted: #6b7280;
  --primary: #4f46e5;
  --primary-dark: #4338ca;
  --success: #16a34a;
  --warning: #d97706;
  --danger: #dc2626;
  --radius: 10px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app { display: flex; min-height: 100vh; }

/* ---------- Login page ---------- */
.login-body {
  min-height: 100vh; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #171a2b, #2a2f4d);
}
.login-card {
  width: 360px; background: #fff; border-radius: 14px; padding: 28px 26px; box-shadow: 0 20px 60px rgba(0,0,0,.25);
}
.login-brand { display: flex; align-items: center; gap: 12px; margin-bottom: 22px; }
.login-error { background: #fdf0f0; color: var(--danger); font-size: 13px; padding: 8px 10px; border-radius: 7px; margin-top: 14px; }
.login-footer { text-align: center; font-size: 11.5px; color: var(--muted); margin-top: 18px; }

/* ---------- Topbar (user/role/logout) ---------- */
.topbar {
  display: flex; align-items: center; justify-content: flex-end; gap: 12px; flex-wrap: wrap;
  padding: 10px 28px; border-bottom: 1px solid var(--border); background: #fff;
}
.nav-toggle {
  display: none; margin-right: auto; background: none; border: 1px solid var(--border);
  border-radius: 8px; font-size: 18px; line-height: 1; padding: 7px 11px; cursor: pointer; color: var(--text);
}
.nav-backdrop { display: none; }
.role-badge { font-size: 11.5px; font-weight: 700; padding: 3px 9px; border-radius: 999px; text-transform: uppercase; letter-spacing: .03em; }
.role-badge-admin { background: #e6edff; color: #3245c9; }
.role-badge-accountant { background: #e3f7ea; color: #157a3d; }
.role-badge-viewer { background: #eef0f6; color: #4b5266; }
.topbar-user { font-size: 13.5px; color: var(--text); }

/* ---------- Force password change modal ---------- */
.modal-overlay { position: fixed; inset: 0; background: rgba(15,17,26,.55); display: flex; align-items: center; justify-content: center; z-index: 998; }
.modal-card { width: 380px; background: #fff; border-radius: 12px; padding: 22px 24px; }

/* ---------- Sidebar ---------- */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: #171a2b;
  color: #e5e7eb;
  display: flex;
  flex-direction: column;
  padding: 20px 14px;
  position: sticky;
  top: 0;
  height: 100vh;
}
.brand { display: flex; align-items: center; gap: 10px; padding: 0 8px 20px; }
.brand-mark {
  width: 36px; height: 36px; border-radius: 9px; background: var(--primary);
  display: flex; align-items: center; justify-content: center; font-weight: 700; color: #fff;
}
.brand-name { font-weight: 700; font-size: 15px; }
.brand-sub { font-size: 11px; color: #9297b0; }
.nav { display: flex; flex-direction: column; gap: 2px; margin-top: 6px; }
.nav-link {
  display: block; padding: 10px 12px; border-radius: 8px; color: #cbd0e3;
  text-decoration: none; font-size: 14px;
}
.nav-link:hover { background: #232742; color: #fff; }
.nav-link.active { background: var(--primary); color: #fff; }
.sidebar-footer { margin-top: auto; font-size: 11px; color: #6b7186; padding: 8px; }

/* ---------- Main ---------- */
.main { flex: 1; min-width: 0; }
.view-root { padding: 24px 28px 60px; max-width: 1180px; margin: 0 auto; }

.page-header {
  display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 18px; flex-wrap: wrap;
}
.page-header h1 { font-size: 22px; margin: 0; }
.page-header .sub { color: var(--muted); font-size: 13px; margin-top: 2px; }
.toolbar { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }

/* ---------- Cards / panels ---------- */
.card {
  background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 18px; margin-bottom: 18px;
}
.card h2, .card h3 { margin-top: 0; }
.grid { display: grid; gap: 16px; }
.grid-4 { grid-template-columns: repeat(4, 1fr); }
.grid-3 { grid-template-columns: repeat(3, 1fr); }
.grid-2 { grid-template-columns: repeat(2, 1fr); }
@media (max-width: 900px) {
  .grid-4 { grid-template-columns: repeat(2, 1fr); }
  .grid-3 { grid-template-columns: repeat(2, 1fr); }
}

.stat-card { background: var(--panel); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; }
.stat-label { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.stat-value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat-sub { font-size: 12px; color: var(--muted); margin-top: 4px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border);
  background: #fff; color: var(--text); padding: 8px 14px; border-radius: 8px;
  font-size: 13px; cursor: pointer; text-decoration: none;
}
.btn:hover { border-color: #c7cbe0; }
.btn-primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); }
.btn-danger { color: var(--danger); border-color: #f3c6c6; }
.btn-danger:hover { background: #fdf0f0; }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn[disabled] { opacity: .5; cursor: not-allowed; }
.icon-btn { background: none; border: none; cursor: pointer; font-size: 15px; padding: 4px 6px; border-radius: 6px; }
.icon-btn:hover { background: #f1f2f8; }

/* ---------- Forms ---------- */
label { display: block; font-size: 12.5px; color: var(--muted); margin-bottom: 4px; margin-top: 12px; }
label:first-child { margin-top: 0; }
input, select, textarea {
  width: 100%; padding: 9px 10px; border: 1px solid var(--border); border-radius: 7px;
  font-size: 14px; font-family: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); }
textarea { resize: vertical; min-height: 60px; }
.form-row { display: flex; gap: 14px; }
.form-row > * { flex: 1; min-width: 0; }
.field-hint { font-size: 11px; color: var(--muted); margin-top: 3px; }
.checkbox-row { display: flex; align-items: center; gap: 8px; margin-top: 14px; }
.checkbox-row input { width: auto; }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--border); }
thead th { color: var(--muted); font-weight: 600; font-size: 12px; text-transform: uppercase; letter-spacing: .03em; }
tbody tr:hover { background: #fafbff; }
.table-wrap { overflow-x: auto; }
.num { text-align: right; font-variant-numeric: tabular-nums; }

/* ---------- Badges ---------- */
.badge { display: inline-block; padding: 3px 9px; border-radius: 999px; font-size: 11.5px; font-weight: 600; }
.badge-draft { background: #eef0f6; color: #4b5266; }
.badge-sent, .badge-approved { background: #e6edff; color: #3245c9; }
.badge-paid, .badge-received { background: #e3f7ea; color: #157a3d; }
.badge-partially-paid { background: #fff2e0; color: #a15a00; }
.badge-overdue { background: #fde8e8; color: #b3261e; }
.badge-cancelled { background: #f1f1f1; color: #888; }

/* ---------- Line items editor ---------- */
.items-editor th, .items-editor td { padding: 6px 8px; }
.items-editor input, .items-editor select { padding: 6px 8px; font-size: 13px; }
.items-editor .item-total { font-weight: 600; white-space: nowrap; }
.totals-panel {
  margin-left: auto; width: 320px; background: #fafbff; border: 1px solid var(--border);
  border-radius: 8px; padding: 14px 16px; margin-top: 16px;
}
.totals-row { display: flex; justify-content: space-between; padding: 4px 0; font-size: 13.5px; }
.totals-row.grand { font-weight: 700; font-size: 16px; border-top: 1px solid var(--border); margin-top: 6px; padding-top: 10px; }

/* ---------- Empty state ---------- */
.empty-state { text-align: center; padding: 40px 20px; color: var(--muted); }
.empty-state .big { font-size: 34px; margin-bottom: 6px; }

/* ---------- Logo / signature previews ---------- */
.upload-preview { display: flex; align-items: center; gap: 14px; margin-top: 8px; }
.upload-preview img { max-height: 70px; max-width: 200px; border: 1px solid var(--border); border-radius: 6px; background: #fff; padding: 6px; }
.upload-box { border: 1px dashed var(--border); border-radius: 8px; padding: 14px; text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- Toasts ---------- */
.toast-root { position: fixed; top: 18px; right: 18px; display: flex; flex-direction: column; gap: 8px; z-index: 999; }
.toast {
  background: #1f2430; color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13.5px;
  opacity: 0; transform: translateY(-6px); transition: all .2s ease; box-shadow: 0 6px 20px rgba(0,0,0,.15);
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success { background: #15803d; }
.toast-error { background: #b91c1c; }

/* ---------- Import summary ---------- */
.import-summary { font-size: 13px; }
.import-summary .err { color: var(--danger); }

/* ---------- Document preview (invoice/PO/quotation/note "view" pages) ---------- */
/* JS (fitPreviewToContainer in printTemplate.js) scales .pt-page down to fit
   this wrapper's width and sets the wrapper's height to match, so on narrow
   screens the whole page is visible and readable instead of just the
   top-left corner. overflow-x:auto is the fallback if JS hasn't run yet. */
.doc-preview-wrap { overflow-x: auto; }

/* =====================================================================
   RESPONSIVE / MOBILE
   ===================================================================== */

@media (max-width: 860px) {
  .nav-toggle { display: inline-flex; align-items: center; justify-content: center; }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 100;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 8px 0 24px rgba(0, 0, 0, .3);
  }
  .sidebar.open { transform: translateX(0); }

  .nav-backdrop.open {
    display: block; position: fixed; inset: 0; background: rgba(15, 17, 26, .5); z-index: 90;
  }
}

@media (max-width: 640px) {
  .topbar { padding: 10px 14px; }
  .view-root { padding: 14px 14px 40px; }
  .page-header h1 { font-size: 19px; }

  .form-row { flex-direction: column; gap: 0; }
  .form-row > * + * { margin-top: 12px; }

  .totals-panel { width: 100%; }

  .card { padding: 14px; }

  .icon-btn { padding: 7px 9px; font-size: 16px; }

  .toast-root { left: 14px; right: 14px; top: 14px; }
  .toast { font-size: 13px; }

  /* Line-item editor: table -> stacked cards, one per line item, using
     data-label attributes (set in each form's addRow()) instead of <thead>.
     Plain horizontal scroll left Qty/Rate/GST/Disc/Amount/Remove pushed off
     the right edge with no visible hint they were there -- this puts every
     field for a line on screen at once, full width, easy to tap. */
  .items-editor thead { display: none; }
  .items-editor, .items-editor tbody { display: block; width: 100%; }
  .items-editor tr[data-item-row] {
    display: block; border: 1px solid var(--border); border-radius: 8px;
    padding: 2px 10px; margin-bottom: 10px; background: #fafbff;
  }
  .items-editor td {
    display: flex; align-items: center; justify-content: space-between; gap: 10px;
    width: 100% !important; padding: 8px 0; border-bottom: 1px dashed var(--border);
  }
  .items-editor td:last-child { border-bottom: none; justify-content: flex-end; }
  .items-editor td::before {
    content: attr(data-label); font-size: 11px; color: var(--muted); text-transform: uppercase;
    letter-spacing: .03em; flex-shrink: 0;
  }
  .items-editor td:last-child::before { content: none; }
  .items-editor td input, .items-editor td select { width: auto; max-width: 60%; text-align: right; }
  .items-editor td[data-label="Product"], .items-editor td[data-label="Description"] {
    flex-direction: column; align-items: stretch; gap: 4px;
  }
  .items-editor td[data-label="Product"] select, .items-editor td[data-label="Description"] input {
    width: 100%; max-width: 100%; text-align: left;
  }
}

@media (max-width: 560px) {
  .grid-4, .grid-3, .grid-2 { grid-template-columns: 1fr; }
  .stat-value { font-size: 22px; }

  .modal-card, .login-card { width: calc(100vw - 40px); }

  table { font-size: 12.5px; }
  th, td { padding: 8px 8px; }
}

/* =====================================================================
   PRINT / PDF TEMPLATE
   ===================================================================== */
.print-area { display: none; }

@media print {
  #app, .toast-root { display: none !important; }
  .print-area { display: block !important; }
  body { background: #fff; }
}

.pt-page {
  width: 190mm; margin: 0 auto; padding: 14mm 12mm; font-size: 12.5px; color: #111; background: #fff;
  font-family: -apple-system, Helvetica, Arial, sans-serif;
}
.pt-header { display: flex; justify-content: space-between; align-items: flex-start; border-bottom: 2px solid #222; padding-bottom: 12px; margin-bottom: 14px; }
.pt-header-left { max-width: 60%; }
.pt-logo-img { max-height: 56px; max-width: 200px; display: block; margin-bottom: 8px; }
.pt-logo-placeholder { font-size: 22px; font-weight: 800; letter-spacing: .03em; margin-bottom: 8px; }
.pt-company-name { font-weight: 700; font-size: 14px; }
.pt-company-line { color: #444; font-size: 11.5px; margin-top: 1px; }
.pt-header-right { text-align: right; }
.pt-doc-title { font-size: 20px; font-weight: 800; letter-spacing: .06em; margin-bottom: 6px; }
.pt-parties { margin-bottom: 14px; }
.pt-section-title { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; color: #666; margin-bottom: 4px; }
.pt-party-name { font-weight: 700; font-size: 13.5px; }
.pt-ship-to { margin-top: 6px; }
.pt-items { width: 100%; border-collapse: collapse; margin-bottom: 10px; }
.pt-items th, .pt-items td { border: 1px solid #ccc; padding: 7px 8px; font-size: 12px; }
.pt-items thead th { background: #f2f2f2; }
.pt-num { text-align: right; white-space: nowrap; }
.pt-hsn { color: #777; font-size: 10.5px; }
.pt-totals { margin-left: auto; width: 260px; margin-bottom: 16px; }
.pt-total-row { display: flex; justify-content: space-between; padding: 3px 0; font-size: 12.5px; }
.pt-grand-total { font-weight: 800; font-size: 14.5px; border-top: 1px solid #222; margin-top: 4px; padding-top: 6px; }
.pt-terms { margin-bottom: 16px; font-size: 11.5px; }
.pt-footer { display: flex; justify-content: space-between; align-items: flex-end; margin-top: 30px; border-top: 1px solid #ccc; padding-top: 14px; }
.pt-bank { font-size: 11px; color: #333; max-width: 60%; }
.pt-bank div { margin-bottom: 2px; }
.pt-qr { margin-top: 8px; }
.pt-qr-box { border: 1px dashed #999; width: 70px; height: 70px; display: flex; align-items: center; justify-content: center; text-align: center; font-size: 9.5px; color: #555; }
.pt-qr-caption { font-size: 9.5px; color: #777; margin-top: 3px; }
.pt-signature { text-align: center; }
.pt-sign-img { max-height: 50px; max-width: 160px; display: block; margin: 0 auto 4px; }
.pt-sign-blank { height: 40px; }
.pt-sign-caption { font-size: 11px; border-top: 1px solid #333; padding-top: 4px; min-width: 160px; }

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