/* ---------------------------------------------------------------------------
   shadcn/ui design tokens (zinc, light + dark) and the handful of components
   this page needs. No framework, no build step, no CDN: the tokens are the
   exact HSL variables shadcn ships, so a future React/Tailwind port maps 1:1.
   --------------------------------------------------------------------------- */

:root {
  --background: 0 0% 100%;
  --foreground: 240 10% 3.9%;
  --card: 0 0% 100%;
  --card-foreground: 240 10% 3.9%;
  --popover: 0 0% 100%;
  --popover-foreground: 240 10% 3.9%;
  --primary: 240 5.9% 10%;
  --primary-foreground: 0 0% 98%;
  --secondary: 240 4.8% 95.9%;
  --secondary-foreground: 240 5.9% 10%;
  --muted: 240 4.8% 95.9%;
  --muted-foreground: 240 3.8% 46.1%;
  --accent: 240 4.8% 95.9%;
  --accent-foreground: 240 5.9% 10%;
  --destructive: 0 84.2% 60.2%;
  --destructive-foreground: 0 0% 98%;
  --border: 240 5.9% 90%;
  --input: 240 5.9% 90%;
  --ring: 240 5.9% 10%;
  --radius: 0.75rem;

  --glow: 240 60% 90%;
  --ok: 142 71% 45%;
}

.dark {
  --background: 240 10% 3.9%;
  --foreground: 0 0% 98%;
  --card: 240 10% 5.6%;
  --card-foreground: 0 0% 98%;
  --popover: 240 10% 5.6%;
  --popover-foreground: 0 0% 98%;
  --primary: 0 0% 98%;
  --primary-foreground: 240 5.9% 10%;
  --secondary: 240 3.7% 15.9%;
  --secondary-foreground: 0 0% 98%;
  --muted: 240 3.7% 15.9%;
  --muted-foreground: 240 5% 64.9%;
  --accent: 240 3.7% 15.9%;
  --accent-foreground: 0 0% 98%;
  --destructive: 0 72.2% 50.6%;
  --destructive-foreground: 0 0% 98%;
  --border: 240 3.7% 15.9%;
  --input: 240 3.7% 15.9%;
  --ring: 240 4.9% 83.9%;

  --glow: 245 60% 32%;
  --ok: 142 69% 58%;
}

*,
*::before,
*::after {
  box-sizing: border-box;
  border-color: hsl(var(--border));
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background-color: hsl(var(--background));
  color: hsl(var(--foreground));
  font-family:
    ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "PingFang SC",
    "Hiragino Sans GB", "Microsoft YaHei", "Noto Sans SC", sans-serif;
  font-size: 15px;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(60% 45% at 50% 0%, hsl(var(--glow) / 0.35), transparent 70%),
    radial-gradient(40% 35% at 85% 15%, hsl(var(--glow) / 0.18), transparent 70%);
  z-index: 0;
}

[hidden] {
  display: none !important;
}

::selection {
  background: hsl(var(--primary) / 0.2);
}

/* ---------------------------------------------------------------- layout -- */

.shell {
  position: relative;
  z-index: 1;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 0 20px;
}

.stack {
  width: 100%;
  max-width: 760px;
  display: flex;
  flex-direction: column;
}

/* The conversation owns the whole window: the header, thread and composer all
   share one full-bleed column, so nothing is inset by a percentage gutter. */
.topbar,
.alert,
.thread,
.composer,
.footer-note {
  width: 100%;
  max-width: none;
}

/* ------------------------------------------------------------------ hero -- */

.hero {
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 18px;
  padding: 64px 0 40px;
  transition: padding 420ms cubic-bezier(0.22, 1, 0.36, 1);
}

.avatar {
  position: relative;
  width: 232px;
  height: 232px;
  border-radius: 9999px;
  padding: 4px;
  background: linear-gradient(140deg, hsl(var(--foreground) / 0.9), hsl(var(--muted-foreground) / 0.35));
  box-shadow:
    0 0 0 1px hsl(var(--border)),
    0 28px 70px -24px hsl(var(--foreground) / 0.45);
  transition:
    width 420ms cubic-bezier(0.22, 1, 0.36, 1),
    height 420ms cubic-bezier(0.22, 1, 0.36, 1),
    box-shadow 420ms ease;
}

.avatar img {
  width: 100%;
  height: 100%;
  border-radius: 9999px;
  object-fit: cover;
  display: block;
  background: hsl(var(--secondary));
}

.avatar__status {
  position: absolute;
  right: 16px;
  bottom: 16px;
  width: 22px;
  height: 22px;
  border-radius: 9999px;
  background: hsl(var(--ok));
  border: 3px solid hsl(var(--background));
  transition: all 420ms ease;
}

.hero__title {
  margin: 0;
  font-size: 2.25rem;
  font-weight: 650;
  letter-spacing: -0.03em;
  line-height: 1.15;
}

.hero__subtitle {
  margin: 0;
  max-width: 34rem;
  color: hsl(var(--muted-foreground));
  font-size: 1rem;
  transition:
    opacity 300ms ease,
    max-height 420ms ease,
    margin 420ms ease;
}

/* Empty state: centre the hero in the space above the composer. */
body:not(.is-chatting) .hero {
  flex: 1 1 auto;
  justify-content: center;
}

/* Compact state once the conversation starts. */
body.is-chatting .hero {
  padding: 28px 0 20px;
  gap: 12px;
}

body.is-chatting .avatar {
  width: 84px;
  height: 84px;
  box-shadow: 0 0 0 1px hsl(var(--border));
}

body.is-chatting .avatar__status {
  right: 4px;
  bottom: 4px;
  width: 16px;
  height: 16px;
  border-width: 2px;
}

body.is-chatting .hero__title {
  font-size: 1.25rem;
}

body.is-chatting .hero__subtitle {
  opacity: 0;
  max-height: 0;
  overflow: hidden;
  margin-top: -12px;
}

/* ---------------------------------------------------------------- thread -- */

.thread {
  display: flex;
  flex-direction: column;
  flex: 1 1 auto;
  gap: 20px;
  padding-bottom: 20px;
}

.turn {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  animation: rise 320ms cubic-bezier(0.22, 1, 0.36, 1);
}

.turn--user {
  flex-direction: row-reverse;
}

.turn__avatar {
  flex: 0 0 auto;
  width: 34px;
  height: 34px;
  border-radius: 9999px;
  overflow: hidden;
  border: 1px solid hsl(var(--border));
  background: hsl(var(--secondary));
}

.turn__avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  display: block;
}

.bubble {
  position: relative;
  max-width: min(88%, 960px);
  padding: 12px 16px;
  border-radius: var(--radius);
  border: 1px solid hsl(var(--border));
  background: hsl(var(--card));
  color: hsl(var(--card-foreground));
  overflow-wrap: anywhere;
  word-break: break-word;
  white-space: pre-wrap;
  box-shadow: 0 1px 2px hsl(var(--foreground) / 0.04);
}

.turn--user .bubble {
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
  border-color: hsl(var(--primary));
  white-space: pre-wrap;
}

.turn--error .bubble {
  border-color: hsl(var(--destructive) / 0.6);
  background: hsl(var(--destructive) / 0.1);
  color: hsl(var(--foreground));
}

.bubble__role {
  display: block;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: hsl(var(--muted-foreground));
  margin-bottom: 4px;
}

.turn--user .bubble__role {
  color: hsl(var(--primary-foreground) / 0.7);
}

/* Rendered markdown-lite output */
.bubble h3 {
  margin: 14px 0 6px;
  font-size: 1rem;
  letter-spacing: 0.01em;
}

.bubble h3:first-child,
.bubble p:first-child {
  margin-top: 0;
}

.bubble p {
  margin: 0 0 10px;
}

.bubble p:last-child {
  margin-bottom: 0;
}

.bubble strong {
  font-weight: 650;
}

.bubble em {
  font-style: normal;
  color: hsl(var(--muted-foreground));
}

/* Per-message markdown actions: revealed on hover, or on keyboard focus. */
.bubble__actions {
  display: flex;
  justify-content: flex-end;
  gap: 2px;
  margin-top: 8px;
  white-space: normal;
  opacity: 0;
  transition: opacity 140ms ease;
}

.turn:hover .bubble__actions,
.turn:focus-within .bubble__actions {
  opacity: 1;
}

/* Touch screens never hover, so keep the actions permanently visible there. */
@media (hover: none) {
  .bubble__actions {
    opacity: 1;
  }
}

.caret {
  display: inline-block;
  width: 0.5em;
  height: 1em;
  margin-left: 2px;
  translate: 0 2px;
  background: currentColor;
  animation: blink 1s steps(2, start) infinite;
}

.dots {
  display: inline-flex;
  gap: 4px;
  align-items: center;
  height: 1em;
}

.dots i {
  width: 6px;
  height: 6px;
  border-radius: 9999px;
  background: hsl(var(--muted-foreground));
  animation: bounce 1.1s infinite ease-in-out;
}

.dots i:nth-child(2) {
  animation-delay: 0.15s;
}

.dots i:nth-child(3) {
  animation-delay: 0.3s;
}

/* -------------------------------------------------------------- composer -- */

.composer {
  position: sticky;
  bottom: 0;
  width: 100%;
  padding: 12px 0 22px;
  background: linear-gradient(to top, hsl(var(--background)) 72%, transparent);
}

.composer__box {
  border: 1px solid hsl(var(--input));
  border-radius: calc(var(--radius) + 6px);
  background: hsl(var(--card) / 0.92);
  backdrop-filter: blur(14px);
  padding: 10px 10px 8px 14px;
  transition:
    border-color 160ms ease,
    box-shadow 160ms ease;
}

.composer__box:focus-within {
  border-color: hsl(var(--ring) / 0.7);
  box-shadow: 0 0 0 3px hsl(var(--ring) / 0.15);
}

.composer__box.is-over {
  border-color: hsl(var(--destructive));
  box-shadow: 0 0 0 3px hsl(var(--destructive) / 0.18);
}

.composer__row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}

textarea.field {
  flex: 1 1 auto;
  min-height: 30px;
  max-height: 228px;
  scrollbar-width: thin;
  resize: none;
  border: 0;
  outline: 0;
  padding: 6px 2px;
  background: transparent;
  color: inherit;
  font: inherit;
  line-height: 1.6;
}

textarea.field::placeholder {
  color: hsl(var(--muted-foreground));
}

.composer__meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 4px 2px;
  font-size: 0.75rem;
  color: hsl(var(--muted-foreground));
}

.composer__hint kbd {
  font: inherit;
  padding: 1px 5px;
  border-radius: 5px;
  border: 1px solid hsl(var(--border));
  background: hsl(var(--secondary));
  color: hsl(var(--secondary-foreground));
}

.composer__actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* --------------------------------------------------------------- counter -- */

.counter {
  position: relative;
  width: 28px;
  height: 28px;
  display: inline-grid;
  place-items: center;
  opacity: 0;
  scale: 0.85;
  transition:
    opacity 180ms ease,
    scale 180ms ease;
}

.counter.is-visible {
  opacity: 1;
  scale: 1;
}

.counter svg {
  position: absolute;
  inset: 0;
  transform: rotate(-90deg);
}

.counter circle {
  fill: none;
  stroke-width: 2.5;
}

.counter__track {
  stroke: hsl(var(--border));
}

.counter__value {
  stroke: hsl(var(--ring));
  stroke-linecap: round;
  transition:
    stroke 200ms ease,
    stroke-dashoffset 120ms linear;
}

.counter.is-warning .counter__value {
  stroke: hsl(38 92% 50%);
}

.counter.is-limit .counter__value {
  stroke: hsl(var(--destructive));
}

.counter__number {
  position: relative;
  font-size: 0.7rem;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  color: hsl(var(--muted-foreground));
}

.counter.is-warning .counter__number {
  color: hsl(38 92% 50%);
}

.counter.is-limit .counter__number {
  color: hsl(var(--destructive));
}

/* -------------------------------------------------------------- controls -- */

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  height: 38px;
  padding: 0 16px;
  border-radius: calc(var(--radius) - 2px);
  border: 1px solid transparent;
  background: hsl(var(--primary));
  color: hsl(var(--primary-foreground));
  font: inherit;
  font-size: 0.9rem;
  font-weight: 550;
  cursor: pointer;
  transition:
    opacity 150ms ease,
    background 150ms ease,
    transform 150ms ease;
}

.btn:hover:not(:disabled) {
  opacity: 0.9;
}

.btn:active:not(:disabled) {
  transform: scale(0.98);
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.btn--icon {
  width: 38px;
  padding: 0;
}

.btn--outline {
  background: transparent;
  color: hsl(var(--foreground));
  border-color: hsl(var(--border));
}

.btn--outline:hover:not(:disabled) {
  background: hsl(var(--accent));
}

.btn--ghost {
  background: transparent;
  color: hsl(var(--muted-foreground));
  border-color: transparent;
  height: 32px;
  padding: 0 10px;
  font-size: 0.8rem;
}

.btn--ghost:hover:not(:disabled) {
  background: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
}

.btn--mini {
  width: 28px;
  height: 28px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  border-color: transparent;
  color: hsl(var(--muted-foreground));
}

.btn--mini:hover:not(:disabled) {
  background: hsl(var(--accent));
  color: hsl(var(--accent-foreground));
  opacity: 1;
}

.turn--user .btn--mini {
  color: hsl(var(--primary-foreground) / 0.8);
}

.turn--user .btn--mini:hover:not(:disabled) {
  background: hsl(var(--primary-foreground) / 0.18);
  color: hsl(var(--primary-foreground));
}

.btn:focus-visible,
textarea.field:focus-visible {
  outline: 0;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  height: 24px;
  padding: 0 10px;
  border-radius: 9999px;
  border: 1px solid hsl(var(--border));
  background: hsl(var(--secondary));
  color: hsl(var(--muted-foreground));
  font-size: 0.7rem;
  letter-spacing: 0.04em;
}

.badge__dot {
  width: 6px;
  height: 6px;
  border-radius: 9999px;
  background: hsl(var(--muted-foreground));
}

.badge.is-live .badge__dot {
  background: hsl(var(--ok));
}

.topbar {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 12px;
  padding: 18px 0 0;
  font-size: 0.75rem;
  color: hsl(var(--muted-foreground));
}

.topbar__left {
  display: flex;
  align-items: center;
  gap: 8px;
}

.alert {
  width: 100%;
  margin: 12px 0 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  border: 1px solid hsl(var(--destructive) / 0.55);
  background: hsl(var(--destructive) / 0.12);
  font-size: 0.85rem;
}

.alert[hidden] {
  display: none;
}

/* The amber variant used for the sensitive-keyword banner. */
.alert--warning {
  border-color: hsl(38 92% 50% / 0.65);
  background: hsl(38 92% 50% / 0.14);
  font-weight: 600;
}

/* Transient confirmation for the copy / export actions. */
.toast {
  position: fixed;
  left: 50%;
  bottom: 104px;
  z-index: 5;
  padding: 8px 16px;
  border-radius: 9999px;
  border: 1px solid hsl(var(--border));
  background: hsl(var(--popover));
  color: hsl(var(--popover-foreground));
  font-size: 0.8rem;
  box-shadow: 0 16px 40px -20px hsl(var(--foreground) / 0.6);
  opacity: 0;
  translate: -50% 10px;
  pointer-events: none;
  transition:
    opacity 180ms ease,
    translate 180ms ease;
}

.toast.is-visible {
  opacity: 1;
  translate: -50% 0;
}

/* ------------------------------------------------ sensitive-keyword dialog -- */
/* Deliberately loud: a red, full-screen block that has to be dismissed before
   the composer can be used again. */

.dialog {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: hsl(0 0% 0% / 0.72);
  backdrop-filter: blur(6px);
  animation: dialog-in 200ms ease-out;
}

.dialog__panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  width: min(560px, 100%);
  padding: 44px 40px 36px;
  text-align: center;
  border: 2px solid hsl(var(--destructive));
  border-radius: calc(var(--radius) + 6px);
  background:
    radial-gradient(120% 90% at 50% 0%, hsl(var(--destructive) / 0.3), transparent 70%),
    hsl(var(--card));
  color: hsl(var(--card-foreground));
  box-shadow:
    0 0 0 1px hsl(var(--destructive) / 0.45),
    0 40px 90px -30px hsl(var(--destructive) / 0.75);
  animation: dialog-pop 220ms cubic-bezier(0.22, 1, 0.36, 1);
}

.dialog__logo {
  width: 96px;
  height: 96px;
  color: hsl(var(--destructive));
  animation: dialog-pulse 1.8s ease-in-out infinite;
}

.dialog__logo svg {
  width: 100%;
  height: 100%;
  display: block;
}

.dialog__title {
  margin: 0;
  font-size: 2rem;
  font-weight: 700;
  letter-spacing: 0.01em;
  color: hsl(var(--destructive));
}

.dialog__description {
  margin: 0;
  max-width: 42ch;
  font-size: 1rem;
  color: hsl(var(--muted-foreground));
  white-space: pre-wrap;
}

.dialog__close {
  min-width: 148px;
  height: 44px;
  margin-top: 6px;
  font-size: 1rem;
  background: hsl(var(--destructive));
  color: hsl(var(--destructive-foreground));
}

/* While the dialog is up the page behind it must not scroll. */
body.is-blocked {
  overflow: hidden;
}

.footer-note {
  width: 100%;
  padding: 0 0 18px;
  text-align: center;
  font-size: 0.72rem;
  color: hsl(var(--muted-foreground));
}

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

@keyframes rise {
  from {
    opacity: 0;
    transform: translateY(6px);
  }
}

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

@keyframes dialog-in {
  from {
    opacity: 0;
  }
}

@keyframes dialog-pop {
  from {
    opacity: 0;
    transform: scale(0.94);
  }
}

@keyframes dialog-pulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.06);
    opacity: 0.85;
  }
}

@keyframes bounce {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.45;
  }
  40% {
    transform: translateY(-3px);
    opacity: 1;
  }
}

@media (max-width: 640px) {
  .hero {
    padding: 40px 0 26px;
  }

  .avatar {
    width: 168px;
    height: 168px;
  }

  .hero__title {
    font-size: 1.9rem;
  }

  .bubble {
    max-width: 92%;
  }
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}