:root {
  color-scheme: light;
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-soft: #fafafa;
  --border: #e5e7eb;
  --text: #171717;
  --muted: #6b7280;
  --primary: #10a37f;
  --primary-hover: #0d8f6e;
  --user: #f7f7f8;
  --assistant: #ffffff;
  --error: #fff1f1;
  --chat-panel-max-width: 840px;
  --chat-message-max-width: 760px;
  --mobile-text-scale: 1;
  --top-banner-height: 68px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
}

.layout {
  display: grid;
  grid-template-columns: 300px 1fr;
  grid-template-rows: minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
}

.sidebar {
  border-right: 1px solid var(--border);
  background: var(--panel);
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
  height: 100%;
}

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  min-height: var(--top-banner-height);
  border-bottom: 1px solid var(--border);
  gap: 8px;
}

.sidebar-header h1 {
  font-size: 16px;
  margin: 0;
}

.session-list {
  padding: 10px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  flex: 1 1 auto;
  min-height: 0;
}

.session-item {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--panel-soft);
  padding: 10px;
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.session-item:hover {
  border-color: var(--primary-hover);
}

.session-item.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 1px rgba(16, 163, 127, 0.22) inset;
}

.session-title {
  font-weight: 600;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-subline {
  margin-top: 4px;
  font-size: 12px;
  color: var(--muted);
}

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  height: 100%;
  max-width: none;
  min-width: 0;
  min-height: 0;
  align-self: stretch;
  justify-self: stretch;
  overflow: hidden;
}

.chat-header {
  position: sticky;
  top: 0;
  z-index: 20;
  width: 100%;
  min-width: 0;
  min-height: var(--top-banner-height);
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  background: var(--panel);
}

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

.chat-header p {
  margin: 4px 0 0;
  font-size: 12px;
  color: var(--muted);
}

.chat-header-actions {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}

.chat-header-actions-main {
  display: none;
  align-items: center;
  gap: 6px;
}

.chat-header-actions-extra {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}

.mobile-only {
  display: none;
}

body.force-mobile-layout .layout {
  grid-template-columns: 1fr;
}

body.force-mobile-layout .sidebar {
  display: none;
}

body.force-mobile-layout .chat-panel {
  max-width: 100%;
  justify-self: stretch;
}

body.force-mobile-layout .mobile-only {
  display: inline-flex;
  align-items: center;
}

body.force-mobile-layout .chat-header {
  min-height: 0;
  padding: 12px;
}

body.force-mobile-layout .chat-header-actions {
  margin-left: auto;
  align-self: flex-start;
}

body.force-mobile-layout .chat-header-actions-main {
  display: flex;
}

body.force-mobile-layout .chat-header-actions-extra {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 35;
  min-width: min(78vw, 240px);
  max-width: min(90vw, 280px);
  display: none;
  flex-direction: column;
  align-items: stretch;
  gap: 6px;
  padding: 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #ffffff;
  box-shadow: 0 12px 24px rgba(15, 23, 42, 0.16);
}

body.force-mobile-layout .chat-header-actions.mobile-extra-open .chat-header-actions-extra {
  display: flex;
}

body.force-mobile-layout .chat-header-actions-extra .link-action,
body.force-mobile-layout .chat-header-actions-extra button,
body.force-mobile-layout .chat-header-actions-extra select {
  width: 100%;
  justify-content: flex-start;
  padding: 7px 10px;
}

body.force-mobile-layout .messages {
  padding: 12px;
  gap: 14px;
}

body.force-mobile-layout .message {
  width: 96%;
  max-width: 96%;
  min-height: 44px;
  font-size: clamp(15px, max(calc(15px * var(--mobile-text-scale)), 3.8vw), 34px);
  line-height: 1.72;
  padding: 12px;
}

body.force-mobile-layout .message.user {
  width: 92%;
  max-width: 92%;
}

body.force-mobile-layout .meta {
  font-size: clamp(12px, max(calc(12px * var(--mobile-text-scale)), 2.8vw), 22px);
}

body.force-mobile-layout .tool-body {
  font-size: clamp(13px, max(calc(13px * var(--mobile-text-scale)), 3vw), 26px);
  line-height: 1.55;
}

.messages {
  width: 100%;
  min-width: 0;
  overflow-y: auto;
  overflow-x: hidden;
  min-height: 0;
  padding: 16px max(16px, calc((100% - var(--chat-panel-max-width)) / 2 + 16px));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
}

.message {
  width: 100%;
  max-width: 100%;
  min-width: 0;
  flex: 0 0 auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 11px 12px;
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  overflow-x: hidden;
  overflow-y: visible;
  line-height: 1.58;
}

.message .body {
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
  line-height: inherit;
}

.message.assistant .body.markdown-body {
  white-space: normal;
}

.message.assistant .body.markdown-body > * {
  margin: 0 0 0.72em;
}

.message.assistant .body.markdown-body > *:last-child {
  margin-bottom: 0;
}

.message.assistant .body.markdown-body h1,
.message.assistant .body.markdown-body h2,
.message.assistant .body.markdown-body h3,
.message.assistant .body.markdown-body h4,
.message.assistant .body.markdown-body h5,
.message.assistant .body.markdown-body h6 {
  line-height: 1.35;
  font-weight: 650;
}

.message.assistant .body.markdown-body h1 {
  font-size: 1.28em;
}

.message.assistant .body.markdown-body h2 {
  font-size: 1.18em;
}

.message.assistant .body.markdown-body h3 {
  font-size: 1.1em;
}

.message.assistant .body.markdown-body ul,
.message.assistant .body.markdown-body ol {
  margin: 0 0 0.72em 1.35em;
  padding: 0;
}

.message.assistant .body.markdown-body li + li {
  margin-top: 0.24em;
}

.message.assistant .body.markdown-body blockquote {
  margin: 0 0 0.72em;
  padding-left: 10px;
  color: #4b5563;
  border-left: 3px solid var(--border);
}

.message.assistant .body.markdown-body pre {
  position: relative;
  margin: 0 0 0.78em;
  padding: 8px 10px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #f8fafc;
  overflow-x: auto;
  white-space: pre;
  font-size: 12px;
  line-height: 1.5;
}

.message.assistant .body.markdown-body pre code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  background: transparent;
  border: none;
  padding: 0;
}

.message.assistant .body.markdown-body .md-code-lang {
  margin-bottom: 6px;
  font-size: 11px;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.03em;
}

.message.assistant .body.markdown-body .md-code-copy-btn {
  position: absolute;
  top: 6px;
  right: 6px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #ffffff;
  color: #334155;
  font-size: 11px;
  line-height: 1;
  padding: 4px 8px;
  cursor: pointer;
}

.message.assistant .body.markdown-body .md-code-copy-btn:hover {
  border-color: #cbd5e1;
  background: #f8fafc;
}

.message.assistant .body.markdown-body .md-code-copy-btn[data-copy-state="copied"] {
  border-color: #b9d8c7;
  background: #f0f8f3;
  color: #2f6b45;
}

.message.assistant .body.markdown-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
  padding: 1px 4px;
  border-radius: 5px;
  border: 1px solid #e5e7eb;
  background: #f3f4f6;
}

.message.assistant .body.markdown-body a {
  color: var(--primary);
}

.message.assistant .body.markdown-body hr {
  border: none;
  border-top: 1px solid var(--border);
  margin: 0.9em 0;
}

.message.user {
  align-self: flex-end;
  margin-left: auto;
  width: fit-content;
  max-width: 50%;
  background: var(--user);
}

.message.assistant,
.message.tool-group,
.message.tool {
  align-self: center;
  margin: 0 auto;
  width: 100%;
  border: none;
  border-radius: 0;
  background: transparent;
  padding: 3px 4px;
}

.message.tool-group {
  white-space: normal;
}

.message.tool[hidden] {
  display: none !important;
}

.message.tool.collapsed .tool-body {
  display: none;
}

.message.tool.expanded .tool-body {
  display: block;
}

.tool-group-summary {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  text-align: left;
  padding: 0;
  font: inherit;
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.tool-group-summary:hover {
  color: var(--text);
}

.tool-summary {
  display: block;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  border: none;
  background: transparent;
  color: var(--muted);
  text-align: left;
  padding: 0;
  font: inherit;
  font-size: 13px;
  line-height: 1.4;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  cursor: pointer;
}

.tool-summary:hover {
  color: var(--text);
}

.tool-body {
  margin-top: 6px;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  background: #ffffff;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}

.tool-body-content {
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}

.tool-body-more {
  display: inline-block;
  margin-top: 6px;
  border: none;
  background: transparent;
  padding: 0;
  color: var(--primary);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
  text-decoration: underline;
}

.tool-body-more:hover {
  color: var(--primary-hover);
}

.message.system {
  background: var(--panel-soft);
}

.message.error {
  background: var(--error);
}

.message.abort-notice {
  background: #fff7e6;
  border-color: #f6d68d;
}

.meta {
  margin-top: 6px;
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  color: var(--muted);
  flex-wrap: wrap;
}

.meta-time {
  color: var(--muted);
}

.message-state {
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 6px;
  font-size: 11px;
  line-height: 1.2;
  background: #f8fafc;
  color: var(--muted);
}

.message-state[data-state="pending"] {
  border-color: #f6d68d;
  background: #fff8e5;
  color: #9a6700;
}

.message-state[data-state="working"] {
  border-color: #9bd7c8;
  background: #eaf9f4;
  color: #0d6e56;
}

.message-state[data-state="done"] {
  border-color: #b9d8c7;
  background: #f0f8f3;
  color: #2f6b45;
}

.message-state[data-state="error"] {
  border-color: #f1b7b7;
  background: #fff1f1;
  color: #b42318;
}

.message-state[data-state="aborted"] {
  border-color: #f6d68d;
  background: #fff8e5;
  color: #9a6700;
}

.composer {
  width: 100%;
  min-width: 0;
  border-top: 1px solid var(--border);
  padding: 10px max(16px, calc((100% - var(--chat-panel-max-width)) / 2 + 16px));
  background: var(--panel);
}

.runtime-status {
  margin-bottom: 6px;
  padding: 0 10px;
  font-size: 12px;
  font-weight: 600;
  color: #475569;
  background: #f8fafc;
  border: 1px solid #dbe3ed;
  border-radius: 8px;
  height: 24px;
  line-height: 22px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.runtime-status[data-state="running"] {
  color: #0d6e56;
  background: #eaf9f4;
  border-color: #9bd7c8;
}

.runtime-status[data-state="idle"] {
  color: #475569;
  background: #f8fafc;
  border-color: #dbe3ed;
}

.runtime-status[data-state="error"] {
  color: #b42318;
  background: #fff1f1;
  border-color: #f1b7b7;
}

.composer-input-wrap {
  position: relative;
}

#prompt-input {
  width: 100%;
  resize: none;
  min-height: 68px;
  max-height: 140px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel);
  color: var(--text);
  padding: 10px 74px 10px 10px;
  font: inherit;
}

.composer-send-btn {
  position: absolute;
  right: 8px;
  bottom: 8px;
  padding: 6px 10px;
  min-height: 30px;
}

button,
.link-action,
select {
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
  border-radius: 9px;
  padding: 7px 11px;
  font-size: 13px;
  cursor: pointer;
  text-decoration: none;
  display: inline-flex;
  align-items: center;
}

select {
  min-width: 96px;
  display: inline-block;
}

button.primary {
  border-color: var(--primary);
  background: var(--primary);
}

button.primary:hover {
  background: var(--primary-hover);
  border-color: var(--primary-hover);
}

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

.empty {
  color: var(--muted);
  font-size: 13px;
  padding: 8px;
}

.mobile-session-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: none;
}

.mobile-session-overlay.open {
  display: block;
}

.mobile-session-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(15, 23, 42, 0.4);
}

.mobile-session-drawer {
  position: absolute;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(88vw, 360px);
  background: var(--panel);
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  transform: translateX(-100%);
  transition: transform 0.2s ease;
}

.mobile-session-overlay.open .mobile-session-drawer {
  transform: translateX(0);
}

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

.mobile-session-header h3 {
  margin: 0;
  font-size: 15px;
}

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

.mobile-session-list {
  flex: 1;
  min-height: 0;
}

body.mobile-session-menu-open {
  overflow: hidden;
}

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

  .sidebar {
    display: none;
  }

  .chat-panel {
    max-width: 100%;
    justify-self: stretch;
  }

  .mobile-only {
    display: inline-flex;
    align-items: center;
  }

  .chat-header {
    padding: 12px;
  }

  .chat-header-actions {
    margin-left: auto;
    align-self: flex-start;
  }

  .chat-header-actions-main {
    display: flex;
  }

  .chat-header-actions-extra {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    z-index: 35;
    min-width: min(78vw, 240px);
    max-width: min(90vw, 280px);
    display: none;
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
    padding: 8px;
    border: 1px solid var(--border);
    border-radius: 10px;
    background: #ffffff;
    box-shadow: 0 12px 24px rgba(15, 23, 42, 0.16);
  }

  .chat-header-actions.mobile-extra-open .chat-header-actions-extra {
    display: flex;
  }

  .chat-header-actions-extra .link-action,
  .chat-header-actions-extra button,
  .chat-header-actions-extra select {
    width: 100%;
    justify-content: flex-start;
    padding: 7px 10px;
  }

  .messages {
    padding: 12px;
    gap: 14px;
  }

  .message {
    width: 96%;
    max-width: 96%;
    min-height: 44px;
    font-size: clamp(15px, max(calc(15px * var(--mobile-text-scale)), 3.8vw), 34px);
    line-height: 1.72;
    padding: 12px;
  }

  .message.user {
    width: fit-content;
    max-width: 92%;
  }

  .message.assistant,
  .message.tool-group,
  .message.tool {
    width: 96%;
    max-width: 96%;
    min-height: 0;
    padding: 3px 4px;
  }

  .meta {
    font-size: clamp(12px, max(calc(12px * var(--mobile-text-scale)), 2.8vw), 22px);
  }

  .tool-body {
    font-size: clamp(13px, max(calc(13px * var(--mobile-text-scale)), 3vw), 26px);
    line-height: 1.55;
  }
}
