:root {
  --bg: #f0fdfa;
  --surface: #ffffff;
  --primary: #0d9488;
  --primary-dark: #0f766e;
  --danger: #dc2626;
  --warning: #d97706;
  --text: #134e4a;
  --border: #99f6e4;
  --shadow: 0 4px 20px rgba(13, 148, 136, 0.12);
  --header-h: 56px;
  --sidebar-w: 260px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

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

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  min-height: 100dvh;
}

/* ---------- Auth ---------- */
.auth-page {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  padding-top: max(20px, env(safe-area-inset-top));
  padding-bottom: max(20px, var(--safe-bottom));
  min-height: 100vh;
  min-height: 100dvh;
  background: linear-gradient(135deg, #0d9488 0%, #0891b2 100%);
}

.auth-card {
  background: var(--surface);
  border-radius: 16px;
  padding: clamp(20px, 5vw, 32px);
  width: 100%;
  max-width: 420px;
  box-shadow: var(--shadow);
}

.auth-card h1 { color: var(--primary); margin-bottom: 8px; font-size: clamp(1.5rem, 5vw, 1.75rem); }
.subtitle { color: #64748b; margin-bottom: 24px; font-size: 14px; line-height: 1.5; }

/* ---------- Mobile header ---------- */
.mobile-header {
  display: none;
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  height: var(--header-h);
  padding: 0 12px;
  padding-top: env(safe-area-inset-top, 0);
  background: linear-gradient(90deg, #0f766e, #115e59);
  color: white;
  align-items: center;
  gap: 12px;
  z-index: 900;
  box-shadow: 0 2px 12px rgba(0,0,0,0.15);
}

.mobile-header-title {
  display: flex;
  flex-direction: column;
  line-height: 1.2;
  min-width: 0;
}

.mobile-header-title strong { font-size: 15px; }
.mobile-header-title span { font-size: 12px; opacity: 0.85; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.menu-toggle {
  width: 44px;
  height: 44px;
  border: none;
  background: rgba(255,255,255,0.12);
  border-radius: 10px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  flex-shrink: 0;
}

.menu-toggle span {
  display: block;
  width: 20px;
  height: 2px;
  background: white;
  border-radius: 2px;
  transition: transform 0.2s, opacity 0.2s;
}

.menu-toggle.open span:nth-child(1) { transform: translateY(7px) rotate(45deg); }
.menu-toggle.open span:nth-child(2) { opacity: 0; }
.menu-toggle.open span:nth-child(3) { transform: translateY(-7px) rotate(-45deg); }

.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 950;
  opacity: 0;
  transition: opacity 0.25s;
}

.sidebar-overlay.open { display: block; opacity: 1; }

/* ---------- Layout ---------- */
.app-layout { display: flex; min-height: 100vh; min-height: 100dvh; }

.sidebar {
  width: var(--sidebar-w);
  background: linear-gradient(180deg, #0f766e, #115e59);
  color: white;
  padding: 24px 16px;
  padding-bottom: max(24px, var(--safe-bottom));
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  z-index: 1000;
}

.sidebar-top {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.sidebar h2 { font-size: 18px; margin-bottom: 0; }
.sidebar-close { display: none; }

.sidebar .user-info {
  font-size: 12px;
  opacity: 0.85;
  margin: 12px 0 20px;
  word-break: break-word;
  line-height: 1.4;
}

.sidebar nav { flex: 1; overflow-y: auto; }

.nav-item {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  min-height: 44px;
  border-radius: 10px;
  color: white;
  text-decoration: none;
  margin-bottom: 4px;
  cursor: pointer;
  transition: background 0.2s;
  -webkit-tap-highlight-color: transparent;
}

.nav-item:hover, .nav-item.active { background: rgba(255,255,255,0.15); }

.btn-logout {
  margin-top: 16px;
  background: rgba(255,255,255,0.15) !important;
  color: white !important;
  border: 1px solid rgba(255,255,255,0.25);
}

.main {
  flex: 1;
  padding: 24px;
  padding-bottom: max(24px, var(--safe-bottom));
  overflow-x: hidden;
  min-width: 0;
}

.page { display: none; }
.page.active { display: block; }

.page-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  margin-bottom: 20px;
  flex-wrap: wrap;
  gap: 12px;
}

.page-header h1 {
  font-size: clamp(1.25rem, 4vw, 1.5rem);
  color: var(--primary-dark);
  line-height: 1.3;
}

.page-header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  width: 100%;
}

@media (min-width: 480px) {
  .page-header-actions { width: auto; }
}

.page-header-actions .form-input { min-width: 140px; flex: 1; }
@media (min-width: 480px) {
  .page-header-actions .form-input { flex: none; width: auto; }
}

/* ---------- Cards & grid ---------- */
.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(min(100%, 200px), 1fr));
  gap: 12px;
  margin-bottom: 20px;
}

.card {
  background: var(--surface);
  border-radius: 12px;
  padding: clamp(14px, 3vw, 20px);
  box-shadow: var(--shadow);
  border: 1px solid var(--border);
}

.card h3 {
  font-size: 12px;
  color: #64748b;
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.card .value {
  font-size: clamp(1.25rem, 4.5vw, 1.75rem);
  font-weight: 700;
  color: var(--primary-dark);
  word-break: break-word;
}

.card .sub { font-size: 12px; color: #94a3b8; margin-top: 4px; line-height: 1.4; }

.card-section { margin-bottom: 16px; }
.card-section-title { margin-bottom: 12px; font-size: 14px; font-weight: 600; color: var(--primary-dark); }

.settings-stack { display: flex; flex-direction: column; gap: 16px; max-width: 600px; }

/* ---------- Forms ---------- */
.form-group { margin-bottom: 16px; }
.form-group label { display: block; font-size: 13px; font-weight: 600; margin-bottom: 6px; }

.form-input, select.form-input, textarea.form-input {
  width: 100%;
  padding: 12px;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 8px;
  font-size: 16px;
  background: white;
}

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

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

.rate-row {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 10px;
}

.rate-row label { font-size: 13px; font-weight: 600; white-space: nowrap; }

/* ---------- Buttons ---------- */
.btn {
  padding: 12px 18px;
  min-height: 44px;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 14px;
  font-weight: 600;
  transition: opacity 0.2s, transform 0.1s;
  -webkit-tap-highlight-color: transparent;
  white-space: nowrap;
}

.btn:active { transform: scale(0.98); }
.btn:hover { opacity: 0.92; }
.btn-primary { background: var(--primary); color: white; }
.btn-secondary { background: #e2e8f0; color: #334155; }
.btn-danger { background: var(--danger); color: white; }
.btn-sm { padding: 8px 12px; min-height: 36px; font-size: 12px; }
.btn-block { width: 100%; }

.row-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

/* ---------- Tables (desktop) ---------- */
.table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  background: var(--surface);
  border-radius: 12px;
  box-shadow: var(--shadow);
}

table { width: 100%; border-collapse: collapse; font-size: 14px; min-width: 520px; }

th, td {
  padding: 12px 14px;
  text-align: left;
  border-bottom: 1px solid #f1f5f9;
  vertical-align: middle;
}

th {
  background: #f8fafc;
  font-weight: 600;
  color: #64748b;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.3px;
  white-space: nowrap;
}

tr:hover td { background: #f8fafc; }
tr:last-child td { border-bottom: none; }

/* ---------- Modal ---------- */
.modal-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  align-items: flex-end;
  justify-content: center;
  z-index: 1100;
  padding: 0;
}

.modal-overlay.open { display: flex; }

.modal {
  background: white;
  border-radius: 16px 16px 0 0;
  padding: 20px;
  padding-bottom: max(20px, var(--safe-bottom));
  width: 100%;
  max-width: 520px;
  max-height: 92vh;
  max-height: 92dvh;
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}

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

.modal h2 {
  margin-bottom: 16px;
  color: var(--primary-dark);
  font-size: 1.15rem;
  padding-right: 8px;
}

.modal-actions {
  display: flex;
  flex-direction: column-reverse;
  gap: 8px;
  margin-top: 20px;
}

.modal-actions .btn { width: 100%; }

/* ---------- Misc ---------- */
.error-msg { background: #fef2f2; color: var(--danger); padding: 12px; border-radius: 8px; margin-bottom: 16px; font-size: 14px; }
.success-msg { background: #ecfdf5; color: var(--primary-dark); padding: 12px; border-radius: 8px; margin-bottom: 16px; }

.badge {
  display: inline-block;
  padding: 4px 10px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: capitalize;
}

.badge-income { background: #d1fae5; color: #065f46; }
.badge-expense { background: #fee2e2; color: #991b1b; }

.progress-bar { height: 8px; background: #e2e8f0; border-radius: 4px; overflow: hidden; margin-top: 8px; }
.progress-fill { height: 100%; background: var(--primary); border-radius: 4px; transition: width 0.3s; }
.progress-fill.over { background: var(--danger); }

.empty { text-align: center; padding: 32px 16px; color: #94a3b8; }

.value-success { color: #059669 !important; }
.value-danger { color: var(--danger) !important; }

.goal-card-actions { margin-top: 12px; display: flex; gap: 8px; flex-wrap: wrap; }

.chart-card { margin-bottom: 20px; }
.chart-wrap { position: relative; height: 280px; max-width: 100%; }

.budget-bar { margin-bottom: 12px; }
.budget-bar-header { display: flex; justify-content: space-between; font-size: 13px; margin-bottom: 4px; }
.budget-bar-header .over { color: var(--danger); font-weight: 600; }

.loan-type-badge {
  display: inline-block; font-size: 10px; padding: 2px 8px; border-radius: 999px;
  background: #e0f2fe; color: #0369a1; text-transform: uppercase; font-weight: 600;
}

.split-preview {
  background: #f0fdfa; border: 1px solid var(--border); border-radius: 8px;
  padding: 12px; margin-bottom: 16px; font-size: 13px;
}

.tabs { display: flex; gap: 8px; margin-bottom: 16px; flex-wrap: wrap; }
.tab-btn {
  padding: 8px 14px; border-radius: 8px; border: 1px solid var(--border);
  background: white; cursor: pointer; font-size: 13px; font-weight: 600;
}
.tab-btn.active { background: var(--primary); color: white; border-color: var(--primary); }

/* ---------- Tablet ---------- */
@media (max-width: 900px) {
  .grid { grid-template-columns: repeat(2, 1fr); }
}

/* ---------- Mobile ---------- */
@media (max-width: 768px) {
  .mobile-header { display: flex; }

  .app-layout { padding-top: calc(var(--header-h) + env(safe-area-inset-top, 0px)); }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    transform: translateX(-105%);
    transition: transform 0.28s ease;
    box-shadow: 4px 0 24px rgba(0,0,0,0.2);
    padding-top: max(20px, env(safe-area-inset-top));
  }

  .sidebar.open { transform: translateX(0); }
  .sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 36px;
    height: 36px;
    border: none;
    background: rgba(255,255,255,0.15);
    color: white;
    border-radius: 8px;
    cursor: pointer;
    font-size: 18px;
    flex-shrink: 0;
  }

  .main { padding: 16px; padding-bottom: max(16px, var(--safe-bottom)); }

  .page-header { flex-direction: column; align-items: stretch; }
  .page-header .btn-primary { width: 100%; }

  .grid { grid-template-columns: 1fr; }

  /* Stacked table cards */
  .table-wrap { background: transparent; box-shadow: none; overflow: visible; }
  .table-wrap table { min-width: 0; }
  .table-wrap thead { display: none; }

  .table-wrap tbody tr {
    display: block;
    background: var(--surface);
    border-radius: 12px;
    padding: 12px 14px;
    margin-bottom: 10px;
    box-shadow: var(--shadow);
    border: 1px solid var(--border);
  }

  .table-wrap tbody tr:hover td { background: transparent; }

  .table-wrap td {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 12px;
    padding: 8px 0;
    border: none;
    font-size: 14px;
  }

  .table-wrap td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #64748b;
    font-size: 11px;
    text-transform: uppercase;
    flex-shrink: 0;
    max-width: 42%;
  }

  .table-wrap td[data-label=""]::before,
  .table-wrap td:not([data-label])::before { display: none; }

  .table-wrap td.actions-cell {
    justify-content: flex-end;
    padding-top: 12px;
    border-top: 1px solid #f1f5f9;
    margin-top: 4px;
  }

  .table-wrap td.actions-cell::before { display: none; }

  .table-wrap tr.empty-row td {
    display: block;
    text-align: center;
  }

  .table-wrap tr.empty-row td::before { display: none; }

  .form-row { grid-template-columns: 1fr; }

  .modal-overlay { align-items: flex-end; }

  .modal {
    border-radius: 16px 16px 0 0;
    max-height: 88dvh;
  }

  @media (min-width: 480px) {
    .modal-overlay { align-items: center; padding: 16px; }
    .modal { border-radius: 16px; max-height: 90dvh; }
    .modal-actions { flex-direction: row; justify-content: flex-end; }
    .modal-actions .btn { width: auto; }
  }
}

@media (min-width: 769px) {
  .modal-overlay { align-items: center; padding: 20px; }
  .modal { border-radius: 16px; animation: none; }
  .modal-actions { flex-direction: row; justify-content: flex-end; }
  .modal-actions .btn { width: auto; }
}

@media (max-width: 380px) {
  .card .value { font-size: 1.15rem; }
  .btn-sm { padding: 8px 10px; }
}
