:root {
  --bg: #f6f8fc;
  --surface: #ffffff;
  --ink: #0f172a;
  --ink-2: #475569;
  --ink-3: #94a3b8;
  --line: #e5eaf2;
  --line-blue: rgba(59, 130, 246, 0.18);
  --blue: #2563eb;
  --blue-deep: #1d4ed8;
  --blue-soft: #eef4ff;
  --amber-soft: #fff7ed;
  --amber-ink: #9a3412;
  --radius: 14px;
  --chat-w: 460px;
  --font-display: "Plus Jakarta Sans", system-ui, sans-serif;
  --font-body: "Inter", system-ui, sans-serif;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 400 15px/1.6 var(--font-body);
  -webkit-font-smoothing: antialiased;
}
button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

.app {
  display: grid;
  grid-template-columns: var(--chat-w) 1fr;
  grid-template-rows: auto 1fr auto;
  grid-template-areas: "header stage" "chat stage" "chat protocol";
  height: var(--app-h, 100dvh);
}
.app-header { grid-area: header; }
.chat { grid-area: chat; }
.stage { grid-area: stage; }
.protocol { grid-area: protocol; }

/* ---------- Chat ---------- */
.chat {
  display: flex;
  flex-direction: column;
  min-height: 0;
  min-width: 0;
  background: var(--surface);
  border-right: 1px solid var(--line);
}
.app-header {
  background: var(--surface);
  border-right: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 24px;
}
.wordmark {
  display: inline-flex;
  align-items: center;
  gap: 9px;
  font: 600 19px/1 var(--font-display);
  letter-spacing: -0.02em;
  color: var(--ink);
  text-decoration: none;
}
.wordmark-dot {
  width: 18px; height: 18px;
  border-radius: 46% 54% 52% 48% / 55% 45% 55% 45%;
  background: radial-gradient(circle at 35% 30%, #93c5fd, #3b82f6 55%, #1d4ed8);
}
.ghost-btn {
  border: 0;
  background: none;
  color: var(--ink-2);
  font-size: 13px;
  padding: 6px 10px;
  border-radius: 8px;
}
.ghost-btn:hover { background: var(--bg); color: var(--ink); }

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 8px 24px 24px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}
.msg { max-width: 100%; animation: rise 0.35s ease both; }
.msg p { margin: 0 0 0.6em; }
.msg p:last-child { margin-bottom: 0; }
.msg strong { font-weight: 600; }
.msg.assistant { color: var(--ink); padding-right: 12px; }
.msg.user {
  align-self: flex-end;
  max-width: 82%;
  background: var(--blue-soft);
  color: var(--ink);
  padding: 10px 14px;
  border-radius: 16px 16px 4px 16px;
}
.msg.error { color: var(--amber-ink); }
.typing { display: inline-flex; gap: 5px; padding: 6px 0; }
.typing span {
  width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-3);
  animation: blink 1.2s infinite ease-in-out;
}
.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

.rec-link {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-top: 10px;
  margin-right: 6px;
  padding: 6px 12px 6px 8px;
  border: 1px solid var(--line-blue);
  border-radius: 999px;
  background: var(--surface);
  color: var(--blue-deep);
  font-size: 13px;
  font-weight: 500;
}
.rec-link:hover { background: var(--blue-soft); }
.rec-link::before {
  content: "";
  width: 14px; height: 14px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #93c5fd, #3b82f6 60%, #1d4ed8);
}

/* ---------- Composer ---------- */
.composer { padding: 12px 24px 16px; border-top: 1px solid var(--line); }
.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips:not(:empty) { margin-bottom: 12px; }
.chip {
  border: 1px solid var(--line-blue);
  background: var(--surface);
  color: var(--blue-deep);
  padding: 7px 14px;
  border-radius: 999px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.15s, border-color 0.15s;
}
.chip:hover { background: var(--blue-soft); border-color: rgba(59, 130, 246, 0.35); }
.chip.muted { color: var(--ink-2); border-color: var(--line); }

.input-row { display: flex; gap: 8px; align-items: center; }
.field {
  flex: 1;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
  border: 1px solid var(--line);
  background: var(--bg);
  border-radius: 12px;
  padding: 0 14px;
  transition: border-color 0.15s, box-shadow 0.15s, background 0.15s;
}
.field:focus-within { border-color: #3b82f6; background: var(--surface); box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.12); }
.field input {
  flex: 1;
  min-width: 0;
  border: 0;
  outline: 0;
  background: none;
  padding: 12px 0;
  font-size: 16px;
}
.field input::placeholder { color: var(--ink-3); }
.field .unit { color: var(--ink-3); font-size: 14px; }
.field input[type="number"] { -moz-appearance: textfield; }
.field input::-webkit-outer-spin-button, .field input::-webkit-inner-spin-button { -webkit-appearance: none; margin: 0; }

.seg {
  display: inline-flex;
  padding: 3px;
  background: var(--bg);
  border: 1px solid var(--line);
  border-radius: 10px;
  flex-shrink: 0;
}
.seg button {
  border: 0;
  background: none;
  padding: 7px 10px;
  border-radius: 7px;
  font-size: 13px;
  color: var(--ink-2);
}
.seg button[aria-pressed="true"] { background: var(--surface); color: var(--ink); box-shadow: 0 1px 2px rgba(15, 23, 42, 0.08); }

.send {
  flex-shrink: 0;
  width: 44px; height: 44px;
  border: 0;
  border-radius: 12px;
  background: var(--blue);
  color: #fff;
  display: grid;
  place-items: center;
  transition: background 0.15s, opacity 0.15s;
}
.send:hover { background: var(--blue-deep); }
.send:disabled { opacity: 0.4; cursor: default; }
.fineprint { margin: 10px 0 0; font-size: 11.5px; color: var(--ink-3); }
.form-error { color: var(--amber-ink); font-size: 13px; margin: 8px 0 0; }

/* ---------- Stage ---------- */
.stage {
  position: relative;
  overflow: hidden;
  display: grid;
  place-items: center;
  background:
    radial-gradient(60% 50% at 50% 45%, rgba(147, 197, 253, 0.28), transparent 70%),
    var(--bg);
}
.orb {
  position: absolute;
  left: 50%; top: 50%;
  width: 300px; height: 300px;
  will-change: transform, border-radius, width, height;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  filter: drop-shadow(0 30px 60px rgba(37, 99, 235, 0.22));
}
.orb-fill, .orb-card-bg {
  position: absolute;
  inset: 0;
  border-radius: inherit;
}
.orb-fill {
  background:
    radial-gradient(circle at 30% 25%, rgba(255, 255, 255, 0.75), transparent 38%),
    radial-gradient(circle at 70% 80%, rgba(56, 189, 248, 0.75), transparent 55%),
    linear-gradient(140deg, #60a5fa, #2563eb 55%, #1e40af);
}
.orb-card-bg {
  background: var(--surface);
  border: 1px solid var(--line-blue);
  opacity: 0;
}
.stage-caption {
  position: absolute;
  bottom: 40px;
  left: 0; right: 0;
  margin: 0;
  text-align: center;
  color: var(--ink-3);
  font-size: 13px;
  transition: opacity 0.4s;
}

.product {
  position: relative;
  z-index: 2;
  width: min(400px, calc(100% - 48px));
  padding: 28px;
  opacity: 0;
  transform: translateY(6px);
  transition: opacity 0.35s ease, transform 0.35s ease;
  pointer-events: none;
}
.product.visible { opacity: 1; transform: none; pointer-events: auto; }
.product-close {
  position: absolute;
  top: 16px; right: 16px;
}
.icon-btn {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border: 0;
  border-radius: 10px;
  background: none;
  color: var(--ink-3);
}
.icon-btn:hover { background: var(--bg); color: var(--ink); }

.eyebrow {
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--blue);
}
.product-head { display: flex; align-items: center; gap: 14px; margin-right: 36px; }
.product-img { flex: 0 0 76px; width: 76px; height: 76px; border-radius: 14px; object-fit: cover; }
.product-head + .tagline { margin-top: 10px; }
.product h2 {
  margin: 4px 0 0;
  font: 500 26px/1.2 var(--font-display);
  letter-spacing: -0.02em;
}
.product .tagline { margin: 0; color: var(--ink-2); font-size: 14px; }
.why {
  margin: 20px 0;
  padding: 14px 16px;
  background: var(--blue-soft);
  border-radius: 12px;
  font-size: 14px;
}
.why b { display: block; font-size: 12px; font-weight: 600; color: var(--blue-deep); margin-bottom: 2px; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1px; background: var(--line); border-radius: 12px; overflow: hidden; border: 1px solid var(--line); }
.stat { background: var(--surface); padding: 12px; }
.stat .v { font: 500 18px/1.2 var(--font-display); font-variant-numeric: tabular-nums; }
.stat .l { font-size: 12px; color: var(--ink-3); }
.how { margin: 18px 0 0; font-size: 13.5px; color: var(--ink-2); }
.how b { color: var(--ink); font-weight: 600; }
.caution { margin: 12px 0 0; padding: 10px 12px; background: var(--amber-soft); color: var(--amber-ink); border-radius: 10px; font-size: 13px; }
.buy {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 22px;
}
.price { font: 500 22px/1 var(--font-display); }
.price small { display: block; margin-top: 4px; font: 400 12px/1 var(--font-body); color: var(--ink-3); }
.primary-btn {
  border: 0;
  background: var(--blue);
  color: #fff;
  padding: 12px 20px;
  border-radius: 12px;
  font-weight: 500;
  text-decoration: none;
  transition: background 0.15s, box-shadow 0.15s;
}
.primary-btn:hover { background: var(--blue-deep); box-shadow: 0 0 16px rgba(37, 99, 235, 0.3); }

.pager { display: flex; justify-content: center; gap: 8px; margin-top: 20px; }
.pager:empty { display: none; }
.pager button {
  border: 1px solid var(--line);
  background: var(--surface);
  padding: 5px 12px;
  border-radius: 999px;
  font-size: 12.5px;
  color: var(--ink-2);
}
.pager button[aria-current="true"] { border-color: var(--blue); color: var(--blue-deep); background: var(--blue-soft); }

/* ---------- Your Protocol ---------- */
.header-actions { display: flex; align-items: center; gap: 6px; }
.protocol-pill { display: none; }

.protocol {
  min-width: 0;
  background: var(--bg);
  border-top: 1px solid var(--line);
  padding: 14px 24px 18px;
}
.protocol.appearing { animation: protocol-in 0.35s ease both; overflow: hidden; }
.protocol-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.protocol-head h2 { margin: 0; font: 500 15px/1.2 var(--font-display); letter-spacing: -0.01em; }
.protocol-count {
  min-width: 20px; height: 20px; padding: 0 6px;
  display: inline-grid; place-items: center;
  border-radius: 999px;
  background: var(--blue-soft);
  color: var(--blue-deep);
  font-size: 12px; font-weight: 600;
  font-variant-numeric: tabular-nums;
}
.protocol-close { display: none; margin-left: auto; }
.protocol-list {
  display: flex;
  gap: 10px;
  overflow-x: auto;
  padding: 2px 2px 4px;
  scroll-snap-type: x proximity;
  scrollbar-width: thin;
}
.proto-item {
  position: relative;
  flex: 0 0 240px;
  scroll-snap-align: start;
  border: 1px solid var(--line-blue);
  border-radius: 12px;
  background: var(--surface);
  transition: border-color 0.15s, box-shadow 0.15s;
}
.proto-item:hover { border-color: rgba(59, 130, 246, 0.4); box-shadow: 0 6px 18px -10px rgba(37, 99, 235, 0.35); }
.proto-item.incoming { visibility: hidden; }
.proto-item.added { animation: proto-pop 0.5s cubic-bezier(0.3, 1.4, 0.5, 1) both, proto-glow 1.6s ease-out both; }
.proto-open {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
  padding: 10px 34px 10px 12px;
  border-radius: inherit;
}
.proto-open .eyebrow { font-size: 10px; }
.proto-img { flex: 0 0 44px; width: 44px; height: 44px; border-radius: 10px; object-fit: cover; }
.proto-text { min-width: 0; display: block; }
.proto-text > span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.proto-name { display: block; margin: 2px 0 1px; font-weight: 500; font-size: 14px; line-height: 1.3; }
.proto-meta { display: block; font-size: 12px; color: var(--ink-3); }
.proto-remove {
  position: absolute;
  top: 4px; right: 4px;
  width: 28px; height: 28px;
  opacity: 0;
  transition: opacity 0.15s;
}
.proto-item:hover .proto-remove, .proto-remove:focus-visible { opacity: 1; }
@media (hover: none) { .proto-remove { opacity: 0.7; } }

/* The card that flies into the list when something is added. */
.proto-ghost {
  position: fixed;
  z-index: 50;
  pointer-events: none;
  transform-origin: 0 0;
  border: 1px solid var(--line-blue);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 18px 40px -16px rgba(37, 99, 235, 0.45);
  overflow: hidden;
}
.proto-ghost .proto-open { padding-right: 12px; }

.protocol-pill.bump { animation: proto-pop 0.45s cubic-bezier(0.3, 1.6, 0.5, 1); }

@keyframes protocol-in {
  from { max-height: 0; padding-top: 0; padding-bottom: 0; opacity: 0; }
  to { max-height: 200px; opacity: 1; }
}
@keyframes proto-pop { 0% { transform: scale(0.9); } 60% { transform: scale(1.04); } 100% { transform: scale(1); } }
@keyframes proto-glow { from { box-shadow: 0 0 0 3px rgba(59, 130, 246, 0.35); border-color: var(--blue); } to { box-shadow: 0 0 0 0 rgba(59, 130, 246, 0); } }

@keyframes rise { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: none; } }
@keyframes blink { 0%, 80%, 100% { opacity: 0.25; } 40% { opacity: 1; } }

/* ---------- Small screens ---------- */
@media (max-width: 860px) {
  :root { --band: clamp(88px, 17dvh, 150px); }
  .app {
    grid-template-columns: minmax(0, 1fr); /* don't let the swipeable chip row widen the page */
    grid-template-rows: auto var(--band) minmax(0, 1fr);
    grid-template-areas: "header" "stage" "chat";
  }
  .app-header {
    border-right: 0;
    padding: calc(10px + env(safe-area-inset-top)) 16px 10px;
    background: var(--bg);
  }
  .stage { background: radial-gradient(50% 60% at 50% 50%, rgba(147, 197, 253, 0.3), transparent 70%), var(--bg); }
  .stage-caption { display: none; }
  .chat {
    border-right: 0;
    border-top: 1px solid var(--line);
    border-radius: 20px 20px 0 0;
    box-shadow: 0 -8px 24px -12px rgba(15, 23, 42, 0.08);
  }
  .messages { padding: 18px 16px 16px; gap: 16px; }
  .msg.user { max-width: 88%; }
  .composer { padding: 10px 16px calc(10px + env(safe-area-inset-bottom)); }
  .fineprint { margin-top: 8px; font-size: 11px; }

  /* Keyboard open, or a short landscape screen: give all the room to the chat. */
  .app.kb-open { grid-template-rows: auto 0 minmax(0, 1fr); }
  .app.kb-open .stage { visibility: hidden; }
  .app.kb-open .fineprint { display: none; }

  /* Chips: goals as a tidy 2-column grid, replies as one swipeable row. */
  .chips { gap: 8px; }
  .chips.grid { display: grid; grid-template-columns: 1fr 1fr; }
  .chips.grid .chip { text-align: center; }
  .chips.scroll {
    flex-wrap: nowrap;
    overflow-x: auto;
    margin: 0 -16px;
    padding: 0 16px 2px;
    scrollbar-width: none;
    -webkit-mask-image: linear-gradient(90deg, #000 85%, transparent);
            mask-image: linear-gradient(90deg, #000 85%, transparent);
  }
  .chips.scroll::-webkit-scrollbar { display: none; }
  .chips.scroll .chip { flex-shrink: 0; }
  .chip { min-height: 40px; padding: 8px 14px; }

  /* Unit switch sits on its own line above the number boxes. */
  .input-row { flex-wrap: wrap; row-gap: 8px; }
  .input-row .seg { order: -1; flex-basis: 100%; }
  .input-row .seg button { flex: 1; padding: 8px 10px; }

  /* Product card opens over everything, scrolls if taller than the screen. */
  .app.show-product .stage {
    position: fixed;
    inset: 0;
    z-index: 10;
    background: rgba(246, 248, 252, calc(0.9 * var(--veil, 0)));
    -webkit-backdrop-filter: blur(calc(8px * var(--veil, 0)));
            backdrop-filter: blur(calc(8px * var(--veil, 0)));
  }
  .product {
    width: min(420px, calc(100% - 32px));
    max-height: calc(var(--app-h, 100dvh) - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 24px 20px 20px;
  }
  .product h2 { font-size: 22px; }
  .product-img { flex-basis: 64px; width: 64px; height: 64px; }
  .product .why { margin: 16px 0; padding: 12px 14px; }
  .product .how { margin-top: 14px; }
  .product .buy { margin-top: 16px; }
  .product .pager { margin-top: 14px; }
  .product { scrollbar-width: none; }
  .product::-webkit-scrollbar { display: none; }
  .product-close { top: 10px; right: 10px; width: 44px; height: 44px; }
  .buy { flex-wrap: wrap; }
  .buy .primary-btn { flex: 1; text-align: center; min-width: 150px; }
}

@media (max-width: 860px) {
  .protocol-pill:not([hidden]) {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    height: 34px;
    padding: 0 6px 0 10px;
    border: 1px solid var(--line-blue);
    border-radius: 999px;
    background: var(--surface);
    color: var(--blue-deep);
    font-size: 13px;
    font-weight: 500;
  }
  .protocol-pill .wordmark-dot { width: 12px; height: 12px; }
  .pill-count {
    min-width: 22px; height: 22px; padding: 0 6px;
    display: inline-grid; place-items: center;
    border-radius: 999px;
    background: var(--blue);
    color: #fff;
    font-size: 12px; font-weight: 600;
  }

  /* On phones Your Protocol is a sheet that slides up from the bottom. */
  .protocol {
    position: fixed;
    left: 0; right: 0; bottom: 0;
    z-index: 30;
    max-height: 75dvh;
    display: flex;
    flex-direction: column;
    padding: 16px 16px calc(16px + env(safe-area-inset-bottom));
    border-top: 0;
    border-radius: 20px 20px 0 0;
    background: var(--surface);
    box-shadow: 0 -12px 40px -12px rgba(15, 23, 42, 0.25);
    transform: translateY(105%);
    visibility: hidden;
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1), visibility 0s linear 0.3s;
  }
  .protocol.appearing { animation: none; }
  .app.show-protocol .protocol { transform: none; visibility: visible; transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1); }
  .protocol-head h2 { font-size: 18px; }
  .protocol-close { display: grid; width: 44px; height: 44px; margin-right: -8px; }
  .protocol-list { flex-direction: column; overflow-x: hidden; overflow-y: auto; scroll-snap-type: none; padding-bottom: 2px; }
  .proto-item { flex: none; }
  .proto-open { padding: 14px 48px 14px 14px; }
  .proto-name { font-size: 15px; }
  .proto-img { flex-basis: 52px; width: 52px; height: 52px; }
  .proto-remove { top: 50%; right: 6px; width: 40px; height: 40px; transform: translateY(-50%); opacity: 0.7; }

  .sheet-backdrop {
    position: fixed;
    inset: 0;
    z-index: 29;
    background: rgba(15, 23, 42, 0.28);
    animation: fade-in 0.2s ease both;
  }
}
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

@media (max-width: 860px) and (max-height: 520px) {
  .app { grid-template-rows: auto 0 minmax(0, 1fr); }
  .stage { visibility: hidden; }
  .app.show-product .stage { visibility: visible; }
}

@media (prefers-reduced-motion: reduce) {
  .msg, .product { animation: none; transition: none; }
}

.primary-btn { white-space: nowrap; }
