:root {
  color-scheme: light;
  --bg: #f5f7fb;
  --panel: #ffffff;
  --panel-soft: #f9fbfd;
  --border: #d7dde8;
  --border-strong: #b8c2d2;
  --text: #17202a;
  --muted: #637083;
  --primary: #15616d;
  --primary-strong: #0f4d56;
  --primary-soft: #e7f3f5;
  --accent: #c85a2e;
  --danger: #b42318;
  --ok: #157347;
  --shadow: 0 8px 24px rgba(23, 32, 42, 0.08);
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

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

button,
input,
textarea {
  font: inherit;
}

button {
  min-height: 34px;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  cursor: pointer;
  transition:
    background 120ms ease,
    border-color 120ms ease,
    color 120ms ease,
    transform 120ms ease;
}

button:hover:not(:disabled) {
  border-color: var(--primary);
}

button:active:not(:disabled) {
  transform: translateY(1px);
}

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

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

.primary:hover:not(:disabled) {
  border-color: var(--primary-strong);
  background: var(--primary-strong);
}

.ghost {
  background: transparent;
}

.auth-gate {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
  background: var(--bg);
}

.auth-gate[hidden] {
  display: none;
}

.auth-card {
  display: grid;
  gap: 14px;
  width: 100%;
  max-width: 380px;
  padding: 28px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: var(--shadow);
}

.auth-card h1 {
  margin: 0;
  font-size: 20px;
}

.auth-sub {
  margin: 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.auth-error {
  margin: 0;
  color: var(--danger);
  font-size: 12px;
}

.auth-card button {
  margin-top: 4px;
}

.danger {
  border-color: #e5b8b4;
  color: var(--danger);
}

.app {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  height: 100vh;
  overflow: hidden;
}

header.appbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: #ffffff;
}

h1 {
  margin: 0;
  font-size: 20px;
  line-height: 1.2;
}

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

.status {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-width: 150px;
  justify-content: flex-end;
  color: var(--muted);
  font-size: 13px;
}

.dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  background: var(--muted);
}

.dot.ready,
.dot.connected {
  background: var(--ok);
}

.dot.busy {
  background: var(--accent);
}

.dot.error {
  background: var(--danger);
}

main {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr) 360px;
  gap: 14px;
  min-height: 0;
  overflow: hidden;
  padding: 14px;
}

.panel {
  min-width: 0;
  min-height: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  box-shadow: var(--shadow);
}

.settings {
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
}

.settings-scroll {
  overflow: auto;
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

.section {
  display: grid;
  gap: 10px;
}

.section h2 {
  margin: 0;
  font-size: 13px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}

.section-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.section-head button {
  min-height: 28px;
  padding: 0 10px;
  font-size: 12px;
}

label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.3;
}

input,
textarea {
  width: 100%;
  border: 1px solid var(--border-strong);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  outline: none;
}

input {
  height: 36px;
  padding: 0 10px;
}

textarea {
  min-height: 84px;
  max-height: 220px;
  resize: vertical;
  padding: 10px;
  line-height: 1.4;
}

input:focus,
textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(21, 97, 109, 0.14);
}

.transport {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.transport button[aria-pressed="true"] {
  border-color: var(--primary);
  background: var(--primary-soft);
  color: var(--primary-strong);
}

.feature-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.check {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 32px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  color: var(--text);
  background: var(--panel-soft);
  font-size: 13px;
}

.check input {
  width: 16px;
  height: 16px;
  padding: 0;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.45;
}

.session-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-height: 360px;
  overflow: auto;
}

.session-empty {
  margin: 0;
  padding: 12px;
  color: var(--muted);
  font-size: 12px;
  border: 1px dashed var(--border);
  border-radius: 8px;
  text-align: center;
  background: var(--panel-soft);
}

.session-item {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 4px;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel-soft);
  cursor: pointer;
}

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

.session-item.active {
  border-color: var(--primary);
  background: var(--primary-soft);
}

.session-item-main {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.session-item-title {
  font-size: 13px;
  line-height: 1.25;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-item-id {
  color: var(--muted);
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
  font-size: 11px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-item-del {
  min-height: 26px;
  min-width: 26px;
  padding: 0 6px;
  font-size: 14px;
  line-height: 1;
  border-color: transparent;
  background: transparent;
  color: var(--muted);
}

.session-item-del:hover {
  border-color: #e5b8b4;
  color: var(--danger);
}

.chat {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
}

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

.chatbar h2 {
  margin: 0;
  font-size: 15px;
}

.session-label {
  min-width: 0;
  color: var(--muted);
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
  font-size: 12px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-height: 0;
  padding: 16px;
  overflow: auto;
  background: #fbfcff;
}

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

.empty strong {
  display: block;
  margin-bottom: 4px;
  color: var(--text);
}

.msg {
  display: grid;
  gap: 5px;
  width: fit-content;
  max-width: min(76ch, 88%);
}

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

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

.bubble {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  background: #ffffff;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.user .bubble {
  border-color: var(--primary);
  background: var(--primary);
  color: #ffffff;
}

.system .bubble {
  border-color: #efd4b6;
  background: #fff8f0;
}

.bubble.error {
  border-color: #e5b8b4;
  background: #fff4f2;
  color: var(--danger);
}

.meta {
  color: var(--muted);
  font-size: 11px;
}

.user .meta {
  text-align: right;
}

.widget {
  display: grid;
  gap: 8px;
}

.widget-title {
  font-weight: 700;
}

.json {
  max-height: 260px;
  overflow: auto;
  margin: 0;
  padding: 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #f4f6f8;
  color: #25313f;
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.chip {
  min-height: 30px;
  padding: 0 10px;
  border-color: #bfd7dd;
  background: #edf8fa;
  color: var(--primary-strong);
}

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

.composer-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pending {
  color: var(--accent);
  font-size: 12px;
}

.send-wrap {
  display: flex;
  gap: 8px;
}

.send-wrap button {
  padding: 0 14px;
}

.attach-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  padding: 2px 0 4px;
}

.attach-thumb {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  background: var(--primary-soft);
  border: 1px solid #bfd7dd;
  border-radius: 8px;
  padding: 4px 8px 4px 4px;
  font-size: 12px;
  max-width: 160px;
}

.attach-thumb img {
  width: 36px;
  height: 36px;
  object-fit: cover;
  border-radius: 4px;
  flex-shrink: 0;
}

.attach-thumb-icon {
  width: 36px;
  height: 36px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #d0e8ee;
  border-radius: 4px;
  font-size: 18px;
  flex-shrink: 0;
}

.attach-thumb-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  flex: 1;
  min-width: 0;
  color: var(--primary-strong);
}

.attach-remove {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 18px;
  height: 18px;
  min-height: unset;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  border: 1px solid #fff;
  font-size: 11px;
  line-height: 1;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.msg-attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 6px;
}

.msg-attachment {
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid var(--border);
  display: inline-block;
}

.msg-attachment img {
  display: block;
  max-width: 200px;
  max-height: 150px;
  object-fit: cover;
}

.msg-attachment-pdf {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: var(--primary-soft);
  font-size: 12px;
  color: var(--primary-strong);
}

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

.logbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}

.logbar h2 {
  margin: 0;
  font-size: 15px;
}

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

.log-actions button {
  min-height: 30px;
  padding: 0 10px;
  font-size: 12px;
}

#eventLog {
  margin: 0;
  padding: 14px;
  min-height: 0;
  overflow: auto;
  color: #25313f;
  background: #f4f6f8;
  font-family:
    ui-monospace, SFMono-Regular, Menlo, Consolas, "Liberation Mono",
    monospace;
  font-size: 12px;
  line-height: 1.45;
  white-space: pre-wrap;
}

[hidden] {
  display: none !important;
}

@media (max-width: 1160px) {
  main {
    grid-template-columns: 300px minmax(0, 1fr);
  }

  .log {
    grid-column: 1 / -1;
    min-height: 240px;
  }
}

@media (max-width: 760px) {
  header.appbar {
    align-items: flex-start;
    flex-direction: column;
  }

  .status {
    justify-content: flex-start;
  }

  main {
    grid-template-columns: 1fr;
    padding: 10px;
  }

  .chat {
    min-height: 60vh;
  }

  .msg {
    max-width: 96%;
  }

  .composer-actions,
  .chatbar {
    align-items: stretch;
    flex-direction: column;
  }

  .send-wrap {
    display: grid;
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }
}
