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

:root {
  --cal-color: #e74c3c;
  --water-color: #3498db;
  --carbs-color: #f39c12;
  --protein-color: #9b59b6;
  --bg: #f0f4f8;
  --card-bg: #ffffff;
  --text: #2d3748;
  --text-muted: #718096;
  --border: #e2e8f0;
  --radius: 16px;
  --shadow: 0 2px 12px rgba(0,0,0,0.08);
  --accent: #4299e1;
}

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang TC",
               "Noto Sans TC", sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  -webkit-font-smoothing: antialiased;
}

#app { max-width: 480px; margin: 0 auto; padding-bottom: 40px; }

/* Header */
.header {
  background: #fff;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  z-index: 100;
  box-shadow: 0 1px 8px rgba(0,0,0,0.06);
}
.header-inner {
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 20px;
}
.logo { display: flex; align-items: center; gap: 8px; }
.logo-icon { font-size: 1.5rem; }
.logo-text { font-size: 1.15rem; font-weight: 700; color: var(--text); }
.goal-btn {
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.8rem;
  cursor: pointer;
  color: var(--text-muted);
  transition: all 0.2s;
}
.goal-btn:hover { background: var(--accent); color: #fff; border-color: var(--accent); }

/* Main */
.main { padding: 16px; display: flex; flex-direction: column; gap: 14px; }

/* Cards */
.card {
  background: var(--card-bg);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

/* Chart Card */
.chart-card { padding: 20px; }
.section-title {
  font-size: 1.05rem;
  font-weight: 700;
  color: var(--text);
  margin-bottom: 16px;
  text-align: center;
}
.chart-container {
  position: relative;
  width: 220px;
  height: 220px;
  margin: 0 auto 12px;
}
#donutChart { display: block; }
.chart-center {
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%, -50%);
  text-align: center;
  pointer-events: none;
}
.chart-consumed { font-size: 0.72rem; color: var(--text-muted); margin-bottom: 2px; }
.chart-remaining { font-size: 1.25rem; font-weight: 800; color: var(--text); line-height: 1.2; }
.chart-goal { font-size: 0.72rem; color: var(--text-muted); margin-top: 2px; }

.legend-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-top: 8px;
}
.legend-item {
  display: flex;
  align-items: center;
  gap: 5px;
  font-size: 0.75rem;
  color: var(--text-muted);
}
.legend-dot {
  width: 10px; height: 10px;
  border-radius: 50%;
  flex-shrink: 0;
}

/* Tracker Cards */
.tracker-card { padding: 0; }

.tracker-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 14px 18px;
  cursor: pointer;
  user-select: none;
  transition: background 0.15s;
}
.tracker-header:hover { background: #f7fafc; }
.tracker-icon { font-size: 1.1rem; }
.tracker-label { font-size: 0.95rem; font-weight: 600; flex: 1; }
.tracker-pct { font-size: 0.85rem; font-weight: 700; color: var(--text-muted); }
.chevron { font-size: 0.7rem; color: var(--text-muted); transition: transform 0.25s; }
.chevron.collapsed { transform: rotate(180deg); }

.tracker-bar-wrap {
  padding: 0 18px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
}
.tracker-bar {
  flex: 1;
  height: 8px;
  background: #edf2f7;
  border-radius: 99px;
  overflow: hidden;
}
.tracker-fill {
  height: 100%;
  border-radius: 99px;
  transition: width 0.4s ease;
  max-width: 100%;
}
.cal-fill     { background: var(--cal-color); }
.water-fill   { background: var(--water-color); }
.carbs-fill   { background: var(--carbs-color); }
.protein-fill { background: var(--protein-color); }

.tracker-nums { font-size: 0.75rem; color: var(--text-muted); white-space: nowrap; }

.tracker-body {
  padding: 4px 18px 18px;
  border-top: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow: hidden;
  transition: max-height 0.3s ease, opacity 0.3s ease;
  max-height: 600px;
  opacity: 1;
}
.tracker-body.collapsed {
  max-height: 0;
  opacity: 0;
  padding-top: 0;
  padding-bottom: 0;
  border-top: none;
}

/* Inputs */
.input-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
}
.input-field {
  flex: 1;
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  font-size: 0.95rem;
  color: var(--text);
  background: #f7fafc;
  transition: border-color 0.2s;
  outline: none;
  width: 100%;
}
.input-field:focus { border-color: var(--accent); background: #fff; }
.unit { font-size: 0.85rem; color: var(--text-muted); white-space: nowrap; }

/* Meal Section */
.meal-section { display: flex; flex-direction: column; gap: 8px; }
.meal-section-title { font-size: 0.8rem; font-weight: 600; color: var(--text-muted); }
.meal-input-row { display: flex; gap: 8px; }
.meal-name-field { flex: 2; }
.meal-cal-field  { flex: 1; }

/* Meal List */
.meal-list { list-style: none; display: flex; flex-direction: column; gap: 6px; }
.meal-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: #f7fafc;
  border-radius: 10px;
  padding: 8px 12px;
  font-size: 0.85rem;
}
.meal-item-name { flex: 1; color: var(--text); }
.meal-item-cal { font-weight: 700; color: var(--cal-color); }
.meal-del {
  background: none;
  border: none;
  color: #cbd5e0;
  cursor: pointer;
  font-size: 1rem;
  padding: 0 0 0 8px;
  line-height: 1;
  transition: color 0.2s;
}
.meal-del:hover { color: var(--cal-color); }

/* Quick Buttons */
.quick-btns { display: flex; gap: 8px; flex-wrap: wrap; }
.btn-quick {
  background: #ebf8ff;
  color: var(--water-color);
  border: 1.5px solid #bee3f8;
  border-radius: 20px;
  padding: 6px 14px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-quick:hover { background: var(--water-color); color: #fff; border-color: var(--water-color); }

/* Primary Button */
.btn-primary {
  width: 100%;
  background: var(--accent);
  color: #fff;
  border: none;
  border-radius: 10px;
  padding: 11px;
  font-size: 0.95rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, transform 0.1s;
}
.btn-primary:hover  { background: #3182ce; }
.btn-primary:active { transform: scale(0.98); }

/* Summary */
.summary-card { padding: 20px; }
.summary-title { font-size: 1rem; font-weight: 700; margin-bottom: 14px; }
.summary-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 16px;
}
.summary-item {
  background: #f7fafc;
  border-radius: 12px;
  padding: 12px 14px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.summary-item-label { font-size: 0.75rem; color: var(--text-muted); }
.summary-item-value { font-size: 1.1rem; font-weight: 800; }
.summary-item-sub { font-size: 0.72rem; color: var(--text-muted); }
.summary-item.over .summary-item-value { color: var(--cal-color); }
.summary-item.ok .summary-item-value   { color: #38a169; }

.btn-reset {
  width: 100%;
  background: #fff5f5;
  color: #fc8181;
  border: 1.5px solid #fed7d7;
  border-radius: 10px;
  padding: 10px;
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.2s;
}
.btn-reset:hover { background: var(--cal-color); color: #fff; border-color: var(--cal-color); }

/* Modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.45);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}
.modal-overlay.open { opacity: 1; pointer-events: auto; }
.modal {
  background: #fff;
  border-radius: 20px;
  width: 100%;
  max-width: 380px;
  box-shadow: 0 20px 60px rgba(0,0,0,0.2);
  overflow: hidden;
  transform: translateY(20px);
  transition: transform 0.2s;
}
.modal-overlay.open .modal { transform: translateY(0); }
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px;
  border-bottom: 1px solid var(--border);
}
.modal-header h3 { font-size: 1rem; font-weight: 700; }
.modal-close {
  background: none;
  border: none;
  font-size: 1.1rem;
  cursor: pointer;
  color: var(--text-muted);
  padding: 4px 8px;
  border-radius: 6px;
  transition: background 0.2s;
}
.modal-close:hover { background: var(--bg); }
.modal-body { padding: 16px 20px; display: flex; flex-direction: column; gap: 12px; }
.goal-input-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.goal-input-row label { font-size: 0.9rem; font-weight: 500; white-space: nowrap; min-width: 100px; }
.goal-input-wrap { display: flex; align-items: center; gap: 6px; }
.goal-field { width: 100px; text-align: right; }
.modal-footer { padding: 12px 20px 20px; }

/* Over goal highlight */
.tracker-fill.over { animation: pulse 1.5s infinite; }
@keyframes pulse {
  0%, 100% { opacity: 1; }
  50%       { opacity: 0.7; }
}

/* Responsive tweak for very small screens */
@media (max-width: 360px) {
  .meal-input-row { flex-direction: column; }
  .quick-btns .btn-quick { flex: 1; }
}
