/* ===== CSS Variables ===== */
:root {
  --primary: #0ea5e9;
  --primary-dark: #0284c7;
  --primary-light: #e0f2fe;
  --success: #22c55e;
  --success-light: #dcfce7;
  --warning: #f59e0b;
  --warning-light: #fef3c7;
  --danger: #ef4444;
  --danger-light: #fee2e2;
  --bg: #f1f5f9;
  --surface: #ffffff;
  --text: #0f172a;
  --text-secondary: #64748b;
  --text-tertiary: #94a3b8;
  --border: #e2e8f0;
  --radius: 12px;
  --radius-sm: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,0.08), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.1);
  --shadow-lg: 0 10px 30px rgba(0,0,0,0.15);
  --nav-height: 64px;
  --header-height: auto;
  --font-cn: "PingFang SC", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

/* ===== Reset ===== */
*, *::before, *::after {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

html {
  font-size: 16px;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: var(--font-cn);
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-tap-highlight-color: transparent;
}

input, select, textarea, button {
  font-family: inherit;
  font-size: inherit;
}

/* ===== Pages ===== */
.page {
  display: none;
  min-height: 100vh;
  padding-bottom: calc(var(--nav-height) + var(--safe-bottom) + 16px);
}

.page.active {
  display: block;
  animation: fadeIn 0.2s ease;
}

@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

/* ===== Page Header ===== */
.page-header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: var(--surface);
  padding: 16px 20px 12px;
  border-bottom: 1px solid var(--border);
}

.page-header h1 {
  font-size: 22px;
  font-weight: 700;
  line-height: 1.3;
}

.subtitle {
  font-size: 13px;
  color: var(--text-secondary);
  margin-top: 2px;
}

/* ===== Page Content ===== */
.page-content {
  padding: 16px 16px 24px;
}

/* ===== Stats Row ===== */
.stats-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 20px;
}

.stat-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 16px 12px;
  text-align: center;
  box-shadow: var(--shadow);
  cursor: pointer;
  transition: transform 0.15s ease, box-shadow 0.15s ease;
  border: 2px solid transparent;
}

.stat-card:active {
  transform: scale(0.97);
}

.stat-card .stat-number {
  font-size: 28px;
  font-weight: 700;
  color: var(--primary);
}

.stat-card .stat-label {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 4px;
}

.stat-card.stat-warning .stat-number { color: var(--warning); }
.stat-card.stat-warning { border-color: var(--warning-light); }
.stat-card.stat-danger .stat-number { color: var(--danger); }
.stat-card.stat-danger { border-color: var(--danger-light); }

/* ===== Section ===== */
.section {
  margin-bottom: 24px;
}

.section-title {
  font-size: 16px;
  font-weight: 600;
  margin-bottom: 12px;
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--text);
}

.section-desc {
  font-size: 13px;
  color: var(--text-secondary);
  margin: -8px 0 12px;
}

/* ===== Medicine Card ===== */
.med-card {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 14px 16px;
  margin-bottom: 10px;
  box-shadow: var(--shadow);
  display: flex;
  align-items: center;
  gap: 12px;
  transition: transform 0.15s ease;
  cursor: pointer;
  border-left: 4px solid var(--success);
}

.med-card:active {
  transform: scale(0.98);
}

.med-card.status-expiring {
  border-left-color: var(--warning);
}

.med-card.status-expired {
  border-left-color: var(--danger);
}

.med-card-body {
  flex: 1;
  min-width: 0;
}

.med-card-name {
  font-size: 15px;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.med-card-meta {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 3px;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.med-card-badge {
  font-size: 11px;
  font-weight: 600;
  padding: 2px 8px;
  border-radius: 20px;
  white-space: nowrap;
  flex-shrink: 0;
}

.badge-safe {
  background: var(--success-light);
  color: #15803d;
}

.badge-expiring {
  background: var(--warning-light);
  color: #92400e;
}

.badge-expired {
  background: var(--danger-light);
  color: #dc2626;
}

.med-card-actions {
  display: flex;
  gap: 4px;
  flex-shrink: 0;
}

/* ===== Search Bar ===== */
.search-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--bg);
  border-radius: var(--radius-sm);
  padding: 8px 12px;
  margin-top: 10px;
}

.search-bar input {
  border: none;
  background: none;
  outline: none;
  width: 100%;
  font-size: 14px;
  color: var(--text);
}

.search-bar svg {
  color: var(--text-tertiary);
  flex-shrink: 0;
}

/* ===== Filter Chips ===== */
.filter-chips {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 4px;
  margin-bottom: 12px;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.filter-chips::-webkit-scrollbar { display: none; }

.chip {
  display: inline-flex;
  align-items: center;
  padding: 6px 14px;
  border-radius: 20px;
  font-size: 13px;
  font-weight: 500;
  background: var(--surface);
  border: 1.5px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  white-space: nowrap;
  transition: all 0.15s ease;
  flex-shrink: 0;
}

.chip:active { transform: scale(0.95); }

.chip.active {
  background: var(--primary);
  border-color: var(--primary);
  color: #fff;
}

.chip-warning.active {
  background: var(--warning);
  border-color: var(--warning);
}

.chip-danger.active {
  background: var(--danger);
  border-color: var(--danger);
}

/* ===== Empty State ===== */
.empty-state {
  text-align: center;
  padding: 40px 20px;
  color: var(--text-secondary);
}

.empty-icon {
  font-size: 48px;
  margin-bottom: 12px;
}

.empty-state p {
  font-size: 14px;
}

/* ===== Bottom Navigation ===== */
.bottom-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  height: calc(var(--nav-height) + var(--safe-bottom));
  padding-bottom: var(--safe-bottom);
  background: var(--surface);
  border-top: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-around;
  z-index: 100;
}

.nav-item {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 6px 16px;
  border: none;
  background: none;
  color: var(--text-tertiary);
  font-size: 11px;
  cursor: pointer;
  transition: color 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.nav-item.active {
  color: var(--primary);
}

.nav-item.active svg {
  stroke: var(--primary);
}

.nav-fab {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  background: var(--primary);
  color: white;
  border: none;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 4px 14px rgba(14, 165, 233, 0.4);
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
  margin-top: -20px;
  -webkit-tap-highlight-color: transparent;
}

.nav-fab:active {
  transform: scale(0.92);
  background: var(--primary-dark);
}

/* ===== Modal ===== */
.modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: flex-end;
  background: rgba(0,0,0,0.4);
  transition: opacity 0.2s ease;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--surface);
  width: 100%;
  max-height: 92vh;
  border-radius: 20px 20px 0 0;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  overflow-y: auto;
  animation: slideUp 0.25s ease;
}

@keyframes slideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 20px;
}

.modal-header h2 {
  font-size: 18px;
  font-weight: 700;
}

/* ===== Form ===== */
.form-group {
  margin-bottom: 16px;
  flex: 1;
}

.form-group label {
  display: block;
  font-size: 13px;
  font-weight: 600;
  color: var(--text-secondary);
  margin-bottom: 6px;
}

.required { color: var(--danger); }

.form-group input[type="text"],
.form-group input[type="number"],
.form-group input[type="date"],
.form-group select,
.form-group textarea {
  width: 100%;
  height: 44px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 12px;
  font-size: 15px;
  color: var(--text);
  background: var(--surface);
  outline: none;
  transition: border-color 0.15s ease;
}

.form-group textarea {
  height: auto;
  padding: 10px 12px;
  resize: none;
}

.form-group input:focus,
.form-group select:focus,
.form-group textarea:focus {
  border-color: var(--primary);
}

.form-row {
  display: flex;
  gap: 12px;
}

.form-actions {
  display: flex;
  gap: 12px;
  margin-top: 24px;
}

.form-actions .btn { flex: 1; }

/* ===== Reminder Toggles ===== */
.reminder-toggles {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.reminder-tag {
  display: inline-flex;
  align-items: center;
  padding: 6px 12px;
  border-radius: 20px;
  font-size: 13px;
  background: var(--bg);
  border: 1.5px solid var(--border);
  color: var(--text-secondary);
  cursor: pointer;
  transition: all 0.15s ease;
  user-select: none;
}

.reminder-tag.active {
  background: var(--primary-light);
  border-color: var(--primary);
  color: var(--primary-dark);
  font-weight: 500;
}

/* ===== Buttons ===== */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  height: 44px;
  padding: 0 20px;
  border-radius: var(--radius-sm);
  font-size: 15px;
  font-weight: 600;
  border: none;
  cursor: pointer;
  transition: all 0.15s ease;
  -webkit-tap-highlight-color: transparent;
}

.btn:active { transform: scale(0.97); }

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:active { background: var(--primary-dark); }

.btn-danger {
  background: var(--danger);
  color: #fff;
}

.btn-outline {
  background: transparent;
  border: 1.5px solid var(--border);
  color: var(--text-secondary);
}

.btn-outline:active { background: var(--bg); }

.btn-block { width: 100%; }

.btn-sm {
  height: 36px;
  font-size: 13px;
  padding: 0 14px;
}

.btn-icon {
  width: 36px;
  height: 36px;
  border: none;
  background: none;
  color: var(--text-secondary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.15s ease;
}

.btn-icon:active { background: var(--bg); }

.btn-icon-sm {
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  color: var(--text-tertiary);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
}

.btn-icon-sm:active { background: var(--bg); }

/* ===== Overlay / Alert ===== */
.overlay {
  position: fixed;
  inset: 0;
  z-index: 300;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.overlay.hidden { display: none; }

.alert-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 360px;
  width: 100%;
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  box-shadow: var(--shadow-lg);
  animation: popIn 0.2s ease;
}

@keyframes popIn {
  from { transform: scale(0.9); opacity: 0; }
  to   { transform: scale(1); opacity: 1; }
}

.alert-header {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 16px;
  color: var(--warning);
}

.alert-header h2 {
  font-size: 17px;
  color: var(--text);
}

.alert-body {
  overflow-y: auto;
  margin-bottom: 20px;
  flex: 1;
}

.alert-item {
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.alert-item:last-child { border-bottom: none; }

.alert-item-name {
  font-weight: 600;
}

.alert-item-detail {
  font-size: 12px;
  color: var(--text-secondary);
  margin-top: 2px;
}

.alert-item-expired .alert-item-name { color: var(--danger); }
.alert-item-expiring .alert-item-name { color: var(--warning); }

/* ===== Confirm Dialog ===== */
.confirm-panel {
  background: var(--surface);
  border-radius: var(--radius);
  padding: 24px;
  max-width: 320px;
  width: 100%;
  box-shadow: var(--shadow-lg);
  animation: popIn 0.2s ease;
}

.confirm-panel p {
  font-size: 15px;
  margin-bottom: 20px;
  line-height: 1.6;
}

.confirm-actions {
  display: flex;
  gap: 12px;
}

.confirm-actions .btn { flex: 1; height: 40px; }

/* ===== Toast ===== */
.toast {
  position: fixed;
  bottom: calc(var(--nav-height) + var(--safe-bottom) + 20px);
  left: 50%;
  transform: translateX(-50%);
  background: var(--text);
  color: #fff;
  padding: 10px 24px;
  border-radius: 24px;
  font-size: 14px;
  font-weight: 500;
  z-index: 400;
  white-space: nowrap;
  box-shadow: var(--shadow-md);
  animation: toastIn 0.25s ease;
}

.toast.hidden { display: none; }

@keyframes toastIn {
  from { opacity: 0; transform: translateX(-50%) translateY(16px); }
  to   { opacity: 1; transform: translateX(-50%) translateY(0); }
}

/* ===== Settings ===== */
.settings-list {
  background: var(--surface);
  border-radius: var(--radius);
  overflow: hidden;
  box-shadow: var(--shadow);
  margin-bottom: 12px;
}

.settings-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s ease;
  position: relative;
}

.settings-item:last-child { border-bottom: none; }

.settings-item:active { background: var(--bg); }

.settings-item.danger { color: var(--danger); }

.settings-arrow {
  color: var(--text-tertiary);
  font-size: 18px;
}

.settings-item .file-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.rule-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.rule-item:last-child { border-bottom: none; }

.rule-item-text {
  font-size: 14px;
}

.category-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
}

.category-item:last-child { border-bottom: none; }

.category-item-name {
  font-size: 14px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.add-category-row {
  display: flex;
  gap: 8px;
}

.add-category-row input {
  flex: 1;
  height: 36px;
  border: 1.5px solid var(--border);
  border-radius: var(--radius-sm);
  padding: 0 12px;
  font-size: 14px;
  outline: none;
}

.add-category-row input:focus { border-color: var(--primary); }

/* ===== App Info ===== */
.app-info {
  text-align: center;
  padding: 32px 20px 16px;
  color: var(--text-tertiary);
  font-size: 12px;
  line-height: 1.8;
}

/* ===== Utilities ===== */
.hidden { display: none !important; }

/* ===== Responsive (tablet+) ===== */
@media (min-width: 640px) {
  .page-content {
    max-width: 480px;
    margin: 0 auto;
  }

  .bottom-nav {
    max-width: 480px;
    left: 50%;
    transform: translateX(-50%);
    border-radius: 20px 20px 0 0;
  }

  .modal-content {
    max-width: 480px;
    margin: 0 auto;
  }
}
