:root {
  --chat-bg: #f5f5f5;
  --chat-surface: #ffffff;
  --chat-border: #e5e5e5;
  --chat-text: #171717;
  --chat-muted: #737373;
  --chat-accent: #10a37f;
  --chat-accent-hover: #0d8f6e;
  --chat-focus-ring: rgba(16, 163, 127, 0.25);
  --chat-error: #dc2626;
  --chat-toggle-hover: rgba(0, 0, 0, 0.04);
}

html {
  height: 100%;
}

html[data-theme="dark"] {
  --chat-bg: #212121;
  --chat-surface: #2f2f2f;
  --chat-border: #424242;
  --chat-text: #ececec;
  --chat-muted: #a0a0a0;
  --chat-focus-ring: rgba(16, 163, 127, 0.25);
  --chat-error: #f87171;
  --chat-toggle-hover: rgba(255, 255, 255, 0.06);
}

.chat-app {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background: var(--chat-bg);
  color: var(--chat-text);
  margin: 0;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  border-bottom: 1px solid var(--chat-border);
  padding: 0.75rem 1.25rem;
  min-height: 3.25rem;
}

.chat-brand {
  font-size: 1rem;
  font-weight: 600;
  margin-right: auto;
}

.theme-toggle {
  background: var(--chat-surface);
  color: var(--chat-text);
  border: 1px solid var(--chat-border);
  border-radius: 0.5rem;
  padding: 0.4rem 0.75rem;
  font-size: 0.8125rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
}

.theme-toggle:hover {
  background: var(--chat-toggle-hover);
  border-color: var(--chat-muted);
}

.theme-toggle:focus-visible {
  outline: none;
  box-shadow: 0 0 0 2px var(--chat-focus-ring);
}

.chat-main {
  flex: 1 1 auto;
  width: min(1200px, 100%);
  margin: 0 auto;
  padding: 1rem;
  display: flex;
  min-height: 0;
  overflow: hidden;
}

.chat-shell {
  width: 100%;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 1rem;
  height: 100%;
  min-height: 0;
}

.chat-sidebar {
  background: var(--chat-surface);
  border: 1px solid var(--chat-border);
  border-radius: 0.75rem;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  min-height: 0;
  overflow: hidden;
}

.chat-new {
  border: 1px solid var(--chat-border);
  background: transparent;
  color: var(--chat-text);
  border-radius: 0.5rem;
  padding: 0.625rem 0.75rem;
  text-align: left;
  cursor: pointer;
}

.chat-new:hover:not(:disabled) {
  background: var(--chat-toggle-hover);
}

.chat-new:disabled {
  opacity: 0.5;
}

.chat-session-list {
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-y: auto;
}

.provider-panel {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
  border: 1px solid var(--chat-border);
  border-radius: 0.5rem;
  padding: 0.6rem;
}

.provider-panel label {
  font-size: 0.78rem;
  color: var(--chat-muted);
}

.provider-panel select,
.provider-panel input {
  width: 100%;
  border: 1px solid var(--chat-border);
  background: var(--chat-surface);
  color: var(--chat-text);
  border-radius: 0.45rem;
  padding: 0.45rem 0.55rem;
  font-size: 0.84rem;
}

.provider-panel select:focus,
.provider-panel input:focus {
  outline: none;
  border-color: var(--chat-accent);
  box-shadow: 0 0 0 2px var(--chat-focus-ring);
}

.chat-session {
  border: 1px solid transparent;
  background: transparent;
  color: var(--chat-text);
  border-radius: 0.5rem;
  text-align: left;
  padding: 0.55rem 0.65rem;
  cursor: pointer;
  font-size: 0.9rem;
}

.chat-session:hover {
  background: var(--chat-toggle-hover);
}

.chat-session.active {
  border-color: var(--chat-border);
  background: var(--chat-toggle-hover);
}

.chat-stage {
  min-width: 0;
  display: flex;
  flex-direction: column;
  border: 1px solid var(--chat-border);
  background: var(--chat-surface);
  border-radius: 0.75rem;
  min-height: 0;
  overflow: hidden;
}

.chat-status-row {
  min-height: 1.6rem;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
  padding: 0 0.15rem;
}

.chat-thread {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

.chat-empty {
  margin: auto;
  color: var(--chat-muted);
  font-size: 0.95rem;
}

.chat-msg {
  display: flex;
}

.chat-msg-user {
  justify-content: flex-end;
}

.chat-msg-assistant {
  justify-content: flex-start;
}

.chat-bubble {
  max-width: min(90%, 780px);
  line-height: 1.5;
  border-radius: 0.85rem;
  border: 1px solid var(--chat-border);
  padding: 0.7rem 0.85rem;
  white-space: normal;
  overflow-wrap: anywhere;
}

.chat-msg-user .chat-bubble {
  background: #e8f8f3;
  border-color: #c9efe3;
}

/* Markdown content inside assistant bubbles */
.chat-bubble-md { line-height: 1.6; }
.chat-bubble-md > *:first-child { margin-top: 0; }
.chat-bubble-md > *:last-child  { margin-bottom: 0; }

.chat-bubble-md p          { margin: 0.5em 0; }
.chat-bubble-md h1,
.chat-bubble-md h2,
.chat-bubble-md h3,
.chat-bubble-md h4         { margin: 0.8em 0 0.3em; font-weight: 600; line-height: 1.3; }
.chat-bubble-md h1         { font-size: 1.25em; }
.chat-bubble-md h2         { font-size: 1.1em; }
.chat-bubble-md h3         { font-size: 1em; }

.chat-bubble-md ul,
.chat-bubble-md ol         { margin: 0.4em 0; padding-left: 1.5em; }
.chat-bubble-md li         { margin: 0.15em 0; }

.chat-bubble-md code {
  font-family: ui-monospace, "Cascadia Code", "Fira Code", Consolas, monospace;
  font-size: 0.875em;
  background: rgba(0,0,0,0.06);
  border-radius: 0.3em;
  padding: 0.1em 0.35em;
}
html[data-theme="dark"] .chat-bubble-md code {
  background: rgba(255,255,255,0.1);
}

.chat-bubble-md pre {
  margin: 0.6em 0;
  border-radius: 0.5rem;
  background: #1e1e1e;
  overflow-x: auto;
}
.chat-bubble-md pre code {
  display: block;
  padding: 0.75em 1em;
  background: transparent;
  color: #d4d4d4;
  font-size: 0.84em;
  border-radius: 0;
  white-space: pre;
}

.chat-bubble-md blockquote {
  margin: 0.5em 0;
  padding: 0.3em 0 0.3em 0.85em;
  border-left: 3px solid var(--chat-border);
  color: var(--chat-muted);
}

.chat-bubble-md table {
  border-collapse: collapse;
  margin: 0.5em 0;
  font-size: 0.9em;
  width: max-content;
  max-width: 100%;
  overflow-x: auto;
  display: block;
}
.chat-bubble-md th,
.chat-bubble-md td {
  border: 1px solid var(--chat-border);
  padding: 0.35em 0.65em;
  text-align: left;
}
.chat-bubble-md th { background: rgba(0,0,0,0.04); font-weight: 600; }
html[data-theme="dark"] .chat-bubble-md th { background: rgba(255,255,255,0.06); }

.chat-bubble-md hr {
  border: none;
  border-top: 1px solid var(--chat-border);
  margin: 0.75em 0;
}

.chat-bubble-md a { color: var(--chat-accent); text-decoration: underline; }

.chat-bubble mjx-container[display="true"] {
  overflow-x: auto;
  overflow-y: hidden;
  max-width: 100%;
  padding: 0.25rem 0;
}

html[data-theme="dark"] .chat-msg-user .chat-bubble {
  background: #113a30;
  border-color: #205245;
}

.chat-composer {
  flex: 0 0 auto;
  border-top: 1px solid var(--chat-border);
  padding: 0.8rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
}

.chat-composer textarea {
  width: 100%;
  resize: none;
  border: 1px solid var(--chat-border);
  border-radius: 0.8rem;
  background: var(--chat-surface);
  color: var(--chat-text);
  padding: 0.75rem 0.9rem;
  font-size: 0.9375rem;
  line-height: 1.5;
  font-family: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 2.8rem;
  max-height: 11.25rem;
}

.chat-composer textarea:focus {
  outline: none;
  border-color: var(--chat-accent);
  box-shadow: 0 0 0 2px var(--chat-focus-ring);
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  justify-content: flex-end;
}

.chat-send {
  background: var(--chat-accent);
  color: #fff;
  border: none;
  border-radius: 0.5rem;
  padding: 0.625rem 1.25rem;
  font-size: 0.9375rem;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s ease;
}

.chat-send:hover:not(:disabled) {
  background: var(--chat-accent-hover);
}

.chat-send:disabled,
.chat-stop:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.chat-stop {
  background: transparent;
  color: var(--chat-text);
  border: 1px solid var(--chat-border);
  border-radius: 0.5rem;
  padding: 0.625rem 1rem;
  font-size: 0.9375rem;
  cursor: pointer;
}

.chat-stop:hover:not(:disabled) {
  background: var(--chat-toggle-hover);
}

.chat-status {
  font-size: 0.8125rem;
  color: var(--chat-muted);
  overflow-wrap: anywhere;
}

.chat-status.error {
  color: var(--chat-error);
}

.chat-log-link {
  border: 1px solid var(--chat-border);
  background: transparent;
  color: var(--chat-text);
  border-radius: 0.45rem;
  font-size: 0.78rem;
  padding: 0.25rem 0.6rem;
  cursor: pointer;
  white-space: nowrap;
}

.chat-log-link:hover {
  background: var(--chat-toggle-hover);
}

.hidden {
  display: none !important;
}

.doc-upload-row {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.doc-upload-btn {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--chat-border);
  background: transparent;
  color: var(--chat-text);
  border-radius: 0.45rem;
  padding: 0.3rem 0.65rem;
  font-size: 0.8125rem;
  cursor: pointer;
  white-space: nowrap;
  transition: background 0.15s ease;
}

.doc-upload-btn:hover {
  background: var(--chat-toggle-hover);
}

.doc-upload-status {
  font-size: 0.78rem;
  color: var(--chat-muted);
  overflow-wrap: anywhere;
}

.doc-upload-status.error {
  color: var(--chat-error);
}

.doc-attachment {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--chat-toggle-hover);
  border: 1px solid var(--chat-border);
  border-radius: 0.5rem;
  padding: 0.35rem 0.6rem;
}

.doc-chip {
  font-size: 0.8rem;
  color: var(--chat-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 360px;
  font-weight: 500;
}

.doc-remove {
  flex-shrink: 0;
  background: transparent;
  border: 1px solid var(--chat-border);
  color: var(--chat-muted);
  cursor: pointer;
  font-size: 0.75rem;
  font-weight: 500;
  padding: 0.2rem 0.5rem;
  border-radius: 0.35rem;
  transition: color 0.15s ease, border-color 0.15s ease;
}

.doc-remove:hover {
  color: var(--chat-error);
  border-color: var(--chat-error);
}

.image-attachment {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  background: var(--chat-toggle-hover);
  border: 1px solid var(--chat-border);
  border-radius: 0.5rem;
  padding: 0.35rem 0.6rem;
}

.image-thumb {
  width: 2.5rem;
  height: 2.5rem;
  object-fit: cover;
  border-radius: 0.35rem;
  border: 1px solid var(--chat-border);
  flex-shrink: 0;
}

.image-chip {
  font-size: 0.8rem;
  color: var(--chat-text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 320px;
  font-weight: 500;
}

.chat-bubble-image {
  display: block;
  max-width: 100%;
  max-height: 320px;
  border-radius: 0.5rem;
  margin-bottom: 0.4rem;
  border: 1px solid var(--chat-border);
}

.chat-bubble-image-placeholder {
  font-size: 0.8rem;
  color: var(--chat-muted);
  font-style: italic;
  margin-bottom: 0.3rem;
}

.chat-composer.drag-over {
  background: var(--chat-focus-ring);
  border-top: 2px dashed var(--chat-accent);
}

.token-counter {
  font-size: 0.75rem;
  color: var(--chat-muted);
  white-space: nowrap;
  margin-left: auto;
  margin-right: 0.5rem;
}

.chat-footer {
  flex: 0 0 auto;
  border-top: 1px solid var(--chat-border);
  padding: 0.625rem 1rem;
  text-align: center;
  font-size: 0.75rem;
  color: var(--chat-muted);
}

.chat-footer a {
  color: var(--chat-muted);
  text-decoration: underline;
}

@media (max-width: 900px) {
  .chat-main {
    width: 100%;
    padding: 0.75rem;
  }

  .chat-shell {
    grid-template-columns: 1fr;
  }

  .chat-sidebar,
  .chat-stage {
    min-height: auto;
  }
}
