:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --border: #e2e5ea;
  --text: #1f2430;
  --text-muted: #6b7280;
  --primary: #2f6feb;
  --primary-hover: #2557c4;
  --danger: #d64545;
  --success-bg: #e7f6ec;
  --success-text: #1f7a3f;
  --error-bg: #fdeceb;
  --error-text: #b3261e;
  --radius: 8px;
}

* { 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);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

.container {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 20px 60px;
}

/* Top navigation */
.topbar {
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 14px 20px;
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 17px; color: var(--text); }
.brand:hover { text-decoration: none; }
/* On desktop the collapse wrapper is invisible to layout — its children
   (nav, logout-form) act as direct flex items of .topbar-inner. */
.topbar-collapse { display: contents; }
.topbar nav { display: flex; gap: 18px; flex: 1; flex-wrap: wrap; }
.topbar nav a { color: var(--text-muted); font-size: 14px; padding: 4px 0; }
.topbar nav a.active { color: var(--primary); font-weight: 600; }
.logout-form { display: flex; align-items: center; gap: 10px; }
.nav-toggle {
  display: none;
  background: none;
  border: 1px solid var(--border);
  border-radius: 6px;
  font-size: 20px;
  line-height: 1;
  padding: 6px 10px;
  color: var(--text);
}
.user-email { color: var(--text-muted); font-size: 13px; }

/* Buttons */
button, .btn-primary, .btn-secondary, .btn-cancel {
  font-family: inherit;
  font-size: 14px;
  cursor: pointer;
}
button[type="submit"], .btn-primary {
  background: var(--primary);
  color: #fff;
  border: none;
  padding: 9px 16px;
  border-radius: var(--radius);
  font-weight: 600;
  display: inline-block;
}
button[type="submit"]:hover, .btn-primary:hover {
  background: var(--primary-hover);
  text-decoration: none;
}
.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  padding: 9px 16px;
  border-radius: var(--radius);
  font-weight: 600;
  display: inline-block;
}
.btn-secondary:hover { border-color: var(--primary); text-decoration: none; }
.btn-cancel {
  padding: 9px 12px;
  color: var(--text-muted);
}
.link-btn, button.link-btn {
  background: none;
  border: none;
  color: var(--primary);
  padding: 0;
  font-size: 14px;
  font-weight: 400;
  border-radius: 0;
}
.link-btn:hover, button.link-btn:hover { background: none; text-decoration: underline; }
.link-btn.danger, button.link-btn.danger { color: var(--danger); }

/* Forms */
label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}
input, select, textarea {
  font-family: inherit;
  font-size: 16px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
}
input:focus, select:focus, textarea:focus {
  outline: none;
  border-color: var(--primary);
}
.field-hint { font-weight: 400; font-size: 12px; }
.checkbox-label { flex-direction: row; align-items: center; gap: 8px; }
.checkbox-label input { width: auto; }

.form-grid {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
  max-width: 760px;
}
.form-grid .full-width { grid-column: 1 / -1; }
.form-actions { display: flex; gap: 12px; align-items: center; }
/* On desktop this wrapper is invisible to the grid — its labels act as
   ordinary direct grid items of .form-grid, same order as unwrapped. */
.form-row { display: contents; }

.inline-form { display: inline-flex; gap: 8px; align-items: center; margin: 0; }
.add-form { margin-bottom: 20px; }
.add-form input[type="text"] { min-width: 260px; }

/* Auth pages */
.auth-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 28px;
  max-width: 380px;
  margin: 40px auto;
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.auth-box form { display: flex; flex-direction: column; gap: 14px; }
.auth-box h1 { margin: 0 0 4px; font-size: 20px; }

/* Flash messages */
.flash {
  padding: 10px 14px;
  border-radius: 6px;
  font-size: 14px;
  margin-bottom: 16px;
}
.flash-success { background: var(--success-bg); color: var(--success-text); }
.flash-error { background: var(--error-bg); color: var(--error-text); }
.muted { color: var(--text-muted); }

/* Page head */
.page-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 18px;
  flex-wrap: wrap;
  gap: 12px;
}
.page-head h1 { margin: 0; font-size: 22px; }
.page-head-actions { display: flex; gap: 10px; }

/* Filter bar */
.filter-bar {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-bottom: 18px;
  align-items: end;
}
.filter-field { display: flex; flex-direction: column; gap: 4px; min-width: 140px; }
.filter-actions { flex-direction: row; align-items: center; gap: 10px; }
/* On desktop this wrapper is invisible to layout — From/To and
   Currency/Billable act as ordinary direct flex items of .filter-bar. */
.filter-row { display: contents; }

/* Filter toggle button: hidden on desktop, where the panel is always shown. */
.filter-toggle {
  display: none;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px 16px;
  margin-bottom: 12px;
  font-weight: 600;
  font-size: 14px;
  color: var(--text);
}
.filter-badge {
  background: var(--primary);
  color: #fff;
  font-size: 12px;
  font-weight: 700;
  border-radius: 999px;
  padding: 1px 8px;
  margin-left: 6px;
}
.filter-toggle-caret { margin-left: auto; transition: transform 0.15s ease; }
.filter-toggle.open .filter-toggle-caret { transform: rotate(180deg); }

.totals-bar {
  margin-bottom: 12px;
  color: var(--text-muted);
  font-size: 14px;
}

/* Table */
.data-table {
  width: 100%;
  border-collapse: collapse;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.data-table th, .data-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  text-align: left;
  font-size: 14px;
}
.data-table th {
  background: #fafbfc;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
}
.data-table tr:last-child td { border-bottom: none; }
.actions-col { white-space: nowrap; display: flex; gap: 10px; align-items: center; }
.amount-col { white-space: nowrap; font-variant-numeric: tabular-nums; }
.desc-col { max-width: 240px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.pagination { display: flex; gap: 6px; margin-top: 16px; }
.pagination a {
  padding: 6px 11px;
  border: 1px solid var(--border);
  border-radius: 6px;
  color: var(--text);
}
.pagination a.active { background: var(--primary); border-color: var(--primary); color: #fff; }

.table-scroll-hint { display: none; font-size: 12px; color: var(--text-muted); margin-bottom: 6px; }

@media (max-width: 720px) {
  /* minmax(0, 1fr), not plain 1fr — otherwise the track refuses to shrink
     below its content's natural (min-content) width and the whole page
     gets pushed wider than the viewport ("grid blowout"). */
  .form-grid { grid-template-columns: minmax(0, 1fr); }

  /* Subtotal/GST/PST share a row, Total/Currency share a row, instead of
     each field taking its own full-width line. */
  .form-row { display: flex; gap: 10px; min-width: 0; }
  .form-row label { flex: 1; min-width: 0; }

  /* Table becomes a horizontally-scrollable strip instead of wrapping/squishing. */
  .data-table { display: block; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .data-table th, .data-table td { white-space: nowrap; }
  .desc-col { white-space: nowrap; }
  .table-scroll-hint { display: block; }

  /* Pin Edit/Delete on the left so they're reachable without scrolling
     through every column first. */
  .data-table .actions-col {
    position: sticky;
    left: 0;
    z-index: 1;
    box-shadow: 1px 0 0 var(--border);
  }
  .data-table td.actions-col { background: var(--surface); }
  .data-table th.actions-col { background: #fafbfc; }

  /* Top nav: brand + hamburger on one row, links/logout collapse into a
     panel underneath that the button shows/hides. */
  .topbar-inner {
    justify-content: space-between;
    padding: 12px 16px;
  }
  .nav-toggle { display: block; }
  .topbar-collapse {
    display: none;
    order: 3;
    width: 100%;
    flex-direction: column;
    align-items: stretch;
    gap: 14px;
    margin-top: 12px;
    padding-top: 12px;
    border-top: 1px solid var(--border);
  }
  .topbar-collapse.open { display: flex; }
  .topbar nav {
    flex: none;
    width: 100%;
    flex-direction: column;
    gap: 12px;
  }
  .logout-form {
    width: 100%;
    justify-content: space-between;
  }

  .page-head-actions { width: 100%; }
  .page-head-actions .btn-primary,
  .page-head-actions .btn-secondary { flex: 1; text-align: center; }

  .filter-bar { flex-direction: column; align-items: stretch; margin-bottom: 0; }
  .filter-field { min-width: 0; width: 100%; }
  .filter-actions { flex-direction: row; }
  .filter-actions button { flex: 1; }

  /* From/To and Currency/Billable pair up two-per-row instead of stacking. */
  .filter-row { display: flex; gap: 14px; }
  .filter-row .filter-field { width: auto; flex: 1; min-width: 0; }

  /* Filter panel: collapsed by default, toggled open by .filter-toggle. */
  .filter-toggle { display: flex; }
  .filter-panel { display: none; margin-bottom: 18px; }
  .filter-panel.open { display: block; }
}
