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

:root {
  --income:   #10b981;
  --income-bg:#d1fae5;
  --expense:  #ef4444;
  --expense-bg:#fee2e2;
  --balance:  #3b82f6;
  --balance-bg:#dbeafe;
  --primary:  #3b82f6;
  --primary-dark: #2563eb;
  --bg:       #f3f4f6;
  --card:     #ffffff;
  --border:   #e5e7eb;
  --text:     #111827;
  --text-sub: #6b7280;
  --header-h: 100px;
  --bottom-h: 64px;
}

html { font-size: 15px; }
body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  padding-bottom: var(--bottom-h);
}

/* ===== HEADER ===== */
.header {
  position: sticky; top: 0; z-index: 50;
  background: #fff;
  border-bottom: 1px solid var(--border);
}
.header-inner {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px 6px;
}
.header-title { font-size: 18px; font-weight: 700; }
.header-year  { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 15px; }
.year-btn { background: none; border: 1px solid var(--border); border-radius: 6px; width: 28px; height: 28px; cursor: pointer; font-size: 16px; display: flex; align-items: center; justify-content: center; }
.year-btn:hover { background: var(--bg); }

.tab-nav {
  display: flex; overflow-x: auto; -webkit-overflow-scrolling: touch;
  padding: 0 12px 0;
  scrollbar-width: none;
}
.tab-nav::-webkit-scrollbar { display: none; }
.tab-btn {
  flex-shrink: 0; background: none; border: none; cursor: pointer;
  padding: 8px 14px; font-size: 13px; color: var(--text-sub);
  border-bottom: 2px solid transparent;
  white-space: nowrap;
  transition: color .2s, border-color .2s;
}
.tab-btn.active { color: var(--primary); border-color: var(--primary); font-weight: 600; }

/* ===== MAIN ===== */
.main { max-width: 900px; margin: 0 auto; padding: 16px; }

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

/* ===== CARD ===== */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 16px;
  margin-bottom: 12px;
}

/* ===== MONTH NAV ===== */
.month-nav {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px;
}
.month-label { font-size: 17px; font-weight: 700; }

/* ===== KPI GRID ===== */
.kpi-grid {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px;
  margin-bottom: 12px;
}
.kpi-card {
  border-radius: 12px; padding: 14px 12px; text-align: center;
}
.kpi-label { font-size: 12px; font-weight: 600; margin-bottom: 4px; }
.kpi-value { font-size: 18px; font-weight: 700; }
.kpi-income  { background: var(--income-bg); color: var(--income); }
.kpi-expense { background: var(--expense-bg); color: var(--expense); }
.kpi-balance { background: var(--balance-bg); color: var(--balance); }

/* ===== SECTION TITLE ===== */
.section-title { font-size: 14px; font-weight: 700; margin-bottom: 12px; color: var(--text-sub); letter-spacing: .04em; }
.section-sub   { font-size: 13px; color: var(--text-sub); margin-bottom: 14px; }

/* ===== BREAKDOWN ===== */
.breakdown-row {
  display: flex; align-items: center; gap: 10px; padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.breakdown-row:last-child { border-bottom: none; }
.breakdown-name  { flex: 1; font-size: 13px; }
.breakdown-bar-wrap { flex: 2; background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.breakdown-bar   { height: 8px; border-radius: 4px; background: var(--expense); transition: width .4s; }
.breakdown-amount{ font-size: 13px; font-weight: 600; min-width: 72px; text-align: right; }

/* ===== BUDGET PROGRESS ===== */
.budget-row { padding: 8px 0; border-bottom: 1px solid var(--border); }
.budget-row:last-child { border-bottom: none; }
.budget-row-top { display: flex; justify-content: space-between; align-items: baseline; margin-bottom: 5px; }
.budget-name     { font-size: 13px; font-weight: 600; }
.budget-amount   { font-size: 12px; color: var(--text-sub); }
.budget-bar-wrap { background: var(--bg); border-radius: 4px; height: 8px; overflow: hidden; }
.budget-bar      { height: 8px; border-radius: 4px; background: var(--income); transition: width .4s; }
.budget-bar.over { background: var(--expense); }
.budget-pct      { font-size: 11px; color: var(--text-sub); text-align: right; margin-top: 3px; }
.no-budget-msg   { font-size: 13px; color: var(--text-sub); }
.badge-annual {
  display: inline-block; font-size: 10px; font-weight: 700;
  background: var(--balance-bg); color: var(--balance);
  border-radius: 4px; padding: 1px 5px; margin-left: 4px; vertical-align: middle;
}

/* ===== MONTHLY TABLE ===== */
.table-controls {
  display: flex; align-items: center; gap: 16px; flex-wrap: wrap;
  padding: 10px 16px;
}
.table-controls label { display: flex; align-items: center; gap: 6px; font-size: 13px; }
.table-controls select { border: 1px solid var(--border); border-radius: 6px; padding: 4px 8px; font-size: 13px; }
.toggle-label { cursor: pointer; }

.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }

.monthly-table {
  border-collapse: collapse; min-width: 860px; font-size: 12.5px; width: 100%;
}
.monthly-table th, .monthly-table td {
  border: 1px solid var(--border);
  padding: 5px 8px; white-space: nowrap;
}
.monthly-table th {
  background: #f9fafb; font-weight: 600; text-align: center;
  position: sticky; top: 0; z-index: 2;
}
.monthly-table td { text-align: right; }
.monthly-table td.cat-cell { text-align: left; font-weight: 600; background: #f9fafb; }
.monthly-table td.sub-cell { text-align: left; color: var(--text-sub); padding-left: 20px; }
.monthly-table td.zero     { color: #d1d5db; }
.monthly-table tr.cat-row  { background: #f0f9ff; }
.monthly-table tr.total-row{ background: #fef3c7; font-weight: 700; }
.monthly-table .income-val { color: var(--income); }
.monthly-table .expense-val{ color: var(--expense); }

/* ===== ANNUAL ===== */
.annual-kpi {
  display: grid; grid-template-columns: repeat(3,1fr); gap: 12px; text-align: center;
}
.annual-kpi-item p:first-child { font-size: 12px; color: var(--text-sub); margin-bottom: 4px; }
.annual-kpi-item p:last-child  { font-size: 20px; font-weight: 700; }
.annual-kpi-item.income-item p:last-child { color: var(--income); }
.annual-kpi-item.expense-item p:last-child{ color: var(--expense); }
.annual-kpi-item.balance-item p:last-child{ color: var(--balance); }

.annual-bar-row {
  display: flex; align-items: center; gap: 10px; padding: 6px 0;
  border-bottom: 1px solid var(--border);
}
.annual-bar-row:last-child { border-bottom: none; }
.annual-bar-name   { width: 90px; font-size: 13px; flex-shrink: 0; }
.annual-bar-wrap   { flex: 1; background: var(--bg); border-radius: 4px; height: 12px; overflow: hidden; }
.annual-bar        { height: 12px; border-radius: 4px; background: var(--expense); transition: width .4s; }
.annual-bar-amount { width: 84px; text-align: right; font-size: 13px; font-weight: 600; }

.chart-wrap { height: 220px; position: relative; }
canvas { width: 100% !important; }

/* ===== IMPORT ===== */
.import-steps {
  background: var(--bg); border-radius: 8px; padding: 14px 14px 14px 28px;
  margin-bottom: 16px; font-size: 13px; line-height: 2;
}
.drop-zone {
  border: 2px dashed var(--border); border-radius: 10px;
  padding: 40px 20px; text-align: center; cursor: pointer;
  transition: border-color .2s, background .2s;
}
.drop-zone:hover, .drop-zone.drag-over {
  border-color: var(--primary); background: var(--balance-bg);
}
.drop-icon { font-size: 36px; margin-bottom: 8px; }
.drop-sub  { font-size: 12px; color: var(--text-sub); margin-top: 4px; }

.import-summary {
  background: #f0fdf4; border: 1px solid #bbf7d0; border-radius: 8px;
  padding: 12px 16px; margin-bottom: 12px; font-size: 13px;
}
.import-summary strong { color: var(--income); }

.preview-table { border-collapse: collapse; font-size: 12px; width: 100%; min-width: 600px; }
.preview-table th, .preview-table td {
  border: 1px solid var(--border); padding: 5px 8px; white-space: nowrap;
}
.preview-table th { background: #f9fafb; font-weight: 600; }
.preview-table td.income-row { color: var(--income); }
.preview-table td.expense-row{ color: var(--expense); }

.import-actions {
  display: flex; justify-content: flex-end; gap: 10px; margin-top: 16px;
}
.import-result {
  text-align: center; padding: 24px; font-size: 15px; font-weight: 600;
  background: var(--income-bg); border-radius: 10px; color: var(--income);
}

/* ===== BUDGET FORM ===== */
#budget-form { display: flex; flex-direction: column; gap: 6px; margin-bottom: 16px; }
.budget-input-row {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--border);
}
.budget-input-row:last-child { border-bottom: none; }
.budget-input-label { flex: 1; font-size: 13px; font-weight: 600; }
.budget-input-wrap  { display: flex; align-items: center; gap: 4px; }
.budget-input-wrap input {
  width: 110px; border: 1px solid var(--border); border-radius: 6px;
  padding: 5px 8px; font-size: 13px; text-align: right;
}
.budget-input-wrap span { font-size: 12px; color: var(--text-sub); }
.budget-actions { text-align: right; }

/* ===== IMPORT HISTORY ===== */
.history-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px;
}
.history-item:last-child { border-bottom: none; }
.history-date   { color: var(--text-sub); width: 80px; flex-shrink: 0; }
.history-type   { flex-shrink: 0; }
.history-cat    { flex: 1; }
.history-amount { font-weight: 600; min-width: 70px; text-align: right; }
.history-income { color: var(--income); }
.history-expense{ color: var(--expense); }

/* ===== BUTTONS ===== */
.btn {
  display: inline-flex; align-items: center; gap: 4px;
  border: none; border-radius: 8px; padding: 8px 16px;
  font-size: 13px; font-weight: 600; cursor: pointer; transition: opacity .2s;
}
.btn:hover { opacity: .85; }
.btn-primary { background: var(--primary); color: #fff; }
.btn-ghost   { background: var(--bg); color: var(--text); border: 1px solid var(--border); }

/* ===== BOTTOM NAV ===== */
.bottom-nav {
  position: fixed; bottom: 0; left: 0; right: 0;
  height: var(--bottom-h);
  background: #fff; border-top: 1px solid var(--border);
  display: flex; z-index: 50;
}
.bottom-btn {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  justify-content: center; gap: 3px;
  background: none; border: none; cursor: pointer;
  color: var(--text-sub); font-size: 10px; font-weight: 500;
  transition: color .2s;
}
.bottom-btn svg { width: 22px; height: 22px; }
.bottom-btn.active { color: var(--primary); }

/* ===== LOADING / TOAST ===== */
#loading-overlay {
  position: fixed; inset: 0; background: rgba(255,255,255,.7);
  display: flex; align-items: center; justify-content: center; z-index: 200;
}
.spinner {
  width: 36px; height: 36px; border: 3px solid var(--border);
  border-top-color: var(--primary); border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

.toast {
  position: fixed; bottom: 80px; left: 50%; transform: translateX(-50%);
  background: #1f2937; color: #fff; padding: 10px 20px;
  border-radius: 20px; font-size: 13px; z-index: 300;
  white-space: nowrap; pointer-events: none;
}

/* ===== RESPONSIVE ===== */
@media (min-width: 640px) {
  .bottom-nav { display: none; }
  body { padding-bottom: 0; }
  .main { padding: 20px; }
  .kpi-value { font-size: 22px; }
}
@media (max-width: 480px) {
  .kpi-value { font-size: 15px; }
  .kpi-card  { padding: 10px 8px; }
}
