:root {
  --green-deep: #17563f;
  --green-dark: #0f3d2c;
  --lime: #98c161;
  --lime-splash: #87b157;
  --lime-light: #b0d97e;
  --lime-wash: #eef6e6;
  --ground: #bdd5bf;
  --card: #f6f8f3;
  --text: #1c2a23;
  --text-muted: #5b6b62;
  --line: #dfe7da;
  --send-off: #cdd5c6;
  --band-height: min(61.4vw, 300px);
  /* How far the card may dip into the band's faded top edge. The band image
     is always drawn 61.4vw tall (cropped from the top on wide screens), and
     its first vegetables start 44.4vw above the band's bottom, so the dip
     stops short of them. */
  --band-overlap: min(calc(var(--band-height) * 0.18), max(0px, calc(var(--band-height) - 44.4vw - 6px)));
}

* {
  box-sizing: border-box;
}

/* Anything the page hides stays hidden, whatever its own display. */

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--ground);
  color: var(--text);
  font-family: 'Arimo', Arial, 'Helvetica Neue', Helvetica, sans-serif;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

body {
  overflow: hidden;
}

/* Vegetable band. Fixed to the bottom of the screen. It sits behind
   everything else, and may go under the home indicator. While the keyboard
   is open it is hidden, so the card has the room. */

.veg {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--band-height);
  z-index: 0;
  background: url(assets/veg-base-1000.webp) center bottom / cover no-repeat;
  /* Melts the top edge into the green, so no line shows where the image starts. */
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 32%);
  mask-image: linear-gradient(to bottom, transparent 0, #000 32%);
  pointer-events: none;
}

/* --visible is the room below the status bar, used to size the text box.
   The bottom padding keeps clear of the home indicator. */

.app {
  --visible: calc(100dvh - env(safe-area-inset-top, 0px));
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 640px;
  margin: 0 auto;
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

/* Keyboard open. app.js adds keyboard-open to the page and sets --vv-height
   and --vv-top to the part of the screen visible above the keyboard. The
   chat is pinned to exactly that area, so the header stays at the top and
   the card sits on the keyboard; the keyboard covers the home indicator. */

.keyboard-open,
.keyboard-open body {
  overflow: hidden;
  overscroll-behavior: none;
}

.keyboard-open .veg {
  display: none;
}

.keyboard-open .app {
  --visible: calc(var(--vv-height) - env(safe-area-inset-top, 0px));
  position: fixed;
  top: var(--vv-top, 0px);
  left: 0;
  right: 0;
  height: var(--vv-height);
  padding-bottom: 0;
}

/* Header */

/* The header is 128px tall below the status bar: the old 76px plus 52px for
   the bar the app used to draw above the page. The avatar and wordmark are
   sized from the header's font size, so they scale together, and are centred
   as a pair. The app's close cross floats over the top-right corner, so a
   56px strip is kept clear on both sides (both, so the pair stays centred).
   The size is 39px, easing down on narrow phones so the pair always fits
   between the two strips; at 320px wide it is the old 26px. */

.header {
  --side: calc(56px + max(env(safe-area-inset-left, 0px), env(safe-area-inset-right, 0px)));
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.615em;
  height: calc(128px + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) var(--side) 0;
  font-size: min(39px, calc((100vw - 2 * var(--side)) / 8));
  background: #fff;
}

.header-avatar {
  flex: none;
  width: 2em;
  height: 2em;
  border-radius: 50%;
}

/* The lime underline echoes the one in the logo, with the same slanted end. */

.header-name {
  position: relative;
  margin: 0 0 0.154em;
  font-size: 1em;
  line-height: 1.15;
  font-weight: 700;
  white-space: nowrap;
  color: var(--green-deep);
}

.header-name::after {
  content: '';
  position: absolute;
  left: 0;
  right: 0;
  bottom: -0.192em;
  height: 0.115em;
  background: var(--lime);
  clip-path: polygon(0 0, 100% 0, calc(100% - 0.115em) 100%, 0 100%);
}

/* Saved chats button. It sits in the clear strip on the left of the header,
   level with the avatar, well away from the app's close cross on the right. */

.header-saved {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 44px);
  left: calc(12px + env(safe-area-inset-left, 0px));
  display: flex;
  align-items: center;
  justify-content: center;
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--lime-wash);
  color: var(--green-deep);
  cursor: pointer;
}

.bookmark path {
  transition: fill 0.2s ease;
}

.is-saved .bookmark path {
  fill: currentColor;
}

.header-saved.is-pulsing {
  animation: bookmark-pulse 0.6s ease-out;
}

@keyframes bookmark-pulse {
  0% { transform: scale(1); box-shadow: 0 0 0 0 rgba(152, 193, 97, 0.5); }
  40% { transform: scale(1.12); box-shadow: 0 0 0 6px rgba(152, 193, 97, 0.25); }
  100% { transform: scale(1); box-shadow: 0 0 0 10px rgba(152, 193, 97, 0); }
}

.header-saved:disabled {
  opacity: 0.5;
  cursor: default;
}

/* Messages. Hidden on the welcome view, when only the card shows. Once the
   conversation starts they fill the space above the card, with the first
   one pushed to the bottom so they sit close to the text box. */

.messages {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

.is-fresh .messages {
  display: none;
}

.messages > :first-child {
  margin-top: auto;
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 18px;
  font-size: 16px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble strong {
  font-weight: 700;
}

.bubble-bot {
  align-self: flex-start;
  background: #fff;
  border: 1px solid var(--line);
  border-bottom-left-radius: 6px;
}

.bubble-user {
  align-self: flex-end;
  background: var(--green-deep);
  color: #fff;
  border-bottom-right-radius: 6px;
}

.bubble-notice {
  align-self: flex-start;
  background: var(--lime-wash);
  border: 1px solid var(--lime-light);
  color: var(--green-dark);
  border-bottom-left-radius: 6px;
}

/* Typing indicator: three dots, echoing the logo */

.typing {
  display: flex;
  gap: 5px;
  align-items: center;
  padding: 14px 16px;
}

.typing span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--green-deep);
  opacity: 0.3;
  animation: typing 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing {
  0%, 60%, 100% { opacity: 0.3; transform: translateY(0); }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* New chat, Save and Copy chat: a small row just above the text box, shown
   once a chat has started. "Saved" and "Copied" appear briefly above it. */

.actions {
  position: relative;
  flex: none;
  padding: 0 max(12px, env(safe-area-inset-right, 0px)) 8px max(12px, env(safe-area-inset-left, 0px));
}

.actions-row,
.confirm-buttons {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.pill {
  min-height: 36px;
  padding: 0 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fff;
  color: var(--green-deep);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

/* The narrowest phones: a little tighter, so the row stays on one line. */

.pill-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding-left: 14px;
}

/* Wide enough for "Saved", so the row does not shift when it changes. */

.pill-save {
  min-width: 7em;
}

@media (max-width: 340px) {
  .actions-row {
    gap: 5px;
  }

  .pill {
    padding: 0 10px;
  }

  .pill-icon {
    gap: 4px;
    padding-left: 9px;
  }

  .pill-save {
    min-width: 6.2em;
  }
}

.pill-icon svg {
  flex: none;
  margin-top: -2px;
}

.pill:disabled {
  opacity: 0.5;
  cursor: default;
}

.pill-strong {
  border-color: var(--green-deep);
  background: var(--green-deep);
  color: #fff;
}

.confirm {
  padding: 10px 14px;
  border: 1px solid var(--lime-light);
  border-radius: 18px;
  background: var(--lime-wash);
}

.confirm-text {
  margin: 0 0 8px;
  font-size: 15px;
  line-height: 1.4;
  color: var(--green-dark);
  text-align: center;
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 100%;
  margin: 0 0 6px;
  padding: 6px 14px;
  border-radius: 16px;
  background: var(--green-deep);
  color: #fff;
  font-size: 14px;
  font-weight: 700;
  white-space: nowrap;
  opacity: 0;
  transform: translate(-50%, 4px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none;
}

.toast.is-shown {
  opacity: 1;
  transform: translate(-50%, 0);
}

/* The card: greeting and text box together, as one panel. On the welcome
   view it sits under the header; in a conversation it sits at the bottom. */

.card {
  display: flex;
  flex-direction: column;
  flex: 0 1 auto;
  min-height: 0;
  overflow-y: auto;
  margin: 16px max(18px, env(safe-area-inset-right, 0px)) 0 max(18px, env(safe-area-inset-left, 0px));
  padding: 14px;
  background: var(--card);
  border-radius: 20px;
}

.app:not(.is-fresh) .card {
  flex: none;
  margin: 0 max(12px, env(safe-area-inset-right, 0px)) 12px max(12px, env(safe-area-inset-left, 0px));
}

/* The greeting keeps its space as it fades, so nothing jumps while typing. */

.greeting {
  flex: none;
  max-width: none;
  margin-bottom: 8px;
  border-radius: 16px;
  transition: opacity 0.25s ease;
}

.greeting.is-hidden {
  opacity: 0;
  pointer-events: none;
}

/* The text box: a tall open writing area with the disclaimer at its base.
   It shortens on small screens, and while the keyboard is open, so the
   greeting and the box always fit. */

.field {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: none;
  height: clamp(120px, calc(var(--visible) - 250px), 200px);
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fff;
}

.app:not(.is-fresh) .field {
  height: clamp(104px, calc(var(--visible) * 0.22), 124px);
}

.field:focus-within {
  border-color: var(--lime);
}

.field-input {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  padding: 14px 58px 4px 14px;
  border: none;
  background: transparent;
  color: var(--text);
  /* 16px stops iOS zooming in when the box is tapped */
  font: inherit;
  font-size: 16px;
  line-height: 1.35;
  resize: none;
  outline: none;
}

.field-input::placeholder {
  color: var(--text-muted);
}

.field-send {
  position: absolute;
  top: 8px;
  right: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--green-deep);
  color: var(--lime-light);
  cursor: pointer;
}

.field-send:disabled {
  background: var(--send-off);
  color: #fff;
  cursor: default;
}

.disclaimer {
  flex: none;
  margin: 0;
  padding: 4px 12px 10px;
  font-size: 12px;
  line-height: 1.35;
  color: var(--text-muted);
  text-align: center;
}

/* Welcome view, keyboard closed. On a screen tall enough, the card reaches
   down to just above the vegetables and the text box fills it, with the
   disclaimer at its base. The card is capped so it never gets absurdly tall
   on a tablet or desktop. Shorter screens, such as a phone on its side, keep
   the fixed size above. The bottom margin measures from the screen edge, so
   it allows for the home indicator space the page already keeps clear. */

@media (min-height: 540px) {
  .app.is-fresh .card {
    flex: 1 1 auto;
    max-height: 640px;
    margin-bottom: max(8px, calc(var(--band-height) - var(--band-overlap) - env(safe-area-inset-bottom, 0px)));
  }

  .app.is-fresh .field {
    flex: 1 1 0;
    height: auto;
    min-height: 120px;
  }
}

/* Keyboard open, at any point in the conversation. The disclaimer steps out
   of the way and, on the welcome view, the card and text box stretch down to
   a small gap above the keyboard. Closing the keyboard removes keyboard-open,
   which puts everything back as it was. */

.keyboard-open .disclaimer,
.keyboard-open .actions {
  display: none;
}

.keyboard-open .app .card {
  margin-bottom: 6px;
}

.keyboard-open .app.is-fresh .card {
  flex: 1 1 auto;
  max-height: none;
}

.keyboard-open .app.is-fresh .field {
  flex: 1 1 0;
  height: auto;
  min-height: 120px;
}

/* Saved chats screen. It covers the chat, with the same white header so the
   app's close cross sits on white as before. Back is on the left. */

.saved {
  position: fixed;
  inset: 0;
  z-index: 3;
  display: flex;
  flex-direction: column;
  background: var(--ground);
  animation: saved-in 0.25s ease-out;
}

.saved-header {
  position: relative;
  flex: none;
  display: flex;
  align-items: center;
  justify-content: center;
  height: calc(128px + env(safe-area-inset-top, 0px));
  padding: env(safe-area-inset-top, 0px) calc(96px + env(safe-area-inset-left, 0px)) 0;
  background: #fff;
}

.saved-back {
  position: absolute;
  top: calc(env(safe-area-inset-top, 0px) + 42px);
  left: calc(12px + env(safe-area-inset-left, 0px));
  display: flex;
  align-items: center;
  gap: 2px;
  height: 44px;
  padding: 0 12px 0 6px;
  border: none;
  background: none;
  color: var(--green-deep);
  font: inherit;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}

.saved-title {
  margin: 0;
  font-size: min(26px, 7vw);
  line-height: 1.15;
  font-weight: 700;
  white-space: nowrap;
  color: var(--green-deep);
}

.saved-body {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 640px;
  margin: 0 auto;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 16px max(16px, env(safe-area-inset-right, 0px)) calc(24px + env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
}

.saved-count {
  margin: 0;
  font-size: 14px;
  font-weight: 700;
  color: var(--green-dark);
  text-align: center;
}

.saved-note {
  margin: 4px 0 14px;
  font-size: 13px;
  line-height: 1.35;
  color: var(--text-muted);
  text-align: center;
}

.saved-notice {
  margin: 0 0 12px;
  padding: 10px 14px;
  border: 1px solid var(--lime-light);
  border-radius: 18px;
  background: var(--lime-wash);
  color: var(--green-dark);
  font-size: 16px;
  line-height: 1.45;
}

.saved-empty {
  margin: 24px 0;
  font-size: 16px;
  line-height: 1.45;
  text-align: center;
}

.saved-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.saved-card {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 8px 6px 4px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fff;
}

.saved-card.is-asking {
  flex-direction: column;
  align-items: stretch;
  padding: 12px 14px;
  border-color: var(--lime-light);
  background: var(--lime-wash);
}

.saved-open {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 8px 10px;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.saved-label {
  display: -webkit-box;
  -webkit-box-orient: vertical;
  -webkit-line-clamp: 2;
  overflow: hidden;
  font-size: 16px;
  line-height: 1.35;
  overflow-wrap: anywhere;
}

.saved-date {
  font-size: 13px;
  color: var(--text-muted);
}

.saved-delete {
  flex: none;
  min-height: 36px;
  padding: 0 14px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: #fff;
  color: var(--green-deep);
  font: inherit;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

@keyframes saved-in {
  from { opacity: 0; transform: translateX(-24px); }
  to { opacity: 1; transform: none; }
}

/* Used only for a moment while copying, out of sight. */

.copy-buffer {
  position: fixed;
  top: 0;
  left: 0;
  width: 1px;
  height: 1px;
  opacity: 0;
  font-size: 16px;
  pointer-events: none;
}

@media (prefers-reduced-motion: reduce) {
  .greeting,
  .typing span,
  .toast,
  .saved,
  .bookmark path,
  .header-saved.is-pulsing {
    transition: none;
    animation: none;
  }
}

/* Splash. Shown when splash-check.js decides it is due (not within 15
   minutes of the last time), and it adds show-splash to the page. The robot fades in and settles, then "Hi," and "I'm" appear in
   lime and settle to deep green, then "NutrAssist" appears in lime and
   stays. The whole line is held for half a second, then at 2.15 seconds the
   splash fades out as the chat fades in, done by 2.5. The band behind is
   the same one the chat uses, so nothing jolts. Helvetica is asked for
   first, so iPhones use their own copy at once; nothing waits on a font. */

.splash {
  --splash-font: min(10.7vw, 46px);
  --robot-w: max(96px, min(92vw, 400px, calc((100dvh - env(safe-area-inset-top, 0px) - var(--band-height) - 16px - var(--splash-font) * 2.4) / 0.997)));
  display: none;
  position: fixed;
  inset: 0;
  z-index: 2;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) calc(var(--band-height) + 16px) env(safe-area-inset-left, 0px);
  font-family: Helvetica, Arimo, sans-serif;
  font-weight: 700;
}

.show-splash .splash {
  display: flex;
  animation: splash-out 0.35s ease-in-out 2.15s forwards;
}

.show-splash .app {
  opacity: 0;
  animation: chat-in 0.35s ease-in-out 2.15s forwards;
}

.splash-robot {
  flex: none;
  width: var(--robot-w, 88vw);
  aspect-ratio: 1024 / 1113;
  opacity: 0;
  animation: splash-fade 0.5s ease-out both;
}

.show-splash .splash-robot {
  background: url(assets/robot-shadow-720.webp) center / contain no-repeat;
}

.splash-text {
  flex: none;
  margin: calc(var(--robot-w, 88vw) * -0.09) 0 0;
  font-size: var(--splash-font);
  letter-spacing: 0.033em;
  line-height: 1.2;
  text-align: center;
  white-space: nowrap;
}

.splash-line {
  display: block;
}

.splash-hi,
.splash-im,
.splash-name {
  opacity: 0;
}

.splash-hi,
.splash-im {
  color: var(--green-deep);
}

.splash-hi {
  animation: word-in 0.25s ease-out 0.85s both;
}

.splash-im {
  animation: word-in 0.25s ease-out 1.1s both;
}

.splash-name {
  color: var(--lime-splash);
  animation: splash-fade 0.3s ease-out 1.35s both;
}

@keyframes splash-fade {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes word-in {
  0% { opacity: 0; color: var(--lime-splash); }
  20% { opacity: 1; color: var(--lime-splash); }
  100% { opacity: 1; color: var(--green-deep); }
}

@keyframes splash-out {
  to { opacity: 0; visibility: hidden; }
}

@keyframes chat-in {
  to { opacity: 1; }
}
