/*
 * Application styles. Uses tokens from tokens.css only — no raw colours here.
 * Mobile first: base rules are for phones; min-width media queries enhance.
 */

/* ---------- Base and accessibility ---------- */
html { -webkit-text-size-adjust: 100%; }
body { min-height: 100vh; overflow-x: hidden; }

:focus-visible {
  outline: 3px solid var(--app-primary);
  outline-offset: 2px;
}

.skip-link {
  position: absolute;
  left: var(--app-space-2);
  top: -100px;
  z-index: 2000;
  padding: var(--app-space-2) var(--app-space-4);
  background: var(--app-surface);
  border: 1px solid var(--app-border-strong);
  border-radius: var(--app-radius);
}
.skip-link:focus { top: var(--app-space-2); }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

h1, .h1 { font-size: var(--app-fs-h1); font-weight: var(--app-fw-semibold); }
.text-muted, .form-text { color: var(--app-text-muted) !important; }

/* ---------- Touch-friendly controls ---------- */
.btn, .form-control, .form-select { min-height: var(--app-touch); }
.btn { font-weight: var(--app-fw-medium); border-radius: var(--app-radius); }
.btn-sm { min-height: 36px; }
.form-label { font-weight: var(--app-fw-medium); margin-bottom: var(--app-space-1); }

.btn-primary {
  --bs-btn-bg: var(--app-primary);
  --bs-btn-border-color: var(--app-primary);
  --bs-btn-hover-bg: var(--app-primary-hover);
  --bs-btn-hover-border-color: var(--app-primary-hover);
  --bs-btn-active-bg: var(--app-primary-hover);
  --bs-btn-active-border-color: var(--app-primary-hover);
  --bs-btn-disabled-bg: var(--app-primary);
  --bs-btn-disabled-border-color: var(--app-primary);
}
.btn-outline-secondary {
  --bs-btn-color: var(--app-text);
  --bs-btn-border-color: var(--app-border-strong);
  --bs-btn-hover-bg: var(--app-surface-muted);
  --bs-btn-hover-color: var(--app-text);
  --bs-btn-hover-border-color: var(--app-border-strong);
  --bs-btn-active-bg: var(--app-surface-muted);
  --bs-btn-active-color: var(--app-text);
}

/* ---------- Surfaces ---------- */
.app-card {
  background: var(--app-surface);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius-lg);
  box-shadow: var(--app-shadow-sm);
  padding: var(--app-space-4);
}
@media (min-width: 768px) { .app-card { padding: var(--app-space-6); } }

.page-header {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: var(--app-space-3);
  margin-bottom: var(--app-space-4);
}
.page-header h1 { margin: 0; }

/* Narrow single-purpose panels: confirmations, short forms */
.panel-narrow { max-width: 560px; }
.panel-wide { max-width: 960px; }

/* ---------- Payment entry (H5): who it's for, the split and its running total ---------- */
.payment-student {
  padding: var(--app-space-3);
  border: 1px solid var(--app-border-strong);
  border-radius: var(--bs-border-radius);
}
.payment-student-name { font-size: 1.25rem; font-weight: 600; }
.split-input { max-width: 10rem; }
.split-summary {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: var(--app-space-2);
}
.min-w-0 { min-width: 0; }

/* ---------- Status badges: text label always present, colour is secondary ---------- */
.status-badge {
  display: inline-flex;
  align-items: center;
  gap: var(--app-space-1);
  padding: 2px var(--app-space-2);
  font-size: var(--app-fs-xs);
  font-weight: var(--app-fw-semibold);
  line-height: 1.5;
  border-radius: 999px;
  border: 1px solid currentColor;
  color: var(--app-neutral);
  background: var(--app-neutral-subtle);
  white-space: nowrap;
}
.status-badge--success { color: var(--app-success); background: var(--app-success-subtle); }
.status-badge--warning { color: var(--app-warning); background: var(--app-warning-subtle); }
.status-badge--danger  { color: var(--app-danger);  background: var(--app-danger-subtle); }
.status-badge--info    { color: var(--app-info);    background: var(--app-info-subtle); }

/* ---------- Empty and permission states ---------- */
.state-panel {
  text-align: center;
  padding: var(--app-space-8) var(--app-space-4);
}
.state-panel .bi { font-size: 2rem; color: var(--app-text-muted); }
.state-panel h2 { font-size: var(--app-fs-lg); margin-top: var(--app-space-3); }

/* ---------- Auth layout (login, errors) ---------- */
.auth-wrap {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--app-space-4);
}
.auth-card { width: 100%; max-width: 400px; }
.auth-brand {
  display: flex;
  align-items: center;
  gap: var(--app-space-2);
  font-weight: var(--app-fw-semibold);
  font-size: var(--app-fs-lg);
  margin-bottom: var(--app-space-6);
}

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border-radius: var(--app-radius);
  background: var(--app-primary);
  color: #FFFFFF;
  flex-shrink: 0;
}

/* ---------- App shell ---------- */
.app-shell { display: flex; min-height: 100vh; }

.app-sidebar {
  display: none;
  flex-direction: column;
  width: var(--app-sidebar-width);
  flex-shrink: 0;
  background: var(--app-surface);
  border-right: 1px solid var(--app-border);
  position: sticky;
  top: 0;
  height: 100vh;
}
.app-sidebar__brand {
  display: flex;
  align-items: center;
  gap: var(--app-space-2);
  height: var(--app-topbar-height);
  padding: 0 var(--app-space-4);
  border-bottom: 1px solid var(--app-border);
  font-weight: var(--app-fw-semibold);
  white-space: nowrap;
  overflow: hidden;
}
.app-sidebar__nav { padding: var(--app-space-3) var(--app-space-2); flex: 1; overflow-y: auto; }

.nav-link-app {
  display: flex;
  align-items: center;
  gap: var(--app-space-3);
  min-height: var(--app-touch);
  padding: 0 var(--app-space-3);
  border-radius: var(--app-radius);
  color: var(--app-text-muted);
  text-decoration: none;
  font-weight: var(--app-fw-medium);
  white-space: nowrap;
}
.nav-link-app .bi { font-size: 1.125rem; }
.nav-link-app:hover { background: var(--app-surface-muted); color: var(--app-text); }
.nav-link-app[aria-current="page"] { background: var(--app-primary-subtle); color: var(--app-primary); }

.app-main { flex: 1; min-width: 0; display: flex; flex-direction: column; }

.app-topbar {
  position: sticky;
  top: 0;
  z-index: 1020;
  display: flex;
  align-items: center;
  gap: var(--app-space-2);
  height: var(--app-topbar-height);
  padding: 0 var(--app-space-3);
  background: var(--app-surface);
  border-bottom: 1px solid var(--app-border);
}
.app-topbar__school {
  min-width: 0;
  flex: 1;
  font-weight: var(--app-fw-semibold);
}
.app-topbar__school .btn { max-width: 100%; }
.app-topbar__school-name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

.icon-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: var(--app-touch);
  height: var(--app-touch);
  padding: 0;
  border: 0;
  border-radius: var(--app-radius);
  background: transparent;
  color: var(--app-text-muted);
}
.icon-btn:hover { background: var(--app-surface-muted); color: var(--app-text); }

.app-content {
  flex: 1;
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding: var(--app-space-4) var(--app-space-4)
           calc(var(--app-bottom-nav-height) + var(--app-space-6) + env(safe-area-inset-bottom, 0px));
}

/* Bottom navigation (phones and small tablets) */
.app-bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1030;
  display: flex;
  height: calc(var(--app-bottom-nav-height) + env(safe-area-inset-bottom, 0px));
  padding-bottom: env(safe-area-inset-bottom, 0px);
  background: var(--app-surface);
  border-top: 1px solid var(--app-border);
}
.app-bottom-nav__item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  min-width: 0;
  border: 0;
  background: transparent;
  color: var(--app-text-muted);
  text-decoration: none;
  font-size: var(--app-fs-xs);
  font-weight: var(--app-fw-medium);
}
.app-bottom-nav__item .bi { font-size: 1.25rem; }
.app-bottom-nav__item[aria-current="page"] { color: var(--app-primary); }

/* Desktop: show sidebar, hide bottom nav */
@media (min-width: 992px) {
  .app-sidebar { display: flex; }
  .app-bottom-nav { display: none; }
  .app-topbar { padding: 0 var(--app-space-6); }
  .app-content { padding: var(--app-space-6); }

  body.sidebar-collapsed .app-sidebar { width: var(--app-sidebar-collapsed-width); }
  body.sidebar-collapsed .app-sidebar .nav-label { display: none; }
  body.sidebar-collapsed .nav-link-app { justify-content: center; padding: 0; }
}

/* ---------- Stacked table: a real table on wide screens, one card per row on phones ---------- */
@media (max-width: 767.98px) {
  .table-stack thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  .table-stack, .table-stack tbody, .table-stack tr, .table-stack th, .table-stack td { display: block; width: 100%; }
  .table-stack tr { padding: var(--app-space-3) var(--app-space-4); border-bottom: 1px solid var(--app-border); }
  .table-stack th[scope="row"] { padding: 0 0 var(--app-space-2); border: 0; font-size: var(--app-fs-lg); }
  .table-stack td { padding: var(--app-space-1) 0; border: 0; }
  .table-stack td[data-label]::before {
    content: attr(data-label);
    display: block;
    font-size: var(--app-fs-sm);
    color: var(--app-text-muted);
    margin-bottom: 2px;
  }
}

/* ---------- Setup checklist ---------- */
.checklist { list-style: none; margin: 0; padding: 0; }
.checklist li {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--app-space-3);
  min-height: var(--app-touch);
  padding: var(--app-space-2) 0;
  border-bottom: 1px solid var(--app-border);
}
.checklist li:last-child { border-bottom: 0; }

/* ---------- Choice list (school selector) ---------- */
.choice-list { list-style: none; margin: 0; padding: 0; display: grid; gap: var(--app-space-2); }
.choice-list a {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--app-space-3);
  min-height: 56px;
  padding: var(--app-space-3) var(--app-space-4);
  background: var(--app-surface);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius-lg);
  color: var(--app-text);
  text-decoration: none;
}
.choice-list a:hover { border-color: var(--app-primary); }

/* ---------- Fee statement ---------- */
.statement-totals {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: var(--app-space-2);
  margin: 0 0 var(--app-space-4);
}
.statement-totals > div {
  padding: var(--app-space-2) var(--app-space-3);
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius);
  background: var(--app-surface-muted);
}
.statement-totals dt { font-size: var(--app-fs-sm); font-weight: normal; color: var(--app-text-muted); }
.statement-totals dd { margin: 0; font-weight: var(--app-fw-semibold); }
.statement-totals__overdue dd { color: var(--app-danger); }
.statement-totals__due { border-color: var(--app-primary) !important; background: var(--app-primary-subtle) !important; }
.statement-group { margin-bottom: var(--app-space-4); }
.statement-group__title {
  display: flex;
  justify-content: space-between;
  font-size: var(--app-fs-sm);
  font-weight: var(--app-fw-semibold);
  text-transform: uppercase;
  color: var(--app-text-muted);
  border-bottom: 1px solid var(--app-border);
  padding-bottom: var(--app-space-1);
  margin-bottom: 0;
}
/* Multi-year statement (H6): one block per year, then the totals for all years. */
.statement-year { margin-bottom: var(--app-space-6); }
.statement-year__title {
  padding: var(--app-space-2) var(--app-space-3);
  border-left: 4px solid var(--app-primary);
  background: var(--app-surface-muted);
  margin-bottom: var(--app-space-3);
}

/* ---------- Print pages ---------- */
.print-page { background: var(--app-surface); }
.print-sheet { max-width: 800px; margin: 0 auto; padding: var(--app-space-6) var(--app-space-4); }
.print-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: var(--app-space-4);
  border-bottom: 2px solid var(--app-text);
  padding-bottom: var(--app-space-2);
}
@media print {
  .no-print, .app-sidebar, .app-topbar, .app-bottom-nav { display: none !important; }
  .print-sheet { padding: 0; max-width: none; }
  .statement-totals > div { background: none !important; }
  /* Always print the full table layout, even on narrow paper. */
  .table-stack thead { position: static; width: auto; height: auto; clip: auto; }
  .table-stack { display: table; }
  .table-stack tbody { display: table-row-group; }
  .table-stack tr { display: table-row; padding: 0; }
  .table-stack th, .table-stack td { display: table-cell; width: auto; padding: 2px 4px; }
  .table-stack td[data-label]::before { content: none; }
}

.school-logo-preview {
  display: block;
  max-width: 160px;
  max-height: 120px;
  border: 1px solid var(--app-border);
  border-radius: var(--app-radius);
  padding: var(--app-space-2);
  background: var(--app-surface);
}

/* ---------- Invoice preview (same body as the PDF, see finance/_invoice_body.html) ---------- */
.invoice-sheet { overflow-x: auto; }
.invoice-sheet table { width: 100%; border-collapse: collapse; }
.invoice-sheet td, .invoice-sheet th { vertical-align: top; padding: var(--app-space-1) var(--app-space-2); }
.invoice-sheet .inv-logo { max-width: 72px; max-height: 72px; }
.invoice-sheet .inv-head__title { text-align: right; }
.invoice-sheet .inv-school { font-size: var(--app-fs-lg); font-weight: var(--app-fw-semibold); }
.invoice-sheet .inv-title { font-size: var(--app-fs-h1); font-weight: var(--app-fw-semibold); color: var(--app-primary); }
.invoice-sheet .inv-void { color: var(--app-danger); font-weight: var(--app-fw-semibold); border: 1px solid var(--app-danger); padding: var(--app-space-2); }
.invoice-sheet .inv-parties { margin: var(--app-space-3) 0; border-top: 2px solid var(--app-text); border-bottom: 1px solid var(--app-border); }
.invoice-sheet .inv-label { color: var(--app-text-muted); font-size: var(--app-fs-xs); text-transform: uppercase; }
.invoice-sheet .inv-lines th { background: var(--app-surface-muted); font-size: var(--app-fs-sm); }
.invoice-sheet .inv-lines td { border-bottom: 1px solid var(--app-border); }
.invoice-sheet .inv-l { text-align: left; }
.invoice-sheet .inv-r { text-align: right; white-space: nowrap; }
.invoice-sheet .inv-totals td { border-bottom: 1px solid var(--app-border); }
.invoice-sheet .inv-due td { font-weight: var(--app-fw-semibold); font-size: var(--app-fs-lg); border-top: 2px solid var(--app-text); border-bottom: 0; }
.invoice-sheet .inv-footer { margin-top: var(--app-space-4); padding-top: var(--app-space-2); border-top: 1px solid var(--app-border); color: var(--app-text-muted); font-size: var(--app-fs-sm); }

/* Long forms on phones: keep the main action reachable at the bottom of the screen. */
@media (max-width: 767.98px) {
  .form-sticky-actions {
    position: sticky;
    bottom: calc(var(--app-bottom-nav-height) + env(safe-area-inset-bottom, 0px));
    background: var(--app-surface);
    padding: var(--app-space-3) 0;
    border-top: 1px solid var(--app-border);
    z-index: 5;
  }
}

/* ---------- Home dashboard ---------- */
.dashboard-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: var(--app-space-3);
}
@media (min-width: 992px) {
  .dashboard-grid { grid-template-columns: 1fr 1fr; }
  .dashboard-panel--wide { grid-column: 1 / -1; }
}
.dashboard-panel { margin: 0; min-width: 0; }
.dashboard-panel h2 .bi, .dashboard-panel summary .bi { color: var(--app-primary); margin-right: var(--app-space-1); }
.dashboard-panel summary { cursor: pointer; min-height: 44px; display: flex; align-items: center; gap: var(--app-space-2); flex-wrap: wrap; }
.panel-list { list-style: none; margin: 0; padding: 0; }
.panel-list li {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0 var(--app-space-2);
  padding: var(--app-space-2) 0;
  border-bottom: 1px solid var(--app-border);
}
.panel-list li:last-child { border-bottom: 0; }
.panel-list li > a { min-height: 44px; display: inline-flex; align-items: center; }

/* ---------- Audit log ---------- */
.audit-value {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  font-size: var(--app-fs-sm);
  font-family: inherit;
}
