:root {
  --bg: #f1f5f9;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --border: #e2e8f0;
  --text: #0f172a;
  --muted: #64748b;
  --primary: #1d4ed8;
  --primary-2: #1e3a8a;
  --primary-soft: #dbeafe;
  --green: #15803d; --green-soft: #dcfce7;
  --red: #b91c1c; --red-soft: #fee2e2;
  --orange: #b45309; --orange-soft: #fef3c7;
  --blue: #1d4ed8; --blue-soft: #dbeafe;
  --grey: #475569; --grey-soft: #e2e8f0;
  --yellow: #a16207; --yellow-soft: #fef9c3;
  --wa: #16a34a;
  --radius: 12px;
  --shadow: 0 1px 2px rgba(15,23,42,.06), 0 4px 12px rgba(15,23,42,.04);
  --sidebar-w: 250px;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  font-size: 14px; line-height: 1.5;
  background: var(--bg); color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
.ic { flex-shrink: 0; vertical-align: middle; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }
.right { text-align: right; }

/* ---------- Layout ---------- */
.layout { display: flex; min-height: 100vh; }
.sidebar {
  position: fixed; inset: 0 auto 0 0; width: var(--sidebar-w);
  background: linear-gradient(180deg, var(--primary-2), #172554);
  color: #cbd5e1; display: flex; flex-direction: column; z-index: 40;
  transition: transform .25s ease;
}
.brand { display: flex; gap: 12px; align-items: center; padding: 20px 18px; border-bottom: 1px solid rgba(255,255,255,.08); }
.brand-logo { width: 40px; height: 40px; border-radius: 10px; background: rgba(255,255,255,.12); display: grid; place-items: center; color: #fff; }
.brand strong { color: #fff; display: block; font-size: 15px; }
.brand small { color: #93c5fd; font-size: 12px; }
.sidebar nav { padding: 12px 10px; flex: 1; overflow-y: auto; }
.sidebar nav a, .sidebar-foot a {
  display: flex; align-items: center; gap: 12px; padding: 10px 12px; margin-bottom: 2px;
  color: #cbd5e1; border-radius: 8px; font-weight: 500;
}
.sidebar nav a:hover, .sidebar-foot a:hover { background: rgba(255,255,255,.08); color: #fff; }
.sidebar nav a.active { background: #fff; color: var(--primary-2); }
.sidebar nav a span { flex: 1; }
.sidebar .count { font-style: normal; background: #ef4444; color: #fff; font-size: 11px; font-weight: 700; padding: 1px 8px; border-radius: 99px; }
.sidebar-foot { padding: 10px; border-top: 1px solid rgba(255,255,255,.08); }

.main { flex: 1; margin-left: var(--sidebar-w); min-width: 0; display: flex; flex-direction: column; }
.topbar {
  position: sticky; top: 0; z-index: 20; background: rgba(255,255,255,.92); backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 12px; padding: 12px 24px; min-height: 60px;
}
.topbar h1 { font-size: 18px; font-weight: 700; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.topbar-right { display: flex; align-items: center; gap: 14px; }
.today { color: var(--muted); font-size: 13px; }
.bell { position: relative; color: var(--muted); display: grid; place-items: center; width: 38px; height: 38px; border-radius: 10px; }
.bell:hover { background: var(--surface-2); }
.bell.has { color: var(--red); animation: ring 2.5s ease-in-out infinite; }
.bell em { position: absolute; top: 2px; right: 0; background: #ef4444; color: #fff; font-style: normal; font-size: 10px; font-weight: 700; min-width: 18px; height: 18px; border-radius: 99px; display: grid; place-items: center; padding: 0 4px; }
@keyframes ring { 0%, 90%, 100% { transform: rotate(0); } 92% { transform: rotate(12deg); } 95% { transform: rotate(-12deg); } 98% { transform: rotate(6deg); } }
.content { padding: 24px; flex: 1; }
.foot { padding: 16px 24px; color: var(--muted); font-size: 12px; text-align: center; }
.only-mobile { display: none; }
.backdrop { display: none; }

/* ---------- Components ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 16px 18px; border-bottom: 1px solid var(--border); flex-wrap: wrap; }
.card-head h2 { font-size: 15px; font-weight: 600; display: flex; align-items: center; gap: 8px; }
.card-body { padding: 18px; }
.card + .card, .grid + .card, .card + .grid, .grid + .grid { margin-top: 20px; }

.grid { display: grid; gap: 20px; }
.grid.cols-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid.cols-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid.cols-side { grid-template-columns: minmax(0, 2fr) minmax(260px, 1fr); }
.stats { display: grid; gap: 16px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); margin-bottom: 20px; }
.stat { padding: 18px; display: flex; gap: 14px; align-items: center; }
.stat .si { width: 46px; height: 46px; border-radius: 12px; display: grid; place-items: center; flex-shrink: 0; }
.stat .sv { font-size: 22px; font-weight: 700; font-variant-numeric: tabular-nums; }
.stat .sl { color: var(--muted); font-size: 13px; }
.stat .ss { font-size: 12px; color: var(--muted); }
a.stat { color: inherit; transition: transform .15s, box-shadow .15s; }
a.stat:hover { transform: translateY(-2px); box-shadow: 0 8px 20px rgba(15,23,42,.08); }
.si.blue { background: var(--blue-soft); color: var(--blue); }
.si.green { background: var(--green-soft); color: var(--green); }
.si.red { background: var(--red-soft); color: var(--red); }
.si.orange { background: var(--orange-soft); color: var(--orange); }
.si.yellow { background: var(--yellow-soft); color: var(--yellow); }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 9px 16px; border-radius: 9px; border: 1px solid transparent; cursor: pointer;
  font: inherit; font-weight: 600; font-size: 13px; background: var(--primary); color: #fff; white-space: nowrap;
  transition: filter .15s, background .15s;
}
.btn:hover { filter: brightness(1.08); }
.btn.light { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn.light:hover { background: var(--surface-2); filter: none; }
.btn.green { background: var(--green); }
.btn.red { background: var(--red); }
.btn.wa { background: var(--wa); }
.btn.sm { padding: 6px 10px; font-size: 12px; border-radius: 7px; }
.btn.block { width: 100%; }
.icon-btn { background: none; border: 0; padding: 6px; border-radius: 8px; cursor: pointer; color: inherit; display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--surface-2); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; justify-content: flex-end; }
.act {
  width: 32px; height: 32px; border-radius: 8px; display: inline-grid; place-items: center;
  border: 1px solid var(--border); background: var(--surface); color: var(--muted); cursor: pointer; padding: 0;
}
.act:hover { color: var(--primary); border-color: var(--primary); }
.act.wa { color: var(--wa); }
.act.wa:hover { border-color: var(--wa); }
.act.danger:hover { color: var(--red); border-color: var(--red); }
.act.pay { color: var(--green); }
.act.pay:hover { border-color: var(--green); }

.badge { display: inline-block; padding: 3px 10px; border-radius: 99px; font-size: 12px; font-weight: 600; white-space: nowrap; }
.badge.green { background: var(--green-soft); color: var(--green); }
.badge.red { background: var(--red-soft); color: var(--red); }
.badge.orange { background: var(--orange-soft); color: var(--orange); }
.badge.blue { background: var(--blue-soft); color: var(--blue); }
.badge.grey { background: var(--grey-soft); color: var(--grey); }
.badge.yellow { background: var(--yellow-soft); color: var(--yellow); border: 1px solid #fde047; }
.udhar { color: var(--yellow); background: var(--yellow-soft); padding: 1px 8px; border-radius: 6px; font-weight: 700; white-space: nowrap; }
.tag { display: inline-flex; align-items: center; gap: 6px; font-weight: 600; font-size: 12px; color: var(--text); white-space: nowrap; }
.tag::before { content: ""; width: 10px; height: 10px; border-radius: 3px; background: var(--tag); }

.flash { padding: 12px 16px; border-radius: 10px; margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center; gap: 12px; font-weight: 500; }
.flash.success { background: var(--green-soft); color: var(--green); }
.flash.error { background: var(--red-soft); color: var(--red); }
.flash.info { background: var(--blue-soft); color: var(--blue); }
.flash-x { background: none; border: 0; font-size: 20px; cursor: pointer; color: inherit; line-height: 1; }

.alert-banner { display: flex; align-items: center; gap: 14px; padding: 14px 18px; border-radius: var(--radius); margin-bottom: 20px; background: var(--red-soft); color: var(--red); border: 1px solid #fecaca; flex-wrap: wrap; }
.alert-banner strong { font-size: 15px; }
.alert-banner .grow { flex: 1; min-width: 200px; }

/* ---------- Forms ---------- */
.form-grid { display: grid; gap: 16px; grid-template-columns: repeat(2, minmax(0, 1fr)); }
.form-grid .full { grid-column: 1 / -1; }
.field label { display: block; font-weight: 600; font-size: 13px; margin-bottom: 6px; }
.field label .req { color: var(--red); }
.field .hint { font-size: 12px; color: var(--muted); margin-top: 4px; }
input[type=text], input[type=tel], input[type=number], input[type=date], input[type=password], input[type=search], select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid var(--border); border-radius: 9px;
  font: inherit; background: var(--surface); color: var(--text); transition: border-color .15s, box-shadow .15s;
}
input[type=color] { width: 56px; height: 40px; border: 1px solid var(--border); border-radius: 9px; padding: 3px; background: var(--surface); cursor: pointer; }
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-soft); }
textarea { min-height: 80px; resize: vertical; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 20px; flex-wrap: wrap; }

.filters { display: flex; gap: 10px; flex-wrap: wrap; align-items: center; }
.filters .search { position: relative; flex: 1 1 240px; }
.filters .search .ic { position: absolute; left: 11px; top: 50%; transform: translateY(-50%); color: var(--muted); }
.filters .search input { padding-left: 36px; }
.filters select, .filters input[type=date] { width: auto; flex: 0 1 180px; }

.chips { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { padding: 6px 12px; border-radius: 99px; border: 1px solid var(--border); background: var(--surface); color: var(--muted); font-weight: 600; font-size: 12px; }
.chip:hover { color: var(--text); }
.chip.on { background: var(--primary); color: #fff; border-color: var(--primary); }
.chip b { margin-left: 4px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
table.tbl { width: 100%; border-collapse: collapse; }
.tbl th { text-align: left; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; padding: 11px 14px; background: var(--surface-2); border-bottom: 1px solid var(--border); white-space: nowrap; }
.tbl td { padding: 12px 14px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.tbl tbody tr:hover { background: var(--surface-2); }
.tbl tbody tr:last-child td { border-bottom: 0; }
.tbl tfoot td { font-weight: 700; background: var(--surface-2); border-top: 2px solid var(--border); }
.tbl .name { font-weight: 600; color: var(--text); }
.tbl .sub { font-size: 12px; color: var(--muted); }
.empty { text-align: center; padding: 40px 16px; color: var(--muted); }
.empty .ic { color: #cbd5e1; margin-bottom: 8px; }

/* ---------- Detail page ---------- */
.profile { display: flex; gap: 16px; align-items: center; flex-wrap: wrap; }
.avatar { width: 58px; height: 58px; border-radius: 16px; background: var(--primary-soft); color: var(--primary); display: grid; place-items: center; font-size: 22px; font-weight: 700; flex-shrink: 0; }
.profile h2 { font-size: 20px; }
.dl { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px 24px; }
.dl div span { display: block; font-size: 12px; color: var(--muted); margin-bottom: 2px; }
.dl div b { font-weight: 600; }
.due-box { border-radius: var(--radius); padding: 18px; text-align: center; }
.due-box.red { background: var(--red-soft); color: var(--red); }
.due-box.green { background: var(--green-soft); color: var(--green); }
.due-box.orange { background: var(--orange-soft); color: var(--orange); }
.due-box.blue { background: var(--blue-soft); color: var(--blue); }
.due-box.grey { background: var(--grey-soft); color: var(--grey); }
.due-box.yellow { background: var(--yellow-soft); color: var(--yellow); }
.pay-summary { margin-top: 14px; padding: 12px 14px; border-radius: 10px; background: var(--surface-2); display: grid; gap: 4px; }
.pay-summary div { display: flex; justify-content: space-between; }
.pay-summary .hl { font-weight: 700; font-size: 15px; }
.due-box .big { font-size: 28px; font-weight: 700; }

/* ---------- Modal ---------- */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: none; align-items: center; justify-content: center; padding: 16px; z-index: 100; }
.modal.open { display: flex; }
.modal-box { background: var(--surface); border-radius: 14px; width: 100%; max-width: 520px; max-height: calc(100vh - 32px); overflow-y: auto; box-shadow: 0 24px 60px rgba(0,0,0,.25); animation: pop .18s ease-out; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 16px; }
.modal-body { padding: 20px; }
@keyframes pop { from { transform: translateY(10px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }

.toast { position: fixed; right: 20px; bottom: 20px; max-width: 360px; background: #1e293b; color: #fff; padding: 16px 18px; border-radius: 12px; box-shadow: 0 16px 40px rgba(0,0,0,.25); z-index: 120; display: flex; gap: 12px; align-items: flex-start; animation: pop .25s ease-out; }
.toast .ic { color: #fca5a5; margin-top: 2px; }
.toast a { color: #93c5fd; font-weight: 600; }

/* ---------- Login ---------- */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; background: linear-gradient(135deg, #1e3a8a, #1d4ed8 55%, #0ea5e9); }
.login-card { width: 100%; max-width: 400px; background: #fff; border-radius: 18px; padding: 32px 28px; box-shadow: 0 30px 80px rgba(0,0,0,.25); }
.login-card .brand-logo { background: var(--primary-soft); color: var(--primary); width: 54px; height: 54px; border-radius: 14px; margin: 0 auto 14px; }
.login-card h1 { text-align: center; font-size: 20px; }
.login-card p { text-align: center; color: var(--muted); margin: 6px 0 24px; }
.login-card .field { margin-bottom: 14px; }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .grid.cols-3 { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 900px) {
  .sidebar { transform: translateX(-100%); }
  body.nav-open .sidebar { transform: none; }
  body.nav-open .backdrop { display: block; position: fixed; inset: 0; background: rgba(15,23,42,.45); z-index: 30; }
  .main { margin-left: 0; }
  .only-mobile { display: inline-grid; }
  .grid.cols-2, .grid.cols-3, .grid.cols-side { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .content { padding: 16px; }
  .topbar { padding: 10px 16px; }
  .today { display: none; }
  .form-grid { grid-template-columns: 1fr; }
  .filters select, .filters input[type=date] { flex: 1 1 140px; }
  .stats { grid-template-columns: 1fr 1fr; gap: 12px; }
  .stat { padding: 14px; flex-direction: column; align-items: flex-start; gap: 8px; }
  .stat .sv { font-size: 18px; }

  /* Tables -> cards */
  table.tbl.responsive thead { display: none; }
  table.tbl.responsive, .tbl.responsive tbody, .tbl.responsive tr, .tbl.responsive td { display: block; width: 100%; }
  .tbl.responsive tr { border-bottom: 1px solid var(--border); padding: 12px 14px; }
  .tbl.responsive tbody tr:last-child { border-bottom: 0; }
  .tbl.responsive td { border: 0; padding: 4px 0; display: flex; justify-content: space-between; gap: 12px; text-align: right; }
  .tbl.responsive td::before { content: attr(data-label); color: var(--muted); font-size: 12px; font-weight: 600; text-align: left; flex-shrink: 0; }
  .tbl.responsive td.primary { display: block; text-align: left; padding-bottom: 6px; }
  .tbl.responsive td.primary::before { display: none; }
  .tbl.responsive td.actions-cell { padding-top: 10px; }
  .tbl.responsive td.actions-cell::before { display: none; }
  .tbl.responsive td.actions-cell .actions { justify-content: flex-start; width: 100%; }
  .tbl.responsive tfoot { display: block; }
  .tbl.responsive tfoot tr { display: flex; justify-content: space-between; }
  .tbl.responsive tfoot td { display: block; width: auto; border: 0; }
  .tbl.responsive tfoot td:empty { display: none; }
  .toast { left: 16px; right: 16px; bottom: 16px; max-width: none; }
}
@media (max-width: 380px) {
  .stats { grid-template-columns: 1fr; }
}

/* ---------- Print ---------- */
@media print {
  .sidebar, .topbar, .foot, .no-print, .flash { display: none !important; }
  .main { margin: 0; }
  .content { padding: 0; }
  body { background: #fff; }
  .card { box-shadow: none; }
}

/* ---------- App install ---------- */
[hidden] { display: none !important; }
.install-bar { display: flex; align-items: center; gap: 14px; padding: 12px 16px; margin-bottom: 20px; border-radius: var(--radius); background: linear-gradient(135deg, #1e3a8a, #1d4ed8); color: #fff; box-shadow: var(--shadow); flex-wrap: wrap; }
.install-bar img { width: 44px; height: 44px; border-radius: 12px; border: 2px solid rgba(255,255,255,.3); }
.install-bar .grow { flex: 1; min-width: 180px; }
.install-bar .small { color: #bfdbfe; }
.install-bar .btn { background: #fff; color: var(--primary-2); }
.install-bar .icon-btn { color: #fff; }
.install-bar .icon-btn:hover { background: rgba(255,255,255,.15); }
@media (max-width: 760px) { .install-btn { display: none !important; } }
/* App (installed) mode: status bar ke liye jagah */
.is-app .topbar { padding-top: max(10px, env(safe-area-inset-top)); }

/* ---------- Profit / Loss box ---------- */
.profit-hero { display: flex; align-items: center; justify-content: space-between; gap: 20px; flex-wrap: wrap; padding: 22px 24px; border-radius: 16px; margin-bottom: 20px; color: #fff; box-shadow: 0 10px 30px rgba(15,23,42,.12); }
.profit-hero.up { background: linear-gradient(135deg, #166534, #16a34a); }
.profit-hero.down { background: linear-gradient(135deg, #991b1b, #dc2626); }
.ph-main { display: flex; align-items: center; gap: 16px; }
.ph-icon { width: 60px; height: 60px; border-radius: 16px; background: rgba(255,255,255,.18); display: grid; place-items: center; flex-shrink: 0; }
.ph-label { font-size: 14px; opacity: .9; font-weight: 600; }
.ph-value { font-size: 34px; font-weight: 800; line-height: 1.1; font-variant-numeric: tabular-nums; }
.ph-break { display: grid; gap: 4px; min-width: 240px; background: rgba(255,255,255,.12); padding: 12px 16px; border-radius: 12px; }
.ph-break div { display: flex; justify-content: space-between; gap: 16px; font-size: 13px; }
.ph-break b { font-variant-numeric: tabular-nums; }
@media (max-width: 760px) { .ph-value { font-size: 26px; } .ph-break { min-width: 0; width: 100%; } }
@media print { .profit-hero { color: #000; background: #fff !important; border: 2px solid #000; box-shadow: none; } }
