:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --surface-alt: #f8fafc;
  --border: #d7e0ea;
  --border-strong: #b6c4d4;
  --text: #0f172a;
  --text-secondary: #334155;
  --text-muted: #475569;
  --brand: #0f2942;
  --brand-hover: #173a5e;
  --brand-soft: #e7eef6;
  --emerald: #047857;
  --emerald-bg: #ecfdf5;
  --emerald-border: #a7f3d0;
  --amber: #92400e;
  --amber-bg: #fffbeb;
  --amber-border: #fcd34d;
  --crimson: #991b1b;
  --crimson-bg: #fef2f2;
  --crimson-border: #fecaca;
  --shadow: 0 2px 10px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  font-size: 18px;
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  line-height: 1.6;
  color: var(--text);
  background: var(--bg);
  padding-bottom: 5.5rem;
}

@media (min-width: 720px) {
  body {
    padding-bottom: 2.5rem;
  }
}

a {
  color: var(--brand);
}

.num,
td.num,
th.num {
  font-variant-numeric: tabular-nums;
  text-align: right;
  white-space: nowrap;
}

.mono {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
}

/* Header & Navigation */
.site-header {
  background: var(--brand);
  color: #ffffff;
  border-bottom: 3px solid #1e4268;
}

.header-inner {
  max-width: 1040px;
  margin: 0 auto;
  padding: 0.75rem 1rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
}

.brand-link {
  display: inline-flex;
  align-items: center;
  gap: 0.65rem;
  color: #ffffff;
  text-decoration: none;
  font-weight: 700;
  font-size: 1.15rem;
  min-height: 48px;
}

.brand-logo {
  width: 36px;
  height: 36px;
  flex-shrink: 0;
}

.desktop-nav {
  display: none;
  align-items: center;
  gap: 0.35rem;
}

@media (min-width: 720px) {
  .desktop-nav {
    display: flex;
  }
}

.nav-link {
  color: #e2e8f0;
  text-decoration: none;
  font-weight: 600;
  font-size: 0.95rem;
  padding: 0.55rem 0.9rem;
  border-radius: var(--radius-sm);
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.nav-link:hover {
  background: rgba(255, 255, 255, 0.14);
  color: #ffffff;
}

.nav-link.active {
  background: #ffffff;
  color: var(--brand);
}

.header-user {
  display: flex;
  align-items: center;
  gap: 0.65rem;
}

.user-pill {
  font-size: 0.85rem;
  color: #e2e8f0;
  font-weight: 600;
}

.btn-logout {
  background: rgba(255, 255, 255, 0.14);
  color: #ffffff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: var(--radius-sm);
  padding: 0.45rem 0.85rem;
  min-height: 44px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
}

.btn-logout:hover {
  background: rgba(255, 255, 255, 0.24);
}

/* Mobile Bottom Nav */
.mobile-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: var(--surface);
  border-top: 2px solid var(--border);
  display: flex;
  justify-content: space-around;
  padding: 0.35rem 0.25rem;
  z-index: 50;
  box-shadow: 0 -4px 16px rgba(15, 23, 42, 0.08);
}

@media (min-width: 720px) {
  .mobile-nav {
    display: none;
  }
}

.mobile-nav-item {
  flex: 1;
  text-align: center;
  text-decoration: none;
  color: var(--text-secondary);
  font-weight: 600;
  font-size: 0.82rem;
  padding: 0.55rem 0.25rem;
  min-height: 52px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
}

.mobile-nav-item.active {
  background: var(--brand-soft);
  color: var(--brand);
  font-weight: 700;
}

/* Layout Container */
.container {
  max-width: 1040px;
  margin: 0 auto;
  padding: 1.25rem 1rem 2rem;
}

.page-header {
  margin-bottom: 1.25rem;
}

.page-title {
  margin: 0 0 0.25rem;
  font-size: 1.65rem;
  line-height: 1.25;
  color: var(--text);
}

.page-subtitle {
  margin: 0;
  color: var(--text-muted);
  font-size: 0.98rem;
}

/* Cards & Grids */
.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.25rem;
  box-shadow: var(--shadow);
  margin-bottom: 1.25rem;
}

.card-title {
  margin: 0 0 0.75rem;
  font-size: 1.15rem;
  font-weight: 700;
  color: var(--text);
}

.hero-card {
  background: linear-gradient(135deg, #0f2942 0%, #173a5e 100%);
  color: #ffffff;
  border: none;
  padding: 1.5rem;
}

.hero-top {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: flex-start;
  gap: 1rem;
  margin-bottom: 1rem;
}

.hero-label {
  font-size: 0.92rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #cbd5e1;
  font-weight: 600;
}

.hero-balance {
  font-size: 2.35rem;
  font-weight: 800;
  line-height: 1.15;
  margin: 0.25rem 0 0;
  font-variant-numeric: tabular-nums;
}

@media (min-width: 600px) {
  .hero-balance {
    font-size: 2.8rem;
  }
}

.progress-track {
  background: rgba(255, 255, 255, 0.2);
  border-radius: 999px;
  height: 16px;
  overflow: hidden;
  margin: 0.9rem 0 0.5rem;
}

.progress-fill {
  background: #34d399;
  height: 100%;
  border-radius: 999px;
  transition: width 0.3s ease;
}

progress.loan-progress {
  width: 100%;
  height: 16px;
  appearance: none;
  -webkit-appearance: none;
  border: none;
  border-radius: 999px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.22);
  margin: 0.9rem 0 0.5rem;
  display: block;
}

progress.loan-progress::-webkit-progress-bar {
  background: rgba(255, 255, 255, 0.22);
  border-radius: 999px;
}

progress.loan-progress::-webkit-progress-value {
  background: #34d399;
  border-radius: 999px;
}

progress.loan-progress::-moz-progress-bar {
  background: #34d399;
  border-radius: 999px;
}

.progress-caption {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.5rem;
  font-size: 0.92rem;
  color: #e2e8f0;
}

.hero-next {
  margin-top: 1.1rem;
  padding-top: 1rem;
  border-top: 1px solid rgba(255, 255, 255, 0.2);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 0.75rem;
  font-size: 0.98rem;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
  margin-bottom: 1.25rem;
}

@media (min-width: 600px) {
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

@media (min-width: 900px) {
  .stats-grid {
    grid-template-columns: repeat(4, 1fr);
  }
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 1.1rem 1.2rem;
  box-shadow: var(--shadow);
}

.stat-label {
  font-size: 0.88rem;
  color: var(--text-muted);
  font-weight: 600;
  margin-bottom: 0.25rem;
}

.stat-value {
  font-size: 1.45rem;
  font-weight: 800;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.stat-hint {
  font-size: 0.85rem;
  color: var(--text-muted);
  margin-top: 0.2rem;
}

.chart-wrap {
  position: relative;
  width: 100%;
  height: 290px;
}

/* Badges & Alerts */
.badge {
  display: inline-flex;
  align-items: center;
  padding: 0.3rem 0.75rem;
  border-radius: 999px;
  font-size: 0.85rem;
  font-weight: 700;
  border: 1px solid transparent;
}

.badge-success {
  background: var(--emerald-bg);
  color: var(--emerald);
  border-color: var(--emerald-border);
}

.badge-warn {
  background: var(--amber-bg);
  color: var(--amber);
  border-color: var(--amber-border);
}

.badge-danger {
  background: var(--crimson-bg);
  color: var(--crimson);
  border-color: var(--crimson-border);
}

.badge-neutral {
  background: var(--brand-soft);
  color: var(--brand);
  border-color: var(--border);
}

.alert {
  padding: 1rem 1.15rem;
  border-radius: var(--radius-sm);
  border: 1px solid var(--border);
  margin-bottom: 1.25rem;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.alert-warn {
  background: var(--amber-bg);
  border-color: var(--amber-border);
  color: var(--amber);
}

.alert-danger {
  background: var(--crimson-bg);
  border-color: var(--crimson-border);
  color: var(--crimson);
}

.alert-success {
  background: var(--emerald-bg);
  border-color: var(--emerald-border);
  color: var(--emerald);
}

.alert-info {
  background: var(--brand-soft);
  border-color: var(--border-strong);
  color: var(--brand);
}

/* Tables & Accordions */
.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

table.data-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.94rem;
}

table.data-table th,
table.data-table td {
  padding: 0.7rem 0.65rem;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table.data-table th {
  text-align: left;
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: var(--text-muted);
  background: var(--surface-alt);
}

table.data-table tr:last-child td {
  border-bottom: none;
}

details.year-group {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 0.9rem;
  box-shadow: var(--shadow);
  overflow: hidden;
}

details.year-group > summary {
  list-style: none;
  cursor: pointer;
  padding: 1rem 1.2rem;
  min-height: 56px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  font-weight: 700;
  background: var(--surface);
}

details.year-group > summary::-webkit-details-marker {
  display: none;
}

details.year-group[open] > summary {
  background: var(--surface-alt);
  border-bottom: 1px solid var(--border);
}

.year-summary-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  font-weight: 600;
  font-size: 0.9rem;
  color: var(--text-secondary);
  font-variant-numeric: tabular-nums;
}

/* Forms & Buttons */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.4rem;
  min-height: 48px;
  padding: 0.6rem 1.15rem;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  font-size: 0.96rem;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
  transition: background-color 0.15s ease, transform 0.1s ease;
}

.btn:active {
  transform: scale(0.98);
}

.btn-sm {
  min-height: 44px;
  padding: 0.45rem 0.85rem;
  font-size: 0.88rem;
}

.btn-primary {
  background: var(--brand);
  color: #ffffff;
}

.btn-primary:hover {
  background: var(--brand-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--brand);
  border-color: var(--border-strong);
}

.btn-secondary:hover {
  background: var(--surface-alt);
}

.btn-success {
  background: var(--emerald);
  color: #ffffff;
}

.btn-danger {
  background: var(--crimson-bg);
  color: var(--crimson);
  border-color: var(--crimson-border);
}

.form-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 1rem;
}

@media (min-width: 640px) {
  .form-grid-2 {
    grid-template-columns: repeat(2, 1fr);
  }
  .form-grid-3 {
    grid-template-columns: repeat(3, 1fr);
  }
}

.field {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.field label {
  font-size: 0.92rem;
  font-weight: 700;
  color: var(--text-secondary);
}

.field input,
.field select {
  min-height: 48px;
  padding: 0.55rem 0.8rem;
  font-size: 1rem;
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  background: #ffffff;
  color: var(--text);
}

.field input:focus {
  outline: 3px solid rgba(15, 41, 66, 0.2);
  border-color: var(--brand);
}

.actions-row {
  display: flex;
  flex-wrap: wrap;
  gap: 0.65rem;
  align-items: center;
}

/* Login Page */
.login-shell {
  max-width: 460px;
  margin: 2.5rem auto;
  padding: 0 1rem;
}

.login-brand {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  margin-bottom: 1.25rem;
}

.login-brand h1 {
  margin: 0;
  font-size: 1.45rem;
  color: var(--brand);
}

/* Loan Terms Grid */
.terms-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 0.75rem;
}

@media (min-width: 640px) {
  .terms-grid {
    grid-template-columns: repeat(2, 1fr);
  }
}

.term-item {
  background: var(--surface-alt);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0.75rem 0.95rem;
}

.term-key {
  font-size: 0.84rem;
  color: var(--text-muted);
  font-weight: 600;
}

.term-val {
  font-size: 1.02rem;
  font-weight: 700;
  color: var(--text);
}

.empty-state {
  padding: 1.5rem;
  text-align: center;
  color: var(--text-muted);
  background: var(--surface-alt);
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
}

.htmx-indicator {
  display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
  display: inline-block;
}
