:root {
  --bg: #ECEDF0;
  --card: #FFFFFF;
  --ink: #16171B;
  --ink-soft: #6C6F76;
  --line: #E7E8EC;
  --accent: #E8542E;
  --accent-ink: #FFFFFF;
  --dark: #17181C;
  --success: #1E9E63;
  --success-bg: #E7F7EF;
  --danger: #D3453B;
  --danger-bg: #FCEAE9;
  --warning: #B8760A;
  --warning-bg: #FBF1DF;
  --info: #2F6FE0;
  --info-bg: #E9F0FD;
  --muted-bg: #F1F1F3;
  --radius-lg: 20px;
  --radius-md: 14px;
  --radius-sm: 10px;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  -webkit-font-smoothing: antialiased;
}

.app {
  display: grid;
  grid-template-columns: 76px 1fr;
  min-height: 100vh;
  transition: grid-template-columns 0.2s ease;
}
.app.sidebar-expanded { grid-template-columns: 208px 1fr; }

/* ---------- Sidebar ---------- */
.sidebar {
  background: var(--card);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 22px 0;
  gap: 6px;
  overflow: hidden;
}
.app.sidebar-expanded .sidebar { align-items: stretch; padding: 22px 14px; }

.sidebar-brand {
  display: flex; align-items: center; justify-content: center; gap: 10px;
  margin-bottom: 24px; width: 100%;
}
.app.sidebar-expanded .sidebar-brand { justify-content: flex-start; }

.sidebar .logo {
  width: 42px; height: 42px; flex-shrink: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  display: flex; align-items: center; justify-content: center;
  color: white; font-weight: 700; font-size: 17px;
  text-decoration: none;
  box-shadow: 0 4px 10px rgba(232, 84, 46, 0.28);
  transition: transform 0.15s ease;
  overflow: hidden; padding: 4px;
}
.sidebar .logo:hover { transform: translateY(-1px); }
.sidebar .logo img { width: 100%; height: 100%; object-fit: contain; border-radius: inherit; }

.sidebar-brand-name {
  display: none; font-weight: 700; font-size: 14px; color: var(--ink);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.app.sidebar-expanded .sidebar-brand-name { display: block; }

.sidebar nav { display: flex; flex-direction: column; gap: 4px; width: 100%; align-items: center; }
.app.sidebar-expanded .sidebar nav { align-items: stretch; }

.sidebar a, .sidebar-toggle {
  width: 44px; height: 44px;
  display: flex; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  color: var(--ink-soft);
  text-decoration: none;
  transition: background 0.15s ease, color 0.15s ease;
  border: none; background: none; cursor: pointer; font-family: inherit;
  flex-shrink: 0;
}
.sidebar a svg, .sidebar-toggle svg { width: 20px; height: 20px; flex-shrink: 0; }
.sidebar a.active { background: var(--dark); color: white; box-shadow: 0 4px 10px rgba(23,24,28,0.18); }
.sidebar a:hover:not(.active), .sidebar-toggle:hover { background: var(--muted-bg); color: var(--ink); }

.nav-label { display: none; white-space: nowrap; font-size: 14px; font-weight: 500; }
.app.sidebar-expanded .sidebar a,
.app.sidebar-expanded .sidebar-toggle {
  width: 100%; justify-content: flex-start; padding: 0 12px; gap: 12px;
}
.app.sidebar-expanded .nav-label { display: inline; }

.sidebar-toggle svg { transition: transform 0.2s ease; }
.app.sidebar-expanded .sidebar-toggle svg { transform: rotate(180deg); }

.sidebar .bottom { margin-top: auto; display: flex; flex-direction: column; gap: 4px; align-items: center; width: 100%; }
.app.sidebar-expanded .sidebar .bottom { align-items: stretch; }
.sidebar-divider { width: 28px; height: 1px; background: var(--line); margin: 8px 0; }
.sidebar .logout-link:hover { background: var(--danger-bg); color: var(--danger); }

/* ---------- Main ---------- */
.main { padding: 28px 36px 60px; max-width: 1400px; }

.topbar {
  display: flex; align-items: center; justify-content: flex-end;
  margin-bottom: 28px;
}
.topbar-user { display: flex; align-items: center; gap: 14px; margin-left: auto; }
.mobile-topbar-logo {
  display: none;
  width: 32px; height: 32px; border-radius: var(--radius-sm);
  background: var(--accent); color: white; font-weight: 700; font-size: 14px;
  align-items: center; justify-content: center; text-decoration: none;
  overflow: hidden;
}
.mobile-topbar-logo img { width: 100%; height: 100%; object-fit: contain; }
.bell-link {
  position: relative; display: flex; align-items: center; justify-content: center;
  width: 36px; height: 36px; border-radius: var(--radius-sm);
  color: var(--ink-soft); text-decoration: none; transition: background 0.15s ease;
}
.bell-link svg { width: 19px; height: 19px; }
.bell-link:hover { background: var(--muted-bg); color: var(--ink); }
.bell-badge {
  position: absolute; top: 2px; right: 2px;
  background: var(--danger); color: white;
  font-size: 10px; font-weight: 700; line-height: 1;
  min-width: 15px; height: 15px; border-radius: 999px;
  display: flex; align-items: center; justify-content: center;
  padding: 0 3px; border: 2px solid var(--card);
}
.avatar {
  width: 38px; height: 38px; border-radius: 50%;
  background: var(--dark); color: white;
  display: flex; align-items: center; justify-content: center;
  font-size: 14px; font-weight: 600;
}
.user-meta { line-height: 1.3; }
.user-meta .name { font-size: 14px; font-weight: 600; }
.user-meta .role { font-size: 12px; color: var(--ink-soft); text-transform: capitalize; }

.greeting { margin-bottom: 24px; }
.greeting h1 { font-size: 28px; margin: 0 0 6px; font-weight: 700; }
.greeting p { margin: 0; color: var(--ink-soft); font-size: 14px; }

/* ---------- Grid ---------- */
.grid-top {
  display: grid;
  grid-template-columns: 1.3fr 1fr 1.1fr;
  gap: 20px;
  margin-bottom: 20px;
}

.card {
  background: var(--card);
  border-radius: var(--radius-lg);
  padding: 22px;
  border: 1px solid var(--line);
}

.card-label { font-size: 13px; color: var(--ink-soft); margin-bottom: 10px; display: flex; align-items: center; justify-content: space-between; }
.card-value { font-size: 30px; font-weight: 700; letter-spacing: -0.02em; }
.card-change { font-size: 12px; margin-top: 8px; display: inline-flex; align-items: center; gap: 4px; }
.card-change.up { color: var(--success); }
.card-change.down { color: var(--danger); }

/* Hero balance card */
.hero-card { display: flex; flex-direction: column; gap: 18px; }
.hero-actions { display: flex; gap: 10px; }
.btn {
  border: none; border-radius: 999px; padding: 10px 18px;
  font-size: 13px; font-weight: 600; cursor: pointer;
}
.btn-dark { background: var(--dark); color: white; }
.btn-outline { background: var(--muted-bg); color: var(--ink); }

/* KPI stack (Earnings/Spending style) */
.kpi-stack { display: grid; grid-template-rows: 1fr 1fr; gap: 14px; }
.kpi-card { border-radius: var(--radius-lg); padding: 18px 20px; }
.kpi-card.accent { background: var(--accent); color: var(--accent-ink); }
.kpi-card.accent .card-label { color: rgba(255,255,255,0.85); }
.kpi-card.accent .card-change.up { color: #FFE8D6; }
.kpi-card.neutral { background: var(--card); border: 1px solid var(--line); }

/* Chart card */
.chart-card canvas { width: 100% !important; height: 180px !important; }
.chart-legend { display: flex; gap: 16px; font-size: 12px; color: var(--ink-soft); }
.dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 5px; }
.dot.billed { background: var(--dark); }
.dot.collected { background: var(--accent); }

/* ---------- Bottom grid ---------- */
.grid-bottom {
  display: grid;
  grid-template-columns: 0.9fr 2fr;
  gap: 20px;
}

.utilization-item { margin-bottom: 16px; }
.utilization-item:last-child { margin-bottom: 0; }
.utilization-top { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 6px; }
.utilization-top .name { font-weight: 600; }
.utilization-top .fig { color: var(--ink-soft); }
.bar-track { height: 8px; background: var(--muted-bg); border-radius: 99px; overflow: hidden; }
.bar-fill { height: 100%; background: var(--accent); border-radius: 99px; }

/* Table */
.table-card { padding: 0; overflow: hidden; }
.table-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 20px 22px; border-bottom: 1px solid var(--line);
}
.table-head h3 { margin: 0; font-size: 16px; }
table { width: 100%; border-collapse: collapse; }
th {
  text-align: left; font-size: 12px; color: var(--ink-soft);
  font-weight: 500; padding: 12px 22px; border-bottom: 1px solid var(--line);
}
td { padding: 14px 22px; font-size: 13px; border-bottom: 1px solid var(--line); }
tr:last-child td { border-bottom: none; }
.inv-cell { display: flex; flex-direction: column; }
.inv-cell .num { font-weight: 600; }
.inv-cell .sub { color: var(--ink-soft); font-size: 12px; }

.badge {
  display: inline-flex; align-items: center; gap: 5px;
  font-size: 12px; font-weight: 600; padding: 4px 10px; border-radius: 999px;
}
.badge::before { content: ''; width: 6px; height: 6px; border-radius: 50%; }
.badge-success { background: var(--success-bg); color: var(--success); }
.badge-success::before { background: var(--success); }
.badge-info { background: var(--info-bg); color: var(--info); }
.badge-info::before { background: var(--info); }
.badge-warning { background: var(--warning-bg); color: var(--warning); }
.badge-warning::before { background: var(--warning); }
.badge-danger { background: var(--danger-bg); color: var(--danger); }
.badge-danger::before { background: var(--danger); }
.badge-muted { background: var(--muted-bg); color: var(--ink-soft); }
.badge-muted::before { background: var(--ink-soft); }

.empty-state { padding: 40px 22px; text-align: center; color: var(--ink-soft); font-size: 13px; }

/* ---------- Forms (shared across all pages) ---------- */
.field { margin-bottom: 16px; }
.field label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.field input, .field select, .field textarea {
  width: 100%; padding: 10px 12px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); font-size: 14px; font-family: inherit;
  background: white; color: var(--ink);
}
.field input:focus, .field select:focus, .field textarea:focus {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.btn-full { width: 100%; padding: 12px; font-size: 14px; }
.form-error {
  background: var(--danger-bg); color: var(--danger);
  font-size: 13px; padding: 10px 12px; border-radius: var(--radius-sm);
  margin-bottom: 16px;
}
.form-success {
  background: var(--success-bg); color: var(--success);
  font-size: 13px; padding: 10px 12px; border-radius: var(--radius-sm);
  margin-bottom: 16px;
}
.section-title { font-size: 14px; font-weight: 700; margin: 28px 0 14px; }
.section-title:first-child { margin-top: 0; }
.pill-toggle { display: flex; gap: 8px; margin-bottom: 16px; }
.pill-toggle label {
  flex: 1; text-align: center; padding: 10px; border-radius: var(--radius-sm);
  border: 1px solid var(--line); font-size: 13px; font-weight: 600; cursor: pointer;
  color: var(--ink-soft);
}
.pill-toggle input { display: none; }
.pill-toggle input:checked + label,
.pill-toggle label.active { background: var(--dark); color: white; border-color: var(--dark); }
.line-items-table { width: 100%; border-collapse: collapse; margin-bottom: 12px; }
.line-items-table th { text-align: left; font-size: 12px; color: var(--ink-soft); padding: 8px 6px; font-weight: 500; }
.line-items-table td { padding: 6px; vertical-align: top; }
.line-items-table input { width: 100%; padding: 8px; border: 1px solid var(--line); border-radius: 8px; font-size: 13px; }
.totals-box { display: flex; flex-direction: column; align-items: flex-end; gap: 6px; margin-top: 12px; font-size: 13px; }
.totals-box .row { display: flex; justify-content: space-between; width: 260px; }
.totals-box .row.grand { font-weight: 700; font-size: 16px; border-top: 1px solid var(--line); padding-top: 8px; margin-top: 4px; }
.link-btn { background: none; border: none; color: var(--accent); font-size: 13px; font-weight: 600; cursor: pointer; padding: 0; }
.back-link { display: inline-block; margin-bottom: 14px; color: var(--ink-soft); text-decoration: none; font-size: 13px; }
.detail-grid { display: grid; grid-template-columns: 2fr 1fr; gap: 20px; align-items: start; }
.meta-row { display: flex; justify-content: space-between; padding: 10px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.meta-row:last-child { border-bottom: none; }
.meta-row .label { color: var(--ink-soft); }
.meta-row .value { font-weight: 600; }

/* ---------- Mobile bottom nav (hidden on desktop) ---------- */
.mobile-nav {
  display: none;
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 50;
  background: var(--card); border-top: 1px solid var(--line);
  padding: 6px 4px calc(6px + env(safe-area-inset-bottom, 0px));
  justify-content: space-around; align-items: center;
}
.mobile-nav a {
  display: flex; flex-direction: column; align-items: center; gap: 3px;
  color: var(--ink-soft); text-decoration: none; font-size: 10px;
  padding: 4px 6px; border-radius: var(--radius-sm); flex: 1;
}
.mobile-nav a svg { width: 20px; height: 20px; }
.mobile-nav a.active { color: var(--accent); }

/* ---------- Mobile "More" menu rows ---------- */
.menu-row {
  display: block; padding: 16px 18px; text-decoration: none; color: var(--ink);
  font-size: 14px; font-weight: 500; border-bottom: 1px solid var(--line);
}
.menu-row:last-child { border-bottom: none; }

/* ---------- Tables scroll horizontally instead of breaking layout ---------- */
.table-card { overflow-x: auto; -webkit-overflow-scrolling: touch; }
.table-card table { min-width: 560px; }

/* ---------- Responsive breakpoints ---------- */

/* Tablet and below: stack multi-column layouts, hide desktop sidebar */
@media (max-width: 1100px) {
  .grid-top { grid-template-columns: 1fr !important; }
  .grid-bottom { grid-template-columns: 1fr !important; }
  .detail-grid { grid-template-columns: 1fr !important; }
  .app { grid-template-columns: 1fr; }
  .sidebar { display: none; }
}

/* Phone: tighter spacing, single-column forms, bottom nav visible */
@media (max-width: 680px) {
  .main { padding: 16px 14px 84px; } /* extra bottom padding clears the fixed mobile nav */
  .mobile-nav { display: flex; }

  .greeting h1 { font-size: 22px; }
  .card { padding: 16px; border-radius: var(--radius-md); }

  .topbar { margin-bottom: 20px; align-items: center; }
  .topbar-nav { display: none; }
  .mobile-topbar-logo { display: flex; }
  .topbar-user { margin-left: auto; }
  .user-meta { display: none; } /* avatar + bell are enough on phone */

  .field-row { grid-template-columns: 1fr; gap: 0; }
  .hero-actions { flex-direction: column; }
  .hero-actions .btn { width: 100%; }

  .kpi-stack { grid-template-rows: none; grid-template-columns: 1fr 1fr; gap: 10px; }
  .card-value { font-size: 24px; }

  .totals-box .row, .totals-box { width: 100%; }
  .aging-grid { grid-template-columns: repeat(2, 1fr) !important; }

  .line-items-table th:nth-child(1), .line-items-table td:nth-child(1) { min-width: 140px; }
}
