:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --panel-soft: #eef1f5;
  --text: #15191f;
  --muted: #667085;
  --line: #d8dee8;
  --brand: #1f6feb;
  --brand-strong: #195cc7;
  --danger: #c93535;
  --danger-bg: #fff0f0;
  --shadow: 0 18px 48px rgba(22, 28, 36, 0.14);
}

[data-theme="dark"] {
  color-scheme: dark;
  --bg: #111418;
  --panel: #191e24;
  --panel-soft: #222933;
  --text: #eef2f7;
  --muted: #9ba7b6;
  --line: #303946;
  --brand: #58a6ff;
  --brand-strong: #7cbcff;
  --danger: #ff7070;
  --danger-bg: #321f22;
  --shadow: 0 20px 54px rgba(0, 0, 0, 0.34);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  min-height: 100%;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

button,
input,
textarea,
select {
  font: inherit;
}

button {
  cursor: pointer;
}

[hidden] {
  display: none !important;
}

.login-view {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
}

.login-panel {
  width: min(420px, 100%);
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 28px;
  display: grid;
  gap: 18px;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--brand);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  line-height: 1.15;
}

h1 {
  font-size: 24px;
}

h2 {
  font-size: 20px;
}

.app-shell {
  height: 100vh;
  height: 100dvh;
  min-height: 0;
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
  overflow: hidden;
}

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

.sidebar-header,
.topbar,
.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
}

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

.session-list {
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 10px;
}

.session-item,
.api-config-item {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 8px;
  padding: 10px;
  text-align: left;
  display: grid;
  gap: 4px;
}

.session-item:hover,
.api-config-item:hover {
  background: var(--panel-soft);
}

.session-item.active,
.api-config-item.active {
  border-color: var(--brand);
  background: color-mix(in srgb, var(--brand) 12%, transparent);
}

.session-item-title,
.api-config-name {
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.session-item-meta,
.api-config-meta,
.muted {
  color: var(--muted);
  font-size: 13px;
}

.workspace {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
}

.topbar {
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  padding: 12px 16px;
}

.title-block {
  min-width: 0;
  flex: 1;
}

.session-title-input {
  width: 100%;
  max-width: 520px;
  border: 1px solid transparent;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-size: 18px;
  font-weight: 750;
  padding: 6px 8px;
}

.session-title-input:focus {
  outline: none;
  border-color: var(--line);
  background: var(--panel-soft);
}

.session-meta {
  margin: 0 0 0 8px;
  color: var(--muted);
  font-size: 12px;
}

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

.api-switcher {
  display: flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
}

select,
input,
textarea {
  border: 1px solid var(--line);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
}

select {
  min-height: 36px;
  padding: 0 34px 0 10px;
  max-width: 180px;
}

.messages {
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 28px 46px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  background: var(--panel);
}

.empty-state {
  margin: auto;
  color: var(--muted);
  text-align: center;
}

.message {
  flex: 0 0 auto;
  width: min(920px, 100%);
  border: 0;
  border-radius: 0;
  padding: 0;
  background: transparent;
  color: var(--text);
  font-size: 15px;
  line-height: 1.75;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.message.user {
  width: auto;
  max-width: min(78%, 640px);
  align-self: flex-end;
  border-radius: 14px;
  padding: 9px 13px;
  background: #f1f1f1;
  color: #111;
  line-height: 1.45;
}

[data-theme="dark"] .message.user {
  background: #2a2d31;
  color: var(--text);
}

[data-theme="dark"] .messages {
  background: var(--panel);
}

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

.message-role {
  display: none;
}

.message-task-meta {
  display: block;
  margin: 2px 0 16px;
  color: #7b7f87;
  font-size: 13px;
  line-height: 1.3;
}

.task-log {
  margin: 12px 0 14px;
  border-top: 0;
  padding-top: 0;
  white-space: normal;
}

.task-log summary {
  color: #9a9fa7;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  gap: 6px;
  font-size: 13px;
  font-weight: 500;
  list-style: none;
}

.task-log summary::-webkit-details-marker {
  display: none;
}

.task-log summary::before {
  content: "▣";
  color: #9a9fa7;
  margin-right: 2px;
  font-size: 11px;
}

.task-log[open] summary::before {
  content: "▣";
}

.task-log-title {
  flex: 0 1 auto;
}

.task-log-count {
  color: #9a9fa7;
  font-weight: 500;
}

.task-log-list {
  display: grid;
  gap: 7px;
  margin-top: 8px;
}

.task-log-item {
  border: 0;
  border-radius: 0;
  background: transparent;
  padding: 0;
}

.task-log-line {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 2px;
  align-items: start;
}

.task-log-badge {
  display: none;
}

.task-log-text {
  min-width: 0;
  color: #9a9fa7;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.45;
  overflow-wrap: anywhere;
}

.task-log-detail {
  max-height: 112px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  margin: 3px 0 0;
  padding: 0;
  border-radius: 0;
  border: 0;
  background: transparent;
  color: #a4a8ae;
  font-family: inherit;
  font-size: 13px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.task-log-item.running .task-log-badge,
.task-log-item.tool .task-log-badge {
  color: #9a9fa7;
  border-color: transparent;
}

.task-log-item.success .task-log-badge {
  color: #9a9fa7;
  border-color: transparent;
}

.task-log-item.failure .task-log-badge,
.task-log-item.stderr .task-log-badge {
  color: #9a9fa7;
  border-color: transparent;
}

.composer {
  border-top: 1px solid var(--line);
  background: var(--panel);
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom));
}

.task-status {
  width: min(920px, 100%);
  margin: 0 auto 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.composer-form {
  width: min(920px, 100%);
  margin: 0 auto;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
}

#promptInput {
  min-height: 46px;
  max-height: 180px;
  resize: none;
  padding: 12px;
}

.primary-button,
.secondary-button,
.danger-button,
.icon-button {
  border-radius: 6px;
  min-height: 36px;
  border: 1px solid transparent;
  padding: 0 14px;
  color: var(--text);
  background: var(--panel-soft);
}

.primary-button {
  background: var(--brand);
  color: #fff;
}

.primary-button:hover {
  background: var(--brand-strong);
}

.secondary-button {
  border-color: var(--line);
  background: var(--panel);
}

.danger-button {
  border-color: color-mix(in srgb, var(--danger) 35%, var(--line));
  background: var(--danger-bg);
  color: var(--danger);
}

.icon-button {
  width: 36px;
  padding: 0;
  display: inline-grid;
  place-items: center;
  font-size: 20px;
}

.field {
  display: grid;
  gap: 7px;
}

.field span,
.check-field {
  color: var(--muted);
  font-size: 13px;
}

.field input,
.field textarea {
  width: 100%;
  padding: 10px;
}

.check-field {
  display: flex;
  align-items: center;
  gap: 8px;
}

.form-error {
  margin: 0;
  color: var(--danger);
}

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.48);
  display: grid;
  place-items: center;
  padding: 18px;
  overflow: hidden;
}

.modal {
  width: min(900px, 100%);
  max-height: min(760px, calc(100dvh - 36px));
  min-height: 0;
  overflow: hidden;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

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

.api-config-layout {
  min-height: 0;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  gap: 16px;
  padding: 16px;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
}

.api-config-list {
  display: grid;
  gap: 8px;
  margin-top: 10px;
}

.api-config-form {
  display: grid;
  align-content: start;
  gap: 12px;
}

.form-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.full-width {
  width: 100%;
}

.mobile-only {
  display: none;
}

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

  .sidebar {
    position: fixed;
    z-index: 20;
    inset: 0 auto 0 0;
    width: min(86vw, 320px);
    transform: translateX(-100%);
    transition: transform 180ms ease;
    box-shadow: var(--shadow);
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .mobile-only {
    display: inline-grid;
  }

  .topbar {
    align-items: flex-start;
  }

  .topbar-actions {
    flex-wrap: wrap;
    justify-content: flex-end;
  }

  .api-switcher span {
    display: none;
  }

  .messages {
    padding: 18px 18px 22px;
  }

  .task-log-detail {
    margin-left: 0;
  }

  .composer-form {
    grid-template-columns: 1fr;
  }

  #promptInput {
    font-size: 16px;
  }

  .api-config-layout {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 560px) {
  .topbar {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
  }

  .topbar-actions {
    grid-column: 1 / -1;
    justify-content: stretch;
  }

  .topbar-actions > * {
    flex: 1 1 auto;
  }

  .api-switcher {
    flex-basis: 100%;
  }

  .api-switcher select {
    width: 100%;
  }

  .modal-backdrop {
    padding: 0;
  }

  .modal {
    width: 100%;
    height: 100vh;
    height: 100dvh;
    max-height: none;
    border-radius: 0;
  }
}
