:root {
  color-scheme: light;
  --bg: #f4f8f6;
  --panel: #ffffff;
  --panel-muted: #edf5f1;
  --line: #d8e6df;
  --text: #14241f;
  --muted: #62736b;
  --soft: #eaf6ef;
  --mint: #dff4eb;
  --green: #2f7a5c;
  --green-dark: #215b45;
  --teal: #16858c;
  --teal-dark: #0e626b;
  --coral: #c65c4a;
  --coral-soft: #f9ded6;
  --gold: #c39a3c;
  --danger: #a83d3d;
  --shadow: 0 20px 54px rgba(22, 61, 48, 0.14);
  --soft-shadow: 0 10px 30px rgba(22, 61, 48, 0.1);
}

* {
  box-sizing: border-box;
}

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

body {
  margin: 0;
  background:
    linear-gradient(115deg, rgba(47, 122, 92, 0.13) 0 18%, transparent 18% 100%),
    linear-gradient(150deg, transparent 0 56%, rgba(198, 92, 74, 0.1) 56% 72%, transparent 72% 100%),
    repeating-linear-gradient(135deg, rgba(22, 133, 140, 0.045) 0 1px, transparent 1px 24px),
    linear-gradient(135deg, #fbfdfb 0%, var(--bg) 100%);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 15px;
  line-height: 1.45;
}

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

button {
  min-height: 40px;
  border: 0;
  border-radius: 8px;
  background: linear-gradient(135deg, var(--teal) 0%, var(--green) 100%);
  box-shadow: 0 8px 18px rgba(22, 133, 140, 0.18);
  color: #fff;
  cursor: pointer;
  padding: 0 15px;
  transition:
    background-color 0.16s ease,
    box-shadow 0.16s ease,
    transform 0.16s ease;
}

button:hover {
  background: linear-gradient(135deg, var(--teal-dark) 0%, var(--green-dark) 100%);
  box-shadow: 0 11px 22px rgba(22, 133, 140, 0.22);
  transform: translateY(-1px);
}

button:active {
  transform: translateY(0);
}

button:disabled {
  cursor: wait;
  opacity: 0.56;
  transform: none;
}

.app-shell {
  display: grid;
  grid-template-columns: 360px minmax(0, 1fr);
  height: 100dvh;
  min-height: 0;
  padding: 12px;
  gap: 12px;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  overflow-y: auto;
  border: 1px solid rgba(216, 230, 223, 0.92);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.88);
  box-shadow: var(--soft-shadow);
  padding: 16px;
  scrollbar-gutter: stable;
}

.panel-close {
  display: none;
}

.brand-visual {
  position: relative;
  flex: 0 0 auto;
  min-height: 248px;
  overflow: hidden;
  border-radius: 12px;
  background: #17201c;
  box-shadow: var(--shadow);
}

.brand-visual img {
  width: 100%;
  height: 100%;
  min-height: 248px;
  object-fit: cover;
  object-position: 58% center;
  display: block;
  transition: transform 0.35s ease;
}

.brand-visual:hover img {
  transform: scale(1.025);
}

.brand-visual::after {
  content: "";
  position: absolute;
  inset: 0;
  background:
    linear-gradient(180deg, rgba(7, 12, 10, 0.02) 0%, rgba(7, 12, 10, 0.76) 100%),
    linear-gradient(110deg, rgba(22, 133, 140, 0.2), transparent 52%);
}

.brand-overlay {
  position: absolute;
  z-index: 1;
  left: 18px;
  right: 18px;
  bottom: 18px;
  color: #fff;
}

.brand-kicker {
  width: fit-content;
  border: 1px solid rgba(255, 255, 255, 0.44);
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.16);
  backdrop-filter: blur(8px);
  font-size: 12px;
  font-weight: 700;
  padding: 4px 10px;
}

.brand-overlay h1 {
  margin: 11px 0 6px;
  font-size: 26px;
  letter-spacing: 0;
}

.brand-overlay p {
  max-width: 280px;
  margin: 0;
  color: rgba(255, 255, 255, 0.82);
}

.dialog-card {
  border: 1px solid rgba(216, 230, 223, 0.94);
  border-radius: 12px;
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 6px 18px rgba(22, 61, 48, 0.055);
  padding: 14px;
}

.history-card {
  display: flex;
  flex: 1 1 auto;
  flex-direction: column;
  min-height: 150px;
  overflow: hidden;
}

.section-title {
  margin-bottom: 11px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

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

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

.field {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
}

.field input,
.field select,
.composer textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.96);
  color: var(--text);
  outline: none;
}

.field input,
.field select {
  min-height: 40px;
  padding: 8px 10px;
}

.field input:focus,
.field select:focus,
.composer textarea:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(29, 124, 130, 0.14);
}

.access-panel {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fbfdfc;
  padding: 0;
}

.access-panel summary {
  cursor: pointer;
  color: var(--text);
  font-size: 13px;
  font-weight: 700;
  list-style-position: inside;
  padding: 10px 11px;
}

.access-panel .field,
.access-panel .check-row {
  margin: 0 11px 11px;
}

.debug-panel {
  display: none;
}

body.debug-mode .debug-panel {
  display: block;
}

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

.button-row,
.side-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.sidebar-actions {
  display: grid;
  flex: 0 0 auto;
  grid-template-columns: 1fr;
  gap: 10px;
}

.secondary-button,
.mini-button {
  background: var(--panel-muted);
  box-shadow: none;
  color: var(--text);
}

.secondary-button:hover,
.mini-button:hover {
  background: #dceee6;
  box-shadow: 0 8px 16px rgba(22, 61, 48, 0.09);
}

.status-row {
  display: flex;
  align-items: flex-start;
  gap: 10px;
}

.status-pill {
  flex: 0 0 auto;
  border-radius: 999px;
  background: var(--mint);
  color: var(--green-dark);
  font-size: 12px;
  font-weight: 800;
  line-height: 1;
  padding: 7px 10px;
}

.status-pill[data-mode="loading"] {
  background: #fff3dc;
  color: #815f18;
}

.status-pill[data-mode="error"] {
  background: #fff0ef;
  color: var(--danger);
}

.status-text,
.context-line,
.database-list {
  color: var(--muted);
  font-size: 13px;
  overflow-wrap: anywhere;
}

.database-list {
  display: flex;
  flex-wrap: wrap;
  gap: 7px;
  margin-top: 12px;
}

.database-list span {
  border-radius: 999px;
  background: var(--mint);
  color: var(--green-dark);
  font-size: 12px;
  font-weight: 700;
  padding: 5px 9px;
}

.dialog-list {
  display: grid;
  flex: 1 1 auto;
  align-content: start;
  gap: 8px;
  min-height: 0;
  max-height: none;
  overflow-y: auto;
  padding-right: 2px;
}

.dialog-empty {
  border: 1px dashed var(--line);
  border-radius: 10px;
  color: var(--muted);
  font-size: 13px;
  padding: 12px;
}

.dialog-item {
  min-height: 0;
  width: 100%;
  border: 1px solid rgba(216, 230, 223, 0.94);
  border-radius: 10px;
  background: #fbfdfc;
  box-shadow: none;
  color: var(--text);
  display: grid;
  gap: 4px;
  font-size: 13px;
  line-height: 1.28;
  overflow: hidden;
  padding: 10px 11px;
  text-align: left;
}

.dialog-item:hover {
  background: var(--panel-muted);
  box-shadow: 0 8px 16px rgba(22, 61, 48, 0.08);
}

.dialog-item.active {
  border-color: rgba(22, 133, 140, 0.34);
  background: var(--mint);
}

.dialog-title,
.dialog-meta {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.dialog-title {
  color: var(--text);
  font-weight: 700;
}

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

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-height: 0;
  height: 100%;
  overflow: hidden;
  border: 1px solid rgba(216, 230, 223, 0.92);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.74);
  box-shadow: var(--soft-shadow);
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-width: 0;
  border-bottom: 1px solid rgba(216, 230, 223, 0.86);
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.96) 0%, rgba(240, 249, 246, 0.9) 100%);
  backdrop-filter: blur(14px);
  padding: 20px 26px;
}

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

.header-label {
  color: var(--coral);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-transform: uppercase;
}

.chat-header h2 {
  margin: 3px 0 3px;
  font-size: 22px;
  letter-spacing: 0;
}

.header-actions {
  display: flex;
  flex: 0 1 auto;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 9px;
  min-width: 0;
}

.header-controls {
  display: grid;
  flex: 0 1 auto;
  gap: 6px;
  justify-items: end;
  min-width: 0;
}

.detected-base {
  max-width: 100%;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.drug-tab {
  min-height: 36px;
  border: 1px solid rgba(47, 122, 92, 0.18);
  background: #f5fbf8;
  box-shadow: none;
  color: var(--green-dark);
  font-size: 13px;
  font-weight: 800;
  padding: 0 14px;
}

.drug-tab:hover {
  background: #e4f4ec;
  box-shadow: 0 8px 16px rgba(47, 122, 92, 0.1);
}

.drug-tab.active {
  border-color: transparent;
  background: linear-gradient(135deg, var(--teal) 0%, var(--green) 100%);
  color: #fff;
}

.compact-status {
  flex: 0 0 auto;
}

.mobile-settings {
  display: none;
  background: var(--panel-muted);
  color: var(--text);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 26px;
  scrollbar-gutter: stable;
}

.side-panel::-webkit-scrollbar,
.messages::-webkit-scrollbar,
.message-body pre::-webkit-scrollbar,
.debug-details pre::-webkit-scrollbar,
.dialog-list::-webkit-scrollbar {
  width: 10px;
  height: 10px;
}

.side-panel::-webkit-scrollbar-thumb,
.messages::-webkit-scrollbar-thumb,
.message-body pre::-webkit-scrollbar-thumb,
.debug-details pre::-webkit-scrollbar-thumb,
.dialog-list::-webkit-scrollbar-thumb {
  border: 2px solid transparent;
  border-radius: 999px;
  background: rgba(47, 122, 92, 0.34);
  background-clip: padding-box;
}

.side-panel::-webkit-scrollbar-track,
.messages::-webkit-scrollbar-track,
.message-body pre::-webkit-scrollbar-track,
.debug-details pre::-webkit-scrollbar-track,
.dialog-list::-webkit-scrollbar-track {
  background: transparent;
}

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

.empty-icon {
  display: grid;
  place-items: center;
  width: 54px;
  height: 54px;
  border-radius: 14px;
  background: linear-gradient(135deg, var(--teal), var(--green));
  box-shadow: 0 12px 26px rgba(22, 133, 140, 0.2);
  color: #fff;
  font-size: 24px;
  font-weight: 800;
  margin: 0 auto 14px;
}

.empty-state h3 {
  margin: 0 0 6px;
  color: var(--text);
  font-size: 20px;
}

.empty-state p {
  margin: 0;
}

.quick-prompts {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 10px;
  margin-top: 18px;
}

.quick-prompts button {
  min-height: 38px;
}

.message {
  max-width: min(820px, 88%);
  border: 1px solid rgba(216, 230, 223, 0.94);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 10px 28px rgba(24, 37, 31, 0.08);
  padding: 14px 16px;
}

.message.user {
  align-self: flex-end;
  border-color: rgba(22, 133, 140, 0.32);
  background: linear-gradient(135deg, var(--teal) 0%, var(--green) 100%);
  color: #fff;
}

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

.message.error {
  align-self: stretch;
  max-width: 100%;
  border-color: rgba(168, 61, 61, 0.32);
  background: #fff7f7;
  color: var(--danger);
}

.message.debug {
  align-self: stretch;
  max-width: 100%;
  background: #f5f7f8;
}

.assistant-badge {
  width: fit-content;
  border-radius: 999px;
  background: var(--mint);
  color: var(--green-dark);
  font-size: 12px;
  font-weight: 800;
  margin-bottom: 10px;
  padding: 5px 9px;
}

.message-body {
  max-width: 100%;
  overflow-wrap: anywhere;
  white-space: normal;
}

.message.assistant .message-body {
  padding-right: 4px;
}

.message-body strong {
  font-weight: 800;
}

.message-body pre,
.debug-details pre {
  max-height: 260px;
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: #fbfcfb;
  color: var(--text);
  font-family: ui-monospace, SFMono-Regular, Consolas, "Liberation Mono", monospace;
  font-size: 12px;
  line-height: 1.45;
  margin: 10px 0 0;
  padding: 10px;
  white-space: pre-wrap;
}

.message-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.mini-button {
  min-height: 32px;
  border-radius: 7px;
  font-size: 12px;
  padding: 0 10px;
}

.debug-details {
  border-top: 1px solid var(--line);
  margin-top: 12px;
  padding-top: 10px;
}

.debug-details summary {
  cursor: pointer;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.kv-list {
  display: grid;
  gap: 7px;
  margin-top: 10px;
}

.kv-row {
  display: grid;
  grid-template-columns: 120px minmax(0, 1fr);
  gap: 10px;
  color: var(--muted);
  font-size: 12px;
}

.kv-row strong {
  color: var(--text);
  font-weight: 700;
  overflow-wrap: anywhere;
}

.debug-title {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  margin-top: 12px;
}

.source-list {
  display: grid;
  gap: 8px;
  margin-top: 8px;
}

.source-item {
  border-top: 1px solid var(--line);
  color: var(--muted);
  font-size: 12px;
  padding-top: 8px;
}

.composer {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 12px;
  border-top: 1px solid rgba(216, 230, 223, 0.86);
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.96) 0%, rgba(248, 253, 251, 0.94) 100%);
  padding: 18px 26px;
}

.composer textarea {
  min-height: 48px;
  max-height: 180px;
  resize: none;
  padding: 13px 14px;
}

.composer button {
  min-width: 124px;
  min-height: 48px;
}

@media (max-width: 960px) {
  .app-shell {
    height: 100dvh;
    padding: 0;
    gap: 0;
  }

  .app-shell {
    grid-template-columns: 1fr;
  }

  .side-panel {
    display: none;
    position: fixed;
    inset: 10px;
    z-index: 10;
    max-height: calc(100dvh - 20px);
    border: 1px solid rgba(216, 230, 223, 0.96);
    border-radius: 14px;
    background: rgba(255, 255, 255, 0.98);
    box-shadow: 0 24px 60px rgba(22, 61, 48, 0.22);
  }

  .side-panel.open {
    display: flex;
  }

  .panel-close {
    display: block;
    flex: 0 0 auto;
  }

  .brand-visual {
    min-height: 190px;
  }

  .brand-visual img {
    min-height: 190px;
  }

  .mobile-settings {
    display: inline-flex;
    align-items: center;
    flex: 0 0 auto;
  }

  .chat-panel {
    height: 100dvh;
    border: 0;
    border-radius: 0;
  }

  .chat-header {
    padding: 14px 16px;
    flex-wrap: wrap;
  }

  .chat-header h2 {
    font-size: 19px;
  }

  .header-actions {
    flex: 1 1 100%;
    justify-content: flex-start;
  }

  .header-controls {
    order: 3;
    flex: 1 1 100%;
    justify-items: start;
  }

  .drug-tab {
    flex: 1 1 0;
    min-width: 0;
    padding: 0 10px;
  }

  .messages {
    padding: 14px;
  }

  .message {
    max-width: 100%;
    border-radius: 12px;
  }

  .composer {
    grid-template-columns: 1fr;
    padding: 12px;
  }

  .composer button {
    width: 100%;
  }
}

@media (max-width: 560px) {
  body {
    font-size: 14px;
  }

  .chat-header {
    align-items: flex-start;
  }

  .message {
    padding: 12px;
  }

  .button-row,
  .side-actions,
  .sidebar-actions {
    grid-template-columns: 1fr;
  }
}

@media (min-width: 961px) {
  body.embed-mode .app-shell {
    grid-template-columns: 300px minmax(0, 1fr);
    padding: 8px;
    gap: 8px;
  }

  body.embed-mode .side-panel {
    padding: 12px;
  }

  body.embed-mode .brand-visual {
    min-height: 174px;
  }

  body.embed-mode .brand-visual img {
    min-height: 174px;
  }
}

@media (min-width: 961px) and (max-width: 1120px) {
  .chat-header {
    gap: 12px;
    padding: 18px 20px;
  }

  .chat-title-block {
    flex: 0 1 230px;
  }

  .chat-header h2 {
    font-size: 20px;
    white-space: nowrap;
  }

  .header-actions {
    gap: 7px;
  }

  .drug-tab {
    padding: 0 12px;
  }
}
