:root {
  --color-bg: #FFF8ED;
  --color-surface: #FFFFFF;
  --color-primary: #E8A33D;
  --color-primary-dark: #C97F1F;
  --color-accent: #F4C978;
  --color-text: #4A3728;
  --color-text-soft: #8A7259;
  --color-income: #5C9B5C;
  --color-expense: #D9705F;
  --color-liability: #D9705F;
  --color-border: #F0DFC0;
  --radius: 18px;
  --radius-sm: 12px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --color-bg: #211A12;
    --color-surface: #2E2419;
    --color-primary: #E8A33D;
    --color-primary-dark: #F4C978;
    --color-accent: #B8863A;
    --color-text: #F5E9D6;
    --color-text-soft: #C8B69A;
    --color-border: #4A3B28;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, 'PingFang TC', 'Noto Sans TC', 'Microsoft JhengHei', sans-serif;
  background: var(--color-bg);
  color: var(--color-text);
  padding-bottom: 76px;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 16px 20px 12px;
  position: sticky;
  top: 0;
  background: var(--color-bg);
  z-index: 5;
}
.app-header-icon { font-size: 22px; }
.app-header-title { font-size: 18px; font-weight: 700; }

.page { padding: 4px 16px 24px; max-width: 480px; margin: 0 auto; }
.page-title { font-size: 20px; font-weight: 700; margin: 4px 0 16px; }

.section { margin-top: 24px; }
.section-title { font-size: 15px; font-weight: 700; color: var(--color-text-soft); margin: 0 0 10px; }
.section-title-sm { font-size: 13px; font-weight: 700; color: var(--color-text-soft); margin: 8px 0 6px; }
.muted { color: var(--color-text-soft); font-size: 14px; }

/* Net worth card */
.networth-card {
  background: linear-gradient(135deg, var(--color-primary), var(--color-primary-dark));
  border-radius: var(--radius);
  padding: 22px 20px;
  color: #fff;
  box-shadow: 0 8px 20px rgba(201, 127, 31, 0.25);
}
.networth-label { font-size: 13px; opacity: 0.9; }
.networth-value { font-size: 32px; font-weight: 800; margin: 4px 0 12px; }
.networth-breakdown { display: flex; gap: 10px; font-size: 12px; }
.networth-breakdown span {
  background: rgba(255,255,255,0.2);
  border-radius: 999px;
  padding: 4px 10px;
}

/* Account rows */
.account-row, .account-card-header {
  display: flex;
  align-items: center;
  gap: 12px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 12px 14px;
  margin-bottom: 8px;
  cursor: pointer;
}
.account-row-icon { font-size: 22px; }
.account-row-main { flex: 1; min-width: 0; }
.account-row-name { font-weight: 600; }
.account-row-sub { font-size: 12px; color: var(--color-text-soft); }
.account-row-amount { text-align: right; font-weight: 700; white-space: nowrap; }
.account-row-base { font-size: 11px; color: var(--color-text-soft); font-weight: 400; }

.account-card { margin-bottom: 8px; }
.account-card-header { margin-bottom: 0; border-radius: var(--radius-sm) var(--radius-sm) 0 0; }
.account-card-body {
  border: 1px solid var(--color-border);
  border-top: none;
  border-radius: 0 0 var(--radius-sm) var(--radius-sm);
  padding: 12px 14px;
  background: var(--color-surface);
}
.account-card-actions { display: flex; gap: 8px; margin-top: 10px; }

.tag-asset, .tag-liability { }

/* Holdings */
.holding-row {
  display: grid;
  grid-template-columns: 1fr 1.4fr auto auto auto;
  align-items: center;
  gap: 6px;
  padding: 6px 0;
  font-size: 13px;
  border-bottom: 1px dashed var(--color-border);
}
.holding-ticker { font-weight: 700; }
.holding-value { font-weight: 700; text-align: right; }

/* Buttons */
.btn-primary, .btn-secondary, .btn-danger {
  border: none;
  border-radius: 999px;
  padding: 10px 18px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}
.btn-primary { background: var(--color-primary); color: #fff; }
.btn-secondary { background: var(--color-accent); color: var(--color-text); }
.btn-danger { background: transparent; color: var(--color-expense); border: 1px solid var(--color-expense); }
.btn-block { width: 100%; margin-bottom: 14px; }
.icon-btn {
  border: none;
  background: transparent;
  color: var(--color-text-soft);
  font-size: 15px;
  cursor: pointer;
  padding: 4px 6px;
}

/* Forms */
.inline-form, .tx-form {
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 14px;
  margin-bottom: 14px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.inline-form label, .tx-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 13px;
  color: var(--color-text-soft);
}
input, select {
  font-family: inherit;
  font-size: 15px;
  padding: 9px 10px;
  border-radius: var(--radius-sm);
  border: 1px solid var(--color-border);
  background: var(--color-bg);
  color: var(--color-text);
}
.form-actions { display: flex; gap: 8px; margin-top: 4px; }

.type-toggle { display: flex; gap: 6px; margin-bottom: 4px; }
.type-btn {
  flex: 1;
  padding: 9px 0;
  border-radius: 999px;
  border: 1px solid var(--color-border);
  background: var(--color-surface);
  color: var(--color-text-soft);
  font-weight: 700;
  cursor: pointer;
}
.type-btn.active { background: var(--color-primary); color: #fff; border-color: var(--color-primary); }

.new-category-wrap { margin-top: -4px; }

/* Transactions list */
.tx-date-header { font-size: 12px; color: var(--color-text-soft); margin: 14px 0 6px; font-weight: 700; }
.tx-row {
  display: flex;
  align-items: center;
  gap: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 6px;
}
.tx-row-main { flex: 1; min-width: 0; }
.tx-row-title { font-weight: 600; font-size: 14px; }
.tx-row-sub { font-size: 12px; color: var(--color-text-soft); }
.tx-row-amount { font-weight: 700; white-space: nowrap; }
.tx-amount-income { color: var(--color-income); }
.tx-amount-expense { color: var(--color-expense); }
.tx-amount-transfer { color: var(--color-text-soft); }
.tx-row-actions { display: flex; }

/* Recurring */
.recurring-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px;
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 10px 12px;
  margin-bottom: 6px;
}
.recurring-row-main { flex: 1; min-width: 0; }
.recurring-edit-slot { flex-basis: 100%; }

/* Stats */
.stats-controls { display: flex; justify-content: space-between; align-items: center; margin-bottom: 14px; gap: 10px; }
.stats-controls .type-toggle { margin-bottom: 0; width: 120px; }
.period-nav { display: flex; align-items: center; gap: 8px; font-weight: 700; }
.stats-summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  background: var(--color-surface);
  border: 1px solid var(--color-border);
  border-radius: var(--radius-sm);
  padding: 14px;
  text-align: center;
  gap: 6px;
}
.bar-chart { display: flex; flex-direction: column; gap: 10px; }
.bar-row { display: grid; grid-template-columns: 70px 1fr 90px; align-items: center; gap: 8px; font-size: 13px; }
.bar-row-label { color: var(--color-text-soft); }
.bar-track { background: var(--color-border); border-radius: 999px; height: 10px; overflow: hidden; }
.bar-fill { height: 100%; border-radius: 999px; }
.bar-expense { background: var(--color-expense); }
.bar-income { background: var(--color-income); }
.bar-row-value { text-align: right; font-weight: 600; }

/* Rates */
.rates-details summary::-webkit-details-marker { display: none; }
.rates-list { display: flex; flex-direction: column; gap: 8px; margin: 10px 0; }
.rate-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.rate-row input { flex: 1; }
.rate-add-row { display: flex; gap: 8px; }
.rate-add-row input { flex: 1; }

/* Empty state */
.empty-state { text-align: center; padding: 40px 20px; color: var(--color-text-soft); }
.empty-msg { font-weight: 700; color: var(--color-text); margin: 10px 0 2px; }
.empty-sub { font-size: 13px; }

/* Bottom nav */
.bottom-nav {
  position: fixed;
  bottom: 0; left: 0; right: 0;
  display: flex;
  background: var(--color-surface);
  border-top: 1px solid var(--color-border);
  padding: 6px 0 calc(6px + env(safe-area-inset-bottom));
  z-index: 10;
}
.nav-btn {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-decoration: none;
  color: var(--color-text-soft);
  font-size: 11px;
  padding: 4px 0;
}
.nav-icon { font-size: 20px; }
.nav-btn.active { color: var(--color-primary-dark); font-weight: 700; }

/* Settings */
.settings-block { margin-bottom: 18px; }
.settings-block:last-child { margin-bottom: 0; }

/* Lock screen */
#lock-overlay {
  position: fixed;
  inset: 0;
  background: var(--color-bg);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
}
.lock-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 24px;
  max-width: 280px;
  text-align: center;
}
.lock-card h2 { margin: 0; font-size: 18px; }
.lock-card input {
  width: 160px;
  text-align: center;
  font-size: 24px;
  letter-spacing: 8px;
}
.lock-error { color: var(--color-expense); font-size: 13px; min-height: 18px; margin: 0; }

/* Toast */
#toast {
  position: fixed;
  left: 50%;
  bottom: 84px;
  transform: translateX(-50%) translateY(10px);
  background: var(--color-text);
  color: #fff;
  padding: 8px 16px;
  border-radius: 999px;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s, transform 0.2s;
  z-index: 20;
}
#toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
