/* === Material Design Variables === */
:root {
  --primary: #1976d2;
  --primary-dark: #1565c0;
  --primary-light: #bbdefb;
  --accent: #ff6f00;
  --success: #2e7d32;
  --success-light: #e8f5e9;
  --error: #c62828;
  --error-light: #ffebee;
  --warning: #f57f17;
  --warning-light: #fff8e1;
  --bg: #f5f5f5;
  --surface: #ffffff;
  --on-surface: #212121;
  --on-surface-medium: #616161;
  --on-surface-light: #9e9e9e;
  --divider: #e0e0e0;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow-1: 0 1px 3px rgba(0,0,0,0.12), 0 1px 2px rgba(0,0,0,0.08);
  --shadow-2: 0 3px 6px rgba(0,0,0,0.15), 0 2px 4px rgba(0,0,0,0.10);
  --shadow-3: 0 6px 12px rgba(0,0,0,0.18), 0 3px 6px rgba(0,0,0,0.12);
  --transition: 0.2s cubic-bezier(0.4, 0, 0.2, 1);
}

/* === Reset & Base === */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html { scroll-behavior: smooth; scroll-padding-top: 130px; }

body {
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'PingFang SC', 'Microsoft YaHei', sans-serif;
  background: var(--bg);
  color: var(--on-surface);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  line-height: 1.6;
}

/* === Header === */
.app-header {
  background: linear-gradient(135deg, var(--primary), var(--primary-dark));
  color: #fff;
  padding: 12px 20px 0;
  box-shadow: var(--shadow-2);
  position: sticky;
  top: 0;
  z-index: 100;
}
.app-header h1 {
  font-size: 1.3rem;
  font-weight: 600;
  margin-bottom: 8px;
  display: flex;
  align-items: center;
  gap: 10px;
}

/* === Navigation Tabs === */
.nav-tabs {
  display: flex;
  gap: 2px;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}
.nav-tab {
  color: rgba(255,255,255,0.75);
  text-decoration: none;
  padding: 8px 14px 10px;
  font-size: 0.85rem;
  font-weight: 500;
  border-bottom: 3px solid transparent;
  transition: var(--transition);
  white-space: nowrap;
  display: flex;
  align-items: center;
  gap: 5px;
  border-radius: 8px 8px 0 0;
}
.nav-tab:hover { color: #fff; background: rgba(255,255,255,0.1); }

/* === Content === */
.app-content {
  flex: 1;
  padding: 16px;
  max-width: 800px;
  width: 100%;
  margin: 0 auto;
}
.page-section {
  margin-bottom: 16px;
  scroll-margin-top: 130px;
}
.page-section:last-child { margin-bottom: 0; }

/* === Section Title === */
.section-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 12px;
  padding-bottom: 8px;
  border-bottom: 2px solid var(--primary-light);
  display: flex;
  align-items: center;
  gap: 8px;
}

/* === Cards === */
.card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 20px;
  margin-bottom: 12px;
  box-shadow: var(--shadow-1);
  transition: var(--transition);
}
.card:hover { box-shadow: var(--shadow-2); }
.card:last-child { margin-bottom: 0; }
.card-header {
  font-size: 1rem;
  font-weight: 600;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--on-surface);
}

/* === Medication List === */
.med-list { list-style: none; }
.med-item {
  display: flex;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid var(--divider);
  gap: 10px;
  transition: var(--transition);
}
.med-item:last-child { border-bottom: none; }
.med-item .time-badge {
  background: var(--primary-light);
  color: var(--primary-dark);
  font-weight: 600;
  padding: 5px 10px;
  border-radius: 20px;
  font-size: 0.82rem;
  min-width: 56px;
  text-align: center;
}
.med-item .med-info { flex: 1; }
.med-item .med-name { font-weight: 600; font-size: 0.95rem; }
.med-item .med-dosage { color: var(--on-surface-medium); font-size: 0.82rem; }

.med-item .status-icon {
  width: 34px; height: 34px;
  border-radius: 50%;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.05rem;
}
.med-item.taken .status-icon {
  background: var(--success-light); color: var(--success);
}
.med-item.missed .status-icon {
  background: var(--error-light); color: var(--error);
}
.med-item.pending .status-icon {
  background: #f5f5f5; color: var(--on-surface-light);
}
.med-item.taken .med-name { color: var(--on-surface-light); }

/* === Rate Circle === */
.rate-container { text-align: center; padding: 6px 0; }
.rate-circle {
  width: 110px; height: 110px;
  border-radius: 50%;
  margin: 0 auto 10px;
  display: flex; align-items: center; justify-content: center;
  font-size: 1.8rem; font-weight: 700;
  border: 5px solid var(--divider);
}
.rate-circle.good { border-color: var(--success); color: var(--success); }
.rate-circle.warn { border-color: var(--warning); color: var(--warning); }
.rate-circle.bad { border-color: var(--error); color: var(--error); }
.rate-label { font-size: 0.85rem; color: var(--on-surface-medium); }

/* === Stats Cards === */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
  gap: 10px;
  margin-bottom: 12px;
}
.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px;
  text-align: center;
  box-shadow: var(--shadow-1);
}
.stat-value { font-size: 1.6rem; font-weight: 700; color: var(--primary); }
.stat-label { font-size: 0.78rem; color: var(--on-surface-medium); margin-top: 2px; }

/* === Calendar === */
.calendar-header {
  display: flex; align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}
.calendar-month { font-size: 1.05rem; font-weight: 600; }
.calendar-nav {
  width: 34px; height: 34px; border-radius: 50%;
  border: none; background: var(--surface); box-shadow: var(--shadow-1);
  cursor: pointer; font-size: 0.95rem; color: var(--on-surface);
  display: flex; align-items: center; justify-content: center;
}
.calendar-nav:hover { background: var(--primary-light); }

.calendar-grid { display: grid; grid-template-columns: repeat(7, 1fr); gap: 3px; }
.calendar-day-name {
  text-align: center; font-size: 0.78rem; font-weight: 600;
  color: var(--on-surface-medium); padding: 5px 0;
}
.calendar-day {
  aspect-ratio: 1;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  border-radius: var(--radius-sm);
  font-size: 0.85rem; cursor: pointer;
  transition: var(--transition);
  border: 2px solid transparent;
  min-width: 0;
}
.calendar-day:hover { background: var(--primary-light); }
.calendar-day.other-month { color: var(--on-surface-light); }
.calendar-day.today { border-color: var(--primary); font-weight: 700; }
.calendar-day.all-taken { background: var(--success-light); color: var(--success); font-weight: 600; }
.calendar-day.partial { background: var(--warning-light); color: var(--warning); }
.calendar-day.missed { background: var(--error-light); color: var(--error); }

.calendar-legend {
  display: flex; gap: 14px; margin-top: 10px; font-size: 0.78rem;
  color: var(--on-surface-medium); flex-wrap: wrap;
}
.legend-dot {
  display: inline-block; width: 10px; height: 10px; border-radius: 50%;
  margin-right: 3px; vertical-align: middle;
}
.legend-dot.all { background: var(--success); }
.legend-dot.partial { background: var(--warning); }
.legend-dot.missed { background: var(--error); }

/* === Schedule Form === */
.form-row {
  display: flex; gap: 8px; margin-bottom: 8px; flex-wrap: wrap;
}
.form-input {
  flex: 1; min-width: 100px;
  padding: 9px 12px; border: 1px solid var(--divider);
  border-radius: var(--radius-sm); font-size: 0.88rem;
  background: var(--surface); transition: var(--transition);
}
.form-input:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-light); }

/* === Buttons === */
.btn {
  display: inline-flex; align-items: center; gap: 5px;
  padding: 9px 18px; border: none; border-radius: 24px;
  font-size: 0.88rem; font-weight: 600; cursor: pointer;
  transition: var(--transition); text-transform: uppercase;
  letter-spacing: 0.5px;
}
.btn-primary { background: var(--primary); color: #fff; }
.btn-primary:hover { background: var(--primary-dark); box-shadow: var(--shadow-2); }
.btn-danger { background: transparent; color: var(--error); padding: 5px 10px; font-size: 0.78rem; }
.btn-danger:hover { background: var(--error-light); }

/* === Export === */
.export-section { text-align: center; padding: 20px; }
.export-section .btn { font-size: 0.95rem; padding: 12px 28px; }

/* === Day Detail === */
.day-detail-list { list-style: none; }
.day-detail-item {
  display: flex; justify-content: space-between; align-items: center;
  padding: 9px 0; border-bottom: 1px solid var(--divider);
}
.day-detail-item .taken-badge {
  padding: 3px 8px; border-radius: 12px; font-size: 0.78rem; font-weight: 600;
}
.taken-badge.yes { background: var(--success-light); color: var(--success); }
.taken-badge.no { background: var(--error-light); color: var(--error); }

/* === Footer === */
.app-footer {
  text-align: center; padding: 14px; font-size: 0.78rem;
  color: var(--on-surface-light); border-top: 1px solid var(--divider);
}

/* === Back to Top FAB === */
.fab-top {
  position: fixed; bottom: 20px; right: 20px;
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center;
  box-shadow: var(--shadow-3); cursor: pointer;
  font-size: 1.2rem; z-index: 50;
  transition: var(--transition); opacity: 0; transform: translateY(20px);
  pointer-events: none; border: none;
}
.fab-top.visible { opacity: 1; transform: translateY(0); pointer-events: auto; }
.fab-top:hover { background: var(--primary-dark); }

/* === Loading & Error & Empty === */
.loading { text-align: center; padding: 30px; color: var(--on-surface-medium); }
.error-msg { background: var(--error-light); color: var(--error); padding: 10px 14px; border-radius: var(--radius-sm); margin: 8px 0; font-size: 0.85rem; }
.empty-state { text-align: center; padding: 30px; color: var(--on-surface-light); }
.empty-state i { font-size: 2.5rem; display: block; margin-bottom: 10px; }
.empty-state small { display: block; margin-top: 4px; }

/* === Chart Container === */
.chart-container { position: relative; height: 280px; margin: 8px 0; }

/* === Nav active highlight via IntersectionObserver === */
.nav-tab.active { color: #fff; border-bottom-color: #fff; }

/* === Responsive === */
@media (max-width: 600px) {
  .app-header { padding: 10px 10px 0; }
  .app-header h1 { font-size: 1.15rem; }
  .nav-tab { padding: 7px 10px 8px; font-size: 0.78rem; }
  .app-content { padding: 10px; }
  .card { padding: 14px; }
  .stats-grid { grid-template-columns: repeat(2, 1fr); }
  .chart-container { height: 240px; }
}
