:root {
  --primary: #6366f1;
  --primary-light: #818cf8;
  --primary-dark: #4f46e5;
  --income: #10b981;
  --expense: #ef4444;
  --balance: #3b82f6;
  --bg: #f5f6fa;
  --card: #ffffff;
  --text: #1f2937;
  --text-secondary: #6b7280;
  --text-light: #9ca3af;
  --border: #e5e7eb;
  --shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.08);
  --radius: 12px;
  --radius-sm: 8px;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html {
  font-size: 16px;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "PingFang SC", "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
}

#app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100vh;
  padding-bottom: calc(70px + var(--safe-bottom));
  position: relative;
  background: var(--bg);
}

.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  padding: 16px 20px;
  padding-top: calc(16px + env(safe-area-inset-top, 0px));
  color: white;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.app-header h1 {
  font-size: 20px;
  font-weight: 600;
  letter-spacing: 0.5px;
}

.header-actions {
  display: flex;
  gap: 8px;
}

.icon-btn {
  background: rgba(255, 255, 255, 0.2);
  border: none;
  border-radius: 50%;
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: white;
  transition: background 0.2s;
}

.icon-btn:hover {
  background: rgba(255, 255, 255, 0.3);
}

.icon-btn svg {
  width: 20px;
  height: 20px;
}

.app-main {
  padding: 16px;
}

.view {
  animation: fadeIn 0.3s ease;
}

.hidden {
  display: none !important;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}

.card {
  background: var(--card);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.card-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.card-header h3 {
  font-size: 16px;
  font-weight: 600;
}

.text-btn {
  background: none;
  border: none;
  color: var(--primary);
  font-size: 14px;
  cursor: pointer;
}

.text-btn:hover {
  text-decoration: underline;
}

.summary-card {
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 100%);
  color: white;
  display: flex;
  align-items: center;
  justify-content: space-around;
  padding: 20px 16px;
  border-radius: var(--radius);
}

.summary-item {
  text-align: center;
  flex: 1;
}

.summary-label {
  display: block;
  font-size: 12px;
  opacity: 0.85;
  margin-bottom: 4px;
}

.summary-value {
  display: block;
  font-size: 18px;
  font-weight: 700;
}

.summary-divider {
  width: 1px;
  height: 32px;
  background: rgba(255, 255, 255, 0.3);
}

.summary-value.income { color: white; }
.summary-value.expense { color: white; }
.summary-value.balance { color: white; }

.transaction-list {
  display: flex;
  flex-direction: column;
}

.transaction-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: background 0.15s;
  border-radius: var(--radius-sm);
}

.transaction-item:hover {
  background: #f9fafb;
  margin: 0 -8px;
  padding: 12px 8px;
}

.transaction-item:last-child {
  border-bottom: none;
}

.transaction-icon {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
  margin-right: 12px;
  flex-shrink: 0;
}

.transaction-info {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  padding-right: 8px;
}

.transaction-category {
  font-size: 15px;
  font-weight: 500;
  margin-bottom: 2px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.transaction-merchant {
  font-size: 12px;
  color: var(--primary);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  margin-bottom: 1px;
}

.transaction-note {
  font-size: 12px;
  color: var(--text-light);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.transaction-right {
  text-align: right;
  flex-shrink: 0;
  margin-left: 8px;
}

.transaction-amount {
  font-size: 16px;
  font-weight: 600;
}

.transaction-amount.income {
  color: var(--income);
}

.transaction-amount.expense {
  color: var(--expense);
}

.transaction-date {
  font-size: 11px;
  color: var(--text-light);
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-state p {
  color: var(--text-secondary);
  margin-bottom: 20px;
}

.btn-primary {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 12px 24px;
  font-size: 15px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary:hover {
  background: var(--primary-dark);
}

.btn-primary-sm {
  background: var(--primary);
  color: white;
  border: none;
  border-radius: var(--radius-sm);
  padding: 8px 16px;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-primary-sm:hover {
  background: var(--primary-dark);
}

.btn-secondary {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 12px 24px;
  font-size: 15px;
  cursor: pointer;
  transition: background 0.2s;
}

.btn-secondary:hover {
  background: #e5e7eb;
}

.filter-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
  background: var(--card);
  border-radius: var(--radius);
  padding: 8px;
  box-shadow: var(--shadow);
}

.filter-tabs {
  display: flex;
  gap: 4px;
}

.tab {
  background: none;
  border: none;
  padding: 8px 14px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: var(--radius-sm);
  transition: all 0.2s;
}

.tab.active {
  background: var(--primary);
  color: white;
}

.month-picker {
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 10px;
  font-size: 13px;
  color: var(--text);
  background: white;
  outline: none;
}

.month-picker:focus {
  border-color: var(--primary);
}

.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  max-width: 480px;
  margin: 0 auto;
  background: white;
  border-top: 1px solid var(--border);
  display: flex;
  padding: 6px 0;
  padding-bottom: calc(6px + var(--safe-bottom));
  z-index: 200;
}

.nav-item {
  flex: 1;
  background: none;
  border: none;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding: 6px 0;
  color: var(--text-light);
  cursor: pointer;
  transition: color 0.2s;
}

.nav-item.active {
  color: var(--primary);
}

.nav-item svg {
  width: 22px;
  height: 22px;
}

.nav-item span {
  font-size: 11px;
}

.modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: flex-end;
  justify-content: center;
}

.modal.hidden {
  display: none;
}

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  animation: fadeIn 0.2s ease;
}

.modal-content {
  position: relative;
  background: white;
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  animation: slideUp 0.3s ease;
}

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

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

.modal-header h3 {
  font-size: 17px;
  font-weight: 600;
}

.modal-body {
  padding: 20px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  display: flex;
  gap: 12px;
  padding: 16px 20px;
  border-top: 1px solid var(--border);
}

.modal-footer button {
  flex: 1;
}

.type-switch {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 16px;
}

.type-btn {
  flex: 1;
  background: none;
  border: none;
  padding: 10px;
  font-size: 14px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
  font-weight: 500;
}

.type-btn.active {
  background: white;
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.form-group {
  margin-bottom: 16px;
}

.form-group label {
  display: block;
  font-size: 14px;
  font-weight: 500;
  margin-bottom: 8px;
  color: var(--text);
}

.form-input {
  width: 100%;
  padding: 12px 14px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 15px;
  background: white;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}

.form-input:focus {
  border-color: var(--primary);
}

.form-input::placeholder {
  color: var(--text-light);
}

.amount-input {
  width: 100%;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  font-size: 28px;
  font-weight: 600;
  text-align: center;
  background: white;
  color: var(--text);
  outline: none;
  transition: border-color 0.2s;
  font-family: inherit;
}

.amount-input:focus {
  border-color: var(--primary);
}

.amount-input::placeholder {
  color: var(--text-light);
  font-weight: 400;
}

.category-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
}

.category-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px 4px;
  border: 2px solid transparent;
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: all 0.2s;
  background: var(--bg);
}

.category-item:hover {
  background: #e5e7eb;
}

.category-item.selected {
  border-color: var(--primary);
  background: #eef2ff;
}

.category-icon {
  font-size: 24px;
  margin-bottom: 4px;
}

.category-name {
  font-size: 11px;
  color: var(--text-secondary);
}

.color-picker {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.color-dot {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: 3px solid transparent;
  cursor: pointer;
  transition: border-color 0.2s;
}

.color-dot.selected {
  border-color: var(--text);
}

.card-list-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 16px;
}

.card-list-header h2 {
  font-size: 18px;
  font-weight: 600;
}

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

.credit-card {
  border-radius: 16px;
  padding: 20px;
  color: white;
  position: relative;
  overflow: hidden;
  min-height: 160px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: var(--shadow-lg);
}

.credit-card:hover {
  transform: translateY(-2px);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.15);
}

.credit-card::before {
  content: '';
  position: absolute;
  top: -50%;
  right: -20%;
  width: 200px;
  height: 200px;
  background: rgba(255, 255, 255, 0.1);
  border-radius: 50%;
}

.card-top {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  position: relative;
  z-index: 1;
}

.card-name {
  font-size: 16px;
  font-weight: 600;
}

.card-bank {
  font-size: 12px;
  opacity: 0.9;
  margin-top: 2px;
}

.card-type-badge {
  font-size: 11px;
  background: rgba(255, 255, 255, 0.25);
  padding: 3px 8px;
  border-radius: 10px;
}

.card-number-display {
  font-size: 18px;
  letter-spacing: 2px;
  font-family: "SF Mono", monospace;
  position: relative;
  z-index: 1;
}

.card-bottom {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  position: relative;
  z-index: 1;
}

.card-balance-info {
  font-size: 12px;
  opacity: 0.9;
}

.card-balance-info .value {
  font-size: 16px;
  font-weight: 600;
  display: block;
  margin-top: 2px;
}

.card-delete-btn {
  position: absolute;
  top: 10px;
  right: 10px;
  background: rgba(0, 0, 0, 0.2);
  border: none;
  color: white;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 2;
  opacity: 0;
  transition: opacity 0.2s;
}

.credit-card:hover .card-delete-btn {
  opacity: 1;
}

.card-detail {
  margin-top: 12px;
  padding: 12px;
  background: #f9fafb;
  border-radius: var(--radius-sm);
  font-size: 13px;
}

.card-detail-row {
  display: flex;
  justify-content: space-between;
  margin-bottom: 6px;
}

.card-detail-row:last-child {
  margin-bottom: 0;
}

.budget-list {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.budget-item {
  background: white;
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
  position: relative;
}

.budget-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 12px;
}

.budget-name {
  font-size: 15px;
  font-weight: 600;
}

.budget-amount {
  font-size: 14px;
  color: var(--text-secondary);
}

.budget-progress {
  height: 8px;
  background: var(--bg);
  border-radius: 4px;
  overflow: hidden;
  margin-bottom: 8px;
}

.budget-bar {
  height: 100%;
  border-radius: 4px;
  transition: width 0.3s;
}

.budget-bar.safe { background: var(--income); }
.budget-bar.warning { background: var(--expense); }

.budget-info {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: var(--text-secondary);
}

.budget-actions {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.budget-actions button {
  flex: 1;
  background: var(--bg);
  border: none;
  padding: 8px;
  border-radius: var(--radius-sm);
  font-size: 13px;
  cursor: pointer;
  color: var(--text-secondary);
  transition: background 0.2s;
}

.budget-actions button:hover {
  background: #e5e7eb;
}

.budget-actions button.danger:hover {
  background: #fee2e2;
  color: var(--expense);
}

.settings-group {
  margin-bottom: 24px;
}

.settings-group h3 {
  font-size: 14px;
  color: var(--text-secondary);
  margin-bottom: 8px;
  padding: 0 4px;
}

.settings-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 14px 16px;
  background: white;
  border: none;
  border-radius: var(--radius);
  margin-bottom: 6px;
  font-size: 15px;
  color: var(--text);
  cursor: pointer;
  box-shadow: var(--shadow);
  transition: background 0.2s;
}

.settings-item:hover {
  background: #f9fafb;
}

.settings-item.danger {
  color: var(--expense);
}

.settings-item .arrow {
  color: var(--text-light);
  font-size: 18px;
}

.stats-tabs {
  display: flex;
  background: white;
  border-radius: var(--radius);
  padding: 6px;
  margin-bottom: 16px;
  box-shadow: var(--shadow);
}

.stats-tabs .tab {
  flex: 1;
}

#categoryChart, #trendChart, #pieChart {
  width: 100% !important;
  height: 100% !important;
}

.category-breakdown-item {
  display: flex;
  align-items: center;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.category-breakdown-item:last-child {
  border-bottom: none;
}

.category-breakdown-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 16px;
  margin-right: 12px;
}

.category-breakdown-info {
  flex: 1;
}

.category-breakdown-name {
  font-size: 14px;
  font-weight: 500;
}

.category-breakdown-bar {
  height: 4px;
  background: var(--bg);
  border-radius: 2px;
  margin-top: 4px;
  overflow: hidden;
}

.category-breakdown-fill {
  height: 100%;
  border-radius: 2px;
  transition: width 0.3s;
}

.category-breakdown-amount {
  font-size: 14px;
  font-weight: 600;
  margin-left: 12px;
}

.toast {
  position: fixed;
  bottom: calc(100px + var(--safe-bottom));
  left: 50%;
  transform: translateX(-50%);
  background: rgba(31, 41, 55, 0.95);
  color: white;
  padding: 12px 24px;
  border-radius: 8px;
  font-size: 14px;
  z-index: 2000;
  animation: toastIn 0.3s ease;
  max-width: 90%;
  text-align: center;
}

.toast.hidden {
  display: none !important;
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(10px); }
  to { opacity: 1; transform: translateX(-50%) translateY(0); }
}

::-webkit-scrollbar {
  width: 0;
  height: 0;
}

@media (min-width: 481px) {
  #app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

@media (max-width: 360px) {
  .category-grid {
    grid-template-columns: repeat(3, 1fr);
  }

  .summary-value {
    font-size: 15px;
  }

  .summary-label {
    font-size: 11px;
  }
}

/* Auth Page */
.auth-page {
  position: fixed;
  inset: 0;
  z-index: 5000;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-light) 50%, #a5b4fc 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.auth-page.hidden {
  display: none !important;
}

.auth-container {
  width: 100%;
  max-width: 360px;
  background: white;
  border-radius: 20px;
  padding: 36px 28px;
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.2);
  animation: slideUp 0.4s ease;
}

.auth-logo {
  font-size: 48px;
  text-align: center;
  margin-bottom: 8px;
}

.auth-title {
  text-align: center;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 4px;
}

.auth-subtitle {
  text-align: center;
  color: var(--text-secondary);
  font-size: 14px;
  margin-bottom: 24px;
}

.auth-tabs {
  display: flex;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 4px;
  margin-bottom: 20px;
}

.auth-tab {
  flex: 1;
  background: none;
  border: none;
  padding: 10px;
  font-size: 15px;
  color: var(--text-secondary);
  cursor: pointer;
  border-radius: 6px;
  transition: all 0.2s;
  font-weight: 500;
}

.auth-tab.active {
  background: white;
  color: var(--primary);
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
}

.auth-form .form-group {
  margin-bottom: 16px;
}

.auth-submit {
  width: 100%;
  margin-top: 8px;
  padding: 14px;
  font-size: 16px;
}

.auth-hint {
  text-align: center;
  font-size: 12px;
  color: var(--text-light);
  margin-top: 16px;
  min-height: 18px;
}

.auth-footer-links {
  text-align: center;
  margin-top: 12px;
}
.auth-footer-links a {
  font-size: 12px;
  color: var(--text-light);
  text-decoration: none;
  cursor: pointer;
}
.auth-footer-links a:hover {
  color: var(--primary);
}

.no-data-hint {
  text-align: center;
  color: var(--text-light);
  padding: 20px;
  font-size: 14px;
}

.btn-secondary-sm {
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 6px 12px;
  font-size: 12px;
  cursor: pointer;
  transition: background 0.2s;
  white-space: nowrap;
}

.btn-secondary-sm:hover {
  background: #e5e7eb;
}

.user-info {
  align-items: center;
  gap: 12px;
}

.user-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: linear-gradient(135deg, var(--primary), var(--primary-light));
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 600;
  font-size: 16px;
  flex-shrink: 0;
}

.register-banner {
  background: linear-gradient(135deg, #fef3c7, #fde68a);
  border-bottom: 1px solid #fbbf24;
  padding: 10px 16px;
  animation: slideDown 0.3s ease;
}

.register-banner .banner-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 14px;
  color: #92400e;
}

.register-banner a {
  color: #d97706;
  font-weight: 600;
  text-decoration: underline;
  cursor: pointer;
}

.register-banner a:hover {
  color: #b45309;
}

.banner-close {
  background: rgba(255, 255, 255, 0.6);
  border: 1px solid #fbbf24;
  border-radius: 6px;
  padding: 4px 12px;
  font-size: 13px;
  color: #92400e;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.banner-close:hover {
  background: #fbbf24;
  color: white;
}

@keyframes slideDown {
  from { opacity: 0; transform: translateY(-10px); }
  to { opacity: 1; transform: translateY(0); }
}

.auth-readonly-notice {
  background: #dbeafe;
  color: #1e40af;
  padding: 10px 12px;
  border-radius: 8px;
  font-size: 13px;
  margin-bottom: 16px;
  line-height: 1.5;
}

.auth-readonly-notice strong {
  font-weight: 600;
}
