:root {
  color-scheme: light;
  --bg: #f7f9fb;
  --panel: #ffffff;
  --panel-strong: #eef3f8;
  --text: #172033;
  --muted: #687386;
  --line: #dbe2ea;
  --accent: #16746f;
  --accent-strong: #105b57;
  --danger: #b3261e;
  --assistant: #f0f6f4;
  --user: #eef2ff;
  --shadow: 0 20px 45px rgba(22, 32, 51, 0.12);
  font-family: "Microsoft YaHei", "Segoe UI", Arial, sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

body.settings-open {
  overflow: hidden;
}

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

button {
  cursor: pointer;
}

h1,
h2,
p {
  margin: 0;
}

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

h2 {
  font-size: 22px;
  line-height: 1.2;
}

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

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 22px;
  padding: 24px;
  background: var(--panel);
  border-right: 1px solid var(--line);
  overflow-y: auto;
}

.brand-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.brand-mark {
  display: grid;
  place-items: center;
  width: 46px;
  height: 46px;
  border-radius: 8px;
  background: #172033;
  color: #ffffff;
  font-weight: 800;
  letter-spacing: 0;
  flex: 0 0 auto;
}

.brand-row p,
.chat-header p,
.privacy-note {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
}

.settings-form,
.conversation-actions,
.history-panel {
  display: grid;
  gap: 14px;
}

label {
  display: grid;
  gap: 7px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 600;
}

label strong {
  color: var(--text);
  font-weight: 700;
}

input,
select,
textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #ffffff;
  color: var(--text);
  outline: none;
  transition: border-color 120ms ease, box-shadow 120ms ease;
}

input,
select {
  height: 42px;
  padding: 0 12px;
}

textarea {
  min-height: 74px;
  max-height: 180px;
  resize: vertical;
  padding: 12px 14px;
  line-height: 1.5;
}

input:focus,
select:focus,
textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(22, 116, 111, 0.13);
}

.secret-row,
.model-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 42px;
  gap: 8px;
}

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

.section-title h2 {
  font-size: 15px;
  line-height: 1.2;
}

.section-title .ghost-button {
  min-height: 34px;
  padding: 0 10px;
  font-size: 13px;
}

.history-list {
  display: grid;
  gap: 8px;
  max-height: 260px;
  overflow-y: auto;
  padding-right: 2px;
}

.history-item {
  display: grid;
  gap: 4px;
  width: 100%;
  min-height: 58px;
  padding: 10px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  text-align: left;
}

.history-item:hover,
.history-item.active {
  border-color: var(--accent);
  background: var(--assistant);
}

.history-title {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 700;
}

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

.settings-actions,
.conversation-actions {
  grid-template-columns: 1fr 1fr;
}

.primary-button,
.ghost-button,
.send-button,
.icon-button {
  border: 1px solid transparent;
  border-radius: 16px;
  min-height: 42px;
  padding: 0 14px;
  font-weight: 700;
}

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

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

.ghost-button,
.icon-button {
  background: var(--panel-strong);
  color: var(--text);
  border-color: var(--line);
}

.ghost-button:hover,
.icon-button:hover {
  border-color: #b8c5d3;
}

.icon-button {
  display: grid;
  place-items: center;
  width: 42px;
  padding: 0;
  font-size: 20px;
  line-height: 1;
}

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

.privacy-note {
  margin-top: auto;
  padding: 12px;
  border-radius: 8px;
  background: var(--panel-strong);
}

.chat-panel {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  min-width: 0;
  height: 100vh;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 18px 24px;
  background: rgba(255, 255, 255, 0.94);
  border-bottom: 1px solid var(--line);
}

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

.settings-toggle {
  display: none;
}

.messages {
  overflow-y: auto;
  padding: 28px;
  -webkit-overflow-scrolling: touch;
}

.empty-state {
  display: grid;
  gap: 10px;
  max-width: 620px;
  margin: 12vh auto 0;
  text-align: center;
  color: var(--muted);
}

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

.empty-state p {
  margin: 0;
}

.message {
  display: grid;
  grid-template-columns: 74px minmax(0, 760px);
  gap: 14px;
  align-items: start;
  margin: 0 auto 18px;
  max-width: 900px;
}

.message-role {
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
  line-height: 1.5;
  text-align: right;
}

.message-content {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding: 14px 16px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--assistant);
  line-height: 1.65;
  box-shadow: 0 4px 14px rgba(22, 32, 51, 0.05);
}

.message.user .message-content {
  background: var(--user);
}

.image-prompt-message .message-content {
  display: grid;
  gap: 10px;
  background: #ffffff;
}

.image-prompt-message .image-prompt {
  color: var(--text);
  font-weight: 700;
}

.message.error .message-content {
  background: #fff1f0;
  color: var(--danger);
  border-color: #f0b8b4;
}

.composer {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) 92px;
  gap: 12px;
  padding: 18px 24px 24px;
  background: rgba(255, 255, 255, 0.94);
  border-top: 1px solid var(--line);
}

.composer-main {
  display: grid;
  gap: 8px;
}

.composer-actions {
  display: grid;
  grid-template-columns: 42px 92px;
  gap: 8px;
  align-self: end;
}

.send-button {
  align-self: end;
  height: 74px;
}

.mode-switch {
  display: grid;
  grid-template-columns: 1fr;
  gap: 6px;
  align-self: end;
  padding: 4px;
  border: 1px solid var(--line);
  border-radius: 18px;
  background: var(--panel-strong);
}

.mode-button {
  min-width: 58px;
  min-height: 30px;
  padding: 0 10px;
  border: 0;
  border-radius: 14px;
  background: transparent;
  color: var(--muted);
  font-weight: 800;
}

.mode-button.active {
  background: #ffffff;
  color: var(--accent);
  box-shadow: 0 2px 8px rgba(22, 32, 51, 0.08);
}

.image-options-panel {
  display: grid;
  grid-column: 1 / -1;
  gap: 12px;
  padding: 14px;
  border: 1px solid var(--line);
  border-radius: 16px;
  background: #ffffff;
  box-shadow: 0 8px 24px rgba(22, 32, 51, 0.06);
}

.image-options-panel[hidden] {
  display: none;
}

.image-options-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

.image-options-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

.image-options-head div {
  display: grid;
  gap: 2px;
}

.image-options-head strong {
  font-size: 15px;
}

.image-options-head span {
  color: var(--muted);
  font-size: 12px;
}

.compact-button {
  min-height: 34px;
  padding: 0 12px;
  border-radius: 12px;
  font-size: 13px;
}

.image-options-grid {
  display: grid;
  grid-template-columns: repeat(6, minmax(0, 1fr));
  gap: 10px;
}

.image-options-grid label,
.advanced-options {
  gap: 6px;
}

.image-options-grid input,
.image-options-grid select {
  height: 38px;
  border-radius: 12px;
  font-size: 14px;
}

.advanced-options textarea {
  min-height: 54px;
  max-height: 120px;
  border-radius: 12px;
  font-family: Consolas, "Courier New", monospace;
  font-size: 13px;
}

.image-message .message-content {
  display: grid;
  gap: 10px;
  padding: 10px;
}

.image-message img {
  display: block;
  width: min(100%, 420px);
  border-radius: 16px;
  border: 1px solid var(--line);
}

.image-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(92px, max-content));
  gap: 10px;
}

.image-placeholder {
  display: grid;
  place-items: center;
  width: 92px;
  aspect-ratio: 1;
  border: 1px dashed #b8c5d3;
  border-radius: 14px;
  background: #f7f9fb;
  color: var(--muted);
  padding: 8px;
  text-align: center;
  font-size: 12px;
  font-weight: 700;
}

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

.attachment-item {
  position: relative;
  width: 58px;
  height: 58px;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--panel-strong);
}

.attachment-item img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.attachment-remove {
  position: absolute;
  top: 3px;
  right: 3px;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  padding: 0;
  border: 0;
  border-radius: 999px;
  background: rgba(23, 32, 51, 0.82);
  color: #ffffff;
  font-weight: 800;
  line-height: 1;
}

.attach-button[hidden] {
  display: none;
}

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

.settings-overlay {
  display: none;
}

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

@media (max-width: 820px) {
  body {
    min-height: 100dvh;
    background: #f7f9fb;
  }

  .app-shell {
    display: block;
    min-height: 100dvh;
  }

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 30;
    width: min(88vw, 360px);
    padding: calc(18px + env(safe-area-inset-top)) 18px calc(18px + env(safe-area-inset-bottom));
    border-right: 1px solid var(--line);
    border-bottom: 0;
    box-shadow: var(--shadow);
    transform: translateX(-105%);
    transition: transform 180ms ease;
  }

  body.settings-open .sidebar {
    transform: translateX(0);
  }

  .history-list {
    max-height: none;
  }

  .settings-overlay {
    position: fixed;
    inset: 0;
    z-index: 20;
    display: block;
    background: rgba(23, 32, 51, 0.36);
  }

  .settings-overlay[hidden] {
    display: none;
  }

  .chat-panel {
    height: 100dvh;
    min-height: 100dvh;
    background: #f7f9fb;
  }

  .chat-header {
    position: relative;
    z-index: 15;
    display: flex;
    align-items: center;
    gap: 10px;
    padding: calc(10px + env(safe-area-inset-top)) 12px 10px;
    background: rgba(255, 255, 255, 0.92);
    border-bottom: 1px solid rgba(219, 226, 234, 0.85);
    backdrop-filter: blur(14px);
  }

  .chat-header > div:first-child {
    display: grid;
    gap: 2px;
    min-width: 0;
  }

  .chat-header h2 {
    display: block;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 16px;
    line-height: 1.25;
  }

  .chat-header p {
    display: block;
    max-width: 52vw;
    margin: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
    line-height: 1.35;
  }

  .header-actions {
    margin-left: auto;
    gap: 8px;
  }

  .install-button,
  #clearChat {
    display: none;
  }

  .settings-toggle {
    display: inline-grid;
    place-items: center;
    width: 38px;
    min-width: 38px;
    min-height: 38px;
    padding: 0;
    border-radius: 12px;
    background: #eef3f8;
    border-color: #dbe2ea;
    font-size: 0;
  }

  .settings-toggle::before {
    content: "\2630";
    font-size: 20px;
    line-height: 1;
  }

  .messages {
    padding: 18px 14px 14px;
  }

  .empty-state {
    width: min(100%, 340px);
    margin: 7vh auto 0;
    padding: 24px 18px;
    border: 1px solid rgba(219, 226, 234, 0.9);
    border-radius: 24px;
    background: rgba(255, 255, 255, 0.72);
    box-shadow: 0 12px 34px rgba(22, 32, 51, 0.07);
  }

  .empty-state h3 {
    font-size: 21px;
    letter-spacing: 0;
  }

  .empty-state p {
    font-size: 14px;
    line-height: 1.7;
  }

  .message {
    display: flex;
    flex-direction: column;
    gap: 5px;
    margin: 0 0 14px;
    max-width: none;
  }

  .message-role {
    text-align: left;
    padding: 0 10px;
  }

  .message.user {
    align-items: flex-end;
  }

  .message.assistant,
  .message.error,
  .message.system {
    align-items: flex-start;
  }

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

  .message-content {
    max-width: min(78vw, 620px);
    padding: 12px 14px;
    border-radius: 20px;
    line-height: 1.6;
  }

  .message.user .message-content {
    border-top-right-radius: 8px;
    background: var(--accent);
    color: #ffffff;
  }

  .message.error .message-content {
    max-width: min(88vw, 520px);
    border-radius: 18px;
  }

  .image-prompt-message .message-content {
    max-width: min(72vw, 360px);
    padding: 10px;
    border-top-right-radius: 8px;
    background: #ffffff;
    color: var(--text);
  }

  .image-prompt-message .image-prompt {
    color: var(--text);
    font-size: 14px;
    line-height: 1.45;
  }

  .image-prompt-message .image-grid {
    grid-template-columns: repeat(auto-fit, minmax(82px, max-content));
  }

  .image-prompt-message .image-grid img,
  .image-prompt-message .image-placeholder {
    width: 82px;
    border-radius: 14px;
  }

  .message.assistant .message-content,
  .message.system .message-content {
    border-top-left-radius: 8px;
  }

  .composer {
    grid-template-columns: minmax(0, 1fr) auto;
    align-items: end;
    gap: 8px;
    padding: 8px 12px calc(8px + env(safe-area-inset-bottom));
    background: rgba(255, 255, 255, 0.94);
    border-top: 1px solid rgba(219, 226, 234, 0.85);
    backdrop-filter: blur(14px);
  }

  .image-options-panel {
    gap: 10px;
    padding: 10px;
    border-radius: 14px;
  }

  .image-options-panel:not(.is-expanded) {
    padding: 8px 10px;
  }

  .image-options-panel:not(.is-expanded) .image-options-grid,
  .image-options-panel:not(.is-expanded) .advanced-options {
    display: none;
  }

  .image-options-head strong {
    font-size: 14px;
  }

  .image-options-actions .compact-button {
    min-height: 32px;
    padding-inline: 10px;
  }

  .image-options-grid {
    grid-template-columns: repeat(3, minmax(0, 1fr));
    gap: 8px;
  }

  .image-options-grid input,
  .image-options-grid select {
    height: 36px;
    padding-inline: 8px;
    font-size: 12px;
  }

  .advanced-options textarea {
    min-height: 44px;
    max-height: 80px;
    font-size: 12px;
  }

  .mode-switch {
    grid-column: 1 / -1;
    grid-row: 1;
    display: flex;
    width: max-content;
    gap: 3px;
    padding: 3px;
    border-radius: 14px;
  }

  .mode-button {
    min-width: 44px;
    min-height: 28px;
    padding: 0 10px;
    border-radius: 11px;
    font-size: 13px;
  }

  .composer-main {
    grid-column: 1;
    gap: 6px;
  }

  .composer-actions {
    grid-column: 2;
    grid-template-columns: 48px;
    gap: 6px;
  }

  .composer textarea {
    min-height: 44px;
    max-height: 104px;
    border-radius: 16px;
    padding: 10px 12px;
    font-size: 14px;
    line-height: 1.55;
  }

  .send-button {
    width: 44px;
    height: 44px;
    min-height: 44px;
    padding: 0;
    border-radius: 14px;
    font-size: 0;
  }

  .send-button::before {
    content: "\2191";
    font-size: 22px;
    font-weight: 900;
    line-height: 1;
  }

  .attachment-tray {
    gap: 6px;
  }

  .attachment-item {
    width: 48px;
    height: 48px;
    border-radius: 12px;
  }

  .attachment-remove {
    width: 18px;
    height: 18px;
    font-size: 14px;
  }

  .image-message img {
    width: min(100%, 78vw);
  }

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

@media (max-width: 420px) {
  .settings-actions,
  .conversation-actions {
    grid-template-columns: 1fr;
  }

  .brand-mark {
    width: 42px;
    height: 42px;
  }

  h1 {
    font-size: 18px;
  }

  .primary-button,
  .ghost-button,
  .icon-button {
    min-height: 42px;
  }

  .chat-header {
    gap: 10px;
    padding-inline: 10px;
  }

  .empty-state {
    margin-top: 5vh;
  }

  .composer {
    padding-inline: 10px;
  }

  .image-options-grid {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}

@media (max-width: 820px) {
  .image-message {
    align-items: center;
  }

  .image-message .message-content {
    width: 100%;
    max-width: min(92vw, 520px);
    padding: 8px;
  }

  .image-message img {
    width: 100%;
    max-height: 52vh;
    object-fit: contain;
  }
}
