:root {
  --bg: #eef2f7;
  --panel: #ffffff;
  --ink: #142032;
  --muted: #4d5b70;
  --accent: #1574d1;
  --line: #d8e0ec;
  --ok: #0f8a5f;
  --warn: #9c5b00;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "IBM Plex Sans", "Segoe UI", sans-serif;
  background: radial-gradient(circle at top right, #dbeafe 0%, var(--bg) 48%, #e6edf6 100%);
  color: var(--ink);
}

.shell {
  max-width: 1080px;
  margin: 0 auto;
  padding: 20px;
}

.header h1 {
  margin-bottom: 8px;
}

.header p {
  margin-top: 0;
  color: var(--muted);
}

.card {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 16px;
  box-shadow: 0 6px 20px rgba(20, 32, 50, 0.06);
}

.grid {
  display: grid;
  gap: 14px;
  margin-top: 14px;
}

.grid.two {
  grid-template-columns: repeat(2, minmax(0, 1fr));
}

.row {
  display: flex;
  align-items: center;
}

.row.between {
  justify-content: space-between;
}

.row.gap-sm {
  gap: 10px;
}

.stack {
  display: grid;
  gap: 8px;
  margin-top: 14px;
}

.stack-sm {
  display: grid;
  gap: 10px;
}

label {
  font-size: 0.92rem;
  color: var(--muted);
}

input,
select {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 1rem;
}

button {
  border: none;
  border-radius: 8px;
  padding: 10px 12px;
  background: var(--accent);
  color: white;
  font-size: 0.95rem;
  cursor: pointer;
}

button.subtle {
  background: #dbe7f5;
  color: #153557;
}

button:hover {
  filter: brightness(0.95);
}

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

.hint {
  margin: 10px 0 0;
  color: var(--warn);
  font-size: 0.92rem;
}

.message {
  min-height: 1.2em;
  margin-top: 10px;
}

.list {
  margin: 10px 0 0;
  padding-left: 18px;
}

.list li {
  margin-bottom: 8px;
}

.pill {
  display: inline-block;
  background: #e8f2fd;
  color: #0f4f8f;
  border: 1px solid #c9ddf4;
  border-radius: 999px;
  padding: 3px 9px;
  font-size: 0.8rem;
  margin-right: 6px;
}

.chat-toggle {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 1200;
  border-radius: 999px;
  padding: 12px 16px;
  box-shadow: 0 6px 16px rgba(20, 32, 50, 0.2);
}

.chat-widget {
  position: fixed;
  right: 18px;
  bottom: 18px;
  width: min(560px, calc(100vw - 24px));
  height: min(78vh, 760px);
  max-height: calc(100dvh - 36px);
  min-width: 340px;
  min-height: 360px;
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 14px 28px rgba(20, 32, 50, 0.2);
  overflow: hidden;
  overscroll-behavior: contain;
  z-index: 1300;
}

body.chat-open .chat-toggle {
  display: none !important;
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}

.chat-controls {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  background: #f9fbff;
}

.chat-controls label {
  font-size: 0.82rem;
}

.chat-controls select {
  padding: 6px 8px;
  font-size: 0.86rem;
}

.chat-hint {
  grid-column: 1 / -1;
  margin: 0;
  font-size: 0.74rem;
  color: var(--muted);
}

.chat-resize-handle {
  position: absolute;
  top: 0;
  left: 0;
  width: 18px;
  height: 18px;
  cursor: nwse-resize;
  z-index: 2;
  opacity: 0.55;
}

.chat-resize-handle::before {
  content: '';
  position: absolute;
  top: 3px;
  left: 3px;
  width: 10px;
  height: 10px;
  border-top: 2px solid #9bb2cc;
  border-left: 2px solid #9bb2cc;
  border-radius: 2px 0 0 0;
}

.chat-list {
  overflow: auto;
  padding: 10px;
  background: #f7faff;
  display: grid;
  gap: 8px;
  overscroll-behavior: contain;
  touch-action: pan-y;
}

.chat-item {
  border: 1px solid var(--line);
  background: white;
  border-radius: 10px;
  padding: 8px 10px;
}

.chat-item.dashboard {
  border-left: 4px solid #1574d1;
}

.chat-item.discord {
  border-left: 4px solid #7f56d9;
}

.chat-item.assistant {
  border-left: 4px solid #0f8a5f;
}

.chat-item.system {
  border-left: 4px solid #d97706;
  background: #fff8ec;
}

.chat-item.failed {
  border-color: #f0b27a;
}

.chat-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.chat-body {
  white-space: normal;
  word-break: break-word;
  line-height: 1.35;
}

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

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

.chat-body p {
  margin: 0 0 0.45rem 0;
}

.chat-body ul,
.chat-body ol {
  margin: 0.2rem 0 0.45rem 1.1rem;
  padding: 0;
}

.chat-body li {
  margin: 0.12rem 0;
}

.chat-body h1,
.chat-body h2,
.chat-body h3,
.chat-body h4 {
  margin: 0.35rem 0 0.3rem;
  line-height: 1.2;
}

.chat-body hr {
  margin: 0.45rem 0;
  border: 0;
  border-top: 1px solid #d6e3f1;
}

.chat-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  background: #eef4fb;
  border: 1px solid #d6e3f1;
  border-radius: 6px;
  padding: 1px 5px;
  font-size: 0.92em;
}

.chat-body pre {
  margin: 8px 0;
  background: #0f172a;
  color: #e2e8f0;
  border-radius: 8px;
  padding: 10px;
  overflow-x: auto;
  white-space: pre;
}

.chat-body pre code {
  border: 0;
  background: transparent;
  padding: 0;
  color: inherit;
}

.chat-body a {
  color: #0f6ac3;
  text-decoration: underline;
}

.chat-actions {
  margin-top: 8px;
}

.chat-retry-btn {
  padding: 4px 8px;
  border: 1px solid #d6e3f1;
  border-radius: 8px;
  background: #fff;
  cursor: pointer;
}

.typing-dots {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  min-height: 18px;
}

.typing-dots span {
  width: 7px;
  height: 7px;
  border-radius: 999px;
  background: #7aa2ca;
  opacity: 0.35;
  animation: typingBlink 1.2s infinite ease-in-out;
}

.typing-dots span:nth-child(2) { animation-delay: 0.15s; }
.typing-dots span:nth-child(3) { animation-delay: 0.3s; }

@keyframes typingBlink {
  0%, 80%, 100% { opacity: 0.3; transform: translateY(0); }
  40% { opacity: 1; transform: translateY(-2px); }
}

.chat-form {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: end;
  padding: 10px;
  border-top: 1px solid var(--line);
  background: white;
}

.chat-form textarea {
  min-height: 42px;
  max-height: 160px;
  resize: none;
  overflow-y: auto;
  line-height: 1.35;
}

.shared-context-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.shared-context-item {
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 8px 10px;
  background: #fbfdff;
}

.shared-context-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
}

.hidden {
  display: none !important;
}

@media (max-width: 800px) {
  .grid.two {
    grid-template-columns: 1fr;
  }

  .row.between {
    flex-direction: column;
    align-items: flex-start;
    gap: 10px;
  }

  .chat-widget {
    left: 8px;
    right: 8px;
    bottom: 8px;
    top: max(8px, env(safe-area-inset-top, 0px));
    width: auto;
    height: auto;
    max-height: calc(100dvh - env(safe-area-inset-top, 0px) - 16px);
    min-width: 0;
    min-height: 0;
    border-radius: 12px;
    resize: none;
  }

  .chat-toggle {
    right: 12px;
    bottom: 12px;
  }

  .chat-resize-handle {
    display: none;
  }
}
