:root {
  --bg: #0b1120;
  --surface: #151d31;
  --surface2: #1d2740;
  --line: #2a3652;
  --text: #e8edf7;
  --muted: #93a1bd;
  --brand: #34d399;
  --brand-dark: #0f766e;
  --warn: #fbbf24;
  --danger: #f87171;
  --carb: #60a5fa;
  --protein: #34d399;
  --fat: #fbbf24;
  --radius: 16px;
  color-scheme: dark;
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "IBM Plex Sans Thai", "Noto Sans Thai", -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  overscroll-behavior-y: none;
}

#app { max-width: 620px; margin: 0 auto; padding-bottom: calc(78px + env(safe-area-inset-bottom)); }
.hidden { display: none !important; }

.page { padding: 0 16px 24px; animation: fade .18s ease; }
@keyframes fade { from { opacity: 0; transform: translateY(4px); } }

.topbar {
  display: flex; align-items: center; gap: 10px;
  padding: calc(14px + env(safe-area-inset-top)) 0 12px;
  position: sticky; top: 0; background: linear-gradient(var(--bg) 72%, transparent); z-index: 5;
}
.topbar h1 { font-size: 19px; margin: 0; flex: 1; }
.pill {
  font-size: 11px; padding: 4px 10px; border-radius: 999px;
  background: var(--surface2); color: var(--muted); border: 1px solid var(--line); white-space: nowrap;
}
.pill.ok { color: var(--brand); border-color: var(--brand-dark); }
.pill.bad { color: var(--danger); border-color: #7f1d1d; }
.icon-btn {
  width: 36px; height: 36px; border-radius: 50%; border: 1px solid var(--line);
  background: var(--surface); color: var(--text); font-size: 20px; line-height: 1; cursor: pointer;
}

/* ---------- ถ่ายรูป ---------- */
/* ซ่อนช่องเลือกไฟล์แบบยังถูกวาดอยู่จริง — ถ้าใช้ display:none หรือ hidden
   Safari บน iPhone บางรุ่นจะไม่เปิดหน้าต่างเลือกรูปให้ */
.file-input {
  position: absolute;
  width: 1px;
  height: 1px;
  opacity: 0;
  pointer-events: none;
}

.dropzone {
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 4px;
  min-height: 220px; border: 2px dashed var(--line); border-radius: var(--radius);
  background: var(--surface); cursor: pointer; transition: border-color .15s, background .15s;
}
.dropzone:active { border-color: var(--brand); background: var(--surface2); }
.dz-icon { font-size: 46px; }
.dz-title { font-weight: 600; font-size: 17px; }
.dz-sub { color: var(--muted); font-size: 13px; }

.tips { margin-top: 16px; padding: 14px 16px; background: var(--surface); border-radius: var(--radius); font-size: 13px; color: var(--muted); }
.tips strong { color: var(--text); display: block; margin-bottom: 6px; }
.tips ul { margin: 0; padding-left: 18px; }
.tips li { margin: 3px 0; }

.preview img { width: 100%; border-radius: var(--radius); display: block; max-height: 46vh; object-fit: cover; }
.row { display: flex; gap: 10px; margin-top: 12px; }
.row > * { flex: 1; }

input[type="text"], input[type="number"], input[type="date"], select, textarea {
  width: 100%; padding: 11px 13px; margin-top: 10px;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  color: var(--text); font: inherit;
}
input:focus, select:focus, textarea:focus { outline: 2px solid var(--brand-dark); border-color: var(--brand); }

.btn {
  padding: 13px 16px; border-radius: 12px; border: 1px solid var(--line);
  background: var(--surface2); color: var(--text); font: inherit; font-weight: 600; cursor: pointer;
}
.btn.primary { background: var(--brand); color: #05261c; border-color: var(--brand); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; }
.btn.small { padding: 7px 12px; font-size: 13px; font-weight: 500; }

.loading { text-align: center; padding: 40px 0; color: var(--muted); }
.spinner {
  width: 38px; height: 38px; margin: 0 auto 14px; border-radius: 50%;
  border: 3px solid var(--line); border-top-color: var(--brand); animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- การ์ด / ผลลัพธ์ ---------- */
.card { background: var(--surface); border-radius: var(--radius); padding: 16px; margin-top: 14px; border: 1px solid var(--line); }
.card h2 { margin: 0 0 4px; font-size: 16px; }
.card h3 { margin: 0 0 10px; font-size: 14px; color: var(--muted); font-weight: 600; }

.kcal-big { font-size: 40px; font-weight: 700; line-height: 1.1; letter-spacing: -1px; }
.kcal-big small { font-size: 15px; color: var(--muted); font-weight: 500; margin-left: 4px; letter-spacing: 0; }

.macro-row { display: flex; gap: 8px; margin-top: 12px; }
.macro {
  flex: 1; background: var(--surface2); border-radius: 12px; padding: 9px 10px; text-align: center;
}
.macro b { display: block; font-size: 16px; }
.macro span { font-size: 11px; color: var(--muted); }
.macro.p b { color: var(--protein); }
.macro.c b { color: var(--carb); }
.macro.f b { color: var(--fat); }

.item {
  display: flex; align-items: center; gap: 10px; padding: 11px 0; border-bottom: 1px solid var(--line);
}
.item:last-child { border-bottom: none; }
.item-main { flex: 1; min-width: 0; }
.item-name { font-weight: 600; }
.item-sub { font-size: 12px; color: var(--muted); }
.item-kcal { font-weight: 700; white-space: nowrap; }
/* ต้องเจาะจงกว่ากฎ input[type="number"] ด้านบน ไม่งั้นช่องนี้จะกว้างเต็มแถว */
.item input.gram-input {
  flex: 0 0 74px; width: 74px; margin: 0; padding: 7px 8px; text-align: right; font-size: 13px;
}
.item .btn.small { flex: 0 0 auto; }

.conf { display: inline-block; font-size: 10px; padding: 1px 7px; border-radius: 999px; margin-left: 6px; vertical-align: middle; }
.conf.high { background: #064e3b; color: #6ee7b7; }
.conf.mid { background: #4a3a08; color: #fcd34d; }
.conf.low { background: #4c1d1d; color: #fca5a5; }

.notes { font-size: 13px; color: var(--muted); margin: 0; padding-left: 18px; }
.notes li { margin: 4px 0; }

/* ---------- แถบความคืบหน้า ---------- */
.bar { height: 9px; border-radius: 999px; background: var(--surface2); overflow: hidden; margin-top: 8px; }
.bar > i { display: block; height: 100%; background: var(--brand); border-radius: 999px; transition: width .35s ease; }
.bar > i.over { background: var(--danger); }
.bar-label { display: flex; justify-content: space-between; font-size: 12px; color: var(--muted); margin-top: 7px; }

.mini-bars { display: grid; gap: 10px; margin-top: 14px; }
.mini-bar-title { display: flex; justify-content: space-between; font-size: 12px; }

/* ---------- กราฟ ---------- */
.chart { display: flex; align-items: flex-end; gap: 4px; height: 150px; margin-top: 12px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; align-items: center; gap: 4px; height: 100%; }
.chart .col > i { width: 100%; background: var(--brand-dark); border-radius: 4px 4px 0 0; min-height: 2px; transition: height .3s; }
.chart .col > i.over { background: var(--danger); }
.chart .col > i.today { background: var(--brand); }
.chart .col > span { font-size: 9px; color: var(--muted); }
.goal-line { position: relative; border-top: 1px dashed var(--warn); font-size: 10px; color: var(--warn); margin-top: -1px; }

/* ---------- มื้ออาหาร ---------- */
.meal { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-bottom: 1px solid var(--line); }
.meal:last-child { border-bottom: none; }
.meal img { width: 56px; height: 56px; border-radius: 12px; object-fit: cover; background: var(--surface2); flex-shrink: 0; }
.meal-noimg { width: 56px; height: 56px; border-radius: 12px; background: var(--surface2); display: grid; place-items: center; font-size: 22px; flex-shrink: 0; }

.empty { text-align: center; color: var(--muted); padding: 36px 12px; font-size: 14px; }

.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 20;
  display: flex; background: rgba(15,23,42,.94); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line); padding-bottom: env(safe-area-inset-bottom);
}
.tabbar button {
  flex: 1; background: none; border: none; color: var(--muted); font: inherit; font-size: 11px;
  padding: 9px 0 11px; display: flex; flex-direction: column; align-items: center; gap: 3px; cursor: pointer;
}
.tabbar button span { font-size: 20px; line-height: 1; }
.tabbar button.active { color: var(--brand); }

.toast {
  position: fixed; left: 50%; bottom: calc(88px + env(safe-area-inset-bottom)); transform: translateX(-50%);
  background: var(--surface2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 18px; border-radius: 999px; font-size: 14px; z-index: 50; max-width: 90vw; text-align: center;
  box-shadow: 0 8px 30px rgba(0,0,0,.5);
}
.toast.bad { border-color: var(--danger); color: #fecaca; }

.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
label.field { display: block; font-size: 12px; color: var(--muted); margin-top: 12px; }
label.field:first-child { margin-top: 0; }
.help { font-size: 12px; color: var(--muted); margin-top: 8px; }
.stat-line { display: flex; justify-content: space-between; padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.stat-line:last-child { border: none; }
.stat-line b { font-weight: 600; }
.seg { display: flex; gap: 6px; margin-top: 10px; flex-wrap: wrap; }
.seg button {
  padding: 7px 13px; border-radius: 999px; border: 1px solid var(--line);
  background: var(--surface); color: var(--muted); font: inherit; font-size: 13px; cursor: pointer;
}
.seg button.active { background: var(--brand); color: #05261c; border-color: var(--brand); font-weight: 600; }
