/* ============================================================
   WEBSTACK — assistant chat widget
   Uses the site's design tokens, so light/dark follow the theme.
   ============================================================ */

.chat-launch,
.chat-panel {
  --chat-gap: 16px;
}

/* ============ LAUNCHER ============ */

.chat-launch {
  position: fixed;
  z-index: 210;
  inset-block-end: calc(var(--statusbar-h) + var(--chat-gap));
  inset-inline-end: var(--chat-gap);
  inline-size: 56px;
  block-size: 56px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: linear-gradient(158deg, var(--panel-2), var(--panel));
  border: 1px solid var(--accent-line);
  color: var(--accent);
  box-shadow: var(--shadow);
  transition: transform .24s var(--ease), opacity .2s var(--ease),
              border-color .2s var(--ease), background-color .2s var(--ease);
}

.chat-launch svg { inline-size: 23px; block-size: 23px; }

.chat-launch:hover,
.chat-launch:focus-visible {
  transform: translateY(-2px) scale(1.04);
  border-color: var(--accent);
  outline: none;
}

.chat-launch.is-active {
  opacity: 0;
  transform: scale(.82);
  pointer-events: none;
}

.chat-launch-label {
  position: absolute;
  inset-inline-end: calc(100% + 10px);
  inset-block-start: 50%;
  transform: translateY(-50%) translateX(-6px);
  padding: 6px 11px;
  border-radius: 8px;
  background: var(--panel-2);
  border: 1px solid var(--line);
  color: var(--text);
  font-family: var(--font-fa);
  font-size: 11.5px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity .2s var(--ease), transform .2s var(--ease);
}

.chat-launch:hover .chat-launch-label,
.chat-launch:focus-visible .chat-launch-label {
  opacity: 1;
  transform: translateY(-50%) translateX(0);
}

/* stillness nudge: speech bubble + hop, never while scrolling or typing */
.chat-hint {
  position: absolute;
  inset-inline-end: calc(100% + 14px);
  inset-block-start: 50%;
  inline-size: max-content;
  max-inline-size: min(250px, 52vw);
  padding: 8px 12px;
  border-radius: 10px;
  background: var(--panel-2);
  border: 1px solid var(--accent-line);
  box-shadow: var(--shadow);
  color: var(--text);
  font-family: var(--font-fa);
  font-size: 12.5px;
  line-height: 1.7;
  text-align: start;
  white-space: normal;
  opacity: 0;
  pointer-events: none;
  transform: translateY(-50%) translateX(-8px) scale(.94);
  transform-origin: left center;
  transition: opacity .22s var(--ease), transform .22s var(--ease);
}

.chat-hint::after {
  content: '';
  position: absolute;
  left: -5px;
  top: 50%;
  inline-size: 9px;
  block-size: 9px;
  background: var(--panel-2);
  border-bottom: 1px solid var(--accent-line);
  border-left: 1px solid var(--accent-line);
  transform: translateY(-50%) rotate(45deg);
}

.chat-launch.show-nudge .chat-hint {
  opacity: 1;
  transform: translateY(-50%) translateX(0) scale(1);
}

.chat-launch.show-nudge .chat-launch-label { display: none; }

.chat-launch.show-nudge {
  animation: chat-hop 2.2s var(--ease) infinite;
}

@keyframes chat-hop {
  0%, 46%, 100% { transform: translateY(0) scale(1); }
  8%            { transform: translateY(-10px) scale(1.03, 1.06); }
  20%           { transform: translateY(0) scale(1.07, .93); }
  28%           { transform: translateY(-4px) scale(1, 1.02); }
  38%           { transform: translateY(0) scale(1.03, .97); }
}

/* attention ring + dot: only until the first open */
.chat-ping,
.chat-dot { display: none; }

.chat-launch.has-ping .chat-ping {
  display: block;
  position: absolute;
  inset: -1px;
  border-radius: 50%;
  border: 1px solid var(--accent);
  animation: chat-ping 2.6s var(--ease) infinite;
}

.chat-launch.has-ping .chat-dot,
.chat-launch.has-history .chat-dot {
  display: block;
  position: absolute;
  inset-block-start: 3px;
  inset-inline-start: 3px;
  inline-size: 9px;
  block-size: 9px;
  border-radius: 50%;
  background: var(--warm);
  border: 2px solid var(--panel);
}

@keyframes chat-ping {
  0%   { transform: scale(1); opacity: .55; }
  70%  { transform: scale(1.55); opacity: 0; }
  100% { transform: scale(1.55); opacity: 0; }
}

/* ============ PANEL ============ */

.chat-panel {
  position: fixed;
  z-index: 205;
  inset-block-end: calc(var(--statusbar-h) + var(--chat-gap) - var(--chat-kbd, 0px));
  inset-inline-end: var(--chat-gap);
  inline-size: min(400px, calc(100vw - (var(--chat-gap) * 2)));
  block-size: min(620px, calc(100dvh - var(--statusbar-h) - var(--titlebar-h) - (var(--chat-gap) * 2)));
  display: flex;
  flex-direction: column;
  border-radius: 16px;
  background: var(--panel);
  border: 1px solid var(--line-strong);
  box-shadow: var(--shadow);
  overflow: hidden;
  opacity: 0;
  transform: translateY(14px) scale(.97);
  transform-origin: bottom left;
  transition: opacity .22s var(--ease), transform .22s var(--ease);
}

.chat-panel.is-open { opacity: 1; transform: none; }

/* only the log shrinks; the chrome around it keeps its height */
.chat-head,
.chat-intro,
.chat-chips,
.chat-form,
.chat-foot { flex: none; }

/* ---- head ---- */

.chat-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 12px 14px;
  border-block-end: 1px solid var(--line);
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
}

.chat-avatar {
  flex: none;
  display: grid;
  place-items: center;
  inline-size: 34px;
  block-size: 34px;
  border-radius: 11px;
  background: var(--accent-dim);
  border: 1px solid var(--accent-line);
  color: var(--accent);
}

.chat-avatar svg { inline-size: 18px; block-size: 18px; }

.chat-id { display: flex; flex-direction: column; gap: 2px; min-inline-size: 0; }
.chat-id strong { font-size: 13.5px; font-weight: 700; color: var(--text-strong); }

.chat-state {
  display: flex;
  align-items: center;
  gap: 6px;
  font-family: var(--font-mono);
  font-size: 10px;
  letter-spacing: .02em;
  color: var(--muted);
}

.chat-orb {
  inline-size: 7px;
  block-size: 7px;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 var(--accent-line);
  animation: chat-orb 2.4s ease-in-out infinite;
}

@keyframes chat-orb {
  0%, 100% { box-shadow: 0 0 0 0 rgba(200, 242, 90, .35); }
  50%      { box-shadow: 0 0 0 5px rgba(200, 242, 90, 0); }
}

.chat-typing { display: inline-flex; align-items: center; gap: 3px; }

.chat-typing i {
  inline-size: 4px;
  block-size: 4px;
  border-radius: 50%;
  background: var(--accent);
  animation: chat-blink 1.05s ease-in-out infinite;
}

.chat-typing i:nth-child(2) { animation-delay: .16s; }
.chat-typing i:nth-child(3) { animation-delay: .32s; }

@keyframes chat-blink {
  0%, 60%, 100% { opacity: .25; transform: translateY(0); }
  30%           { opacity: 1; transform: translateY(-2px); }
}

.chat-tools { margin-inline-start: auto; display: flex; gap: 4px; }

.chat-act {
  display: grid;
  place-items: center;
  inline-size: 28px;
  block-size: 28px;
  border-radius: 8px;
  color: var(--muted);
  border: 1px solid transparent;
  transition: color .18s, background-color .18s, border-color .18s;
}

.chat-act svg { inline-size: 15px; block-size: 15px; }
.chat-act:hover { color: var(--text-strong); background: var(--panel-3); border-color: var(--line); }

.chat-intro {
  padding: 6px 14px;
  font-size: 10.5px;
  line-height: 1.6;
  color: var(--muted-2);
  background: var(--bg-soft);
  border-block-end: 1px solid var(--line);
}

/* ---- log ---- */

.chat-log {
  flex: 1 1 auto;
  min-block-size: 96px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 14px;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-gutter: stable;
  background:
    repeating-linear-gradient(180deg, transparent 0 27px, var(--line) 27px 28px)
    var(--bg);
  background-size: 100% 28px;
  mask-image: linear-gradient(180deg, transparent 0, #000 14px);
}

.chat-msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-inline-size: 88%;
}

/* short conversations sit at the bottom, like every other chat */
.chat-log > .chat-msg:first-child { margin-block-start: auto; }

.chat-msg--model { align-self: flex-start; }
.chat-msg--user  { align-self: flex-end; align-items: flex-end; }

.chat-msg time {
  font-family: var(--font-mono);
  font-size: 9.5px;
  color: var(--muted-2);
  padding-inline: 4px;
}

.chat-body {
  padding: 10px 13px;
  border-radius: 12px;
  font-size: 13px;
  line-height: 1.85;
  color: var(--text);
  overflow-wrap: anywhere;
  word-break: break-word;
}

.chat-msg--model .chat-body {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-start-start-radius: 3px;
}

.chat-msg--user .chat-body {
  background: linear-gradient(160deg, var(--accent), color-mix(in oklab, var(--accent) 78%, var(--accent-2)));
  color: var(--on-accent);
  border-start-end-radius: 3px;
}

.chat-body p + p { margin-block-start: .55em; }
.chat-body ul, .chat-body ol { margin-block: .35em; padding-inline-start: 1.3em; }
.chat-body li { list-style: disc; margin-block-start: .2em; }
.chat-body ol li { list-style: decimal; }
.chat-body strong { color: var(--text-strong); font-weight: 700; }
.chat-msg--user .chat-body strong { color: inherit; }
.chat-body em { color: var(--muted); font-style: normal; border-inline-start: 2px solid var(--accent-line); padding-inline-start: 8px; }
.chat-body a { color: var(--accent-2); text-decoration: underline; text-underline-offset: 3px; }

/* Digit groups injected by chat.js (phone numbers, ranges): keep them readable
   left-to-right and never break a number across two lines. */
.chat-num {
  direction: ltr;
  unicode-bidi: isolate;
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}
.chat-body code {
  font-family: var(--font-mono);
  font-size: 11.5px;
  padding: 1px 5px;
  border-radius: 5px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
}
.chat-body pre {
  margin-block: .5em;
  padding: 9px 11px;
  border-radius: 9px;
  background: var(--bg-soft);
  border: 1px solid var(--line);
  overflow-x: auto;
}
.chat-body pre code { background: none; border: 0; padding: 0; }

.chat-msg--model.is-pending .chat-body::after {
  content: '•••';
  color: var(--accent);
  letter-spacing: 2px;
  animation: chat-blink 1.05s ease-in-out infinite;
}

.chat-msg-tools {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-block-start: 2px;
}

.chat-mini {
  display: grid;
  place-items: center;
  inline-size: 22px;
  block-size: 22px;
  border-radius: 6px;
  color: var(--muted-2);
  opacity: 0;
  transition: opacity .18s, color .18s, background-color .18s;
}

.chat-mini svg { inline-size: 12px; block-size: 12px; }
.chat-msg:hover .chat-mini, .chat-mini:focus-visible { opacity: 1; }
.chat-mini:hover { color: var(--accent); background: var(--accent-dim); }
.chat-mini.is-ok { color: var(--accent); opacity: 1; }

/* ---- chips + hint ---- */

.chat-chips {
  display: flex;
  gap: 6px;
  padding: 0 14px 8px;
  overflow-x: auto;
  flex-wrap: nowrap;
  scrollbar-width: none;
  -webkit-overflow-scrolling: touch;
}

.chat-chips::-webkit-scrollbar { display: none; }

.chat-chip {
  flex: none;
  padding: 6px 11px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--line-strong);
  color: var(--muted);
  font-family: var(--font-fa);
  font-size: 11.5px;
  transition: color .18s, border-color .18s, background-color .18s, transform .18s var(--ease);
}

.chat-chip:hover {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
  transform: translateY(-1px);
}

/* ---- form ---- */

.chat-form {
  position: relative;
  padding: 11px 12px;
  border-block-start: 1px solid var(--line);
  background: var(--panel-2);
}

.chat-form textarea {
  display: block;
  inline-size: 100%;
  min-block-size: 40px;
  max-block-size: 132px;
  padding-block: 10px;
  padding-inline: 12px 52px;
  border-radius: 11px;
  background: var(--bg-soft);
  border: 1px solid var(--line-strong);
  color: var(--text);
  font-family: var(--font-fa);
  font-size: 13px;
  line-height: 1.7;
  resize: none;
  transition: border-color .18s, background-color .18s;
}

.chat-form textarea::placeholder { color: var(--muted-2); }
.chat-form textarea:focus {
  outline: none;
  border-color: var(--accent);
  background: var(--panel);
}

.chat-count {
  position: absolute;
  inset-block-start: -8px;
  inset-inline-start: 18px;
  padding: 0 5px;
  border-radius: 4px;
  background: var(--panel-2);
  font-family: var(--font-mono);
  font-size: 9.5px;
  color: var(--warm);
}

.chat-send {
  position: absolute;
  inset-block-end: 12px;
  inset-inline-end: 17px;
  display: grid;
  place-items: center;
  inline-size: 38px;
  block-size: 38px;
  border-radius: 11px;
  background: var(--accent);
  color: var(--on-accent);
  transition: transform .18s var(--ease), opacity .18s, background-color .18s;
}

.chat-send svg { inline-size: 17px; block-size: 17px; }
.chat-send:hover:not(:disabled) { transform: translateY(-1px) scale(1.04); }
.chat-send:disabled { opacity: .45; cursor: default; }

.chat-foot {
  padding: 0 14px 10px;
  background: var(--panel-2);
  font-family: var(--font-mono);
  font-size: 9.5px;
  line-height: 1.7;
  color: var(--muted-2);
  text-align: center;
}

.chat-foot a { color: var(--muted); text-decoration: underline; text-underline-offset: 2px; }

/* ============ RESPONSIVE ============ */

@media (max-width: 720px) {
  .chat-panel {
    --chat-gap: 0px;
    inset: 0;
    inline-size: 100%;
    block-size: 100%;
    border-radius: 0;
    border: 0;
    padding-block-end: env(safe-area-inset-bottom);
    transform-origin: bottom center;
  }
  .chat-launch {
    inset-inline-end: max(14px, env(safe-area-inset-right));
    inset-block-end: max(calc(var(--statusbar-h) + 12px), env(safe-area-inset-bottom));
    inline-size: 50px;
    block-size: 50px;
  }
  .chat-launch-label { display: none; }
  .chat-hint {
    inset-inline-end: 0;
    inset-block-start: auto;
    inset-block-end: calc(100% + 12px);
    max-inline-size: min(250px, 72vw);
    transform: translateY(10px) scale(.94);
    transform-origin: bottom left;
  }
  .chat-hint::after {
    left: 22px;
    top: auto;
    bottom: -5px;
    border-bottom: 1px solid var(--accent-line);
    border-left: 1px solid var(--accent-line);
    border-top: 0;
    transform: none;
  }
  .chat-launch.show-nudge .chat-hint { transform: translateY(0) scale(1); }
  .chat-msg { max-inline-size: 92%; }
  .chat-intro { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .chat-panel, .chat-launch, .chat-chip, .chat-send, .chat-hint { transition: none; }
  .chat-launch.has-ping .chat-ping,
  .chat-launch.show-nudge,
  .chat-orb,
  .chat-typing i,
  .chat-msg--model.is-pending .chat-body::after { animation: none; }
}

/* keep the panel corners clipping its own children */
.chat-panel { overflow: hidden; }
