/* chat.css — Playground 3-column reskin for the llm_wrapper demo chat (cycle 44).
 *
 * Front-end-only reskin (design_spec cycle 44). Server contracts, JS wiring ids, and the
 * single innerHTML sink (renderMarkdown) are all preserved; only the visual surface changes.
 * Palette = warm cream / coral (ADR-351, base :root updated). All hex live in :root tokens
 * (rule bodies use var(--*) only, ADR-352). WCAG pairs are Python-verified (design_spec §6).
 */

:root {
  /* --- Palette (cycle 44, ADR-351/352; adjusted hex from design_spec §1-2). The cycle-18
     "preserve six colors byte-for-byte" invariant is intentionally lifted this cycle (the
     reskin is the goal). Every fg/bg pair is verified >=4.5 (body) / >=3.0 (UI) — §6. ---- */
  --bg: #F4EEE6;
  --user-bg: #BE4F2B;
  --user-fg: #FFFFFF;
  --assistant-bg: #FFFFFF;
  --assistant-fg: #2B2622;
  --error-fg: #A8441F;

  /* Spacing — 4/8 grid (§1-3). */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 24px;
  --space-6: 32px;
  --space-7: 48px;
  --space-8: 64px;

  /* Typography — modular scale + line-heights (§1-4). Font = system-ui stack (no external
     font). --text-2xl is the one new type token (empty-state title / brand). */
  --text-xs: 12px;
  --text-sm: 14px;
  --text-base: 16px;
  --text-lg: 18px;
  --text-xl: 20px;
  --text-2xl: 24px;
  --leading-tight: 1.25;
  --leading-normal: 1.5;
  --leading-relaxed: 1.6;

  /* Semantic colors (§1-2, adjusted hex). */
  --surface: #FBF7F1;
  --text: #2B2622;
  --text-muted: #6E6358;
  --border: #8F7E6C;
  --border-soft: #E7DFD3;
  --accent: #BE4F2B;
  --accent-text: #FFFFFF;
  --accent-hover: #A6451F;
  --accent-active: #8E3B1B;
  --accent-soft: #FBEAE2;
  --accent-ink: #A8441F;
  --header-chip: #F0E9DF;
  --disabled-bg: #6E6358;
  --disabled-text: #FFFFFF;
  --danger: #A8441F;
  --success: #2F6A53;
  --focus-ring: #BE4F2B;

  /* Markdown-area semantic colors (warm tone, assistant surface = white, §1-2). */
  --md-quote-text: #5A5145;
  --md-code-bg: #F0E9DF;
  --md-code-text: #2B2622;
  --md-pre-bg: #2B2622;
  --md-pre-text: #FBF7F1;
  --md-th-bg: #F0E9DF;
  --md-link: #A8441F;

  /* Radius / shadow / motion (§1-5). --radius-xl is the new large round (bubbles / composer /
     avatar). --dur-slow is the new drawer-slide duration. */
  --radius-sm: 6px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-xl: 18px;
  --radius-full: 9999px;
  --shadow-1: 0 1px 2px rgba(43, 38, 34, 0.08);
  --shadow-2: 0 4px 16px rgba(43, 38, 34, 0.14);
  --dur-fast: 120ms;
  --dur-base: 200ms;
  --dur-slow: 320ms;
  --ease-out: cubic-bezier(0.2, 0.8, 0.2, 1);

  /* Layout dimensions (§1-6). 4/8 grid multiples (256=8x32, 340=4x85, 1280=8x160, 300=4x75). */
  --rail-width: 256px;
  --panel-width: 340px;
  --layout-max: 1280px;
  --drawer-width: 300px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: var(--text-base);
  line-height: var(--leading-normal);
}

/* Single shared focus ring (§5). outline-offset creates a gap so the ring lands on the
   surrounding surface/bg rather than accent-on-accent over a button. */
:focus-visible {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

/* ===================================================================================== *
 * Layout — mobile-first stack (base) + desktop 3-column grid (>=900px). (§2-1, ADR-350)  *
 * DOM order = header, left rail, source-panel, main (center), right settings panel.      *
 * No CSS `order` trick — visual order == tab order at every breakpoint (WCAG 2.4.3).     *
 * ===================================================================================== */
.app-layout {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  height: 100vh;
  max-width: var(--layout-max);
  margin: 0 auto;
  padding: var(--space-4);
}

@media (min-width: 900px) {
  .app-layout {
    display: grid;
    grid-template-columns: var(--rail-width) minmax(0, 1fr) var(--panel-width);
    grid-template-rows: auto auto minmax(0, 1fr);
    grid-template-areas:
      "header header header"
      "rail   source panel"
      "rail   main   panel";
    gap: var(--space-4) var(--space-5);
  }

  .app-header {
    grid-area: header;
  }

  .history-rail {
    grid-area: rail;
  }

  .source-panel {
    grid-area: source;
  }

  .chat-main {
    grid-area: main;
  }

  .options-panel {
    grid-area: panel;
  }
}

/* The chat column keeps a flex column; min-height:0 is the mandatory grid/flex-child guard so
   #chat-log scrolls instead of stretching the track. */
.chat-main {
  display: flex;
  flex-direction: column;
  min-height: 0;
  flex: 1;
}

/* ----- Header ----------------------------------------------------------------------- */
.app-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-3) var(--space-4);
  background: var(--surface);
  border-bottom: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
}

.app-brand {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-width: 0;
}

/* Brand mark = a rounded coral square with two decorative dots (CSS only, no img / innerHTML). */
.app-brand-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  width: var(--space-7);
  height: var(--space-7);
  background: var(--accent);
  border-radius: var(--radius-lg);
  flex: 0 0 auto;
}

.app-brand-mark::before,
.app-brand-mark::after {
  content: "";
  width: var(--space-1);
  height: var(--space-1);
  background: var(--accent-text);
  border-radius: var(--radius-full);
}

.app-brand-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.app-brand-title {
  margin: 0;
  font-size: var(--text-lg);
  font-weight: 600;
  color: var(--text);
}

.app-brand-subtitle {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Right side of the header: session usage + (narrow) drawer triggers. */
.app-header-end {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  margin-left: auto;
}

/* Drawer triggers: visible only on narrow screens (hidden on the desktop grid). */
.drawer-trigger {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-base);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}

.drawer-trigger:hover {
  background: var(--header-chip);
}

@media (min-width: 900px) {
  .drawer-trigger {
    display: none;
  }
}

/* In-app logout (cycle 62 R-1). A NEUTRAL/SECONDARY affordance: surface fill + 1px border, not
   the terracotta primary (there is one primary per screen). The accent appears only on hover
   (subtle warm tint + border) and on the shared focus-visible ring (§5). >=44px touch target so
   it is keyboard- and finger-reachable; text label "로그아웃" so no aria-label is needed. The
   wrapping form is layout-only (no box of its own) so it sits inline in .app-header-end. */
.logout-form {
  display: inline-flex;
  margin: 0;
}

.logout-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-sm);
  line-height: var(--leading-tight);
  cursor: pointer;
  transition:
    background-color var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out),
    color var(--dur-fast) var(--ease-out);
}

.logout-button:hover {
  background: var(--accent-soft);
  border-color: var(--accent);
  color: var(--accent-ink);
}

.logout-button:active {
  background: var(--header-chip);
}

/* ----- Left history rail (256px) ---------------------------------------------------- */
.history-rail {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-height: 0;
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  overflow-y: auto;
}

/* New conversation button (full-width coral, §2-4 / §3-9). */
.new-chat-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  width: 100%;
  min-height: 44px;
  padding: var(--space-2) var(--space-4);
  border: none;
  border-radius: var(--radius-lg);
  background: var(--accent);
  color: var(--accent-text);
  font-size: var(--text-sm);
  font-weight: 600;
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

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

.new-chat-button:active {
  background: var(--accent-active);
  transform: translateY(1px);
}

/* Section label ("저장된 대화"): uppercase muted caption (§2-4). */
.rail-section-label {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-muted);
}

/* ----- Right settings panel (340px) — our real controls only (§2-5) ----------------- */
.options-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-5);
  min-height: 0;
  padding: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  overflow-y: auto;
}

.options-heading {
  margin: 0;
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--text);
}

.option-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
}

.option-field {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
}

.option-label {
  font-size: var(--text-sm);
  color: var(--text-muted);
}

/* CSS-only show/hide for the whole Model field (chat.js toggles only modelSelect.hidden). */
#model-field:has(> select[hidden]) {
  display: none;
}

/* ===================================================================================== *
 * Responsive drawers (<900px): the left rail + right panel become slide-in drawers.      *
 * (§2-6, ADR-354/355/356). Off-canvas via transform translateX; .is-open slides them in. *
 * The desktop grid (>=900px) above resets them to static columns.                        *
 * ===================================================================================== */
@media (max-width: 899px) {
  .history-rail,
  .options-panel {
    position: fixed;
    top: 0;
    bottom: 0;
    width: var(--drawer-width);
    max-width: 85vw;
    z-index: 30;
    border-radius: 0;
    box-shadow: var(--shadow-2);
    transition: transform var(--dur-slow) var(--ease-out);
  }

  .history-rail {
    left: 0;
    transform: translateX(-100%);
  }

  .options-panel {
    right: 0;
    transform: translateX(100%);
  }

  .history-rail.is-open,
  .options-panel.is-open {
    transform: translateX(0);
  }

  .drawer-scrim {
    position: fixed;
    inset: 0;
    z-index: 20;
    background: rgba(43, 38, 34, 0.45);
    opacity: 0;
    visibility: hidden;
    transition: opacity var(--dur-base) var(--ease-out), visibility var(--dur-base);
  }

  .drawer-scrim.is-open {
    opacity: 1;
    visibility: visible;
  }
}

/* The scrim is never shown on the desktop grid. */
@media (min-width: 900px) {
  .drawer-scrim {
    display: none;
  }
}

/* ===================================================================================== *
 * Chat log + bubbles (§3-3/§3-4)                                                          *
 * ===================================================================================== */
.chat-log {
  flex: 1;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  padding: var(--space-4);
  background: var(--bg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
}

.bubble {
  position: relative;
  max-width: 80%;
  padding: var(--space-3) var(--space-6) var(--space-3) var(--space-4);
  white-space: pre-wrap;
  word-break: break-word;
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  animation: bubble-enter var(--dur-base) var(--ease-out);
}

@keyframes bubble-enter {
  from {
    opacity: 0;
    transform: translateY(var(--space-1));
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.bubble-role {
  font-size: var(--text-xs);
  text-transform: uppercase;
}

/* user: right-aligned coral bubble, asymmetric radius (xl xl sm xl — bottom-right small). */
.bubble-user {
  align-self: flex-end;
  background: var(--user-bg);
  color: var(--user-fg);
  border-radius: var(--radius-xl) var(--radius-xl) var(--radius-sm) var(--radius-xl);
  box-shadow: var(--shadow-1);
}

.bubble-user .bubble-role {
  color: var(--user-fg);
}

/* assistant: left-aligned white bubble, asymmetric radius (xl xl xl sm — bottom-left small),
   wrapped in a row with the avatar (see .bubble-assistant below). */
.bubble-assistant {
  align-self: flex-start;
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: var(--space-2);
  padding: 0;
  background: transparent;
  max-width: 88%;
}

/* assistant avatar (30px coral-soft round, ADR-359). Built by chat.js appendBubble. */
.bubble-avatar {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex: 0 0 auto;
  width: 30px;
  height: 30px;
  margin-top: var(--space-1);
  background: var(--accent-soft);
  border-radius: var(--radius-full);
  color: var(--accent);
  font-size: var(--text-sm);
}

/* The assistant text column (the actual white bubble surface). */
.bubble-assistant .bubble-body {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  min-width: 0;
  padding: var(--space-3) var(--space-6) var(--space-3) var(--space-4);
  background: var(--assistant-bg);
  color: var(--assistant-fg);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-xl) var(--radius-xl) var(--radius-xl) var(--radius-sm);
}

.bubble-assistant .bubble-role {
  color: var(--text-muted);
}

.bubble-error {
  align-self: center;
  color: var(--error-fg);
  font-style: italic;
}

/* ----- Empty state (§3-7) ----------------------------------------------------------- */
.chat-empty {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  padding: var(--space-5);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-3);
  animation: chat-empty-fade var(--dur-base) var(--ease-out);
}

/* Decorative 64px coral-soft round with three dots (CSS only, aria-hidden span in markup). */
.chat-empty-mark {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-1);
  width: var(--space-8);
  height: var(--space-8);
  background: var(--accent-soft);
  border-radius: var(--radius-full);
}

.chat-empty-mark span {
  width: var(--space-1);
  height: var(--space-1);
  background: var(--accent);
  border-radius: var(--radius-full);
}

.chat-empty-title {
  margin: 0;
  font-size: var(--text-2xl);
  font-weight: 600;
  color: var(--text);
}

.chat-empty-body {
  margin: 0;
  font-size: var(--text-sm);
}

@keyframes chat-empty-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

/* ===================================================================================== *
 * Compose form + send button (§3-6)                                                      *
 * ===================================================================================== */
.chat-form {
  display: flex;
  gap: var(--space-2);
  align-items: center;
  margin-top: var(--space-3);
  padding: var(--space-2);
  background: var(--surface);
  /* Soft default border, accent on focus (design-reference composer + DESIGN.md
     "입력 포커스: 테두리 #BF5A37, 기본 #E2DCD3"). */
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-xl);
  transition: border-color var(--dur-fast) var(--ease-out);
}

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

#message-input {
  flex: 1;
  padding: var(--space-2) var(--space-3);
  border: none;
  border-radius: var(--radius-lg);
  font-size: var(--text-base);
  min-height: 44px;
  background: transparent;
  color: var(--text);
}

#message-input:disabled {
  background: var(--bg);
  cursor: not-allowed;
}

.backend-select-label {
  font-size: var(--text-sm);
  white-space: nowrap;
}

.backend-select {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  background: var(--surface);
  color: var(--text);
  min-height: 44px;
}

.model-select {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  background: var(--surface);
  color: var(--text);
  min-height: 44px;
}

.model-select[hidden] {
  display: none;
}

.options-panel .backend-select,
.options-panel .model-select {
  width: 100%;
}

/* select disabled visual (while sending). */
.backend-select:disabled,
.model-select:disabled {
  background: var(--bg);
  color: var(--text-muted);
  cursor: not-allowed;
}

#send-button {
  padding: var(--space-2) var(--space-4);
  border: none;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: var(--accent-text);
  font-size: var(--text-base);
  cursor: pointer;
  min-height: 44px;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

#send-button:hover {
  background: var(--accent-hover);
}

#send-button:active {
  background: var(--accent-active);
  transform: translateY(1px);
}

#send-button:disabled {
  background: var(--disabled-bg);
  color: var(--disabled-text);
  cursor: not-allowed;
}

/* Loading state: send button shows "Sending…" + aria-busy + an inline spinner. The text is the
   primary signal (no color-only info); the spinner keeps its arc when reduced-motion stops it. */
#send-button[aria-busy="true"]::after {
  content: "";
  display: inline-block;
  width: 0.9em;
  height: 0.9em;
  margin-left: var(--space-2);
  vertical-align: -0.15em;
  border: 2px solid var(--accent-text);
  border-top-color: transparent;
  border-radius: var(--radius-full);
  animation: send-spin 0.8s linear infinite;
}

@keyframes send-spin {
  to {
    transform: rotate(360deg);
  }
}

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

/* ===================================================================================== *
 * Switch toggles (Playground signature, §3-2). The native checkbox is restyled as a      *
 * 42x24 track + 18px knob; chat.js id / .checked logic is untouched (CSS-only).           *
 * ===================================================================================== */
.stream-toggle {
  font-size: var(--text-sm);
  display: flex;
  align-items: center;
  gap: var(--space-2);
  white-space: nowrap;
  min-height: 44px;
  cursor: pointer;
}

.stream-toggle input[type="checkbox"] {
  appearance: none;
  -webkit-appearance: none;
  position: relative;
  flex: 0 0 auto;
  width: 42px;
  height: 24px;
  margin: 0;
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}

.stream-toggle input[type="checkbox"]::before {
  content: "";
  position: absolute;
  top: 2px;
  left: 2px;
  width: 18px;
  height: 18px;
  border-radius: var(--radius-full);
  background: var(--accent-text);
  box-shadow: var(--shadow-1);
  transition: transform var(--dur-fast) var(--ease-out);
}

.stream-toggle input[type="checkbox"]:checked {
  background: var(--accent);
  border-color: var(--accent);
}

.stream-toggle input[type="checkbox"]:checked::before {
  transform: translateX(18px);
}

.stream-toggle input[type="checkbox"]:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

/* ===================================================================================== *
 * Audience segmented toggle (§3-1). Two segments form one pill; only background animates. *
 * ===================================================================================== */
.audience-fieldset {
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: var(--space-1);
}

.audience-legend {
  padding: 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
  white-space: nowrap;
}

.audience-segmented {
  display: flex;
  align-items: stretch;
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  overflow: hidden;
}

.audience-segment {
  display: flex;
  align-items: center;
  white-space: nowrap;
  flex: 1;
}

/* The 2nd segment gets the inner divider line (status/structure border -> --border). */
.audience-segment + .audience-segment {
  border-left: 1px solid var(--border);
}

.audience-radio {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
}

.audience-segment-text {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  min-height: 44px;
  padding: 0 var(--space-3);
  font-size: var(--text-sm);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out);
}

.audience-radio:not(:checked) + .audience-segment-text:hover {
  background: var(--header-chip);
}

.audience-radio:checked + .audience-segment-text {
  background: var(--accent);
  color: var(--accent-text);
}

.audience-radio:focus-visible + .audience-segment-text {
  outline: 2px solid var(--focus-ring);
  outline-offset: 2px;
}

.audience-radio:disabled + .audience-segment-text {
  color: var(--text-muted);
  cursor: not-allowed;
}

/* ----- RAG sub-options (§3, hierarchy via left border) ------------------------------ */
.rag-suboptions {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  padding-left: var(--space-3);
  border-left: 1px solid var(--border);
}

.rag-subhint {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.agentic-hint {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Always-on toggle description (cycle 50). Same tokens as .agentic-hint / .rag-subhint
   (no new :root tokens or hex). Static caption shown under the RAG / Stream labels. */
.option-hint {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* RAG-gated show/hide (CSS-only primary path). */
.option-group:has(> .stream-toggle > #rag-toggle:not(:checked)) .rag-suboptions {
  display: none;
}

/* Agentic > RAG priority: mute the RAG group when agentic is ON (opacity only, no layout shift). */
.options-panel:has(#agentic-toggle:checked) .option-group:has(#rag-toggle) {
  opacity: 0.55;
}

/* ===================================================================================== *
 * Reduced motion — neutralize all animation/transition (covers new drawer/scrim/enter).   *
 * (§4, ADR-356)                                                                           *
 * ===================================================================================== */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

@media (max-width: 480px) {
  .chat-form {
    flex-wrap: wrap;
  }
}

/* ===================================================================================== *
 * Rendered-markdown readability (scoped to .bubble-assistant .bubble-text only).          *
 * ===================================================================================== */
.bubble-assistant .bubble-text {
  display: block;
  white-space: normal;
  max-width: 66ch;
}

.bubble-assistant .bubble-text > :first-child {
  margin-top: 0;
}

.bubble-assistant .bubble-text > :last-child {
  margin-bottom: 0;
}

.bubble-assistant .bubble-text h1,
.bubble-assistant .bubble-text h2,
.bubble-assistant .bubble-text h3,
.bubble-assistant .bubble-text h4,
.bubble-assistant .bubble-text h5,
.bubble-assistant .bubble-text h6 {
  margin: 0.6em 0 0.3em;
  line-height: 1.25;
}

.bubble-assistant .bubble-text p {
  margin: 0.4em 0;
}

.bubble-assistant .bubble-text ul,
.bubble-assistant .bubble-text ol {
  margin: 0.4em 0;
  padding-left: 1.4em;
}

.bubble-assistant .bubble-text li {
  margin: 0.15em 0;
}

.bubble-assistant .bubble-text blockquote {
  margin: 0.5em 0;
  padding: 0.1em 0.8em;
  border-left: 3px solid var(--border);
  color: var(--md-quote-text);
}

.bubble-assistant .bubble-text code {
  font-family: ui-monospace, "SF Mono", "Cascadia Code", Consolas, "Liberation Mono", monospace;
  font-size: 0.9em;
  background: var(--md-code-bg);
  color: var(--md-code-text);
  padding: 0.1em 0.3em;
  border-radius: var(--radius-sm);
}

.bubble-assistant .bubble-text pre {
  margin: 0.5em 0;
  padding: 0.6em 0.8em;
  background: var(--md-pre-bg);
  color: var(--md-pre-text);
  border-radius: var(--radius-sm);
  overflow-x: auto;
}

.bubble-assistant .bubble-text pre code {
  background: transparent;
  color: inherit;
  padding: 0;
  border-radius: 0;
  font-size: 0.85em;
}

.bubble-assistant .bubble-text table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.9em;
}

.bubble-assistant .bubble-text th,
.bubble-assistant .bubble-text td {
  border: 1px solid var(--border);
  padding: 0.3em 0.6em;
  text-align: left;
}

.bubble-assistant .bubble-text th {
  background: var(--md-th-bg);
}

.bubble-assistant .bubble-text hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.6em 0;
}

.bubble-assistant .bubble-text a {
  color: var(--md-link);
  text-decoration: underline;
}

/* ===================================================================================== *
 * Image attachment (§3)                                                                   *
 * ===================================================================================== */
.attach-area[hidden] {
  display: none;
}

.attach-button,
.attach-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 44px;
  min-height: 44px;
  padding: var(--space-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-base);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

.attach-button:hover,
.attach-remove:hover {
  background: var(--header-chip);
}

.attach-button:active,
.attach-remove:active {
  transform: translateY(1px);
}

.attach-button:disabled {
  color: var(--disabled-bg);
  cursor: not-allowed;
}

.attach-preview {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  margin-top: var(--space-3);
  padding: var(--space-2);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  animation: attach-preview-fade var(--dur-base) var(--ease-out);
}

.attach-preview[hidden] {
  display: none;
}

@keyframes attach-preview-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.attach-preview-thumb {
  display: block;
  max-width: calc(var(--space-8) + var(--space-7));
  max-height: var(--space-8);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-md);
}

.attach-preview-caption {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.attach-remove {
  margin-left: auto;
}

.attach-status {
  margin-top: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.attach-status[hidden] {
  display: none;
}

.attach-status--danger {
  color: var(--danger);
}

.bubble-image {
  display: block;
  max-width: 100%;
  border-radius: var(--radius-md);
}

/* ===================================================================================== *
 * Typing indicator (§3-5) — opacity-only blink, layout-shift 0; reduced-motion stops it.  *
 * ===================================================================================== */
.typing-indicator {
  display: inline-flex;
  gap: var(--space-1);
  align-items: center;
}

.typing-dot {
  width: 0.4em;
  height: 0.4em;
  border-radius: var(--radius-full);
  background: var(--text-muted);
  opacity: 1;
  animation: typing-blink 1.2s var(--ease-out) infinite;
}

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

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

@keyframes typing-blink {
  0%,
  100% {
    opacity: 1;
  }
  50% {
    opacity: 0.3;
  }
}

/* ===================================================================================== *
 * History rows / suggestions / sources / usage / save / copy / mode badge.                *
 * Class hooks preserved (chat.js createElement); visual only.                             *
 * ===================================================================================== */
.history-panel {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  min-width: 0;
}

.history-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  overflow-y: auto;
}

/* One saved chat = a card with a soft hairline (decorative -> --border-soft, §3-8). */
.history-item {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  padding: var(--space-3);
  background: var(--surface);
  transition: box-shadow var(--dur-fast) var(--ease-out),
    border-color var(--dur-fast) var(--ease-out);
}

.history-item:hover,
.history-item:focus-within {
  border-color: var(--border);
  box-shadow: var(--shadow-1);
}

.history-item-title {
  margin: 0;
  font-size: var(--text-sm);
  font-weight: 600;
  color: var(--text);
  word-break: break-word;
}

.history-item-time {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* Tag chips: soft coral fill + accent-ink text (§3-8). Status/structure border uses --border. */
.history-tags,
.save-tags {
  display: inline-flex;
  flex-wrap: wrap;
  gap: var(--space-1);
}

.history-tag,
.tag-chip {
  display: inline-flex;
  align-items: center;
  gap: var(--space-1);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  padding: 0 var(--space-2);
  font-size: var(--text-xs);
  background: var(--header-chip);
  color: var(--text-muted);
}

.history-item-actions {
  display: flex;
  gap: var(--space-1);
  margin-top: var(--space-1);
}

/* Suggestion block (right-aligned, on the user side). */
.suggest-block {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  align-self: flex-end;
  margin-top: var(--space-2);
  padding-right: var(--space-3);
  border-right: 1px solid var(--border);
}

.suggest-label {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-muted);
  text-align: right;
}

.suggest-chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  justify-content: flex-end;
}

/* Executed-mode badge (outline/soft fill pill, label-led meaning, §3-10). */
.mode-badge {
  align-self: flex-start;
  margin-top: var(--space-2);
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--header-chip);
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-muted);
}

/* Sources block (left-aligned, attached to the answer). */
.sources-block {
  display: flex;
  flex-direction: column;
  gap: var(--space-1);
  align-self: flex-start;
  margin-top: var(--space-2);
  padding-left: var(--space-3);
  border-left: 1px solid var(--border);
}

.sources-label {
  margin: 0;
  font-size: var(--text-xs);
  font-weight: 500;
  color: var(--text-muted);
}

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

.source-item {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2);
}

/* Per-source ordinal, mirrors the answer's [출처 N] markers so a citation maps to a chip. */
.source-index {
  font-size: var(--text-xs);
  font-weight: 600;
  color: var(--text-muted);
}

/* Citation "title#chunk" = an outline chip / clickable button (opens source panel). */
.source-cite {
  font-family: inherit;
  font-size: var(--text-sm);
  color: var(--text);
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

.source-cite:hover {
  background: var(--header-chip);
}

.source-cite:active {
  transform: translateY(1px);
}

.source-distance {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* ===================================================================================== *
 * Agentic step panel (cycle 60, design_spec §4 / ADR-470). Same visual language as        *
 * .sources-block (left border-left, muted, answer-aligned). Existing tokens only; no new   *
 * :root token / hex (AC-D4). All text is textContent (single innerHTML sink unchanged).   *
 * ===================================================================================== */
.steps-panel {
  align-self: flex-start;
  margin-top: var(--space-2);
  padding-left: var(--space-3);
  border-left: 1px solid var(--border);
  max-width: 66ch; /* answer-width rhythm (§2) */
}

/* summary = the always-visible disclosure row. >=44px touch target via padding (§2). The native
   marker stays (no list-style:none) so the expand affordance is keyboard/SR-native (§6). */
.steps-summary {
  padding: var(--space-2) 0;
  font-size: var(--text-sm);
  font-weight: 500;
  color: var(--text);
  cursor: pointer;
  border-radius: var(--radius-md);
  transition: color var(--dur-fast) var(--ease-out);
}

.steps-summary:hover {
  color: var(--text);
}

.steps-summary:active {
  transform: translateY(1px);
}

/* The timeline list. Fades in on open (opacity only, no height/top anim, so no layout-shift; the
   global prefers-reduced-motion block neutralizes it, §5 / AC-D7). */
.steps-panel[open] .steps-list {
  animation: steps-fade var(--dur-base) var(--ease-out);
}

@keyframes steps-fade {
  from {
    opacity: 0;
  }
  to {
    opacity: 1;
  }
}

.steps-list {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  margin: var(--space-1) 0 var(--space-2) 0;
  padding-left: var(--space-4);
}

/* One step = an icon + label (+ optional muted detail). The vertical rhythm comes from the list
   gap; no ::before connector line that could shift layout (§2). */
.step-item {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-2);
  font-size: var(--text-sm);
  color: var(--text);
}

.step-icon {
  font-size: var(--text-sm);
  line-height: var(--leading-tight);
}

.step-label {
  font-weight: 500;
}

.step-detail {
  font-size: var(--text-xs);
  color: var(--text-muted);
}

/* grade_generation status emphasis: color is reinforcement only (the icon + label carry meaning,
   §4-2, color-free). >=4.5 on --surface (success 5.95 / danger 5.60, §1-1). */
.step--ok .step-label {
  color: var(--success);
}

.step--warn .step-label {
  color: var(--danger);
}

/* ----- Source-body preview panel (cycle 43) -- new grid placement (§3-11) ------------ */
.source-panel {
  background: var(--surface);
  border: 1px solid var(--border-soft);
  border-radius: var(--radius-lg);
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  overflow-y: auto;
  max-height: 40vh;
  animation: attach-preview-fade var(--dur-base) var(--ease-out);
}

.source-panel[hidden] {
  display: none;
}

@media (min-width: 900px) {
  .source-panel {
    max-height: 50vh;
  }
}

.source-panel-head {
  display: flex;
  align-items: center;
  gap: var(--space-3);
}

.source-panel-title {
  margin: 0;
  font-size: var(--text-base);
  font-weight: 600;
  color: var(--text);
}

.source-panel-close {
  margin-left: auto;
}

.source-panel-cite {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
  word-break: break-word;
}

.source-panel-body {
  font-size: var(--text-sm);
  line-height: var(--leading-relaxed);
  color: var(--text);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-y: auto;
}

/* ----- Usage lines ------------------------------------------------------------------ */
.usage-line {
  align-self: flex-start;
  margin: var(--space-2) 0 0;
  padding-left: var(--space-3);
  border-left: 1px solid var(--border);
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.usage-total {
  margin: 0;
  font-size: var(--text-xs);
  color: var(--text-muted);
}

.usage-total[hidden] {
  display: none;
}

/* ----- Save row --------------------------------------------------------------------- */
.save-row {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
  margin-top: var(--space-2);
}

#save-title {
  flex: 1;
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  font-size: var(--text-base);
  min-height: 44px;
  background: var(--surface);
  color: var(--text);
}

#save-tags-input {
  padding: var(--space-2) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  font-size: var(--text-sm);
  min-height: 44px;
  background: var(--surface);
  color: var(--text);
}

#save-title:disabled,
#save-tags-input:disabled {
  background: var(--bg);
  color: var(--text-muted);
  cursor: not-allowed;
}

#save-button {
  padding: var(--space-2) var(--space-4);
  border: none;
  border-radius: var(--radius-full);
  background: var(--accent);
  color: var(--accent-text);
  font-size: var(--text-base);
  cursor: pointer;
  min-height: 44px;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

#save-button:hover {
  background: var(--accent-hover);
}

#save-button:active {
  background: var(--accent-active);
  transform: translateY(1px);
}

#save-button:disabled {
  background: var(--disabled-bg);
  color: var(--disabled-text);
  cursor: not-allowed;
}

/* ----- Chips: suggest / history actions / tag-remove ------------------------- */
.suggest-chip,
.history-load,
.history-delete,
.tag-chip-remove {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: var(--space-1) var(--space-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-full);
  background: var(--surface);
  color: var(--text);
  font-size: var(--text-sm);
  cursor: pointer;
  transition: background-color var(--dur-fast) var(--ease-out),
    transform var(--dur-fast) var(--ease-out);
}

.history-delete,
.tag-chip-remove {
  min-width: 44px;
  padding: var(--space-1) var(--space-2);
}

.suggest-chip:hover,
.history-load:hover,
.history-delete:hover,
.tag-chip-remove:hover {
  background: var(--header-chip);
}

.suggest-chip:active,
.history-load:active,
.history-delete:active,
.tag-chip-remove:active {
  transform: translateY(1px);
}

.suggest-chip:disabled,
.history-load:disabled,
.history-delete:disabled {
  color: var(--disabled-bg);
  cursor: not-allowed;
}

.tag-chip-remove {
  min-height: var(--space-5);
  min-width: var(--space-5);
  padding: 0 var(--space-1);
  border: none;
  border-radius: var(--radius-full);
  background: transparent;
}

.tag-chip-remove:hover {
  background: var(--border-soft);
}

/* ----- Status lines (history / save) ----------------------------------------- */
.history-status,
.save-status {
  margin: 0;
  font-size: var(--text-sm);
  color: var(--text-muted);
}

.history-status[hidden],
.save-status[hidden] {
  display: none;
}

.status-danger {
  color: var(--danger);
}
