/* ── Reset & Tokens ──────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg:          #0f172a;
  --surface:     #1e293b;
  --surface2:    #263147;
  --border:      rgba(255,255,255,0.08);
  --accent:      #38bdf8;
  --accent-dim:  rgba(56,189,248,0.15);
  --green:       #4ade80;
  --red:         #f87171;
  --orange:      #fb923c;
  --text:        #f1f5f9;
  --muted:       #94a3b8;
  --radius:      16px;
  --radius-sm:   10px;
  --nav-h:       calc(64px + env(safe-area-inset-bottom, 0px));
  --header-h:    60px;
}

html { font-size: 18px; }

body {
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  min-height: 100dvh;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen { display: none; }
.screen.active { display: block; }
.hidden { display: none !important; }

/* ── Auth ────────────────────────────────────────────────────────────────── */
#screen-auth {
  min-height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 32px 24px;
}

.auth-wrap {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  gap: 40px;
}

.auth-logo {
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.auth-logo-icon {
  width: 72px;
  height: 72px;
  border-radius: 20px;
  background: var(--accent);
  color: var(--bg);
  font-size: 2rem;
  font-weight: 800;
  display: flex;
  align-items: center;
  justify-content: center;
}

.auth-title {
  font-size: 1.8rem;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.auth-subtitle {
  font-size: 0.9rem;
  color: var(--muted);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ── Header ──────────────────────────────────────────────────────────────── */
#app-header {
  position: sticky;
  top: 0;
  z-index: 50;
  height: var(--header-h);
  background: var(--bg);
  border-bottom: 1px solid var(--border);
  padding-top: env(safe-area-inset-top, 0px);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  height: var(--header-h);
}

.header-logo {
  font-size: 1rem;
  font-weight: 700;
  color: var(--accent);
  letter-spacing: -0.01em;
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

.loan-select {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 0.85rem;
  height: 38px;
  max-width: 160px;
}

/* ── Buttons ─────────────────────────────────────────────────────────────── */
button, input, select, textarea {
  font-family: inherit;
  font-size: inherit;
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 44px;
  padding: 10px 20px;
  border-radius: var(--radius-sm);
  border: none;
  cursor: pointer;
  font-weight: 600;
  font-size: 1rem;
  transition: opacity 0.15s, transform 0.1s;
}
.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--accent);
  color: var(--bg);
}
.btn-primary:hover { opacity: 0.9; }

.btn-ghost {
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--border);
}
.btn-ghost:hover { background: var(--surface); color: var(--text); }

.btn-danger {
  background: var(--red);
  color: #fff;
}
.btn-danger:hover { opacity: 0.9; }

.btn-sm { min-height: 38px; padding: 6px 14px; font-size: 0.85rem; }
.btn-lg { width: 100%; min-height: 54px; font-size: 1.05rem; border-radius: var(--radius); }

/* ── Stat Grid ───────────────────────────────────────────────────────────── */
#screen-dashboard {
  padding: 20px 20px 0;
  padding-bottom: calc(var(--nav-h) + 20px);
}

.stat-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.stat-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 20px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.stat-card-wide {
  grid-column: 1 / -1;
  background: var(--accent-dim);
  border-color: rgba(56,189,248,0.25);
}

.stat-label {
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--muted);
  font-weight: 600;
}

.stat-value {
  font-size: clamp(2.2rem, 7vw, 3.6rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--accent);
  letter-spacing: -0.03em;
  line-height: 1;
}

.stat-value-md {
  font-size: clamp(1.4rem, 4.5vw, 2.2rem);
}

.stat-value.overdue { color: var(--red); }
.stat-value.due-soon { color: var(--orange); }
.stat-value.on-track { color: var(--green); }

/* ── Progress Bar ────────────────────────────────────────────────────────── */
.progress-section {
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.progress-bar-wrap { width: 100%; }

.progress-bar {
  width: 100%;
  height: 10px;
  background: var(--surface2);
  border-radius: 99px;
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  background: var(--accent);
  border-radius: 99px;
  transition: width 0.5s ease;
}

.progress-label {
  font-size: 0.82rem;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
}

/* ── Loan Meta ───────────────────────────────────────────────────────────── */
.loan-meta {
  margin-top: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px 20px;
  display: flex;
  flex-wrap: wrap;
  gap: 12px 24px;
  font-size: 0.85rem;
  color: var(--muted);
}

.loan-meta-item strong { color: var(--text); }

/* ── Forms ───────────────────────────────────────────────────────────────── */
.screen-inner {
  padding: 20px 20px 0;
  padding-bottom: calc(var(--nav-h) + 20px);
}

.screen-title {
  font-size: 1.4rem;
  font-weight: 700;
  margin-bottom: 20px;
  letter-spacing: -0.02em;
}

.form-card {
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.form-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 14px;
}

.form-label {
  font-size: 0.8rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--muted);
}

.form-input {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  min-height: 48px;
  font-size: 1rem;
  width: 100%;
  -webkit-appearance: none;
  appearance: none;
}

.form-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-dim);
}

.form-textarea {
  min-height: 80px;
  resize: vertical;
}

.input-prefix-wrap {
  position: relative;
}

.input-prefix {
  position: absolute;
  left: 14px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--muted);
  font-size: 1rem;
  pointer-events: none;
}

.input-prefixed {
  padding-left: 28px;
}

/* ── Payment Preview ─────────────────────────────────────────────────────── */
.payment-preview {
  background: var(--surface2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 14px 18px;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.preview-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
}

.preview-row span:first-child { color: var(--muted); }
.preview-row span:last-child { font-weight: 600; font-variant-numeric: tabular-nums; }

/* ── History ─────────────────────────────────────────────────────────────── */
.history-list {
  display: flex;
  flex-direction: column;
  gap: 0;
}

.history-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 16px;
  padding: 16px 0;
  border-bottom: 1px solid var(--border);
  position: relative;
}

.history-row:last-child { border-bottom: none; }

.h-date {
  font-size: 0.85rem;
  color: var(--muted);
  align-self: center;
}

.h-amount {
  font-size: 1.1rem;
  font-weight: 700;
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.h-split {
  font-size: 0.8rem;
  color: var(--muted);
  display: flex;
  gap: 12px;
}

.h-split-item { display: flex; gap: 4px; }
.h-split-label { font-weight: 600; }

.h-balance {
  font-size: 0.85rem;
  text-align: right;
  color: var(--muted);
  align-self: end;
}

.h-row-actions {
  position: absolute;
  right: -8px;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  gap: 2px;
}

.h-edit {
  background: transparent;
  border: none;
  color: var(--accent);
  font-size: 1rem;
  min-width: 36px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 50%;
  opacity: 0.5;
}
.h-edit:hover { opacity: 1; background: var(--accent-dim); }

.h-delete {
  background: transparent;
  border: none;
  color: var(--red);
  font-size: 1.1rem;
  min-width: 36px;
  min-height: 44px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  border-radius: 50%;
  opacity: 0.5;
}
.h-delete:hover { opacity: 1; background: rgba(248,113,113,0.1); }

.h-type-badge {
  font-size: 0.7rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 2px 7px;
  border-radius: 99px;
  display: inline-block;
}
.badge-scheduled { background: var(--accent-dim); color: var(--accent); }
.badge-extra     { background: rgba(74,222,128,0.15); color: var(--green); }
.badge-principal_only { background: rgba(251,146,60,0.15); color: var(--orange); }

/* ── Bottom Nav ──────────────────────────────────────────────────────────── */
#main-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  z-index: 100;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--border);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.nav-btn {
  flex: 1;
  background: transparent;
  border: none;
  color: var(--muted);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  min-height: 64px;
  cursor: pointer;
  transition: color 0.15s;
}

.nav-btn.active { color: var(--accent); }
.nav-btn:active { opacity: 0.7; }

.nav-icon { font-size: 1.3rem; line-height: 1; }
.nav-label { font-size: 0.7rem; font-weight: 600; letter-spacing: 0.04em; }

/* ── Modals ──────────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.modal-overlay:not(.hidden) { display: flex; }

.modal-box {
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  padding: 28px 24px;
  padding-bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  width: 100%;
  max-width: 600px;
  max-height: 90dvh;
  overflow-y: auto;
}

.modal-box-sm { max-height: 50dvh; }

.modal-title {
  font-size: 1.3rem;
  font-weight: 700;
  margin-bottom: 20px;
  letter-spacing: -0.02em;
}

.modal-body {
  color: var(--muted);
  margin-bottom: 24px;
  line-height: 1.5;
}

.modal-actions {
  display: flex;
  gap: 12px;
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

/* ── Misc ────────────────────────────────────────────────────────────────── */
.error-msg {
  color: var(--red);
  font-size: 0.875rem;
  text-align: center;
}

.empty-state {
  text-align: center;
  color: var(--muted);
  padding: 48px 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

/* ── iPad optimizations ──────────────────────────────────────────────────── */
@media (min-width: 768px) {
  :root { --header-h: 68px; }

  #screen-dashboard { padding: 28px 32px 0; padding-bottom: calc(var(--nav-h) + 28px); }
  .stat-grid { gap: 18px; }
  .stat-card { padding: 28px 24px; }

  .screen-inner { padding: 28px 32px 0; padding-bottom: calc(var(--nav-h) + 28px); }

  .form-card { max-width: 540px; }

  .modal-box {
    border-radius: var(--radius);
    margin: auto;
    margin-bottom: 5dvh;
  }

  .modal-overlay { align-items: center; }

  .header-inner { padding: 0 32px; }
}

@media (min-width: 1024px) {
  .stat-value { font-size: clamp(2.5rem, 4vw, 4rem); }
}
