:root {
  color-scheme: light dark;
  --bg: #0f1218;
  --surface: #171b24;
  --border: #2a3142;
  --text: #e8eaef;
  --muted: #9aa3b2;
  --accent: #8b6bc9;
  --accent-hover: #a78ae0;
  --user-bubble: #243044;
  --guide-bubble: #1f1a2e;
  --danger: #e85d5d;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
}

* {
  box-sizing: border-box;
}

html {
  height: 100%;
  height: 100dvh;
}

body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

.site-header {
  flex-shrink: 0;
  width: 100%;
  padding: 0.4rem 1rem;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-sizing: border-box;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.75rem;
}

.site-header__usage {
  margin: 0;
  font-size: 0.78rem;
  opacity: 0.85;
  max-width: 14rem;
  text-align: right;
  line-height: 1.25;
}

.site-header__logout {
  flex-shrink: 0;
  font-size: 0.8rem;
  padding: 0.35rem 0.65rem;
}

.site-header__brand {
  display: flex;
  align-items: center;
  gap: 0.625rem;
}

.site-header__logo {
  flex-shrink: 0;
  height: 1.75rem;
  width: auto;
  display: block;
}

.site-header h1 {
  margin: 0;
  font-size: 1rem;
  font-weight: 600;
  line-height: 1.3;
  color: var(--text);
  text-align: left;
}

.layout {
  flex: 1 1 0;
  min-height: 0;
  overflow: hidden;
  max-width: 1240px;
  width: 100%;
  margin: 0 auto;
  padding: 0.75rem 1rem 1rem;
  display: grid;
  grid-template-columns: minmax(180px, 240px) 1fr;
  grid-template-rows: 1fr;
  grid-template-areas: "sidePanel chat";
  gap: 1.25rem;
  align-items: stretch;
  box-sizing: border-box;
}

@media (max-width: 900px) {
  .layout {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    grid-template-areas:
      "sidePanel"
      "chat";
  }

  /* Keep the chat row from collapsing when many conversation chips wrap */
  .conversations-panel {
    max-height: 45vh;
    overflow-y: auto;
  }

  .conversation-list {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
    gap: 0.5rem;
    max-height: none;
    overflow-x: auto;
    padding-bottom: 0.15rem;
  }

  .conversation-list li {
    flex: 0 0 auto;
    min-width: 0;
    max-width: 14rem;
  }

  .conversation-item {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
}

.conversations-panel {
  grid-area: sidePanel;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.65rem;
  min-height: 0;
}

.btn-new-chat {
  width: 100%;
  padding: 0.45rem 0.65rem;
  font-size: 0.85rem;
  font-weight: 600;
}

.conversation-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  overflow-y: auto;
  flex: 1 1 auto;
  min-height: 0;
}

.conversation-list li {
  margin: 0;
}

.conversation-item {
  width: 100%;
  text-align: left;
  padding: 0.5rem 0.55rem;
  font-size: 0.8rem;
  line-height: 1.35;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: stretch;
  gap: 0.2rem;
  cursor: pointer;
  font-weight: 500;
}

.conversation-item:hover:not(.active) {
  border-color: var(--muted);
}

.conversation-item.active {
  border-color: var(--accent);
  background: rgba(139, 107, 201, 0.15);
  box-shadow: 0 0 0 1px var(--accent);
}

.conversation-item.conversation-item-closed {
  border-style: dashed;
}

.conversation-item-preview {
  font-weight: 500;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  word-break: break-word;
}

.conversation-item-meta {
  font-size: 0.68rem;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.chat-panel {
  grid-area: chat;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  display: flex;
  flex-direction: column;
  min-height: 0;
  height: 100%;
}

.getting-started-hint {
  flex-shrink: 0;
  margin: 0;
  padding: 0.65rem 0.85rem;
  font-size: 0.82rem;
  line-height: 1.45;
  color: var(--muted);
  background: rgba(139, 107, 201, 0.08);
  border-bottom: 1px solid var(--border);
  border-radius: 12px 12px 0 0;
}

.getting-started-hint strong {
  color: var(--text);
  font-weight: 600;
}

.conversation-summary {
  flex-shrink: 0;
  border-bottom: 1px solid var(--border);
  padding: 0.75rem;
  background: rgba(139, 107, 201, 0.1);
}

.conversation-summary-title {
  margin: 0 0 0.4rem;
  font-size: 0.9rem;
  font-weight: 600;
}

.conversation-summary-text {
  margin: 0;
  white-space: pre-wrap;
  font-size: 0.85rem;
  line-height: 1.45;
}

.transcript {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
}

.msg {
  max-width: 92%;
  padding: 0.65rem 0.85rem;
  border-radius: 10px;
  word-break: break-word;
}

.msg.user .msg-content {
  white-space: pre-wrap;
}

.msg.assistant .msg-content--md {
  white-space: normal;
  line-height: 1.5;
}

.msg-content--md > :first-child {
  margin-top: 0;
}

.msg-content--md > :last-child {
  margin-bottom: 0;
}

.msg-content--md p {
  margin: 0.5em 0;
}

.msg-content--md h1,
.msg-content--md h2,
.msg-content--md h3,
.msg-content--md h4 {
  margin: 0.65em 0 0.4em;
  font-weight: 600;
  line-height: 1.25;
}

.msg-content--md h1 {
  font-size: 1.15rem;
}

.msg-content--md h2 {
  font-size: 1.05rem;
}

.msg-content--md h3,
.msg-content--md h4 {
  font-size: 0.98rem;
}

.msg-content--md ul,
.msg-content--md ol {
  margin: 0.45em 0;
  padding-left: 1.35rem;
}

.msg-content--md li {
  margin: 0.2em 0;
}

.msg-content--md code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.88em;
  padding: 0.12em 0.35em;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.22);
}

.msg-content--md pre {
  clear: both;
  margin: 0.55em 0;
  padding: 0.55rem 0.65rem;
  overflow-x: auto;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.28);
  border: 1px solid var(--border);
  font-size: 0.82rem;
}

.msg-content--md pre code {
  padding: 0;
  background: none;
  font-size: inherit;
}

.msg-content--md blockquote {
  margin: 0.5em 0;
  padding: 0.35em 0 0.35em 0.85rem;
  border-left: 3px solid rgba(167, 138, 224, 0.55);
  color: var(--text);
  opacity: 0.92;
}

.msg-content--md a {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.msg-content--md hr {
  margin: 0.75em 0;
  border: none;
  border-top: 1px solid var(--border);
}

.msg-content--md table {
  clear: both;
  width: 100%;
  border-collapse: collapse;
  margin: 0.55em 0;
  font-size: 0.88rem;
}

.msg-content--md th,
.msg-content--md td {
  border: 1px solid var(--border);
  padding: 0.35rem 0.5rem;
  text-align: left;
}

.msg.user {
  align-self: flex-end;
  background: var(--user-bubble);
  border: 1px solid var(--border);
}

.msg.assistant {
  align-self: flex-start;
  background: var(--guide-bubble);
  border: 1px solid var(--border);
}

.msg.assistant::after {
  content: "";
  display: table;
  clear: both;
}

.msg.msg-highlight {
  outline: 2px solid var(--accent);
  outline-offset: 3px;
  border-radius: 10px;
  transition: outline 0.15s ease;
}

.msg-meta {
  font-size: 0.7rem;
  color: var(--muted);
  margin-bottom: 0.2rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.msg-card-badge {
  display: inline-block;
  margin-bottom: 0.45rem;
  padding: 0.2rem 0.5rem;
  font-size: 0.72rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  text-transform: uppercase;
  color: #e4d9f7;
  background: rgba(139, 107, 201, 0.35);
  border: 1px solid rgba(167, 138, 224, 0.5);
  border-radius: 6px;
}

.msg-card-image {
  display: block;
  float: left;
  width: min(100%, 220px);
  max-height: 340px;
  margin: 0 0.85rem 0.55rem 0;
  border-radius: 8px;
  border: 1px solid rgba(167, 138, 224, 0.45);
  object-fit: cover;
  background: rgba(0, 0, 0, 0.18);
  cursor: zoom-in;
}

.card-image-dialog {
  margin: 0;
  padding: 0;
  border: none;
  width: 100vw;
  height: 100vh;
  max-width: none;
  max-height: none;
  background: transparent;
  box-sizing: border-box;
}

.card-image-dialog::backdrop {
  background: rgba(0, 0, 0, 0.82);
}

.card-image-dialog__shell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  padding: min(2rem, 4vw);
  box-sizing: border-box;
}

.card-image-dialog__close {
  position: absolute;
  top: min(1rem, 3vw);
  right: min(1rem, 3vw);
  z-index: 1;
}

.card-image-dialog__img {
  display: block;
  max-width: 100%;
  max-height: min(96vh, calc(100vh - 4rem));
  width: auto;
  height: auto;
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, 0.45);
}

.composer {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  padding: 0.55rem 0.65rem;
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
}

.composer-row {
  display: flex;
  align-items: stretch;
  gap: 0.5rem;
  min-width: 0;
}

.composer textarea {
  flex: 1 1 auto;
  min-width: 0;
  width: auto;
  resize: vertical;
  min-height: 3.25rem;
  padding: 0.5rem 0.65rem;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font: inherit;
}

.composer textarea:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.composer-actions {
  display: flex;
  flex-direction: column;
  flex-shrink: 0;
  justify-content: center;
  gap: 0.5rem;
}

.composer-closed-hint {
  margin: 0;
}

.hint {
  font-size: 0.85rem;
  color: var(--muted);
}

button {
  font: inherit;
  cursor: pointer;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: var(--accent);
  color: #fff;
  padding: 0.45rem 0.85rem;
  font-weight: 600;
}

button:hover:not(:disabled) {
  background: var(--accent-hover);
}

button:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-secondary {
  background: transparent;
  color: var(--text);
}

.btn-secondary:hover:not(:disabled) {
  background: rgba(255, 255, 255, 0.06);
}

.error-banner {
  align-self: center;
  max-width: 100%;
  padding: 0.5rem 0.75rem;
  border-radius: 8px;
  background: rgba(232, 93, 93, 0.15);
  border: 1px solid var(--danger);
  color: #ffb4b4;
  font-size: 0.88rem;
}

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

@media (max-width: 520px) {
  .msg-card-image {
    float: none;
    margin-right: 0;
  }

  .composer-row {
    flex-direction: column;
  }

  .composer-actions {
    flex-direction: row;
    justify-content: flex-end;
    align-items: center;
  }
}
