:root {
  --bg: #0f172a;
  --surface: rgba(255, 255, 255, 0.08);
  --surface-2: rgba(255, 255, 255, 0.12);
  --surface-3: rgba(255, 255, 255, 0.16);
  --border: rgba(255, 255, 255, 0.14);
  --text: rgba(255, 255, 255, 0.92);
  --muted: rgba(255, 255, 255, 0.72);

  --primary: #7bb6ff;
  --primary-2: #4d8dff;
  --primary-soft: rgba(123, 182, 255, 0.16);

  --radius: 14px;
  --radius-sm: 12px;
  --radius-lg: 18px;
  --shadow: 0 16px 50px rgba(0, 0, 0, 0.28);

  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, Arial;

  /* JS updates this */
  --header-h: 72px;
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font);
  color: var(--text);
  background:
    radial-gradient(1200px 650px at 10% 0%, rgba(123, 182, 255, 0.22), transparent 60%),
    radial-gradient(900px 520px at 90% 10%, rgba(77, 141, 255, 0.18), transparent 55%),
    linear-gradient(180deg, #0b1020, var(--bg));
  min-height: 100vh;
}

.hidden {
  display: none;
}

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

a {
  color: inherit;
}

button,
input,
textarea {
  font: inherit;
}

#settingsHint {
  font-size: 12px;
  line-height: 1.3;
  margin-top: 12px;
  opacity: 0.9;
}

/* Header */
.site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 18px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.75);
  backdrop-filter: blur(10px);
}

.brand {
  text-decoration: none;
  color: var(--text);
  font-weight: 800;
  letter-spacing: 0.2px;
  white-space: nowrap;
}

.site-nav {
  display: flex;
  gap: 8px;
  align-items: center;
  flex-wrap: wrap;
  min-width: 0;
}

.site-nav a {
  text-decoration: none;
  color: var(--muted);
  padding: 8px 10px;
  border-radius: 10px;
  transition: background 120ms ease, color 120ms ease, border-color 120ms ease;
}

.site-nav a:hover {
  background: var(--surface);
  color: var(--text);
}

.site-nav a.active {
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--border);
}

.site-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.who {
  padding: 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  max-width: 220px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Buttons / fields */
.btn {
  appearance: none;
  padding: 10px 13px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  transition: transform 80ms ease, background 120ms ease, border-color 120ms ease;
}

.btn:hover {
  background: var(--surface-2);
}

.btn:active {
  transform: translateY(1px);
}

.btn.primary {
  background: linear-gradient(180deg, var(--primary), var(--primary-2));
  border-color: rgba(255, 255, 255, 0.18);
  color: #0b1020;
  font-weight: 800;
}

.settings-head-actions .btn {
  white-space: nowrap;
  line-height: 1;
}

#newChat.btn {
  box-shadow: none;
}

@supports selector(:has(*)) {
  #page-chats:has(.convo-link.active) #newChat.btn {
    border-color: rgba(255, 255, 255, 0.18);
    background: var(--surface);
    color: var(--text);
  }

  #page-chats:not(:has(.convo-link.active)) #newChat.btn {
    border-color: rgba(123, 182, 255, 0.5);
    background: rgba(123, 182, 255, 0.14);
  }
}

#newChat.btn:hover {
  background: rgba(123, 182, 255, 0.18);
}

.label {
  display: block;
  color: var(--muted);
}

input,
textarea {
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  outline: none;
  resize: none;
}

input:focus,
textarea:focus {
  border-color: rgba(123, 182, 255, 0.55);
  box-shadow: 0 0 0 4px rgba(123, 182, 255, 0.16);
}

/* Pages */
.page {
  padding: 16px;
  max-width: 1700px;
  margin: 0 auto;
}

.card {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  background: rgba(255, 255, 255, 0.07);
  box-shadow: var(--shadow);
}

.form-stack {
  display: flex;
  flex-direction: column;
  gap: 12px;
  max-width: 520px;
}

/* Chats page */
#page-chats {
  margin: 0;
  padding: 0;
  height: calc(100dvh - var(--header-h));
  height: calc(100vh - var(--header-h));
  overflow: hidden;
  max-width: none;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(280px, 340px) minmax(0, 1fr);
  gap: 14px;
  height: 100%;
  min-height: 0;
  padding: 16px 8px 20px 16px;
}

/* Sidebar */
.app-shell > aside {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.06);
  overflow: auto;
  padding: 12px;
  min-height: 0;
  min-width: 0;
}

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

.top-title {
  font-weight: 800;
  color: var(--text);
  opacity: 0.95;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 8px;
}

/* Main chat area */
.chat-wrap {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(420px, 560px);
  gap: 12px;
  height: 100%;
  min-height: 0;
  min-width: 0;
}

.chat-main,
.settings-panel,
.settings-body,
.settings-group,
#messages {
  min-width: 0;
  min-height: 0;
}

.chat-main {
  display: grid;
  grid-template-rows: auto 1fr auto;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
}

.chat-header {
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-title {
  font-weight: 850;
}

.chat-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

#messages {
  padding: 14px;
  overflow: auto;
  overscroll-behavior: contain;
}

.msg {
  padding: 10px 12px;
  margin: 10px 0;
  border-radius: 14px;
  max-width: min(105ch, 92%);
  border: 1px solid rgba(255, 255, 255, 0.12);
  white-space: pre-wrap;
}

.msg.user {
  background: rgba(123, 182, 255, 0.16);
  margin-left: auto;
}

.msg.assistant {
  background: rgba(255, 255, 255, 0.07);
}

.composer {
  padding: 12px 14px;
  border-top: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.08);
}

.composer-hint {
  margin-top: 8px;
  font-size: 12px;
}

#msg {
  width: 100%;
  min-height: 84px;
}

/* Conversations */
.convo-list {
  list-style: none;
  padding: 0;
  margin: 12px 0 0;
}

.convo-item {
  margin: 8px 0;
}

.convo-link {
  display: block;
  width: 100%;
  padding: 12px 14px;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.12);
  background: rgba(255, 255, 255, 0.05);
  color: var(--text);
  text-decoration: none;
  transition: background 120ms ease, border-color 120ms ease, transform 80ms ease;
  line-height: 1.35;
}

.convo-link:hover {
  background: rgba(255, 255, 255, 0.08);
}

.convo-link.active {
  border-color: rgba(123, 182, 255, 0.5);
  background: rgba(123, 182, 255, 0.14);
}

/* Settings */
.settings-panel {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: rgba(255, 255, 255, 0.06);
  overflow: hidden;
  padding: 0;
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 0;
}

.settings-header {
  padding: 12px;
  border-bottom: 1px solid var(--border);
  background: rgba(0, 0, 0, 0.08);
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 12px;
}

.settings-head-left {
  min-width: 0;
}

.settings-head-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-shrink: 0;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.settings-title {
  font-weight: 850;
}

#settingsSub {
  font-size: 12px;
  margin-top: 2px;
}

.settings-body {
  padding: 12px 18px 14px 12px;
  overflow: auto;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  align-content: start;
}

.settings-group {
  border: 1px solid rgba(255, 255, 255, 0.1);
  background: rgba(0, 0, 0, 0.06);
  border-radius: 12px;
  padding: 12px;
}

.settings-group-title {
  font-weight: 800;
  font-size: 12px;
  letter-spacing: 0.2px;
  color: var(--muted);
  margin-bottom: 10px;
  text-transform: uppercase;
}

.settings-body .settings-group:nth-child(3) {
  grid-column: 1 / -1;
  padding: 16px;
}

.label-row {
  display: grid;
  grid-template-columns: minmax(140px, 1fr) minmax(160px, 220px);
  align-items: center;
  gap: 10px;
  column-gap: 14px;
  margin: 10px 0;
}

.label-row span {
  color: var(--muted);
  font-size: 13px;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.label-col {
  display: grid;
  gap: 6px;
  margin: 10px 0;
}

.label-col span {
  color: var(--muted);
  font-size: 13px;
}

.settings-body .settings-group:nth-child(3) .label-col {
  margin: 0;
}

.settings-body .label-row input[type="number"] {
  width: 75%;
  justify-self: end;
  margin-right: 24px;
}

.settings-body textarea {
  box-sizing: border-box;
  overflow: auto;
}

#s_direct_system_prompt,
#s_rag_system_prompt {
  width: 100%;
  min-height: 220px;
}

/* Assistant markdown */
.msg.assistant .msg-main {
  white-space: normal;
}

.msg.assistant .msg-main p {
  margin: 6px 0;
}

.msg.assistant .msg-main h1,
.msg.assistant .msg-main h2,
.msg.assistant .msg-main h3 {
  margin: 10px 0 6px;
  line-height: 1.15;
}

.msg.assistant .msg-main ul,
.msg.assistant .msg-main ol {
  margin: 6px 0 6px 20px;
  padding: 0;
}

.msg.assistant .msg-main li {
  margin: 3px 0;
}

.msg.assistant .msg-main hr {
  border: 0;
  border-top: 1px solid rgba(255, 255, 255, 0.14);
  margin: 10px 0;
}

.msg.assistant .msg-main code {
  padding: 2px 6px;
  border-radius: 8px;
  background: rgba(0, 0, 0, 0.18);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.msg.assistant .msg-main pre code {
  display: block;
  padding: 10px 12px;
  overflow: auto;
}

.msg-meta {
  margin-top: 8px;
  font-size: 12px;
  line-height: 1.35;
  white-space: pre-line;
}

/* Chats page lock on desktop only */
@media (min-width: 901px) {
  html:has(#page-chats:not([hidden])),
  body:has(#page-chats:not([hidden])) {
    height: 100%;
    overflow: hidden;
  }
}

/* Feedback page */
html:has(#page-feedback:not([hidden])),
body:has(#page-feedback:not([hidden])) {
  height: 100%;
  overflow: hidden;
}

#page-feedback {
  margin: 0;
  padding: 0;
}

#page-feedback .card {
  height: calc(100vh - 120px - 28px - 60px);
  overflow: hidden;
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 12px;
}

#page-feedback .form-embed {
  overflow: auto;
}

#page-feedback .form-embed iframe {
  display: block;
  width: 100%;
  height: 100%;
  max-width: 640px;
  margin: 0 auto;
  border: 0;
}

/* Medium screens */
@media (max-width: 1200px) {
  .chat-wrap {
    grid-template-columns: minmax(0, 1fr) minmax(360px, 460px);
  }

  .settings-body {
    grid-template-columns: 1fr;
  }

  .settings-body .settings-group:nth-child(3) {
    grid-column: auto;
  }
}

/* Mobile / tablet */
@media (max-width: 900px) {
  html,
  body {
    height: auto;
    overflow: auto;
  }

  .site-header {
    grid-template-columns: 1fr;
    align-items: stretch;
    gap: 10px;
    padding: 12px 14px;
  }

  .brand {
    font-size: 18px;
  }

  .site-nav {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    gap: 8px;
    padding-bottom: 2px;
    -webkit-overflow-scrolling: touch;
  }

  .site-nav a {
    flex: 0 0 auto;
    white-space: nowrap;
  }

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

  .who {
    max-width: 100%;
  }

  .page {
    padding: 12px;
  }

  #page-chats {
    height: auto;
    min-height: calc(100dvh - var(--header-h, 0px));
    overflow: visible;
  }

  .app-shell {
    display: block;
    height: auto;
    padding: 12px;
  }

  .app-shell > aside {
    margin-bottom: 12px;
    overflow: visible;
    padding: 12px;
  }

  .top {
    padding-bottom: 8px;
  }

  .top-title {
    font-size: 14px;
  }

  .top-actions {
    gap: 6px;
  }

  /* Standard list feel on mobile, not tiles */
  .convo-list {
    display: block;
    margin-top: 10px;
  }

  .convo-item {
    margin: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .convo-item:last-child {
    border-bottom: 0;
  }

  .convo-link {
    padding: 14px 4px;
    border: 0;
    border-radius: 0;
    background: transparent;
  }

  .convo-link:hover {
    background: transparent;
  }

  .convo-link.active {
    background: transparent;
    border: 0;
    color: var(--primary);
  }

  .chat-wrap {
    display: block;
    height: auto;
  }

  .chat-main {
    min-height: 72dvh;
  }

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

  .chat-actions {
    margin-left: 0;
    width: 100%;
    justify-content: flex-start;
  }

  #messages {
    min-height: 40dvh;
    max-height: 50dvh;
    overflow: auto;
  }

  .msg {
    max-width: 100%;
  }

  .composer {
    padding: 10px 12px 12px;
  }

  #msg {
    min-height: 104px;
    font-size: 16px;
  }

  .composer .btn,
  .chat-actions .btn,
  .top .btn,
  .site-actions .btn,
  .settings-head-actions .btn {
    min-height: 44px;
  }

  /* Mobile settings overlay */
  .settings-panel {
    position: fixed;
    inset: 0;
    z-index: 100;
    width: 100%;
    max-width: none;
    height: 100dvh;
    border-radius: 0;
    border: 0;
    background:
      linear-gradient(180deg, rgba(8, 13, 28, 0.98), rgba(15, 23, 42, 0.995));
    overflow: hidden;
    display: grid;
    grid-template-rows: auto 1fr;
  }

  .settings-panel.hidden,
  .settings-panel[hidden] {
    display: none !important;
  }

  .settings-header {
    position: sticky;
    top: 0;
    z-index: 2;
    padding: 12px;
    background: rgba(11, 16, 32, 0.98);
    backdrop-filter: blur(8px);
  }

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

  .settings-body {
    grid-template-columns: 1fr;
    padding: 12px 12px 28px;
    gap: 12px;
    overflow: auto;
    min-height: 0;
    align-content: start;
  }

  .settings-body .settings-group:nth-child(3) {
    grid-column: auto;
    padding: 12px;
  }

  .label-row {
    grid-template-columns: 1fr;
    gap: 6px;
    margin: 12px 0;
  }

  .label-row span {
    white-space: normal;
    overflow: visible;
    text-overflow: clip;
  }

  .settings-body .label-row input[type="number"] {
    width: 100%;
    justify-self: stretch;
    margin-right: 0;
  }

  #s_direct_system_prompt,
  #s_rag_system_prompt {
    min-height: 180px;
  }

  #page-feedback .card {
    height: auto;
    min-height: calc(100dvh - 120px);
  }
}

/* Small phones */
@media (max-width: 560px) {
  .site-actions {
    display: grid;
    grid-template-columns: 1fr;
  }

  .chat-main {
    min-height: 74dvh;
  }

  #messages {
    max-height: 52dvh;
    padding: 12px 10px;
  }

  .chat-header,
  .composer,
  .settings-header,
  .settings-body,
  .app-shell > aside {
    padding-left: 10px;
    padding-right: 10px;
  }

  .msg {
    padding: 10px;
    border-radius: 12px;
  }
}

@media (max-width: 900px) {
  #toggleSettingsBtn {
    display: none !important;
  }

  #settingsPanel {
    display: none !important;
  }
}

/* Collapsible conversation panel */
#page-chats .app-shell > aside {
  display: grid;
  grid-template-rows: auto 1fr;
}

#page-chats .convo-list {
  max-height: 320px;
  overflow: auto;
  padding-right: 4px;
  scrollbar-gutter: stable;
}

/* Desktop: keep panel clean and contained */
@media (min-width: 901px) {
  #page-chats .app-shell > aside {
    max-height: 100%;
    overflow: hidden;
  }

  #page-chats .convo-list {
    max-height: calc(100vh - var(--header-h) - 180px);
  }
}

/* Mobile: make it feel like a dropdown panel */
@media (max-width: 900px) {
  #page-chats .app-shell > aside {
    overflow: hidden;
  }

  #page-chats .top {
    margin-bottom: 0;
  }

  #page-chats .convo-list {
    max-height: 140px;
    overflow: auto;
    margin-top: 10px;
    padding-top: 4px;
    border-top: 1px solid rgba(255, 255, 255, 0.08);
  }
}

@media (max-width: 900px) {
  html,
  body {
    height: auto;
    overflow: auto;
  }

  #page-chats {
    height: auto;
    min-height: calc(100dvh - var(--header-h, 0px));
    overflow: visible;
  }

  .app-shell {
    display: block;
    height: auto;
    min-height: 0;
    padding: 12px;
  }

  .app-shell > aside {
    overflow: visible;
    height: auto;
    max-height: none;
    margin-bottom: 12px;
  }

  .convo-list {
    max-height: none;
    overflow: visible;
    margin-top: 10px;
  }

  .convo-item {
    margin: 0;
    border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  }

  .convo-item:last-child {
    border-bottom: 0;
  }

  .convo-link {
    display: block;
    padding: 14px 4px;
    border: 0;
    border-radius: 0;
    background: transparent;
  }

  .convo-link:hover {
    background: transparent;
  }

  .convo-link.active {
    background: transparent;
    border: 0;
    color: var(--primary);
  }

  .chat-wrap {
    display: block;
    height: auto;
  }

  .chat-main {
    min-height: 70dvh;
  }

  #messages {
    min-height: 0;
    max-height: none;
    overflow: visible;
  }
}
