/* The regulations chat (chat.js). Colours come from the page's variables.
   Full window height: history on the left, a wide reading column, the
   question box always on screen at the bottom. */

.chat {
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  height: 640px; /* replaced by chat.js to fill the window */
  min-height: 380px;
  border-top: 1px solid var(--line);
  background: var(--bg); /* bone, so the mark's bone ground disappears */
  position: relative;
}

/* ---------- history ---------- */

.chat-history {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 16px 12px;
  border-right: 1px solid var(--line);
  background: var(--card);
  overflow-y: auto;
  min-height: 0;
}

.chat-history h3 {
  margin: 14px 8px 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--muted);
}

.chat-history ol {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 2px;
}

.chat-history ol button {
  width: 100%;
  display: grid;
  gap: 1px;
  text-align: left;
  padding: 8px 10px;
  background: none;
  color: var(--ink);
  font-weight: 500;
  border-radius: var(--radius);
}

.chat-history ol button:hover,
.chat-history ol button[aria-current='true'] {
  background: var(--bg);
}

.chat-history ol button[aria-current='true'] {
  box-shadow: inset 3px 0 0 var(--terracotta);
}

.chat-session-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13.5px;
}

.chat-session-when {
  font-size: 11.5px;
  font-weight: 400;
  color: var(--muted);
}

.chat-history-empty {
  margin: 4px 8px;
  font-size: 13px;
  color: var(--muted);
}

.chat-new,
.chat-bar button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
}

.chat-new {
  justify-content: center;
  width: 100%;
}

.chat svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  flex: none;
}

/* Phones: history is a drawer opened from the bar above the log. */
.chat-bar {
  display: none;
}

.chat-backdrop {
  display: none;
}

/* ---------- conversation ---------- */

.chat-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: 16px 32px 24px;
  min-height: 0;
  overflow-anchor: none; /* never let the browser scroll to follow new text */
}

.chat-welcome,
.turn,
.chat-form,
.chat-disclaimer {
  width: 100%;
  max-width: 880px;
  margin-left: auto;
  margin-right: auto;
}

.chat-welcome {
  padding: 40px 0 16px;
}

.chat-welcome img {
  display: block;
  width: 96px;
  height: auto;
  margin-bottom: 22px;
}

.chat-welcome-label {
  margin: 0 0 10px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.18em;
  text-transform: uppercase;
  color: var(--terracotta);
}

.chat-starters {
  display: grid;
  gap: 8px;
}

.chat-starters button {
  text-align: left;
  padding: 12px 14px;
  background: var(--card);
  color: var(--ink);
  border: 1px solid var(--line);
  font-weight: 400;
  font-family: var(--serif);
  font-size: 16px;
  line-height: 1.45;
}

.chat-starters button:hover {
  background: var(--line-soft);
  border-color: var(--muted);
}

.turn {
  padding: 16px 0 8px;
}

.turn + .turn {
  border-top: 1px solid var(--line-soft);
}

.turn-q {
  display: inline-block;
  max-width: 100%;
  padding: 10px 14px;
  margin-bottom: 14px;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  color: var(--ink);
  font-weight: 600;
  font-size: 15px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.turn-a {
  font-family: var(--serif);
  font-size: 16.5px;
  line-height: 1.6;
  color: var(--body);
  overflow-wrap: anywhere;
}

.turn-a > :first-child {
  margin-top: 0;
}

.turn-a p,
.turn-a ul,
.turn-a ol,
.turn-a blockquote {
  margin: 0 0 12px;
}

.turn-a h3,
.turn-a h4,
.turn-a h5,
.turn-a h6 {
  font-family: Inter, sans-serif;
  color: var(--ink);
  font-size: 15px;
  margin: 18px 0 6px;
}

.turn-a strong {
  color: var(--ink);
}

.chat a {
  color: var(--terracotta-deep);
  text-underline-offset: 2px;
}

.chat a:hover {
  color: var(--ink);
}

.turn-a blockquote {
  padding: 2px 0 2px 14px;
  border-left: 3px solid var(--terracotta);
  color: var(--ink);
}

.turn-a code {
  font-size: 13px;
  background: var(--card);
  padding: 1px 4px;
  border-radius: 3px;
}

.turn-a pre {
  overflow-x: auto;
  background: var(--card);
  padding: 10px;
  border-radius: var(--radius);
}

.thinking {
  display: flex;
  gap: 5px;
  padding: 8px 0;
}

.thinking span:not(.visually-hidden) {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--terracotta);
  animation: chat-dot 1.2s ease-in-out infinite;
}

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

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

@keyframes chat-dot {
  0%, 80%, 100% { opacity: 0.25; }
  40% { opacity: 1; }
}

.turn-error {
  color: #b3261e;
  font-family: Inter, sans-serif;
  font-size: 14px;
}

.turn-actions {
  display: flex;
  gap: 2px;
  margin: 4px 0 10px -6px;
}

.turn-actions:empty {
  display: none;
}

.turn-action {
  padding: 6px;
  background: none;
  color: var(--muted);
  border-radius: var(--radius);
}

.turn-action:hover {
  background: var(--line-soft);
  color: var(--ink);
}

.turn-action[aria-pressed='true'],
.turn-action.done {
  color: var(--terracotta-deep);
}

.turn-action[aria-pressed='true'] svg {
  fill: var(--sand);
}

.turn-sources {
  margin-bottom: 8px;
  padding: 10px 12px 12px;
  border: 1px solid var(--line);
  border-radius: var(--radius);
  background: var(--card);
}

.sources-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 0 4px 8px;
  margin-bottom: 8px;
  border-bottom: 1px solid var(--line-soft);
  font-size: 13px;
  font-weight: 500;
  color: var(--ink);
}

.sources-count {
  color: var(--muted);
  white-space: nowrap;
  font-variant-numeric: tabular-nums;
}

.sources-body {
  display: flex;
  align-items: center;
  gap: 4px;
}

.sources-viewport {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  touch-action: pan-y;
}

.sources-track {
  display: flex;
  margin: 0;
  padding: 0;
  list-style: none;
  transition: transform 0.28s ease;
}

.source-card {
  flex: 0 0 100%;
  min-width: 0;
  display: flex;
  gap: 12px;
  align-items: flex-start;
  padding: 4px 8px;
}

.source-icon {
  flex: none;
  display: grid;
  place-items: center;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--muted);
}

.source-card p {
  margin: 2px 0;
  font-size: 13px;
  color: var(--body);
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.source-title {
  font-weight: 600;
  font-size: 14px;
}

.source-domain {
  font-size: 12px;
  color: var(--muted);
}

.sources-arrow {
  flex: none;
  width: 32px;
  height: 32px;
  padding: 0;
  display: grid;
  place-items: center;
  background: none;
  color: var(--ink);
  border-radius: 50%;
}

.sources-arrow:hover:not(:disabled) {
  background: var(--line-soft);
}

.sources-arrow:disabled {
  opacity: 0.3;
}

.sources-arrow[hidden] {
  display: none;
}

@media (prefers-reduced-motion: reduce) {
  .sources-track {
    transition: none;
  }
}

.chat-loading {
  color: var(--muted);
}

/* ---------- question box ---------- */

.chat-form {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin-top: 0;
  margin-bottom: 0;
  padding: 10px 10px 10px 16px;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: var(--card);
  box-shadow: 0 2px 10px rgba(27, 44, 66, 0.06);
}

.chat-form:focus-within {
  border-color: var(--ink);
}

.chat-form textarea {
  flex: 1;
  min-width: 0;
  resize: none;
  border: 0;
  outline: none;
  padding: 7px 0;
  font: inherit;
  font-size: 15.5px;
  line-height: 1.45;
  color: var(--ink);
  background: none;
  max-height: 160px;
}

.chat-form textarea:focus-visible {
  outline: none;
}

.chat-form button {
  width: 38px;
  height: 38px;
  padding: 0;
  display: grid;
  place-items: center;
  border-radius: 8px;
  flex: none;
}

.chat-form button.stop {
  background: var(--terracotta-deep);
}

.chat-disclaimer {
  margin-top: 8px;
  margin-bottom: 12px;
  padding: 0 4px;
  color: var(--muted);
  font-family: var(--serif);
  font-size: 13.5px;
  text-align: center;
}

.chat-main > .chat-form {
  width: calc(100% - 64px);
}

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

@media (prefers-reduced-motion: reduce) {
  .thinking span:not(.visually-hidden) {
    animation: none;
    opacity: 0.6;
  }
}

/* ---------- phones and narrow windows ---------- */

@media (max-width: 760px) {
  .chat {
    grid-template-columns: minmax(0, 1fr);
  }

  .chat-bar {
    display: flex;
    justify-content: space-between;
    gap: 8px;
    padding: 8px 12px;
    border-bottom: 1px solid var(--line-soft);
  }

  .chat-bar button {
    padding: 6px 10px;
    background: none;
    color: var(--ink);
    font-size: 13.5px;
  }

  .chat-bar button:hover {
    background: var(--line-soft);
  }

  .chat-history {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(300px, 86%);
    z-index: 5;
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    box-shadow: 0 8px 30px rgba(27, 44, 66, 0.18);
  }

  .chat.drawer-open .chat-history {
    transform: none;
  }

  .chat:not(.drawer-open) .chat-history {
    visibility: hidden;
  }

  .chat-backdrop:not([hidden]) {
    display: block;
    position: absolute;
    inset: 0;
    z-index: 4;
    background: rgba(27, 44, 66, 0.25);
  }

  .chat-log {
    padding: 12px 16px 16px;
  }

  .chat-main > .chat-form {
    width: calc(100% - 24px);
  }

  .turn-a {
    font-size: 16px;
  }

  .chat-welcome {
    padding-top: 16px;
  }

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