/* style.css — Mobile-first loan tracker UI */

/* ─── Reset & Base ─────────────────────────────────────────────────────────── */

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

:root {
  --blue:        #2c7be5;
  --blue-dark:   #1a5bb8;
  --blue-light:  #e8f1fd;
  --green:       #27ae60;
  --green-light: #e9f7ef;
  --red:         #e74c3c;
  --red-light:   #fdecea;
  --yellow:      #f39c12;
  --gray-50:     #f8f9fa;
  --gray-100:    #f0f2f5;
  --gray-200:    #e1e5ea;
  --gray-400:    #9aa5b4;
  --gray-600:    #5a6473;
  --gray-800:    #2d3748;
  --gray-900:    #1a202c;
  --white:       #ffffff;
  --radius:      12px;
  --radius-sm:   8px;
  --shadow:      0 2px 8px rgba(0,0,0,0.08);
  --shadow-md:   0 4px 16px rgba(0,0,0,0.12);
  --font:        -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --transition:  0.2s ease;
}

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

body {
  font-family: var(--font);
  background: var(--gray-100);
  color: var(--gray-900);
  min-height: 100dvh;
  line-height: 1.5;
}

a {
  color: var(--blue);
  text-decoration: none;
}

ul {
  list-style: none;
}

button {
  font-family: var(--font);
  font-size: 1rem;
  cursor: pointer;
  border: none;
  background: none;
}

input, select, textarea {
  font-family: var(--font);
  font-size: 1rem;
}

/* ─── App Shell ────────────────────────────────────────────────────────────── */

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  background: var(--white);
  position: relative;
}

/* ─── Header ───────────────────────────────────────────────────────────────── */

.app-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  background: var(--white);
  border-bottom: 1px solid var(--gray-200);
  position: sticky;
  top: 0;
  z-index: 10;
  min-height: 56px;
}

.app-header__left,
.app-header__right {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 80px;
}

.app-header__right {
  justify-content: flex-end;
}

.app-header__title {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--gray-900);
  text-align: center;
  flex: 1;
}

/* ─── Buttons ──────────────────────────────────────────────────────────────── */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 0 20px;
  height: 48px;
  border-radius: var(--radius);
  font-size: 1rem;
  font-weight: 600;
  transition: background var(--transition), opacity var(--transition);
  -webkit-tap-highlight-color: transparent;
  cursor: pointer;
  border: none;
  white-space: nowrap;
}

.btn--primary {
  background: var(--blue);
  color: var(--white);
}
.btn--primary:active { background: var(--blue-dark); }

.btn--secondary {
  background: var(--gray-100);
  color: var(--gray-800);
}
.btn--secondary:active { background: var(--gray-200); }

.btn--danger {
  background: var(--red);
  color: var(--white);
}
.btn--danger:active { opacity: 0.85; }

.btn--ghost {
  background: transparent;
  color: var(--blue);
  padding: 0 12px;
  height: 44px;
  font-weight: 500;
}
.btn--ghost:active { background: var(--blue-light); border-radius: var(--radius-sm); }

.btn--full {
  width: 100%;
}

.btn--sm {
  height: 36px;
  padding: 0 14px;
  font-size: 0.875rem;
}

.btn--icon {
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  background: var(--gray-100);
  color: var(--gray-800);
  font-size: 1.25rem;
}
.btn--icon:active { background: var(--gray-200); }

/* ─── View containers ──────────────────────────────────────────────────────── */

.view[hidden] {
  display: none;
}

.view {
  flex: 1;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.view-content {
  flex: 1;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* ─── Status Badges ────────────────────────────────────────────────────────── */

.status-badge {
  display: inline-block;
  padding: 2px 10px;
  border-radius: 20px;
  font-size: 0.75rem;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.status-badge--active {
  background: var(--blue-light);
  color: var(--blue-dark);
}

.status-badge--paid {
  background: var(--green-light);
  color: var(--green);
}

/* ─── Balance color coding ─────────────────────────────────────────────────── */

.balance--high  { color: var(--red);   }
.balance--low   { color: var(--green); }
.balance--zero  { color: var(--green); }

/* ─── Loan Detail View ─────────────────────────────────────────────────────── */

.detail-card {
  background: var(--white);
  border-radius: var(--radius);
  border: 1px solid var(--gray-200);
  padding: 16px;
  box-shadow: var(--shadow);
}

.detail-card__header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 12px;
}

.detail-card__title {
  font-size: 1.25rem;
  font-weight: 700;
  color: var(--gray-900);
  line-height: 1.3;
}

.detail-card__borrower {
  font-size: 0.9rem;
  color: var(--gray-600);
  margin-top: 2px;
}

.detail-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-top: 4px;
}

.detail-stat {
  background: var(--gray-50);
  border-radius: var(--radius-sm);
  padding: 12px;
}

.detail-stat__label {
  font-size: 0.75rem;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-weight: 500;
}

.detail-stat__value {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--gray-900);
  margin-top: 2px;
}

.detail-stat--balance .detail-stat__value {
  font-size: 1.3rem;
}

.detail-balance { font-weight: 700; }

.detail-interest-note {
  font-size: 0.8rem;
  color: var(--gray-600);
  margin-top: 8px;
  padding: 8px;
  background: var(--gray-50);
  border-radius: var(--radius-sm);
}

/* ─── Record Payment button area ───────────────────────────────────────────── */

.detail-actions {
  display: flex;
  gap: 10px;
  flex-direction: column;
}

/* ─── Payment List ─────────────────────────────────────────────────────────── */

.section-title {
  font-size: 0.8rem;
  font-weight: 600;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  padding: 0 4px;
}

.payment-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.payment-list__empty {
  text-align: center;
  color: var(--gray-400);
  font-size: 0.9rem;
  padding: 24px 0;
}

.payment-item {
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  transition: background var(--transition);
}

.payment-item:active {
  background: var(--gray-50);
}

.payment-item__top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.payment-item__date {
  font-size: 0.875rem;
  color: var(--gray-600);
}

.payment-item__amount {
  font-size: 1rem;
  font-weight: 700;
  color: var(--gray-900);
}

.payment-item__note {
  font-size: 0.8rem;
  color: var(--gray-600);
  margin-top: 4px;
  font-style: italic;
}

.payment-item__running {
  font-size: 0.8rem;
  color: var(--gray-400);
  margin-top: 4px;
}

.payment-item__running strong {
  color: var(--gray-600);
}

/* ─── Loan List View ───────────────────────────────────────────────────────── */

.loans-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.loan-list__group-header {
  font-size: 0.75rem;
  font-weight: 700;
  color: var(--gray-600);
  text-transform: uppercase;
  letter-spacing: 0.07em;
  padding: 4px 4px 0;
  margin-top: 8px;
}

.loan-list__group-header:first-child {
  margin-top: 0;
}

.loan-list__group-header--paid {
  color: var(--green);
}

.loan-list__empty {
  text-align: center;
  color: var(--gray-400);
  font-size: 0.95rem;
  padding: 40px 16px;
  line-height: 1.6;
}

.loan-item {
  background: var(--white);
  border: 1px solid var(--gray-200);
  border-radius: var(--radius);
  padding: 14px 16px;
  cursor: pointer;
  transition: background var(--transition), transform var(--transition);
  -webkit-tap-highlight-color: transparent;
  box-shadow: var(--shadow);
}

.loan-item:active {
  background: var(--gray-50);
  transform: scale(0.99);
}

.loan-item__main {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.loan-item__name {
  font-size: 1rem;
  font-weight: 600;
  color: var(--gray-900);
}

.loan-item__sub {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.loan-item__borrower {
  font-size: 0.875rem;
  color: var(--gray-600);
}

.loan-item__amounts {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 0.875rem;
}

.loan-item__principal {
  color: var(--gray-600);
}

.loan-item__sep {
  color: var(--gray-400);
  font-size: 0.75rem;
}

.loan-item__balance {
  font-weight: 700;
  color: var(--gray-900);
}

/* ─── Forms ────────────────────────────────────────────────────────────────── */

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

.form-field {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-field__label {
  font-size: 0.875rem;
  font-weight: 600;
  color: var(--gray-800);
}

.form-field__label span {
  color: var(--red);
  margin-left: 2px;
}

.form-field__input,
.form-field__select,
.form-field__textarea {
  width: 100%;
  padding: 12px 14px;
  border: 1.5px solid var(--gray-200);
  border-radius: var(--radius-sm);
  font-size: 1rem;
  color: var(--gray-900);
  background: var(--white);
  -webkit-appearance: none;
  appearance: none;
  transition: border-color var(--transition);
  min-height: 48px;
}

.form-field__input:focus,
.form-field__select:focus,
.form-field__textarea:focus {
  outline: none;
  border-color: var(--blue);
  box-shadow: 0 0 0 3px rgba(44,123,229,0.15);
}

.form-field__textarea {
  min-height: 80px;
  resize: vertical;
  line-height: 1.5;
}

.form-field__select {
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='16' height='16' viewBox='0 0 24 24' fill='none' stroke='%239aa5b4' stroke-width='2'%3E%3Cpath d='M6 9l6 6 6-6'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 12px center;
  padding-right: 40px;
}

.form-hint {
  font-size: 0.8rem;
  color: var(--gray-400);
}

.form-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 4px;
}

.form-section-divider {
  border: none;
  border-top: 1px solid var(--gray-200);
  margin: 4px 0;
}

/* ─── Payment Modal ────────────────────────────────────────────────────────── */

.modal-backdrop[hidden] {
  display: none;
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 100;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal-sheet {
  background: var(--white);
  border-radius: var(--radius) var(--radius) 0 0;
  width: 100%;
  max-width: 480px;
  padding: 0 0 env(safe-area-inset-bottom, 16px);
  animation: slideUp 0.28s ease;
  max-height: 85dvh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 16px 12px;
  border-bottom: 1px solid var(--gray-200);
}

.modal-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--gray-900);
}

.modal-body {
  padding: 16px;
}

/* ─── Sync Status ──────────────────────────────────────────────────────────── */

.sync-status {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  display: inline-block;
  flex-shrink: 0;
}

.sync-status--synced  { background: var(--green); }
.sync-status--pending { background: var(--yellow); }
.sync-status--offline { background: var(--gray-400); }

/* ─── Restore overlay ──────────────────────────────────────────────────────── */

.restore-overlay[hidden] {
  display: none;
}

.restore-overlay {
  position: fixed;
  inset: 0;
  background: var(--white);
  z-index: 200;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;
  text-align: center;
}

.restore-overlay__spinner {
  width: 40px;
  height: 40px;
  border: 4px solid var(--gray-200);
  border-top-color: var(--blue);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.restore-overlay__text {
  color: var(--gray-600);
  font-size: 0.95rem;
}

.restore-error[hidden] {
  display: none;
}

.restore-error {
  position: fixed;
  inset: 0;
  z-index: 200;
  background: var(--white);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
  padding: 32px;
  text-align: center;
}

.restore-error__icon {
  font-size: 2.5rem;
}

.restore-error__msg {
  font-size: 0.95rem;
  color: var(--gray-600);
  line-height: 1.6;
}

/* ─── Toast ────────────────────────────────────────────────────────────────── */

#toast-container {
  position: fixed;
  bottom: calc(24px + env(safe-area-inset-bottom, 0px));
  left: 50%;
  transform: translateX(-50%);
  z-index: 300;
  display: flex;
  flex-direction: column;
  gap: 8px;
  pointer-events: none;
  width: calc(100% - 32px);
  max-width: 400px;
}

.toast {
  background: var(--gray-900);
  color: var(--white);
  border-radius: var(--radius-sm);
  padding: 12px 16px;
  font-size: 0.9rem;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  opacity: 0;
  transform: translateY(8px);
  transition: opacity 0.25s ease, transform 0.25s ease;
  text-align: center;
}

.toast--visible {
  opacity: 1;
  transform: translateY(0);
}

.toast--error {
  background: var(--red);
}

/* ─── Divider ──────────────────────────────────────────────────────────────── */

.divider {
  border: none;
  border-top: 1px solid var(--gray-200);
}

/* ─── List view footer ─────────────────────────────────────────────────────── */

.list-footer {
  padding: 8px 16px 16px;
  display: flex;
  justify-content: center;
}

/* ─── Safe area padding for bottom actions ─────────────────────────────────── */

.bottom-safe {
  padding-bottom: env(safe-area-inset-bottom, 16px);
}

/* ─── Scrollable payment section ───────────────────────────────────────────── */

.payments-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1;
}
