:root {
  color-scheme: light;
  --ink: #0d0d0d;
  --muted: #6b7280;
  --line: #ececec;
  --paper: #ffffff;
  --sidebar: #f9f9f9;
  --sidebar-hover: #eeeeee;
  --surface: #ffffff;
  --soft: #f4f4f5;
  --accent: #c13a24;
  --accent-hover: #a9311f;
  --accent-soft: #fff0ec;
  --accent-line: #f2c2b8;
  --green: var(--accent);
  --blue: var(--accent-soft);
  --coral: #c13a24;
  --amber: #a85b12;
  --shadow: 0 8px 28px rgba(0, 0, 0, 0.08);
  --composer-reserve: 132px;
}

body.composer-has-preview {
  --composer-reserve: 280px;
}

body.composer-has-audio {
  --composer-reserve: 182px;
}

body.composer-has-preview.composer-has-audio {
  --composer-reserve: 318px;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  color: var(--ink);
  background: var(--paper);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
textarea {
  font: inherit;
}

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

.app-shell {
  min-height: 100vh;
  min-height: 100dvh;
  display: grid;
  grid-template-columns: 260px minmax(0, 1fr);
  background: var(--paper);
}

.sidebar {
  height: 100vh;
  height: 100dvh;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  border-right: 1px solid var(--line);
  background: var(--sidebar);
  color: var(--ink);
}

.sidebar-top {
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding: 18px 8px 10px;
}

.sidebar-brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 0 10px 12px;
  font-size: 19px;
  font-weight: 700;
}

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

.hex-mark,
.avatar {
  width: 30px;
  height: 30px;
  display: grid;
  place-items: center;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--accent);
  color: #ffffff;
  font-size: 13px;
  font-weight: 800;
}

.sidebar-nav {
  display: grid;
  gap: 2px;
  padding: 0 8px;
}

.nav-item,
.recent-item {
  width: 100%;
  min-height: 36px;
  display: flex;
  align-items: center;
  gap: 10px;
  border: 0;
  border-radius: 8px;
  padding: 8px 10px;
  color: var(--ink);
  background: transparent;
  text-align: left;
  cursor: pointer;
  font-size: 14px;
}

.nav-item:hover,
.recent-item:hover,
.recent-item.active {
  background: var(--sidebar-hover);
}

.nav-item svg {
  width: 18px;
  height: 18px;
}

.ghost-icon {
  width: 32px;
  height: 32px;
  border: 0;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: #5f6368;
  background: transparent;
  cursor: pointer;
}

.ghost-icon:hover {
  background: var(--sidebar-hover);
}

.ghost-icon svg {
  width: 18px;
  height: 18px;
}

.recent-section {
  min-height: 0;
  overflow: hidden;
  padding: 0 8px;
}

.recent-section h2 {
  margin: 14px 8px 8px;
  color: #8a8f98;
  font-size: 13px;
  font-weight: 600;
}

.recent-list {
  display: grid;
  gap: 2px;
  max-height: calc(100vh - 260px);
  overflow-y: auto;
  padding-right: 2px;
}

.recent-item {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.sidebar-account {
  min-height: 64px;
  display: grid;
  grid-template-columns: 32px minmax(0, 1fr) 32px;
  align-items: center;
  gap: 10px;
  border-top: 1px solid var(--line);
  padding: 10px 14px;
}

.account-avatar {
  width: 32px;
  height: 32px;
  display: grid;
  place-items: center;
  border-radius: 50%;
  color: #ffffff;
  background: var(--accent);
  font-size: 12px;
  font-weight: 700;
}

.sidebar-account strong,
.sidebar-account span {
  display: block;
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
}

.sidebar-account strong {
  font-size: 14px;
}

.sidebar-account span {
  margin-top: 2px;
  color: var(--muted);
  font-size: 12px;
}

.chat-area {
  width: 100%;
  min-width: 0;
  display: grid;
  grid-template-rows: auto 1fr auto;
  height: 100vh;
  height: 100dvh;
}

.topbar {
  min-height: 58px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 16px;
  padding: 10px 18px;
  background: var(--paper);
}

.mobile-brand {
  display: none;
  margin-right: auto;
  font-weight: 700;
}

.top-action {
  min-height: 34px;
  display: inline-flex;
  align-items: center;
  gap: 7px;
  border: 0;
  border-radius: 8px;
  padding: 0 9px;
  color: #5f6368;
  background: transparent;
  cursor: pointer;
  font-size: 14px;
}

.top-action:hover {
  background: var(--soft);
}

.top-action svg {
  width: 17px;
  height: 17px;
}

.messages {
  overflow-y: auto;
  padding: 30px 24px var(--composer-reserve);
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: min(100%, 800px);
  margin: 0 auto;
}

.message {
  display: flex;
  align-items: flex-start;
  gap: 8px;
  width: 100%;
}

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

.message.user .avatar {
  display: none;
}

.bubble {
  border: 0;
  border-radius: 18px;
  padding: 8px 10px;
  background: transparent;
  box-shadow: none;
  line-height: 1.65;
  white-space: pre-wrap;
  max-width: min(650px, calc(100vw - 96px));
}

.message.assistant .bubble {
  padding-left: 0;
  padding-right: 0;
  white-space: normal;
}

.message.user .bubble {
  color: #0f172a;
  background: var(--blue);
}

.message.user .bubble.has-media,
.message.user .bubble.has-voice {
  background: transparent;
  padding: 0;
  display: grid;
  gap: 8px;
  justify-items: end;
}

.message-text {
  margin: 0;
}

.bubble.has-media .message-text,
.bubble.has-voice .message-text {
  width: fit-content;
  max-width: min(520px, calc(100vw - 96px));
  margin-left: auto;
  border-radius: 18px;
  padding: 8px 12px;
  background: var(--blue);
}

.message-image {
  display: block;
  width: min(380px, calc(100vw - 96px));
  max-height: 128px;
  object-fit: cover;
  object-position: top left;
  border: 1px solid rgba(193, 58, 36, 0.08);
  border-radius: 26px;
  background: #ffffff;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.04);
}

.voice-message {
  min-width: 148px;
  height: 42px;
  border: 0;
  border-radius: 21px;
  padding: 0 14px 0 16px;
  display: inline-flex;
  align-items: center;
  justify-content: flex-end;
  gap: 9px;
  color: #0f172a;
  background: var(--blue);
  cursor: pointer;
}

.voice-message:hover {
  background: #ffe5dd;
}

.voice-message svg {
  width: 18px;
  height: 18px;
  color: var(--accent);
}

.voice-bars {
  width: 32px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 3px;
}

.voice-bars span {
  width: 3px;
  border-radius: 999px;
  background: var(--accent);
}

.voice-bars span:nth-child(1) {
  height: 8px;
}

.voice-bars span:nth-child(2) {
  height: 14px;
}

.voice-bars span:nth-child(3) {
  height: 10px;
}

.voice-message.playing .voice-bars span {
  animation: voice-pulse 720ms ease-in-out infinite;
}

.voice-message.playing .voice-bars span:nth-child(2) {
  animation-delay: 120ms;
}

.voice-message.playing .voice-bars span:nth-child(3) {
  animation-delay: 240ms;
}

.voice-duration {
  min-width: 24px;
  color: #475569;
  font-size: 13px;
  text-align: right;
}

@keyframes voice-pulse {
  0%,
  100% {
    transform: scaleY(0.65);
  }

  50% {
    transform: scaleY(1.18);
  }
}

.markdown-body {
  white-space: normal;
}

.markdown-body > :first-child {
  margin-top: 0;
}

.markdown-body > :last-child {
  margin-bottom: 0;
}

.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body blockquote,
.markdown-body pre,
.markdown-body table {
  margin: 0 0 12px;
}

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin: 16px 0 8px;
  line-height: 1.35;
}

.markdown-body h1 {
  font-size: 20px;
}

.markdown-body h2 {
  font-size: 18px;
}

.markdown-body h3,
.markdown-body h4 {
  font-size: 16px;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 22px;
}

.markdown-body li + li {
  margin-top: 4px;
}

.markdown-body a {
  color: var(--accent);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

.markdown-body code {
  border: 1px solid #d9e2ec;
  border-radius: 6px;
  background: #f4f7fb;
  padding: 2px 5px;
  font-family: "SFMono-Regular", Consolas, "Liberation Mono", monospace;
  font-size: 0.92em;
}

.markdown-body pre {
  overflow-x: auto;
  border: 1px solid #d9e2ec;
  border-radius: 8px;
  background: #f4f7fb;
  padding: 12px;
}

.markdown-body pre code {
  border: 0;
  background: transparent;
  padding: 0;
}

.markdown-body blockquote {
  border-left: 4px solid var(--line);
  color: var(--muted);
  padding-left: 12px;
}

.markdown-body table {
  width: 100%;
  border-collapse: collapse;
  overflow: hidden;
  font-size: 14px;
}

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--line);
  padding: 8px 10px;
  text-align: left;
  vertical-align: top;
}

.markdown-body th {
  background: #f5f7fa;
  font-weight: 700;
}

.markdown-body hr {
  border: 0;
  border-top: 1px solid var(--line);
  margin: 16px 0;
}

.markdown-body .katex {
  font-size: 1.05em;
}

.markdown-body .katex-display {
  overflow-x: auto;
  overflow-y: hidden;
  margin: 12px 0;
  padding: 2px 0 6px;
  text-align: left;
}

.markdown-body .katex-display > .katex {
  display: inline-block;
  min-width: max-content;
}

.composer {
  position: fixed;
  right: 0;
  bottom: 0;
  left: 260px;
  background: linear-gradient(to top, #ffffff 78%, rgba(255, 255, 255, 0));
  padding: 26px 18px calc(20px + env(safe-area-inset-bottom));
}

.composer-box {
  width: min(768px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 8px;
  border: 1px solid #e3e3e3;
  border-radius: 28px;
  background: #ffffff;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.08);
  padding: 8px;
}

.preview {
  position: relative;
  width: 148px;
  height: 148px;
  margin: 0;
}

.preview img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border: 1px solid var(--line);
  border-radius: 18px;
}

.preview button {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 30px;
  height: 30px;
  border: 0;
  border-radius: 999px;
  color: white;
  background: rgba(24, 32, 42, 0.72);
  display: grid;
  place-items: center;
  cursor: pointer;
}

.audio-preview {
  width: 100%;
  min-height: 36px;
  margin: 0;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid var(--accent-line);
  border-radius: 999px;
  padding: 6px 8px 6px 12px;
  color: var(--accent);
  background: var(--accent-soft);
  font-size: 14px;
}

.audio-preview[hidden] {
  display: none;
}

.audio-preview svg {
  width: 17px;
  height: 17px;
}

.audio-preview button {
  width: 26px;
  height: 26px;
  margin-left: auto;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  color: var(--accent);
  background: transparent;
  cursor: pointer;
}

.audio-preview button:hover {
  background: rgba(193, 58, 36, 0.1);
}

.composer-row {
  width: 100%;
  margin: 0;
  display: grid;
  grid-template-columns: 44px minmax(0, 1fr) 38px 48px;
  gap: 8px;
  align-items: center;
  border: 0;
  border-radius: 0;
  background: #ffffff;
  box-shadow: none;
  padding: 0;
}

.icon-button {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  display: grid;
  place-items: center;
  background: transparent;
  color: var(--ink);
  cursor: pointer;
}

.icon-button:hover {
  background: var(--soft);
}

.upload-button input {
  display: none;
}

.send-button {
  width: 38px;
  background: var(--accent);
  color: white;
}

.send-button:hover {
  background: var(--accent-hover);
}

.mic-button.recording {
  color: var(--accent);
  background: var(--accent-soft);
}

textarea {
  width: 100%;
  max-height: 170px;
  min-height: 38px;
  resize: none;
  border: 0;
  border-radius: 18px;
  padding: 8px 10px;
  line-height: 1.5;
  color: var(--ink);
  background: white;
  outline: none;
}

textarea::placeholder {
  color: #8a8f98;
}

button:focus-visible,
a:focus-visible,
.upload-button:focus-within {
  outline: 2px solid rgba(193, 58, 36, 0.35);
  outline-offset: 2px;
}

button:disabled {
  cursor: wait;
  opacity: 0.72;
}

.thinking {
  position: fixed;
  left: calc(50% + 130px);
  top: 18%;
  transform: translateX(-50%);
  color: #c0c4cc;
  font-size: 16px;
}

@media (max-width: 820px) {
  :root {
    --composer-reserve: calc(108px + env(safe-area-inset-bottom));
  }

  body.composer-has-preview {
    --composer-reserve: calc(202px + env(safe-area-inset-bottom));
  }

  body.composer-has-audio {
    --composer-reserve: calc(146px + env(safe-area-inset-bottom));
  }

  body.composer-has-preview.composer-has-audio {
    --composer-reserve: calc(240px + env(safe-area-inset-bottom));
  }

  body {
    overflow: hidden;
  }

  .app-shell {
    grid-template-columns: 1fr;
    height: 100dvh;
    min-height: 0;
  }

  .sidebar {
    display: none;
  }

  .chat-area {
    height: 100dvh;
    min-height: 0;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .topbar {
    min-height: calc(56px + env(safe-area-inset-top));
    justify-content: space-between;
    border-bottom: 1px solid var(--line);
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  }

  .mobile-brand {
    display: block;
  }

  .top-action {
    min-height: 34px;
    padding: 0 8px;
  }

  .top-action span {
    display: none;
  }

  .messages {
    width: 100%;
    min-height: 0;
    padding: 18px 14px var(--composer-reserve);
    gap: 18px;
    -webkit-overflow-scrolling: touch;
  }

  .message {
    gap: 8px;
  }

  .avatar {
    width: 28px;
    height: 28px;
    font-size: 12px;
  }

  .bubble {
    max-width: calc(100vw - 62px);
    padding: 7px 10px;
    font-size: 15px;
    line-height: 1.62;
  }

  .message.assistant .bubble {
    max-width: calc(100vw - 64px);
  }

  .message.user .bubble {
    max-width: min(82vw, 320px);
  }

  .bubble.has-media .message-text,
  .bubble.has-voice .message-text {
    max-width: min(82vw, 320px);
  }

  .message-image {
    width: min(330px, 82vw);
    max-height: 118px;
    border-radius: 22px;
  }

  .voice-message {
    min-width: 136px;
    max-width: 82vw;
    height: 40px;
    border-radius: 20px;
  }

  .composer {
    left: 0;
    padding: 10px 10px calc(10px + env(safe-area-inset-bottom));
    background: linear-gradient(to top, #ffffff 86%, rgba(255, 255, 255, 0));
  }

  .composer-box {
    width: 100%;
    border-radius: 24px;
    padding: 6px;
  }

  .composer-row {
    grid-template-columns: 38px minmax(0, 1fr) 36px 38px;
    gap: 4px;
  }

  .icon-button,
  .send-button {
    width: 36px;
    height: 36px;
  }

  .composer-row svg {
    width: 19px;
    height: 19px;
  }

  textarea {
    min-height: 36px;
    max-height: 116px;
    padding: 7px 8px;
    font-size: 16px;
    line-height: 1.45;
  }

  .preview {
    width: 112px;
    height: 112px;
  }

  .preview img {
    border-radius: 16px;
  }

  .preview button {
    width: 28px;
    height: 28px;
    top: 4px;
    right: 4px;
  }

  .audio-preview {
    min-height: 34px;
    font-size: 13px;
    padding: 5px 7px 5px 10px;
  }

  .thinking {
    left: 50%;
    width: calc(100% - 32px);
    text-align: center;
    font-size: 15px;
  }
}

@media (max-width: 390px) {
  .messages {
    padding-left: 10px;
    padding-right: 10px;
  }

  .topbar {
    padding-left: 10px;
    padding-right: 10px;
  }

  .composer {
    padding-left: 8px;
    padding-right: 8px;
  }

  .composer-row {
    grid-template-columns: 36px minmax(0, 1fr) 34px 36px;
    gap: 2px;
  }

  .icon-button,
  .send-button {
    width: 34px;
    height: 34px;
  }
}

@media (max-height: 560px) and (max-width: 820px) {
  .topbar {
    min-height: calc(48px + env(safe-area-inset-top));
  }

  .messages {
    padding-top: 12px;
  }
}

.admin-body {
  background: #f6f7f9;
}

.admin-shell {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 28px;
}

.admin-card {
  width: min(1100px, 100%);
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
  box-shadow: var(--shadow);
  padding: 24px;
}

.admin-card.narrow {
  width: min(440px, 100%);
}

.admin-header {
  display: flex;
  justify-content: space-between;
  align-items: flex-start;
  gap: 16px;
  padding-bottom: 20px;
  border-bottom: 1px solid var(--line);
}

.admin-header h1 {
  margin: 0;
  font-size: 22px;
}

.admin-header p {
  margin: 6px 0 0;
  color: var(--muted);
}

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

.admin-form,
.settings-grid,
.settings-section,
.api-forms {
  display: grid;
  gap: 16px;
}

.admin-form {
  margin-top: 22px;
}

.settings-grid {
  margin-top: 22px;
}

.settings-section h2 {
  margin: 0;
  font-size: 18px;
}

.admin-form label,
.settings-section label,
.api-card label {
  display: grid;
  gap: 7px;
  margin: 0;
  color: #344054;
  font-size: 14px;
}

input,
.settings-section textarea {
  width: 100%;
  min-height: 42px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 10px 12px;
  color: var(--ink);
  background: white;
  outline: none;
}

input:focus,
.settings-section textarea:focus {
  border-color: var(--green);
  box-shadow: 0 0 0 4px rgba(193, 58, 36, 0.13);
}

.primary-button,
.secondary-button {
  min-height: 42px;
  border-radius: 8px;
  padding: 0 14px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  cursor: pointer;
  text-decoration: none;
}

.primary-button {
  border: 1px solid var(--green);
  background: var(--green);
  color: white;
}

.secondary-button {
  border: 1px solid var(--line);
  background: white;
  color: var(--ink);
}

.form-message {
  min-height: 20px;
  margin: 0;
  color: var(--coral);
  font-size: 14px;
}

.api-card {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 16px;
  display: grid;
  gap: 14px;
  background: #fcfcfd;
}

.api-card-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: baseline;
}

.api-card-head h3 {
  margin: 0;
  font-size: 16px;
}

.api-card-head span {
  color: var(--muted);
  font-size: 13px;
}

.model-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 10px;
  align-items: end;
}

.model-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  min-height: 24px;
  color: var(--muted);
  font-size: 14px;
}

.model-list button {
  border: 1px solid var(--accent-line);
  border-radius: 999px;
  padding: 6px 10px;
  color: var(--accent);
  background: var(--accent-soft);
  cursor: pointer;
}

.settings-footer {
  display: flex;
  align-items: center;
  gap: 14px;
}

@media (max-width: 720px) {
  .admin-shell {
    padding: 14px;
  }

  .admin-header,
  .api-card-head,
  .settings-footer {
    flex-direction: column;
    align-items: stretch;
  }

  .model-row {
    grid-template-columns: 1fr;
  }
}
