/* Karamos web widget — light theme is the default token set; dark theme
   overrides the same tokens under prefers-color-scheme. Everything else
   reads the tokens, never a hardcoded color, so the two themes stay in
   lockstep. */

@font-face {
  font-family: "Inter";
  font-style: normal;
  font-weight: 100 900;
  font-display: swap;
  src: url("./fonts/InterVariable.woff2") format("woff2");
}

:root {
  --bg: #f7f7f8;
  --bg-elevated: #ffffff;
  --bg-sunken: #eceef2;
  --sidebar-bg: #f0f1f5;
  --text: #1f2328;
  --text-muted: #6b7280;
  --border: #e3e5ea;

  --accent: #4f46e5;
  --accent-hover: #4338ca;
  --accent-contrast: #ffffff;
  --accent-soft: rgba(79, 70, 229, 0.1);

  --bubble-user-bg: #eceef2;
  --bubble-user-text: #1f2328;

  --error-bg: #fdecec;
  --error-text: #a3241a;
  --error-border: #f3b9b3;

  --warning-bg: #fff6e0;
  --warning-text: #7a5900;
  --warning-border: #f0d789;

  --chip-bg: #eef0f3;
  --chip-text: #4b5563;

  --code-bg: #f6f6f8;
  --code-head-bg: #eceaf4;

  --focus-ring: #4f46e5;
  --shadow: 0 1px 2px rgba(16, 24, 40, 0.06), 0 1px 3px rgba(16, 24, 40, 0.08);
  --shadow-md: 0 8px 24px rgba(16, 24, 40, 0.1), 0 2px 6px rgba(16, 24, 40, 0.06);

  --font-sans:
    "Inter", -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif,
    "Apple Color Emoji", "Segoe UI Emoji";
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171c;
    --bg-elevated: #1d1f26;
    --bg-sunken: #22242c;
    --sidebar-bg: #1a1b21;
    --text: #e8e9ee;
    --text-muted: #9aa0ac;
    --border: #2c2e37;

    --accent: #8d84f5;
    --accent-hover: #a099f7;
    --accent-contrast: #14121f;
    --accent-soft: rgba(141, 132, 245, 0.16);

    --bubble-user-bg: #262832;
    --bubble-user-text: #e8e9ee;

    --error-bg: #3a1a17;
    --error-text: #ffb4a8;
    --error-border: #6b2a22;

    --warning-bg: #332a0c;
    --warning-text: #f1d778;
    --warning-border: #5b4a14;

    --chip-bg: #262832;
    --chip-text: #c2c8d2;

    --code-bg: #1b1c22;
    --code-head-bg: #24252d;

    --focus-ring: #8d84f5;
    --shadow: 0 1px 2px rgba(0, 0, 0, 0.4), 0 1px 3px rgba(0, 0, 0, 0.35);
    --shadow-md: 0 10px 28px rgba(0, 0, 0, 0.5), 0 2px 6px rgba(0, 0, 0, 0.3);
  }
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.45;
}

/* Author display rules (e.g. .login-screen { display: flex }) override the
   UA's [hidden] { display: none }, so restate it with !important — app.js
   drives all view switching through the hidden attribute. */
[hidden] {
  display: none !important;
}

button,
input,
textarea {
  font: inherit;
  color: inherit;
}

button {
  cursor: pointer;
}

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

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

/* ------------------------------------------------------------ brand mark */

.brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 30px;
  height: 30px;
  padding: 6px;
  border-radius: 9px;
  background: var(--accent);
  color: var(--accent-contrast);
}

.login-brand-mark {
  width: 46px;
  height: 46px;
  padding: 10px;
  border-radius: 14px;
  margin: 0 auto 6px;
}

.message-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 28px;
  height: 28px;
  padding: 6px;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  margin-top: 2px;
}

/* ------------------------------------------------------------ login screen */

.login-screen {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

.login-card {
  width: 100%;
  max-width: 320px;
  background: var(--bg-elevated);
  border: 1px solid var(--border);
  border-radius: 16px;
  box-shadow: var(--shadow-md);
  padding: 32px 28px;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  gap: 12px;
}

.login-card h1 {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.login-sub {
  margin: 0 0 4px;
  color: var(--text-muted);
  font-size: 0.9rem;
}

.login-card input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.login-card input:focus-visible {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.login-card button[type="submit"] {
  width: 100%;
  padding: 10px 12px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 600;
  transition: background-color 0.15s ease;
}

.login-card button[type="submit"]:hover {
  background: var(--accent-hover);
}

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

/* --------------------------------------------------------------- app shell */

.app-shell {
  height: 100vh;
  display: flex;
  flex-direction: column;
}

.app-header {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 12px 20px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
  flex-shrink: 0;
}

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

.app-title {
  margin: 0;
  font-size: 1.05rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.status-indicator {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--text-muted);
  padding: 3px 10px;
  border-radius: 999px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 160px;
  transition: color 0.15s ease, background-color 0.15s ease, border-color 0.15s ease;
}

.status-indicator.streaming {
  color: var(--accent);
  background: var(--accent-soft);
  border-color: transparent;
}

.icon-button,
.text-button {
  background: none;
  border: none;
  padding: 6px 10px;
  border-radius: 8px;
  color: var(--text);
  transition: background-color 0.15s ease;
}

.icon-button:hover,
.text-button:hover {
  background: var(--bg-sunken);
}

.icon-button {
  display: none; /* shown only below the sidebar-collapse breakpoint */
  font-size: 1.1rem;
}

.app-body {
  flex: 1;
  display: flex;
  min-height: 0;
}

/* ------------------------------------------------------------------ sidebar */

.sidebar {
  width: 272px;
  flex-shrink: 0;
  border-right: 1px solid var(--border);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  padding: 12px;
  gap: 4px;
  overflow-y: auto;
}

.new-chat-button {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 12px;
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  color: var(--text);
  font-weight: 500;
  text-align: left;
  transition: background-color 0.15s ease;
}

.new-chat-button .icon {
  color: var(--text-muted);
  flex-shrink: 0;
}

.new-chat-button:hover {
  background: var(--bg-sunken);
}

.thread-list {
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 8px;
  overflow-y: auto;
}

.thread-item {
  display: block;
  width: 100%;
  text-align: left;
  background: none;
  border: none;
  border-radius: 8px;
  padding: 8px 10px;
  transition: background-color 0.15s ease;
}

.thread-item:hover {
  background: var(--bg-sunken);
}

.thread-item.active {
  background: var(--accent-soft);
}

.thread-item-title {
  font-size: 0.88rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.thread-item-time {
  font-size: 0.75rem;
  color: var(--text-muted);
  margin-top: 2px;
}

.thread-list-empty {
  padding: 10px 10px 4px;
  color: var(--text-muted);
  font-size: 0.82rem;
  text-align: center;
}

/* -------------------------------------------------------- sidebar search */

.sidebar-search {
  position: relative;
  margin: 6px 0 2px;
}

.sidebar-search-icon {
  position: absolute;
  left: 10px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--text-muted);
  pointer-events: none;
}

.sidebar-search input {
  width: 100%;
  padding: 7px 10px 7px 30px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font-size: 0.85rem;
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.sidebar-search input::placeholder {
  color: var(--text-muted);
}

.sidebar-search input:focus-visible {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.sidebar-scrim {
  display: none;
}

/* -------------------------------------------------------------- chat pane */

.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.message-list {
  flex: 1;
  overflow-y: auto;
  padding: 24px 0;
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.message-row {
  display: flex;
  gap: 14px;
  width: 100%;
  max-width: 48rem;
  margin: 0 auto;
  padding: 0 24px;
}

.message-row-user {
  justify-content: flex-end;
}

.message-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message-row-user .message-content {
  flex: 0 1 auto;
  align-items: flex-end;
}

/* Generic bubble: used for the right-aligned user message and for inline
   error rows. Plain text, so newlines matter — pre-wrap stays here. */
.bubble {
  padding: 10px 16px;
  border-radius: 18px;
  white-space: pre-wrap;
  overflow-wrap: break-word;
}

.bubble-user {
  background: var(--bubble-user-bg);
  color: var(--bubble-user-text);
  max-width: 75%;
  border-bottom-right-radius: 6px;
}

.bubble-error {
  align-self: flex-start;
  background: var(--error-bg);
  color: var(--error-text);
  border: 1px solid var(--error-border);
  border-radius: 12px;
}

/* Assistant message body: no bubble background, full row width, generous
   line-height. NOT pre-wrap — the renderer emits real block elements
   (p/h/ul/pre/table), and pre-wrap on top of those would double the
   spacing. */
.message-body {
  color: var(--text);
  line-height: 1.6;
  overflow-wrap: break-word;
}

.message-body > :first-child {
  margin-top: 0;
}

.message-body > :last-child {
  margin-bottom: 0;
}

.message-body p {
  margin: 0.5em 0;
}

.message-body h1,
.message-body h2,
.message-body h3,
.message-body h4 {
  font-weight: 650;
  line-height: 1.3;
  margin: 0.9em 0 0.4em;
}

.message-body h1 {
  font-size: 1.35em;
}

.message-body h2 {
  font-size: 1.2em;
}

.message-body h3 {
  font-size: 1.08em;
}

.message-body h4 {
  font-size: 1em;
}

.message-body ul,
.message-body ol {
  margin: 0.5em 0;
  padding-left: 1.4em;
}

.message-body li {
  margin: 0.2em 0;
}

.message-body li ul,
.message-body li ol {
  margin: 0.2em 0;
}

.message-body blockquote {
  margin: 0.6em 0;
  padding: 0.1em 0 0.1em 0.9em;
  border-left: 3px solid var(--border);
  color: var(--text-muted);
}

.message-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 1em 0;
}

.message-body a {
  color: var(--accent);
  text-decoration: none;
}

.message-body a:hover {
  text-decoration: underline;
}

.message-body code {
  background: rgba(127, 127, 127, 0.16);
  border-radius: 4px;
  padding: 0.1em 0.4em;
  font-family: var(--font-mono);
  font-size: 0.9em;
}

/* Blinking-caret streaming indicator. The classic "steps(2)" trick: a
   single-keyframe animation that jumps the caret straight from visible to
   hidden halfway through, instead of fading — a sharp blink, not a pulse.
   Reduced motion is already handled globally (see the reduced-motion query
   at the end of this file, which forces animation-duration to ~0 on every
   element), so under that preference this just renders as a steady caret. */
@keyframes blink {
  to {
    visibility: hidden;
  }
}

.message-body.streaming::after {
  content: "";
  display: inline-block;
  width: 3px;
  height: 1em;
  margin-left: 2px;
  vertical-align: text-bottom;
  background: var(--accent);
  border-radius: 1px;
  animation: blink 1s steps(2) infinite;
}

/* Code block: div.md-codeblock > div.md-codeblock-head (lang + copy) + pre > code */
.message-body .md-codeblock {
  margin: 0.75em 0;
  border: 1px solid var(--border);
  border-radius: 10px;
  overflow: hidden;
  background: var(--code-bg);
}

.message-body .md-codeblock-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 6px 12px;
  background: var(--code-head-bg);
  border-bottom: 1px solid var(--border);
}

.message-body .md-codeblock-lang {
  font-family: var(--font-mono);
  font-size: 0.72em;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--text-muted);
}

.message-body .md-codeblock-copy {
  background: none;
  border: none;
  padding: 3px 9px;
  border-radius: 6px;
  font-size: 0.78em;
  color: var(--text-muted);
  transition: background-color 0.15s ease, color 0.15s ease;
}

.message-body .md-codeblock-copy:hover {
  background: var(--bg-sunken);
  color: var(--text);
}

.message-body .md-codeblock pre {
  margin: 0;
  padding: 12px 14px;
  overflow-x: auto;
}

.message-body .md-codeblock code {
  background: none;
  padding: 0;
  font-family: var(--font-mono);
  font-size: 0.875em;
}

/* Table: div.md-table-wrap > table.md-table */
.message-body .md-table-wrap {
  overflow-x: auto;
  margin: 0.75em 0;
}

.message-body .md-table {
  border-collapse: collapse;
  width: 100%;
  font-size: 0.9em;
}

.message-body .md-table th,
.message-body .md-table td {
  padding: 6px 10px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}

.message-body .md-table th {
  background: var(--bg-sunken);
  font-weight: 600;
}

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

.chip-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.chip {
  border: 1px solid var(--border);
  background: var(--chip-bg);
  color: var(--chip-text);
  border-radius: 999px;
  padding: 3px 10px;
  font-size: 0.76rem;
  font-family: var(--font-mono);
  transition: border-color 0.15s ease;
}

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

.chip-details {
  margin: 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  color: var(--chip-text);
  font-family: var(--font-mono);
  font-size: 0.75rem;
  max-width: 60ch;
  overflow-x: auto;
}

.sources {
  font-size: 0.82rem;
  color: var(--text-muted);
  background: var(--bg-sunken);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 12px;
}

.sources-heading {
  font-weight: 600;
  color: var(--text);
  margin-bottom: 3px;
}

.sources ul {
  margin: 0;
  padding-left: 1.1em;
}

.sources-truncated {
  margin-top: 3px;
  font-style: italic;
}

.staleness-banner {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  font-size: 0.82rem;
  background: var(--warning-bg);
  color: var(--warning-text);
  border: 1px solid var(--warning-border);
  border-radius: 10px;
  padding: 7px 12px;
}

.staleness-icon {
  flex-shrink: 0;
  line-height: 1.4;
}

.staleness-text {
  flex: 1;
  min-width: 0;
  line-height: 1.4;
}

/* ---------------------------------------------------------- message actions */

/* Faded-until-hover action row (Copy, …) under a completed assistant
   message. Always visible on touch devices, which don't have a hover
   state to reveal it, and whenever a control inside it holds focus. */
.message-actions {
  display: flex;
  gap: 4px;
  opacity: 0;
  transition: opacity 0.15s ease;
}

.message-row:hover .message-actions,
.message-row:focus-within .message-actions {
  opacity: 1;
}

@media (hover: none) {
  .message-actions {
    opacity: 1;
  }
}

.message-action-button {
  display: flex;
  align-items: center;
  gap: 5px;
  border: none;
  background: none;
  padding: 4px 9px;
  border-radius: 7px;
  color: var(--text-muted);
  font-size: 0.76rem;
  transition: background-color 0.15s ease, color 0.15s ease;
}

.message-action-button:hover {
  background: var(--bg-sunken);
  color: var(--text);
}

.message-action-button.copied {
  color: var(--accent);
}

.message-action-button svg {
  flex-shrink: 0;
}

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

.empty-state {
  margin: auto;
  width: 100%;
  max-width: 640px;
  padding: 0 24px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  text-align: center;
}

.empty-state-avatar {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  width: 56px;
  height: 56px;
  padding: 12px;
  border-radius: 16px;
  background: var(--accent);
  color: var(--accent-contrast);
}

.empty-state-greeting {
  margin: 0;
  font-size: 1.3rem;
  font-weight: 650;
  letter-spacing: -0.01em;
}

.prompt-grid {
  width: 100%;
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
}

.prompt-card {
  text-align: left;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--bg-elevated);
  color: var(--text);
  font-size: 0.86rem;
  line-height: 1.4;
  transition: transform 0.15s ease, background-color 0.15s ease, border-color 0.15s ease;
}

.prompt-card:hover,
.prompt-card:focus-visible {
  transform: translateY(-2px);
  background: var(--accent-soft);
  border-color: var(--accent);
}

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

.composer-area {
  flex-shrink: 0;
  padding: 0 24px 20px;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 48rem;
  margin: 0 auto;
  padding: 9px 9px 9px 18px;
  border: 1px solid var(--border);
  border-radius: 22px;
  background: var(--bg-elevated);
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

.composer:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}

.composer textarea {
  flex: 1;
  resize: none;
  max-height: 160px;
  padding: 7px 0;
  border: none;
  background: transparent;
  color: var(--text);
  line-height: 1.5;
}

.composer textarea:focus-visible {
  /* the wrapping .composer shows a focus-within ring; a second outline on
     the borderless textarea inside it would double up */
  outline: none;
}

.composer textarea:disabled {
  opacity: 0.6;
}

.composer-send {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-contrast);
  transition: background-color 0.15s ease, opacity 0.15s ease;
}

.composer-send:hover:not(:disabled) {
  background: var(--accent-hover);
}

.composer-send:disabled {
  opacity: 0.5;
  cursor: default;
}

.composer-caption {
  max-width: 48rem;
  margin: 8px auto 0;
  padding: 0 24px;
  text-align: center;
  font-size: 0.75rem;
  color: var(--text-muted);
}

/* ------------------------------------------------------------ scrollbars */

.message-list,
.sidebar,
.thread-list,
pre {
  scrollbar-width: thin;
  scrollbar-color: var(--border) transparent;
}

.message-list::-webkit-scrollbar,
.sidebar::-webkit-scrollbar,
.thread-list::-webkit-scrollbar,
pre::-webkit-scrollbar {
  width: 8px;
  height: 8px;
}

.message-list::-webkit-scrollbar-track,
.sidebar::-webkit-scrollbar-track,
.thread-list::-webkit-scrollbar-track,
pre::-webkit-scrollbar-track {
  background: transparent;
}

.message-list::-webkit-scrollbar-thumb,
.sidebar::-webkit-scrollbar-thumb,
.thread-list::-webkit-scrollbar-thumb,
pre::-webkit-scrollbar-thumb {
  background: var(--border);
  border-radius: 8px;
}

/* ------------------------------------------------------------- responsive */

@media (max-width: 720px) {
  .icon-button {
    display: inline-block;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    z-index: 20;
    width: 280px;
    transform: translateX(-100%);
    transition: transform 0.18s ease;
    box-shadow: var(--shadow-md);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .sidebar-scrim {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 10;
  }

  .message-row {
    padding: 0 16px;
  }

  .message-row-user .bubble-user {
    max-width: 85%;
  }

  .prompt-grid {
    grid-template-columns: 1fr;
  }

  .composer-area,
  .composer-caption {
    padding-left: 16px;
    padding-right: 16px;
  }
}

@media (max-width: 380px) {
  .app-header {
    padding: 8px 10px;
    gap: 8px;
  }

  .app-title {
    font-size: 0.92rem;
  }

  .status-indicator {
    max-width: 96px;
  }

  .message-list {
    padding: 16px 0;
    gap: 18px;
  }

  .message-row {
    padding: 0 10px;
  }

  .composer-area {
    padding: 0 10px 14px;
  }

  .composer-caption {
    padding: 0 10px;
  }

  .composer {
    padding: 7px 7px 7px 14px;
  }
}

/* ------------------------------------------------------- reduced motion */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
