/* The chat page (SPEC.md Steps 7c, 7g and 7h): page-specific rules on top of base.css (tokens and
   shared parts). No external fonts or images: the CSP allows only this origin. */

html,
body {
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

main {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6) var(--space-4);
}

.thread {
  max-width: 48rem;
  margin: 0 auto;
}

/* ------------------------------------------------------------ empty state */

.empty-state {
  padding: var(--space-6);
  text-align: center;
}

.empty-state[hidden] {
  display: none;
}

.empty-state h2 {
  margin: 0 0 var(--space-2);
  font-size: 1.35rem;
}

.empty-state p {
  margin: 0 0 var(--space-3);
  color: var(--muted);
}

.empty-state .examples-label {
  margin-top: var(--space-4);
  font-size: 0.9rem;
}

.examples {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(16rem, 1fr));
  gap: var(--space-2);
  text-align: left;
}

.examples button {
  justify-content: flex-start;
  min-height: 3rem;
  padding: var(--space-2) var(--space-3);
  border-color: var(--border);
  border-radius: var(--radius);
  background: var(--bg);
  font-weight: 400;
  text-align: left;
}

.examples button:hover:not(:disabled) {
  border-color: var(--accent);
  background: var(--raised);
}

/* ------------------------------------------------------------ messages */

.messages {
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
  margin: 0;
  padding: 0;
  list-style: none;
}

.message {
  position: relative; /* holds its visually hidden text ("Thinking…") */
  padding: var(--space-3) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-l);
  background: var(--surface);
  box-shadow: var(--shadow-1);
  overflow-wrap: anywhere;
}

.message.user {
  align-self: flex-end;
  max-width: 85%;
  border-color: transparent;
  border-bottom-right-radius: var(--radius-s);
  background: var(--user);
  box-shadow: none;
}

.message.assistant,
.message.pending {
  border-bottom-left-radius: var(--radius-s);
}

.message.pending {
  align-self: flex-start;
  color: var(--muted);
}

.message.error {
  border-color: transparent;
  background: var(--error-bg);
  color: var(--error-text);
  box-shadow: none;
}

.message.error button {
  margin-top: var(--space-2);
}

/* Typing dots of the pending reply; still under prefers-reduced-motion (base.css). */
.typing {
  display: inline-flex;
  gap: 0.3rem;
  align-items: center;
  height: 1.5rem;
}

.typing span {
  width: 0.45rem;
  height: 0.45rem;
  border-radius: 50%;
  background: currentcolor;
  opacity: 0.4;
  animation: typing 1.2s infinite ease-in-out;
}

.typing span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing span:nth-child(3) {
  animation-delay: 0.3s;
}

@keyframes typing {
  0%,
  60%,
  100% {
    opacity: 0.4;
    transform: translateY(0);
  }

  30% {
    opacity: 1;
    transform: translateY(-0.2rem);
  }
}

/* Answer text is plain text: line breaks are kept, nothing is parsed as HTML. */
.text {
  margin: 0;
  white-space: pre-wrap;
}

.citations {
  display: flex;
  flex-wrap: wrap;
  gap: 0.35rem;
  margin: var(--space-3) 0 0;
  padding: 0;
  list-style: none;
}

/* Step 7i: a citation chip that opens the manual's PDF at the cited page (app.js). Text on
   --raised as the plain chips (a checked contrast pair), underlined on hover, the theme's focus ring. */
.citation-link {
  display: inline-flex;
  align-items: center;
  gap: 0.25rem;
  color: var(--text);
  text-decoration: none;
}

.citation-link:hover {
  text-decoration: underline;
}

.citation-link:focus-visible {
  outline: 2px solid var(--focus);
  outline-offset: 2px;
}

.citation-link .icon {
  width: 0.85em;
  height: 0.85em;
  flex: none;
}

/* Step 7h: the layout follows the width the page gets, never the device. Under 40rem one figure
   per row; at 40rem and wider two per row, and a lone figure takes the whole answer (the
   (min-width: 40rem) block below). */
.figures {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: var(--space-3);
  margin-top: var(--space-3);
}

figure {
  min-width: 0;
  margin: 0;
  padding: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg);
}

/* Never upscaled (at most the natural size), shrunk to the width and to a height cap, aspect kept. */
figure img {
  display: block;
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 70vh;
  object-fit: contain;
  margin: 0 auto var(--space-2);
  border-radius: var(--radius-s);
  background: #ffffff; /* the figures are drawn for white paper, in both themes */
}

/* A figure shown smaller than its natural size sits in this button (app.js); it opens the viewer. */
.figure-open {
  display: block;
  width: 100%;
  min-height: 0;
  padding: 0;
  border: 0;
  border-radius: var(--radius-s);
  background: transparent;
  cursor: zoom-in;
}

.figure-open:hover:not(:disabled) {
  background: transparent;
}

figcaption {
  font-size: 0.85rem;
  color: var(--muted);
}

.note {
  margin: 0.3rem 0 0;
  font-size: 0.85rem;
  color: var(--error-text);
}

details {
  margin-top: var(--space-3);
  font-size: 0.85rem;
  color: var(--muted);
}

summary {
  width: fit-content;
  cursor: pointer;
}

details dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 0.1rem var(--space-3);
  margin: var(--space-2) 0 0;
}

details dd {
  margin: 0;
}

/* Copy, Listen (Step 7k) and the rating, under each reply. */
.message-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
  margin-top: var(--space-3);
  padding-top: var(--space-2);
  border-top: 1px solid var(--border);
  font-size: 0.85rem;
}

.message-actions button {
  min-height: 2rem;
  font-size: 0.85rem;
}

/* Listen (Step 7k): while the reply plays, the button reads Stop and shows it is on. */
.message-actions .listen.playing {
  border-color: var(--accent);
}

.message-actions .listen:disabled {
  cursor: progress;
}

.feedback {
  display: flex;
  flex: 1 1 auto;
  flex-wrap: wrap;
  gap: 0.4rem;
  align-items: center;
  color: var(--muted);
}

.feedback > button {
  width: 2rem;
  padding: 0;
}

.feedback button[aria-pressed="true"] {
  border-color: var(--accent);
  color: var(--accent);
}

.feedback form {
  display: flex;
  flex-wrap: wrap;
  gap: 0.4rem;
  width: 100%;
}

.feedback textarea {
  flex: 1 1 16rem;
}

.feedback .status {
  color: var(--muted);
}

.notice {
  margin: var(--space-4) 0 0;
}

/* ------------------------------------------------------------ figure viewer (Step 7h) */

/* A modal <dialog> (showModal) almost as big as the screen; the 1rem rim is the backdrop, and a
   click there closes it. */
#figure-viewer {
  width: calc(100% - 2rem);
  max-width: none;
  height: calc(100vh - 2rem);
  height: calc(100dvh - 2rem);
  max-height: none;
  margin: auto;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-l);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow-2);
  overflow: hidden;
}

#figure-viewer[open] {
  display: flex;
  flex-direction: column;
}

#figure-viewer::backdrop {
  background: rgb(0 0 0 / 65%);
}

/* base.css gives buttons a display, which would beat the hidden attribute. */
#figure-viewer [hidden] {
  display: none;
}

.viewer-bar {
  display: flex;
  gap: var(--space-2);
  justify-content: flex-end;
  padding: var(--space-2);
  border-bottom: 1px solid var(--border);
}

/* Fit mode: the image shrinks to the body and is never upscaled. */
.viewer-body {
  display: flex;
  flex: 1;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: var(--space-2);
  overflow: hidden;
}

.viewer-body img {
  display: block;
  flex: none;
  width: auto;
  max-width: 100%;
  height: auto;
  max-height: 100%;
  object-fit: contain;
  border-radius: var(--radius-s);
  background: #ffffff; /* drawn for white paper, in both themes */
}

#figure-viewer.zoomable .viewer-body img {
  cursor: zoom-in;
}

/* Actual size: 1:1, and the body scrolls (pans) around it. */
#figure-viewer.actual .viewer-body {
  display: block;
  overflow: auto;
}

#figure-viewer.actual .viewer-body img {
  max-width: none;
  max-height: none;
  margin: 0 auto;
  cursor: zoom-out;
}

.viewer-caption {
  margin: 0;
  padding: var(--space-2) var(--space-3);
  border-top: 1px solid var(--border);
  font-size: 0.9rem;
  color: var(--muted);
}

@media (min-width: 40rem) {
  .figures {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .figures > figure:only-child {
    grid-column: 1 / -1;
  }

  figure img {
    max-height: 60vh;
  }
}

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

.composer {
  padding: var(--space-3) var(--space-4) var(--space-2);
  background: var(--bg);
}

.composer-box {
  position: relative;
  display: flex;
  gap: var(--space-2);
  align-items: flex-end;
  max-width: 48rem;
  margin: 0 auto;
  padding: var(--space-2);
  border: 1px solid var(--field-border);
  border-radius: var(--radius-l);
  background: var(--surface);
  box-shadow: var(--shadow-2);
}

.composer-box:focus-within {
  border-color: var(--focus);
  box-shadow:
    0 0 0 1px var(--focus),
    var(--shadow-2);
}

/* Grows with its text (app.js sets the height) up to about 8 lines, then scrolls. */
.composer textarea {
  flex: 1;
  min-height: 2.5rem;
  max-height: calc(8 * 1.5em + 1rem);
  padding: 0.5rem;
  border: 0;
  background: transparent;
  overflow-y: auto;
  resize: none;
}

.composer textarea:focus-visible {
  outline: none; /* the box around it shows the focus */
}

.composer #send {
  border-radius: 999px;
}

.composer-hint {
  max-width: 48rem;
  margin: var(--space-1) auto 0;
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
}

@media (max-width: 40rem) {
  main {
    padding: var(--space-4) var(--space-3);
  }

  .top {
    padding-inline: var(--space-3);
  }

  .top h1 {
    font-size: 1rem;
  }

  /* "New conversation" shows its icon only; the text stays for screen readers. */
  #new-conversation {
    position: relative;
    width: 2.25rem;
    padding: 0;
  }

  #new-conversation .label {
    position: absolute;
    width: 1px;
    height: 1px;
    overflow: hidden;
    clip: rect(0 0 0 0);
    white-space: nowrap;
  }

  #sign-out {
    padding-inline: var(--space-2);
  }

  .message.user {
    max-width: 100%;
  }

  .empty-state {
    padding: var(--space-4);
  }

  .composer {
    padding: var(--space-2);
  }

  .composer-hint {
    display: none;
  }
}
