:root {
  --bg: #f5f7f5;
  --bg-card: #ffffff;
  --primary: #0f766e;
  --primary-light: #14b8a6;
  --primary-dark: #115e59;
  --accent: #ea580c;
  --text: #1f2937;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --success: #16a34a;
  --warn: #d97706;
  --danger: #dc2626;
  --hot: #ef4444;
  --cool: #3b82f6;
  --warm: #f97316;
  --neutral: #84cc16;
  --bitter: #8b5cf6;
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  font-family: 'Noto Sans Thai', 'Noto Sans SC', 'Inter', system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 15px;
  line-height: 1.5;
  -webkit-text-size-adjust: 100%;
}

body[data-lang="en"] { font-family: 'Inter', 'Noto Sans Thai', system-ui, sans-serif; }
body[data-lang="zh"] { font-family: 'Noto Sans SC', 'Noto Sans Thai', system-ui, sans-serif; }

.topbar {
  position: sticky; top: 0; z-index: 50;
  background: linear-gradient(135deg, var(--primary) 0%, var(--primary-dark) 100%);
  color: white;
  padding: 12px 16px;
  display: flex; align-items: center; justify-content: space-between;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 17px; }
.leaf { font-size: 22px; }
.lang-switch { display: flex; gap: 4px; background: rgba(255,255,255,0.15); border-radius: 8px; padding: 3px; }
.lang-switch button {
  background: transparent; color: rgba(255,255,255,0.8);
  border: none; padding: 5px 10px; border-radius: 6px; font-size: 12px;
  font-weight: 600; cursor: pointer; transition: all 0.2s;
}
.lang-switch button.active { background: white; color: var(--primary-dark); }

.modes {
  display: flex; gap: 4px; padding: 8px; background: var(--bg-card);
  border-bottom: 1px solid var(--border);
  overflow-x: auto; -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
  position: sticky; top: 56px; z-index: 40;
}
.modes::-webkit-scrollbar { display: none; }
.mode-btn {
  flex: 0 0 auto;
  background: transparent; border: 1px solid var(--border);
  padding: 8px 14px; border-radius: 20px; font-size: 13px;
  display: flex; align-items: center; gap: 6px;
  cursor: pointer; white-space: nowrap;
  color: var(--text); font-family: inherit;
}
.mode-btn.active { background: var(--primary); color: white; border-color: var(--primary); }
.mode-btn .ic { font-size: 14px; }

main { padding: 12px; min-height: calc(100vh - 110px); }

/* === Browse === */
.search-bar {
  display: flex; gap: 8px; margin-bottom: 12px;
}
.search-bar input {
  flex: 1; padding: 10px 14px; border: 1px solid var(--border);
  border-radius: 10px; font-size: 14px; font-family: inherit;
  background: var(--bg-card);
}
.search-bar input:focus { outline: 2px solid var(--primary-light); border-color: var(--primary); }

.filters {
  display: flex; gap: 6px; flex-wrap: wrap; margin-bottom: 12px;
}
.filter-chip {
  padding: 5px 12px; border: 1px solid var(--border); border-radius: 14px;
  font-size: 12px; cursor: pointer; background: var(--bg-card);
}
.filter-chip.active { background: var(--accent); color: white; border-color: var(--accent); }

.herb-grid {
  display: grid; grid-template-columns: 1fr; gap: 10px;
}
@media (min-width: 640px) { .herb-grid { grid-template-columns: 1fr 1fr; } }
@media (min-width: 1024px) { .herb-grid { grid-template-columns: 1fr 1fr 1fr; } }

.herb-card {
  background: var(--bg-card); border-radius: 12px; padding: 14px;
  border: 1px solid var(--border); cursor: pointer;
  display: flex; gap: 12px; align-items: flex-start;
  transition: transform 0.15s, box-shadow 0.15s;
}
.herb-card:active { transform: scale(0.98); }
.herb-card .num {
  flex: 0 0 32px; height: 32px; border-radius: 8px;
  background: var(--primary); color: white; font-weight: 700;
  display: flex; align-items: center; justify-content: center; font-size: 12px;
}
.herb-card .body { flex: 1; min-width: 0; }
.herb-card .name-th { font-weight: 700; font-size: 16px; }
.herb-card .name-en { font-size: 13px; color: var(--text-muted); }
.herb-card .name-zh { font-size: 13px; color: var(--text-muted); font-family: 'Noto Sans SC', sans-serif; }
.herb-card .taste-row { margin-top: 6px; display: flex; gap: 4px; flex-wrap: wrap; }

.taste-tag {
  padding: 2px 8px; border-radius: 10px; font-size: 11px; font-weight: 600;
  color: white;
}
.taste-tag[data-rasa~="เผ็ดร้อน"], .taste-tag[data-rasa~="เผ็ด"] { background: var(--hot); }
.taste-tag[data-rasa~="ร้อน"] { background: var(--warm); }
.taste-tag[data-rasa~="เย็น"] { background: var(--cool); }
.taste-tag[data-rasa~="ขม"] { background: var(--bitter); }
.taste-tag[data-rasa~="หวาน"] { background: var(--neutral); }
.taste-tag[data-rasa~="เปรี้ยว"] { background: #eab308; }
.taste-tag[data-rasa~="ฝาด"] { background: #78716c; }
.taste-tag[data-rasa~="เค็ม"] { background: #06b6d4; }
.taste-tag[data-rasa~="มัน"] { background: #a16207; }

/* === Detail modal === */
.modal-overlay {
  position: fixed; inset: 0; background: rgba(0,0,0,0.5);
  display: flex; align-items: flex-end; justify-content: center;
  z-index: 100; padding: 0;
}
.modal {
  background: var(--bg); border-radius: 16px 16px 0 0;
  width: 100%; max-width: 720px; max-height: 90vh;
  overflow-y: auto; padding: 20px;
  animation: slideUp 0.25s ease;
}
@media (min-width: 640px) {
  .modal-overlay { align-items: center; padding: 20px; }
  .modal { border-radius: 16px; max-height: 85vh; }
}
@keyframes slideUp { from { transform: translateY(100%); } to { transform: translateY(0); } }
.modal-close {
  float: right; background: var(--border); border: none;
  width: 32px; height: 32px; border-radius: 50%; font-size: 18px; cursor: pointer;
}
.modal h2 { font-size: 22px; margin-bottom: 4px; }
.modal .sub { color: var(--text-muted); margin-bottom: 14px; }
.modal h3 { font-size: 14px; color: var(--primary-dark); margin: 12px 0 6px; text-transform: uppercase; letter-spacing: 0.5px; }
.modal .props { background: var(--bg-card); padding: 12px; border-radius: 10px; border: 1px solid var(--border); }
.modal .props ul { padding-left: 18px; }
.modal .props li { margin-bottom: 4px; font-size: 14px; }
.modal .page-img { margin-top: 16px; }
.modal .page-img img { width: 100%; border-radius: 10px; border: 1px solid var(--border); }
.modal .tags { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 8px; }
.modal .tag { padding: 3px 9px; background: var(--bg-card); border: 1px solid var(--border); border-radius: 12px; font-size: 12px; }

/* === Flashcard === */
.flash-wrap {
  display: flex; flex-direction: column; align-items: center; gap: 12px;
  padding: 8px;
}
.flash-progress { font-size: 13px; color: var(--text-muted); }
.flash-card {
  width: 100%; max-width: 480px; min-height: 380px;
  background: var(--bg-card); border-radius: 16px; padding: 24px;
  box-shadow: 0 4px 16px rgba(0,0,0,0.08);
  display: flex; flex-direction: column; justify-content: center; align-items: center;
  text-align: center; cursor: pointer;
  border: 1px solid var(--border);
  perspective: 1000px;
}
.flash-card .big-name { font-size: 32px; font-weight: 700; color: var(--primary-dark); margin-bottom: 8px; }
.flash-card .alt-name { font-size: 18px; color: var(--text-muted); margin-bottom: 4px; }
.flash-card .hint { font-size: 12px; color: var(--text-muted); margin-top: 14px; }
.flash-card .answer { width: 100%; text-align: left; }
.flash-card .answer .label { font-size: 11px; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.5px; margin-top: 10px; }
.flash-card .answer .val { font-size: 15px; margin-top: 2px; }
.flash-controls { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; }
.flash-controls button {
  padding: 10px 18px; border-radius: 10px; border: 1px solid var(--border);
  background: var(--bg-card); font-family: inherit; font-size: 13px;
  cursor: pointer; font-weight: 600;
}
.flash-controls .ok { background: var(--success); color: white; border-color: var(--success); }
.flash-controls .meh { background: var(--warn); color: white; border-color: var(--warn); }
.flash-controls .bad { background: var(--danger); color: white; border-color: var(--danger); }

/* === Quiz === */
.quiz-wrap { max-width: 540px; margin: 0 auto; }
.quiz-progress { display: flex; justify-content: space-between; margin-bottom: 14px; font-size: 13px; color: var(--text-muted); }
.quiz-bar { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin-bottom: 18px; }
.quiz-bar-fill { height: 100%; background: var(--primary); transition: width 0.3s; }
.quiz-q { background: var(--bg-card); border-radius: 12px; padding: 18px; border: 1px solid var(--border); margin-bottom: 14px; }
.quiz-q .qlabel { font-size: 12px; color: var(--text-muted); margin-bottom: 6px; text-transform: uppercase; }
.quiz-q .qtext { font-size: 18px; font-weight: 700; }
.quiz-opts { display: flex; flex-direction: column; gap: 8px; }
.quiz-opt {
  background: var(--bg-card); border: 1.5px solid var(--border); border-radius: 10px;
  padding: 14px; font-size: 15px; cursor: pointer; text-align: left;
  font-family: inherit; transition: all 0.15s;
}
.quiz-opt:hover { border-color: var(--primary-light); }
.quiz-opt.correct { background: #dcfce7; border-color: var(--success); color: var(--success); }
.quiz-opt.wrong { background: #fee2e2; border-color: var(--danger); color: var(--danger); }
.quiz-opt.dim { opacity: 0.5; }
.quiz-next { width: 100%; padding: 14px; background: var(--primary); color: white; border: none; border-radius: 10px; font-size: 15px; font-weight: 600; margin-top: 14px; cursor: pointer; font-family: inherit; }
.quiz-result { text-align: center; padding: 24px; background: var(--bg-card); border-radius: 12px; }
.quiz-result .score { font-size: 48px; font-weight: 700; color: var(--primary); }

/* === Pages mode === */
.pages-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; }
.page-thumb { aspect-ratio: 3/4; border-radius: 8px; overflow: hidden; cursor: pointer; position: relative; border: 1px solid var(--border); }
.page-thumb img { width: 100%; height: 100%; object-fit: cover; }
.page-thumb .pn { position: absolute; top: 4px; left: 4px; background: rgba(15,118,110,0.9); color: white; padding: 2px 8px; border-radius: 6px; font-size: 11px; font-weight: 700; }

/* === Stats === */
.stat-card { background: var(--bg-card); padding: 16px; border-radius: 12px; margin-bottom: 10px; border: 1px solid var(--border); }
.stat-card h3 { font-size: 13px; color: var(--text-muted); margin-bottom: 6px; }
.stat-card .val { font-size: 28px; font-weight: 700; color: var(--primary); }
.stat-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--border); font-size: 14px; }
.stat-row:last-child { border-bottom: none; }

/* Toast */
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%) translateY(100px);
  background: var(--primary-dark); color: white; padding: 10px 18px; border-radius: 8px;
  z-index: 200; transition: transform 0.3s; font-size: 14px; max-width: 90%;
}
.toast.show { transform: translateX(-50%) translateY(0); }

/* Empty state */
.empty { text-align: center; padding: 40px 20px; color: var(--text-muted); }
.empty h2 { font-size: 18px; margin-bottom: 8px; color: var(--text); }
