:root {
  --blue-50: #eef4ff;
  --blue-100: #dce8ff;
  --blue-500: #2f5fe0;
  --blue-600: #244bc4;
  --blue-700: #1c3a9c;
  --navy: #0f1f3d;
  --ink: #1a2333;
  --muted: #64748b;
  --line: #e4e9f2;
  --bg: #f5f7fb;
  --card: #ffffff;
  --green: #16a34a;
  --green-bg: #e8f8ef;
  --orange: #d97706;
  --orange-bg: #fef3e2;
  --red: #dc2626;
  --red-bg: #fdecec;
  --gray-bg: #eef0f4;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15, 31, 61, 0.04), 0 8px 24px -12px rgba(15, 31, 61, 0.12);
  font-size: 15px;
}

* { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
}
a { color: inherit; text-decoration: none; }
button { font-family: inherit; cursor: pointer; }
input, select, textarea { font-family: inherit; }

.hidden { display: none !important; }

/* ---------- Login ---------- */
#loginScreen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(160deg, var(--navy) 0%, var(--blue-700) 55%, var(--blue-500) 100%);
  padding: 24px;
}
.login-card {
  width: 100%;
  max-width: 400px;
  background: var(--card);
  border-radius: 18px;
  box-shadow: 0 30px 60px -20px rgba(0,0,0,0.35);
  padding: 40px 36px;
}
.login-logo {
  display: flex; align-items: center; gap: 10px; margin-bottom: 6px;
}
.login-logo .mark {
  width: 40px; height: 40px; border-radius: 10px;
  background: linear-gradient(135deg, var(--blue-500), var(--navy));
  color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700;
}
.login-logo span { font-weight: 700; font-size: 1.15rem; }
.login-card p.sub { color: var(--muted); margin: 4px 0 28px; font-size: 0.92rem; }
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 0.82rem; font-weight: 600; margin-bottom: 6px; color: var(--ink); }
.field input {
  width: 100%; padding: 11px 13px; border: 1.5px solid var(--line); border-radius: 9px;
  font-size: 0.94rem; outline: none; transition: border-color .15s;
}
.field input:focus { border-color: var(--blue-500); }
.field-row { display: flex; align-items: center; justify-content: space-between; margin-bottom: 20px; font-size: 0.85rem; }
.field-row label { display: flex; align-items: center; gap: 6px; color: var(--muted); font-weight: 500; }
.field-row a { color: var(--blue-600); font-weight: 600; }
.btn { border: none; border-radius: 9px; padding: 11px 18px; font-weight: 600; font-size: 0.92rem; transition: all .15s; display: inline-flex; align-items: center; gap: 8px; justify-content: center; }
.btn-primary { background: var(--blue-600); color: #fff; }
.btn-primary:hover { background: var(--blue-700); }
.btn-block { width: 100%; }
.btn-ghost { background: transparent; color: var(--ink); border: 1.5px solid var(--line); }
.btn-ghost:hover { background: var(--gray-bg); }
.btn-danger { background: var(--red); color: #fff; }
.btn-danger:hover { background: #b91c1c; }
.btn-sm { padding: 7px 12px; font-size: 0.82rem; }
.login-hint { margin-top: 18px; font-size: 0.78rem; color: var(--muted); text-align: center; }

/* ---------- App shell ---------- */
#appShell { display: flex; min-height: 100vh; }

.sidebar {
  width: 232px; flex-shrink: 0; background: var(--navy); color: #cbd5f5;
  display: flex; flex-direction: column; padding: 20px 14px; position: fixed; top: 0; bottom: 0; left: 0;
  transition: transform .2s; z-index: 40;
}
.sidebar-brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 22px; }
.sidebar-brand .mark { width: 34px; height: 34px; border-radius: 9px; background: var(--blue-500); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; }
.sidebar-brand span { color: #fff; font-weight: 700; font-size: 1.02rem; }
.nav-group { flex: 1; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px; border-radius: 8px;
  font-size: 0.88rem; font-weight: 500; color: #aebbe0; cursor: pointer;
}
.nav-item i { width: 18px; text-align: center; opacity: 0.85; }
.nav-item:hover { background: rgba(255,255,255,0.06); color: #fff; }
.nav-item.active { background: var(--blue-600); color: #fff; }
.sidebar-footer { border-top: 1px solid rgba(255,255,255,0.08); padding-top: 12px; margin-top: 8px; }

.main {
  margin-left: 232px; flex: 1; display: flex; flex-direction: column; min-height: 100vh; width: calc(100% - 232px);
}
.topbar {
  height: 64px; background: var(--card); border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between; padding: 0 24px; position: sticky; top: 0; z-index: 20;
}
.topbar-left { display: flex; align-items: center; gap: 14px; }
.hamburger { display: none; background: none; border: none; font-size: 1.2rem; color: var(--ink); }
.page-title { font-size: 1.15rem; font-weight: 700; }
.page-sub { font-size: 0.8rem; color: var(--muted); margin-top: 1px; }
.topbar-right { display: flex; align-items: center; gap: 16px; }
.company-pill {
  font-size: 0.78rem; font-weight: 600; padding: 6px 12px; border-radius: 20px; background: var(--blue-50); color: var(--blue-700);
}
.profile-menu { position: relative; }
.avatar { width: 36px; height: 36px; border-radius: 50%; background: var(--blue-500); color: #fff; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; cursor: pointer; }
.profile-dropdown {
  position: absolute; right: 0; top: 46px; background: var(--card); border: 1px solid var(--line); border-radius: 10px;
  box-shadow: var(--shadow); width: 190px; padding: 6px; display: none; flex-direction: column;
}
.profile-dropdown.open { display: flex; }
.profile-dropdown a, .profile-dropdown button {
  display: flex; align-items: center; gap: 9px; padding: 9px 10px; border-radius: 7px; font-size: 0.85rem; background: none; border: none; text-align: left; width: 100%; color: var(--ink);
}
.profile-dropdown a:hover, .profile-dropdown button:hover { background: var(--gray-bg); }

.view { padding: 24px; flex: 1; }
.view-header { display: flex; align-items: flex-end; justify-content: space-between; margin-bottom: 20px; flex-wrap: wrap; gap: 12px; }

/* ---------- Cards / stats ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(6, 1fr); gap: 14px; margin-bottom: 22px; }
.stat-card {
  background: var(--card); border-radius: var(--radius); padding: 16px 16px; box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 8px;
}
.stat-card .icon { width: 34px; height: 34px; border-radius: 9px; display: flex; align-items: center; justify-content: center; font-size: 0.9rem; }
.stat-card .label { font-size: 0.74rem; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.03em; }
.stat-card .value { font-size: 1.35rem; font-weight: 800; color: var(--ink); }
.icon-blue { background: var(--blue-50); color: var(--blue-600); }
.icon-green { background: var(--green-bg); color: var(--green); }
.icon-red { background: var(--red-bg); color: var(--red); }
.icon-orange { background: var(--orange-bg); color: var(--orange); }

.grid-2 { display: grid; grid-template-columns: 1.3fr 1fr; gap: 16px; margin-bottom: 20px; }
.panel { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.panel h3 { margin: 0 0 14px; font-size: 0.96rem; }
.panel canvas { max-height: 260px; }

/* ---------- Table ---------- */
.table-wrap { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); overflow: hidden; }
table.data-table { width: 100%; border-collapse: collapse; font-size: 0.86rem; }
table.data-table th {
  text-align: left; background: var(--blue-50); color: var(--blue-700); font-weight: 700; padding: 11px 14px; font-size: 0.74rem; text-transform: uppercase; letter-spacing: 0.03em;
}
table.data-table td { padding: 11px 14px; border-top: 1px solid var(--line); vertical-align: middle; }
table.data-table tr:hover td { background: var(--blue-50); }
.badge { display: inline-flex; align-items: center; padding: 4px 10px; border-radius: 20px; font-size: 0.72rem; font-weight: 700; }
.badge-paid { background: var(--green-bg); color: var(--green); }
.badge-unpaid { background: var(--red-bg); color: var(--red); }
.badge-partial { background: var(--orange-bg); color: var(--orange); }
.badge-draft { background: var(--gray-bg); color: var(--muted); }
.badge-overdue { background: #fde2e2; color: #7f1d1d; }
.row-actions { display: flex; gap: 6px; }
.icon-btn { width: 30px; height: 30px; border-radius: 7px; border: 1px solid var(--line); background: #fff; display: flex; align-items: center; justify-content: center; font-size: 0.78rem; color: var(--muted); }
.icon-btn:hover { background: var(--blue-50); color: var(--blue-600); border-color: var(--blue-100); }

.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.search-input { position: relative; flex: 1; min-width: 220px; }
.search-input input { width: 100%; padding: 10px 12px 10px 36px; border: 1.5px solid var(--line); border-radius: 9px; font-size: 0.86rem; }
.search-input i { position: absolute; left: 12px; top: 50%; transform: translateY(-50%); color: var(--muted); font-size: 0.82rem; }
select.filter-select {
  padding: 9px 10px; border: 1.5px solid var(--line); border-radius: 9px; font-size: 0.84rem; background: #fff; color: var(--ink);
}

/* ---------- Create Invoice ---------- */
.invoice-form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; align-items: start; }
.form-section { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; margin-bottom: 16px; }
.form-section h4 { margin: 0 0 14px; font-size: 0.9rem; color: var(--blue-700); }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
.form-row.full { grid-template-columns: 1fr; }
.form-group label { display: block; font-size: 0.78rem; font-weight: 600; margin-bottom: 5px; color: var(--muted); }
.form-group input, .form-group select, .form-group textarea {
  width: 100%; padding: 9px 11px; border: 1.5px solid var(--line); border-radius: 8px; font-size: 0.86rem; outline: none;
}
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--blue-500); }
.company-select-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.company-option {
  border: 1.5px solid var(--line); border-radius: 10px; padding: 12px; cursor: pointer; display: flex; gap: 10px; align-items: center;
}
.company-option.selected { border-color: var(--blue-500); background: var(--blue-50); }
.company-option .dot { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; }
.company-option .name { font-size: 0.82rem; font-weight: 700; }
.company-option .sub { font-size: 0.72rem; color: var(--muted); }

.items-table-wrap { overflow-x: auto; }
table.item-edit-table { width: 100%; border-collapse: collapse; font-size: 0.84rem; }
table.item-edit-table th { text-align: left; padding: 6px 8px; font-size: 0.72rem; color: var(--muted); text-transform: uppercase; }
table.item-edit-table td { padding: 5px 6px; }
table.item-edit-table input { width: 100%; padding: 7px 8px; border: 1.5px solid var(--line); border-radius: 7px; font-size: 0.84rem; }
table.item-edit-table .amt { font-weight: 700; text-align: right; padding-right: 12px; }
.add-item-btn { margin-top: 10px; }
.remove-item-btn { background: none; border: none; color: var(--red); font-size: 0.9rem; }

table.term-edit-table { width: 100%; border-collapse: collapse; font-size: 0.84rem; }
table.term-edit-table th { text-align: left; padding: 6px 8px; font-size: 0.72rem; color: var(--muted); text-transform: uppercase; }
table.term-edit-table td { padding: 5px 6px; }
table.term-edit-table input { width: 100%; padding: 7px 8px; border: 1.5px solid var(--line); border-radius: 7px; font-size: 0.84rem; }

.calc-summary { display: flex; flex-direction: column; gap: 8px; margin-top: 6px; font-size: 0.86rem; }
.calc-summary .row { display: flex; justify-content: space-between; }
.calc-summary .row.grand { font-weight: 800; font-size: 1.02rem; border-top: 1px dashed var(--line); padding-top: 8px; color: var(--blue-700); }

.preview-sticky { position: sticky; top: 84px; }
.preview-toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.preview-frame { background: #dfe6f2; border-radius: var(--radius); padding: 16px; max-height: 78vh; overflow: auto; }

/* ---------- Invoice document (shared preview + print look) ---------- */
.inv-doc {
  width: 720px; max-width: 100%; margin: 0 auto; background: #fff; color: #1a2333;
  box-shadow: 0 4px 20px rgba(0,0,0,0.12); font-size: 12.5px; padding-bottom: 4px; overflow: hidden;
}
.inv-header { display: flex; align-items: center; justify-content: space-between; padding: 18px 24px; position: relative; }
.inv-brand { display: flex; align-items: center; gap: 12px; position: relative; z-index: 1; }
.inv-logo-badge { width: 46px; height: 46px; border-radius: 50%; background: var(--sc); color: #1a1a1a; display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 0.8rem; flex-shrink: 0; border: 2px solid rgba(255,255,255,0.5); }
.inv-logo-badge.red { background: #fff; color: var(--pc); border-color: var(--pc); }
.inv-logo-badge.generic { background: var(--pc); color: #fff; }
.inv-company-name { font-weight: 800; font-size: 1.02rem; line-height: 1.25; }
.inv-company-sub { font-size: 0.72rem; opacity: 0.85; font-weight: 600; }
.inv-company-ar { font-size: 0.8rem; opacity: 0.85; }
.inv-contacts { text-align: right; font-size: 0.78rem; display: flex; flex-direction: column; gap: 3px; position: relative; z-index: 1; }

/* Navy letterhead (Al Bait Al Hadeeth) */
.inv-header-navy { background: linear-gradient(120deg, var(--pc), color-mix(in srgb, var(--pc) 60%, black)); color: #fff; overflow: hidden; }
.inv-header-navy .inv-company-ar { color: var(--sc); }
.inv-header-diag { position: absolute; inset: 0; background: linear-gradient(115deg, rgba(0,0,0,0.25) 0%, rgba(0,0,0,0.25) 30%, transparent 31%); pointer-events: none; }
.inv-gold-line { height: 5px; background: linear-gradient(90deg, var(--sc), #fff, var(--sc)); }
.inv-logo-badge.navy { background: var(--sc); color: var(--pc); }
.inv-stamp-circle { width: 54px; height: 54px; border-radius: 50%; border: 2px dashed var(--pc); color: var(--pc); display: flex; align-items: center; justify-content: center; font-weight: 800; font-size: 0.7rem; margin: 0 auto 8px; opacity: 0.75; }
.inv-stamp-circle.red { border-color: var(--pc); color: var(--pc); }
.inv-contact-strip { display: flex; justify-content: center; gap: 24px; padding: 8px; font-size: 0.72rem; color: #fff; background: var(--pc); }
.inv-contact-strip i { margin-right: 4px; }

/* Red letterhead (Noor Al Taj) */
.inv-header-red-plain { background: #fff; color: #1a1a1a; padding-top: 10px; }
.inv-header-blocks { position: absolute; top: 0; left: 0; display: flex; height: 8px; width: 220px; }
.inv-header-blocks span { flex: 1; background: var(--pc); opacity: 0.25; }
.inv-header-blocks span:last-child { opacity: 1; }
.inv-header-red-plain .inv-company-ar { color: #555; }
.inv-bottom-strip { height: 10px; margin-top: 10px; }
.inv-bottom-strip.red { background: linear-gradient(90deg, var(--pc), var(--sc)); }

/* Generic letterhead (new companies) */
.inv-header-generic { background: #fff; border-bottom: 2px solid var(--pc); color: #1a2333; }
.inv-header-generic .inv-company-name { color: var(--pc); }
.inv-header-generic .inv-contacts { color: var(--pc); }

.inv-title-bar { text-align: center; font-weight: 800; font-size: 1.05rem; padding: 10px; letter-spacing: 0.06em; border-bottom: 2px solid var(--pc); }
.inv-title-bar.underline { text-decoration: underline; border-bottom: none; }
.inv-meta-table { width: 100%; border-collapse: collapse; margin-top: 10px; }
.inv-meta-table td { border: 1px solid #333; padding: 10px 12px; vertical-align: top; font-size: 0.82rem; line-height: 1.55; }
.inv-statement-bar { text-align: center; font-weight: 700; font-size: 0.86rem; padding: 8px; background: #f2f4f8; border: 1px solid #333; border-top: none; }
.inv-statement-bar.plain { background: #fff; }
.inv-items-table { width: 100%; border-collapse: collapse; margin-top: -1px; font-size: 0.82rem; }
.inv-items-table th { background: #fff; color: #1a1a1a; padding: 8px 10px; text-align: left; font-size: 0.76rem; border: 1px solid #333; font-weight: 700; }
.inv-items-table.teal th { background: #1f6f78; color: #fff; border-color: #1f6f78; }
.inv-items-table td { border: 1px solid #333; padding: 8px 10px; }
.inv-items-table td.c { text-align: center; width: 40px; }
.inv-items-table td.r, .inv-items-table th.r { text-align: right; }
.inv-items-table tfoot td { font-weight: 700; border-top: 2px solid #333; }
.inv-summary-table { width: 100%; border-collapse: collapse; margin-top: -1px; font-size: 0.84rem; }
.inv-summary-table td { border: 1px solid #333; padding: 8px 12px; }
.inv-summary-table td.r { text-align: right; width: 140px; font-weight: 600; }
.inv-summary-table tr.grand td { background: #eef1f8; font-weight: 800; font-size: 0.92rem; }
.inv-terms-table { width: 100%; border-collapse: collapse; margin-top: 10px; font-size: 0.8rem; }
.inv-terms-table td { padding: 6px 4px; vertical-align: top; }
.inv-terms-table td.tlabel { font-weight: 700; width: 160px; }
.inv-footer { display: flex; justify-content: space-between; padding: 16px 24px 6px; font-size: 0.78rem; line-height: 1.7; }
.inv-bank-title { font-weight: 700; margin-top: 6px; text-decoration: underline; }
.inv-signature { text-align: center; align-self: flex-end; min-width: 140px; }
.inv-sign-line { border-top: 1px solid #333; margin-bottom: 4px; width: 140px; margin-left: auto; margin-right: auto; }
.inv-empty { padding: 60px 20px; text-align: center; color: var(--muted); font-size: 0.9rem; }

/* ---------- Company cards / settings ---------- */
.company-card-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.company-card { background: var(--card); border-radius: var(--radius); box-shadow: var(--shadow); padding: 20px; position: relative; }
.company-card .swatch { width: 100%; height: 8px; border-radius: 6px; margin-bottom: 14px; }
.company-card .card-delete-btn { position: absolute; top: 16px; right: 16px; }
.add-company-tile {
  border: 2px dashed var(--line); border-radius: var(--radius); display: flex; flex-direction: column;
  align-items: center; justify-content: center; gap: 8px; color: var(--muted); cursor: pointer; min-height: 180px;
  font-weight: 600; font-size: 0.9rem; transition: all .15s;
}
.add-company-tile:hover { border-color: var(--blue-500); color: var(--blue-600); background: var(--blue-50); }
.add-company-tile i { font-size: 1.6rem; }
.modal.wide { max-width: 600px; }
.modal.wide .form-row { grid-template-columns: 1fr 1fr; }
.color-swatch-row { display: flex; gap: 10px; align-items: center; }
.color-swatch-row input[type="color"] { width: 46px; height: 38px; border: 1.5px solid var(--line); border-radius: 8px; padding: 2px; }
.settings-tabs { display: flex; gap: 6px; margin-bottom: 16px; }
.settings-tab { padding: 9px 16px; border-radius: 8px; font-size: 0.85rem; font-weight: 600; color: var(--muted); background: var(--card); border: 1px solid var(--line); }
.settings-tab.active { background: var(--blue-600); color: #fff; border-color: var(--blue-600); }

/* ---------- Modal / toast ---------- */
.overlay { position: fixed; inset: 0; background: rgba(15,23,42,0.5); display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.overlay.open { display: flex; }
.modal { background: #fff; border-radius: 14px; padding: 24px; width: 100%; max-width: 420px; box-shadow: 0 30px 60px rgba(0,0,0,0.25); }
.modal h3 { margin: 0 0 8px; }
.modal p { color: var(--muted); font-size: 0.9rem; margin: 0 0 20px; }
.modal-actions { display: flex; justify-content: flex-end; gap: 10px; }
.modal.payment-modal .field { margin-bottom: 14px; }

#toastContainer { position: fixed; bottom: 20px; right: 20px; z-index: 200; display: flex; flex-direction: column; gap: 10px; }
.toast {
  background: #1a2333; color: #fff; padding: 12px 16px; border-radius: 9px; font-size: 0.85rem; display: flex; align-items: center; gap: 10px;
  box-shadow: 0 10px 24px rgba(0,0,0,0.2); opacity: 0; transform: translateY(10px); transition: all .25s;
}
.toast.show { opacity: 1; transform: translateY(0); }
.toast-success i { color: #4ade80; }
.toast-error i { color: #f87171; }
.toast-info i { color: #60a5fa; }

/* ---------- Responsive ---------- */
@media (max-width: 1080px) {
  .stat-grid { grid-template-columns: repeat(3, 1fr); }
  .grid-2 { grid-template-columns: 1fr; }
  .invoice-form-grid { grid-template-columns: 1fr; }
  .preview-sticky { position: static; }
}
@media (max-width: 720px) {
  .sidebar { transform: translateX(-100%); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; width: 100%; }
  .hamburger { display: block; }
  .stat-grid { grid-template-columns: repeat(2, 1fr); }
  .form-row { grid-template-columns: 1fr; }
  .company-select-grid { grid-template-columns: 1fr; }
  .company-card-grid { grid-template-columns: 1fr; }
  table.data-table { display: block; overflow-x: auto; white-space: nowrap; }
  .view { padding: 16px; }
}
