:root {
  --bg: #f4f6f8;
  --panel: #ffffff;
  --border: #e2e6ea;
  --text: #1a2233;
  --text-muted: #6b7685;
  --accent: #1e7d4f;
  --accent-hover: #17663f;
  --accent-soft: #e5f4ec;
  --bad: #c1442b;
  --bad-soft: #fbe9e5;
  --good: #1e7d4f;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.04), 0 1px 3px rgba(16, 24, 40, 0.06);
  --radius: 10px;
  --court-blue: #2d6ca8;
}

[data-theme="dark"] {
  --bg: #12161c;
  --panel: #1a2029;
  --border: #2a3241;
  --text: #e8ecf1;
  --text-muted: #93a0b3;
  --accent: #3fbf7f;
  --accent-hover: #34a56d;
  --accent-soft: #163427;
  --bad: #e5765e;
  --bad-soft: #3a221e;
  --good: #3fbf7f;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
  --court-blue: #5b9bd5;
}

* { box-sizing: border-box; }

/* The `hidden` attribute must always win over any component's own `display` declaration
   (several elements below set display:flex, which otherwise out-specifies [hidden]). */
[hidden] { display: none !important; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Inter, Roboto, Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  transition: background 0.2s ease, color 0.2s ease;
}

.app { max-width: 1180px; margin: 0 auto; padding: 0 24px 60px; }

/* Topbar */
.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 24px 0 20px;
  gap: 16px;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 12px; }
.brand-icon { font-size: 32px; line-height: 1; }
.brand h1 { font-size: 20px; margin: 0; font-weight: 700; }
.subtitle { margin: 2px 0 0; font-size: 13px; color: var(--text-muted); }
.topbar-actions { display: flex; gap: 10px; align-items: center; }

/* Buttons */
.btn {
  font: inherit;
  font-size: 13.5px;
  font-weight: 600;
  padding: 9px 16px;
  border-radius: 8px;
  border: 1px solid transparent;
  cursor: pointer;
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn-primary { background: var(--accent); color: #fff; }
.btn-primary:hover { background: var(--accent-hover); }
.btn-ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--accent-soft); }
.btn-icon { padding: 6px 10px; font-size: 14px; background: transparent; border: 1px solid var(--border); color: var(--text); border-radius: 7px; }
.btn-icon:hover { background: var(--accent-soft); }
.btn-sm { padding: 6px 10px; font-size: 12.5px; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn:disabled:hover { background: inherit; }

/* Stats */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 14px;
  margin-bottom: 20px;
}
.stat-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 18px;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.stat-label { font-size: 12px; color: var(--text-muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 22px; font-weight: 700; }
.stat-good { color: var(--good); }
.stat-bad { color: var(--bad); }
.stat-sub { font-size: 12px; color: var(--text-muted); }

/* Panels */
.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 20px 22px;
  margin-bottom: 20px;
}
.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  margin-bottom: 16px;
}
.panel-header h2 { font-size: 16px; margin: 0; font-weight: 700; }

.chart-controls { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; }
.segmented {
  display: inline-flex;
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 3px;
  gap: 2px;
}
.seg-btn {
  border: none;
  background: transparent;
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  color: var(--text-muted);
  border-radius: 6px;
  cursor: pointer;
}
.seg-btn.active { background: var(--panel); color: var(--accent); box-shadow: var(--shadow); }
.nav-controls { display: flex; align-items: center; gap: 8px; }
.period-label { font-size: 13px; font-weight: 600; color: var(--text); min-width: 150px; text-align: center; }

.chart-wrap { height: 320px; position: relative; }
.chart-wrap-sm { height: 240px; margin-bottom: 18px; }
.chart-legend-note { display: flex; gap: 18px; margin-top: 12px; font-size: 12.5px; color: var(--text-muted); }
.dot { display: inline-block; width: 9px; height: 9px; border-radius: 2px; margin-right: 6px; }
.dot-paid { background: var(--accent); }
.dot-unpaid { background: var(--bad); }
.dot-line { background: var(--court-blue); border-radius: 50%; }

/* Table filters */
.table-filters { display: flex; gap: 8px; flex-wrap: wrap; }
.input {
  font: inherit;
  font-size: 13px;
  padding: 8px 10px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
}
.input-sm { padding: 7px 10px; }

/* Table */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
thead th {
  text-align: left;
  font-size: 11.5px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  padding: 10px 12px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  user-select: none;
  white-space: nowrap;
}
thead th:hover { color: var(--text); }
tbody td { padding: 11px 12px; border-bottom: 1px solid var(--border); white-space: nowrap; }
tbody tr:hover { background: var(--accent-soft); }
.col-actions { text-align: right; }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.row-actions button {
  background: transparent;
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 4px 8px;
  font-size: 12px;
  cursor: pointer;
  color: var(--text);
}
.row-actions button:hover { background: var(--panel); }

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 9px;
  border-radius: 999px;
  font-size: 11.5px;
  font-weight: 700;
}
.badge-paid { background: var(--accent-soft); color: var(--good); }
.badge-unpaid { background: var(--bad-soft); color: var(--bad); }

.empty-state { text-align: center; padding: 50px 20px; color: var(--text-muted); }
.empty-state p { margin-bottom: 14px; }

/* Modal */
.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(10, 14, 20, 0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 20px;
}
.modal {
  background: var(--panel);
  border-radius: 14px;
  width: 100%;
  max-width: 440px;
  max-height: 90vh;
  overflow-y: auto;
  box-shadow: 0 20px 60px rgba(0,0,0,0.3);
}
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 22px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { margin: 0; font-size: 16px; }
.modal-body { padding: 20px 22px; display: flex; flex-direction: column; gap: 14px; }
.field { display: flex; flex-direction: column; gap: 6px; font-size: 13px; font-weight: 600; color: var(--text-muted); flex: 1; }
.field input {
  font: inherit;
  font-size: 14px;
  font-weight: 500;
  color: var(--text);
  padding: 9px 11px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
}
.field-row { display: flex; gap: 12px; }
.checkbox-field { display: flex; align-items: center; gap: 8px; font-size: 14px; font-weight: 600; cursor: pointer; }
.checkbox-field input { width: 18px; height: 18px; accent-color: var(--accent); cursor: pointer; }
.modal-actions { display: flex; align-items: center; gap: 10px; margin-top: 6px; }
.spacer { flex: 1; }

/* Role banner */
.role-banner {
  max-width: 1180px;
  margin: 0 auto 16px;
  padding: 8px 14px;
  border-radius: 8px;
  background: var(--accent-soft);
  color: var(--good);
  font-size: 12.5px;
  font-weight: 600;
}

/* Modal helper text */
.field-hint { margin: -8px 0 0; font-size: 11.5px; color: var(--text-muted); }
.field-readonly {
  margin: 0;
  font-size: 14px;
  font-weight: 600;
  padding: 9px 11px;
  border-radius: 8px;
  background: var(--bg);
  border: 1px solid var(--border);
  color: var(--text-muted);
}
.field em { font-style: normal; font-weight: 400; opacity: 0.7; }

/* Payment badges */
.payment-cell { display: flex; flex-direction: column; gap: 3px; align-items: flex-start; }
.route-tag { font-size: 10.5px; color: var(--text-muted); font-weight: 600; }

/* Payout table */
#payoutTable td, #payoutTable th { white-space: nowrap; }
.owed-amount { font-weight: 700; color: var(--bad); }
.owed-amount.settled { color: var(--good); }

/* Calendar */
.calendar-nav { display: flex; align-items: center; gap: 8px; margin-bottom: 14px; }
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 6px;
}
.cal-dow { text-align: center; font-size: 11px; font-weight: 700; color: var(--text-muted); padding-bottom: 4px; text-transform: uppercase; }
.cal-day {
  border: 1px solid var(--border);
  border-radius: 8px;
  min-height: 76px;
  padding: 6px 8px;
  cursor: pointer;
  background: var(--bg);
  display: flex;
  flex-direction: column;
  gap: 4px;
  transition: border-color 0.15s ease;
}
.cal-day:hover { border-color: var(--accent); }
.cal-day.empty { visibility: hidden; }
.cal-day.today { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent); }
.cal-day.selected { background: var(--accent-soft); }
.cal-day-num { font-size: 12px; font-weight: 700; color: var(--text-muted); }
.cal-day-amount { font-size: 11.5px; font-weight: 700; color: var(--text); }
.cal-day-dots { display: flex; gap: 3px; flex-wrap: wrap; }
.cal-dot { width: 6px; height: 6px; border-radius: 50%; }
.cal-dot.paid { background: var(--accent); }
.cal-dot.unpaid { background: var(--bad); }

.calendar-day-detail { margin-top: 18px; border-top: 1px solid var(--border); padding-top: 14px; }
.calendar-day-detail .panel-header { margin-bottom: 10px; }
.cal-session-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  font-size: 13px;
  gap: 10px;
}
.cal-session-row:last-child { border-bottom: none; }

@media (max-width: 640px) {
  .calendar-grid { gap: 3px; }
  .cal-day { min-height: 54px; padding: 4px; }
  .cal-day-amount { display: none; }
}

/* Auth screen */
.auth-screen {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  z-index: 200;
  padding: 20px;
}
.auth-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow);
  padding: 40px 34px;
  text-align: center;
  max-width: 360px;
  width: 100%;
}
.auth-card .brand-icon { font-size: 44px; display: block; margin-bottom: 10px; }
.auth-card h1 { font-size: 19px; margin: 0 0 6px; }
.auth-card .subtitle { margin: 0 0 26px; }
.btn-google {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  width: 100%;
  background: var(--accent);
}
.btn-google svg { background: #fff; border-radius: 3px; padding: 2px; }
.auth-note { margin: 14px 0 0; font-size: 12.5px; color: var(--bad); min-height: 16px; }

/* User menu / sync badge */
.user-menu { display: flex; align-items: center; gap: 8px; }
.user-avatar { width: 30px; height: 30px; border-radius: 50%; border: 1px solid var(--border); object-fit: cover; }
.sync-badge {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  background: var(--accent-soft);
  padding: 6px 10px;
  border-radius: 999px;
}
.sync-badge.offline { color: var(--bad); background: var(--bad-soft); }

@media (max-width: 900px) {
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
}
@media (max-width: 560px) {
  .stats-grid { grid-template-columns: 1fr; }
  .field-row { flex-direction: column; }
  .panel { padding: 16px; }
}
