/* Layout only — all colors/spacing/typography/shadows reference the copied
   SecDim design-system tokens (vendor/design-system/tokens/*.css). */

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--surface-page);
  color: var(--text-body);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  line-height: var(--lh-normal);
}

/* ---- Auth gate ---- */
/* `display: flex` here would otherwise beat the UA default `[hidden] {
   display: none }` on specificity, leaving the full-viewport overlay
   rendered (and intercepting clicks) even while the `hidden` attribute is
   set. Only apply flex layout when NOT hidden. */
.auth-gate {
  position: fixed;
  inset: 0;
  align-items: center;
  justify-content: center;
  background: var(--backdrop);
  z-index: var(--z-modal);
}

.auth-gate:not([hidden]) {
  display: flex;
}

.auth-card {
  background: var(--surface-card);
  border: var(--border-width) solid var(--border-subtle);
  border-radius: var(--radius-card);
  box-shadow: var(--shadow-lg);
  padding: var(--space-8);
  max-width: 420px;
  text-align: center;
}

.auth-card h1 {
  font-family: var(--font-display);
  font-size: var(--fs-lg);
  color: var(--text-strong);
  margin: 0 0 var(--space-3);
}

.auth-card p {
  color: var(--text-muted);
  margin: 0 0 var(--space-4);
}

.auth-card p.auth-message {
  color: var(--sd-danger-fg);
}

.auth-form {
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
}

.auth-form .composer-input {
  text-align: center;
}

.auth-form .btn {
  height: var(--control-md);
}

.auth-disclaimer {
  margin: var(--space-4) 0 0;
  color: var(--text-subtle);
  font-size: var(--fs-xs);
  line-height: 1.4;
}

.auth-disclaimer a {
  color: var(--text-subtle);
  text-decoration: underline;
}

.auth-disclaimer a:hover {
  color: var(--sd-primary);
}

/* ---- App shell ---- */
.app {
  flex-direction: column;
  height: 100vh;
}

.app:not([hidden]) {
  display: flex;
}

.app-header {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  padding: var(--space-4) var(--space-8);
  background: var(--surface-card);
  border-bottom: var(--border-width) solid var(--border-subtle);
  box-shadow: var(--shadow-xs);
}

.sidebar-toggle {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  background: none;
  border: var(--border-width) solid transparent;
  border-radius: var(--radius-control);
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard), background var(--dur-fast) var(--ease-standard);
}

.sidebar-toggle:hover,
.sidebar-toggle.is-active {
  background: var(--surface-sunken);
  border-color: var(--border-default);
  color: var(--sd-cyan-700);
}

.brand-logo {
  display: block;
  border-radius: var(--radius-xs);
}

.brand {
  font-family: var(--font-display);
  font-weight: var(--fw-semibold);
  font-size: var(--fs-md);
  color: var(--text-strong);
}

.brand-sub {
  color: var(--text-muted);
  font-size: var(--fs-xs);
  flex: 1;
}

.trace-toggle {
  position: relative;
  background: none;
  border: var(--border-width) solid var(--border-default);
  border-radius: var(--radius-control);
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-body);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}

.trace-toggle:hover,
.trace-toggle.is-active {
  border-color: var(--sd-cyan-400);
  color: var(--sd-cyan-700);
}

/* A brief pulsing dot on the (usually closed) trace drawer's toggle button,
   so tool activity is noticeable even without opening the drawer. */
.trace-toggle-dot {
  display: none;
  position: absolute;
  top: -3px;
  right: -3px;
  width: 8px;
  height: 8px;
  border-radius: var(--radius-pill);
  background: var(--sd-primary);
}

.trace-toggle.has-activity .trace-toggle-dot {
  display: block;
  animation: trace-dot-pulse 1s ease-in-out infinite;
}

@keyframes trace-dot-pulse {
  0%, 100% { opacity: 1; transform: scale(1); }
  50% { opacity: 0.35; transform: scale(1.4); }
}

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

/* ---- Chat pane ---- */
.chat-pane {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  padding: 0 var(--space-8);
}

/* Everything above the footer. Normally chat-log (flex:1) grows to fill,
   pinning suggestions/composer to the bottom. In welcome mode (no
   messages yet — see setWelcomeMode in history.js) chat-log is hidden and
   this becomes a centered column instead, so the greeting + composer sit
   in the middle of the screen rather than at the bottom. */
.chat-pane-main {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat-pane-main.is-welcome {
  justify-content: center;
  align-items: center;
}

.chat-pane-main.is-welcome .chat-log {
  display: none;
}

.welcome-center {
  display: none;
  text-align: center;
  margin-bottom: var(--space-6);
}

.chat-pane-main.is-welcome .welcome-center {
  display: block;
}

.welcome-heading {
  font-family: var(--font-display);
  font-weight: var(--fw-bold);
  font-size: var(--fs-4xl);
  color: var(--text-strong);
  margin: 0 0 var(--space-3);
}

.welcome-subheading {
  font-size: var(--fs-md);
  color: var(--text-muted);
  margin: 0;
}

.chat-pane-main.is-welcome .suggestions,
.chat-pane-main.is-welcome .composer {
  width: 100%;
  max-width: 640px;
}

.chat-log {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-6) 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: var(--space-4);
}

.message {
  width: 100%;
  max-width: 72ch;
  flex-shrink: 0;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-card);
  border: var(--border-width) solid var(--border-subtle);
  background: var(--surface-card);
  box-shadow: var(--shadow-xs);
  white-space: pre-wrap;
  animation: message-fade-in 0.25s var(--ease-out);
}

.message-user {
  align-self: flex-end;
  background: var(--surface-primary-subtle);
  border-color: var(--sd-cyan-200);
}

.message-assistant {
  align-self: flex-start;
}

/* A turn's content streams live into a normal-looking bubble from its very
   first token (optimistic default: it might be the final answer). The
   instant the server learns otherwise — almost always while still
   streaming, see tool_calls_detected in app.js — this class is added to
   that SAME element: no text ever moves, only its styling dims down to
   read as pre-tool-call narration rather than the response. */
.message-narration {
  background: var(--surface-sunken);
  border-style: dashed;
  border-color: var(--border-default);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-style: italic;
}

.message-error {
  border-color: var(--sd-danger);
  background: var(--sd-danger-bg);
  color: var(--sd-danger-fg);
}

/* Standalone "still working" indicator for the gap between hitting send and
   the first visible content of any kind — including a tool call with no
   narration at all, which otherwise leaves the log blank while it runs.
   Removed the instant real content appears (see hideTypingIndicator()). */
.typing-indicator {
  display: flex;
  align-self: flex-start;
  flex-shrink: 0;
  gap: 4px;
  padding: var(--space-3) var(--space-4);
  border-radius: var(--radius-card);
  background: var(--surface-sunken);
  animation: message-fade-in 0.25s var(--ease-out);
}

.typing-indicator span {
  width: 6px;
  height: 6px;
  border-radius: var(--radius-pill);
  background: var(--text-subtle);
  opacity: 0.3;
  animation: thinking-dot-pulse 1.2s ease-in-out infinite;
}

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

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

@keyframes thinking-dot-pulse {
  0%, 60%, 100% { opacity: 0.3; transform: scale(1); }
  30% { opacity: 1; transform: scale(1.3); }
}

/* Thinking: the reasoning channel only (e.g. DeepSeek's reasoning_content)
   — unambiguous, never the final answer, so it's always safe to collect
   into one collapsible container and stream live with no relabeling. */
.thinking-container {
  width: 100%;
  max-width: 72ch;
  flex-shrink: 0;
  align-self: flex-start;
  border: var(--border-width) solid var(--border-subtle);
  border-radius: var(--radius-card);
  background: var(--surface-sunken);
  overflow: hidden;
  animation: message-fade-in 0.25s var(--ease-out);
}

@keyframes message-fade-in {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.thinking-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  cursor: pointer;
  background: none;
  border: none;
  width: 100%;
  font-family: var(--font-body);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--text-muted);
  text-align: left;
}

.thinking-chevron {
  display: inline-block;
  transition: transform var(--dur-fast) var(--ease-standard);
  color: var(--text-subtle);
}

.thinking-container.is-expanded .thinking-chevron {
  transform: rotate(90deg);
}

.thinking-body {
  max-height: 220px;
  overflow-y: auto;
  padding: 0 var(--space-3) var(--space-3);
  color: var(--text-muted);
  font-size: var(--fs-xs);
  font-style: italic;
  white-space: pre-wrap;
}

.thinking-container:not(.is-expanded) .thinking-body {
  display: none;
}

/* ---- Starter suggestions ---- */
.suggestions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  padding: 0 0 var(--space-3);
  justify-content: center;
}

.suggestions[hidden] {
  display: none;
}

.suggestion-chip {
  background: var(--surface-card);
  border: var(--border-width) solid var(--border-default);
  color: var(--text-body);
  border-radius: var(--radius-pill);
  padding: var(--space-2) var(--space-4);
  font-family: var(--font-body);
  font-size: var(--fs-xs);
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-standard), background var(--dur-fast) var(--ease-standard);
}

.suggestion-chip:hover {
  border-color: var(--sd-cyan-300);
  background: var(--surface-primary-subtle);
}

.md-table {
  border-collapse: collapse;
  margin: var(--space-2) 0;
  font-size: var(--fs-xs);
}

.md-table th,
.md-table td {
  border: var(--border-width) solid var(--border-subtle);
  padding: var(--space-1) var(--space-2);
  text-align: left;
}

.md-table th {
  background: var(--surface-sunken);
  color: var(--text-strong);
  font-weight: var(--fw-semibold);
}

.message code,
.thinking-body code {
  font-family: var(--font-mono);
  background: var(--surface-sunken);
  padding: 1px 4px;
  border-radius: var(--radius-xs);
  font-size: 0.9em;
}

.message .md-p,
.thinking-body .md-p {
  margin: 0 0 var(--space-3);
}

.message .md-heading,
.thinking-body .md-heading {
  font-family: var(--font-display);
  font-weight: var(--fw-semibold);
  color: var(--text-strong);
  line-height: var(--lh-snug);
  margin: var(--space-4) 0 var(--space-2);
}

.message h1.md-heading, .thinking-body h1.md-heading { font-size: var(--fs-lg); }
.message h2.md-heading, .thinking-body h2.md-heading { font-size: var(--fs-md); }
.message h3.md-heading, .thinking-body h3.md-heading { font-size: var(--fs-base); font-weight: var(--fw-semibold); }
.message h4.md-heading, .thinking-body h4.md-heading,
.message h5.md-heading, .thinking-body h5.md-heading,
.message h6.md-heading, .thinking-body h6.md-heading { font-size: var(--fs-sm); text-transform: uppercase; letter-spacing: var(--ls-wide); color: var(--text-muted); }

.message .md-list,
.thinking-body .md-list {
  margin: 0 0 var(--space-3);
  padding-left: var(--space-5);
}

.message .md-list li,
.thinking-body .md-list li {
  margin-bottom: var(--space-1);
}

.message .md-hr,
.thinking-body .md-hr {
  border: none;
  border-top: var(--border-width) solid var(--border-subtle);
  margin: var(--space-4) 0;
}

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

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

.composer {
  position: relative;
  display: flex;
  gap: var(--space-3);
  padding: var(--space-3);
  margin-bottom: var(--space-2);
  border: var(--border-width) solid var(--border-default);
  border-radius: var(--radius-card);
  background: var(--surface-card);
  box-shadow: var(--shadow-xs);
}

/* While waiting on a response: a glowing line travels around the composer
   border, plus a soft pulsing glow, so it's obvious something is in flight
   even before any text or trace activity appears. Traced with a real SVG
   rect stroke (stroke-dasharray/dashoffset), not a CSS gradient-mask trick —
   the mask-composite approach silently failed to clip to a ring at all and
   only ever showed a stray fragment, which is not something to repeat. Using
   pathLength="100" on the <rect> (see index.html) makes the dash math
   resolution-independent: percentages of the perimeter, not pixels. */
.composer.is-loading {
  animation: composer-glow-pulse 1.6s ease-in-out infinite;
}

.composer-glow-border {
  display: none;
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  overflow: visible;
}

.composer.is-loading .composer-glow-border {
  display: block;
}

.composer-glow-border rect {
  x: 1px;
  y: 1px;
  width: calc(100% - 2px);
  height: calc(100% - 2px);
  rx: 14px;
  fill: none;
  stroke: var(--sd-primary);
  stroke-width: 2px;
  stroke-linecap: round;
  stroke-dasharray: 18 82;
  filter: drop-shadow(0 0 3px var(--sd-primary));
  animation: composer-border-travel 1.8s linear infinite;
}

@keyframes composer-border-travel {
  to {
    stroke-dashoffset: -100;
  }
}

@keyframes composer-glow-pulse {
  0%, 100% { box-shadow: var(--shadow-xs); }
  50% { box-shadow: var(--glow-primary-soft); }
}

.app-footer {
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-items: baseline;
  gap: var(--space-3);
  padding: 0 var(--space-1) var(--space-5);
  font-size: var(--fs-2xs);
  color: var(--text-subtle);
}

.app-footer-disclaimer a {
  color: var(--text-subtle);
  text-decoration: underline;
}

.app-footer-disclaimer a:hover {
  color: var(--sd-primary);
}

.composer-input {
  flex: 1;
  resize: none;
  border: var(--border-width) solid var(--border-default);
  border-radius: var(--radius-control);
  padding: var(--space-3);
  font-family: var(--font-body);
  font-size: var(--fs-sm);
  color: var(--text-body);
}

.composer-input:focus {
  outline: none;
  border-color: var(--border-focus);
  box-shadow: var(--focus-ring);
}

.btn {
  border: none;
  border-radius: var(--radius-control);
  padding: 0 var(--space-5);
  font-family: var(--font-body);
  font-weight: var(--fw-semibold);
  font-size: var(--fs-sm);
  cursor: pointer;
  transition: background var(--dur-fast) var(--ease-standard);
}

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

.btn-primary:hover {
  background: var(--accent-hover);
}

.btn-primary:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---- History pane: hidden-by-default slide-in drawer on the LEFT,
   listing saved chats (localStorage-only — the server never sees or
   stores this) — toggled via the header "Chats" button, mirroring the
   tool-trace drawer's pattern on the right. ---- */
.history-backdrop:not([hidden]) {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  z-index: var(--z-overlay);
}

.history-pane {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(300px, 100vw);
  transform: translateX(-100%);
  transition: transform var(--dur-slow) var(--ease-standard);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--surface-card);
  box-shadow: var(--shadow-xl);
  z-index: var(--z-modal);
}

.history-pane.is-open {
  transform: translateX(0);
}

.history-pane-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: var(--space-2);
  padding: var(--space-4);
  border-bottom: var(--border-width) solid var(--border-subtle);
  font-size: var(--fs-xs);
  font-weight: var(--fw-semibold);
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: var(--ls-wide);
}

.history-close-btn {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--text-subtle);
  font-size: var(--fs-lg);
  line-height: 1;
  cursor: pointer;
  padding: 0 var(--space-1);
  text-transform: none;
}

.history-close-btn:hover {
  color: var(--text-body);
}

.history-footnote {
  padding: var(--space-3) var(--space-4);
  margin: 0;
  border-top: var(--border-width) solid var(--border-subtle);
  color: var(--text-subtle);
  font-size: var(--fs-2xs);
  line-height: 1.4;
}

.history-new-btn {
  background: none;
  border: var(--border-width) solid var(--border-default);
  border-radius: var(--radius-control);
  padding: var(--space-1) var(--space-3);
  font-family: var(--font-body);
  font-size: var(--fs-xs);
  font-weight: var(--fw-medium);
  color: var(--text-body);
  text-transform: none;
  letter-spacing: normal;
  cursor: pointer;
  transition: border-color var(--dur-fast) var(--ease-standard), color var(--dur-fast) var(--ease-standard);
}

.history-new-btn:hover {
  border-color: var(--sd-cyan-400);
  color: var(--sd-cyan-700);
}

.history-list {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-2);
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.history-empty {
  padding: var(--space-4);
  color: var(--text-subtle);
  font-size: var(--fs-xs);
  text-align: center;
}

.history-item {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  border-radius: var(--radius-control);
  border: var(--border-width) solid transparent;
  cursor: pointer;
  font-size: var(--fs-xs);
  color: var(--text-body);
}

.history-item:hover {
  background: var(--surface-sunken);
}

.history-item.is-active {
  background: var(--surface-primary-subtle);
  border-color: var(--sd-cyan-200);
}

.history-item-title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.history-item-delete {
  flex-shrink: 0;
  background: none;
  border: none;
  color: var(--text-subtle);
  font-size: var(--fs-sm);
  line-height: 1;
  padding: 0 var(--space-1);
  cursor: pointer;
  visibility: hidden;
}

.history-item:hover .history-item-delete {
  visibility: visible;
}

.history-item-delete:hover {
  color: var(--sd-danger-fg);
}

/* ---- Trace pane: hidden-by-default slide-in drawer (CodeBlock-styled),
   not a permanent sidebar — toggled via the header button. ---- */
.trace-backdrop:not([hidden]) {
  position: fixed;
  inset: 0;
  background: var(--backdrop);
  z-index: var(--z-overlay);
}

.trace-pane {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  width: min(420px, 100vw);
  transform: translateX(100%);
  transition: transform var(--dur-slow) var(--ease-standard);
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--code-bg);
  color: var(--code-fg);
  box-shadow: var(--shadow-xl);
  z-index: var(--z-modal);
}

.trace-pane.is-open {
  transform: translateX(0);
}

.trace-close {
  margin-left: auto;
  background: none;
  border: none;
  color: var(--code-muted);
  font-size: var(--fs-lg);
  line-height: 1;
  cursor: pointer;
  padding: 0 var(--space-1);
}

.trace-close:hover {
  color: var(--code-fg);
}

.trace-pane-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-4);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
  letter-spacing: var(--ls-wide);
  text-transform: uppercase;
  color: var(--code-muted);
  border-bottom: var(--border-width) solid var(--code-line);
}

.trace-log {
  flex: 1;
  overflow-y: auto;
  padding: var(--space-3);
  display: flex;
  flex-direction: column;
  gap: var(--space-3);
  font-family: var(--font-mono);
  font-size: var(--fs-xs);
}

.trace-entry {
  flex-shrink: 0;
  border: var(--border-width) solid var(--code-line);
  border-radius: var(--radius-md);
  overflow: hidden;
}

/* Flashed on creation and again when a result lands, so new tool activity
   is visually obvious in the trace log, not just silently appended. */
.trace-flash {
  animation: trace-entry-flash-in 0.8s ease-out;
}

@keyframes trace-entry-flash-in {
  0% {
    background-color: rgba(85, 193, 226, 0.28);
    border-color: var(--sd-cyan-400);
  }
  100% {
    background-color: transparent;
    border-color: var(--code-line);
  }
}

.trace-entry-header {
  display: flex;
  align-items: center;
  gap: var(--space-2);
  padding: var(--space-2) var(--space-3);
  background: rgba(255, 255, 255, 0.03);
}

.trace-dots {
  display: flex;
  gap: 4px;
}

.trace-dot {
  width: 8px;
  height: 8px;
  border-radius: var(--radius-pill);
}

.trace-dot-red { background: var(--code-red); }
.trace-dot-amber { background: var(--code-amber); }
.trace-dot-green { background: var(--code-green); }

.trace-tool-name {
  color: var(--code-cyan);
  font-weight: var(--fw-medium);
}

.trace-badge {
  margin-left: auto;
  padding: 2px var(--space-2);
  border-radius: var(--radius-pill);
  font-size: var(--fs-2xs);
  text-transform: uppercase;
  letter-spacing: var(--ls-wide);
}

.trace-badge-mcp {
  background: var(--sd-cyan-800);
  color: var(--sd-cyan-100);
}

.trace-badge-rest {
  background: var(--sd-slate-700);
  color: var(--sd-slate-100);
}

.trace-badge-schema {
  background: var(--sd-slate-600);
  color: var(--sd-slate-50);
}

.trace-entry-body {
  padding: var(--space-2) var(--space-3);
  color: var(--code-fg);
  word-break: break-word;
}

.trace-meta {
  color: var(--code-muted);
}

.trace-result-ok { color: var(--code-green); }
.trace-result-error { color: var(--code-red); }

/* ---- Spinner ---- */
.spinner[hidden] {
  display: none;
}

.spinner {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid rgba(85, 193, 226, 0.25);
  border-top-color: var(--sd-primary);
  animation: sd-spin 0.7s linear infinite;
}

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