:root {
  color-scheme: light;
  --bg: #f3f5f8;
  --surface: #ffffff;
  --surface-2: #f8fafc;
  --surface-3: #edf2f7;
  --ink: #17202a;
  --muted: #667085;
  --line: #d8e0ea;
  --line-strong: #bac6d4;
  --primary: #1f63d6;
  --primary-strong: #164ca6;
  --green: #0f7a52;
  --green-soft: #e7f5ef;
  --red: #b42318;
  --red-soft: #fff0ed;
  --message-user: #e8f1ff;
  --message-assistant: #ffffff;
  --shadow: 0 14px 34px rgba(24, 34, 48, 0.08);
  --shadow-soft: 0 8px 22px rgba(24, 34, 48, 0.06);
}

body[data-theme="dark"] {
  color-scheme: dark;
  --bg: #11151b;
  --surface: #181e27;
  --surface-2: #202835;
  --surface-3: #283140;
  --ink: #edf2f7;
  --muted: #a9b4c2;
  --line: #334154;
  --line-strong: #526176;
  --primary: #6ea8ff;
  --primary-strong: #95c0ff;
  --green: #67d391;
  --green-soft: #173c2b;
  --red: #ff8b7d;
  --red-soft: #3f201c;
  --message-user: #17365f;
  --message-assistant: #202835;
  --shadow: 0 14px 34px rgba(0, 0, 0, 0.24);
  --shadow-soft: 0 8px 22px rgba(0, 0, 0, 0.18);
}

* {
  box-sizing: border-box;
}

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, sans-serif;
  letter-spacing: 0;
  transition: background 180ms ease, color 180ms ease;
}

body[dir="rtl"] {
  font-family: Tahoma, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

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

button {
  cursor: pointer;
}

code,
pre {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", monospace;
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 50;
  min-height: 70px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
  padding: 12px 22px;
  background: color-mix(in srgb, var(--surface) 94%, transparent);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(12px);
}

.brand-block,
.top-actions,
.sidebar-head,
.section-head,
.chat-toolbar,
.chat-controls,
.composer-actions,
.log-summary {
  display: flex;
  align-items: center;
}

.brand-block {
  gap: 12px;
  min-width: 0;
}

.brand-block.large {
  margin-bottom: 24px;
}

.brand-mark {
  width: 40px;
  height: 40px;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: #11253f;
  color: #fff;
  font-weight: 850;
}

body[data-theme="dark"] .brand-mark {
  background: #d9e7ff;
  color: #11253f;
}

.brand-title {
  font-size: 17px;
  font-weight: 800;
  line-height: 1.1;
}

.brand-subtitle {
  margin-top: 3px;
  color: var(--muted);
  font-size: 13px;
}

.top-actions {
  justify-content: flex-end;
  gap: 10px;
  min-width: 0;
  flex-wrap: wrap;
}

.compact-select,
select,
input,
textarea {
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--surface);
  color: var(--ink);
  outline: none;
}

.compact-select {
  min-height: 38px;
  padding: 0 10px;
}

.segmented {
  display: inline-grid;
  grid-template-columns: 1fr 1fr;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  overflow: hidden;
  min-height: 38px;
}

.segmented button {
  border: 0;
  padding: 0 12px;
  background: var(--surface);
  color: var(--muted);
  font-weight: 750;
}

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

.user-pill,
.health-badge {
  min-height: 38px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 12px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 13px;
  white-space: nowrap;
}

.health-badge {
  color: var(--green);
  background: var(--green-soft);
  border-color: color-mix(in srgb, var(--green) 38%, var(--line));
  font-weight: 800;
}

.shell {
  display: grid;
  grid-template-columns: 310px minmax(0, 1fr);
  align-items: start;
  width: min(1540px, 100%);
  margin: 0 auto;
}

.single-shell {
  display: block;
  width: min(1280px, 100%);
}

.chat-sidebar {
  position: sticky;
  top: 70px;
  height: calc(100vh - 70px);
  display: flex;
  flex-direction: column;
  gap: 14px;
  padding: 18px;
  border-inline-end: 1px solid var(--line);
  background: var(--surface);
}

.sidebar-head,
.section-head,
.chat-toolbar {
  justify-content: space-between;
  gap: 14px;
}

.eyebrow {
  margin: 0 0 5px;
  color: var(--primary);
  font-size: 12px;
  font-weight: 850;
  text-transform: uppercase;
}

h1,
h2 {
  margin: 0;
  letter-spacing: 0;
}

h1 {
  font-size: 28px;
  line-height: 1.15;
}

h2 {
  font-size: 18px;
  line-height: 1.2;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 1px solid var(--line-strong);
  border-radius: 7px;
  background: var(--primary);
  color: #fff;
  font-size: 22px;
  font-weight: 700;
}

.chat-list {
  display: grid;
  gap: 9px;
  overflow: auto;
  padding-inline-end: 2px;
}

.chat-item {
  width: 100%;
  min-height: 78px;
  display: grid;
  gap: 6px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  text-align: start;
  background: var(--surface-2);
  color: var(--ink);
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease, box-shadow 160ms ease;
}

.chat-item:hover,
.model-row:hover {
  transform: translateY(-1px);
  box-shadow: var(--shadow-soft);
}

.chat-item.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.chat-item strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item small {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.main-stack {
  min-width: 0;
  display: grid;
  gap: 20px;
  padding: 22px;
}

.single-shell .main-stack {
  padding: 24px;
}

.chat-workspace,
.section,
.surface,
.metric {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.chat-workspace {
  min-height: calc(100vh - 114px);
  display: grid;
  grid-template-rows: auto auto minmax(320px, 1fr) auto;
  overflow: hidden;
  animation: panelIn 220ms ease both;
}

.single-shell .chat-workspace {
  min-height: calc(100vh - 118px);
}

.chat-toolbar {
  padding: 18px;
  border-bottom: 1px solid var(--line);
  align-items: flex-start;
}

.chat-title-block {
  min-width: 240px;
  flex: 1;
}

.title-input {
  width: 100%;
  border: 0;
  background: transparent;
  color: var(--ink);
  padding: 0;
  font-size: 24px;
  font-weight: 850;
}

.chat-controls {
  justify-content: flex-end;
  gap: 10px;
  flex-wrap: wrap;
}

.control {
  display: grid;
  gap: 5px;
  min-width: 178px;
}

.control.small {
  min-width: 124px;
}

.control span,
label span,
.metric-label,
.config-grid span,
.login-meta span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

select,
input,
textarea {
  width: 100%;
  padding: 9px 10px;
}

textarea {
  line-height: 1.55;
  resize: vertical;
}

select:focus,
input:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.system-drawer {
  border-bottom: 1px solid var(--line);
  background: var(--surface-2);
  padding: 0 18px;
}

.chat-cost-strip {
  min-height: 48px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 8px 18px;
  border-bottom: 1px solid var(--line);
  background: var(--surface);
}

.cost-total {
  display: flex;
  align-items: center;
  gap: 10px;
  color: var(--muted);
  font-size: 13px;
}

.cost-total strong {
  color: var(--ink);
}

.check-row {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 750;
}

.check-row input {
  width: auto;
}

.system-drawer summary {
  min-height: 42px;
  display: flex;
  align-items: center;
  color: var(--muted);
  font-weight: 800;
}

.system-drawer textarea {
  min-height: 90px;
  margin-bottom: 14px;
}

.chat-messages {
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow: auto;
  padding: 20px;
  background: var(--bg);
  scrollbar-width: thin;
}

.empty-chat {
  margin: auto;
  max-width: 520px;
  color: var(--muted);
  text-align: center;
  line-height: 1.65;
}

.message {
  display: grid;
  gap: 6px;
  max-width: min(780px, 88%);
  animation: messageIn 180ms ease both;
}

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

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

body[dir="rtl"] .message.user {
  align-self: flex-start;
}

body[dir="rtl"] .message.assistant {
  align-self: flex-end;
}

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

.bubble {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 13px 14px;
  white-space: pre-wrap;
  line-height: 1.7;
  background: var(--message-assistant);
}

.bubble pre {
  overflow: auto;
  border-radius: 7px;
  padding: 12px;
  background: #0b1220;
  color: #e5e7eb;
}

.bubble code {
  border-radius: 5px;
  padding: 2px 5px;
  background: color-mix(in srgb, var(--surface-3) 82%, transparent);
}

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

.message.user .bubble {
  background: var(--message-user);
  border-color: color-mix(in srgb, var(--primary) 28%, var(--line));
}

.composer {
  border-top: 1px solid var(--line);
  padding: 14px;
  background: var(--surface);
  display: grid;
  gap: 10px;
}

.composer textarea {
  min-height: 96px;
  max-height: 260px;
}

.composer-actions {
  justify-content: space-between;
  gap: 12px;
  flex-wrap: wrap;
}

.state-text {
  color: var(--muted);
  font-size: 13px;
}

.button {
  min-height: 38px;
  border: 1px solid transparent;
  border-radius: 7px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  color: #fff;
  background: var(--primary);
  font-weight: 800;
  text-decoration: none;
  white-space: nowrap;
  transition: transform 140ms ease, background 140ms ease, border-color 140ms ease;
}

.button:hover,
.icon-button:hover {
  transform: translateY(-1px);
}

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

.button.ghost,
.button.subtle {
  color: var(--ink);
  background: var(--surface);
  border-color: var(--line-strong);
}

.button.ghost:hover,
.button.subtle:hover {
  background: var(--surface-3);
}

.section,
.surface {
  padding: 18px;
}

.notice {
  border-radius: 8px;
  border: 1px solid var(--line);
  padding: 12px 14px;
  margin-bottom: 14px;
  font-weight: 700;
}

.notice.success {
  color: var(--green);
  background: var(--green-soft);
}

.notice.danger {
  color: var(--red);
  background: var(--red-soft);
}

.metric-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 14px;
}

.metric {
  min-height: 120px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  box-shadow: none;
  transition: transform 160ms ease, border-color 160ms ease;
}

.metric:hover {
  transform: translateY(-1px);
  border-color: var(--line-strong);
}

.metric strong {
  display: block;
  margin: 8px 0;
  font-size: 25px;
  line-height: 1.15;
  overflow-wrap: anywhere;
}

.metric small {
  color: var(--muted);
  font-size: 13px;
}

.split {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(360px, 0.9fr);
  gap: 16px;
}

.log-summary {
  justify-content: flex-end;
  gap: 8px;
  flex-wrap: wrap;
}

.log-summary span {
  min-height: 30px;
  display: inline-flex;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 0 10px;
  color: var(--muted);
  background: var(--surface-2);
  font-size: 12px;
  font-weight: 750;
}

.log-box,
.code-box {
  width: 100%;
  margin: 0;
  border-radius: 8px;
  border: 1px solid #1f2937;
  background: #111827;
  color: #e5e7eb;
  overflow: auto;
  font-size: 12px;
  line-height: 1.55;
  white-space: pre-wrap;
}

.log-box {
  min-height: 300px;
  max-height: 520px;
  padding: 14px;
}

.code-box {
  min-height: 150px;
  padding: 14px;
}

.route-list,
.config-grid {
  display: grid;
  gap: 0;
}

.model-editor {
  display: grid;
  grid-template-columns: minmax(260px, 0.8fr) minmax(320px, 1.2fr);
  gap: 16px;
}

.model-list {
  display: grid;
  gap: 9px;
  max-height: 520px;
  overflow: auto;
}

.model-row {
  width: 100%;
  display: grid;
  gap: 5px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px;
  background: var(--surface-2);
  color: var(--ink);
  text-align: start;
  transition: transform 160ms ease, border-color 160ms ease, background 160ms ease, box-shadow 160ms ease;
}

.model-row.active {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--primary) 18%, transparent);
}

.model-row span,
.model-row small {
  color: var(--muted);
  font-size: 12px;
}

.model-form {
  display: grid;
  gap: 12px;
}

.field-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 10px;
}

.form-actions {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.usage-grid {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 12px;
}

.usage-grid div {
  min-height: 88px;
  display: grid;
  align-content: center;
  gap: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px;
  background: var(--surface-2);
}

.usage-grid span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.usage-grid strong {
  font-size: 20px;
}

.usage-table-wrap {
  margin-top: 14px;
  overflow: auto;
}

.usage-table {
  width: 100%;
  border-collapse: collapse;
  min-width: 680px;
}

.usage-table th,
.usage-table td {
  border-bottom: 1px solid var(--line);
  padding: 10px;
  text-align: start;
}

.usage-table th {
  color: var(--muted);
  font-size: 12px;
}

.docs-layout {
  display: grid;
  grid-template-columns: 260px minmax(0, 920px);
  gap: 22px;
  width: min(1220px, calc(100vw - 32px));
  margin: 24px auto;
}

.chats-page {
  width: min(1120px, calc(100vw - 32px));
  margin: 24px auto;
}

.chats-browser {
  min-height: calc(100vh - 118px);
}

.page-chat-list {
  max-height: calc(100vh - 230px);
  overflow: auto;
  padding: 4px;
  scrollbar-width: thin;
}

.page-chat-list .chat-item {
  min-height: 92px;
  grid-template-columns: 1fr auto;
}

.page-chat-list .chat-item strong,
.page-chat-list .chat-item small {
  grid-column: 1 / -1;
}

.docs-nav,
.docs-content {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.docs-nav {
  position: sticky;
  top: 90px;
  align-self: start;
  display: grid;
  gap: 6px;
  padding: 12px;
}

.docs-nav a {
  min-height: 38px;
  display: flex;
  align-items: center;
  border-radius: 7px;
  padding: 0 10px;
  color: var(--muted);
  text-decoration: none;
  font-weight: 750;
}

.docs-nav a.active,
.docs-nav a:hover {
  color: var(--ink);
  background: var(--surface-3);
}

.docs-content {
  padding: 24px;
  line-height: 1.75;
}

.docs-content pre {
  overflow: auto;
  border-radius: 8px;
  background: #111827;
  color: #e5e7eb;
  padding: 14px;
}

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

@keyframes panelIn {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

@keyframes messageIn {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.route-list div,
.config-grid div {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 46px;
  padding: 10px 0;
  border-bottom: 1px solid var(--line);
}

.route-list div:last-child,
.config-grid div:last-child {
  border-bottom: 0;
}

.route-list code {
  color: var(--primary);
  font-weight: 850;
  overflow-wrap: anywhere;
}

.route-list span,
.config-grid strong {
  color: var(--muted);
  text-align: end;
  font-size: 13px;
  overflow-wrap: anywhere;
}

.login-body {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--bg);
}

.login-shell {
  width: min(460px, 100%);
}

.login-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 26px;
}

.login-copy {
  margin-bottom: 20px;
}

.login-copy h1 {
  font-size: 26px;
  margin-bottom: 8px;
}

.login-copy p {
  margin: 0;
  color: var(--muted);
  line-height: 1.55;
}

.login-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-top: 18px;
  padding-top: 16px;
  border-top: 1px solid var(--line);
}

.login-meta strong {
  overflow-wrap: anywhere;
  text-align: end;
  font-size: 13px;
}

.button.wide {
  width: 100%;
}

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

  .chat-sidebar {
    position: static;
    height: auto;
    border-inline-end: 0;
    border-bottom: 1px solid var(--line);
  }

  .chat-list {
    grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
    max-height: 220px;
  }

  .chat-workspace {
    min-height: 760px;
  }

  .metric-grid,
  .split,
  .model-editor,
  .usage-grid,
  .docs-layout {
    grid-template-columns: 1fr 1fr;
  }
}

@media (max-width: 760px) {
  .topbar,
  .top-actions,
  .chat-toolbar,
  .section-head,
  .composer-actions {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions,
  .top-actions form,
  .top-actions .button,
  .compact-select,
  .segmented {
    width: 100%;
  }

  .main-stack {
    padding: 14px;
  }

  .chats-page {
    width: calc(100vw - 20px);
    margin: 10px auto;
  }

  .chat-controls,
  .metric-grid,
  .split,
  .model-editor,
  .usage-grid,
  .field-grid,
  .docs-layout {
    display: grid;
    grid-template-columns: 1fr;
  }

  .message {
    max-width: 100%;
  }

  h1 {
    font-size: 24px;
  }
}
