/* Chat page for shop-support-agent. No external fonts or images: the CSP allows only
   this file and app.js. Colours are variables so light and dark schemes share one sheet. */

:root {
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #eef1f5;
  --text: #1b1f24;
  --muted: #59636e;
  --border: #d5dae1;
  --accent: #1f5fbf;
  --accent-text: #ffffff;
  --accent-hover: #194f9f; /* darker, so text contrast only goes up */
  --ease-out: cubic-bezier(0.23, 1, 0.32, 1); /* strong ease-out for UI feedback */
  /* better-colors: one colour, one meaning. The accent marks what is interactive, so the
     user's own (static) bubble is neutral and the pending state has its own hue. */
  --user-bg: #2e3238;
  --user-text: #ffffff;
  --banner-bg: #fff4d6;
  --banner-text: #5c4200;
  --status-bg: #fff4d6; /* waits and errors under the composer */
  --status-text: #5c4200;
  --pending-bg: #5b3fb5;
  --pending-text: #ffffff;
  --focus: #b35c00;
  --low: #1d7a3a;
  --medium: #8a5a00;
  --high: #b3261e;
  --badge-text: #ffffff;
  --radius: 12px;

  /* Type scale (better-typography: a small named scale, used everywhere). */
  --text-xs: 0.75rem; /* badges */
  --text-sm: 0.875rem; /* hints, meta, captions */
  --text-md: 1rem; /* body, controls */
  --text-lg: 1.125rem; /* section headings */
  --text-xl: 1.625rem; /* page title */
  color-scheme: light;
}

/* Dark: deep navy with electric cyan for what is interactive or selected. The variables
   below the palette exist only in this scheme; the light scheme does not use them. */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #050d18;
    --surface: #0a1626; /* chat and trace panels */
    --surface-2: #13273f; /* assistant bubble, tool cards */
    --text: #e8f4ff;
    --muted: #8fb4d6;
    --border: #1c3a57; /* quiet dividers inside panels */
    --control-border: #3478a6; /* edges of controls: 3:1 on every surface */
    --accent: #29b6ff; /* dark text on it: white would fail contrast */
    --accent-text: #041018;
    --accent-hover: #5fcbff;
    --user-bg: #25364d;
    --user-text: #ffffff;
    --link: #4cc2ff;
    --role: #8fd4ff; /* "Assistant" labels and trace fact names */
    --tagline: #b9dcf5;
    --field-bg: #07111f;
    --placeholder: #7fa3c4;
    --glow: rgba(0, 217, 255, 0.35); /* soft outer glow, never over text */
    --border-glow: rgba(0, 217, 255, 0.55); /* panel outline */
    --banner-bg: #2a2208;
    --banner-text: #ffe3a1;
    --status-bg: #3a2f10;
    --status-text: #ffe3a1;
    --pending-bg: #b9a6ff;
    --pending-text: #160f33;
    --focus: #ffb454;
    --low: #3fb867;
    --medium: #e0a526;
    --high: #ff6b61;
    --badge-text: #0b1220;
    color-scheme: dark;
  }
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  background: var(--bg);
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 16px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  overflow-x: hidden;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a {
  color: var(--accent);
}

:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

code {
  font-family: ui-monospace, "Cascadia Mono", Consolas, monospace;
  font-size: 0.9em;
  overflow-wrap: anywhere;
}

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

.banner {
  margin: 0;
  padding: 10px 16px;
  background: var(--banner-bg);
  color: var(--banner-text);
  font-weight: 600;
  text-align: center;
}

.site-header,
.layout,
.site-footer {
  width: 100%;
  max-width: 1200px;
  margin: 0 auto;
  padding-inline: 16px;
}

.site-header {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 24px;
  align-items: flex-end;
  justify-content: space-between;
  padding-block: 20px 12px;
}

.site-header h1 {
  margin: 0;
  font-size: var(--text-xl);
  line-height: 1.15;
  letter-spacing: -0.01em;
  text-wrap: balance;
}

.tagline {
  margin: 6px 0 0;
  max-width: 60ch;
  color: var(--muted);
  text-wrap: pretty;
}

.site-links {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 16px;
}

.site-links a {
  display: inline-flex;
  align-items: center;
  min-height: 44px;
}

.layout {
  display: grid;
  gap: 16px;
  grid-template-columns: minmax(0, 1fr);
  padding-block: 8px 24px;
}

@media (min-width: 900px) {
  .layout {
    grid-template-columns: minmax(0, 1fr) minmax(320px, 400px);
    align-items: start;
  }

  .trace {
    position: sticky;
    top: 16px;
  }
}

.chat,
.trace__details {
  min-width: 0;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

/* better-layout: the gap between groups (messages, composer, samples) is at least
   twice the gap inside a group, so the three groups read as three. */
.chat {
  display: flex;
  flex-direction: column;
  gap: 24px;
  padding: 16px;
}

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

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
  min-height: 120px;
  max-height: 60vh;
  overflow-y: auto;
}

/* On a phone a scroll area inside the scrolling page hides the oldest messages
   behind a second scroll edge; let the page be the only scroller. */
@media (max-width: 899px) {
  .messages {
    max-height: none;
    overflow-y: visible;
  }
}

.msg {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: var(--radius);
  overflow-wrap: anywhere;
}

.msg p {
  margin: 0 0 6px;
}

.msg p:last-child,
.msg ul:last-child,
.msg ol:last-child {
  margin-bottom: 0;
}

.msg ul,
.msg ol {
  margin: 0 0 6px;
  padding-left: 1.25em;
}

.msg--user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-text);
}

.msg__text {
  white-space: pre-wrap; /* keep the line breaks the user typed */
}

/* Who is speaking, in words: a small label above the text. */
.msg__role {
  display: block;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: var(--text-xs);
  font-weight: 700;
  letter-spacing: 0.02em;
}

.msg--user .msg__role {
  color: var(--user-text);
}

/* A message the server never received (rate limit, outage): its text goes back to the box. */
.msg--user.is-unsent {
  outline: 2px dashed var(--user-bg);
  outline-offset: 2px;
}

/* Said in words, not only by the dashed outline (better-accessibility: no meaning
   carried by style alone). Full opacity keeps the text at full contrast. */
.msg__unsent {
  display: block;
  margin-top: 4px;
  font-size: var(--text-sm);
  font-weight: 600;
}

.msg--assistant {
  align-self: flex-start;
  background: var(--surface-2);
  /* A reply eases in instead of popping. Only the reply moves: the user's own
     message comes from a key press and appears at once. */
  transition:
    opacity 200ms var(--ease-out),
    transform 200ms var(--ease-out);
}

@starting-style {
  .msg--assistant {
    opacity: 0;
    transform: translateY(4px);
  }
}

.msg--assistant.is-clickable {
  cursor: pointer;
  border: 2px solid transparent;
  transition:
    opacity 200ms var(--ease-out),
    transform 200ms var(--ease-out),
    border-color 160ms var(--ease-out);
}

/* A reply that opens its trace shows it can be picked before it is picked. */
.msg--assistant.is-clickable:focus-visible {
  border-color: var(--border);
}

@media (hover: hover) and (pointer: fine) {
  .msg--assistant.is-clickable:hover {
    border-color: var(--border);
  }
}

.msg--assistant.is-selected {
  border-color: var(--accent);
}

.msg--notice {
  align-self: center;
  max-width: 100%;
  background: transparent;
  border: 1px dashed var(--border);
  color: var(--muted);
  text-align: center;
}

.msg__badges {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

.typing {
  display: flex;
  gap: 4px;
  margin: 0;
  padding: 4px 2px;
}

.typing[hidden] {
  display: none;
}

.typing__dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  animation: pulse 1.2s infinite ease-in-out;
}

.typing__dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing__dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes pulse {
  0%, 80%, 100% { opacity: 0.3; }
  40% { opacity: 1; }
}

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

.status {
  font-variant-numeric: tabular-nums; /* the countdown changes every second */
  margin: 0;
  padding: 10px 14px;
  border-radius: var(--radius);
  background: var(--status-bg);
  color: var(--status-text);
}

/* Running cost of this chat: a quiet line, numbers that do not jump as they change. */
.totals {
  margin: -12px 0 0; /* sits with the messages, not as a group of its own */
  color: var(--muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
  text-align: right;
}

.totals[hidden] {
  display: none;
}

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

.composer {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.composer__label {
  font-weight: 600;
}

.composer textarea {
  width: 100%;
  min-height: 88px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  resize: vertical;
}

.composer__meta {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  gap: 4px 12px;
  color: var(--muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.composer__meta .is-over {
  color: var(--high);
  font-weight: 600;
}

.composer__actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.button {
  min-height: 44px;
  min-width: 44px;
  padding: 10px 18px;
  border-radius: 8px;
  border: 1px solid transparent;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
  transition:
    transform 160ms var(--ease-out),
    background-color 160ms var(--ease-out),
    border-color 160ms var(--ease-out);
}

/* Press feedback: a small scale-down (never below 0.96) so the click feels received. */
.button:not(:disabled):active,
.chip:active {
  transform: scale(0.96);
}

@media (hover: hover) and (pointer: fine) {
  .button--primary:not(:disabled):hover {
    background: var(--accent-hover);
  }

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

.button--primary {
  background: var(--accent);
  color: var(--accent-text);
}

.button--secondary {
  background: transparent;
  border-color: var(--border);
  color: var(--text);
}

.button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* --- samples ---------------------------------------------------------------- */

.samples {
  padding-top: 4px;
}

.samples h2 {
  margin: 0;
  font-size: var(--text-lg);
  line-height: 1.25;
}

.hint {
  margin: 4px 0 8px;
  color: var(--muted);
  font-size: var(--text-sm);
  text-wrap: pretty;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  min-height: 44px;
  padding: 8px 14px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--text);
  font: inherit;
  cursor: pointer;
  transition:
    transform 160ms var(--ease-out),
    border-color 160ms var(--ease-out);
}

@media (hover: hover) and (pointer: fine) {
  .chip:hover {
    border-color: var(--accent);
  }
}

.sample-orders {
  margin-top: 16px;
  color: var(--muted);
  font-size: var(--text-sm);
}

.sample-orders h3 {
  margin: 0 0 4px;
  font-size: var(--text-sm);
  font-weight: 600;
}

.order-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}

/* Each sample order is a button that fills the box, like the chips above. */
.order-pick {
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text);
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition:
    transform 160ms var(--ease-out),
    border-color 160ms var(--ease-out);
}

.order-pick:active {
  transform: scale(0.98); /* a wide target moves less, or its edges travel too far */
}

@media (hover: hover) and (pointer: fine) {
  .order-pick:hover {
    border-color: var(--accent);
  }
}

/* --- trace panel ------------------------------------------------------------ */

.trace__details {
  padding: 0 16px 16px;
}

.trace__details > summary {
  display: flex;
  align-items: center;
  min-height: 44px;
  padding-top: 12px;
  cursor: pointer;
  list-style: none;
}

.trace__details > summary::-webkit-details-marker {
  display: none;
}

.trace__details > summary h2 {
  margin: 0;
  font-size: var(--text-lg);
  line-height: 1.25;
}

.trace__details > summary::after {
  content: "Show";
  margin-left: auto;
  color: var(--accent);
  font-size: var(--text-sm);
}

.trace__details[open] > summary::after {
  content: "Hide";
}

.trace__details > .hint {
  margin-bottom: 16px;
}

.trace__body {
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* A status badge sizes to its text instead of stretching across the panel. */
.trace__body > .badge {
  align-self: flex-start;
  margin: 0;
}

.trace__empty {
  margin: 0;
  color: var(--muted);
}

.trace__section h3 {
  margin: 0 0 2px;
  font-size: var(--text-md);
  line-height: 1.3;
}

.trace__section h3 + * {
  margin-top: 4px;
}

.trace__checks {
  margin: 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--surface-2);
}

/* The explanations live in one place, folded away until someone asks. */
.glossary {
  margin-top: 20px;
  border-top: 1px solid var(--border);
}

.glossary > summary {
  display: flex;
  align-items: center;
  min-height: 44px;
  color: var(--accent);
  font-weight: 600;
  cursor: pointer;
}

.glossary__list {
  margin: 0;
  font-size: var(--text-sm);
}

.glossary__list dt {
  font-weight: 600;
}

.glossary__list dd {
  margin: 0 0 8px;
  color: var(--muted);
  text-wrap: pretty;
}

.facts {
  display: grid;
  grid-template-columns: max-content minmax(0, 1fr);
  gap: 2px 12px;
  margin: 0;
}

.facts dt {
  color: var(--muted);
}

.facts dd {
  margin: 0;
  overflow-wrap: anywhere;
  font-variant-numeric: tabular-nums;
}

.step {
  margin: 0 0 8px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.step__title {
  margin: 0 0 4px;
  font-weight: 600;
  font-variant-numeric: tabular-nums;
}

/* Share of model time: a thin bar plus the same number in words. */
.step__share {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 6px;
  color: var(--muted);
  font-size: var(--text-sm);
  font-variant-numeric: tabular-nums;
}

.step__bar {
  flex: 1;
  height: 6px;
  border-radius: 3px;
  background: var(--border);
  overflow: hidden;
}

.step__fill {
  display: block;
  height: 100%;
  border-radius: 3px;
  background: var(--muted);
}

.step__share-text {
  flex: none;
  min-width: 13ch;
  text-align: right;
}

.step__summary {
  margin: 0;
  text-wrap: pretty;
}

.tool {
  margin: 6px 0 0;
  padding: 6px 8px;
  border-radius: 6px;
  background: var(--surface-2);
}

.tool__head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.tool__args {
  margin: 4px 0 0;
  padding: 0;
  list-style: none;
  font-size: var(--text-sm);
  overflow-wrap: anywhere;
}

.plain-list {
  margin: 0;
  padding-left: 1.1em;
}

.badge {
  display: inline-block;
  padding: 2px 8px;
  border-radius: 999px;
  background: var(--muted);
  color: var(--badge-text);
  font-size: var(--text-xs);
  font-weight: 700;
  line-height: 1.6;
  text-transform: uppercase;
  letter-spacing: 0.04em; /* small uppercase labels need a little air */
  white-space: nowrap;
}

/* "Risk: high", "Result: no matching order": sentence case reads better than shouting. */
.badge--label {
  text-transform: none;
  letter-spacing: 0;
}

.badge--unknown {
  background: var(--muted);
}

.badge--low {
  background: var(--low);
}

.badge--medium {
  background: var(--medium);
}

.badge--high {
  background: var(--high);
}

.badge--ok {
  background: var(--low);
}

.badge--error {
  background: var(--high);
}

.badge--guard {
  background: var(--medium);
}

.badge--pending {
  background: var(--pending-bg);
  color: var(--pending-text);
  text-transform: none;
  letter-spacing: 0;
  /* Long enough to wrap on a phone: a pill radius would turn two lines into a blob. */
  white-space: normal;
  border-radius: 8px;
  padding: 3px 10px;
}

.site-footer {
  padding-block: 0 24px;
  color: var(--muted);
  font-size: var(--text-sm);
}

/* --- dark scheme: navy and cyan ---------------------------------------------- */

/* Glow is quiet: it sits behind and around the panels, never over text. Cyan marks what
   is interactive or selected; amber, red, green and lavender keep their own meanings. */
@media (prefers-color-scheme: dark) {
  /* A soft cyan glow behind the panels and a darker vignette toward the corners. On html
     only, with body transparent, so the glow is not painted twice. */
  html {
    background:
      radial-gradient(ellipse 70% 55% at 50% 45%, rgba(0, 217, 255, 0.1), transparent 70%),
      radial-gradient(ellipse at center, transparent 55%, rgba(0, 0, 0, 0.45) 100%),
      var(--bg);
    background-attachment: fixed;
  }

  body {
    background: transparent;
  }

  a {
    color: var(--link);
  }

  .banner {
    border-bottom: 1px solid var(--border-glow);
    box-shadow: 0 1px 12px var(--glow);
  }

  .tagline {
    color: var(--tagline);
  }

  .chat,
  .trace__details {
    border-color: var(--border-glow);
    box-shadow:
      0 0 0 1px rgba(0, 217, 255, 0.08),
      0 0 24px var(--glow);
  }

  .msg__role {
    color: var(--role); /* the user's own label stays white (.msg--user .msg__role) */
  }

  .msg--assistant.is-clickable:focus-visible {
    border-color: var(--control-border);
  }

  .msg--assistant.is-selected {
    box-shadow: 0 0 14px var(--glow);
  }

  .typing__dot {
    background: var(--accent);
  }

  /* The focus ring stays the amber outline from :focus-visible; the glow only adds to it. */
  .composer textarea {
    background: var(--field-bg);
    border-color: var(--control-border);
  }

  .composer textarea::placeholder {
    color: var(--placeholder);
  }

  .composer textarea:focus {
    box-shadow: 0 0 12px var(--glow);
  }

  /* Controls keep a 3:1 edge; the quieter --border is only for dividers. */
  .button--secondary,
  .chip,
  .order-pick {
    border-color: var(--control-border);
  }

  .order-pick {
    background: var(--surface-2);
  }

  .trace__details > summary::after,
  .glossary > summary {
    color: var(--link);
  }

  .facts dt {
    color: var(--role);
  }

  /* Steps keep the panel colour and a quiet border, so the tool card inside still stands out. */
  .step__fill {
    background: linear-gradient(90deg, #1e90d6, #5fd4ff);
  }
}

@media (prefers-color-scheme: dark) and (hover: hover) and (pointer: fine) {
  .msg--assistant.is-clickable:hover {
    border-color: var(--control-border);
  }

  .button--primary:not(:disabled):hover,
  .chip:hover,
  .order-pick:hover {
    box-shadow: 0 0 10px var(--glow);
  }

  .button--secondary:not(:disabled):hover,
  .chip:hover,
  .order-pick:hover {
    border-color: var(--accent);
  }
}

/* Reduced motion turns every transition and press scale off. It sits last so it
   wins over each component's own transition rule. */
@media (prefers-reduced-motion: reduce) {
  .button,
  .chip,
  .order-pick,
  .msg--assistant,
  .msg--assistant.is-clickable {
    transition: none;
  }

  .button:not(:disabled):active,
  .chip:active,
  .order-pick:active {
    transform: none;
  }
}
