:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --border: #e3e8ef;
  --text: #1a2230;
  --muted: #667085;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --link-strong: #0f3f96;
  --danger: #dc2626;
  --ok: #16a34a;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(16, 24, 40, 0.06), 0 1px 2px rgba(16, 24, 40, 0.04);
  --sidebar-w: 232px;
  --control-h: 34px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  line-height: 1.45;
}
a { color: var(--primary); text-decoration: none; }
a:hover { color: var(--primary-dark); }

/* ---------- App frame ---------- */
.app { min-height: 100vh; }

.sidebar {
  position: fixed; top: 0; left: 0; bottom: 0; width: var(--sidebar-w);
  background: var(--surface); border-right: 1px solid var(--border);
  display: flex; flex-direction: column; padding: 16px 12px;
}
.brand { display: flex; align-items: center; gap: 10px; font-weight: 700; font-size: 18px; padding: 6px 8px 18px; }
.logo {
  width: 30px; height: 30px; border-radius: 8px; background: var(--primary);
  color: #fff; display: grid; place-items: center; font-weight: 800;
}
.nav { display: flex; flex-direction: column; gap: 2px; flex: 1; }
.nav-link {
  display: flex; align-items: center; gap: 11px; padding: 10px 12px;
  border-radius: 9px; color: var(--muted); font-weight: 500;
}
.nav-link svg { width: 19px; height: 19px; flex: none; }
.nav-link:hover { background: var(--bg); color: var(--text); }
.nav-link.active { background: #eef3ff; color: var(--primary); }
.sidebar-foot { border-top: 1px solid var(--border); padding-top: 12px; }
.who { font-weight: 600; font-size: 14px; }
.who small { display: block; color: var(--muted); font-weight: 400; }
.sidebar-foot-links { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
.account-link { font-size: 13px; color: var(--primary); text-decoration: none; }
.account-link:hover { text-decoration: underline; }
.signout { font-size: 13px; color: var(--muted); margin-left: auto; }

.main { margin-left: var(--sidebar-w); min-height: 100vh; }
.topbar {
  position: sticky; top: 0; z-index: 5; background: var(--surface);
  border-bottom: 1px solid var(--border); padding: 14px 24px;
  display: flex; align-items: center; justify-content: space-between;
}
.topbar-title { font-weight: 700; font-size: 17px; }
.topbar-user { color: var(--muted); font-size: 14px; }
.content { padding: 18px; max-width: none; width: 100%; }
.flash { margin: 16px 24px 0; padding: 10px 14px; background: #ecfdf3; color: #027a48; border-radius: 9px; font-size: 14px; }
.flash-success { background: #ecfdf3; color: #027a48; }

.bottomnav { display: none; }

/* ---------- Components ---------- */
.card {
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; box-shadow: var(--shadow); padding: 12px; margin-bottom: 12px;
}
.card h2 { margin: 0 0 4px; font-size: 16px; }
.card h2 + .sub { margin-top: 0; }
.sub { color: var(--muted); font-size: 12px; margin: 0 0 10px; }

.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; gap: 10px; flex-wrap: wrap; }
.page-head h1 { font-size: 18px; margin: 0; line-height: 1.2; }

.grid { display: grid; gap: 12px; }
.grid.cols-2 { grid-template-columns: repeat(2, 1fr); }
.grid.cols-3 { grid-template-columns: repeat(3, 1fr); }
.grid.cols-4 { grid-template-columns: repeat(4, 1fr); }

.stat { background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 12px; box-shadow: var(--shadow); }
.stat .n { font-size: 20px; font-weight: 700; }
.stat .l { color: var(--muted); font-size: 12px; }

/* Buttons */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px; min-height: 32px; padding: 6px 10px;
  border-radius: 7px; border: 1px solid var(--border); background: var(--surface);
  color: var(--text); font-weight: 600; font-size: 13px; line-height: 1.2; cursor: pointer;
  white-space: nowrap;
}
.btn:hover { background: var(--bg); }
.btn.primary { background: var(--primary); border-color: var(--primary); color: #fff; }
.btn.primary:hover { background: var(--primary-dark); }
.btn.sm { min-height: 28px; padding: 4px 8px; font-size: 12px; }
.btn.danger { color: var(--danger); border-color: #f3c2c2; }

/* Forms */
.field { margin-bottom: 10px; }
.field label { display: block; font-size: 12px; font-weight: 600; margin-bottom: 4px; color: #334155; }
.field input, .field select, .field textarea {
  width: 100%; min-height: var(--control-h); padding: 6px 9px; border: 1px solid var(--border);
  border-radius: 7px; font-size: 13px; font-family: inherit; background: #fff;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: 2px solid #c7d7fe; border-color: var(--primary); }
.field .hint { color: var(--muted); font-size: 12px; margin-top: 4px; }
.row { display: flex; gap: 10px; flex-wrap: wrap; align-items: flex-end; }
.row > * { flex: 0 1 260px; min-width: 140px; }
.check { display: flex; align-items: center; gap: 8px; font-size: 14px; }
.check input { width: auto; }

/* Tables -> cards on mobile */
.table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 10px; overflow: hidden; border: 1px solid var(--border); }
.table th, .table td { text-align: left; padding: 8px 10px; border-bottom: 1px solid var(--border); font-size: 13px; }
.table th { background: #f8fafc; font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .025em; color: #64748b; }
.table tr:last-child td { border-bottom: none; }
.table a:not(.btn):not(.icon-btn),
.ledger-scope a:not(.btn):not(.icon-btn),
.customer-work-table a:not(.btn):not(.icon-btn),
.customer-demand-table a:not(.btn):not(.icon-btn),
.demand-sched a:not(.btn):not(.icon-btn),
.link-like {
  color: var(--link-strong);
  font-weight: 700;
  text-decoration: none;
}
.table a:not(.btn):not(.icon-btn):hover,
.table a:not(.btn):not(.icon-btn):focus,
.ledger-scope a:not(.btn):not(.icon-btn):hover,
.ledger-scope a:not(.btn):not(.icon-btn):focus,
.customer-work-table a:not(.btn):not(.icon-btn):hover,
.customer-work-table a:not(.btn):not(.icon-btn):focus,
.customer-demand-table a:not(.btn):not(.icon-btn):hover,
.customer-demand-table a:not(.btn):not(.icon-btn):focus,
.demand-sched a:not(.btn):not(.icon-btn):hover,
.demand-sched a:not(.btn):not(.icon-btn):focus,
.link-like:hover,
.link-like:focus {
  color: #092f73;
  text-decoration: underline;
  text-decoration-thickness: 1.5px;
  text-underline-offset: 2px;
}

/* ---------- Wide worklist defaults ----------
   Desktop screens use the full horizontal workspace. Tables stay readable by
   keeping rows single-line, truncating long narration/remarks, and letting the
   surrounding card provide horizontal scroll when needed. */
@media (min-width: 861px) {
  .main { overflow-x: hidden; }
  .content { max-width: none; width: 100%; }
  .card { max-width: 100%; }
  .card:has(> .table),
  .card:has(> form + .table),
  .card:has(> h2 + .table),
  .card:has(> .sub + .table),
  .ledger-scope,
  .tabwrap section > .card {
    overflow-x: auto;
  }
  .table {
    min-width: 100%;
    table-layout: fixed;
  }
  .table th,
  .table td {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 220px;
    vertical-align: middle;
    padding: 7px 8px;
  }
  .table th {
    position: sticky;
    top: 0;
    z-index: 2;
    box-shadow: 0 1px 0 var(--border);
  }
  .table td[data-label*="Narration"],
  .table td[data-label*="Remarks"],
  .table td[data-label*="Particulars"],
  .table .vch-remarks,
  .table .ledger-narration,
  .table td:nth-child(n) .sub {
    white-space: nowrap !important;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0 !important;
  }
  .table td[data-label*="Narration"],
  .table td[data-label*="Remarks"],
  .table td[data-label*="Particulars"],
  .table .vch-remarks,
  .table .ledger-narration {
    width: 280px !important;
    max-width: 280px !important;
  }
  .vch-note p,
  .vch-table .vl-acc {
    white-space: nowrap !important;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .smart-table-toolbar,
  .smart-table-footer {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
    padding: 7px 0;
    min-width: 100%;
  }
  .smart-table-toolbar {
    position: relative;
  }
  .smart-table-search {
    width: min(360px, 34vw);
    min-height: var(--control-h);
    padding: 6px 9px;
    border: 1px solid var(--border);
    border-radius: 8px;
    font: inherit;
  }
  .smart-table-tools,
  .smart-table-pages {
    display: flex;
    align-items: center;
    gap: 7px;
    flex-wrap: wrap;
  }
  .smart-column-picker {
    position: absolute;
    right: 0;
    top: calc(100% - 2px);
    z-index: 18;
    width: min(560px, 80vw);
    max-height: 360px;
    overflow: auto;
    padding: 10px;
    background: #fff;
    border: 1px solid var(--border);
    border-radius: 10px;
    box-shadow: 0 16px 40px rgba(15,23,42,.18);
  }
  .smart-column-picker[hidden] { display: none; }
  .smart-column-picker label {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    width: calc(50% - 6px);
    margin: 3px;
    padding: 6px 8px;
    border: 1px solid var(--border);
    border-radius: 7px;
    background: #f8fafc;
    font-size: 12px;
    font-weight: 600;
  }
  .smart-table-sortable {
    cursor: pointer;
    user-select: none;
  }
  .smart-table-sortable.active {
    color: var(--primary);
    background: #eef3ff;
  }
  .smart-hidden-column {
    display: none !important;
  }
  .rs-modal-box {
    width: min(94vw, 1280px);
    max-width: 1280px;
  }
  .rs-modal-box .table td {
    max-width: 240px;
  }
}

.badge { display: inline-flex; align-items: center; gap: 6px; padding: 3px 9px; border-radius: 20px; font-size: 12px; font-weight: 600; }
.dot { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.pill { padding: 2px 9px; border-radius: 20px; font-size: 12px; background: var(--bg); color: var(--muted); }
.pill-muted { background: #f1f5f9; color: #94a3b8; }
.kv { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 9px 0; border-bottom: 1px solid var(--border); }
.kv:last-child { border-bottom: none; }
.callout, .lookup-box {
  border: 1px solid #bfdbfe;
  background: #eff6ff;
  color: #1e3a8a;
  border-radius: 8px;
  padding: 10px 12px;
  margin: 10px 0 14px;
}
.callout ul { margin: 6px 0 0 18px; padding: 0; }

.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--border); margin-bottom: 18px; overflow-x: auto; }
.tab-link { padding: 9px 14px; font-weight: 600; color: var(--muted); border-bottom: 2px solid transparent; white-space: nowrap; }
.tab-link.active { color: var(--primary); border-bottom-color: var(--primary); }

.empty { text-align: center; color: var(--muted); padding: 40px 20px; }
.hidden { display: none !important; }
.inline-edit { display: flex; gap: 6px; align-items: center; margin-top: 8px; flex-wrap: wrap; }
.inline-edit input, .inline-edit select { padding: 5px 8px; font-size: 13px; }

/* ---------- Compact professional density ----------
   Keeps filters and table controls from stretching into oversized blocks. */
input[type="search"],
input[type="text"],
input[type="number"],
input[type="date"],
input[type="password"],
input[type="email"],
input[type="tel"],
select,
textarea {
  line-height: 1.25;
}

.card > form.row:only-child,
.card > form.row:first-child:last-child {
  margin: 0;
}

.card > form.row .field {
  max-width: 320px;
}

.card > form.row .field:only-child {
  max-width: 420px;
}

.customer-work-toolbar,
.customer-demand-toolbar,
.demand-toolbar {
  padding: 10px 12px !important;
  border-radius: 10px !important;
  gap: 12px !important;
  align-items: end !important;
}

.customer-work-toolbar,
.customer-demand-toolbar {
  grid-template-columns: max-content minmax(260px, 420px) !important;
}

.customer-smart-search input,
.customer-demand-search input,
.demand-search-row input,
.smart-table-search {
  min-height: var(--control-h) !important;
  padding: 6px 9px !important;
  border-radius: 7px !important;
  font-size: 13px !important;
}

.customer-smart-search label,
.customer-demand-search label,
.demand-search-wrap label {
  font-size: 12px !important;
  font-weight: 600 !important;
  margin-bottom: 4px !important;
}

.customer-smart-search > div,
.customer-demand-search > div,
.demand-search-row {
  gap: 6px !important;
}

.customer-column-picker,
.customer-demand-columns,
.column-picker,
.smart-column-picker {
  border-radius: 10px !important;
  padding: 10px !important;
}

.customer-sort-head,
.cd-sort,
.sort-head {
  min-height: 30px !important;
  padding: 7px 7px !important;
  background: #f8fafc !important;
  color: #64748b !important;
  font-size: 10.5px !important;
  font-weight: 650 !important;
  letter-spacing: .02em !important;
}

.customer-sort-head:hover,
.cd-sort:hover,
.sort-head:hover {
  background: #eef2f7 !important;
  color: #334155 !important;
}

.customer-sort-head.active,
.cd-sort.active,
.sort-head.active {
  background: #e8f0ff !important;
  color: var(--primary) !important;
}

.customer-work-table td,
.customer-demand-table td,
.demand-sched td {
  padding: 6px 8px !important;
  font-size: 12.5px !important;
}

.customer-work-footer,
.demand-pagination {
  padding: 7px 10px !important;
  font-size: 12px !important;
}

/* ---------- Mobile ---------- */
@media (max-width: 860px) {
  .sidebar { display: none; }
  .main { margin-left: 0; padding-bottom: 70px; }
  .content { padding: 16px; }
  .topbar { padding: 13px 16px; }
  .flash { margin: 12px 16px 0; }
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr 1fr; }

  .bottomnav {
    display: flex; position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
    background: var(--surface); border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom));
  }
  .tab { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 5px; color: var(--muted); font-size: 11px; font-weight: 600; }
  .tab svg { width: 22px; height: 22px; }
  .tab.active { color: var(--primary); }

  /* Stacked table cards */
  .table, .table tbody, .table tr, .table td { display: block; width: 100%; }
  .table thead { display: none; }
  .table tr { border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 10px; padding: 6px 4px; background: var(--surface); }
  .table td { border: none; padding: 7px 12px; display: flex; justify-content: space-between; gap: 12px; }
  .table td::before { content: attr(data-label); font-weight: 600; color: var(--muted); font-size: 12px; }
  .table td:first-child { font-weight: 700; }
}

@media (max-width: 520px) {
  .grid.cols-2, .grid.cols-3, .grid.cols-4 { grid-template-columns: 1fr; }
}

/* ---------- Dashboard alert panels ---------- */
.alert-card { border-left: 4px solid #f59e0b; }
.alert-card.alert-urgent { border-left-color: #dc2626; }
.alert-card h2 { font-size: 15px; }
.alert-pill { display: inline-block; padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 700; }
.alert-pill.overdue { background: #fee2e2; color: #991b1b; }
.alert-pill.urgent  { background: #fef3c7; color: #92400e; }
.alert-pill.warn    { background: #fef9c3; color: #854d0e; }
.alert-pill.ok      { background: #dcfce7; color: #166534; }

/* ---------- Dashboard project cards ---------- */
.dash-project-card { margin-bottom: 16px; }
.dash-proj-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.dash-proj-name { margin: 0; font-size: 17px; font-weight: 700; }

.dash-stats-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr) repeat(3, 1.4fr);
  gap: 10px;
  margin-bottom: 14px;
}
.dash-stat { background: var(--bg); border: 1px solid var(--border); border-radius: 9px; padding: 10px 12px; }
.dash-stat-n { font-size: 22px; font-weight: 700; }
.dash-stat-n.fin { font-size: 15px; }
.dash-stat-n.green { color: #16a34a; }
.dash-stat-n.red { color: #dc2626; }
.dash-stat-l { color: var(--muted); font-size: 11px; margin-top: 2px; }

.avail-col .dash-stat-n { color: #16a34a; }
.hold-col  .dash-stat-n { color: #d97706; }
.book-col  .dash-stat-n { color: #2563eb; }
.total-col .dash-stat-n { color: var(--text); }

/* Progress bars */
.dash-bar { display: flex; height: 8px; border-radius: 4px; overflow: hidden; background: var(--border); margin-bottom: 4px; }
.dash-bar-seg { height: 100%; transition: width .3s; }
.dash-bar-seg.avail { background: #16a34a; }
.dash-bar-seg.hold  { background: #f59e0b; }
.dash-bar-seg.book  { background: #2563eb; }
.dash-bar-labels { display: flex; gap: 14px; font-size: 11px; color: var(--muted); }
.avail-dot { color: #16a34a; }
.hold-dot  { color: #f59e0b; }
.book-dot  { color: #2563eb; }

@media (max-width: 860px) {
  .dash-stats-grid { grid-template-columns: repeat(4, 1fr); }
  .fin-col { grid-column: span 1; }
}
@media (max-width: 520px) {
  .dash-stats-grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Login ---------- */
.login-wrap { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login-card { width: 100%; max-width: 380px; background: var(--surface); border: 1px solid var(--border); border-radius: 16px; box-shadow: var(--shadow); padding: 28px; }
.login-card .brand { justify-content: center; font-size: 22px; padding-bottom: 8px; }
.login-card .sub { text-align: center; }
.login-error { background: #fef3f2; color: #b42318; padding: 9px 12px; border-radius: 9px; font-size: 13px; margin-bottom: 12px; }
