:root {
  --color-primary: #146c43;
  --color-primary-dark: #0f5132;
  --color-primary-light: #e7f4ec;
  --color-danger: #b02a37;
  --color-danger-light: #fbe8ea;
  --color-warning: #a15c00;
  --color-warning-light: #fff3e0;
  --color-text: #1d2229;
  --color-text-muted: #4b5563;
  --color-border: #e2e5ea;
  --color-bg: #f6f7f9;
  --color-surface: #ffffff;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.08);
  --topbar-h: 62px;
  --bottomnav-h: 64px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--color-bg);
  color: var(--color-text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  font-size: 17px;
  font-weight: 500;
  line-height: 1.5;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overflow-x: hidden;
  width: 100%;
}

a { color: inherit; text-decoration: none; }

h1, h2, h3, h4 { margin: 0 0 0.5em; line-height: 1.25; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.2rem; }
h3 { font-size: 1.05rem; }
p { margin: 0 0 0.75em; }

/* ---------------- Top bar (all sizes) ---------------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--color-surface);
  border-bottom: 1px solid var(--color-border);
}
.topbar-inner {
  height: var(--topbar-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 16px;
}
.brand { font-weight: 700; color: var(--color-primary-dark); font-size: 1.1rem; }
.topbar-actions { display: flex; align-items: center; gap: 12px; }
.user-chip { font-size: 0.92rem; font-weight: 600; color: var(--color-text-muted); }
.btn-link {
  background: none; border: none; color: var(--color-primary-dark);
  font-size: 0.95rem; font-weight: 700; cursor: pointer;
  padding: 12px 4px; min-height: 44px;
}

/* Desktop nav row (hidden on mobile) */
.desktop-nav { display: none; }

/* ---------------- Main content ---------------- */
.page { padding: 16px; max-width: 1080px; margin: 0 auto; }
.page.with-nav { padding-bottom: calc(var(--bottomnav-h) + 24px); }

/* ---------------- Bottom nav (mobile) ---------------- */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  height: var(--bottomnav-h);
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  display: flex;
  align-items: stretch;
  z-index: 50;
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom-nav a {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  font-size: 0.72rem;
  font-weight: 600;
  color: var(--color-text-muted);
}
.bottom-nav a.active { color: var(--color-primary); font-weight: 700; }
.bottom-nav .icon { font-size: 1.3rem; line-height: 1; }
.bottom-nav-cta {
  flex: 0 0 56px !important;
  align-self: center;
  margin: 0 4px;
}
.bottom-nav-cta .icon {
  background: var(--color-primary);
  color: #fff;
  width: 48px; height: 48px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.6rem;
  box-shadow: var(--shadow);
}

/* ---------------- Cards / lists ---------------- */
.card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 12px;
  box-shadow: var(--shadow);
}
.card-title { font-weight: 600; margin-bottom: 8px; }
.grid-stats {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 16px;
}
.stat-tile {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px;
}
.stat-tile .label { font-size: 0.85rem; font-weight: 700; color: var(--color-text-muted); margin-bottom: 4px; }
.stat-tile .value { font-size: 1.4rem; font-weight: 800; }
.stat-tile.warn .value { color: var(--color-warning); }
.stat-tile.danger .value { color: var(--color-danger); }
.stat-tile.primary .value { color: var(--color-primary-dark); }

.section-head {
  display: flex; align-items: center; justify-content: space-between;
  margin: 20px 0 10px;
}
.section-head h2 { margin: 0; }

/* ---------------- Expense / transaction card (mobile list) ---------------- */
.txn-card {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 12px 14px;
  margin-bottom: 10px;
  display: block;
}
.txn-top { display: flex; justify-content: space-between; align-items: flex-start; gap: 8px; }
.txn-title { font-weight: 700; font-size: 1.02rem; }
.txn-amount { font-weight: 800; white-space: nowrap; }
.txn-amount.type-advance { color: var(--color-warning); }
.txn-amount.type-return { color: var(--color-danger); }
.txn-meta { font-size: 0.9rem; font-weight: 600; color: var(--color-text-muted); margin-top: 4px; }
.txn-narration { font-size: 0.92rem; margin-top: 6px; color: var(--color-text); }
.badge {
  display: inline-block;
  font-size: 0.85rem;
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--color-primary-light);
  color: var(--color-primary-dark);
  font-weight: 700;
  margin-left: 6px;
}
.badge.advance { background: var(--color-warning-light); color: var(--color-warning); }
.badge.return { background: var(--color-danger-light); color: var(--color-danger); }
.badge.cancelled { background: #eee; color: #888; text-decoration: line-through; }

/* Category badge — light color per top-level category, so the eye can
   group entries (Material, Labour, Equipment...) at a glance. */
.cat-badge {
  display: inline-block;
  font-size: 0.8rem;
  font-weight: 700;
  padding: 3px 10px;
  border-radius: 999px;
  margin-top: 4px;
}
.cat-badge.cat-material { background: #dcf5f1; color: #0d7a6c; }
.cat-badge.cat-equipment { background: #dfeefd; color: #1567b3; }
.cat-badge.cat-labour { background: #e3f5e1; color: #2f7a34; }
.cat-badge.cat-professional { background: #efe6fb; color: #7040c2; }
.cat-badge.cat-construction { background: #fdf0da; color: #9a6300; }
.cat-badge.cat-miscellaneous, .cat-badge.cat-other { background: #edf0f3; color: #56606d; }

/* ---------------- Tables (desktop) ---------------- */
.table-wrap { overflow-x: auto; }
table.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--color-surface);
  border-radius: var(--radius);
  overflow: hidden;
}
table.data-table th, table.data-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--color-border);
  font-size: 0.95rem;
  font-weight: 500;
  white-space: nowrap;
}
table.data-table td.wrap { white-space: normal; }
table.data-table th { background: #fafbfc; font-weight: 700; color: var(--color-text-muted); }
table.data-table td.amount, table.data-table th.amount { text-align: right; }
table.data-table tfoot td { font-weight: 700; background: #fafbfc; }
table.data-table .cat-badge { margin-top: 0; }

/* Expenses list: fixed column widths that add up to 100%, with long
   fields wrapping instead of forcing the table wider than its container
   — so the whole row is visible on load, no horizontal scrollbar needed. */
table.data-table-expenses { table-layout: fixed; }
table.data-table-expenses th, table.data-table-expenses td {
  white-space: normal;
  word-break: break-word;
  overflow-wrap: break-word;
}
table.data-table-expenses th:nth-child(1), table.data-table-expenses td:nth-child(1) { width: 11%; }
table.data-table-expenses th:nth-child(2), table.data-table-expenses td:nth-child(2) { width: 13%; }
table.data-table-expenses th:nth-child(3), table.data-table-expenses td:nth-child(3) { width: 15%; }
table.data-table-expenses th:nth-child(4), table.data-table-expenses td:nth-child(4) { width: 12%; }
table.data-table-expenses th:nth-child(5), table.data-table-expenses td:nth-child(5) { width: 12%; }
table.data-table-expenses th:nth-child(6), table.data-table-expenses td:nth-child(6) { width: 13%; }
table.data-table-expenses th:nth-child(7), table.data-table-expenses td:nth-child(7) { width: 24%; }

/* Key/value detail table (single-record view) — always wraps, never forces horizontal scroll */
table.detail-table { table-layout: fixed; width: 100%; }
table.detail-table th, table.detail-table td {
  white-space: normal;
  overflow-wrap: break-word;
  word-break: break-word;
}
table.detail-table td:first-child {
  width: 38%;
  color: var(--color-text-muted);
  font-weight: 700;
}

/* ---------------- Forms ---------------- */
.form-group { margin-bottom: 16px; }
label { display: block; font-weight: 700; font-size: 0.95rem; margin-bottom: 6px; }
label .optional { font-weight: 500; color: var(--color-text-muted); }
input[type=text], input[type=number], input[type=date], input[type=password],
input[type=search], input[type=tel], select, textarea {
  width: 100%;
  padding: 14px;
  min-height: 48px;
  font-size: 1rem;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  background: var(--color-surface);
  color: var(--color-text);
  font-family: inherit;
}
textarea { resize: vertical; min-height: 70px; }
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--color-primary);
  box-shadow: 0 0 0 3px var(--color-primary-light);
}
.help-text { font-size: 0.86rem; font-weight: 500; color: var(--color-text-muted); margin-top: 4px; }
.input-prefix { position: relative; }
.input-prefix span {
  position: absolute; left: 14px; top: 50%; transform: translateY(-50%);
  color: var(--color-text-muted);
}
.input-prefix input { padding-left: 28px; }

/* Number inputs: hide the spinner arrows (easy to bump by mistake, e.g.
   scrolling the page while the cursor sits over the field, which also
   silently changes the value one step at a time). */
input[type=number]::-webkit-outer-spin-button,
input[type=number]::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
input[type=number] {
  -moz-appearance: textfield;
  appearance: textfield;
}

.radio-group { display: flex; gap: 10px; flex-wrap: wrap; }
.radio-option {
  flex: 1;
  min-width: 100px;
  min-height: 44px;
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 12px;
  display: flex; align-items: center; gap: 8px;
  cursor: pointer;
  font-weight: 600;
}
.radio-option input { width: auto; }
.radio-option.checked { border-color: var(--color-primary); background: var(--color-primary-light); }

/* Searchable combobox */
.combo { position: relative; }
.combo input[type=text] { width: 100%; }
.combo-options {
  position: absolute;
  top: calc(100% + 4px);
  left: 0; right: 0;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(16,24,40,0.12);
  max-height: 260px;
  overflow-y: auto;
  z-index: 40;
  display: none;
}
.combo-options.open { display: block; }
.combo-option { padding: 13px 14px; min-height: 44px; box-sizing: border-box; cursor: pointer; font-size: 1rem; font-weight: 500; }
.combo-option .path-parent { color: var(--color-text-muted); font-size: 0.86rem; }
.combo-option:hover, .combo-option.highlighted { background: var(--color-primary-light); }
.combo-option.add-new { color: var(--color-primary-dark); font-weight: 600; border-top: 1px solid var(--color-border); }
.combo-clear {
  position: absolute; right: 2px; top: 50%; transform: translateY(-50%);
  background: none; border: none; color: var(--color-text-muted); font-size: 1.3rem; cursor: pointer;
  display: none;
  width: 44px; height: 44px;
}
.combo.has-value .combo-clear { display: block; }

/* ---------------- Buttons ---------------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center;
  gap: 6px;
  padding: 12px 20px;
  font-size: 1rem;
  font-weight: 600;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  cursor: pointer;
  text-align: center;
}
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-primary:hover { background: var(--color-primary-dark); }
.btn-secondary { background: var(--color-surface); border-color: var(--color-border); color: var(--color-text); }
.btn-danger { background: var(--color-danger); color: #fff; }
.btn-outline { background: transparent; border-color: var(--color-primary); color: var(--color-primary-dark); }
.btn-block { width: 100%; }
.btn-sm { padding: 12px 16px; font-size: 0.95rem; min-height: 44px; }
.btn-nav-cta { background: var(--color-primary); color: #fff !important; padding: 8px 16px; border-radius: 999px; font-weight: 600; }

.actions-row { display: flex; gap: 14px; flex-wrap: wrap; margin-top: 16px; }

/* ---------------- Alerts / flash ---------------- */
.alert {
  padding: 12px 14px;
  border-radius: var(--radius-sm);
  margin-bottom: 14px;
  font-size: 0.95rem;
  font-weight: 600;
}
.alert-success { background: var(--color-primary-light); color: var(--color-primary-dark); }
.alert-error { background: var(--color-danger-light); color: var(--color-danger); }
.field-error { color: var(--color-danger); font-size: 0.85rem; font-weight: 600; margin-top: 4px; }

/* ---------------- Filters ---------------- */
.filter-bar {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 16px;
}
.filter-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}
.filter-grid .form-group { margin-bottom: 0; }
.search-box { margin-bottom: 12px; }

/* ---------------- Dashboard insight panels ---------------- */
.insight-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  margin-bottom: 8px;
}
.insight-card { margin-bottom: 0; }
.insight-card-title {
  font-weight: 700;
  font-size: 1.05rem;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
}
.insight-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}
.insight-dot.party { background: var(--color-primary); }
.insight-dot.material { background: #6a4fd6; }
.insight-row {
  display: block;
  padding: 8px 0;
  border-bottom: 1px solid var(--color-border);
}
.insight-row:last-of-type { border-bottom: none; }
.insight-row-top {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 5px;
  font-size: 0.96rem;
}
.insight-label { font-weight: 700; color: var(--color-text); }
.insight-value { font-weight: 700; color: var(--color-text-muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.insight-bar { background: var(--color-bg); border-radius: 999px; height: 6px; overflow: hidden; }
.insight-bar > span { display: block; height: 100%; border-radius: 999px; }
.insight-bar > span.party { background: linear-gradient(90deg, var(--color-primary), #3fae7a); }
.insight-bar > span.material { background: linear-gradient(90deg, #6a4fd6, #9c86f0); }
.insight-more {
  display: block;
  text-align: center;
  font-size: 0.9rem;
  font-weight: 700;
  color: var(--color-primary-dark);
  padding-top: 12px;
  margin-top: 4px;
}

.deal-card { display: block; }
.deal-remaining {
  font-weight: 700;
  white-space: nowrap;
  color: var(--color-warning);
}
.deal-remaining.over { color: var(--color-danger); }

/* ---------------- Category tree list ---------------- */
.cat-tree-row {
  display: flex; align-items: center; justify-content: space-between;
  gap: 10px;
  padding: 14px 12px;
  border-bottom: 1px solid var(--color-border);
}
.cat-tree-row:last-child { border-bottom: none; }
.cat-tree-name.inactive { color: var(--color-text-muted); text-decoration: line-through; }

/* ---------------- Login page ---------------- */
.login-wrap {
  min-height: 100vh;
  display: flex; align-items: center; justify-content: center;
  padding: 24px;
}
.login-card {
  width: 100%; max-width: 380px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius);
  padding: 28px 24px;
  box-shadow: var(--shadow);
}
.login-card h1 { text-align: center; color: var(--color-primary-dark); font-size: 1.4rem; }
.login-sub { text-align: center; color: var(--color-text-muted); font-size: 0.92rem; font-weight: 600; margin-bottom: 20px; }

/* ---------------- Utility ---------------- */
.text-muted { color: var(--color-text-muted); }
.text-right { text-align: right; }
.mt-0 { margin-top: 0; }
.mb-0 { margin-bottom: 0; }
.empty-state { text-align: center; padding: 40px 16px; color: var(--color-text-muted); }
.progress-bar { background: var(--color-border); border-radius: 999px; height: 8px; overflow: hidden; }
.progress-bar > span { display: block; height: 100%; background: var(--color-primary); }
.progress-bar.over > span { background: var(--color-danger); }

/* ---------------- Responsive show/hide ---------------- */
.show-desktop { display: none; }
.hide-desktop { display: block; }

/* ---------------- Modal ---------------- */
.modal-backdrop {
  display: none;
  position: fixed; inset: 0;
  background: rgba(16, 24, 40, 0.45);
  z-index: 100;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.modal-backdrop.open { display: flex; }
.modal-card {
  background: var(--color-surface);
  border-radius: var(--radius);
  padding: 20px;
  width: 100%;
  max-width: 360px;
}
.modal-card h3 { margin-bottom: 14px; }
.modal-actions { display: flex; gap: 10px; margin-top: 16px; }
.modal-actions .btn { flex: 1; }

/* ---------------- Desktop breakpoint ---------------- */
@media (min-width: 860px) {
  .bottom-nav { display: none; }
  .show-desktop { display: block; }
  .hide-desktop { display: none; }
  .page.with-nav { padding-bottom: 24px; }
  .desktop-nav {
    display: flex;
    align-items: center;
    gap: 4px;
    padding: 0 16px 10px;
    flex-wrap: wrap;
  }
  .desktop-nav a { padding: 8px 12px; border-radius: var(--radius-sm); font-size: 0.9rem; color: var(--color-text-muted); font-weight: 500; }
  .desktop-nav a.active, .desktop-nav a:hover { background: var(--color-primary-light); color: var(--color-primary-dark); }
  .desktop-nav .btn-nav-cta { margin-left: auto; }
  .nav-dropdown { position: relative; }
  .nav-dropdown-menu {
    display: none;
    position: absolute; top: 100%; left: 0;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: 0 8px 24px rgba(16,24,40,0.12);
    min-width: 190px;
    z-index: 60;
    padding: 6px;
  }
  .nav-dropdown:hover .nav-dropdown-menu { display: block; }
  .nav-dropdown-menu a { display: block; padding: 8px 10px; border-radius: 6px; }
  .grid-stats { grid-template-columns: repeat(5, 1fr); }
  .filter-grid { grid-template-columns: repeat(4, 1fr); }
  .insight-grid { grid-template-columns: 1fr 1fr; }
  .page { padding: 20px 24px; }
}
