/* By Damian — Section 3: the AI agent (#agent).
   Back on graphite: after About, this is the product moment. The chat is my brand
   (graphite, bone, a little yellow); the website being built is THEIR brand (their palette).

   html.agent-ready (set by agent.js): the chat is live; the no-JS seed message is gone.
   .cs__slot.is-on: a piece of the client's site has been built.
   html.is-static (reduced motion / ?static): everything appears at once, no transitions. */

.agent {
  position: relative;
  background: var(--bg);
  color: var(--ink);
  --chat-surface: #2B2B2A;
  --bubble-bot: #383836;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
}

/* ---------- Stage ---------- */
.agent__stage {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100svh;
  min-height: 600px;
  padding: 76px clamp(16px, 4vw, 56px) 32px;
}
.agent__label {
  margin: 0 0 clamp(16px, 2.6svh, 28px);
  color: var(--muted);
  font-family: var(--font-body);
  font-size: 0.78rem;
  font-weight: 600;
  letter-spacing: 0.16em;
  text-align: center;
  text-transform: uppercase;
}

/* Desktop: the chat on the left, their site on the right. */
.agent__inner {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(340px, 0.8fr) minmax(0, 1fr);
  gap: clamp(24px, 4vw, 64px);
  width: 100%;
  max-width: 1320px;
  max-height: 780px;
  margin: 0 auto;
}

/* ---------- The chat ---------- */
.chat {
  min-height: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid rgba(138, 138, 133, 0.22);
  border-radius: 24px;
  background: var(--chat-surface);
  overflow: hidden;
  box-shadow: 0 40px 80px -40px rgba(0, 0, 0, 0.6);
}

.chat__head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 14px 16px 14px 18px;
  border-bottom: 1px solid rgba(138, 138, 133, 0.18);
}
.chat__avatar {
  flex: none;
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: var(--ink);
  color: var(--bg);
  font-family: var(--font-display);
  font-stretch: var(--display-stretch);
  font-weight: var(--display-weight);
  font-size: 0.95rem;
}
.chat__who { flex: 1; min-width: 0; display: grid; gap: 2px; line-height: 1.2; }
.chat__who b { font-size: 0.95rem; font-weight: 600; }
.chat__status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-size: 0.78rem;
}
.chat__status i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--accent);
}
/* Discreet: grey text, no box. */
.chat__reset {
  flex: none;
  min-height: 36px;
  padding: 0 12px;
  border: 1px solid transparent;
  border-radius: 999px;
  background: none;
  color: var(--muted);
  font: 500 0.8rem var(--font-body);
  cursor: pointer;
  transition: color 0.25s var(--ease), border-color 0.25s var(--ease);
}
.chat__reset:hover { color: var(--ink); border-color: rgba(138, 138, 133, 0.35); }
.chat__reset[hidden] { display: none; }

.chat__log {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0;
  padding: 20px 18px 12px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
  scrollbar-color: rgba(138, 138, 133, 0.35) transparent;
}
/* Bubbles stack from the bottom, like a real chat. */
.chat__log > :first-child { margin-top: auto; }

.msg {
  max-width: 84%;
  margin: 0;
  padding: 11px 15px;
  border-radius: 20px;
  font-size: 0.95rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
}
.msg--bot {
  align-self: flex-start;
  border-bottom-left-radius: 6px;
  background: var(--bubble-bot);
  color: var(--ink);
}
.msg--me {
  align-self: flex-end;
  border-bottom-right-radius: 6px;
  background: var(--ink);
  color: var(--bg);
}
.msg--me + .msg--bot,
.msg--bot + .msg--me { margin-top: 8px; }

/* Entrance: rises a little and settles. Never a bounce. */
.msg.is-new { animation: msg-in 0.45s var(--ease) both; }
.msg--me.is-new { transform-origin: 100% 100%; }
.msg--bot.is-new { transform-origin: 0 100%; }
@keyframes msg-in {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to { opacity: 1; transform: none; }
}

/* "Typing…" */
.msg--typing {
  display: inline-flex;
  gap: 5px;
  padding: 15px 16px;
}
.msg--typing i {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing 1.2s ease-in-out infinite;
}
.msg--typing i:nth-child(2) { animation-delay: 0.15s; }
.msg--typing i:nth-child(3) { animation-delay: 0.3s; }
@keyframes typing {
  0%, 60%, 100% { opacity: 0.35; transform: none; }
  30% { opacity: 1; transform: translateY(-3px); }
}

/* Before JS takes over, the seed bubble waits hidden (nothing flickers);
   if the scripts never load, the static fallback shows it. */
.js .agent__seed { visibility: hidden; }
.is-static:not(.agent-ready) .agent__seed { visibility: visible; }
.chat__nojs { margin: 0; color: var(--muted); font-size: 0.85rem; }

.chat__form {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 12px 12px 14px;
  border-top: 1px solid rgba(138, 138, 133, 0.18);
}
.chat__input {
  flex: 1;
  min-width: 0;
  height: 48px;
  padding: 0 18px;
  border: 1px solid rgba(138, 138, 133, 0.3);
  border-radius: 999px;
  background: var(--bg);
  color: var(--ink);
  font: 400 16px var(--font-body); /* 16px: phones don't zoom in on focus */
  transition: border-color 0.25s var(--ease);
}
.chat__input::placeholder { color: rgba(138, 138, 133, 0.85); }
.chat__input:focus { outline: none; border-color: rgba(237, 235, 230, 0.55); }
.chat__input:disabled { opacity: 0.6; }
.chat__send {
  flex: none;
  display: grid;
  place-items: center;
  width: 48px;
  height: 48px;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
  transition: opacity 0.25s var(--ease), transform 0.25s var(--ease);
}
.chat__send svg { width: 20px; height: 20px; }
.chat__send:disabled { opacity: 0.3; cursor: default; }
.chat__send:not(:disabled):active { transform: scale(0.94); }

.chat__note {
  margin: -4px 0 0;
  padding: 0 16px 12px;
  color: var(--muted);
  font-size: 0.74rem;
  text-align: center;
}
.chat__note[hidden] { display: none; }
.chat__note a {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-color: var(--accent);
  text-underline-offset: 3px;
}

/* ---------- Their site (right) ---------- */
.preview {
  /* Before their business type is known: neutral greys. agent.js swaps in their palette. */
  --c-page: #FAFAF8;
  --c-ink: #2A2A28;
  --c-soft: #77776F;
  --c-line: #E4E2DC;
  --c-bar: #ECEAE5;
  --c-primary: #4A4A46;
  --c-on: #FFFFFF;
  --c-tint: #EFEDE8;
  --c-accent: #D3CFC6;
  --c-star: #D3CFC6;
  --c-shadow: rgba(0, 0, 0, 0.35);
  position: relative;
  min-height: 0;
  display: flex;
  flex-direction: column;
}
.preview__open,
.preview__close { display: none; }
.preview__body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 16px;
}

/* Their site (styles: client-site.css). Here: as big as the column allows. */
.preview .cs-wrap { max-width: min(100%, calc((100svh - 400px) * 1.6)); }

/* ---------- Estimate (my brand, under their site) ---------- */
.estimate {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  column-gap: 20px;
  row-gap: 4px;
  align-items: baseline;
  width: 100%;
  max-width: min(100%, calc((100svh - 400px) * 1.6));
  margin: 0 auto;
  padding: 16px 18px;
  border: 1px solid rgba(138, 138, 133, 0.22);
  border-radius: 16px;
  background: var(--chat-surface);
  visibility: hidden;
  opacity: 0;
  transform: translateY(10px);
  transition: opacity 0.6s var(--ease), transform 0.7s var(--ease), visibility 0s linear 0.7s;
}
.estimate.is-on { visibility: visible; opacity: 1; transform: none; transition-delay: 0s; }
.estimate p { margin: 0; }
.estimate__k { grid-column: 1 / -1; color: var(--muted); font-size: 0.74rem; font-weight: 600; letter-spacing: 0.1em; text-transform: uppercase; }
.estimate__v { grid-row: 2 / span 2; color: var(--ink); font-size: 0.95rem; white-space: nowrap; }
.estimate__v b {
  display: block;
  color: var(--accent);
  font-family: var(--font-display);
  font-stretch: var(--display-stretch);
  font-weight: var(--display-weight);
  font-size: clamp(1.7rem, 2.6vw, 2.3rem);
  line-height: 1.05;
  letter-spacing: -0.02em;
}
.estimate__f { color: var(--muted); font-size: 0.74rem; line-height: 1.4; }
.estimate__d { color: var(--ink); font-size: 0.8rem; line-height: 1.45; opacity: 0.85; }

.preview__label {
  margin: 0;
  color: var(--muted);
  font-size: 0.75rem;
  letter-spacing: 0.06em;
  text-align: center;
  opacity: 0;
  transition: opacity 0.6s var(--ease) 0.4s;
}
.preview.is-done .preview__label { opacity: 1; }

/* ---------- Phone ---------- */
@media (max-width: 900px) {
  .agent__stage {
    min-height: 0;
    padding: 60px 16px max(14px, env(safe-area-inset-bottom));
  }
  .agent__label { margin-bottom: 10px; font-size: 0.72rem; }
  .agent__inner {
    display: flex;
    flex-direction: column;
    gap: 10px;
    max-height: none;
  }

  /* The site peeks in above the chat: a strip with its top. */
  .preview {
    order: -1;
    flex: none;
    height: 124px;
    border-radius: 16px;
    overflow: hidden;
    background: #262625;
    box-shadow: 0 0 0 1px rgba(138, 138, 133, 0.22);
    transition: height 0.35s var(--ease);
  }
  .preview__body { display: block; pointer-events: none; }
  .preview__body .cs { border-radius: 0; box-shadow: none; transform: none; }
  .preview .estimate,
  .preview .preview__label { display: none; }
  .preview__open {
    position: absolute;
    inset: 0;
    z-index: 2;
    display: flex;
    align-items: flex-end;
    justify-content: flex-end;
    padding: 0 10px 10px;
    border: 0;
    background: linear-gradient(to bottom, rgba(31, 31, 31, 0) 70%, rgba(31, 31, 31, 0.35));
    cursor: pointer;
  }
  .preview__chip {
    display: inline-flex;
    align-items: center;
    gap: 7px;
    padding: 6px 12px;
    border-radius: 99px;
    background: rgba(31, 31, 31, 0.92);
    color: var(--ink);
    font: 600 0.75rem var(--font-body);
    box-shadow: 0 6px 16px -6px rgba(0, 0, 0, 0.5);
  }
  .preview__chip i { display: none; width: 7px; height: 7px; border-radius: 50%; background: var(--accent); }
  .preview.has-new .preview__chip i { display: block; }
  .preview.has-new { animation: peek-pulse 0.9s var(--ease); }
  @keyframes peek-pulse {
    0% { box-shadow: 0 0 0 1px rgba(138, 138, 133, 0.22); }
    35% { box-shadow: 0 0 0 2px var(--accent); }
    100% { box-shadow: 0 0 0 1px rgba(138, 138, 133, 0.22); }
  }

  /* Opened: their whole site, full screen. */
  .preview.is-open {
    position: fixed;
    inset: 0;
    z-index: 60;
    height: auto;
    border-radius: 0;
    background: var(--bg);
    overflow-y: auto;
    overscroll-behavior: contain;
    animation: sheet-in 0.45s var(--ease);
  }
  @keyframes sheet-in {
    from { transform: translateY(40px); opacity: 0; }
    to { transform: none; opacity: 1; }
  }
  .preview.is-open .preview__open { display: none; }
  .preview.is-open .preview__body {
    flex: none;
    display: flex;
    justify-content: flex-start;
    gap: 14px;
    padding: max(64px, calc(env(safe-area-inset-top) + 52px)) 16px 32px;
    pointer-events: auto;
  }
  .preview.is-open .preview__body .cs { border-radius: 5cqw; }
  .preview.is-open .cs.is-done { box-shadow: 0 8cqw 16cqw -8cqw rgba(0, 0, 0, 0.6); }
  .preview.is-open .estimate { display: grid; }
  .preview.is-open .preview__label { display: block; }
  .preview__close {
    position: fixed;
    top: max(12px, env(safe-area-inset-top));
    right: 12px;
    z-index: 3;
    width: 44px;
    height: 44px;
    place-items: center;
    border: 1px solid rgba(138, 138, 133, 0.35);
    border-radius: 50%;
    background: var(--bg);
    color: var(--ink);
    cursor: pointer;
  }
  .preview__close svg { width: 20px; height: 20px; }
  .preview.is-open .preview__close { display: grid; }
  .agent-sheet,
  .agent-sheet body { overflow: hidden; }

  .cs-wrap,
  .estimate { max-width: 100%; }
  .estimate { grid-template-columns: 1fr; }
  .estimate__v { grid-row: auto; }

  .chat { flex: 1; border-radius: 20px; }
  .chat__head { padding: 10px 12px 10px 14px; }
  .chat__avatar { width: 34px; height: 34px; font-size: 0.85rem; }
  .chat__log { padding: 14px 12px 10px; }
  .msg { max-width: 88%; font-size: 0.93rem; padding: 10px 14px; }
  .chat__form { padding: 10px; }
  .chat__input { height: 46px; padding: 0 16px; }
  .chat__send { width: 46px; height: 46px; }

  /* Keyboard open: the section is exactly what's visible; the peek steps aside. */
  .agent.kb-open .agent__stage { height: var(--kb-h); padding-top: 12px; padding-bottom: 8px; }
  .agent.kb-open .agent__label { display: none; }
  .agent.kb-open .preview:not(.is-open) { height: 0; box-shadow: none; }
}

/* ---------- Reduced motion / ?static: everything instant ---------- */
.is-static .msg.is-new,
.is-static .preview.has-new,
.is-static .preview.is-open { animation: none; }
.is-static .msg--typing i { animation: none; }
.is-static .estimate,
.is-static .preview,
.is-static .preview__label { transition: none !important; }
@media (prefers-reduced-motion: reduce) {
  .msg.is-new, .preview.has-new, .preview.is-open, .msg--typing i { animation: none; }
}
