:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --text: #14202b;
  --muted: #65717d;
  --line: #d8dee6;
  --accent: #155f49;
  --accent-dark: #0f4737;
  --agent: #eef4f7;
  --user: #e8f5ee;
  --error: #8b1e1e;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 16px;
  width: min(1480px, calc(100% - 32px));
  margin: 0 auto;
  padding: 16px 0;
  min-height: 100vh;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.settings {
  padding: 14px 16px;
  display: grid;
  gap: 10px;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}

.auth-controls {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.auth-status {
  min-width: 104px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 6px 10px;
  font-size: 12px;
  font-weight: 700;
  color: var(--accent-dark);
  background: #e8f5ee;
  text-align: center;
}

.auth-status.is-signed-out {
  color: var(--muted);
  background: #fbfcfd;
}

.server-status {
  display: grid;
  place-items: center;
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  padding: 0;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fbfcfd;
  color: var(--muted);
}

.server-status:hover {
  filter: brightness(0.97);
}

.server-status span::before {
  display: block;
  font-size: 16px;
  font-weight: 800;
  line-height: 1;
}

.server-status.is-checking span::before {
  content: "...";
  transform: translateY(-3px);
}

.server-status.is-running {
  border-color: #5ca487;
  background: #e8f5ee;
  color: var(--accent-dark);
}

.server-status.is-running span::before {
  content: "\2713";
}

.server-status.is-unavailable {
  border-color: #f04438;
  background: #fef3f2;
  color: #b42318;
}

.server-status.is-unavailable span::before {
  content: "!";
}

h1 {
  margin: 0 0 4px;
  font-size: 22px;
  letter-spacing: 0;
}

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

label {
  display: grid;
  gap: 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
  font: 14px/1.45 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: var(--text);
  background: #fbfcfd;
}

textarea {
  resize: vertical;
}

summary {
  cursor: pointer;
  font-weight: 700;
}

.connection-details {
  border-top: 1px solid var(--line);
  padding-top: 9px;
}

.connection-details summary {
  color: var(--muted);
  font-size: 13px;
}

.settings-grid {
  display: grid;
  grid-template-columns: minmax(260px, 420px) minmax(360px, 1fr);
  gap: 14px;
  margin-top: 12px;
}

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

.chat-toolbar {
  min-height: 42px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--line);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.conversation-heading {
  min-width: 0;
  display: flex;
  align-items: center;
  gap: 8px;
}

.id-chip {
  min-width: 0;
  max-width: min(58vw, 720px);
  padding: 3px 7px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fbfcfd;
  color: var(--accent-dark);
  font: 11px/1.25 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.id-chip.is-empty {
  color: var(--muted);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-style: italic;
}

.secondary-button {
  min-height: 28px;
  padding: 4px 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
  font-size: 12px;
}

.secondary-button:hover {
  background: var(--agent);
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
}

.icon-button:hover {
  background: var(--agent);
  color: var(--text);
}

.icon-button.copied {
  color: var(--accent);
}

.icon-button svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.messages {
  padding: 18px;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

@media (max-width: 860px) {
  .app {
    width: min(100% - 20px, 1480px);
    padding: 10px 0;
  }

  .settings-grid {
    grid-template-columns: 1fr;
  }

  .chat {
    min-height: calc(100vh - 190px);
  }
}

.event-log {
  margin-top: 8px;
  max-height: min(56vh, 520px);
  overflow: auto;
  padding: 0;
  display: grid;
  align-content: start;
  gap: 8px;
}

.event-log:empty::before {
  content: "No stream events received yet.";
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
}

.event-log.no-filter-matches::before {
  content: "No matching events.";
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
}

.event-log-entry {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  padding: 7px 8px 8px;
}

.event-log-entry-header {
  min-height: 26px;
  margin-bottom: 5px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.event-log-type {
  min-width: 0;
  color: var(--accent-dark);
  font-size: 12px;
  font-style: italic;
  font-weight: 700;
  overflow-wrap: anywhere;
}

.event-log-payload {
  margin: 0;
  max-height: 110px;
  overflow: auto;
  color: var(--muted);
  font: 11px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

.event-log-raw {
  display: none;
}

.event-bubble {
  margin-top: 10px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.event-bubble summary {
  min-height: 30px;
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.event-count {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 20px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent-dark);
  font-size: 11px;
}

.event-latest {
  min-width: 0;
  color: var(--accent-dark);
  font-size: 11px;
  font-style: italic;
  font-weight: 650;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.event-filter {
  margin-top: 8px;
  display: grid;
  grid-template-columns: minmax(180px, 360px) auto;
  align-items: center;
  gap: 8px;
}

.event-search {
  min-height: 30px;
  padding: 6px 9px;
  font: 12px/1.35 -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--surface);
}

.event-filter-status {
  color: var(--muted);
  font-size: 11px;
  font-style: italic;
}

.structured-response {
  margin-top: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  overflow: hidden;
}

.structured-response summary {
  min-height: 30px;
  padding: 7px 9px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  display: flex;
  align-items: center;
  gap: 8px;
}

.structured-count {
  display: inline-grid;
  place-items: center;
  min-width: 22px;
  height: 20px;
  padding: 0 6px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: var(--surface);
  color: var(--accent-dark);
  font-size: 11px;
}

.structured-response-body {
  padding: 8px;
  border-top: 1px solid var(--line);
  display: grid;
  gap: 10px;
}

.structured-result {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #ffffff;
  padding: 8px;
}

.structured-result-header {
  min-height: 26px;
  margin-bottom: 8px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 8px;
}

.structured-result-title {
  min-width: 0;
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 750;
  overflow-wrap: anywhere;
}

.structured-value,
.structured-object {
  min-width: 0;
}

.structured-grid {
  display: grid;
  grid-template-columns: minmax(140px, 240px) minmax(0, 1fr);
  border: 1px solid var(--line);
  border-radius: 6px;
  overflow: hidden;
  background: var(--surface);
}

.structured-row {
  display: contents;
}

.structured-name,
.structured-scalar {
  min-width: 0;
  font-size: 12px;
  line-height: 1.35;
}

.structured-grid .structured-name,
.structured-grid .structured-scalar {
  padding: 7px 8px;
  border-bottom: 1px solid var(--line);
}

.structured-grid .structured-row:last-child .structured-name,
.structured-grid .structured-row:last-child .structured-scalar {
  border-bottom: 0;
}

.structured-name {
  color: var(--muted);
  font-weight: 750;
  background: #f4f6f8;
}

.structured-scalar {
  color: var(--text);
  overflow-wrap: anywhere;
}

.structured-scalar-long {
  display: inline-flex;
  max-width: 100%;
  align-items: center;
  gap: 6px;
}

.structured-scalar-preview {
  min-width: 0;
  overflow-wrap: anywhere;
}

.structured-table .structured-scalar {
  display: inline;
  padding: 0;
  border: 0;
}

.structured-table .structured-scalar-long {
  display: inline-flex;
}

.structured-nested {
  margin-top: 8px;
  display: grid;
  gap: 8px;
}

.structured-nested-details {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  padding: 0;
}

.structured-nested-details > summary {
  min-height: 30px;
  padding: 6px 8px;
  border-bottom: 1px solid var(--line);
  color: var(--accent-dark);
  font-size: 12px;
}

.structured-nested-details:not([open]) > summary {
  border-bottom: 0;
}

.structured-nested-details > .structured-object,
.structured-nested-details > .structured-table-wrap,
.structured-nested-details > .structured-list,
.structured-nested-details > .structured-value {
  margin: 8px;
}

.structured-list {
  margin: 0;
  padding-left: 20px;
  display: grid;
  gap: 5px;
  font-size: 12px;
}

.structured-table-wrap {
  min-width: 0;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
}

.structured-table-caption {
  padding: 7px 8px;
  border-bottom: 1px solid var(--line);
  color: var(--accent-dark);
  font-size: 12px;
  font-weight: 750;
}

.structured-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12px;
}

.structured-table th,
.structured-table td {
  min-width: 120px;
  max-width: 340px;
  padding: 7px 8px;
  border-right: 1px solid var(--line);
  border-bottom: 1px solid var(--line);
  text-align: left;
  vertical-align: top;
}

.structured-table th:last-child,
.structured-table td:last-child {
  border-right: 0;
}

.structured-table tr:last-child td {
  border-bottom: 0;
}

.structured-table th {
  color: var(--muted);
  font-weight: 750;
  background: #f4f6f8;
}

.structured-empty {
  color: var(--muted);
  font-size: 12px;
  font-style: italic;
}

.structured-raw {
  display: none;
}

.message {
  position: relative;
  max-width: 82%;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px 10px;
}

.message.user {
  align-self: flex-end;
  background: var(--user);
}

.message.agent {
  align-self: flex-start;
  width: 100%;
  max-width: 100%;
  background: var(--agent);
}

.message.error {
  align-self: flex-start;
  border-color: color-mix(in srgb, var(--error) 40%, var(--line));
  color: var(--error);
  background: #fff4f4;
}

.meta {
  min-height: 24px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 0 0 4px;
  font-size: 10px;
  font-weight: 700;
  line-height: 1;
  color: var(--muted);
}

.meta-main {
  min-width: 0;
  display: flex;
  align-items: baseline;
  gap: 8px;
  flex-wrap: wrap;
}

.meta-label {
  flex: 0 0 auto;
}

.message-state {
  min-width: 0;
  color: var(--accent-dark);
  font: 10px/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  overflow-wrap: anywhere;
}

.content {
  display: block;
  white-space: pre-wrap;
}

.message.agent .content {
  white-space: normal;
  line-height: 1.48;
}

.message.agent .content:empty {
  display: none;
}

.message.agent .content p {
  margin: 0 0 9px;
  color: inherit;
}

.message.agent .content p:last-child,
.message.agent .content ul:last-child,
.message.agent .content ol:last-child,
.message.agent .content pre:last-child {
  margin-bottom: 0;
}

.message.agent .content ul,
.message.agent .content ol {
  margin: 0 0 10px;
  padding-left: 20px;
}

.message.agent .content li {
  margin: 2px 0;
  font-size: 13px;
  line-height: 1.42;
}

.message.agent .content h3,
.message.agent .content h4,
.message.agent .content h5 {
  margin: 12px 0 5px;
  color: var(--text);
  font-weight: 750;
  line-height: 1.25;
}

.message.agent .content h3 {
  font-size: 17px;
}

.message.agent .content h4 {
  font-size: 15px;
}

.message.agent .content h5 {
  font-size: 14px;
}

.message.agent .content code {
  padding: 1px 4px;
  border: 1px solid var(--line);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.72);
  font: 0.92em/1.35 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.message.agent .content pre {
  margin: 0 0 10px;
  max-height: 340px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fbfcfd;
  white-space: pre-wrap;
}

.message.agent .content pre code {
  padding: 0;
  border: 0;
  background: transparent;
}

.message.agent .content a {
  color: var(--accent-dark);
  font-weight: 700;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.approval-panel {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

.approval-title {
  margin-bottom: 4px;
  font-size: 13px;
  font-weight: 700;
}

.approval-arguments {
  margin: 0 0 8px;
  max-height: 160px;
  overflow: auto;
  color: var(--muted);
  font: 12px/1.4 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  white-space: pre-wrap;
}

.approval-actions {
  display: flex;
  gap: 8px;
}

.approval-actions button {
  min-height: 30px;
  padding: 5px 12px;
  font-size: 12px;
}

.approval-actions .deny-button {
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--text);
}

.oauth-consent-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px solid var(--line);
}

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

.oauth-consent-actions {
  display: flex;
  flex: 0 0 auto;
  align-items: center;
  gap: 8px;
}

.oauth-consent-link {
  flex: 0 0 auto;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 32px;
  padding: 6px 12px;
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 700;
  text-decoration: none;
  cursor: pointer;
}

.oauth-consent-link:hover {
  filter: brightness(0.94);
}

.oauth-resume-button {
  min-height: 32px;
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--surface);
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.oauth-consent-actions button:disabled {
  cursor: wait;
  opacity: 0.65;
}

.copy-message,
.copy-event,
.copy-structured,
.copy-scalar {
  flex: 0 0 auto;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: 0;
  background: transparent;
  color: var(--muted);
  border-radius: 4px;
  padding: 0;
  opacity: 0.55;
}

.copy-message svg,
.copy-event svg,
.copy-structured svg,
.copy-scalar svg {
  width: 14px;
  height: 14px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.copy-message:hover,
.copy-event:hover,
.copy-structured:hover,
.copy-scalar:hover {
  background: rgba(255, 255, 255, 0.7);
  opacity: 1;
}

.copy-message.copied,
.copy-event.copied,
.copy-structured.copied,
.copy-scalar.copied {
  color: var(--accent);
  opacity: 1;
}

.copy-event,
.copy-structured {
  width: 26px;
  height: 26px;
  border: 1px solid var(--line);
  background: var(--surface);
  opacity: 1;
}

.copy-event svg,
.copy-structured svg {
  width: 15px;
  height: 15px;
}

.copy-event:hover,
.copy-structured:hover {
  background: var(--agent);
}

.copy-scalar {
  width: 22px;
  height: 22px;
  border: 1px solid var(--line);
  background: #ffffff;
  opacity: 1;
}

.copy-scalar svg {
  width: 13px;
  height: 13px;
}

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

  .structured-name {
    border-bottom: 0;
    padding-bottom: 2px;
  }

  .structured-scalar {
    padding-top: 2px;
  }
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 120px;
  gap: 10px;
  padding: 14px;
  border-top: 1px solid var(--line);
  background: #fbfcfd;
}

button {
  border: 0;
  border-radius: 6px;
  background: var(--accent);
  color: #ffffff;
  font-size: 15px;
  font-weight: 750;
  cursor: pointer;
}

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

button:disabled {
  opacity: 0.6;
  cursor: wait;
}

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

  .chat {
    min-height: 70vh;
  }
}
