/* Chat UI for the Vitrinaa seller assistant.

   No build step and no framework: there is no Node in the runtime image, so
   the page has to be servable exactly as written by the same process that
   serves the API.

   The accent is Vitrinaa's own brand token -- teal.600 (#0d9488) in light,
   teal.300 (#2dd4bf) in dark -- taken from the seller panel's theme/tokens.ts,
   so the assistant does not look like a different product bolted on.

   Both themes are full palettes rather than one filtered from the other: a
   seller works in daylight and the panel is light by default, while this page
   is also opened at 1am over a phone, and neither should be the afterthought. */

:root {
  --bg: #0d1117;
  --bg-raised: #161b22;
  --bg-input: #1c2128;
  --border: #272e38;
  --border-strong: #3a434f;
  --text: #e6edf3;
  --text-dim: #8b949e;
  --text-faint: #6b7480;
  --accent: #2dd4bf;
  --accent-dim: #0e7c66;
  --accent-soft: rgba(45, 212, 191, .10);
  --user-bubble: #1f6feb;
  --user-text: #ffffff;
  --tool: #a78bfa;
  --tool-soft: rgba(167, 139, 250, .10);
  --warn: #f0b429;
  --warn-soft: rgba(240, 180, 41, .10);
  --danger: #f85149;
  --radius: 14px;
  --radius-sm: 9px;
  --mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f8fa;
    --bg-raised: #ffffff;
    --bg-input: #ffffff;
    --border: #d9dee4;
    --border-strong: #c2c9d1;
    --text: #1f2328;
    --text-dim: #5b6572;
    --text-faint: #848d97;
    --accent: #0d9488;
    --accent-dim: #0f766e;
    --accent-soft: rgba(13, 148, 136, .08);
    --user-bubble: #0969da;
    --tool: #6d4aff;
    --tool-soft: rgba(109, 74, 255, .07);
    --warn: #9a6700;
    --warn-soft: rgba(154, 103, 0, .08);
    --danger: #cf222e;
  }
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: Vazirmatn, "Segoe UI", system-ui, -apple-system, sans-serif;
  font-size: 15px;
  line-height: 1.75;
  -webkit-font-smoothing: antialiased;
}

.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;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 920px;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- topbar */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  position: sticky;
  top: 0;
  z-index: 5;
}

.brand { display: flex; align-items: center; gap: 11px; min-width: 0; }

.logo {
  width: 34px; height: 34px;
  display: grid; place-items: center;
  border-radius: 10px;
  background: var(--accent-dim);
  color: #fff;
  font-weight: 700;
  font-size: 17px;
  flex-shrink: 0;
}

.brand-text { min-width: 0; }
.brand-text h1 { margin: 0; font-size: 15px; font-weight: 600; line-height: 1.3; }
.brand-text p {
  margin: 0; font-size: 11.5px; color: var(--text-faint);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}

.topbar-actions { display: flex; align-items: center; gap: 6px; flex-shrink: 0; }

button {
  font-family: inherit;
  font-size: 13px;
  cursor: pointer;
  border-radius: var(--radius-sm);
  border: 1px solid transparent;
  transition: background .13s, border-color .13s, opacity .13s;
}

.ghost {
  background: transparent;
  border-color: var(--border);
  color: var(--text-dim);
  padding: 5px 11px;
}
.ghost:hover { background: var(--bg-raised); color: var(--text); }

.dot {
  width: 8px; height: 8px; border-radius: 50%;
  background: var(--text-faint);
  flex-shrink: 0;
  margin-inline-start: 4px;
}
.dot.ok { background: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.dot.bad { background: var(--danger); box-shadow: 0 0 0 3px rgba(248, 81, 73, .12); }

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

.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 22px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  scrollbar-width: thin;
  scrollbar-color: var(--border-strong) transparent;
}
.messages::-webkit-scrollbar { width: 9px; }
.messages::-webkit-scrollbar-thumb {
  background: var(--border-strong); border-radius: 5px;
  border: 2px solid var(--bg);
}

.welcome { margin: auto 0; text-align: center; padding: 20px 0 30px; }
.welcome h2 { font-size: 21px; font-weight: 600; margin: 0 0 10px; }
.welcome-note {
  color: var(--text-dim); font-size: 13.5px; margin: 0 auto 24px;
  max-width: 480px; line-height: 1.85;
}
.welcome-note strong { color: var(--warn); font-weight: 600; }

.suggestions {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(210px, 1fr));
  gap: 9px;
  text-align: start;
}
.suggestion {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  color: var(--text-dim);
  padding: 11px 14px;
  text-align: start;
  font-size: 13.5px;
  line-height: 1.6;
  border-radius: var(--radius-sm);
}
.suggestion:hover {
  border-color: var(--accent-dim);
  color: var(--text);
  background: var(--accent-soft);
}

.msg { display: flex; flex-direction: column; gap: 8px; }
.msg.user { align-items: flex-start; }

.bubble {
  padding: 11px 15px;
  border-radius: var(--radius);
  max-width: 85%;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.msg.user .bubble {
  background: var(--user-bubble);
  color: var(--user-text);
  border-end-start-radius: 4px;
}

.msg.bot .bubble {
  background: var(--bg-raised);
  border: 1px solid var(--border);
  border-end-end-radius: 4px;
  max-width: 100%;
  width: 100%;
}

.bubble strong { font-weight: 600; }
.bubble ul { margin: 8px 0; padding-inline-start: 20px; }
.bubble li { margin: 3px 0; }
.bubble code {
  font-family: var(--mono);
  font-size: .88em;
  background: var(--bg-input);
  padding: 1px 5px;
  border-radius: 4px;
  direction: ltr;
  display: inline-block;
}
.num { font-variant-numeric: tabular-nums; }

/* ------------------------------------------------------------ tool cards */

.tool {
  border: 1px solid var(--border);
  border-inline-start: 3px solid var(--tool);
  border-radius: var(--radius-sm);
  background: var(--tool-soft);
  overflow: hidden;
  font-size: 13px;
}

.tool summary {
  cursor: pointer;
  padding: 8px 13px;
  display: flex;
  align-items: center;
  gap: 9px;
  list-style: none;
  user-select: none;
}
.tool summary::-webkit-details-marker { display: none; }

.tool-name {
  font-family: var(--mono);
  font-size: 12px;
  color: var(--tool);
  direction: ltr;
  font-weight: 500;
}
.tool-state { color: var(--text-faint); font-size: 11.5px; margin-inline-start: auto; }
.tool-state.ok { color: var(--accent); }
.tool-state.bad { color: var(--danger); }

.tool pre {
  margin: 0;
  padding: 10px 13px;
  border-top: 1px solid var(--border);
  background: var(--bg);
  font-family: var(--mono);
  font-size: 11.5px;
  line-height: 1.65;
  direction: ltr;
  text-align: left;
  overflow-x: auto;
  white-space: pre-wrap;
  word-break: break-all;
  max-height: 320px;
  color: var(--text-dim);
}

/* The spinner only exists while a tool is in flight. */
.spin {
  width: 11px; height: 11px;
  border: 2px solid var(--border-strong);
  border-top-color: var(--tool);
  border-radius: 50%;
  animation: spin .7s linear infinite;
  flex-shrink: 0;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* ------------------------------------------------------- proposed action */

.proposal {
  border: 1px solid var(--warn);
  background: var(--warn-soft);
  border-radius: var(--radius-sm);
  padding: 12px 15px;
  font-size: 13.5px;
}
.proposal h4 {
  margin: 0 0 7px;
  font-size: 13px;
  color: var(--warn);
  display: flex;
  align-items: center;
  gap: 7px;
}
.proposal pre {
  margin: 8px 0 0;
  font-family: var(--mono);
  font-size: 11.5px;
  direction: ltr;
  text-align: left;
  overflow-x: auto;
  background: var(--bg);
  padding: 9px 11px;
  border-radius: 6px;
  max-height: 260px;
  white-space: pre-wrap;
  word-break: break-all;
}

.meta {
  font-size: 11.5px;
  color: var(--text-faint);
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  font-variant-numeric: tabular-nums;
}

.error-box {
  border: 1px solid var(--danger);
  background: rgba(248, 81, 73, .08);
  color: var(--danger);
  border-radius: var(--radius-sm);
  padding: 10px 14px;
  font-size: 13.5px;
}

.typing { display: inline-block; width: 7px; height: 15px; background: var(--accent); vertical-align: -2px; animation: blink 1s steps(2) infinite; }
@keyframes blink { 50% { opacity: 0; } }

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

.composer-wrap {
  padding: 10px 18px 14px;
  border-top: 1px solid var(--border);
  background: var(--bg);
}

.banner {
  margin-bottom: 9px;
  padding: 8px 13px;
  border-radius: var(--radius-sm);
  font-size: 12.5px;
  background: var(--warn-soft);
  border: 1px solid var(--warn);
  color: var(--warn);
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 7px 8px 7px 7px;
}
.composer:focus-within { border-color: var(--accent-dim); }

.composer textarea {
  flex: 1;
  background: transparent;
  border: none;
  outline: none;
  resize: none;
  color: var(--text);
  font-family: inherit;
  font-size: 14.5px;
  line-height: 1.7;
  padding: 4px 7px;
  max-height: 170px;
}
.composer textarea::placeholder { color: var(--text-faint); }

#send {
  width: 36px; height: 36px;
  display: grid; place-items: center;
  background: var(--accent-dim);
  color: #fff;
  border-radius: 10px;
  flex-shrink: 0;
}
#send:disabled { opacity: .4; cursor: not-allowed; }
#send svg { transform: scaleX(-1); }

.stop {
  background: transparent;
  border: 1px solid var(--danger);
  color: var(--danger);
  padding: 7px 13px;
  flex-shrink: 0;
}

.hint {
  margin: 7px 2px 0;
  font-size: 11px;
  color: var(--text-faint);
  text-align: center;
}

/* ---------------------------------------------------------------- dialog */

dialog {
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  background: var(--bg-raised);
  color: var(--text);
  padding: 0;
  max-width: 540px;
  width: calc(100% - 32px);
}
dialog::backdrop { background: rgba(0, 0, 0, .6); }

.dialog-body { padding: 20px 22px; }
.dialog-body h3 { margin: 0 0 8px; font-size: 16px; font-weight: 600; }
.dialog-body p { margin: 0 0 14px; font-size: 13px; color: var(--text-dim); line-height: 1.8; }

.dialog-body input {
  width: 100%;
  background: var(--bg-input);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  color: var(--text);
  font-family: var(--mono);
  font-size: 13px;
  padding: 9px 12px;
  direction: ltr;
  outline: none;
}
.dialog-body input:focus { border-color: var(--accent-dim); }

.dialog-actions { display: flex; gap: 8px; justify-content: flex-start; margin-top: 16px; }
#key-save, #tools-close.primary {
  background: var(--accent-dim); color: #fff; padding: 7px 18px;
}

.tools-list { max-height: 420px; overflow-y: auto; font-size: 13px; }
.tool-row {
  padding: 8px 0;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 10px;
  align-items: baseline;
}
.tool-row:last-child { border-bottom: none; }
.tool-row code {
  font-family: var(--mono); font-size: 11.5px; color: var(--accent);
  direction: ltr; flex-shrink: 0;
}
.tool-row span { color: var(--text-dim); font-size: 12.5px; }
.tool-row.write code { color: var(--warn); }

@media (max-width: 600px) {
  .topbar { padding: 10px 13px; }
  .messages { padding: 16px 13px 6px; }
  .composer-wrap { padding: 8px 13px 12px; }
  .brand-text h1 { font-size: 14px; }
  #btn-tools, #btn-clear { display: none; }
  .bubble { max-width: 92%; }
}


/* --- Proposals ------------------------------------------------------------ */
/* A write action renders as a visually distinct, not-committed card. The whole
   safety story of this agent is that it proposes rather than acts, and a
   proposal that looks like a completed action in the UI throws that away. */

.proposal .exec {
  margin-top: 10px;
  padding: 8px 10px;
  border: 1px dashed var(--border-strong);
  border-radius: var(--radius-sm);
  font-family: var(--mono);
  font-size: 12px;
  color: var(--text-dim);
  direction: ltr;
  text-align: left;
  overflow-x: auto;
}

.proposal .warn-list { color: var(--warn); }

/* --- Store / token setup -------------------------------------------------- */

.dialog-body .field {
  display: flex;
  flex-direction: column;
  gap: 5px;
  margin-bottom: 12px;
}

.dialog-body .field label {
  font-size: 12px;
  color: var(--text-dim);
}

.dialog-body .field input {
  padding: 9px 11px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  font: inherit;
}

.dialog-body .hint-line {
  font-size: 12px;
  color: var(--text-faint);
  line-height: 1.7;
}

/* --- Trace --------------------------------------------------------------- */
/* Collapsed by default: useful when an answer looks wrong, noise otherwise. */

.trace {
  margin-top: 6px;
  font-size: 12px;
  color: var(--text-faint);
}

.trace summary { cursor: pointer; }

.trace pre {
  margin: 6px 0 0;
  padding: 8px 10px;
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  direction: ltr;
  text-align: left;
  overflow-x: auto;
  font-family: var(--mono);
}

/* --- Tool dialog groups --- */

.tool-group {
  margin: 12px 0 4px;
  font-size: 12px;
  color: var(--accent);
  letter-spacing: .02em;
}

.tool-group:first-child { margin-top: 0; }

/* --- Sign-in gate --------------------------------------------------------- */
/* A full-cover overlay rather than a route: the chat behind it is useless
   without a session, and a half-usable page invites a seller to type a
   question and lose it to a redirect. */

.gate {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 20px;
  background: var(--bg);
}

/* A class selector beats the user-agent's `[hidden] { display: none }`, so
   `display: grid` above would keep the gate on screen after a successful
   sign-in -- visibly, behind the store picker. Every element in this file that
   is toggled with `hidden` needs its own rule saying so. */
.gate[hidden] { display: none; }

.gate-card {
  width: 100%;
  max-width: 380px;
  padding: 28px 24px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--bg-raised);
}

.gate-brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 18px;
}

.gate-brand h2 {
  margin: 0;
  font-size: 17px;
  font-weight: 600;
}

.gate-note {
  margin: 0 0 16px;
  font-size: 13px;
  line-height: 1.9;
  color: var(--text-dim);
}

.gate-form .field { margin-bottom: 14px; }

.gate-form input {
  width: 100%;
  padding: 11px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  font: inherit;
  font-size: 15px;
}

.gate-form input:focus {
  outline: none;
  border-color: var(--accent);
}

/* The code is six digits read off a notification and typed once. Wide
   tracking makes a mistyped digit visible before the seller submits. */
.code-input {
  letter-spacing: .5em;
  text-align: center;
  font-family: var(--mono);
}

.gate-primary {
  width: 100%;
  padding: 11px 14px;
  border: 0;
  border-radius: var(--radius-sm);
  background: var(--accent);
  color: #04221d;
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}

.gate-primary:disabled { opacity: .55; cursor: default; }

.gate-actions {
  display: flex;
  justify-content: space-between;
  margin-top: 12px;
}

.link {
  border: 0;
  background: none;
  color: var(--accent);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  padding: 0;
}

.link:disabled { color: var(--text-faint); cursor: default; }

.gate-error {
  margin-bottom: 12px;
  padding: 9px 11px;
  border: 1px solid var(--danger);
  border-radius: var(--radius-sm);
  color: var(--danger);
  font-size: 13px;
  line-height: 1.8;
}

.gate-foot {
  margin: 18px 0 0;
  font-size: 12px;
  color: var(--text-faint);
  text-align: center;
}

/* --- Store chip and picker ------------------------------------------------ */

.store-chip {
  max-width: 180px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.store-list { max-height: 300px; overflow-y: auto; }

.store-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  width: 100%;
  padding: 10px 12px;
  margin-bottom: 6px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background: var(--bg-input);
  color: var(--text);
  font: inherit;
  text-align: right;
  cursor: pointer;
}

.store-row:hover { border-color: var(--accent); }

.store-row.current {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.store-row .role {
  font-size: 12px;
  color: var(--text-faint);
}


/* --- Version badge -------------------------------------------------------- */
/* Small and always present. "Which version am I looking at?" is the first
   question when a deploy is supposed to have changed something, and an answer
   that needs a terminal is not an answer for most of the people asking. The
   commit and build time are in the tooltip so the badge stays unobtrusive. */

.version-badge {
  font-family: var(--mono);
  font-size: 11px;
  color: var(--text-faint);
  padding: 2px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  white-space: nowrap;
  cursor: default;
}

/* A build that did not come from the pipeline should not look like a release. */
.version-badge.dev {
  color: var(--warn);
  border-color: var(--warn);
}

/* A tab left open across a deploy is running old JavaScript against a new
   backend. Amber, like the dev badge, because both mean "what you are looking
   at is not what is deployed". */
.version-badge.stale {
  color: var(--warn);
  border-color: var(--warn);
  cursor: pointer;
}
