:root {
  --bg: #f4f6f8;
  --card: #ffffff;
  --ink: #1c2430;
  --muted: #5c6b7a;
  --line: #e4e7ec;
  --teal: #0f766e;
  --teal-dark: #0b5f59;
  --teal-soft: #e7f4f2;
  --navy: #0b1f3a;
  --danger: #b42318;
  --danger-soft: #fef3f2;
  --warn: #b54708;
  --warn-soft: #fffaeb;
  --ok: #067647;
  --ok-soft: #ecfdf3;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06);
  --radius: 12px;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; }
body { background: var(--bg); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
a { color: var(--teal); }

.shell { display: grid; grid-template-columns: 240px 1fr; min-height: 100vh; }
.sidebar {
  background: var(--card);
  border-right: 1px solid var(--line);
  padding: 22px 16px;
  position: sticky;
  top: 0;
  height: 100vh;
  display: flex;
  flex-direction: column;
}
.brand { display: flex; gap: 12px; align-items: center; padding: 0 8px 18px; text-decoration: none; color: inherit; }
.brand img { width: 40px; height: 40px; border-radius: 10px; }
.brand strong { display: block; font-size: 17px; letter-spacing: -0.02em; color: var(--ink); }
.brand span { display: block; color: var(--muted); font-size: 12px; margin-top: 1px; }
.nav { display: flex; flex-direction: column; gap: 4px; }
.nav a {
  text-decoration: none;
  color: var(--ink);
  padding: 10px 12px;
  border-radius: 8px;
  font-weight: 600;
  font-size: 14.5px;
}
.nav a:hover { background: var(--bg); }
.nav a.active { background: var(--teal-soft); color: var(--teal-dark); }
.sidebar .signout { margin-top: auto; width: 100%; }
.main { padding: 28px 32px 48px; }
.main-inner { max-width: 1080px; }
.tabbar { display: none; }

.page-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; margin-bottom: 18px; }
.page-head h1 { margin: 0; font-size: 26px; letter-spacing: -0.03em; font-weight: 650; }
.page-head p, .lead { margin: 6px 0 0; color: var(--muted); }
.actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn, button.btn, a.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 40px;
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: var(--card);
  text-decoration: none;
  color: var(--ink);
  font-weight: 650;
  font-size: 14px;
}
.btn.primary { background: var(--teal); border-color: var(--teal); color: white; }
.btn.primary:hover { background: var(--teal-dark); }
.btn.danger { color: var(--danger); border-color: #f3c7c3; background: var(--danger-soft); }
.btn.small { min-height: 34px; padding: 6px 10px; font-size: 13px; }
.btn.full { width: 100%; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
}
.stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; margin-bottom: 16px; }
.stat .label { color: var(--muted); font-size: 13px; font-weight: 650; }
.stat .value { font-size: 26px; letter-spacing: -0.03em; margin-top: 6px; font-weight: 700; }
.stat .sub { color: var(--muted); font-size: 12px; margin-top: 4px; }
.stat.warn .value { color: var(--danger); }
.split { display: grid; grid-template-columns: 1.4fr 0.8fr; gap: 12px; }
.stack { display: flex; flex-direction: column; gap: 12px; }

.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 14px; }
th { text-align: left; font-size: 12px; letter-spacing: 0.04em; text-transform: uppercase; color: var(--muted); font-weight: 700; padding: 8px 10px; border-bottom: 1px solid var(--line); }
td { padding: 12px 10px; border-bottom: 1px solid var(--line); vertical-align: middle; }
tr:last-child td { border-bottom: 0; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
a.rowlink { color: inherit; font-weight: 700; text-decoration: none; }
a.rowlink:hover { color: var(--teal); }

.badge {
  display: inline-flex;
  align-items: center;
  border-radius: 999px;
  padding: 3px 8px;
  font-size: 12px;
  font-weight: 700;
  background: #eef1f4;
  color: #3d4b5c;
  text-transform: capitalize;
}
.badge.sent { background: #eef4ff; color: #1849a9; }
.badge.partial, .badge.unpaid { background: var(--warn-soft); color: var(--warn); }
.badge.paid { background: var(--ok-soft); color: var(--ok); }
.badge.overdue, .badge.void { background: var(--danger-soft); color: var(--danger); }
.badge.credit { background: #f4f3ff; color: #5925dc; }
.badge.draft { background: #f2f4f7; color: #475467; }

.item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 0;
  border-bottom: 1px solid var(--line);
  text-decoration: none;
  color: inherit;
}
.item:last-child { border-bottom: 0; }
.item strong { display: block; }
.item span { color: var(--muted); font-size: 13px; }
.cards-only { display: none; }

label { display: block; font-size: 13px; font-weight: 700; margin-bottom: 6px; }
.field { margin-bottom: 14px; }
input, select, textarea {
  width: 100%;
  border: 1px solid #d0d5dd;
  background: white;
  border-radius: 8px;
  padding: 10px 12px;
  min-height: 44px;
  font-size: 16px;
}
textarea { min-height: 88px; resize: vertical; }
input:focus, select:focus, textarea:focus { outline: 2px solid var(--teal); border-color: var(--teal); }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid-3 { display: grid; grid-template-columns: 1.6fr 0.5fr 0.7fr auto; gap: 8px; align-items: end; }
.help { color: var(--muted); font-size: 13px; margin-top: 4px; }
.error {
  background: var(--danger-soft);
  color: var(--danger);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 12px;
  font-weight: 650;
}
.notice {
  background: var(--teal-soft);
  color: var(--teal-dark);
  border-radius: 8px;
  padding: 10px 12px;
  margin-bottom: 12px;
}
.chips { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.line { margin-bottom: 8px; }
.line-amt { font-variant-numeric: tabular-nums; font-weight: 700; padding-bottom: 10px; }
.totals { margin-left: auto; width: min(280px, 100%); }
.totals div { display: flex; justify-content: space-between; padding: 6px 0; }
.totals .grand { font-size: 18px; font-weight: 750; border-top: 1px solid var(--line); margin-top: 4px; padding-top: 10px; }

.auth {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px 16px;
}
.auth-card { width: min(420px, 100%); }
.auth-brand { text-align: center; margin-bottom: 18px; }
.auth-brand img { width: 72px; height: 72px; border-radius: 16px; }
.auth-brand h1 { margin: 10px 0 0; font-size: 28px; letter-spacing: -0.03em; }
.auth-brand p { margin: 6px 0 0; }

.sheet {
  background: white;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 28px;
  position: relative;
}
.sheet-top { display: flex; justify-content: space-between; gap: 16px; }
.sheet h2 { margin: 8px 0 0; font-size: 22px; }
.muted { color: var(--muted); }
.billto { margin: 22px 0; }
.sheet.draft { overflow: hidden; }
.sheet.draft::before {
  content: "DRAFT";
  position: absolute;
  left: 50%;
  top: 42%;
  transform: translate(-50%, -50%) rotate(-18deg);
  font-size: 84px;
  font-weight: 800;
  color: rgba(180, 35, 24, 0.08);
  letter-spacing: 0.12em;
  pointer-events: none;
}
.filterbar { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filterbar input { max-width: 280px; }
.more-list a {
  display: flex;
  justify-content: space-between;
  padding: 16px 4px;
  border-bottom: 1px solid var(--line);
  text-decoration: none;
  color: inherit;
  font-weight: 700;
  min-height: 52px;
  align-items: center;
}

@media (max-width: 860px) {
  .shell { display: block; }
  .sidebar { display: none; }
  .main { padding: 16px 16px calc(88px + env(safe-area-inset-bottom)); }
  .tabbar {
    display: flex;
    position: fixed;
    left: 0; right: 0; bottom: 0;
    background: white;
    border-top: 1px solid var(--line);
    padding: 4px 6px calc(6px + env(safe-area-inset-bottom));
    z-index: 30;
  }
  .tabbar a {
    flex: 1;
    text-align: center;
    text-decoration: none;
    color: var(--muted);
    font-size: 12px;
    font-weight: 750;
    padding: 10px 0;
    border-radius: 8px;
  }
  .tabbar a.active { color: var(--teal-dark); background: var(--teal-soft); }
  .stats, .split, .grid-2 { grid-template-columns: 1fr 1fr; }
  .split { grid-template-columns: 1fr; }
  .grid-3 { grid-template-columns: 1fr 1fr; }
  .page-head { flex-direction: column; }
  .page-head h1 { font-size: 22px; }
  .table-only { display: none; }
  .cards-only { display: block; }
  .sheet { padding: 16px; }
  .stat .value { font-size: 22px; }
}

@media print {
  .sidebar, .tabbar, .noprint, .page-head .actions { display: none !important; }
  .shell, .main, .main-inner { display: block; padding: 0; max-width: none; }
  body { background: white; }
  .sheet { border: 0; box-shadow: none; }
  .sheet.draft::before { color: rgba(180, 35, 24, 0.16); }
}
