:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-strong: #f0f4f7;
  --text: #17202a;
  --muted: #657282;
  --line: #dfe5eb;
  --accent: #2563eb;
  --accent-soft: #dbeafe;
  --good: #0f766e;
  --warn: #b45309;
  --bad: #b91c1c;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 14px;
  line-height: 1.45;
  -webkit-font-smoothing: antialiased;
}

a {
  color: inherit;
  text-decoration: none;
}

.topbar {
  align-items: center;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 24px;
  min-height: 64px;
  justify-content: space-between;
  padding: 0 32px;
  position: sticky;
  top: 0;
  z-index: 2;
}

.brand {
  font-size: 16px;
  font-weight: 760;
  letter-spacing: 0;
}

.nav {
  display: flex;
  gap: 8px;
}

.nav-form {
  display: inline-flex;
  margin: 0;
}

.nav a,
.button,
button {
  align-items: center;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  cursor: pointer;
  display: inline-flex;
  font: inherit;
  gap: 6px;
  min-height: 34px;
  padding: 7px 11px;
  transition: border-color 120ms ease, box-shadow 120ms ease, transform 120ms ease;
}

.nav a:hover,
.button:hover,
button:hover {
  border-color: #b8c5d2;
  box-shadow: 0 1px 2px rgba(23, 32, 42, 0.06);
}

.button.primary,
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #ffffff;
}

.button.danger,
button.danger {
  color: var(--bad);
}

.container {
  margin: 0 auto;
  max-width: 1220px;
  padding: 30px 28px 40px;
}

.auth-panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 1px 2px rgba(23, 32, 42, 0.04);
  margin: 42px auto 0;
  max-width: 430px;
  padding: 26px;
}

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

.auth-form,
.account-form {
  display: grid;
  gap: 14px;
}

.account-panel {
  max-width: 560px;
}

.alert {
  border: 1px solid var(--line);
  border-radius: 6px;
  font-size: 13px;
  margin-bottom: 14px;
  padding: 10px 12px;
}

.alert.error {
  background: #fee2e2;
  border-color: #fecaca;
  color: #991b1b;
}

.alert.success {
  background: #ccfbf1;
  border-color: #99f6e4;
  color: #115e59;
}

.alert.warn {
  background: #fef3c7;
  border-color: #fde68a;
  color: #92400e;
}

.page-head {
  align-items: center;
  display: flex;
  gap: 16px;
  justify-content: space-between;
  margin-bottom: 22px;
}

h1 {
  font-size: 28px;
  font-weight: 780;
  letter-spacing: 0;
  line-height: 1.2;
  margin: 0 0 5px;
}

h2 {
  font-size: 15px;
  font-weight: 760;
  letter-spacing: 0;
  line-height: 1.3;
  margin: 0;
}

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

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

.stat {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  min-height: 100px;
  padding: 18px;
  box-shadow: 0 1px 2px rgba(23, 32, 42, 0.04);
}

.stat .label {
  color: var(--muted);
  font-size: 12px;
  font-weight: 650;
  letter-spacing: 0;
  margin-bottom: 12px;
}

.stat .value {
  font-size: 30px;
  font-weight: 790;
  line-height: 1;
}

.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-bottom: 18px;
  overflow: hidden;
  padding: 18px;
  box-shadow: 0 1px 2px rgba(23, 32, 42, 0.04);
}

.panel > h2 {
  margin-bottom: 16px;
}

.panel-head {
  align-items: center;
  display: flex;
  gap: 12px;
  justify-content: space-between;
  margin-bottom: 16px;
}

.panel-head > h2 {
  margin-bottom: 0;
}

.toolbar {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.toolbar input {
  flex: 1 1 180px;
  width: auto;
}

.toolbar button,
.toolbar .button {
  flex: 0 0 auto;
}

.chart {
  align-items: end;
  background-image: linear-gradient(to bottom, transparent calc(25% - 1px), var(--surface-strong) 25%, transparent calc(25% + 1px)),
    linear-gradient(to bottom, transparent calc(50% - 1px), var(--surface-strong) 50%, transparent calc(50% + 1px)),
    linear-gradient(to bottom, transparent calc(75% - 1px), var(--surface-strong) 75%, transparent calc(75% + 1px));
  border-bottom: 1px solid var(--line);
  display: grid;
  gap: 5px;
  grid-template-columns: repeat(var(--bar-count), minmax(5px, 1fr));
  height: 210px;
  padding: 12px 0 0;
}

.bar {
  background: linear-gradient(180deg, #2563eb, #0f766e);
  border-radius: 5px 5px 0 0;
  min-height: 3px;
  position: relative;
}

.bar[data-errors]:not([data-errors="0"]) {
  box-shadow: inset 0 4px 0 var(--warn);
}

table {
  border-collapse: collapse;
  width: 100%;
}

th,
td {
  border-bottom: 1px solid var(--line);
  padding: 12px 8px;
  text-align: left;
  vertical-align: middle;
}

th {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0;
  background: var(--surface-strong);
}

tbody tr:hover {
  background: rgba(240, 244, 247, 0.55);
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr.detail-row:hover {
  background: transparent;
}

tr.request-summary-row {
  cursor: pointer;
}

tr.request-summary-row:focus-visible {
  outline: 3px solid var(--accent-soft);
  outline-offset: -3px;
}

tr.request-summary-row.is-open {
  background: rgba(240, 244, 247, 0.75);
}

tr.request-summary-row.is-open td {
  border-bottom-color: transparent;
}

tr.detail-row td {
  background: #fbfcfd;
  padding: 0 8px 12px;
}

tr.detail-row.is-hidden {
  display: none;
}

td.num,
th.num {
  text-align: right;
}

.status {
  align-items: center;
  border-radius: 999px;
  display: inline-flex;
  font-size: 12px;
  font-weight: 650;
  line-height: 1;
  min-height: 22px;
  padding: 3px 8px;
}

.status.active,
.status.ok {
  background: #ccfbf1;
  color: #115e59;
}

.status.warn {
  background: #fef3c7;
  color: #92400e;
}

.status.disabled,
.status.error {
  background: #fee2e2;
  color: #991b1b;
}

.check-list {
  display: grid;
}

.check-row {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  gap: 18px;
  justify-content: space-between;
  padding: 14px 0;
}

.check-row:first-child {
  padding-top: 0;
}

.check-row:last-child {
  border-bottom: 0;
  padding-bottom: 0;
}

.check-name {
  font-weight: 700;
  margin-bottom: 3px;
}

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

.config-grid > div {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  border-radius: 6px;
  min-width: 0;
  padding: 12px;
}

.config-grid dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 5px;
}

.config-grid dd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  margin: 0;
  overflow-wrap: anywhere;
}

.diagnostic-response {
  background: #0f172a;
  border-radius: 8px;
  color: #e2e8f0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  overflow-wrap: anywhere;
  padding: 14px;
  white-space: pre-wrap;
}

.form-grid {
  display: grid;
  gap: 12px;
  grid-template-columns: repeat(5, minmax(0, 1fr));
  align-items: end;
}

label {
  color: var(--muted);
  display: grid;
  gap: 5px;
  font-size: 12px;
}

input {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  min-height: 36px;
  padding: 8px 10px;
  width: 100%;
}

textarea {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  color: var(--text);
  font: inherit;
  min-height: 76px;
  padding: 9px 10px;
  resize: vertical;
  width: 100%;
}

input:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
  outline: none;
}

input:disabled,
textarea:disabled,
button:disabled {
  cursor: not-allowed;
  opacity: 0.58;
}

.key-box {
  background: #0f172a;
  border-radius: 8px;
  color: #e2e8f0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  margin-bottom: 16px;
  overflow-wrap: anywhere;
  padding: 14px;
}

.key-row {
  align-items: center;
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-bottom: 7px;
}

.key-prefix,
.mono-soft {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
}

.key-prefix {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 4px 7px;
}

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

.action-stack {
  display: grid;
  gap: 8px;
  justify-content: end;
}

.empty-row {
  color: var(--muted);
  padding: 22px 8px;
}

.request-detail {
  border: 1px solid var(--line);
  border-radius: 8px;
  margin-top: 4px;
  overflow: hidden;
}

.request-detail-body {
  display: grid;
  gap: 14px;
  padding: 14px;
}

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

.detail-grid div {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 6px;
  padding: 10px;
}

.detail-grid dt {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  margin-bottom: 4px;
}

.detail-grid dd {
  margin: 0;
  overflow-wrap: anywhere;
}

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

.payload-block {
  min-width: 0;
}

.payload-block h3 {
  font-size: 13px;
  margin: 0 0 8px;
}

.payload-block pre {
  background: #0f172a;
  border-radius: 8px;
  color: #e2e8f0;
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 12px;
  line-height: 1.55;
  margin: 0;
  max-height: 420px;
  overflow: auto;
  padding: 12px;
  white-space: pre-wrap;
  word-break: break-word;
}

.nowrap {
  white-space: nowrap;
}

.breadcrumb {
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 6px;
}

.breadcrumb a,
.inline-link {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
}

.chat-layout {
  align-items: start;
  display: grid;
  gap: 18px;
  grid-template-columns: minmax(240px, 320px) minmax(0, 1fr);
}

.chat-settings {
  display: grid;
  gap: 15px;
  position: sticky;
  top: 82px;
}

.chat-settings .panel-head {
  margin-bottom: 0;
}

.field-help {
  color: var(--muted);
  font-size: 11px;
  margin-top: -9px;
}

.chat-facts {
  border-top: 1px solid var(--line);
  display: grid;
  gap: 9px;
  margin: 2px 0 0;
  padding-top: 14px;
}

.chat-facts > div {
  align-items: start;
  display: flex;
  gap: 12px;
  justify-content: space-between;
}

.chat-facts dt {
  color: var(--muted);
  font-size: 11px;
}

.chat-facts dd {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 11px;
  margin: 0;
  max-width: 65%;
  overflow-wrap: anywhere;
  text-align: right;
}

.chat-panel {
  display: flex;
  flex-direction: column;
  min-height: 650px;
}

.chat-panel .panel-head {
  border-bottom: 1px solid var(--line);
  margin: -2px 0 0;
  padding: 0 0 14px;
}

.chat-panel .panel-head h2 {
  margin-bottom: 3px;
}

.chat-transcript {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 14px;
  max-height: 640px;
  min-height: 390px;
  overflow-y: auto;
  padding: 20px 2px;
}

.chat-empty {
  align-items: center;
  color: var(--muted);
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 5px;
  justify-content: center;
  text-align: center;
}

.chat-empty strong {
  color: var(--text);
}

.chat-message {
  align-self: flex-start;
  max-width: min(82%, 720px);
}

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

.chat-message-role {
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
  margin: 0 7px 5px;
}

.chat-message.user .chat-message-role {
  text-align: right;
}

.chat-message-body {
  background: var(--surface-strong);
  border: 1px solid var(--line);
  border-radius: 4px 12px 12px;
  overflow-wrap: anywhere;
  padding: 11px 13px;
  white-space: pre-wrap;
}

.chat-message.user .chat-message-body {
  background: var(--accent);
  border-color: var(--accent);
  border-radius: 12px 4px 12px 12px;
  color: #ffffff;
}

.chat-message-meta {
  color: var(--muted);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: 10px;
  margin: 5px 7px 0;
}

.chat-error {
  margin-bottom: 12px;
}

.chat-composer {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.chat-composer textarea {
  min-height: 86px;
}

.chat-compose-actions {
  align-items: center;
  display: flex;
  gap: 12px;
  justify-content: space-between;
  margin-top: 8px;
}

.sr-only {
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  height: 1px;
  overflow: hidden;
  position: absolute;
  white-space: nowrap;
  width: 1px;
}

@media (max-width: 900px) {
  .stat-grid,
  .form-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

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

  .chat-settings {
    position: static;
  }

  .page-head {
    display: block;
  }

  .page-head .toolbar,
  .page-head > .button {
    margin-top: 14px;
  }
}

@media (max-width: 640px) {
  .topbar,
  .nav {
    align-items: flex-start;
    flex-direction: column;
    height: auto;
  }

  .topbar {
    padding: 14px 18px;
  }

  .container {
    padding: 18px;
  }

  .stat-grid,
  .form-grid,
  .payload-grid,
  .config-grid {
    grid-template-columns: 1fr;
  }

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

  .chat-panel {
    min-height: 560px;
  }

  .chat-message {
    max-width: 92%;
  }

  .chat-compose-actions {
    align-items: stretch;
    flex-direction: column;
  }

  table {
    display: block;
    overflow-x: auto;
  }
}
