:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --border: #e3e6eb;
  --border-strong: #d3d8e0;
  --text: #1a2233;
  --muted: #6b7280;
  --primary: #2f6fed;
  --primary-dark: #1f4fc4;
  --primary-light: #eef3ff;
  --green: #1a9e5c;
  --green-bg: #e2f7ec;
  --amber: #b3781a;
  --amber-bg: #fff3dd;
  --red: #c0392b;
  --red-bg: #fbe4e1;
  --radius: 10px;
  --radius-sm: 6px;
  --shadow-card: 0 1px 2px rgba(16, 24, 40, 0.04);
  --shadow-modal: 0 20px 50px rgba(16, 24, 40, 0.25);
}
* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}
a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }
.muted { color: var(--muted); }
h1, h2, h3 { font-weight: 600; letter-spacing: -0.01em; }

/* Auth pages */
.auth-page { display: flex; align-items: center; justify-content: center; height: 100vh; background: linear-gradient(180deg, #f7f8fa, var(--bg)); }
.auth-card { background: var(--card); padding: 40px; border-radius: 14px; box-shadow: 0 8px 30px rgba(16,24,40,0.1); width: 360px; border: 1px solid var(--border); }
.auth-card h1 { margin-top: 0; font-size: 22px; }
.auth-card label { display: block; margin-bottom: 14px; font-size: 13px; color: var(--muted); }
.auth-card input { width: 100%; padding: 10px 12px; margin-top: 4px; border: 1px solid var(--border); border-radius: var(--radius-sm); font-size: 14px; transition: border-color .12s, box-shadow .12s; }
.auth-card input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.auth-card button { width: 100%; padding: 11px; background: var(--primary); color: #fff; border: none; border-radius: var(--radius-sm); font-size: 14px; font-weight: 600; cursor: pointer; transition: background .12s; }
.auth-card button:hover { background: var(--primary-dark); }
.error { color: var(--red); font-size: 13px; margin-top: 10px; }

/* App shell */
.app-shell { display: flex; height: 100vh; }
.sidebar { width: 226px; background: #141b29; color: #b7c2d4; flex-shrink: 0; display: flex; flex-direction: column; padding: 18px 0; }
.sidebar .brand { padding: 0 20px 20px; font-weight: 700; font-size: 16px; color: #fff; letter-spacing: -0.01em; }
.sidebar nav a { display: flex; align-items: center; gap: 8px; padding: 9px 20px; color: #b7c2d4; font-size: 13.5px; cursor: pointer; border-left: 3px solid transparent; transition: background .1s, color .1s; }
.sidebar nav a:hover { background: #1c2637; color: #fff; }
.sidebar nav a.active { background: #1c2637; color: #fff; border-left-color: var(--primary); font-weight: 500; }
.sidebar .spacer { flex: 1; }
.sidebar .user-box { padding: 14px 20px; font-size: 12px; border-top: 1px solid #24304433; }
.sidebar .user-box #userName { color: #e5e9f0; font-weight: 600; font-size: 13px; }
.sidebar .user-box button { background: none; border: none; color: #8a97ac; cursor: pointer; font-size: 12px; padding: 4px 0; }
.sidebar .user-box button:hover { color: #fff; }

.main { flex: 1; overflow-y: auto; padding: 30px 38px 60px; }
.main h1 { margin: 0 0 4px; font-size: 21px; }
.page-header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 22px; gap: 12px; flex-wrap: wrap; }
.page-header > div { display: flex; gap: 8px; flex-wrap: wrap; }

.btn { display: inline-flex; align-items: center; gap: 6px; padding: 9px 16px; background: var(--primary); color: #fff; border: none; border-radius: var(--radius-sm); font-size: 13.5px; font-weight: 500; cursor: pointer; transition: background .12s, box-shadow .12s; }
.btn:hover { background: var(--primary-dark); }
.btn:active { transform: translateY(1px); }
.btn.secondary { background: #fff; color: var(--text); border: 1px solid var(--border-strong); }
.btn.secondary:hover { background: #f6f7f9; border-color: #c3cad6; }
.btn.danger { background: var(--red); }
.btn.danger:hover { background: #a33325; }
.btn.small { padding: 6px 11px; font-size: 12px; }
.btn:disabled { opacity: 0.55; cursor: not-allowed; }

.cards-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 14px; margin-bottom: 24px; }
.stat-card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; box-shadow: var(--shadow-card); }
.stat-card .label { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.stat-card .value { font-size: 25px; font-weight: 700; margin-top: 8px; letter-spacing: -0.01em; }
.stat-card .value.green { color: var(--green); }
.stat-card .value.red { color: var(--red); }
.stat-card .value.amber { color: var(--amber); }

table { width: 100%; border-collapse: collapse; background: var(--card); border-radius: var(--radius); overflow: hidden; box-shadow: var(--shadow-card); border: 1px solid var(--border); }
table th, table td { text-align: left; padding: 11px 14px; border-bottom: 1px solid var(--border); font-size: 13.5px; }
table th { background: #fafbfc; color: var(--muted); font-weight: 600; text-transform: uppercase; font-size: 10.5px; letter-spacing: 0.04em; }
table tr:last-child td { border-bottom: none; }
table tr:hover td { background: #fafbfd; }
table tr.clickable { cursor: pointer; }

.badge { display: inline-block; padding: 3px 9px; border-radius: 12px; font-size: 11px; font-weight: 600; letter-spacing: 0.01em; }
.badge.draft { background: #eef0f3; color: #555; }
.badge.sent { background: var(--primary-light); color: #1a5cc4; }
.badge.paid { background: var(--green-bg); color: var(--green); }
.badge.partially_paid { background: var(--amber-bg); color: var(--amber); }
.badge.overdue { background: var(--red-bg); color: var(--red); }
.badge.void { background: #eee; color: #999; }
.badge.matched { background: var(--green-bg); color: var(--green); }
.badge.unmatched { background: var(--red-bg); color: var(--red); }
.badge.partially_matched { background: var(--amber-bg); color: var(--amber); }
.badge.reviewed { background: var(--primary-light); color: #1a5cc4; }
.badge.ignored { background: #eee; color: #999; }

.card { background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); padding: 22px; margin-bottom: 20px; box-shadow: var(--shadow-card); }
.card h2 { margin-top: 0; font-size: 16px; }
.card h3 { font-size: 13.5px; }

.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px 16px; }
.form-grid.wide { grid-template-columns: 1fr; }
.form-grid label { font-size: 12.5px; color: var(--muted); font-weight: 500; display: block; }
.form-grid input, .form-grid select, .form-grid textarea,
form input[type], form select, form textarea {
  width: 100%; padding: 9px 10px; margin-top: 5px; border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); font-size: 13.5px; font-family: inherit; color: var(--text);
  background: #fff; transition: border-color .12s, box-shadow .12s;
}
.form-grid input:focus, .form-grid select:focus, .form-grid textarea:focus,
form input:focus, form select:focus, form textarea:focus {
  outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light);
}
.section-label { font-size: 12px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; margin: 20px 0 10px; }

/* ── Modals ── */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(16, 22, 34, 0.55); backdrop-filter: blur(1px);
  display: flex; align-items: flex-start; justify-content: center; z-index: 50;
  padding: 5vh 16px; overflow-y: auto; animation: overlayIn .12s ease-out;
}
.modal-overlay ~ .modal-overlay { background: rgba(16, 22, 34, 0.35); }
@keyframes overlayIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes modalIn { from { opacity: 0; transform: translateY(8px) scale(0.98); } to { opacity: 1; transform: translateY(0) scale(1); } }
.modal {
  background: #fff; border-radius: 14px; width: 580px; max-width: 100%;
  max-height: 90vh; display: flex; flex-direction: column;
  box-shadow: var(--shadow-modal); animation: modalIn .16s ease-out;
}
.modal-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 18px 24px; border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.modal-header h2 { margin: 0; font-size: 17px; }
.modal .close-x { cursor: pointer; color: var(--muted); font-size: 16px; line-height: 1; padding: 6px; border-radius: 6px; }
.modal .close-x:hover { background: #f0f1f3; color: var(--text); }
.modal-body { padding: 22px 24px; overflow-y: auto; flex: 1; }
.modal-footer {
  display: flex; justify-content: flex-end; gap: 10px;
  padding: 16px 24px; border-top: 1px solid var(--border); flex-shrink: 0; background: #fafbfc; border-radius: 0 0 14px 14px;
}
/* Fallback for modals not yet split into header/body/footer sections */
.modal > form > p, .modal > form > .form-grid, .modal > form > input[type=file] { margin: 0 24px 16px; }
.modal > form > button, .modal > form > div > button[type=submit] { margin: 0 24px 20px; }

.line-items-table { width: 100%; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; box-shadow: none; }
.line-items-table th { font-size: 10px; padding: 8px 10px; }
.line-items-table td { padding: 6px 8px; border-bottom: 1px solid var(--border); }
.line-items-table tr:last-child td { border-bottom: none; }
.line-items-table input { padding: 6px 8px; font-size: 12.5px; margin-top: 0; }
.line-items-table span { color: var(--muted); }
.line-items-table span:hover { color: var(--red); }

.toast { position: fixed; bottom: 22px; right: 22px; background: var(--text); color: #fff; padding: 12px 18px; border-radius: var(--radius-sm); font-size: 13px; z-index: 100; box-shadow: 0 8px 24px rgba(0,0,0,0.2); animation: overlayIn .12s ease-out; }
.toast.error { background: var(--red); }
.empty-state { padding: 48px; text-align: center; color: var(--muted); background: var(--card); border: 1px solid var(--border); border-radius: var(--radius); }
.tabs { display: flex; gap: 4px; margin-bottom: 18px; border-bottom: 1px solid var(--border); }
.tabs button { background: none; border: none; padding: 9px 14px; cursor: pointer; font-size: 13px; color: var(--muted); border-bottom: 2px solid transparent; font-family: inherit; }
.tabs button:hover { color: var(--text); }
.tabs button.active { color: var(--primary); border-bottom-color: var(--primary); font-weight: 500; }
