:root {
  --bg: #f5f7fb;
  --card: #ffffff;
  --line: #e7ebf3;
  --text: #1f2a44;
  --muted: #7b8499;
  --blue: #2563eb;
  --blue-soft: #e8efff;
  --green: #22c55e;
  --green-soft: #dcfce7;
  --green-text: #15803d;
  --red: #ef4444;
  --red-soft: #fee2e2;
  --red-text: #b91c1c;
  --yellow: #f59e0b;
  --yellow-soft: #fef3c7;
  --yellow-text: #a16207;
  --purple: #8b5cf6;
  --purple-soft: #ede9fe;
  --orange: #f59e0b;
  --orange-soft: #ffedd5;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(16, 24, 40, .04), 0 4px 16px rgba(16, 24, 40, .05);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body { font: 14px/1.45 "Inter", "Segoe UI", system-ui, -apple-system, sans-serif; color: var(--text); background: var(--bg); }
.hidden { display: none !important; }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--blue); text-decoration: none; }
h1, h2, h3 { margin: 0; }

/* ---------- Login ---------- */
.login { min-height: 100%; display: grid; place-items: center; background: linear-gradient(160deg, #eef3ff, #f8fafc); }
.login-card { background: var(--card); padding: 36px 32px; border-radius: 18px; box-shadow: var(--shadow); width: min(360px, 92vw); display: grid; gap: 12px; text-align: center; justify-items: center; }
.login-card h1 { font-size: 22px; }
.login-card p { margin: -8px 0 8px; color: var(--muted); }
.login-card input { width: 100%; }
.login-card .btn { width: 100%; }
.err { color: var(--red-text); min-height: 1.2em; font-size: 13px; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.sidebar { width: 236px; flex: none; background: #f8faff; border-right: 1px solid var(--line); padding: 20px 14px; display: flex; flex-direction: column; position: sticky; top: 0; height: 100vh; }
.brand { display: flex; flex-direction: column; align-items: center; gap: 6px; font-weight: 700; text-align: center; font-size: 15px; line-height: 1.25; padding: 6px 0 22px; }
.sidebar nav { display: grid; gap: 2px; }
.sidebar a { display: flex; align-items: center; gap: 12px; padding: 10px 12px; border-radius: 10px; color: #48526a; font-weight: 500; }
.sidebar a:hover { background: #eef2fb; }
.sidebar a.active { background: var(--blue-soft); color: var(--blue); font-weight: 600; }
.sidebar .nav-bottom { margin-top: auto; }
.ic { width: 18px; height: 18px; display: inline-grid; place-items: center; flex: none; }
.ic svg { width: 18px; height: 18px; }
.main { flex: 1; min-width: 0; display: flex; flex-direction: column; }
.topbar { display: flex; align-items: center; gap: 16px; padding: 22px 28px 8px; }
.topbar h1 { font-size: 22px; font-weight: 700; }
.topbar .sub { color: var(--muted); font-size: 12.5px; margin-top: 2px; }
.top-right { margin-left: auto; display: flex; align-items: center; gap: 14px; }
.user { position: relative; display: flex; align-items: center; gap: 8px; cursor: pointer; padding: 6px 10px; border-radius: 10px; font-weight: 500; }
.user:hover { background: #eef2fb; }
.avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--yellow-soft); color: var(--yellow-text); display: grid; place-items: center; font-weight: 700; }
.caret { color: var(--muted); font-size: 11px; }
.user-menu { position: absolute; right: 0; top: 110%; background: var(--card); border: 1px solid var(--line); border-radius: 10px; box-shadow: var(--shadow); padding: 6px; display: none; z-index: 20; }
.user.open .user-menu { display: block; }
.user-menu button { background: none; border: 0; padding: 8px 14px; cursor: pointer; white-space: nowrap; border-radius: 8px; }
.user-menu button:hover { background: var(--bg); }
.menu-btn { display: none; background: none; border: 0; font-size: 22px; cursor: pointer; }
.view { padding: 12px 28px 32px; }

/* ---------- Cards & grid ---------- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); padding: 18px 20px; }
.card-h { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 10px; }
.card-h h2 { font-size: 15px; font-weight: 700; }
.link { color: var(--blue); font-size: 12.5px; font-weight: 500; cursor: pointer; background: none; border: 0; padding: 0; }
.grid { display: grid; gap: 18px; }
.kpis { grid-template-columns: repeat(4, 1fr); margin-bottom: 18px; }
.two { grid-template-columns: 1.25fr 1fr; margin-bottom: 18px; }
.kpi { display: flex; align-items: center; gap: 14px; padding: 18px; }
.kpi .bubble { width: 46px; height: 46px; border-radius: 50%; display: grid; place-items: center; flex: none; }
.kpi .bubble svg { width: 22px; height: 22px; }
.kpi .lbl { font-size: 12px; color: var(--muted); }
.kpi .val { font-size: 22px; font-weight: 700; letter-spacing: -.3px; }
.b-blue { background: var(--blue); color: #fff; }
.b-orange { background: var(--orange); color: #fff; }
.b-green { background: var(--green); color: #fff; }
.b-purple { background: var(--purple); color: #fff; }
.kpi.k-blue { background: linear-gradient(180deg, #fff, #f5f8ff); }
.kpi.k-orange { background: linear-gradient(180deg, #fff, #fffaf2); }
.kpi.k-green { background: linear-gradient(180deg, #fff, #f3fdf6); }
.kpi.k-purple { background: linear-gradient(180deg, #fff, #f8f5ff); }

/* ---------- Unit tiles ---------- */
.blk { margin-bottom: 12px; }
.blk-t { font-size: 12.5px; font-weight: 600; margin-bottom: 6px; }
.blk-t span { color: var(--muted); font-weight: 400; }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(46px, 1fr)); gap: 6px; }
.tile { border: 1px solid transparent; border-radius: 7px; padding: 5px 0; font-size: 11.5px; font-weight: 600; text-align: center; cursor: pointer; transition: transform .08s; }
.tile:hover { transform: translateY(-1px); }
.tile.paid { background: var(--green-soft); color: var(--green-text); border-color: #bbf7d0; }
.tile.unpaid { background: var(--red-soft); color: var(--red-text); border-color: #fecaca; }
.tile.partial { background: var(--yellow-soft); color: var(--yellow-text); border-color: #fde68a; }
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--muted); align-items: center; flex-wrap: wrap; }
.legend i { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; vertical-align: -1px; }
.dot-g { background: var(--green); } .dot-r { background: var(--red); } .dot-y { background: var(--yellow); }
.dot-b { background: #3b82f6; border-radius: 50% !important; } .dot-o { background: #fb923c; border-radius: 50% !important; }

/* ---------- Chart ---------- */
.chart svg { width: 100%; height: auto; display: block; }
.chart text { font-size: 11px; fill: var(--muted); }
.chart .grid-l { stroke: #eef1f6; }

/* ---------- Drop zone ---------- */
.drop { border: 2px dashed #c7d4ee; border-radius: 12px; background: #f8faff; display: grid; place-items: center; text-align: center; padding: 26px 16px; color: var(--muted); gap: 6px; min-height: 150px; }
.drop b { color: var(--text); font-size: 14px; }
.drop svg { width: 34px; height: 34px; color: var(--blue); }
.drop.over { border-color: var(--blue); background: var(--blue-soft); }

/* ---------- Tables ---------- */
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { padding: 9px 10px; border-bottom: 1px solid #f1f3f8; vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; white-space: nowrap; }
tbody tr.clickable { cursor: pointer; }
tbody tr.clickable:hover td { background: #f8faff; }
.table-wrap { overflow-x: auto; }
.recent td { padding: 8px 6px; }
.recent .name { font-weight: 600; }
.recent .muted { color: var(--muted); font-size: 12.5px; }

/* ---------- Controls ---------- */
.btn { border: 1px solid var(--line); background: var(--card); padding: 8px 14px; border-radius: 9px; cursor: pointer; font-weight: 500; display: inline-flex; align-items: center; gap: 6px; white-space: nowrap; }
.btn:hover { background: #f8faff; }
.btn.primary { background: var(--blue); color: #fff; border-color: var(--blue); }
.btn.primary:hover { background: #1d4ed8; }
.btn.danger { color: var(--red-text); }
.btn.sm { padding: 4px 10px; font-size: 12.5px; }
.wa { display: inline-flex; align-items: center; gap: 5px; background: var(--green); color: #fff; border: 0; border-radius: 999px; padding: 4px 11px; font-size: 12px; font-weight: 600; cursor: pointer; white-space: nowrap; }
.wa:hover { background: #16a34a; }
.wa svg { width: 13px; height: 13px; }
input, select, textarea { border: 1px solid var(--line); background: #fff; border-radius: 9px; padding: 8px 11px; outline: none; }
input:focus, select:focus, textarea:focus { border-color: var(--blue); box-shadow: 0 0 0 3px var(--blue-soft); }
textarea { width: 100%; min-height: 160px; resize: vertical; }
.form { display: grid; grid-template-columns: repeat(auto-fill, minmax(170px, 1fr)); gap: 12px; align-items: end; }
.form label { display: grid; gap: 4px; font-size: 12px; color: var(--muted); font-weight: 500; }
.form label input, .form label select { width: 100%; color: var(--text); }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 14px; }
.toolbar .grow { flex: 1; }
.pill { display: inline-block; padding: 2px 9px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.pill.paid { background: var(--green-soft); color: var(--green-text); }
.pill.unpaid { background: var(--red-soft); color: var(--red-text); }
.pill.partial { background: var(--yellow-soft); color: var(--yellow-text); }
.pill.nakit { background: var(--orange-soft); color: #c2410c; }
.pill.havale { background: var(--blue-soft); color: var(--blue); }
.muted { color: var(--muted); }
.pos { color: var(--red-text); font-weight: 600; }
.neg { color: var(--green-text); font-weight: 600; }
.note { background: var(--blue-soft); color: #1e3a8a; border-radius: 10px; padding: 12px 14px; font-size: 13px; margin-bottom: 14px; }
.note.warn { background: var(--yellow-soft); color: #78350f; }
.section-gap { margin-top: 18px; }
.stat-row { display: flex; gap: 24px; flex-wrap: wrap; margin-top: 10px; }
.stat-row div { font-size: 12px; color: var(--muted); }
.stat-row b { display: block; font-size: 18px; color: var(--text); }

/* ---------- Drawer ---------- */
.drawer { position: fixed; inset: 0; z-index: 40; }
.drawer-bg { position: absolute; inset: 0; background: rgba(15, 23, 42, .35); }
.drawer-panel { position: absolute; right: 0; top: 0; bottom: 0; width: min(560px, 100vw); background: var(--card); box-shadow: -8px 0 30px rgba(0, 0, 0, .12); padding: 22px; overflow-y: auto; }
.drawer-panel h2 { font-size: 19px; }
.drawer-close { float: right; background: none; border: 0; font-size: 22px; cursor: pointer; color: var(--muted); }

.toast { position: fixed; bottom: 22px; left: 50%; transform: translateX(-50%) translateY(20px); background: #111827; color: #fff; padding: 10px 18px; border-radius: 10px; opacity: 0; transition: .2s; pointer-events: none; z-index: 60; }
.toast.show { opacity: 1; transform: translateX(-50%); }

/* ---------- Print (reports, announcements) ---------- */
.print-area { background: #fff; }
.a4 { background: #fff; max-width: 820px; margin: 0 auto; padding: 40px 46px; border: 1px solid var(--line); border-radius: 6px; }
.a4 h1 { text-align: center; font-size: 22px; margin-bottom: 18px; }
.a4 .body { font-size: 17px; line-height: 1.7; white-space: pre-wrap; text-align: center; }
.a4 .sig { text-align: right; margin-top: 40px; font-weight: 600; }
@media print {
  body { background: #fff; }
  .sidebar, .topbar, .no-print, .toast { display: none !important; }
  .view { padding: 0; }
  .card { box-shadow: none; border: 0; padding: 0; }
  .a4 { border: 0; padding: 0; max-width: none; }
}

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .two { grid-template-columns: 1fr; }
}
@media (max-width: 760px) {
  .sidebar { position: fixed; z-index: 30; left: 0; transform: translateX(-100%); transition: transform .2s; box-shadow: 4px 0 20px rgba(0, 0, 0, .1); }
  .sidebar.open { transform: none; }
  .menu-btn { display: block; }
  .topbar { padding: 14px 16px 6px; flex-wrap: wrap; }
  .topbar h1 { font-size: 18px; }
  .top-right { width: 100%; justify-content: space-between; }
  .view { padding: 10px 14px 24px; }
  .kpi .val { font-size: 18px; }
  .kpi { padding: 14px; gap: 10px; }
  .kpi .bubble { width: 38px; height: 38px; }
}
.print-only { display: none; }
@media print {
  .print-only { display: block; }
  .print-only .a4 h1 { font-size: 34px; margin-top: 60px; }
  .print-only .a4 .body { font-size: 24px; }
}
