:root {
  --cat-orange: #e8874a;
  --cat-orange-dark: #c96a2f;
  --bg: #fdf9f4;
  --card: #ffffff;
  --border: #e8ddd0;
  --text: #3a2f28;
  --muted: #8a7a6d;
  --danger: #d1544a;
  --danger-bg: #fbe9e7;
  --success: #4a8c5e;
}

* { box-sizing: border-box; }

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

header.topbar {
  background: var(--cat-orange);
  color: white;
  padding: 16px 24px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

header.topbar h1 {
  font-size: 1.3rem;
  margin: 0;
}

header.topbar a {
  color: white;
  text-decoration: none;
  opacity: 0.9;
}

header.topbar a:hover { opacity: 1; text-decoration: underline; }

nav.tabs {
  display: flex;
  gap: 4px;
  padding: 12px 24px 0;
  flex-wrap: wrap;
}

nav.tabs a {
  padding: 8px 16px;
  border-radius: 8px 8px 0 0;
  text-decoration: none;
  color: var(--muted);
  background: #f2e8dd;
  font-size: 0.95rem;
}

nav.tabs a.active {
  background: var(--card);
  color: var(--cat-orange-dark);
  font-weight: 600;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 20px 24px;
}

.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 20px;
  margin-bottom: 20px;
}

.table-scroll {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  margin: 0 -20px;
  padding: 0 20px;
}

table {
  width: 100%;
  min-width: 560px;
  border-collapse: collapse;
  font-size: 0.92rem;
}

th, td {
  text-align: left;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-weight: 600;
  font-size: 0.85rem;
}

tr.inactive-row {
  opacity: 0.5;
}

input[type=text], input[type=number], input[type=datetime-local], input[type=date], select, textarea {
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
  font-size: 0.9rem;
  width: 100%;
  font-family: inherit;
}

textarea { min-height: 50px; }

.btn {
  display: inline-block;
  border: none;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 0.85rem;
  cursor: pointer;
  text-decoration: none;
}

.btn-primary { background: var(--cat-orange); color: white; }
.btn-primary:hover { background: var(--cat-orange-dark); }
.btn-secondary { background: #eee; color: var(--text); }
.btn-danger { background: var(--danger-bg); color: var(--danger); }
.btn-danger:hover { background: var(--danger); color: white; }
.btn-ghost { background: transparent; color: var(--muted); border: 1px solid var(--border); }
.btn-sm { padding: 3px 8px; font-size: 0.75rem; }

.inline-form { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; margin-top: 8px; }

.period-banner {
  background: #fdeceb;
  color: var(--danger);
  border-radius: 10px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  font-size: 0.9rem;
}

.actions { display: flex; gap: 6px; flex-wrap: wrap; }

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.75rem;
}
.badge-pending { background: #fff2d9; color: #a8720e; }
.badge-done { background: #e2f3e6; color: var(--success); }

.top-actions {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 16px;
  flex-wrap: wrap;
  gap: 8px;
}

.add-form {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: flex-end;
}
.add-form .field { display: flex; flex-direction: column; gap: 4px; min-width: 140px; }
.add-form label { font-size: 0.78rem; color: var(--muted); }

.flash {
  background: #e2f3e6;
  color: var(--success);
  padding: 10px 16px;
  border-radius: 8px;
  margin-bottom: 16px;
}

.login-box {
  max-width: 340px;
  margin: 80px auto;
  text-align: center;
}
.login-box h1 { font-size: 2rem; }
.login-box input { margin-bottom: 12px; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 12px;
}
.stat-grid .stat {
  background: #fff6ec;
  border-radius: 10px;
  padding: 16px;
  text-align: center;
  display: block;
  text-decoration: none;
  transition: background 0.15s;
}
.stat-grid a.stat:hover,
.stat-grid a.stat:active {
  background: #ffecd6;
}
.stat .num { font-size: 1.6rem; font-weight: 700; color: var(--cat-orange-dark); }
.stat .label { font-size: 0.8rem; color: var(--muted); }

.quick-list { margin: 0; padding: 0; list-style: none; }
.quick-list li { padding: 10px 2px; border-bottom: 1px dashed #e6d7c3; }
.quick-list li:last-child { border-bottom: none; }
.quick-primary { color: var(--muted); font-size: 0.85rem; }
.quick-secondary { margin-top: 2px; }

/* 消費統計：圓環圖 */
.donut-wrap {
  display: flex;
  align-items: center;
  gap: 24px;
  flex-wrap: wrap;
}
.donut {
  width: 140px;
  height: 140px;
  border-radius: 50%;
  flex-shrink: 0;
}
.donut-legend { list-style: none; margin: 0; padding: 0; flex: 1; min-width: 180px; }
.donut-legend li {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 0;
  font-size: 0.9rem;
}
.donut-legend .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* 消費統計：支出趨勢長條圖 */
.trend-chart {
  display: flex;
  align-items: flex-end;
  gap: 6px;
  height: 140px;
  overflow-x: auto;
  padding-bottom: 4px;
}
.trend-bar-wrap {
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  align-items: center;
  flex: 0 0 auto;
  width: 28px;
  height: 100%;
}
.trend-bar {
  width: 100%;
  min-height: 2px;
  background: var(--cat-orange);
  border-radius: 3px 3px 0 0;
}
.trend-label {
  margin-top: 4px;
  font-size: 0.65rem;
  color: var(--muted);
  white-space: nowrap;
}

.muted { color: var(--muted); font-size: 0.85rem; }

.report-presets { margin: 4px 0 0; font-size: 0.85rem; color: var(--muted); }
.report-presets a { color: var(--cat-orange-dark); text-decoration: none; margin-left: 6px; font-weight: 600; }
.report-presets a:hover { text-decoration: underline; }

.report-day h3 { color: var(--cat-orange-dark); }
.report-section { margin-bottom: 14px; }
.report-section:last-child { margin-bottom: 0; }
.report-section h4 {
  margin: 0 0 6px;
  font-size: 0.85rem;
  color: var(--muted);
}
.report-section ul { margin: 0; padding-left: 20px; }
.report-section li { padding: 2px 0; }

body { overflow-x: hidden; }

@media (max-width: 640px) {
  header.topbar { padding: 12px 16px; }
  header.topbar h1 { font-size: 1.05rem; }
  nav.tabs { padding: 10px 16px 0; }
  nav.tabs a { padding: 7px 10px; font-size: 0.85rem; }
  main { padding: 14px 16px; }
  .card { padding: 14px; }
  .add-form { flex-direction: column; align-items: stretch; }
  .add-form .field { min-width: 0; }
  .top-actions h2 { font-size: 1.1rem; }

  /* 手機版把表格轉成一張一張的卡片，不再橫向捲動——
     資料庫/後端邏輯完全沒變，純粹是這裡的顯示方式不同 */
  .table-scroll { overflow-x: visible; margin: 0; padding: 0; }
  table { min-width: 0; }
  table, tbody, tr { display: block; width: 100%; }
  thead { display: none; }

  tr {
    background: var(--card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 12px 14px;
    margin-bottom: 12px;
  }
  tr.inactive-row { opacity: 0.55; }

  td {
    display: block;
    border: none;
    padding: 7px 0;
  }
  td:not(:last-child) { border-bottom: 1px dashed var(--border); }

  td::before {
    content: attr(data-label);
    display: block;
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--muted);
    margin-bottom: 4px;
  }
  td:not([data-label])::before { content: none; }

  td.cell-actions {
    border-bottom: none;
    padding-top: 10px;
  }
}
