/*
  idirnet "link" chat widget — plain CSS port of ChatWidget.module.scss.
  CSP-safe: no external refs, no @import, no CDN. Same-origin only.

  Tokens consumed from the hero's :root (style.css):
    --fg, --muted, --bg, --link, --link-blue (#4cc4e3), --link-orange.
  The hero registers 'Inter' + 'JetBrains Mono' as same-origin @font-face but
  does NOT define --font-mono / --font-sans vars, so we use explicit stacks.

  Every class is prefixed `chat-` so the mandala click-handler in app.js
  (closest('[class*="chat"]')) skips all chat UI and never erupts a ripple
  over a control.
*/

/* Hidden host for the SVG #linkGrain filter used by .chat-panel's backdrop.
   Zero-size, non-interactive. */
.chat-grain-defs {
  position: absolute;
  width: 0;
  height: 0;
  pointer-events: none;
}

/* ── Corner brand toggle ─────────────────────────────────────────────────── */
.chat-toggle {
  position: fixed;
  top: clamp(1.75rem, 3.5vw, 2.5rem);
  right: clamp(1.75rem, 3.5vw, 2.5rem);
  z-index: 10;
  width: 80px;
  height: 80px;
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 0;
  display: grid;
  place-items: center;
  /* Fade in gently when the widget mounts, matching the brand word's fade-in. */
  animation: chatMarkFadeIn 1.618s ease both;
}

@keyframes chatMarkFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

.chat-icon-logo,
.chat-icon-question {
  grid-area: 1 / 1;
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  transition: opacity 0.45s steps(4) ease;
  will-change: opacity;
}

.chat-icon-logo {
  opacity: 1;
}

/* Corner brand mark — single SVG morphing between two states.
   Closed: idirnet mark (white outer ring + small white CENTRE dot).
   Open:   link logo   (blue outer ring + small blue ring SHIFTED LEFT). */
.chat-brand-mark {
  width: 80px;
  height: auto;
  display: block;
}

.chat-brand-ring {
  stroke: var(--fg);
  stroke-width: 1.4;
  transition: stroke 0.45s ease;
  animation: chatRingGlow 7.5s ease-in-out infinite;
  will-change: opacity;
}

.chat-brand-inner {
  fill: var(--fg);
  stroke: var(--fg);
  stroke-width: 0;
  transition:
    cx 0.45s cubic-bezier(0.22, 0.61, 0.36, 1),
    cy 0.45s cubic-bezier(0.22, 0.61, 0.36, 1),
    r 0.45s cubic-bezier(0.22, 0.61, 0.36, 1),
    fill 0.45s ease,
    stroke 0.45s ease,
    stroke-width 0.45s ease;
  animation: chatDotGlow 4.5s ease-in-out infinite;
  will-change: opacity;
}

/* OPEN state: morph from idirnet mark → link logo.
   Inner circle shifts left + slightly down, grows, loses the fill, gains a
   stroke. CSS geometry props (cx/cy/r) — faithful to the source. */
.chat-toggle.open .chat-brand-ring {
  stroke: var(--link-blue);
}

.chat-toggle.open .chat-brand-inner {
  cx: 11;
  cy: 18;
  r: 5;
  fill: transparent;
  stroke: var(--link-blue);
  stroke-width: 1.4;
}

.chat-icon-question {
  opacity: 0;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 2rem;
  color: var(--fg);
  line-height: 1;
}

.chat-toggle:hover .chat-icon-logo {
  opacity: 0;
}

.chat-toggle:hover .chat-icon-question {
  opacity: 1;
}

.chat-toggle:hover .chat-brand-ring,
.chat-toggle:hover .chat-brand-inner {
  animation-play-state: paused;
}

@keyframes chatRingGlow {
  0%, 100% { opacity: 0.55; }
  50%      { opacity: 1; }
}

@keyframes chatDotGlow {
  0%, 100% { opacity: 1; }
  50%      { opacity: 0.4; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-icon-logo,
  .chat-icon-question,
  .chat-brand-ring,
  .chat-brand-inner {
    transition: none;
  }
  .chat-brand-ring,
  .chat-brand-inner {
    animation: none;
  }
}

/* ── Chat panel ──────────────────────────────────────────────────────────── */
/* Anchored to the brand toggle in the top-right corner. The panel's top-right
   is the same point as the icon, and the panel scales out from there.
   Frosted glass: pure backdrop blur + #linkGrain noise, no tint — the blur
   discontinuity alone defines the panel edge (matches idir-www ChatWidget). */
.chat-panel {
  position: fixed;
  /* Down + left so the panel's top-right corner sits BELOW the link mark's
     centre, letting the logo nest snugly inside the corner (matches the mark's
     top/right inset). */
  top: clamp(1.75rem, 3.5vw, 2.5rem);
  right: clamp(1.75rem, 3.5vw, 2.5rem);
  z-index: 9;
  --chat-panel-w: clamp(320px, 40vw, 640px);
  width: var(--chat-panel-w);
  max-width: calc(100vw - 2.5rem);
  /* Stay compact (~5:6, never a tall skinny column) AND clear of the centre:
     the panel's bottom is held above the vertical band where the 'idirnet'
     wordmark + tagline sit, so an open panel never covers them. The smaller of
     the two bounds wins — the aspect bound caps tall viewports, the centre
     bound caps everything else. */
  max-height: min(
    calc(var(--chat-panel-w) * 1.2),
    calc(50vh - clamp(1.75rem, 3.5vw, 2.5rem) - 7rem)
  );
  border-radius: 14px;
  background: transparent;
  backdrop-filter: blur(28px) url(#linkGrain);
  -webkit-backdrop-filter: blur(28px);
  border: none;
  box-shadow: none;
  overflow: hidden;

  /* Grow-from-icon animation: the panel scales out of its top-right corner.
     Closed = scale 0.85 + opacity 0 + no pointer events, so it never covers
     the canvas. .open lifts it in. */
  transform-origin: top right;
  transform: scale(0.85);
  opacity: 0;
  pointer-events: none;
  display: flex;
  flex-direction: column;
  transition: transform 0.28s cubic-bezier(0.22, 0.61, 0.36, 1),
              opacity 0.22s ease;
}

.chat-panel.open {
  transform: scale(1);
  opacity: 1;
  pointer-events: auto;
}

.chat-header {
  /* Top padding lifts the 'link' title onto the same horizontal line as the
     logo's centre (the 80px mark box centres ~40px below the panel's top edge),
     so the wordmark + the ring read as one unit. Right padding clears the logo.
     No bottom border here — the panel keeps a single hairline above the input,
     so open + empty never shows a cramped double line. */
  padding: 32px 96px 30px 22px;
  font-family: 'Inter', system-ui, sans-serif;
  display: flex;
  justify-content: flex-start;
  align-items: flex-start;
}

/* <details> accordion for the link description. */
.chat-details {
  min-width: 0;
  width: 100%;
}

.chat-summary {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  cursor: pointer;
  list-style: none;
  user-select: none;
}

.chat-summary::-webkit-details-marker { display: none; }
.chat-summary::marker { content: ''; }

/* Panel-header title — text wordmark 'link' in link-blue, JetBrains Mono. */
.chat-title {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-weight: 600;
  font-size: 1.15rem;
  color: var(--link-blue);
  letter-spacing: -0.01em;
  line-height: 1.1;
}

/* Plus → minus glyph rotates 45° when the accordion opens (CSS-only). */
.chat-chevron {
  font-family: 'Inter', system-ui, sans-serif;
  font-weight: 300;
  font-size: 1rem;
  color: var(--fg);
  opacity: 0.45;
  line-height: 1;
  transition: transform 0.2s ease, opacity 0.2s ease;
}

.chat-details[open] .chat-chevron {
  transform: rotate(45deg);
  opacity: 0.75;
}

/* Subtitle: JetBrains Mono, smaller + dimmer than the input question. */
.chat-subtitle {
  font-family: 'JetBrains Mono', ui-monospace, monospace;
  font-weight: 400;
  font-size: 0.80rem;
  color: var(--fg);
  opacity: 0.62;
  line-height: 1.55;
  letter-spacing: 0;
  margin: 0.7rem 0 0 0;
}

/* Accordion reveal: subtitle eases down into place. Ends at 0.62 opacity. */
.chat-details[open] .chat-subtitle {
  animation: chatAccordionReveal 0.32s cubic-bezier(0.22, 0.61, 0.36, 1) both;
}

@keyframes chatAccordionReveal {
  from { opacity: 0; transform: translateY(-6px); }
  to { opacity: 0.62; transform: none; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-details[open] .chat-subtitle {
    animation: none;
  }
}

/* ── Messages ────────────────────────────────────────────────────────────── */
.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px;
  font-size: 0.85rem;
  line-height: 1.6;
  color: var(--fg);
}

.chat-msg {
  margin-bottom: 12px;
  white-space: pre-wrap;
}

.chat-msg-bot {
  color: var(--fg);
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.chat-bot-paragraph {
  width: 100%;
}

.chat-msg-user {
  color: var(--fg);
  text-align: right;
  font-weight: 500;
}

/* System line — Inter, dim. Defined in case it's reintroduced. */
.chat-msg-system {
  color: var(--fg);
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.78rem;
  opacity: 0.7;
}

/* Inline chips inside bot answers — visual differentiation only (single-font
   discipline: all Inter, differentiated by weight/colour/background). */
.chat-chip-mention {
  font-weight: 500;
  color: var(--link-blue);
}

.chat-chip-email {
  font-weight: 500;
  color: var(--link);
}

.chat-chip-code {
  font-weight: 400;
  background: rgba(255, 255, 255, 0.08);
  border-radius: 4px;
  padding: 0 0.3em;
}

/* ── Input row ───────────────────────────────────────────────────────────── */
.chat-form {
  display: flex;
  align-items: center;
  border-top: 1px solid rgba(255, 255, 255, 0.06);
}

.chat-input-wrap {
  flex: 1;
  position: relative;
  min-width: 0;
  display: flex;
}

.chat-input {
  flex: 1;
  min-width: 0;
  padding: 14px 18px;
  background: transparent;
  border: none;
  color: var(--fg);
  font-family: 'Inter', system-ui, sans-serif;
  /* 16px floor: below it iOS Safari force-zooms on focus. The animated placeholder
     overlay below is matched to this so typed text and placeholder stay aligned. */
  font-size: 16px;
  outline: none;
}

.chat-input::placeholder {
  color: transparent;
}

/* Animated placeholder overlay. Visible when input is empty AND not focused.
   Text + a blinking caret. pointer-events:none so clicks pass to the input.
   chat.js toggles .chat-placeholder-hidden to mirror the React showPlaceholder
   logic (input non-empty OR focused → hide). */
.chat-placeholder-overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  padding: 14px 18px;
  pointer-events: none;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 16px; /* match .chat-input (16px iOS-zoom floor) */
  color: #9a9a9f;
  white-space: nowrap;
  overflow: hidden;
}

.chat-placeholder-overlay.chat-placeholder-hidden {
  display: none;
}

.chat-placeholder-text {
  display: inline-block;
}

.chat-caret {
  display: inline-block;
  width: 1px;
  height: 1.05em;
  background: var(--fg);
  margin-left: 6px;
  vertical-align: text-bottom;
  animation: chatCaretBlink 1.05s steps(2, jump-none) infinite;
}

@keyframes chatCaretBlink {
  50% { opacity: 0; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-caret {
    animation: none;
  }
}

/* Send arrow: green at rest (link colour), orange once the input has content. */
.chat-send {
  padding: 12px 16px;
  background: none;
  border: none;
  border-left: 1px solid rgba(255, 255, 255, 0.06);
  color: var(--link);
  cursor: pointer;
  font-family: 'Inter', system-ui, sans-serif;
  font-size: 0.95rem;
  line-height: 1;
  transition: color 0.2s ease;
}

.chat-send:hover {
  color: var(--fg);
}

.chat-send.chat-send-ready {
  color: var(--link-orange);
}

.chat-send.chat-send-ready:hover {
  color: var(--fg);
}
