:root {
  --bg: #0f1115;
  --card: #171a21;
  --card2: #1f232c;
  --line: #2a2f3a;
  --text: #e8eaf0;
  --muted: #8b93a7;
  --accent: #ff7a45;
  --green: #35c48b;
  --blue: #4a9dff;
  --danger: #ff5a5a;
  --radius: 16px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}

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

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", "Noto Sans Thai", "Sarabun", system-ui, sans-serif;
  padding-bottom: calc(76px + var(--safe-b));
  overscroll-behavior-y: contain;
}

h2 { font-size: 15px; margin: 0; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  display: flex; align-items: center; gap: 8px;
  padding: calc(env(safe-area-inset-top, 0px) + 10px) 12px 10px;
  background: rgba(15, 17, 21, .92);
  backdrop-filter: blur(12px);
  border-bottom: 1px solid var(--line);
}
.dayhead { flex: 1; text-align: center; }
.daylabel { font-weight: 700; font-size: 16px; }
.topbar input[type=date] {
  background: none; border: 0; color: var(--muted);
  font: inherit; font-size: 12px; text-align: center; padding: 0;
}
.daynav {
  width: 38px; height: 38px; flex: none;
  border: 1px solid var(--line); border-radius: 12px;
  background: var(--card); color: var(--text); font-size: 20px; line-height: 1;
}

main { padding: 14px 12px 20px; max-width: 560px; margin: 0 auto; }

.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  margin-bottom: 14px;
}

.sectiontitle {
  display: flex; align-items: center; justify-content: space-between;
  margin: 18px 4px 10px; color: var(--muted); font-weight: 600;
}
.card .sectiontitle { margin: 0 0 12px; }

/* ---------- ring ---------- */
.ringcard { display: grid; place-items: center; position: relative; padding-top: 22px; }
.ring { width: 190px; height: 190px; transform: rotate(-90deg); }
.ring circle { fill: none; stroke-width: 12; stroke-linecap: round; }
.ring-bg { stroke: var(--card2); }
.ring-fg {
  stroke: var(--accent);
  stroke-dasharray: 326.7;
  stroke-dashoffset: 326.7;
  transition: stroke-dashoffset .6s cubic-bezier(.4, 0, .2, 1), stroke .3s;
}
.ringtext { position: absolute; top: 96px; text-align: center; pointer-events: none; }
.bignum { font-size: 40px; font-weight: 800; letter-spacing: -1px; }
.subnum { color: var(--muted); font-size: 12px; }
.ringlegend {
  display: flex; gap: 14px; flex-wrap: wrap; justify-content: center;
  margin-top: 14px; font-size: 12px; color: var(--muted);
}
.ringlegend b { color: var(--text); }
.dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 5px; }
.dot-target { background: var(--muted); }
.dot-eaten { background: var(--accent); }
.dot-burn { background: var(--green); }

/* ---------- macros ---------- */
.macros { display: grid; gap: 12px; }
.macro { display: grid; grid-template-columns: 52px 1fr 68px; align-items: center; gap: 10px; }
.mlabel { font-size: 13px; color: var(--muted); }
.mbar { height: 8px; background: var(--card2); border-radius: 99px; overflow: hidden; }
.mbar i { display: block; height: 100%; width: 0; border-radius: 99px; background: var(--blue); transition: width .5s; }
.macro:nth-child(2) .mbar i { background: var(--accent); }
.macro:nth-child(3) .mbar i { background: #f0c05a; }
.mval { font-size: 12px; color: var(--muted); text-align: right; }

/* ---------- buttons ---------- */
.actions { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.btn {
  display: flex; align-items: center; justify-content: center; gap: 7px;
  width: 100%; padding: 14px 12px;
  border: 1px solid var(--line); border-radius: 14px;
  background: var(--card); color: var(--text);
  font: inherit; font-weight: 600; cursor: pointer;
}
.btn:active { transform: scale(.98); }
.btn-primary { background: var(--accent); border-color: var(--accent); color: #1a0d06; }
.btn { text-decoration: none; }
.btn-ghost { background: none; color: var(--muted); font-weight: 500; }
.btn:disabled { opacity: .5; }
.ico { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 1.8; stroke-linejoin: round; }
.linkbtn { background: none; border: 0; color: var(--blue); font: inherit; font-size: 13px; padding: 0; cursor: pointer; }

/* ---------- lists ---------- */
.list { display: grid; gap: 10px; }
.row {
  display: grid; grid-template-columns: auto 1fr auto; gap: 12px; align-items: center;
  background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px;
}
.row img { width: 46px; height: 46px; border-radius: 10px; object-fit: cover; }
.row .emoji {
  width: 46px; height: 46px; border-radius: 10px; background: var(--card2);
  display: grid; place-items: center; font-size: 20px;
}
.row .title { font-weight: 600; font-size: 14px; }
.row .sub { color: var(--muted); font-size: 12px; margin-top: 2px; }
.row .kcal { font-weight: 700; white-space: nowrap; }
.row .del { background: none; border: 0; color: var(--muted); font-size: 18px; padding: 4px 2px; cursor: pointer; }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 18px; border: 1px dashed var(--line); border-radius: 14px; }

/* ---------- forms ---------- */
.formgrid { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; margin-bottom: 12px; }
label { display: grid; gap: 6px; font-size: 12px; color: var(--muted); }
input, select, textarea {
  background: var(--card2); border: 1px solid var(--line); border-radius: 10px;
  padding: 11px 12px; color: var(--text); font: inherit; font-size: 15px; width: 100%;
}
textarea { resize: vertical; min-height: 64px; }
.switch { display: flex; flex-direction: row; align-items: center; gap: 10px; margin: 6px 0 12px; color: var(--text); font-size: 14px; }
.switch input { width: 20px; height: 20px; accent-color: var(--accent); }
.hint { color: var(--muted); font-size: 12.5px; margin: 8px 0; }
.hint b { color: var(--text); }

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 30;
  display: grid; grid-template-columns: repeat(3, 1fr);
  background: rgba(23, 26, 33, .96); backdrop-filter: blur(12px);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-b);
}
.tab { background: none; border: 0; color: var(--muted); font: inherit; font-size: 13px; padding: 16px 0; cursor: pointer; }
.tab.active { color: var(--accent); font-weight: 700; }

/* ---------- sheet ---------- */
.sheet { position: fixed; inset: 0; z-index: 50; background: rgba(0, 0, 0, .6); display: flex; align-items: flex-end; }
.sheet-inner {
  background: var(--bg); width: 100%; max-height: 92vh; overflow-y: auto;
  border-radius: 20px 20px 0 0; padding: 10px 16px calc(24px + var(--safe-b));
  animation: up .25s ease; max-width: 560px; margin: 0 auto;
}
@keyframes up { from { transform: translateY(40px); opacity: .5 } }
.sheet-handle { width: 40px; height: 4px; background: var(--line); border-radius: 9px; margin: 4px auto 14px; }

.preview { width: 100%; max-height: 210px; object-fit: cover; border-radius: 14px; margin-bottom: 14px; }
.dishname { font-size: 20px; font-weight: 800; }
.kcalbig { font-size: 34px; font-weight: 800; color: var(--accent); }
.kcalrange { color: var(--muted); font-size: 12px; }

.items { display: grid; gap: 8px; margin: 14px 0; }
.item { display: grid; grid-template-columns: 1fr auto; gap: 8px; background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.item .n { font-size: 14px; font-weight: 600; }
.item .p { color: var(--muted); font-size: 12px; }
.item .k { font-weight: 700; align-self: center; }
.conf { font-size: 10px; padding: 1px 6px; border-radius: 99px; margin-left: 6px; vertical-align: middle; }
.conf.high { background: rgba(53, 196, 139, .18); color: var(--green); }
.conf.medium { background: rgba(240, 192, 90, .18); color: #f0c05a; }
.conf.low { background: rgba(255, 90, 90, .18); color: var(--danger); }

.assume { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 12px; font-size: 12.5px; color: var(--muted); }
.assume li { margin: 3px 0; }
.assume ul { margin: 6px 0 0; padding-left: 18px; }

.portion { display: flex; align-items: center; gap: 10px; margin: 14px 0; }
.portion input[type=range] { flex: 1; accent-color: var(--accent); padding: 0; background: none; border: 0; }
.portion .pv { width: 52px; text-align: right; font-weight: 700; }

.sheetactions { display: grid; grid-template-columns: 1fr 2fr; gap: 10px; margin-top: 16px; }

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

/* ---------- chart ---------- */
.chart { display: flex; align-items: flex-end; gap: 4px; height: 160px; padding-top: 8px; }
.chart .col { flex: 1; display: flex; flex-direction: column; justify-content: flex-end; gap: 2px; height: 100%; position: relative; }
.chart .b { border-radius: 3px 3px 0 0; min-height: 2px; }
.chart .b-eaten { background: var(--accent); }
.chart .b-burn { background: var(--green); opacity: .8; }
.chart .lbl { font-size: 9px; color: var(--muted); text-align: center; margin-top: 4px; }
.chartlegend { display: flex; gap: 14px; margin-top: 12px; font-size: 12px; color: var(--muted); }
.sw { display: inline-block; width: 10px; height: 10px; border-radius: 3px; margin-right: 5px; }
.sw-eaten { background: var(--accent); }
.sw-burn { background: var(--green); }
.sw-line { background: var(--muted); height: 2px; border-radius: 0; }
.statrow { display: flex; justify-content: space-between; padding: 9px 0; border-bottom: 1px solid var(--line); font-size: 14px; }
.statrow:last-child { border: 0; }
.statrow b { font-weight: 700; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(96px + var(--safe-b)); transform: translateX(-50%);
  background: var(--card2); border: 1px solid var(--line); color: var(--text);
  padding: 11px 18px; border-radius: 99px; font-size: 13.5px; z-index: 60;
  max-width: 88vw; text-align: center; animation: up .2s ease;
}
.toast.err { border-color: var(--danger); color: #ffc4c4; }

.badge { display: inline-block; font-size: 11px; padding: 2px 8px; border-radius: 99px; background: var(--card2); color: var(--muted); }
.badge.ok { background: rgba(53, 196, 139, .16); color: var(--green); }

/* ให้ attribute hidden ชนะ display ที่ตั้งไว้ในคลาส */
[hidden] { display: none !important; }

/* ---------- สมดุลพลังงาน ---------- */
.ioRow { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; text-align: center; }
.iolabel { font-size: 11px; color: var(--muted); }
.ioval { font-size: 21px; font-weight: 800; letter-spacing: -.4px; }
.ioval.green { color: var(--green); }
.ioval.neg { color: var(--green); }
.ioval.pos { color: var(--danger); }

.iobars { display: grid; gap: 5px; margin: 14px 0 4px; }
.iobar { height: 9px; background: var(--card2); border-radius: 99px; overflow: hidden; }
.iobar i { display: block; height: 100%; width: 0; border-radius: 99px; transition: width .6s cubic-bezier(.4,0,.2,1); }
.b-in { background: var(--accent); }
.b-out { background: var(--green); }

.breakdown { margin-top: 12px; border-top: 1px solid var(--line); padding-top: 4px; }
.brow { display: flex; justify-content: space-between; align-items: baseline; padding: 7px 0; font-size: 13px; }
.brow .bl { color: var(--muted); }
.brow .bl b { color: var(--text); font-weight: 600; }
.brow .bv { font-weight: 700; white-space: nowrap; }
.brow.total { border-top: 1px dashed var(--line); margin-top: 4px; padding-top: 10px; }

.live { display: inline-block; width: 7px; height: 7px; border-radius: 50%; background: var(--green); margin-right: 6px; animation: pulse 2s ease-in-out infinite; }
@keyframes pulse { 0%,100% { opacity: 1 } 50% { opacity: .25 } }

code { background: var(--card2); padding: 1px 5px; border-radius: 5px; font-size: 12px; }

/* ---------- หน้าล็อกอิน ---------- */
.lockscreen {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: grid; place-items: center; padding: 24px;
}
.lockbox { display: grid; gap: 12px; justify-items: center; text-align: center; width: 100%; max-width: 320px; }
.lockbox input { text-align: center; font-size: 17px; letter-spacing: 2px; }
.lockbox .btn { margin-top: 4px; }
#lockErr { color: var(--danger); min-height: 18px; }
.btn-google { background: #fff; border-color: #fff; color: #1f1f1f; text-decoration: none; }

/* ---------- ร่างกายวันนี้ (หัวใจ / การนอน) ---------- */
.vgrid { display: grid; grid-template-columns: repeat(auto-fit, minmax(130px, 1fr)); gap: 10px; }
.vtile {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 14px 12px; text-align: center;
}
.vicon { font-size: 18px; margin-bottom: 4px; }
.vval { font-size: 21px; font-weight: 800; letter-spacing: -.4px; }
.vunit { font-size: 12px; font-weight: 600; color: var(--muted); }
.vlabel { font-size: 12px; color: var(--muted); margin-top: 2px; }
.vsub { font-size: 11px; color: var(--muted); opacity: .75; margin-top: 3px; }

/* ---------- พิมพ์ชื่อเมนู ---------- */
.btn-type { border-style: dashed; }
.typedmenu {
  background: var(--card); border: 1px solid var(--line); border-radius: 14px;
  padding: 16px; margin-bottom: 14px; font-size: 15px; line-height: 1.5;
}
.quickmenu { display: flex; flex-wrap: wrap; gap: 7px; margin-top: 10px; }
.chip {
  background: var(--card2); border: 1px solid var(--line); border-radius: 99px;
  padding: 7px 12px; color: var(--muted); font: inherit; font-size: 12.5px; cursor: pointer;
}
.chip:active { background: var(--accent); color: #1a0d06; border-color: var(--accent); }
.row .src { font-size: 11px; opacity: .6; margin-left: 4px; }
