/* ---------- theme ---------- */
:root {
  --bg: #f5f5f7;
  --card: #ffffff;
  --card2: #f0f0f3;
  --text: #1c1c1e;
  --text2: #6e6e73;
  --line: rgba(0, 0, 0, 0.08);
  --green: #34c759;
  --blue: #0a84ff;
  --orange: #ff9f0a;
  --red: #ff453a;
  --purple: #bf5af2;
  --ring-track: #e4e4e9;
  --shadow: 0 4px 18px rgba(0, 0, 0, 0.08);
  --sheet-bg: #ffffff;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0d1117;
    --card: #1a2029;
    --card2: #232b36;
    --text: #f2f4f7;
    --text2: #98a1ae;
    --line: rgba(255, 255, 255, 0.09);
    --ring-track: #2b3441;
    --shadow: 0 4px 18px rgba(0, 0, 0, 0.4);
    --sheet-bg: #161c24;
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100%;
  overscroll-behavior-y: none;
}
button { font: inherit; color: inherit; background: none; border: none; cursor: pointer; }
input, textarea, select {
  font: inherit; color: var(--text); background: var(--card2);
  border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; width: 100%;
}
input:focus, textarea:focus, select:focus { outline: 2px solid var(--blue); outline-offset: -1px; }
h1 { font-size: 1.7rem; letter-spacing: -0.02em; }
h2 { font-size: 1.2rem; letter-spacing: -0.01em; }

#view {
  max-width: 560px; margin: 0 auto;
  padding: calc(env(safe-area-inset-top) + 16px) 16px calc(env(safe-area-inset-bottom) + 110px);
}

/* ---------- tab bar ---------- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 40;
  display: flex; align-items: center; justify-content: space-around;
  padding: 8px 8px calc(env(safe-area-inset-bottom) + 8px);
  background: color-mix(in srgb, var(--card) 85%, transparent);
  backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
  border-top: 1px solid var(--line);
}
.tab {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
  font-size: 0.68rem; color: var(--text2); min-width: 58px; padding: 4px 0;
}
.tab.active { color: var(--blue); }
.tab-icon { font-size: 1.35rem; }
.capture-fab {
  width: 62px; height: 62px; border-radius: 50%; font-size: 1.7rem;
  background: linear-gradient(135deg, var(--green), #28a745);
  box-shadow: 0 6px 16px rgba(52, 199, 89, 0.45);
  margin-top: -26px; color: #fff;
}
.capture-fab:active { transform: scale(0.94); }

/* ---------- cards & layout ---------- */
.card {
  background: var(--card); border-radius: 18px; padding: 16px;
  box-shadow: var(--shadow); margin-bottom: 14px;
}
.row { display: flex; align-items: center; gap: 10px; }
.spread { display: flex; align-items: center; justify-content: space-between; }
.muted { color: var(--text2); font-size: 0.85rem; }
.center { text-align: center; }
.hidden { display: none !important; }

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  border-radius: 12px; padding: 12px 18px; font-weight: 600; font-size: 0.95rem;
  background: var(--card2); border: 1px solid var(--line); width: 100%;
}
.btn.primary { background: var(--blue); color: #fff; border: none; }
.btn.green { background: var(--green); color: #fff; border: none; }
.btn.danger { background: transparent; color: var(--red); border: 1px solid var(--red); }
.btn.small { width: auto; padding: 7px 14px; font-size: 0.85rem; border-radius: 10px; }
.btn:disabled { opacity: 0.5; }

/* ---------- today: date nav + ring ---------- */
.date-nav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 14px; }
.date-nav button { font-size: 1.4rem; color: var(--blue); padding: 4px 14px; }
.date-nav .date-label { font-weight: 700; font-size: 1.1rem; }

.ring-wrap { display: flex; align-items: center; gap: 18px; }
.ring-box { position: relative; width: 150px; height: 150px; flex-shrink: 0; }
.ring-box svg { transform: rotate(-90deg); }
.ring-center {
  position: absolute; inset: 0; display: flex; flex-direction: column;
  align-items: center; justify-content: center;
}
.ring-center .big { font-size: 1.6rem; font-weight: 800; letter-spacing: -0.03em; }
.ring-center .sub { font-size: 0.7rem; color: var(--text2); }

.macros { flex: 1; display: flex; flex-direction: column; gap: 10px; }
.macro-label { display: flex; justify-content: space-between; font-size: 0.78rem; margin-bottom: 3px; }
.macro-bar { height: 7px; border-radius: 4px; background: var(--ring-track); overflow: hidden; }
.macro-fill { height: 100%; border-radius: 4px; transition: width 0.4s ease; }

/* ---------- meal cards ---------- */
.meal-group-title { font-size: 0.8rem; font-weight: 700; text-transform: uppercase;
  letter-spacing: 0.06em; color: var(--text2); margin: 16px 4px 8px; }
.meal-card {
  display: flex; align-items: center; gap: 12px; background: var(--card);
  border-radius: 16px; padding: 10px; box-shadow: var(--shadow); margin-bottom: 10px;
  width: 100%; text-align: left;
}
.meal-thumb {
  width: 56px; height: 56px; border-radius: 12px; object-fit: cover; flex-shrink: 0;
  background: var(--card2); display: flex; align-items: center; justify-content: center;
  font-size: 1.5rem;
}
.meal-info { flex: 1; min-width: 0; }
.meal-name { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.meal-macros { font-size: 0.75rem; color: var(--text2); margin-top: 2px; }
.meal-cals { font-weight: 700; font-size: 1rem; white-space: nowrap; }

.empty-state { text-align: center; padding: 40px 20px; color: var(--text2); }
.empty-state .emoji { font-size: 2.6rem; margin-bottom: 8px; }

/* ---------- sheets ---------- */
.sheet-backdrop {
  position: fixed; inset: 0; background: rgba(0, 0, 0, 0.45); z-index: 50;
  animation: fade 0.2s ease;
}
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 51;
  background: var(--sheet-bg); border-radius: 22px 22px 0 0;
  max-height: 92dvh; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: 10px 16px calc(env(safe-area-inset-bottom) + 20px);
  animation: slide-up 0.25s ease;
  max-width: 560px; margin: 0 auto;
}
.sheet-handle { width: 40px; height: 4px; border-radius: 2px; background: var(--line); margin: 6px auto 12px; }
.sheet h2 { margin-bottom: 12px; }
@keyframes slide-up { from { transform: translateY(60px); opacity: 0.4; } to { transform: none; opacity: 1; } }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }

.field { margin-bottom: 12px; }
.field label { display: block; font-size: 0.78rem; font-weight: 600; color: var(--text2); margin-bottom: 5px; }
.field-row { display: flex; gap: 10px; }
.field-row .field { flex: 1; }

.photo-preview { width: 100%; border-radius: 14px; max-height: 260px; object-fit: cover; margin-bottom: 12px; }
.capture-choices { display: flex; gap: 10px; margin-bottom: 12px; }

/* ---------- review items ---------- */
.item-row {
  background: var(--card2); border-radius: 14px; padding: 12px; margin-bottom: 10px;
  border: 1px solid var(--line);
}
.item-head { display: flex; gap: 8px; align-items: center; margin-bottom: 8px; }
.item-head input { flex: 1; background: transparent; border: none; font-weight: 600; padding: 2px 4px; }
.item-del { color: var(--red); font-size: 1.1rem; padding: 4px 8px; }
.item-portion input { background: transparent; border: none; font-size: 0.8rem; color: var(--text2); padding: 2px 4px; width: 100%; }
.item-macros { display: flex; align-items: center; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.macro-pill { font-size: 0.72rem; padding: 3px 8px; border-radius: 8px; background: var(--card); border: 1px solid var(--line); }
.qty-stepper { display: flex; align-items: center; gap: 2px; margin-left: auto; }
.qty-stepper button {
  width: 30px; height: 30px; border-radius: 8px; background: var(--card);
  border: 1px solid var(--line); font-size: 1.05rem; font-weight: 700;
}
.qty-stepper .qty { min-width: 40px; text-align: center; font-weight: 700; font-size: 0.9rem; }

.review-total { display: flex; justify-content: space-between; font-weight: 700; padding: 10px 4px; }

.spinner {
  width: 34px; height: 34px; border-radius: 50%; margin: 24px auto;
  border: 3px solid var(--ring-track); border-top-color: var(--blue);
  animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ---------- charts ---------- */
.chart-svg { width: 100%; height: auto; display: block; }
.legend { display: flex; gap: 14px; font-size: 0.72rem; color: var(--text2); margin-top: 6px; }
.legend .dot { width: 8px; height: 8px; border-radius: 50%; display: inline-block; margin-right: 4px; }

.stat-row { display: flex; gap: 10px; }
.stat-tile { flex: 1; background: var(--card); border-radius: 16px; padding: 14px; box-shadow: var(--shadow); text-align: center; }
.stat-tile .val { font-size: 1.35rem; font-weight: 800; letter-spacing: -0.02em; }
.stat-tile .lbl { font-size: 0.68rem; color: var(--text2); text-transform: uppercase; letter-spacing: 0.05em; margin-top: 2px; }

/* ---------- coach ---------- */
.chat-log { display: flex; flex-direction: column; gap: 10px; margin-bottom: 12px; }
.bubble { max-width: 85%; padding: 11px 14px; border-radius: 18px; font-size: 0.93rem; line-height: 1.45; white-space: pre-wrap; }
.bubble.user { align-self: flex-end; background: var(--blue); color: #fff; border-bottom-right-radius: 6px; }
.bubble.model { align-self: flex-start; background: var(--card); box-shadow: var(--shadow); border-bottom-left-radius: 6px; }
.targets-card { align-self: flex-start; background: var(--card); border: 1.5px solid var(--green);
  border-radius: 16px; padding: 12px 14px; max-width: 85%; }
.targets-card .tgt-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 4px 16px; font-size: 0.85rem; margin: 8px 0; }
.chat-input-row { display: flex; gap: 8px; position: sticky; bottom: calc(env(safe-area-inset-bottom) + 96px); }
.chat-input-row textarea { flex: 1; resize: none; max-height: 110px; border-radius: 16px; }
.chat-send { width: 44px; height: 44px; border-radius: 50%; background: var(--blue); color: #fff; font-size: 1.1rem; flex-shrink: 0; align-self: flex-end; }

/* ---------- pin gate ---------- */
.pin-gate {
  position: fixed; inset: 0; z-index: 100; background: var(--bg);
  display: flex; align-items: center; justify-content: center; padding: 24px;
}
.pin-card { width: 100%; max-width: 320px; text-align: center; display: flex; flex-direction: column; gap: 14px; }
.pin-emoji { font-size: 2.6rem; }
.pin-card input { text-align: center; font-size: 1.3rem; letter-spacing: 0.3em; }
.pin-error { color: var(--red); font-size: 0.85rem; min-height: 1.2em; }

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; transform: translateX(-50%);
  bottom: calc(env(safe-area-inset-bottom) + 110px); z-index: 90;
  background: var(--text); color: var(--bg); padding: 11px 18px; border-radius: 14px;
  font-size: 0.88rem; box-shadow: var(--shadow); max-width: 86vw; text-align: center;
}

/* ---------- misc ---------- */
.seg { display: flex; background: var(--card2); border-radius: 11px; padding: 3px; border: 1px solid var(--line); }
.seg button { flex: 1; padding: 8px 4px; border-radius: 8px; font-size: 0.82rem; font-weight: 600; color: var(--text2); }
.seg button.on { background: var(--card); color: var(--text); box-shadow: 0 1px 4px rgba(0,0,0,0.15); }
.streak-badge { display: inline-flex; align-items: center; gap: 5px; background: var(--card2);
  border-radius: 20px; padding: 5px 12px; font-size: 0.8rem; font-weight: 700; }
