/* Personal Income & Expense Tracker: navy, white/grey, green buttons. */
:root {
  --navy: #0b2a4a;
  --navy-2: #1a4474;
  --navy-tint: #e7edf5;
  --green: #1f9d55;
  --green-dark: #167a42;
  --green-tint: #e6f5ec;
  --red: #b83227;
  --red-tint: #fbebe9;
  --bg: #f2f4f7;
  --card: #ffffff;
  --text: #1c2733;
  --muted: #5f6b7a;
  --border: #dde3ea;
  --radius: 10px;
}
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  color: var(--text);
  background: var(--bg);
  /* PWA enhancement: prevents overscroll bounce on iOS when installed */
  overscroll-behavior-y: none; 
}
h1 { font-size: 1.6rem; line-height: 1.25; margin: 0; color: var(--navy); }
h2 { font-size: 1.1rem; margin: 0 0 12px; color: var(--navy); }
a { color: var(--navy-2); }
.icon { width: 1.15em; height: 1.15em; flex: none; vertical-align: -0.17em; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.muted { color: var(--muted); }
.sub { margin: 4px 0 0; color: var(--muted); }
.hint { margin: 12px 0 0; color: var(--muted); font-size: 0.9rem; }
.empty { margin: 0; padding: 18px 0; color: var(--muted); }
:focus-visible { outline: 3px solid #7aa7d9; outline-offset: 2px; }

/* ---------- Layout ---------- */
.app { display: flex; min-height: 100vh; }
.topbar { display: none; }
.overlay { display: none; position: fixed; top: 0; right: 0; bottom: 0; left: 0; background: rgba(7, 28, 51, 0.55); z-index: 40; }
.sidebar {
  width: 250px; flex: none; background: var(--navy); color: #cfdbea;
  display: flex; flex-direction: column; padding: 20px 14px;
  position: sticky; top: 0; height: 100vh; overflow-y: auto;
}
.brand { display: flex; align-items: center; gap: 12px; padding: 4px 10px 24px; color: #fff; font-weight: 700; font-size: 1.1rem; }
.brand img { width: 32px; height: 32px; display: block; }
.sidebar nav { display: flex; flex-direction: column; gap: 2px; }
.nav-link {
  display: flex; align-items: center; gap: 12px; width: 100%;
  padding: 11px 12px; border: 0; border-radius: 8px; background: none;
  color: #cfdbea; font: inherit; font-weight: 500; text-align: left; text-decoration: none; cursor: pointer;
}
.nav-link:hover { background: rgba(255, 255, 255, 0.09); color: #fff; }
.nav-link.active { background: rgba(255, 255, 255, 0.15); color: #fff; box-shadow: inset 3px 0 0 #6fdc9e; }
.sidebar-foot { margin-top: auto; padding-top: 12px; border-top: 1px solid rgba(255, 255, 255, 0.14); }
.sidebar-foot form { margin: 0; }
.who { margin: 0 0 6px; padding: 0 12px; font-size: 0.85rem; color: #9db2c9; }
.content { flex: 1; min-width: 0; width: 100%; max-width: 1180px; margin: 0 auto; padding: 28px; }
.footer { padding: 8px 0 4px; text-align: center; font-size: 0.85rem; color: var(--muted); }

.page-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 20px; }
.btn-row { display: flex; flex-wrap: wrap; gap: 10px; align-items: center; }
.page-head + .btn-row, .btn-row.no-print { margin-bottom: 20px; }
.report-month { margin: 0 0 12px; font-size: 1.25rem; }

/* ---------- Cards ---------- */
.card {
  background: var(--card); border: 1px solid var(--border); border-radius: var(--radius);
  padding: 20px; margin-bottom: 20px; box-shadow: 0 1px 2px rgba(16, 24, 40, 0.05);
}
.card-head { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 10px; margin-bottom: 12px; }
.card-head h2 { margin: 0; }
.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(230px, 1fr)); gap: 16px; margin-bottom: 20px; }
.cards .card { margin: 0; }
.stat { display: flex; align-items: center; gap: 14px; }
.stat-icon { width: 46px; height: 46px; flex: none; display: grid; place-items: center; border-radius: 50%; font-size: 1.2rem; }
.stat-income .stat-icon { background: var(--green-tint); color: var(--green-dark); }
.stat-expense .stat-icon { background: var(--red-tint); color: var(--red); }
.stat-savings .stat-icon { background: var(--navy-tint); color: var(--navy-2); }
.stat p { margin: 0; }
.stat-label { font-size: 0.9rem; font-weight: 600; color: var(--muted); }
.stat-value { font-size: 1.45rem; font-weight: 700; line-height: 1.3; color: var(--navy); font-variant-numeric: tabular-nums; word-break: break-word; }
.stat-note { font-size: 0.82rem; color: var(--muted); }
.stat-balance { background: var(--navy); border-color: var(--navy); }
.stat-balance .stat-icon { background: rgba(255, 255, 255, 0.13); color: #6fdc9e; }
.stat-balance .stat-label, .stat-balance .stat-note { color: #bccbdd; }
.stat-balance .stat-value { color: #fff; }
.stat-balance .stat-value.negative { color: #ffb8ae; }

/* ---------- Chart (SVG Line Chart) ---------- */
.legend { display: flex; gap: 16px; font-size: 0.9rem; color: var(--muted); }
.sw { display: inline-block; width: 12px; height: 12px; margin-right: 6px; border-radius: 3px; vertical-align: -1px; }
.sw-income, .bar-income { background: var(--green); }
.sw-expense, .bar-expense { background: var(--navy); }
.chart { display: flex; gap: 10px; padding-top: 8px; }
.chart-col { flex: 1; min-width: 0; }
.bars { display: flex; align-items: flex-end; justify-content: center; gap: 4px; height: 170px; border-bottom: 1px solid var(--border); }
.bar { width: 38%; max-width: 30px; border-radius: 4px 4px 0 0; }
.chart-label { display: block; margin-top: 6px; text-align: center; font-size: 0.82rem; color: var(--muted); }

/* New SVG Chart Styles */
.chart-container { width: 100%; overflow-x: auto; padding-top: 1rem; }
.trend-svg { width: 100%; height: auto; min-width: 500px; display: block; }
.chart-legend { display: flex; gap: 1rem; font-size: 0.875rem; flex-wrap: wrap; }
.legend-item { display: flex; align-items: center; gap: 0.4rem; }
.legend-dot { width: 12px; height: 12px; border-radius: 3px; }

/* ---------- Buttons ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  padding: 10px 18px; border: 1px solid transparent; border-radius: 8px;
  font: inherit; font-weight: 600; line-height: 1.2; text-decoration: none; cursor: pointer;
}
.btn-green { background: var(--green); color: #fff; }
.btn-green:hover { background: var(--green-dark); }
.btn-outline { background: #fff; border-color: var(--border); color: var(--text); }
.btn-outline:hover { background: var(--navy-tint); border-color: #c3cfdd; }
.btn-danger { background: #fff; border-color: #e8b9b4; color: var(--red); }
.btn-danger:hover { background: var(--red); border-color: var(--red); color: #fff; }
.btn-sm { padding: 7px 12px; font-size: 0.9rem; }
.btn-icon { width: 36px; height: 36px; padding: 0; }
.btn-block { width: 100%; }

/* ---------- Forms ---------- */
.field { min-width: 0; }
.field label { display: block; margin-bottom: 5px; font-weight: 600; font-size: 0.92rem; }
input[type="text"], input[type="password"], input[type="number"], input[type="date"], input[type="search"], select, textarea {
  width: 100%; padding: 10px 12px; border: 1px solid #c5cdd8; border-radius: 8px;
  background: #fff; color: var(--text); font: inherit; font-size: 16px;
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--navy-2); box-shadow: 0 0 0 3px rgba(26, 68, 116, 0.18); }
textarea { resize: vertical; }
.form-grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 16px 20px; }
.form-grid .full { grid-column: 1 / -1; }
.filters { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 14px; align-items: end; }
.filter-form { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filter-form select { width: auto; padding: 8px 10px; }
.inline-form { display: flex; flex-wrap: wrap; gap: 14px; align-items: end; }
.inline-form .field { flex: 1 1 220px; }
.field.check label { display: flex; align-items: center; gap: 8px; margin: 0; padding: 10px 0; font-weight: 500; }
.field.check input { width: 18px; height: 18px; }

/* ---------- Messages ---------- */
.alert { margin-bottom: 16px; padding: 12px 14px; border: 1px solid; border-radius: 8px; }
.alert-success { background: var(--green-tint); border-color: #b5e0c6; color: #11553a; }
.alert-error { background: var(--red-tint); border-color: #f0bdb7; color: #86281e; }
.alert ul { margin: 0; padding-left: 20px; }

/* ---------- Tables ---------- */
.table-wrap { overflow-x: auto; }
.table { width: 100%; border-collapse: collapse; font-size: 0.95rem; }
.table th { padding: 10px 12px; border-bottom: 2px solid var(--border); text-align: left; font-size: 0.85rem; font-weight: 600; color: var(--muted); white-space: nowrap; }
.table td { padding: 12px; border-bottom: 1px solid var(--border); vertical-align: middle; }
.table tbody tr:hover { background: #f7f9fb; }
.table .num { text-align: right; white-space: nowrap; font-variant-numeric: tabular-nums; }
.table th.num { text-align: right; }
.table .actions-col { text-align: right; }
.table tfoot td { background: #f7f9fb; font-weight: 700; }
.amt-income { color: var(--green-dark); font-weight: 600; }
.amt-expense { color: var(--red); font-weight: 600; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 999px; font-size: 0.8rem; font-weight: 600; white-space: nowrap; }
.badge-income { background: var(--green-tint); color: var(--green-dark); }
.badge-expense { background: var(--red-tint); color: var(--red); }
.badge-saving, .badge-custom { background: var(--navy-tint); color: var(--navy-2); }
.row-actions { display: flex; gap: 6px; justify-content: flex-end; }
.row-actions form, td form { margin: 0; }
.chip { padding: 3px 14px; border: 1px solid var(--border); border-radius: 999px; font: inherit; font-size: 0.85rem; font-weight: 600; cursor: pointer; }
.chip-on { background: var(--green-tint); border-color: #b5e0c6; color: var(--green-dark); }
.chip-off { background: #fff; color: var(--muted); }
.share { display: flex; align-items: center; gap: 10px; min-width: 150px; }
.share-bar { flex: 1; height: 8px; background: #e8edf3; border-radius: 4px; overflow: hidden; }
.share-bar span { display: block; height: 100%; background: var(--navy); }
.share-pct { width: 52px; text-align: right; font-variant-numeric: tabular-nums; }
.summary { display: flex; flex-wrap: wrap; gap: 4px 22px; margin: 0; color: var(--muted); }
.summary strong { color: var(--text); }
.pagination { display: flex; align-items: center; justify-content: center; gap: 14px; margin-top: 16px; color: var(--muted); }

/* ---------- Details dialog & Modals ---------- */
dialog { width: min(480px, 92vw); padding: 0; border: 0; border-radius: 12px; box-shadow: 0 12px 40px rgba(0, 0, 0, 0.3); color: var(--text); }
dialog::backdrop { background: rgba(7, 28, 51, 0.55); }
.dialog-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 18px; background: var(--navy); color: #fff; }
.dialog-close { display: grid; place-items: center; width: 32px; height: 32px; border: 0; border-radius: 6px; background: transparent; color: #fff; cursor: pointer; }
.dialog-close:hover { background: rgba(255, 255, 255, 0.15); }
.dialog-body { padding: 18px; }
.detail { display: grid; grid-template-columns: 120px 1fr; gap: 10px 14px; margin: 0; }
.detail dt { font-weight: 600; color: var(--muted); }
.detail dd { margin: 0; white-space: pre-wrap; word-break: break-word; }

/* Custom Add/Edit Modal */
dialog.modal { width: min(560px, 92vw); }
dialog.modal .modal-head { display: flex; align-items: center; justify-content: space-between; padding: 1rem 1.25rem; border-bottom: 1px solid var(--border); }
dialog.modal .modal-head h2 { margin: 0; font-size: 1.1rem; }
dialog.modal .modal-body { padding: 1.25rem; }
dialog.modal .modal-foot { display: flex; gap: .5rem; justify-content: flex-end; padding: 1rem 1.25rem; border-top: 1px solid var(--border); background: #f9fafb; border-radius: 0 0 12px 12px; }
dialog.modal .close-x { background: none; border: 0; font-size: 1.4rem; line-height: 1; cursor: pointer; color: var(--muted); }

/* ---------- Login / setup ---------- */
.auth-body { display: grid; place-items: center; min-height: 100vh; padding: 16px; }
.auth-card { width: 100%; max-width: 400px; padding: 28px; background: #fff; border: 1px solid var(--border); border-radius: 12px; box-shadow: 0 4px 16px rgba(16, 24, 40, 0.08); }
.auth-card h1 { margin-bottom: 16px; font-size: 1.4rem; }
.auth-card .field { margin-bottom: 14px; }
.auth-brand { display: flex; flex-direction: column; align-items: center; gap: 10px; margin-bottom: 24px; color: var(--navy); font-weight: 700; font-size: 1.2rem; }
.auth-brand img { width: 48px; height: 48px; display: block; }

.print-only { display: none; }

/* ---------- Tablet and phone ---------- */
@media (max-width: 900px) {
  .topbar { display: flex; align-items: center; gap: 12px; position: sticky; top: 0; z-index: 30; padding: 10px 14px; background: var(--navy); color: #fff; }
  .menu-btn { display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 8px; background: rgba(255, 255, 255, 0.12); color: #fff; font-size: 1.2rem; cursor: pointer; }
  .topbar-title { font-weight: 700; }
  .sidebar { position: fixed; top: 0; left: 0; bottom: 0; z-index: 50; height: 100%; transform: translateX(-100%); }
  .sidebar.open { transform: none; }
  .overlay.show { display: block; }
  .content { padding: 16px; }
  h1 { font-size: 1.4rem; }
}
@media (max-width: 640px) {
  .form-grid { grid-template-columns: 1fr; }
  .card { padding: 16px; }
  .detail { grid-template-columns: 1fr; gap: 2px 0; }
  .detail dd { margin-bottom: 10px; }
}
/* Tables turn into stacked cards on phones. */
@media screen and (max-width: 720px) {
  .table.responsive thead { display: none; }
  .table.responsive, .table.responsive tbody, .table.responsive tfoot, .table.responsive tr, .table.responsive td { display: block; width: 100%; }
  .table.responsive tr { margin-bottom: 10px; padding: 6px 12px; border: 1px solid var(--border); border-radius: 8px; background: #fff; }
  .table.responsive td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 7px 0; border: 0; text-align: right; }
  .table.responsive td[data-label]::before { content: attr(data-label); flex: none; font-weight: 600; color: var(--muted); text-align: left; }
  .table.responsive td:empty { display: none; }
  .table.responsive tfoot td { background: none; }
  .share { min-width: 0; width: 100%; max-width: 220px; }
}

/* ---------- Print (reports) ---------- */
@media print {
  body { background: #fff; color: #000; }
  .sidebar, .topbar, .overlay, .no-print, .footer, dialog, .row-actions { display: none !important; }
  .app { display: block; }
  .content { max-width: none; padding: 0; }
  .print-only { display: block; margin-bottom: 14px; }
  .report-title h1 { color: #000; font-size: 1.4rem; }
  .report-title p { margin: 2px 0 0; color: #333; }
  .card { border: 1px solid #bbb; box-shadow: none; break-inside: avoid; margin-bottom: 20px !important; }
  .stat-balance { background: #fff; border-color: #bbb; }
  .stat-balance .stat-value, .stat-balance .stat-label, .stat-balance .stat-note { color: #000; }
  .stat-icon { display: none; }
  .share-bar span { background: #555 !important; -webkit-print-color-adjust: exact; print-color-adjust: exact; }
  table { width: 100% !important; border-collapse: collapse !important; }
  th, td { border: 1px solid #ddd !important; padding: 8px !important; }
  .table tr { break-inside: avoid; }
}