:root {
  --bg: #f6f8f5;
  --panel: #ffffff;
  --text: #18211c;
  --muted: #66736c;
  --line: #dfe7e1;
  --green: #17834f;
  --green-dark: #0f5e39;
  --soft-green: #e7f5ed;
  --danger: #b42318;
  --warning: #a16207;
  --shadow: 0 14px 40px rgba(30, 45, 35, 0.08);
}

* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
}

a { color: var(--green-dark); text-decoration: none; }
.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px max(16px, env(safe-area-inset-left)) 12px;
  background: rgba(246, 248, 245, 0.92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.brand { font-weight: 800; color: var(--text); white-space: nowrap; }
.nav { display: flex; gap: 6px; overflow-x: auto; flex: 1; }
.nav a, .icon-button {
  min-height: 38px;
  padding: 9px 12px;
  border-radius: 8px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--muted);
  font: inherit;
}
.nav a:hover, .icon-button:hover { background: var(--panel); color: var(--text); }
.page { width: min(1180px, 100%); margin: 0 auto; padding: 22px 16px 60px; }
.fade-in { animation: fade 180ms ease both; }
@keyframes fade { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: translateY(0); } }

.hero, .section-head {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin: 12px 0 18px;
}
.hero h1, .section-head h1 { margin: 0; font-size: clamp(30px, 5vw, 54px); line-height: 1.02; letter-spacing: 0; }
.section-head h1 { font-size: clamp(26px, 4vw, 40px); }
.eyebrow { margin: 0 0 8px; color: var(--green-dark); font-weight: 800; text-transform: uppercase; font-size: 12px; letter-spacing: 0; }
.muted { color: var(--muted); }

.dashboard-actions, .metric-grid, .report-grid, .detail-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 12px;
}
.big-action, .metric-grid article, .invoice-card, .form-card, .upload-panel, .preview-panel, .chart-card, .note-panel, .detail-grid article, .auth-panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}
.big-action {
  min-height: 122px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  color: var(--text);
  transition: transform 160ms ease, border-color 160ms ease;
}
.big-action:hover { transform: translateY(-2px); border-color: #a8dabc; }
.big-action span { font-size: 21px; font-weight: 800; }
.big-action small { color: var(--muted); font-size: 14px; }
.big-action.accent { background: var(--soft-green); border-color: #bfe5ce; }

.metric-grid { margin: 18px 0; }
.metric-grid article, .detail-grid article { padding: 16px; }
.metric-grid span, .detail-grid span { display: block; color: var(--muted); font-size: 13px; margin-bottom: 8px; }
.metric-grid strong, .detail-grid strong { font-size: 23px; }

button, .primary, .secondary, .danger {
  min-height: 44px;
  border-radius: 8px;
  border: 1px solid transparent;
  padding: 11px 16px;
  font: inherit;
  font-weight: 800;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: transform 140ms ease, background 140ms ease;
}
.small-button { min-height: 36px; padding: 7px 10px; font-size: 13px; }
button:active, .primary:active, .secondary:active { transform: scale(0.98); }
.primary { background: var(--green); color: #fff; }
.primary:hover { background: var(--green-dark); }
.secondary { background: #fff; color: var(--text); border-color: var(--line); }
.danger { background: #fff1f1; color: var(--danger); border-color: #facaca; }
.action { white-space: nowrap; }

.form-card, .upload-panel { padding: 18px; display: grid; grid-template-columns: repeat(2, 1fr); gap: 14px; }
label { display: grid; gap: 7px; color: var(--muted); font-weight: 700; font-size: 13px; }
input, select, textarea {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--text);
  background: #fff;
  font: inherit;
}
textarea { resize: vertical; }
.wide { grid-column: 1 / -1; }
.button-row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; grid-column: 1 / -1; }

.drop-zone {
  grid-column: 1 / -1;
  min-height: 240px;
  border: 2px dashed #aacfb9;
  background: var(--soft-green);
  border-radius: 8px;
  place-items: center;
  text-align: center;
  padding: 24px;
}
.drop-zone span { font-size: 24px; color: var(--text); }
.drop-zone input { max-width: 360px; background: transparent; border: 0; }

.filters {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 10px;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 14px;
}

.table-wrap {
  overflow-x: auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
}
table { width: 100%; border-collapse: collapse; min-width: 980px; }
th, td { padding: 13px 12px; text-align: left; border-bottom: 1px solid var(--line); font-size: 14px; }
th { color: var(--muted); font-size: 12px; text-transform: uppercase; letter-spacing: 0; }
.table-wrap.compact table { min-width: 720px; }
.table-wrap.compact th, .table-wrap.compact td { padding: 9px 10px; }
.preline { white-space: pre-line; }
.line-items { display: grid; gap: 10px; }
.line-items h2 { margin: 0; font-size: 18px; }
.line-items-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.line-items-list { display: grid; gap: 10px; }
.line-item-row {
  display: grid;
  grid-template-columns: 72px minmax(180px, 1.2fr) 130px repeat(4, minmax(92px, 1fr)) 112px auto;
  gap: 8px;
  align-items: end;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfb;
  overflow-x: auto;
}
.line-item-row label { min-width: 72px; }

.cards-list { display: grid; gap: 10px; }
.invoice-card { position: relative; padding: 16px; display: grid; gap: 12px; }
.invoice-card div:first-child { display: grid; gap: 3px; }
.invoice-card span { color: var(--muted); }
.amount { font-size: 24px; font-weight: 900; }
.chip-row { display: flex; gap: 8px; flex-wrap: wrap; }
.chip { display: inline-flex; align-items: center; min-height: 28px; padding: 5px 9px; border-radius: 999px; background: #eef2ef; color: var(--text); font-size: 12px; font-weight: 800; }
.chip.paid { background: #dff7ea; color: #12613c; }
.chip.unpaid { background: #fff3d8; color: #8a5600; }
.chip.partial { background: #e8efff; color: #294b9b; }
.stretched { position: absolute; inset: 0; font-size: 0; }
.mobile-only { display: none; }
.empty { padding: 24px; border: 1px dashed var(--line); border-radius: 8px; text-align: center; color: var(--muted); background: #fff; }

.review-layout { display: grid; grid-template-columns: minmax(0, 1fr) minmax(320px, 430px); gap: 14px; align-items: start; }
.preview-panel { padding: 14px; position: sticky; top: 78px; }
.preview-panel summary { font-weight: 900; cursor: pointer; margin-bottom: 12px; }
.preview-panel img, .preview-panel iframe { width: 100%; max-height: 72vh; border: 1px solid var(--line); border-radius: 8px; object-fit: contain; background: #fff; }
.preview-panel iframe { min-height: 560px; }

.report-grid { grid-template-columns: repeat(2, 1fr); }
.chart-card, .note-panel { padding: 18px; }
.chart-card h2, .note-panel h2 { margin-top: 0; }
.bar-row { display: grid; grid-template-columns: 120px 1fr auto; align-items: center; gap: 10px; margin: 10px 0; }
.bar-row div { height: 10px; background: #edf1ee; border-radius: 999px; overflow: hidden; }
.bar-row i { display: block; height: 100%; background: var(--green); border-radius: inherit; }
.detail-grid { grid-template-columns: repeat(4, 1fr); }

.flash-stack { display: grid; gap: 8px; margin-bottom: 12px; }
.flash { padding: 12px 14px; border-radius: 8px; background: #eef6ff; border: 1px solid #c9ddff; }
.flash.success { background: #e7f8ee; border-color: #bde8cb; }
.flash.error { background: #fff0f0; border-color: #ffcaca; }
.flash.warning { background: #fff8e8; border-color: #f4dda3; color: var(--warning); }
.auth-panel { width: min(460px, 100%); margin: 8vh auto; padding: 24px; }
.auth-panel .form-card { box-shadow: none; border: 0; padding: 0; grid-template-columns: 1fr; }

.loading-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  align-content: center;
  gap: 16px;
  background: rgba(246, 248, 245, 0.82);
  backdrop-filter: blur(8px);
}
.loading-overlay[hidden] {
  display: none !important;
}
.spinner {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 4px solid #cfe7d8;
  border-top-color: var(--green);
  animation: spin 700ms linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

@media (max-width: 860px) {
  .hero, .section-head { align-items: stretch; flex-direction: column; }
  .dashboard-actions, .metric-grid, .report-grid, .detail-grid { grid-template-columns: repeat(2, 1fr); }
  .filters, .form-card, .upload-panel, .review-layout { grid-template-columns: 1fr; }
  .line-item-row { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .table-wrap { display: none; }
  .mobile-only { display: block; }
  .preview-panel { position: static; }
}

@media (max-width: 560px) {
  .topbar { align-items: stretch; flex-wrap: wrap; }
  .brand { width: 100%; }
  .dashboard-actions, .metric-grid, .detail-grid { grid-template-columns: 1fr; }
  .page { padding-inline: 12px; }
  .hero h1, .section-head h1 { font-size: 30px; }
  .big-action { min-height: 104px; }
  .drop-zone { min-height: 190px; }
  .bar-row { grid-template-columns: 1fr; }
}
