/* Ivy — the website assistant (assets/js/ivy/, shown on /tools/ivy).
   Uses the site's tokens from styles.css so it follows light/dark theme. */

.ivy-panel {
  --ivy-glow: #6cf2b6;
  --ivy-stage-top: #13402c;
  --ivy-stage-bottom: #0b1f17;
  --ivy-radius: 22px;
  font-family: var(--font-sans, system-ui, sans-serif);
}

/* ── Panel ──────────────────────────────────────────────────────────── */

.ivy-panel {
  position: fixed;
  right: max(16px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 950;
  width: min(400px, calc(100vw - 32px));
  height: min(680px, calc(100vh - 32px));
  height: min(680px, calc(100dvh - 32px));
  display: flex;
  flex-direction: column;
  border-radius: var(--ivy-radius);
  background: var(--bg-card, #fff);
  color: var(--text, #0b1f17);
  box-shadow: 0 24px 60px rgba(11, 31, 23, 0.28), 0 6px 18px rgba(11, 31, 23, 0.16), 0 0 0 1px var(--color-line, #d8e2db);
  overflow: hidden;
  opacity: 0;
  transform: translateY(16px) scale(0.98);
  transform-origin: bottom right;
  transition: opacity 240ms ease, transform 300ms cubic-bezier(0.22, 0.61, 0.36, 1);
}
.ivy-panel[hidden] { display: none; }
.ivy-panel [hidden] { display: none !important; }   /* buttons set display, which would override [hidden] */
.ivy-panel--open { opacity: 1; transform: none; }

.ivy-stage {
  position: relative;
  flex: none;
  height: clamp(190px, 38%, 270px);
  /* A lit studio backdrop: warm key light behind her head, green bounce from
     below, deep greenhouse green falling off to the edges. */
  background:
    radial-gradient(42% 46% at 50% var(--ivy-head-y, 40%), rgba(255, 238, 210, 0.26), rgba(255, 238, 210, 0.07) 55%, transparent 75%),
    radial-gradient(80% 55% at 50% 112%, rgba(63, 179, 127, 0.42), transparent 62%),
    radial-gradient(70% 60% at 85% 0%, rgba(108, 242, 182, 0.12), transparent 60%),
    linear-gradient(180deg, var(--ivy-stage-top), var(--ivy-stage-bottom));
  color: #eaf3ed;
  overflow: hidden;
  isolation: isolate;
}
.ivy-stage::before {         /* out-of-focus greenhouse lights (bokeh) */
  content: "";
  position: absolute;
  inset: -10%;
  z-index: 0;
  background:
    radial-gradient(circle at 12% 22%, rgba(200, 162, 74, 0.32) 0 2.2%, transparent 2.6%),
    radial-gradient(circle at 22% 64%, rgba(108, 242, 182, 0.20) 0 3.4%, transparent 3.9%),
    radial-gradient(circle at 86% 30%, rgba(255, 236, 200, 0.22) 0 2.8%, transparent 3.3%),
    radial-gradient(circle at 78% 70%, rgba(63, 179, 127, 0.26) 0 4.2%, transparent 4.8%),
    radial-gradient(circle at 92% 52%, rgba(200, 162, 74, 0.20) 0 1.8%, transparent 2.2%),
    radial-gradient(circle at 6% 46%, rgba(255, 236, 200, 0.14) 0 3%, transparent 3.5%);
  filter: blur(5px);
  opacity: 0.9;
  pointer-events: none;
}
.ivy-stage::after {          /* faint greenhouse-glass grid + a soft vignette */
  content: "";
  position: absolute;
  inset: 0;
  z-index: 0;
  background:
    radial-gradient(120% 95% at 50% 45%, transparent 55%, rgba(4, 14, 10, 0.55)),
    linear-gradient(rgba(255, 255, 255, 0.03) 1px, transparent 1px) 0 0 / 28px 28px,
    linear-gradient(90deg, rgba(255, 255, 255, 0.03) 1px, transparent 1px) 0 0 / 28px 28px;
  pointer-events: none;
}
.ivy-canvas { position: absolute; inset: 0; z-index: 1; }
.ivy-canvas canvas { position: relative; display: block; width: 100% !important; height: 100% !important; }

/* A halo behind her head that shows what she's doing. */
.ivy-canvas::before {
  content: "";
  position: absolute;
  left: 50%;
  top: var(--ivy-head-y, 40%);
  width: var(--ivy-halo, 62%);
  aspect-ratio: 1;
  transform: translate(-50%, -50%) scale(0.92);
  border-radius: 50%;
  background: radial-gradient(circle, rgba(108, 242, 182, 0.22), rgba(108, 242, 182, 0.06) 48%, transparent 70%);
  opacity: 0;
  transition: opacity 400ms ease, transform 500ms ease, background 400ms ease;
  pointer-events: none;
}
.ivy-panel--ready .ivy-canvas::before { opacity: 0.8; }
.ivy-panel[data-state="speaking"] .ivy-canvas::before { opacity: 1; animation: ivy-halo 1.6s ease-in-out infinite; }
.ivy-panel[data-state="listening"] .ivy-canvas::before {
  opacity: 1;
  background: radial-gradient(circle, rgba(255, 138, 101, 0.16), transparent 52%, rgba(255, 138, 101, 0.28) 60%, transparent 68%);
  animation: ivy-halo 1.2s ease-in-out infinite;
}
.ivy-panel[data-state="thinking"] .ivy-canvas::before {
  opacity: 1;
  background: radial-gradient(circle, rgba(200, 162, 74, 0.24), rgba(200, 162, 74, 0.06) 48%, transparent 70%);
  animation: ivy-halo 2.4s ease-in-out infinite;
}

/* The 3D Ivy fades in once the model has loaded. */
.ivy-canvas canvas { opacity: 0; transition: opacity 600ms ease; }
.ivy-panel--ready .ivy-canvas canvas { opacity: 1; }

.ivy-loading {
  position: absolute;
  left: 50%;
  bottom: 18px;
  z-index: 3;
  margin: 0;
  padding: 5px 12px;
  border-radius: 999px;
  transform: translateX(-50%);
  background: rgba(11, 31, 23, 0.6);
  backdrop-filter: blur(6px);
  white-space: nowrap;
  font-size: 0.78rem;
  color: rgba(234, 243, 237, 0.9);
}
.ivy-flat { position: absolute; left: 50%; bottom: 0; height: 92%; transform: translateX(-50%); }
.ivy-flat__eyes { transform-origin: center; transform-box: fill-box; animation: ivy-blink 5s infinite; }
.ivy-canvas[data-state="thinking"] .ivy-flat__mouth { animation: ivy-pulse 0.9s ease-in-out infinite; }

.ivy-stage__top {
  position: absolute;
  inset: 0 0 auto;
  z-index: 2;
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  padding: 14px 12px 0 18px;
}
.ivy-id__name {
  margin: 0;
  font: 600 1.35rem/1.1 var(--font-display, Georgia, serif);
  color: #fff;
}
.ivy-id__role { margin: 2px 0 0; font-size: 0.78rem; color: rgba(234, 243, 237, 0.7); }
.ivy-actions { display: flex; gap: 2px; }

.ivy-icon-btn {
  width: 38px;
  height: 38px;
  display: inline-grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
  transition: background 140ms, color 140ms;
}
.ivy-stage .ivy-icon-btn { color: rgba(234, 243, 237, 0.85); }
.ivy-stage .ivy-icon-btn:hover { background: rgba(255, 255, 255, 0.1); color: #fff; }
.ivy-icon-btn:focus-visible, .ivy-send:focus-visible, .ivy-chip:focus-visible { outline: 3px solid var(--color-sprout, #3fb37f); outline-offset: 1px; }
.ivy-icon-btn[aria-pressed="false"][data-act="voice"] { color: rgba(234, 243, 237, 0.5); }

.ivy-status {
  position: absolute;
  left: 18px;
  bottom: 12px;
  z-index: 2;
  margin: 0;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px 5px 9px;
  border-radius: 999px;
  background: rgba(11, 31, 23, 0.55);
  backdrop-filter: blur(6px);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  color: #eaf3ed;            /* the site colours <p> dark in light theme */
}
.ivy-status__dot { width: 8px; height: 8px; border-radius: 50%; background: var(--ivy-glow); box-shadow: 0 0 10px var(--ivy-glow); }
.ivy-panel[data-state="thinking"] .ivy-status__dot,
.ivy-panel[data-state="listening"] .ivy-status__dot { animation: ivy-pulse 0.9s ease-in-out infinite; }
.ivy-panel[data-state="listening"] .ivy-status__dot { background: #ff8a65; box-shadow: 0 0 10px #ff8a65; }

/* ── Conversation ───────────────────────────────────────────────────── */

.ivy-log {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 16px 14px 8px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  scroll-behavior: smooth;
}
.ivy-log:focus-visible { outline: 2px solid var(--color-sprout, #3fb37f); outline-offset: -2px; }
.ivy-msg { display: flex; }
.ivy-msg--user { justify-content: flex-end; }
.ivy-msg__bubble {
  max-width: 86%;
  padding: 10px 13px;
  border-radius: 16px;
  font-size: 0.94rem;
  line-height: 1.45;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}
.ivy-msg--ivy .ivy-msg__bubble { background: var(--color-mist, #e6efe9); border-bottom-left-radius: 5px; }
.ivy-msg--user .ivy-msg__bubble { background: var(--color-emerald, #0e5c3a); color: #fff; border-bottom-right-radius: 5px; }
.ivy-msg__bubble a { color: var(--color-emerald, #0e5c3a); font-weight: 600; text-underline-offset: 2px; }
[data-theme="dark"] .ivy-msg__bubble a { color: var(--ivy-glow); }
.ivy-link { white-space: nowrap; }

.ivy-typing { display: inline-flex; gap: 4px; padding: 4px 2px; }
.ivy-typing i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; opacity: 0.35; animation: ivy-dots 1.1s infinite; }
.ivy-typing i:nth-child(2) { animation-delay: 0.15s; }
.ivy-typing i:nth-child(3) { animation-delay: 0.3s; }

.ivy-chips { display: flex; flex-wrap: wrap; gap: 6px; padding: 0 14px 10px; }
.ivy-chips[hidden] { display: none; }
.ivy-chip {
  border: 1px solid var(--color-line, #d8e2db);
  border-radius: 999px;
  padding: 7px 12px;
  background: transparent;
  color: var(--text, #0b1f17);
  font: 500 0.82rem/1.2 var(--font-sans, system-ui, sans-serif);
  cursor: pointer;
  transition: background 140ms, border-color 140ms;
}
.ivy-chip:hover { background: var(--color-mist, #e6efe9); border-color: var(--color-sprout, #3fb37f); }

.ivy-form {
  flex: none;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  margin: 0 12px;
  padding: 6px 6px 6px 14px;
  border: 1px solid var(--color-line, #d8e2db);
  border-radius: 18px;
  background: var(--bg-page, #f7f4ee);
  transition: border-color 140ms, box-shadow 140ms;
}
.ivy-form:focus-within { border-color: var(--color-sprout, #3fb37f); box-shadow: 0 0 0 3px rgba(63, 179, 127, 0.18); }
.ivy-input {
  flex: 1;
  min-width: 0;
  max-height: 120px;
  margin: 0;
  padding: 9px 0;
  border: 0;
  background: transparent;
  color: var(--text, #0b1f17);
  font: 400 16px/1.4 var(--font-sans, system-ui, sans-serif); /* 16px stops iOS zooming in */
  resize: none;
  outline: none;
  overflow-y: hidden;
}
.ivy-input::placeholder { color: var(--text-muted, #5b6b62); }
.ivy-form .ivy-icon-btn { color: var(--text-muted, #5b6b62); }
.ivy-form .ivy-icon-btn:hover { background: var(--color-mist, #e6efe9); color: var(--text, #0b1f17); }
.ivy-mic.is-live { background: #ffe3da; color: #b3261e; animation: ivy-ring 1.4s infinite; }
.ivy-send {
  width: 38px;
  height: 38px;
  flex: none;
  display: inline-grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--color-emerald, #0e5c3a);
  color: #fff;
  cursor: pointer;
  transition: background 140ms, transform 140ms;
}
.ivy-send:hover { background: var(--color-emerald-2, #0a4a2f); }
.ivy-send svg { width: 17px; height: 17px; }
.ivy-form.is-busy .ivy-send { opacity: 0.5; pointer-events: none; }

.ivy-note { flex: none; margin: 8px 16px 12px; font-size: 0.72rem; line-height: 1.35; color: var(--text-muted, #5b6b62); text-align: center; }
.ivy-note a { color: inherit; }
.ivy-sr { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); border: 0; }

/* Phones: a full-screen sheet. */
@media (max-width: 520px) {
  .ivy-panel {
    right: 0;
    bottom: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    border-radius: 0;
    transform: translateY(24px);
  }
  .ivy-panel.ivy-panel--open { transform: none; }
  .ivy-stage { height: 34%; }
  .ivy-is-open body { overflow: hidden; }
}

/* Inline: part of a page (/tools/ivy) rather than floating in the corner. */
.ivy-panel.ivy-panel--inline {
  position: relative;
  inset: auto;
  z-index: auto;
  width: 100%;
  height: min(720px, 82vh);
  height: min(720px, 82dvh);
  border-radius: var(--ivy-radius);
  opacity: 1;
  transform: none;
  box-shadow: var(--shadow-lg, 0 18px 40px rgba(11, 31, 23, 0.12)), 0 0 0 1px var(--color-line, #d8e2db);
}
.ivy-panel--inline .ivy-stage { height: clamp(220px, 46%, 360px); }

/* Wide screens: Ivy stands beside the conversation, like a video call. */
@media (min-width: 760px) {
  .ivy-panel.ivy-panel--inline:not([hidden]) {   /* :not([hidden]) so [hidden] still wins */
    display: grid;
    grid-template-columns: minmax(0, 0.92fr) minmax(0, 1.08fr);
    grid-template-rows: minmax(0, 1fr) auto auto auto;
    height: min(640px, 82vh);
    height: min(640px, 82dvh);
  }
  .ivy-panel--inline .ivy-stage { grid-column: 1; grid-row: 1 / -1; height: auto; }
  .ivy-panel--inline .ivy-log,
  .ivy-panel--inline .ivy-chips,
  .ivy-panel--inline .ivy-form,
  .ivy-panel--inline .ivy-note { grid-column: 2; }
  .ivy-panel--inline .ivy-log { padding-top: 20px; }
  .ivy-panel--inline .ivy-form { margin-top: 4px; }
}

/* Name card and status sit on frosted glass so they read over her. */
.ivy-id {
  padding: 8px 14px 9px;
  border-radius: 14px;
  background: linear-gradient(135deg, rgba(11, 31, 23, 0.62), rgba(11, 31, 23, 0.32));
  backdrop-filter: blur(8px);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}
.ivy-id__name::after {       /* a sprout of green after her name */
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-left: 6px;
  vertical-align: 0.2em;
  border-radius: 50% 0;
  background: var(--color-sprout, #3fb37f);
}
.ivy-stage .ivy-actions { padding: 2px; border-radius: 999px; background: rgba(11, 31, 23, 0.35); backdrop-filter: blur(8px); }
.ivy-status { box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08); }

@media (prefers-reduced-motion: reduce) {
  .ivy-panel { transition: none; }
  .ivy-flat__eyes { animation: none; }
  .ivy-log { scroll-behavior: auto; }
  .ivy-canvas::before { animation: none !important; }
  .ivy-canvas canvas { transition: none; }
}

@media print { .ivy-panel { display: none !important; } }

@keyframes ivy-halo { 0%, 100% { transform: translate(-50%, -50%) scale(0.92); } 50% { transform: translate(-50%, -50%) scale(1.02); } }

@keyframes ivy-blink { 0%, 92%, 100% { transform: scaleY(1); } 95% { transform: scaleY(0.1); } }
@keyframes ivy-pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }
@keyframes ivy-dots { 0%, 60%, 100% { opacity: 0.25; transform: translateY(0); } 30% { opacity: 0.8; transform: translateY(-3px); } }
@keyframes ivy-ring { 0% { box-shadow: 0 0 0 0 rgba(179, 38, 30, 0.35); } 100% { box-shadow: 0 0 0 10px rgba(179, 38, 30, 0); } }
