/* =========================
   基礎設定
   ========================= */
* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

:root {
  --bg: #f5f7fb;
  --bg-soft: #eef1f7;
  --card: #ffffff;
  --text: #1f2937;
  --text-soft: #6b7280;
  --text-faint: #9ca3af;
  --border: #e5e7eb;
  --primary: #4f46e5;
  --primary-soft: #eef2ff;
  --income: #059669;
  --income-soft: #ecfdf5;
  --expense: #dc2626;
  --expense-soft: #fef2f2;
  --shadow-sm: 0 1px 2px rgba(15, 23, 42, 0.04);
  --shadow-md: 0 4px 16px rgba(15, 23, 42, 0.06);
  --radius: 14px;
  --radius-sm: 10px;
}

html, body {
  height: 100%;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
    "Microsoft JhengHei", "Noto Sans TC", Roboto, sans-serif;
  font-size: 15px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

button {
  font: inherit;
  cursor: pointer;
  border: none;
  background: none;
  color: inherit;
}

input, select, textarea {
  font: inherit;
  color: inherit;
}

/* =========================
   版面
   ========================= */
.app {
  min-height: 100%;
  display: flex;
  flex-direction: column;
}

.header {
  background: linear-gradient(135deg, #4f46e5 0%, #7c3aed 100%);
  color: #fff;
  padding: 36px 20px 56px;
}

.header-inner {
  max-width: 960px;
  margin: 0 auto;
}

.header h1 {
  font-size: 26px;
  font-weight: 700;
  letter-spacing: 0.5px;
}

.header p {
  margin-top: 6px;
  font-size: 14px;
  opacity: 0.85;
}

.container {
  max-width: 960px;
  width: 100%;
  margin: -32px auto 32px;
  padding: 0 20px;
  flex: 1;
}

.footer {
  text-align: center;
  padding: 24px 20px 32px;
  color: var(--text-faint);
  font-size: 12px;
}

/* =========================
   卡片 / 面板
   ========================= */
.card,
.panel {
  background: var(--card);
  border-radius: var(--radius);
  box-shadow: var(--shadow-md);
}

.panel {
  padding: 24px;
  margin-top: 20px;
}

.panel-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 16px;
}

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

.panel-header .panel-title {
  margin-bottom: 0;
}

/* =========================
   摘要
   ========================= */
.summary {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 16px;
}

.summary-card {
  padding: 20px;
  border-top: 4px solid var(--primary);
}

.summary-card.income {
  border-top-color: var(--income);
}

.summary-card.expense {
  border-top-color: var(--expense);
}

.summary-label {
  font-size: 13px;
  color: var(--text-soft);
  margin-bottom: 6px;
}

.summary-amount {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
}

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

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

/* =========================
   表單
   ========================= */
.type-toggle {
  display: inline-flex;
  background: var(--bg-soft);
  padding: 4px;
  border-radius: 999px;
  margin-bottom: 18px;
}

.type-option {
  position: relative;
  cursor: pointer;
}

.type-option input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.type-option span {
  display: inline-block;
  padding: 8px 20px;
  font-size: 14px;
  font-weight: 500;
  color: var(--text-soft);
  border-radius: 999px;
  transition: all 0.2s ease;
}

.type-option input:checked + span {
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.type-option input[value="income"]:checked + span {
  color: var(--income);
}

.type-option input[value="expense"]:checked + span {
  color: var(--expense);
}

.form-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 14px;
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.form-group-wide {
  grid-column: span 2;
}

.form-group label {
  font-size: 13px;
  color: var(--text-soft);
  font-weight: 500;
}

.form-group input,
.form-group select {
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--card);
  outline: none;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.form-group input:focus,
.form-group select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-soft);
}

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

.form-actions {
  display: flex;
  gap: 10px;
  margin-top: 18px;
}

/* =========================
   按鈕
   ========================= */
.btn-primary {
  background: var(--primary);
  color: #fff;
  padding: 10px 22px;
  border-radius: var(--radius-sm);
  font-weight: 600;
  transition: background 0.15s ease, transform 0.05s ease;
}

.btn-primary:hover {
  background: #4338ca;
}

.btn-primary:active {
  transform: translateY(1px);
}

.btn-ghost {
  background: var(--bg-soft);
  color: var(--text-soft);
  padding: 10px 18px;
  border-radius: var(--radius-sm);
  font-weight: 500;
  transition: background 0.15s ease;
}

.btn-ghost:hover {
  background: #e2e6ee;
}

.btn-text {
  color: var(--text-soft);
  font-size: 13px;
  padding: 4px 8px;
  border-radius: 6px;
  transition: color 0.15s ease, background 0.15s ease;
}

.btn-text:hover {
  color: var(--expense);
  background: var(--expense-soft);
}

.icon-btn {
  width: 28px;
  height: 28px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 6px;
  color: var(--text-faint);
  transition: background 0.15s ease, color 0.15s ease;
}

.icon-btn:hover {
  background: var(--expense-soft);
  color: var(--expense);
}

/* =========================
   篩選
   ========================= */
.filter-group {
  display: inline-flex;
  background: var(--bg-soft);
  padding: 4px;
  border-radius: 999px;
}

.filter-btn {
  padding: 6px 16px;
  font-size: 13px;
  font-weight: 500;
  color: var(--text-soft);
  border-radius: 999px;
  transition: all 0.2s ease;
}

.filter-btn.active {
  background: var(--card);
  color: var(--text);
  box-shadow: var(--shadow-sm);
}

.list-toolbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 4px 2px 12px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 8px;
}

.record-count {
  font-size: 13px;
  color: var(--text-soft);
}

/* =========================
   記錄列表
   ========================= */
.records-list {
  list-style: none;
  display: flex;
  flex-direction: column;
}

.record-item {
  display: grid;
  grid-template-columns: auto 1fr auto auto;
  align-items: center;
  gap: 14px;
  padding: 14px 6px;
  border-bottom: 1px solid var(--border);
  transition: background 0.15s ease;
}

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

.record-item:hover {
  background: var(--bg-soft);
  border-radius: var(--radius-sm);
}

.record-badge {
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  font-size: 18px;
}

.record-badge.income {
  background: var(--income-soft);
  color: var(--income);
}

.record-badge.expense {
  background: var(--expense-soft);
  color: var(--expense);
}

.record-info {
  min-width: 0;
}

.record-title {
  font-size: 14px;
  font-weight: 500;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.record-category {
  font-size: 11px;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--bg-soft);
  color: var(--text-soft);
  font-weight: 500;
}

.record-meta {
  font-size: 12px;
  color: var(--text-faint);
  margin-top: 2px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.record-meta .dot {
  width: 3px;
  height: 3px;
  border-radius: 50%;
  background: var(--text-faint);
  display: inline-block;
}

.record-amount {
  font-size: 15px;
  font-weight: 700;
  white-space: nowrap;
}

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

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

.record-actions {
  display: flex;
  gap: 2px;
}

/* =========================
   空狀態
   ========================= */
.empty-state {
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 40px 20px;
  color: var(--text-faint);
  text-align: center;
}

.empty-state.show {
  display: flex;
}

.empty-icon {
  font-size: 36px;
  margin-bottom: 8px;
  opacity: 0.6;
}

/* =========================
   編輯中狀態
   ========================= */
.record-item.editing {
  background: var(--primary-soft);
  border-radius: var(--radius-sm);
}

/* =========================
   圓餅圖
   ========================= */
.chart-section {
  /* panel 已有的 padding/margin 沿用 */
}

.charts {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
}

.chart-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  background: var(--bg-soft);
  border-radius: var(--radius-sm);
}

.chart-title {
  font-size: 14px;
  font-weight: 600;
  color: var(--text-soft);
  margin-bottom: 14px;
  letter-spacing: 0.5px;
}

.donut-wrap {
  position: relative;
  width: 200px;
  height: 200px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.donut {
  width: 100%;
  height: 100%;
  overflow: visible;
}

.donut-slice {
  transition: transform 0.18s ease, filter 0.18s ease, opacity 0.18s ease;
  transform-origin: 0 0;
  cursor: default;
}

.donut-slice:hover {
  transform: scale(1.04);
  filter: brightness(1.08);
}

.donut-slice.dimmed {
  opacity: 0.35;
}

.donut-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  text-align: center;
}

.donut-total {
  font-size: 18px;
  font-weight: 700;
  color: var(--text);
  line-height: 1.2;
}

.donut-label {
  font-size: 11px;
  color: var(--text-faint);
  margin-top: 2px;
  letter-spacing: 0.5px;
}

.donut-empty {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  color: var(--text-faint);
  font-size: 13px;
  background: var(--bg-soft);
  border-radius: 50%;
  margin: 25px;
  pointer-events: none;
}

.donut-empty.show {
  display: flex;
}

/* 圖例 */
.chart-legend {
  list-style: none;
  width: 100%;
  margin-top: 18px;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.legend-item {
  display: grid;
  grid-template-columns: 12px 1fr auto auto;
  align-items: center;
  gap: 10px;
  padding: 6px 4px;
  font-size: 13px;
  border-radius: 6px;
  transition: background 0.15s ease;
  cursor: default;
}

.legend-item:hover {
  background: var(--card);
}

.legend-item.highlighted {
  background: var(--card);
  font-weight: 500;
}

.legend-dot {
  width: 12px;
  height: 12px;
  border-radius: 3px;
}

.legend-name {
  color: var(--text);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.legend-pct {
  color: var(--text-soft);
  font-variant-numeric: tabular-nums;
  font-size: 12px;
}

.legend-amount {
  color: var(--text);
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* 圖表動畫 */
@keyframes donutGrow {
  from {
    opacity: 0;
    transform: scale(0.85);
  }
  to {
    opacity: 1;
    transform: scale(1);
  }
}

.donut-slice {
  animation: donutGrow 0.4s ease-out backwards;
}

/* =========================
   響應式
   ========================= */
@media (max-width: 720px) {
  .summary {
    grid-template-columns: 1fr;
  }
  .form-grid {
    grid-template-columns: 1fr 1fr;
  }
  .form-group-wide {
    grid-column: span 2;
  }
  .header {
    padding: 28px 18px 48px;
  }
  .panel {
    padding: 20px;
  }
  .summary-amount {
    font-size: 20px;
  }
  .charts {
    grid-template-columns: 1fr;
    gap: 16px;
  }
  .donut-wrap {
    width: 180px;
    height: 180px;
  }
}

@media (max-width: 480px) {
  .form-grid {
    grid-template-columns: 1fr;
  }
  .form-group-wide {
    grid-column: span 1;
  }
  .record-item {
    grid-template-columns: auto 1fr auto;
  }
  .record-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
    margin-top: 4px;
  }
}
