/* ─── Variables ─────────────────────────────────────────── */
:root {
  --primary: #4A90D9;
  --primary-dark: #3570B2;
  --primary-light: #EBF3FD;
  --accent: #F0595C;
  --bg-page: #F0F4F8;
  --bg-card: #FFFFFF;
  --text-primary: #1E293B;
  --text-secondary: #64748B;
  --text-muted: #94A3B8;
  --border: #E2E8F0;
  --border-hover: #CBD5E1;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.06), 0 1px 2px rgba(0,0,0,0.04);
  --shadow-md: 0 4px 12px rgba(0,0,0,0.08), 0 2px 4px rgba(0,0,0,0.04);
  --shadow-lg: 0 12px 32px rgba(0,0,0,0.10), 0 4px 8px rgba(0,0,0,0.06);
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --header-h: 60px;
  --tab-h: 52px;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

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

html {
  scroll-behavior: smooth;
  -webkit-text-size-adjust: 100%;
}

body {
  font-family: 'Noto Sans TC', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  background: var(--bg-page);
  color: var(--text-primary);
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  line-height: 1.6;
  -webkit-font-smoothing: antialiased;
  overflow-x: hidden;
}

body.ios-app {
  overscroll-behavior: none;
}

/* ─── Scrollbar ──────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-hover); border-radius: 99px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-muted); }

/* ─── Selection ──────────────────────────────────────────── */
::selection { background: var(--primary-light); color: var(--primary-dark); }

/* ─── Header ─────────────────────────────────────────────── */
.app-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: rgba(255,255,255,0.88);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--border);
  min-height: calc(var(--header-h) + var(--safe-top));
  padding-top: var(--safe-top);
}

.header-inner {
  max-width: 1120px;
  margin: 0 auto;
  padding: 0 16px;
  min-height: var(--header-h);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text-primary);
  white-space: nowrap;
}

.stats-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 0.8rem;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 4px;
}

.stat-num {
  font-weight: 600;
  color: var(--primary);
  font-variant-numeric: tabular-nums;
}

.stat-label {
  color: var(--text-secondary);
}

.stat-divider {
  color: var(--border-hover);
}

/* ─── Main ────────────────────────────────────────────────── */
.app-main {
  flex: 1;
  max-width: 1120px;
  margin: 0 auto;
  width: 100%;
  padding: 0 12px 40px;
}

/* ─── Search Section ─────────────────────────────────────── */
.search-section {
  padding: 20px 0 0;
}

.search-box-wrap {
  margin-bottom: 16px;
}

.search-box {
  position: relative;
  display: flex;
  align-items: center;
  background: var(--bg-card);
  border: 1.5px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 0 14px;
  height: 52px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.2s, box-shadow 0.2s;
}

.search-box:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(74,144,217,0.12), var(--shadow-sm);
}

.search-icon {
  flex-shrink: 0;
  margin-right: 10px;
}

.search-input {
  flex: 1;
  border: none;
  outline: none;
  background: transparent;
  font-family: inherit;
  font-size: 16px;
  color: var(--text-primary);
  height: 100%;
}

.search-input::placeholder {
  color: var(--text-muted);
}

.search-clear {
  flex-shrink: 0;
  display: none;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border: none;
  background: var(--bg-page);
  border-radius: 50%;
  cursor: pointer;
  margin-left: 8px;
  transition: background 0.15s;
}

.search-clear:hover { background: var(--border); }
.search-clear.visible { display: flex; }

/* ─── Tabs ────────────────────────────────────────────────── */
.tabs-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}
.tabs-wrap::-webkit-scrollbar { display: none; }

.tabs {
  display: flex;
  gap: 4px;
  min-width: max-content;
}

.search-mode-wrap {
  display: inline-flex;
  gap: 6px;
  margin-top: 14px;
  padding: 4px;
  background: rgba(255,255,255,0.82);
  border: 1px solid var(--border);
  border-radius: 999px;
  box-shadow: var(--shadow-sm);
}

.search-fields-wrap {
  display: flex;
  flex-wrap: wrap;
  gap: 10px 14px;
  margin-top: 12px;
  color: #475569;
  font-size: 0.95rem;
}

.search-fields-wrap label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.recent-searches {
  margin-top: 14px;
}

.recent-searches-title {
  font-size: 0.9rem;
  color: #64748B;
  margin-bottom: 8px;
}

.recent-searches-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.recent-search-chip {
  border: 1px solid #CBD5E1;
  background: #F8FAFC;
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 0.85rem;
  color: #334155;
}

.search-mode-btn {
  border: none;
  background: transparent;
  color: var(--text-secondary);
  font: inherit;
  font-size: 0.88rem;
  font-weight: 600;
  padding: 8px 14px;
  border-radius: 999px;
  cursor: pointer;
}

.search-mode-btn.active {
  background: var(--primary);
  color: #fff;
  box-shadow: 0 4px 12px rgba(74, 144, 217, 0.25);
}

.tab {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 18px;
  border: none;
  background: transparent;
  border-radius: var(--radius-md);
  font-family: inherit;
  font-size: 0.92rem;
  font-weight: 500;
  color: var(--text-secondary);
  cursor: pointer;
  transition: background 0.15s, color 0.15s;
  white-space: nowrap;
}

.tab:hover {
  background: rgba(74,144,217,0.07);
  color: var(--primary);
}

.tab.active {
  background: var(--primary);
  color: #fff;
}

.tab-count {
  font-size: 0.75rem;
  padding: 1px 7px;
  border-radius: 99px;
  background: rgba(0,0,0,0.08);
  font-weight: 600;
}

.tab.active .tab-count {
  background: rgba(255,255,255,0.22);
}

/* ─── Loading ─────────────────────────────────────────────── */
.loading-state {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 80px 20px;
  gap: 16px;
  color: var(--text-secondary);
  font-size: 0.95rem;
}

.spinner {
  width: 36px;
  height: 36px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin 0.7s linear infinite;
}

@keyframes spin { to { transform: rotate(360deg); } }

/* ─── Results Meta ────────────────────────────────────────── */
.results-section { padding-top: 20px; }

.results-meta {
  font-size: 0.85rem;
  color: var(--text-secondary);
  margin-bottom: 14px;
  padding: 0 2px;
}

.results-meta strong {
  color: var(--primary);
  font-weight: 600;
}

/* ─── Results Grid ────────────────────────────────────────── */
.results-grid {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

/* ─── Medicine Card ───────────────────────────────────────── */
.medicine-card {
  background: var(--bg-card);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  padding: 14px 16px;
  cursor: pointer;
  transition: border-color 0.15s, box-shadow 0.15s, transform 0.1s;
  text-align: left;
  width: 100%;
  font-family: inherit;
}

html.ios-app .medicine-card,
html.ios-app .modal-sheet,
html.ios-app .search-box,
html.ios-app .tab {
  transform: none !important;
  animation: none !important;
}

.medicine-card:hover {
  border-color: var(--primary);
  box-shadow: var(--shadow-md);
  transform: translateY(-1px);
}

.medicine-card:active {
  transform: translateY(0);
}

.card-top {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  margin-bottom: 8px;
}

.card-type-badge {
  flex-shrink: 0;
  padding: 2px 9px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.02em;
}

.badge-xiyao {
  background: #EBF3FD;
  color: #2B6CB0;
}

.badge-zhongcheng {
  background: #F0FDF4;
  color: #15803D;
}

.card-names {
  flex: 1;
  min-width: 0;
}

.card-name-primary {
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text-primary);
  line-height: 1.4;
  margin-bottom: 2px;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-name-secondary {
  font-size: 0.8rem;
  color: var(--text-secondary);
  display: -webkit-box;
  -webkit-line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.card-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.chip {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 10px;
  background: var(--bg-page);
  border: 1px solid var(--border);
  border-radius: 99px;
  font-size: 0.75rem;
  color: var(--text-secondary);
  white-space: nowrap;
}

.chip-label {
  font-weight: 500;
  color: var(--text-muted);
  font-size: 0.7rem;
}

.chip.highlight {
  background: var(--primary-light);
  border-color: rgba(74,144,217,0.25);
  color: var(--primary-dark);
}

/* ─── No Results ──────────────────────────────────────────── */
.no-results {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 60px 20px;
  gap: 12px;
  text-align: center;
}

.no-results p {
  font-size: 1.05rem;
  font-weight: 600;
  color: var(--text-secondary);
}

.no-results span {
  font-size: 0.88rem;
  color: var(--text-muted);
}

/* ─── Detail Modal ────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(15,23,42,0.45);
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  z-index: 200;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s;
}

.modal-overlay.open {
  opacity: 1;
  pointer-events: all;
}

.modal-sheet {
  background: var(--bg-card);
  border-radius: var(--radius-xl) var(--radius-xl) 0 0;
  width: 100%;
  max-width: 1040px;
  max-height: calc(90dvh - var(--safe-top));
  overflow-y: auto;
  transform: translateY(100%);
  transition: transform 0.3s cubic-bezier(0.32, 0.72, 0, 1);
}

.modal-overlay.open .modal-sheet {
  transform: translateY(0);
}

@media (min-width: 768px) {
  .modal-overlay {
    align-items: center;
  }
  .modal-overlay.open .modal-sheet {
    transform: translateY(0);
    border-radius: var(--radius-xl);
    max-height: 88vh;
    margin: auto;
  }
}

.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
  padding: 20px 20px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-card);
  z-index: 1;
}

.modal-title-wrap {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.modal-badge {
  display: inline-flex;
  padding: 2px 10px;
  border-radius: 99px;
  font-size: 0.72rem;
  font-weight: 600;
  width: fit-content;
}

.modal-badge.xiyao {
  background: #EBF3FD;
  color: #2B6CB0;
}

.modal-badge.zhongcheng {
  background: #F0FDF4;
  color: #15803D;
}

.modal-title {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--text-primary);
  line-height: 1.35;
}

.modal-close {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: var(--bg-page);
  border-radius: 50%;
  cursor: pointer;
  transition: background 0.15s;
}

.modal-close:hover { background: var(--border); }

.modal-body {
  padding: 20px 20px calc(20px + var(--safe-bottom));
}

.detail-grid {
  display: grid;
  gap: 12px;
}

.detail-row {
  display: grid;
  grid-template-columns: 110px minmax(0, 1fr);
  gap: 12px;
  padding: 12px 14px;
  background: var(--bg-page);
  border-radius: var(--radius-sm);
  align-items: start;
}

.detail-row.highlight-row {
  background: var(--primary-light);
}

.detail-label {
  font-size: 0.78rem;
  font-weight: 600;
  color: var(--text-muted);
  padding-top: 2px;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.detail-value {
  font-size: 0.95rem;
  color: var(--text-primary);
  line-height: 1.65;
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: normal;
  writing-mode: horizontal-tb;
}

.detail-value.empty {
  color: var(--text-muted);
  font-style: italic;
}

.detail-section-title {
  font-size: 0.78rem;
  font-weight: 700;
  color: var(--text-secondary);
  text-transform: uppercase;
  letter-spacing: 0.06em;
  margin-top: 4px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  grid-column: 1 / -1;
}

.ingredient-list {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.ingredient-tag {
  display: inline-block;
  padding: 2px 10px;
  background: var(--primary-light);
  color: var(--primary-dark);
  border-radius: 99px;
  font-size: 0.8rem;
}

/* ─── Footer ─────────────────────────────────────────────── */
.app-footer {
  text-align: center;
  padding: 20px 20px calc(20px + var(--safe-bottom));
  font-size: 0.78rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

html.ios-app body {
  background: linear-gradient(to bottom, #ffffff 0, #ffffff calc(var(--safe-top) + 12px), var(--bg-page) calc(var(--safe-top) + 12px));
}

/* ─── Responsive ──────────────────────────────────────────── */
@media (min-width: 640px) {
  .app-main { padding: 0 20px 40px; }
  .results-grid { grid-template-columns: 1fr 1fr; gap: 12px; }
  .search-section { padding-top: 24px; }
}

@media (min-width: 768px) {
  .results-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .modal-body { padding: 24px; }
  .detail-grid {
    grid-template-columns: 1fr 1fr;
    gap: 14px;
  }
  .detail-row {
    grid-template-columns: 120px minmax(0, 1fr);
    min-height: 72px;
  }
  .detail-row.highlight-row,
  .detail-section-title {
    grid-column: 1 / -1;
  }
}

@media (min-width: 1100px) {
  .results-grid { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 14px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}

/* ─── Animations ──────────────────────────────────────────── */
@keyframes fadeSlideIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.medicine-card {
  animation: fadeSlideIn 0.2s ease-out both;
}

/* stagger cards */
.medicine-card:nth-child(1)  { animation-delay: 0ms; }
.medicine-card:nth-child(2)  { animation-delay: 30ms; }
.medicine-card:nth-child(3)  { animation-delay: 60ms; }
.medicine-card:nth-child(4)  { animation-delay: 90ms; }
.medicine-card:nth-child(5)  { animation-delay: 120ms; }
.medicine-card:nth-child(n+6){ animation-delay: 150ms; }
