:root {
  --bg: #f6f7f5;
  --card: #ffffff;
  --ink: #1d2421;
  --muted: #6b7770;
  --line: #e4e8e4;
  --accent: #0e7a65;
  --accent-soft: #e1f0eb;
  --danger: #c0392b;
  --warn: #b9770e;
  --radius: 16px;
  --shadow: 0 1px 3px rgba(20, 40, 30, 0.06), 0 8px 24px rgba(20, 40, 30, 0.05);
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  font-size: 16px;
  line-height: 1.45;
}

.hidden { display: none !important; }
.muted { color: var(--muted); }
.error { color: var(--danger); font-size: 0.9rem; }

/* --- Brand / layout --- */
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 1.05rem; }
.brand-dot {
  width: 14px; height: 14px; border-radius: 50%;
  background: var(--accent); box-shadow: inset 0 0 0 3px #fff, 0 0 0 2px var(--accent);
}

.app { min-height: 100vh; display: flex; flex-direction: column; }

.topbar {
  position: sticky; top: 0; z-index: 5;
  padding: calc(12px + var(--safe-top)) 18px 12px;
  background: rgba(246, 247, 245, 0.9);
  backdrop-filter: saturate(140%) blur(8px);
  border-bottom: 1px solid var(--line);
}

.view {
  flex: 1;
  padding: 16px 16px 96px;
  max-width: 640px;
  width: 100%;
  margin: 0 auto;
}

/* --- Tab bar --- */
.tabbar {
  position: fixed; bottom: 0; left: 0; right: 0; z-index: 10;
  display: grid; grid-template-columns: repeat(4, 1fr);
  background: var(--card);
  border-top: 1px solid var(--line);
  padding-bottom: var(--safe-bottom);
}
.tab {
  appearance: none; border: 0; background: none;
  padding: 12px 4px; font-size: 0.82rem; font-weight: 600;
  color: var(--muted); cursor: pointer;
}
.tab.active { color: var(--accent); }

/* --- Cards & generic --- */
.card {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 16px;
  margin-bottom: 14px;
}
.card h2 { margin: 0 0 12px; font-size: 1.05rem; }
.section-title { font-size: 0.8rem; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); margin: 4px 2px 8px; }

.btn {
  appearance: none; cursor: pointer;
  border: 1px solid var(--line); background: var(--card); color: var(--ink);
  border-radius: 12px; padding: 12px 16px; font-size: 1rem; font-weight: 600;
  width: 100%;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--danger); border-color: transparent; background: transparent; width: auto; padding: 6px 8px; }
.btn:disabled { opacity: 0.55; cursor: default; }
.btn-row { display: flex; gap: 10px; }

input, select, textarea {
  width: 100%; font-size: 1rem; font-family: inherit;
  padding: 12px 12px; border: 1px solid var(--line); border-radius: 12px;
  background: #fff; color: var(--ink);
}
textarea { resize: vertical; min-height: 64px; }
label { display: block; font-size: 0.85rem; color: var(--muted); margin: 10px 2px 4px; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }

/* --- Login --- */
.login {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center; padding: 20px;
  background: var(--bg);
}
.login-card {
  width: 100%; max-width: 360px;
  background: var(--card); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow); padding: 22px;
}
.login-card .brand { margin-bottom: 8px; }
.login-card form { display: flex; flex-direction: column; gap: 12px; margin-top: 14px; }

/* --- Today: progress ring --- */
.ring-wrap { display: flex; align-items: center; gap: 18px; }
.ring { width: 132px; height: 132px; flex: 0 0 auto; }
.ring-track { fill: none; stroke: var(--line); stroke-width: 12; }
.ring-fill { fill: none; stroke: var(--accent); stroke-width: 12; stroke-linecap: round;
  transform: rotate(-90deg); transform-origin: 50% 50%; transition: stroke-dashoffset 0.5s ease; }
.ring-fill.over { stroke: var(--warn); }
.ring-center { text-align: left; }
.ring-big { font-size: 2rem; font-weight: 750; line-height: 1; }
.ring-sub { color: var(--muted); font-size: 0.9rem; margin-top: 4px; }

.macros { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin-top: 16px; }
.macro { text-align: center; background: var(--accent-soft); border-radius: 12px; padding: 10px 6px; }
.macro .v { font-weight: 700; }
.macro .k { font-size: 0.75rem; color: var(--muted); }

/* --- Date nav --- */
.datenav { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.datenav .d { font-weight: 700; }
.iconbtn { appearance: none; border: 1px solid var(--line); background: var(--card); border-radius: 10px;
  width: 40px; height: 40px; font-size: 1.1rem; cursor: pointer; }

/* --- Entry list --- */
.entry { display: flex; gap: 12px; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.entry:first-child { border-top: 0; }
.entry .thumb { width: 52px; height: 52px; border-radius: 10px; object-fit: cover; background: var(--accent-soft); flex: 0 0 auto; }
.entry .body { flex: 1; min-width: 0; }
.entry .body .t { font-weight: 600; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.entry .body .s { font-size: 0.85rem; color: var(--muted); }
.entry .kcal { font-weight: 700; white-space: nowrap; }

/* --- Add / analysis result --- */
.photo-pick {
  display: grid; place-items: center; gap: 8px;
  border: 2px dashed var(--line); border-radius: var(--radius);
  padding: 24px; text-align: center; color: var(--muted); cursor: pointer; background: var(--card);
}
.photo-pick.has-image { padding: 8px; border-style: solid; }
.photo-pick img { max-width: 100%; max-height: 280px; border-radius: 10px; }
.item-row { display: grid; grid-template-columns: 1fr 84px 34px; gap: 8px; align-items: center; margin-bottom: 8px; }
.item-row .nm { font-size: 0.95rem; }
.item-row .q { font-size: 0.78rem; color: var(--muted); }
.item-row input.kcal-in { text-align: right; padding: 8px; }
.chip { display: inline-block; font-size: 0.75rem; padding: 2px 9px; border-radius: 999px; font-weight: 600; }
.chip.high { background: #e1f0eb; color: #0e7a65; }
.chip.medium { background: #fdf0d8; color: #b9770e; }
.chip.low { background: #fbe2de; color: #c0392b; }

.spinner {
  width: 22px; height: 22px; border-radius: 50%;
  border: 3px solid rgba(255,255,255,0.4); border-top-color: #fff;
  animation: spin 0.8s linear infinite; display: inline-block; vertical-align: -5px; margin-right: 8px;
}
@keyframes spin { to { transform: rotate(360deg); } }

.empty { text-align: center; color: var(--muted); padding: 28px 10px; }

/* --- History --- */
.dayrow { display: flex; justify-content: space-between; align-items: center; padding: 12px 0; border-top: 1px solid var(--line); }
.dayrow:first-child { border-top: 0; }
.bar { height: 6px; border-radius: 999px; background: var(--line); overflow: hidden; margin-top: 6px; }
.bar > i { display: block; height: 100%; background: var(--accent); }
.bar > i.over { background: var(--warn); }

/* --- Toast --- */
.toast {
  position: fixed; left: 50%; bottom: calc(80px + var(--safe-bottom)); transform: translateX(-50%);
  background: #1d2421; color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 0.9rem;
  z-index: 60; box-shadow: var(--shadow); max-width: 90%;
}
