:root {
  --bg: #f5f7fa;
  --panel: #ffffff;
  --ink: #0f1e2e;
  --muted: #6b7a8d;
  --line: #e6eaf0;
  --brand: #1e5eff;
  --brand-dk: #1848c9;
  --in: #0f9d58;        /* inflow / positive */
  --in-bg: #e9f7ef;
  --out: #d64545;       /* outflow / negative */
  --out-bg: #fdecec;
  --neutral: #33475b;
  --shadow: 0 1px 3px rgba(16,30,54,.06), 0 1px 2px rgba(16,30,54,.04);
}
* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg); color: var(--ink); line-height: 1.45; font-size: 14px;
}
.app { max-width: 1180px; margin: 0 auto; padding: 0 20px 48px; }

/* Top bar */
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 24px 4px 18px; gap: 16px; flex-wrap: wrap;
}
.topbar h1 { margin: 0; font-size: 24px; letter-spacing: -.3px; }
.sub { color: var(--muted); margin: 2px 0 0; font-size: 13px; }
.topbar-actions { display: flex; align-items: center; gap: 14px; }
.fetch-status { font-size: 13px; color: var(--muted); max-width: 320px; }

/* Buttons */
.btn {
  border: 1px solid var(--line); background: #fff; color: var(--ink);
  padding: 9px 16px; border-radius: 9px; font-size: 14px; cursor: pointer;
  font-weight: 500; transition: background .12s, box-shadow .12s;
}
.btn:hover { background: #f0f3f8; }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); box-shadow: var(--shadow); }
.btn.primary:hover { background: var(--brand-dk); }
.btn:disabled { opacity: .55; cursor: not-allowed; pointer-events: none; }

.flash {
  margin: 0 0 16px; padding: 11px 15px; background: var(--in-bg);
  border: 1px solid #b7e4c7; border-radius: 9px; font-size: 13px;
}

/* Nav tabs */
.tabs { display: flex; gap: 6px; margin: 0 4px 18px; border-bottom: 1px solid var(--line); }
.tabs.sub { margin: 0 0 14px; border-bottom: none; }
.tab {
  border: none; background: transparent; color: var(--muted); cursor: pointer;
  padding: 9px 14px; font-size: 14px; font-weight: 600; border-radius: 8px 8px 0 0;
  border-bottom: 2px solid transparent; transition: color .12s, background .12s;
}
.tabs.sub .tab { border-radius: 8px; border-bottom: none; background: #fff; border: 1px solid var(--line); }
.tab:hover { color: var(--ink); background: #eef1f6; }
.tab.active { color: var(--brand); border-bottom-color: var(--brand); }
.tabs.sub .tab.active { color: #fff; background: var(--brand); border-color: var(--brand); }
.view[hidden] { display: none; }

/* Records tools (search + count) */
.records-tools { display: flex; align-items: center; gap: 14px; }
#records-search {
  padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 13.5px; background: #fff; color: var(--ink); min-width: 260px;
}
#records-search:focus { outline: 2px solid #cdd9ff; border-color: var(--brand); }

/* Sortable column header */
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--ink); }
th.sortable.active { color: var(--brand); }
.sort-arrow { font-size: 10px; }

/* Records table grouping */
tr.group-head { cursor: pointer; background: #f7f9fc; font-weight: 600; }
tr.group-head:hover { background: #eef2f9; }
tr.group-head .chevron { display: inline-block; width: 12px; color: var(--brand); }
tr.group-child td { color: var(--muted); font-weight: 400; }

/* Controls */
.controls {
  display: flex; align-items: flex-end; gap: 18px; flex-wrap: wrap;
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 18px;
}
.field { display: flex; flex-direction: column; gap: 6px; }
.field label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; }
.field input, .field select {
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 8px;
  font-size: 14px; background: #fff; color: var(--ink); min-width: 150px;
}
.field input:focus, .field select:focus { outline: 2px solid #cdd9ff; border-color: var(--brand); }
.money-input { display: flex; align-items: center; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: #fff; }
.money-input span { padding: 0 10px; color: var(--muted); background: #f2f5fa; align-self: stretch; display: flex; align-items: center; font-weight: 600; }
.money-input input { border: none; min-width: 180px; font-size: 16px; font-weight: 600; }
.money-input input:focus { outline: none; }
.controls .btn { align-self: flex-end; }

/* KPI cards */
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 14px; margin-bottom: 18px; }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: 14px;
  padding: 16px 18px; box-shadow: var(--shadow); border-left: 4px solid var(--line);
}
.card.in { border-left-color: var(--in); }
.card.out { border-left-color: var(--out); }
.card.neutral { border-left-color: var(--brand); }
.card-t { font-size: 12px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .4px; }
.card-v { font-size: 26px; font-weight: 700; margin: 6px 0 2px; letter-spacing: -.5px; }
.card.in .card-v { color: var(--in); }
.card.out .card-v { color: var(--out); }
.card-c { font-size: 12px; color: var(--muted); }

/* Panels */
.panel { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 18px 20px; box-shadow: var(--shadow); margin-bottom: 18px; }
.panel-head { display: flex; align-items: baseline; justify-content: space-between; margin-bottom: 14px; }
.panel-head h2 { margin: 0; font-size: 16px; }
/* Collapsible Config panels — collapse to just the header so all settings fit on screen. */
#view-config .panel-head { cursor: pointer; user-select: none; align-items: center; }
#view-config .panel-head h2 { display: flex; align-items: center; gap: 8px; }
#view-config .panel-head .chevron { display: inline-block; width: 15px; color: var(--brand); font-size: 15px; }
#view-config .panel.collapsed .panel-head { margin-bottom: 0; }
#view-config .panel.collapsed > :not(.panel-head) { display: none; }
.muted { color: var(--muted); font-size: 13px; }
.warn { color: var(--out); font-weight: 600; }

/* Chart */
.chart { display: flex; align-items: stretch; gap: 4px; height: 200px; padding-top: 6px; }
.bar-col { flex: 1; display: flex; flex-direction: column; align-items: center; min-width: 0; }
.bar-slot { width: 100%; display: flex; justify-content: center; align-items: flex-end; height: 82px; }
.bar-slot.bot { align-items: flex-start; }
.bar { width: 62%; max-width: 34px; border-radius: 4px 4px 0 0; }
.bar.up { background: linear-gradient(180deg, #34c77b, var(--in)); }
.bar.down { background: linear-gradient(180deg, var(--out), #b02f2f); border-radius: 0 0 4px 4px; }
.axis { width: 100%; height: 1px; background: var(--line); }
.bar-lbl { font-size: 10px; color: var(--muted); margin-top: 6px; white-space: nowrap; transform: rotate(-40deg); transform-origin: center; height: 28px; }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { text-align: right; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th.l, td.l { text-align: left; }
thead th { color: var(--muted); font-weight: 600; font-size: 11.5px; text-transform: uppercase; letter-spacing: .3px; position: sticky; top: 0; background: #fbfcfe; }
thead th.in { color: var(--in); }
thead th.out { color: var(--out); }
tbody tr:hover { background: #f7f9fc; }
td.in { color: var(--in); }
td.out { color: var(--out); }
td.strong-in { color: var(--in); }
td.strong-out { color: var(--out); }
tr.danger td.strong-out { background: var(--out-bg); }
.foot { margin: 14px 2px 0; line-height: 1.5; }

@media (max-width: 640px) {
  .controls { gap: 12px; }
  .field input, .field select, .money-input input { min-width: 120px; }
}

/* Fetch progress modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(15, 30, 54, .45);
  display: flex; align-items: center; justify-content: center;
  padding: 20px;
}
.modal-backdrop[hidden] { display: none; }
.modal {
  background: var(--panel); border-radius: 14px; width: 100%; max-width: 460px;
  box-shadow: 0 12px 40px rgba(16, 30, 54, .25); overflow: hidden;
}
.modal-head { padding: 18px 22px 12px; border-bottom: 1px solid var(--line); }
.modal-head h3 { margin: 0 0 3px; font-size: 17px; }
.modal-head .muted { font-size: 13px; }
.modal-foot { padding: 14px 22px 18px; display: flex; justify-content: flex-end; }

.fetch-progress { list-style: none; margin: 0; padding: 10px 22px 4px; }
.fetch-progress li {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px;
}
.fetch-progress li:last-child { border-bottom: none; }
.fetch-progress .fp-icon { width: 20px; text-align: center; flex: none; }
.fetch-progress .fp-name { font-weight: 600; min-width: 120px; }
.fetch-progress .fp-detail { font-size: 12.5px; }
.fetch-progress li.ok  .fp-icon { color: var(--in); }
.fetch-progress li.err .fp-icon { color: var(--out); }
.fetch-progress li.err .fp-detail { color: var(--out); }

/* Original-currency badge on converted (non-INR) records */
.ccy {
  display: inline-block; margin-left: 6px; padding: 1px 6px;
  font-size: 11px; font-weight: 600; color: var(--brand-dk);
  background: #eef3ff; border: 1px solid #d5e1ff; border-radius: 6px;
  vertical-align: middle;
}

/* Per-month invoice open/draft counts under the incoming amount */
.sub-count { font-size: 11px; color: var(--muted); font-weight: 600; margin-top: 2px; }

/* Config tab: Salaries & Wages override inputs */
#salary-table input.salary-input {
  width: 160px; text-align: right; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 8px; font-size: 14px;
  font-family: inherit;
}
#salary-table input.salary-input:focus { outline: none; border-color: var(--brand); }
#salary-table td { vertical-align: middle; }

/* Config tab: customer exclusion */
#cust-search { padding: 8px 12px; border: 1px solid var(--line); border-radius: 8px; font-size: 14px; font-family: inherit; min-width: 220px; }
#cust-search:focus { outline: none; border-color: var(--brand); }
#cust-table input.cust-ex { width: 17px; height: 17px; cursor: pointer; }

/* Forecast drill-down */
.fc-row { cursor: pointer; }
.fc-row:hover { background: #f0f3f8; }
.fc-row .chevron { display: inline-block; width: 16px; color: var(--muted); font-size: 15px; }
.fc-detail > td { background: #f8fafc; padding: 0; }
/* Past (actuals) rows: subtle neutral tint to distinguish real data from projection. */
tr.fc-row.actual { background: #f7f9fc; }
tr.fc-row.actual:hover { background: #eef2f9; }
tr.fc-row.actual .pill { background: #e3ecff; color: var(--brand); }
/* Single-column stack of sections — avoids the side-by-side overlap. */
.di-wrap { padding: 12px 16px 18px; display: flex; flex-direction: column; gap: 10px; }
.di-sec { min-width: 0; }
.di-h { font-weight: 700; font-size: 13px; margin: 0 0 4px; color: var(--neutral); }
/* Collapsible section / party-group headers in the drill-down. */
.di-toggle { display: flex; align-items: center; gap: 6px; cursor: pointer; user-select: none;
  padding: 5px 8px; border-radius: 6px; }
.di-toggle:hover { background: #eef2f9; }
.di-toggle .chevron { display: inline-block; width: 16px; color: var(--brand); font-size: 15px; flex: none; }
.di-toggle.di-h { margin: 0; }
.di-toggle .di-sub { margin-left: auto; font-weight: 600; color: var(--neutral); white-space: nowrap; padding-left: 12px; }
/* Party sub-group header: indented, lighter than its section. */
.di-gh { padding-left: 22px; font-size: 13px; font-weight: 600; color: var(--neutral); }
.di-gh .di-sub { font-weight: 500; }
.di-body { display: flex; flex-direction: column; gap: 6px; }
.di-body[hidden] { display: none; }   /* explicit display: must re-hide when collapsed */
.di-grp-body { margin-left: 10px; }
.di-tbl { width: 100%; border-collapse: collapse; table-layout: fixed; }
.di-tbl td { padding: 5px 8px; border-bottom: 1px solid var(--line); font-size: 13px; vertical-align: middle; }
/* checkbox | label (flex) | amount (right) */
.di-tbl td.di-chk { width: 34px; text-align: center; }
.di-tbl td.l { white-space: normal; word-break: break-word; }
.di-tbl td.di-amt { width: 230px; text-align: right; white-space: nowrap; }
.di-chk input { width: 16px; height: 16px; cursor: pointer; }
.di-chk input:disabled { opacity: .5; }
/* Unchecked (not considered) rows are dimmed + struck. */
.di-gone td.l, .di-gone td.di-amt { text-decoration: line-through; color: var(--muted); }
.pill { display: inline-block; padding: 0 6px; font-size: 11px; border-radius: 5px; background: #eef1f6; color: var(--neutral); }
/* Deep links to a Zoho Books record inside the forecast drill-down. */
.zoho-link { color: var(--brand); text-decoration: none; }
.zoho-link:hover { text-decoration: underline; }

/* ---- Auth: topbar user chip + Users config action buttons ---- */
.user-chip {
  font-size: 13px; font-weight: 600; color: var(--neutral);
  background: #eef1f6; border: 1px solid var(--line);
  padding: 5px 11px; border-radius: 999px;
}
#users-table .btn { padding: 5px 10px; font-size: 12.5px; margin-right: 6px; }
#users-table .user-del { color: var(--out); border-color: #f3c2c2; }
#users-table .user-del:hover { background: var(--out-bg); }

/* ---- Brand logo in the topbar ---- */
.brand { display: flex; align-items: center; gap: 14px; }
.brand-logo { height: 44px; width: auto; display: block; }

/* ---- Stop button in the fetch modal ---- */
#fetch-modal-stop { color: var(--out); border-color: #f3c2c2; }
#fetch-modal-stop:hover { background: var(--out-bg); }
.modal-foot { display: flex; gap: 8px; justify-content: flex-end; }

/* ---- Custom (manual) forecast entries ---- */
.custom-form {
  border: 1px solid var(--line); border-radius: 10px; background: #f8fafc;
  padding: 14px 16px; margin: 0 0 16px;
}
.custom-form .controls { flex-wrap: wrap; gap: 12px; }
button.custom-del {
  padding: 2px 8px; font-size: 12px; line-height: 1.4; color: var(--out);
  border-color: #f3c2c2; background: #fff;
}
button.custom-del:hover { background: var(--out-bg); }
