:root {
  --bg: #f4f7fb;
  --panel: #ffffff;
  --border: #d8e1ec;
  --text: #1e293b;
  --muted: #64748b;
  --primary: #2563eb;
  --primary-hover: #1d4ed8;
  --soft: #eff6ff;
  --assistant: #f8fafc;
  --user: #dbeafe;
  --danger: #dc2626;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
  --radius: 16px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  font-family: Arial, Helvetica, sans-serif;
  background: var(--bg);
  color: var(--text);
}

body {
  overflow: hidden;
}

.app-shell {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar {
  background: white;
  border-bottom: 1px solid var(--border);
  padding: 14px 18px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

.topbar h1 {
  margin: 0;
  font-size: 20px;
}

.topbar .sub {
  color: var(--muted);
  font-size: 13px;
  margin-top: 4px;
}

.status {
  font-size: 13px;
  color: var(--muted);
  white-space: nowrap;
}

.status.error {
  color: var(--danger);
  font-weight: 700;
}

.main-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(420px, 1.2fr) minmax(320px, 0.9fr);
  gap: 16px;
  padding: 16px;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  min-height: 0;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}

.panel-header {
  padding: 16px 18px;
  border-bottom: 1px solid var(--border);
}

.panel-header h2 {
  margin: 0;
  font-size: 16px;
}

.panel-header p {
  margin: 6px 0 0 0;
  font-size: 13px;
  color: var(--muted);
}

.sample-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.sample-btn,
button {
  border: none;
  border-radius: 10px;
  padding: 10px 12px;
  cursor: pointer;
  font-weight: 600;
  font-size: 13px;
}

.sample-btn {
  background: var(--soft);
  color: var(--primary);
  border: 1px solid #bfdbfe;
}

.btn-primary {
  background: var(--primary);
  color: white;
}

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

.btn-secondary {
  background: #e2e8f0;
  color: #0f172a;
}

.chip-btn {
  background: white;
  color: #334155;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
}

.chat-history {
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  background: linear-gradient(to bottom, #fbfdff 0%, #f7faff 100%);
}

.message {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 88%;
}

.message.user {
  align-self: flex-end;
}

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

.bubble {
  border-radius: 16px;
  padding: 14px 14px;
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-word;
  border: 1px solid var(--border);
}

.message.user .bubble {
  background: var(--user);
  border-color: #bfdbfe;
}

.message.assistant .bubble {
  background: var(--assistant);
}

.message-meta {
  font-size: 12px;
  color: var(--muted);
  padding: 0 4px;
}

.message-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  padding: 0 4px;
}

.collapsible {
  display: none;
  margin-top: 4px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 13px;
}

.collapsible.open {
  display: block;
}

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

.composer textarea {
  width: 100%;
  min-height: 74px;
  max-height: 200px;
  resize: vertical;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  font-size: 14px;
  outline: none;
}

.composer textarea:focus {
  border-color: var(--primary);
}

.composer-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.viewer-panel {
  display: grid;
  grid-template-rows: auto auto 1fr;
  overflow: hidden;
}

.viewer-toolbar {
  border-bottom: 1px solid var(--border);
  padding: 12px 16px;
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  background: white;
}

.viewer-toolbar-left {
  min-width: 0;
}

.viewer-title {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 4px;
  word-break: break-word;
}

.viewer-meta {
  color: var(--muted);
  font-size: 12px;
  word-break: break-word;
}

.viewer-tabs {
  display: flex;
  gap: 8px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  background: #fbfdff;
  flex-wrap: wrap;
}

.viewer-tab {
  background: #eef2f7;
  color: #0f172a;
  border-radius: 999px;
  padding: 8px 12px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.viewer-tab.active {
  background: var(--primary);
  color: white;
}

.viewer-body {
  min-height: 0;
  overflow: hidden;
  position: relative;
  background: #f8fafc;
}

.viewer-empty,
.viewer-json,
.viewer-text {
  height: 100%;
  overflow: auto;
  padding: 16px;
}

.viewer-empty {
  color: var(--muted);
}

.viewer-json pre,
.viewer-text pre {
  margin: 0;
  white-space: pre-wrap;
  word-break: break-word;
  background: white;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 14px;
  font-size: 13px;
}

.viewer-iframe {
  width: 100%;
  height: 100%;
  border: 0;
  background: white;
}

.source-list {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-top: 10px;
}

.source-card {
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  background: white;
}

.source-title {
  font-weight: 700;
  margin-bottom: 6px;
}

.source-meta {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
  word-break: break-word;
}

.source-actions {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 10px;
}

.metric-box {
  margin-top: 10px;
  background: white;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
  font-family: Consolas, monospace;
  font-size: 12px;
  white-space: pre-wrap;
  max-height: 180px;
  overflow: auto;
}

.history-tools {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.inline-note {
  font-size: 12px;
  color: var(--muted);
}

.source-mini-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-top: 8px;
}

.source-mini-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px;
  background: #f8fafc;
}

.source-mini-title {
  font-size: 12px;
  font-weight: 700;
  margin-bottom: 4px;
  word-break: break-word;
}

.source-mini-meta {
  font-size: 11px;
  color: var(--muted);
  margin-bottom: 6px;
  word-break: break-word;
}

@media (max-width: 980px) {
  .main-layout {
    grid-template-columns: 1fr;
  }

  body {
    overflow: auto;
  }

  .app-shell {
    height: auto;
    min-height: 100vh;
  }

  .main-layout {
    min-height: auto;
  }

  .panel {
    min-height: 420px;
  }
}
