:root {
  --bg: #0f172a;
  --surface: #1e293b;
  --surface2: #253347;
  --border: rgba(59, 130, 246, 0.15);
  --text: #f8fafc;
  --muted: #94a3b8;
  --accent: #3b82f6;
  --accent-dim: #60a5fa;
  --warn: #f4a261;
  --protein: #7c9cff;
  --carbs: #ffd166;
  --fat: #ee9b6a;
  --radius: 14px;
  --font: "Sarabun", "Segoe UI", system-ui, sans-serif;
}

@import url("https://fonts.googleapis.com/css2?family=Sarabun:wght@400;500;600;700&display=swap");

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
  min-height: 100dvh;
  padding-bottom: 2rem;
}

.container {
  width: 100%;
  max-width: 520px;
  margin: 0 auto;
  padding: 0 16px;
}

/* Header */
.header {
  position: sticky;
  top: 0;
  z-index: 50;
  background: rgba(15, 20, 25, 0.92);
  backdrop-filter: blur(10px);
  border-bottom: 1px solid var(--border);
}

.header-inner {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 0;
}

.logo {
  display: flex;
  align-items: center;
  gap: 10px;
}

.logo-icon {
  font-size: 1.75rem;
  line-height: 1;
}

.logo-title {
  font-weight: 700;
  font-size: 1.15rem;
  letter-spacing: -0.02em;
}

.logo-sub {
  font-size: 0.78rem;
  color: var(--muted);
}

.header-cal {
  text-align: right;
  font-weight: 700;
  font-size: 1.1rem;
  color: var(--accent);
}

.header-cal small {
  display: block;
  font-size: 0.7rem;
  font-weight: 500;
  color: var(--muted);
}

/* Tabs */
.tabs {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  padding: 14px 0 10px;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.tabs::-webkit-scrollbar {
  display: none;
}

.tab {
  flex: 0 0 auto;
  padding: 10px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface);
  color: var(--muted);
  font-family: inherit;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s, color 0.15s, border-color 0.15s;
}

.tab:hover {
  color: var(--text);
  border-color: var(--muted);
}

.tab.active {
  background: var(--accent-dim);
  border-color: var(--accent);
  color: #fff;
}

.tab-pane {
  display: none;
  animation: fadeIn 0.2s ease;
}

.tab-pane.active {
  display: block;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}

/* Search */
.search-wrap {
  position: relative;
  margin-bottom: 12px;
}

.search-input {
  width: 100%;
  padding: 14px 44px 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
}

.search-input::placeholder {
  color: var(--muted);
}

.search-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(61, 214, 140, 0.15);
}

.search-icon {
  position: absolute;
  right: 14px;
  top: 50%;
  transform: translateY(-50%);
  opacity: 0.5;
  pointer-events: none;
}

.cat-scroll {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  padding-bottom: 8px;
  margin-bottom: 8px;
  scrollbar-width: thin;
}

.cat-btn {
  flex: 0 0 auto;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--muted);
  font-family: inherit;
  font-size: 0.8rem;
  cursor: pointer;
  transition: all 0.15s;
}

.cat-btn:hover {
  color: var(--text);
}

.cat-btn.active {
  background: var(--surface);
  border-color: var(--accent);
  color: var(--accent);
}

.results-info {
  font-size: 0.8rem;
  color: var(--muted);
  margin-bottom: 10px;
}

.food-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.food-card {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  text-align: left;
  width: 100%;
  font-family: inherit;
  color: inherit;
  transition: border-color 0.15s, transform 0.1s;
}

.food-card:hover {
  border-color: var(--accent-dim);
}

.food-card:active {
  transform: scale(0.99);
}

.food-card-name {
  font-weight: 600;
  font-size: 0.95rem;
}

.food-card-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-top: 2px;
}

.food-card-cal {
  font-weight: 700;
  color: var(--accent);
  font-size: 0.95rem;
}

/* Meal */
.section-title {
  margin: 0 0 4px;
  font-size: 1.25rem;
}

.section-desc {
  color: var(--muted);
  font-size: 0.9rem;
  margin: 0 0 16px;
}

.meal-top {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  margin-bottom: 16px;
}

.today-date {
  font-size: 0.85rem;
  color: var(--muted);
}

.ring-wrap {
  display: flex;
  align-items: center;
  gap: 20px;
  margin-bottom: 20px;
}

.ring-container {
  position: relative;
  width: 120px;
  height: 120px;
  flex-shrink: 0;
}

.ring-svg {
  width: 100%;
  height: 100%;
  transform: rotate(-90deg);
}

.ring-bg {
  fill: none;
  stroke: var(--surface2);
  stroke-width: 10;
}

.ring-fg {
  fill: none;
  stroke: var(--accent);
  stroke-width: 10;
  stroke-linecap: round;
  stroke-dasharray: 314;
  stroke-dashoffset: 314;
  transition: stroke-dashoffset 0.4s ease;
}

.ring-center {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  pointer-events: none;
}

.ring-cal {
  font-size: 1.35rem;
  font-weight: 700;
}

.ring-label {
  font-size: 0.7rem;
  color: var(--muted);
}

.ring-goal-text {
  font-size: 0.65rem;
  color: var(--muted);
  margin-top: 2px;
}

.ring-remain {
  flex: 1;
}

.remain-num {
  font-size: 1.75rem;
  font-weight: 700;
  color: var(--warn);
}

.remain-label {
  font-size: 0.85rem;
  color: var(--muted);
}

.macro-bars {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 18px;
}

.macro-row {
  display: grid;
  grid-template-columns: 88px 1fr 48px;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
}

.macro-row:last-child {
  margin-bottom: 0;
}

.macro-name {
  font-size: 0.8rem;
}

.macro-bar-wrap {
  height: 8px;
  background: var(--surface2);
  border-radius: 4px;
  overflow: hidden;
}

.macro-bar-fill {
  height: 100%;
  border-radius: 4px;
  width: 0%;
  max-width: 100%;
  transition: width 0.35s ease;
}

.macro-bar-fill.protein {
  background: var(--protein);
}

.macro-bar-fill.carbs {
  background: var(--carbs);
}

.macro-bar-fill.fat {
  background: var(--fat);
}

.macro-val {
  font-size: 0.8rem;
  font-weight: 600;
  text-align: right;
}

.meal-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  margin-bottom: 8px;
}

.meal-item-info {
  flex: 1;
  min-width: 0;
}

.meal-item-name {
  font-weight: 600;
  font-size: 0.9rem;
}

.meal-item-cal {
  font-size: 0.8rem;
  color: var(--accent);
}

.meal-qty {
  display: flex;
  align-items: center;
  gap: 6px;
}

.meal-qty button {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
}

.meal-qty span {
  font-size: 0.85rem;
  min-width: 44px;
  text-align: center;
}

.meal-remove {
  padding: 6px 10px;
  border: none;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  font-size: 1rem;
}

.meal-remove:hover {
  color: #f87171;
}

.empty-state {
  text-align: center;
  padding: 32px 16px;
  color: var(--muted);
}

.empty-icon {
  font-size: 2.5rem;
  margin-bottom: 8px;
}

.empty-sub {
  font-size: 0.85rem;
  margin-top: 4px;
}

.btn-save,
.btn-primary,
.btn-secondary {
  width: 100%;
  padding: 14px;
  border-radius: var(--radius);
  font-family: inherit;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  border: none;
  margin-top: 12px;
}

.btn-primary {
  background: linear-gradient(135deg, var(--accent), var(--accent-dim));
  color: #0a0f0c;
}

.btn-primary:hover {
  filter: brightness(1.05);
}

.btn-secondary {
  background: var(--surface2);
  color: var(--text);
  border: 1px solid var(--border);
  margin-top: 16px;
}

.btn-save {
  background: var(--surface2);
  color: var(--accent);
  border: 1px solid var(--accent-dim);
}

/* TDEE form */
.form-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px;
}

.form-group {
  margin-bottom: 14px;
}

.form-group label {
  display: block;
  font-size: 0.85rem;
  color: var(--muted);
  margin-bottom: 6px;
}

.form-group input,
.form-group select {
  width: 100%;
  padding: 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
  font-size: 1rem;
}

.radio-row {
  display: flex;
  gap: 16px;
}

.radio-opt {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  color: var(--text);
}

.result-card {
  margin-top: 20px;
  padding: 18px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.result-card h3 {
  margin: 0 0 14px;
  font-size: 1.05rem;
}

.result-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.result-box {
  text-align: center;
  padding: 12px 8px;
  background: var(--surface2);
  border-radius: 10px;
}

.result-box.highlight {
  border: 1px solid var(--accent);
  background: rgba(61, 214, 140, 0.08);
}

.result-num {
  font-size: 1.1rem;
  font-weight: 700;
  color: var(--accent);
}

.result-box:not(.highlight) .result-num {
  color: var(--text);
}

.result-lbl {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 4px;
}

.result-unit {
  font-size: 0.65rem;
  color: var(--muted);
}

.macro-rec {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid var(--border);
}

.macro-rec h4 {
  margin: 0 0 12px;
  font-size: 0.95rem;
}

.macro-rec-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.macro-rec-item {
  text-align: center;
  padding: 12px 6px;
  border-radius: 10px;
}

.protein-bg {
  background: rgba(124, 156, 255, 0.15);
}

.carbs-bg {
  background: rgba(255, 209, 102, 0.12);
}

.fat-bg {
  background: rgba(238, 155, 106, 0.15);
}

.mrec-val {
  font-weight: 700;
  font-size: 0.95rem;
}

.mrec-lbl {
  font-size: 0.7rem;
  color: var(--muted);
  margin-top: 4px;
}

/* History */
.history-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 14px;
  margin-bottom: 10px;
}

.history-date {
  font-weight: 600;
  margin-bottom: 8px;
}

.history-cal {
  color: var(--accent);
  font-size: 0.9rem;
}

.history-items {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 6px;
}

/* Modal */
.modal-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 100;
  background: rgba(0, 0, 0, 0.65);
  align-items: flex-end;
  justify-content: center;
  padding: 0;
}

.modal-backdrop.open {
  display: flex;
}

@media (min-width: 480px) {
  .modal-backdrop.open {
    align-items: center;
    padding: 20px;
  }
}

.modal {
  width: 100%;
  max-width: 480px;
  max-height: 90vh;
  overflow-y: auto;
  background: var(--surface);
  border-radius: var(--radius) var(--radius) 0 0;
  border: 1px solid var(--border);
  border-bottom: none;
}

@media (min-width: 480px) {
  .modal {
    border-radius: var(--radius);
    border-bottom: 1px solid var(--border);
  }
}

.modal-head {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 16px 16px 0;
  gap: 12px;
}

.modal-food-name {
  font-weight: 700;
  font-size: 1.1rem;
}

.modal-food-sub {
  font-size: 0.8rem;
  color: var(--muted);
  margin-top: 4px;
}

.modal-close {
  width: 36px;
  height: 36px;
  border: none;
  border-radius: 10px;
  background: var(--surface2);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
}

.modal-body {
  padding: 16px;
}

.serving-label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 0.85rem;
}

.use-default-btn {
  font-family: inherit;
  font-size: 0.8rem;
  padding: 6px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--accent);
  cursor: pointer;
}

.serving-controls {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.srv-btn {
  width: 48px;
  height: 48px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-size: 1.4rem;
  cursor: pointer;
}

#srv-input {
  flex: 1;
  text-align: center;
  padding: 12px;
  font-size: 1.1rem;
  font-weight: 600;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-family: inherit;
}

.quick-btns {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 16px;
}

.quick-btn {
  padding: 8px 14px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--surface2);
  color: var(--text);
  font-family: inherit;
  font-size: 0.85rem;
  cursor: pointer;
}

.quick-btn:hover {
  border-color: var(--accent);
}

.preview-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.preview-item {
  background: var(--surface2);
  padding: 12px;
  border-radius: 10px;
  text-align: center;
}

.preview-val {
  font-weight: 700;
  font-size: 1rem;
  color: var(--accent);
}

.preview-lbl {
  font-size: 0.72rem;
  color: var(--muted);
  margin-top: 4px;
}

.modal-foot {
  padding: 0 16px 16px;
}

.btn-add {
  margin-top: 0;
}

.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%) translateY(100px);
  padding: 12px 20px;
  background: var(--surface2);
  border: 1px solid var(--accent);
  border-radius: 999px;
  font-size: 0.9rem;
  z-index: 200;
  opacity: 0;
  pointer-events: none;
  transition: transform 0.25s, opacity 0.25s;
}

.toast.show {
  transform: translateX(-50%) translateY(0);
  opacity: 1;
}
