:root {
  --bg: #14140F;
  --surface: #1F211A;
  --surface-raised: #262920;
  --mango: #FF8A3D;
  --leaf: #8BC34A;
  --text: #F5F3EC;
  --muted: #8A8C7F;
  --danger: #E05C4B;

  --font-display: "Space Grotesk", sans-serif;
  --font-body: "Inter", sans-serif;
  --font-mono: "JetBrains Mono", monospace;

  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
}

body {
  min-height: 100vh;
  min-height: 100dvh;
}

.app {
  max-width: 480px;
  margin: 0 auto;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  padding: env(safe-area-inset-top) 0 env(safe-area-inset-bottom);
}

/* ---------- Topbar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 18px 20px 8px;
}
.topbar h1 {
  font-family: var(--font-display);
  font-size: 17px;
  font-weight: 600;
  letter-spacing: 0.01em;
  margin: 0;
}
.logo-mark { display: flex; }

main {
  flex: 1;
  padding: 8px 20px 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* ---------- Stage switching ---------- */
.stage { display: none; }
.stage--active { display: flex; flex-direction: column; gap: 18px; }

/* ---------- Viewfinder ---------- */
.viewfinder {
  position: relative;
  aspect-ratio: 1 / 1;
  background: var(--surface);
  border-radius: 20px;
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
}
.viewfinder__img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}
.viewfinder__empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  color: var(--muted);
}
.viewfinder__empty p {
  margin: 0;
  font-size: 14px;
}
.viewfinder__corner {
  position: absolute;
  width: 22px;
  height: 22px;
  border: 2px solid var(--leaf);
  opacity: 0.85;
}
.viewfinder__corner.tl { top: 14px; left: 14px; border-right: none; border-bottom: none; border-radius: 6px 0 0 0; }
.viewfinder__corner.tr { top: 14px; right: 14px; border-left: none; border-bottom: none; border-radius: 0 6px 0 0; }
.viewfinder__corner.bl { bottom: 14px; left: 14px; border-right: none; border-top: none; border-radius: 0 0 0 6px; }
.viewfinder__corner.br { bottom: 14px; right: 14px; border-left: none; border-top: none; border-radius: 0 0 6px 0; }

/* ---------- Buttons ---------- */
.btn {
  display: flex;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 15px 20px;
  border-radius: 14px;
  font-family: var(--font-body);
  font-weight: 600;
  font-size: 15px;
  border: none;
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.15s ease;
}
.btn:active { transform: scale(0.97); }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }

.btn--primary {
  background: var(--text);
  color: var(--bg);
}
.btn--ghost {
  background: var(--surface);
  color: var(--text);
  border: 1px solid #33362B;
}
.btn--accent {
  background: var(--mango);
  color: #1A0F06;
}

.capture-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

/* ---------- Loading ---------- */
#stage-loading {
  align-items: center;
  justify-content: center;
  min-height: 50vh;
  gap: 24px;
}
.scan-loader {
  width: 220px;
  height: 220px;
  border-radius: 20px;
  background: var(--surface);
  position: relative;
  overflow: hidden;
}
.scan-loader__line {
  position: absolute;
  left: 0; right: 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--leaf), transparent);
  box-shadow: 0 0 12px 2px var(--leaf);
  animation: scan 1.6s ease-in-out infinite;
}
@keyframes scan {
  0% { top: 6%; }
  50% { top: 92%; }
  100% { top: 6%; }
}
.loading-text {
  color: var(--muted);
  font-size: 14px;
}

/* ---------- Ticket (receipt) ---------- */
.ticket {
  background: var(--surface);
  border-radius: 20px;
  padding: 26px 22px;
  position: relative;
}
.ticket::before, .ticket::after {
  content: "";
  position: absolute;
  left: 0; right: 0;
  height: 10px;
  background:
    radial-gradient(circle at 10px 5px, transparent 6px, var(--bg) 6.5px) repeat-x;
  background-size: 20px 10px;
}
.ticket::before { top: -1px; }
.ticket::after { bottom: -1px; transform: rotate(180deg); }

.ticket__header { margin-bottom: 18px; }
.ticket__eyebrow {
  font-family: var(--font-mono);
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}
.ticket__header h2 {
  font-family: var(--font-display);
  font-size: 22px;
  font-weight: 600;
  margin: 4px 0 0;
}

.ticket__kcal {
  display: flex;
  align-items: baseline;
  gap: 8px;
  margin-bottom: 20px;
}
.ticket__kcal span:first-child {
  font-family: var(--font-mono);
  font-size: 44px;
  font-weight: 700;
  color: var(--mango);
  line-height: 1;
}
.ticket__kcal-unit {
  font-size: 14px;
  color: var(--muted);
}

.ticket__macros {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
  margin-bottom: 18px;
}
.macro {
  background: var(--surface-raised);
  border-radius: 12px;
  padding: 12px 10px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}
.macro__label {
  font-size: 11px;
  color: var(--muted);
}
.macro__value {
  font-family: var(--font-mono);
  font-size: 16px;
  font-weight: 700;
  color: var(--leaf);
}

.ticket__divider {
  border-top: 1px dashed #3A3D30;
  margin: 14px 0;
}
.ticket__note {
  font-size: 13.5px;
  color: var(--muted);
  line-height: 1.5;
  margin: 0;
}
.ticket__footer {
  display: flex;
  justify-content: space-between;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--muted);
  margin-top: 4px;
}

.result-actions {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* ---------- Error ---------- */
#stage-error {
  align-items: center;
  justify-content: center;
  min-height: 40vh;
  text-align: center;
  gap: 18px;
}
.error-text { color: var(--danger); font-size: 14px; max-width: 280px; }

/* ---------- Diary ---------- */
.diary {
  margin-top: 4px;
  padding-top: 16px;
  border-top: 1px solid #262920;
}
.diary__header {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  margin-bottom: 12px;
}
.diary__header h2 {
  font-family: var(--font-display);
  font-size: 15px;
  font-weight: 600;
  margin: 0;
  color: var(--muted);
}
.diary__total {
  font-family: var(--font-mono);
  font-size: 14px;
  font-weight: 700;
  color: var(--mango);
}
.diary__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
}
.diary__empty {
  color: var(--muted);
  font-size: 13px;
  padding: 14px 0;
}
.diary__item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: var(--surface);
  border-radius: 12px;
  padding: 12px 14px;
  font-size: 13.5px;
}
.diary__item-name { font-weight: 500; }
.diary__item-time { color: var(--muted); font-size: 11.5px; font-family: var(--font-mono); }
.diary__item-kcal { font-family: var(--font-mono); font-weight: 700; color: var(--leaf); }
.diary__item-remove {
  background: none;
  border: none;
  color: var(--muted);
  font-size: 16px;
  cursor: pointer;
  padding: 0 0 0 10px;
  line-height: 1;
}

@media (prefers-reduced-motion: reduce) {
  .scan-loader__line { animation: none; top: 50%; }
}
