:root {
  --ink: #283238;
  --muted: #71808a;
  --line: #e8e1d2;
  --panel: #fffdf8;
  --blue: #b7e0ff;
  --mint: #cfeadc;
  --cream: #fbedca;
  --peach: #ffdca4;
  --pink: #fedee1;
  --rose: #cc5d75;
  --accent: #6aaed6;
  --accent-dark: #4c8eb4;
  --shadow: 0 18px 48px rgba(141, 126, 99, 0.18);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  color: var(--ink);
  font-family: Inter, "Segoe UI", "Microsoft YaHei", Arial, sans-serif;
  background:
    linear-gradient(135deg, rgba(183, 224, 255, 0.55), rgba(254, 222, 225, 0.55)),
    linear-gradient(45deg, var(--cream), var(--mint));
}

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

button {
  cursor: pointer;
}

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

.sidebar {
  display: flex;
  flex-direction: column;
  gap: 16px;
  min-width: 0;
  padding: 22px;
  border-right: 1px solid rgba(232, 225, 210, 0.8);
  background: rgba(255, 253, 248, 0.78);
  backdrop-filter: blur(20px);
}

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

.brand-mark {
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  border-radius: 8px;
  color: #2c4050;
  font-weight: 800;
  background: linear-gradient(135deg, var(--blue), var(--pink));
  box-shadow: 0 10px 24px rgba(106, 174, 214, 0.22);
}

h1,
h2,
p {
  margin: 0;
}

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

h2 {
  font-size: 22px;
}

.brand p,
.chat-topbar p {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

.primary-button,
.send-button {
  border: 0;
  border-radius: 8px;
  color: #263642;
  background: linear-gradient(135deg, var(--blue), var(--mint));
  box-shadow: 0 10px 24px rgba(106, 174, 214, 0.2);
}

.primary-button {
  min-height: 44px;
  padding: 0 16px;
  font-weight: 800;
}

.primary-button:hover,
.send-button:hover {
  filter: saturate(1.08) brightness(0.98);
}

.wide {
  width: 100%;
}

.ghost-button,
.conversation-item {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.76);
}

.ghost-button {
  padding: 7px 10px;
  color: var(--muted);
}

.conversation-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
}

.conversation-item {
  display: grid;
  gap: 6px;
  width: 100%;
  padding: 12px;
  text-align: left;
  color: var(--ink);
}

.conversation-item span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-weight: 800;
}

.conversation-item small {
  overflow: hidden;
  color: var(--muted);
  text-overflow: ellipsis;
  white-space: nowrap;
}

.conversation-item.active {
  border-color: rgba(106, 174, 214, 0.7);
  background: linear-gradient(135deg, rgba(183, 224, 255, 0.72), rgba(207, 234, 220, 0.72));
}

.account-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  color: var(--muted);
}

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

.chat-topbar {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 24px;
  padding: 20px 28px;
  border-bottom: 1px solid rgba(232, 225, 210, 0.8);
  background: rgba(255, 253, 248, 0.68);
  backdrop-filter: blur(16px);
}

.settings {
  display: grid;
  grid-template-columns: 98px 112px 190px minmax(260px, 360px);
  gap: 12px;
  align-items: end;
}

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

input,
select,
textarea {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--ink);
  background: rgba(255, 255, 255, 0.88);
  outline: 0;
}

input,
select {
  min-height: 38px;
  padding: 0 10px;
}

.settings input,
.settings select,
.settings textarea {
  width: 100%;
}

input:focus,
select:focus,
textarea:focus {
  border-color: rgba(106, 174, 214, 0.9);
  box-shadow: 0 0 0 4px rgba(183, 224, 255, 0.45);
}

.system-prompt-control textarea {
  min-height: 38px;
  max-height: 90px;
  resize: vertical;
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 16px;
  overflow: auto;
  padding: 26px 30px;
}

.empty-state {
  align-self: center;
  max-width: 540px;
  margin: auto;
  padding: 18px 20px;
  border: 1px dashed rgba(113, 128, 138, 0.36);
  border-radius: 8px;
  color: var(--muted);
  background: rgba(255, 253, 248, 0.72);
  text-align: center;
}

.message {
  max-width: min(850px, 80%);
  padding: 14px 16px;
  border: 1px solid rgba(232, 225, 210, 0.9);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.94);
  box-shadow: 0 12px 32px rgba(141, 126, 99, 0.12);
}

.message.user {
  align-self: flex-end;
  border-color: rgba(183, 224, 255, 0.9);
  background: linear-gradient(135deg, rgba(183, 224, 255, 0.86), rgba(207, 234, 220, 0.62));
}

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

.message.error {
  border-color: rgba(204, 93, 117, 0.42);
  color: var(--rose);
}

.message-meta {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
}

.message-content {
  line-height: 1.68;
  word-break: break-word;
}

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

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

.markdown-body code:not(pre code) {
  padding: 2px 5px;
  border-radius: 6px;
  background: rgba(251, 237, 202, 0.9);
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.92em;
}

.markdown-body pre {
  overflow: auto;
  padding: 13px 14px;
  border: 1px solid rgba(232, 225, 210, 0.9);
  border-radius: 8px;
  background: #fff8eb;
}

.markdown-body pre code {
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 13px;
}

.markdown-body blockquote {
  padding-left: 12px;
  border-left: 4px solid var(--peach);
  color: #596b73;
}

.markdown-body table {
  display: block;
  max-width: 100%;
  overflow: auto;
  border-collapse: collapse;
}

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

.katex-display {
  overflow-x: auto;
  overflow-y: hidden;
}

.message-images {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-top: 12px;
}

.message-image {
  display: block;
  overflow: hidden;
  width: 148px;
  aspect-ratio: 4 / 3;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--cream);
}

.message-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.composer {
  display: grid;
  gap: 10px;
  padding: 18px 30px 24px;
  border-top: 1px solid rgba(232, 225, 210, 0.8);
  background: rgba(255, 253, 248, 0.78);
  backdrop-filter: blur(16px);
}

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

.icon-button,
.send-button {
  display: grid;
  place-items: center;
  height: 44px;
  min-width: 44px;
}

.icon-button {
  border: 1px solid var(--line);
  border-radius: 8px;
  color: #4d7288;
  background: rgba(255, 255, 255, 0.88);
}

.icon-button input {
  display: none;
}

textarea {
  min-height: 44px;
  max-height: 160px;
  padding: 11px 13px;
  resize: vertical;
}

.send-button {
  font-size: 18px;
}

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

.preview-item {
  position: relative;
  overflow: hidden;
  width: 72px;
  aspect-ratio: 1;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: white;
}

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

.preview-item button {
  position: absolute;
  top: 4px;
  right: 4px;
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border: 0;
  border-radius: 50%;
  color: white;
  background: rgba(40, 50, 56, 0.72);
}

.auth-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(251, 237, 202, 0.48);
  backdrop-filter: blur(18px);
}

.auth-screen.hidden,
.hidden {
  display: none;
}

.auth-card,
.new-dialog form {
  display: grid;
  gap: 16px;
  width: min(430px, 100%);
  padding: 24px;
  border: 1px solid rgba(232, 225, 210, 0.95);
  border-radius: 8px;
  background: rgba(255, 253, 248, 0.96);
  box-shadow: var(--shadow);
}

.auth-brand {
  margin-bottom: 4px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 8px;
}

.auth-tab {
  min-height: 40px;
  border: 0;
  color: var(--muted);
  background: white;
  font-weight: 800;
}

.auth-tab.active {
  color: #2c4050;
  background: linear-gradient(135deg, var(--pink), var(--peach));
}

.form-error {
  min-height: 20px;
  color: var(--rose);
  font-size: 13px;
}

.new-dialog {
  width: min(520px, calc(100vw - 32px));
  padding: 0;
  border: 0;
  border-radius: 8px;
  background: transparent;
}

.new-dialog::backdrop {
  background: rgba(40, 50, 56, 0.22);
  backdrop-filter: blur(10px);
}

.new-dialog form {
  width: 100%;
}

fieldset {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin: 0;
  padding: 0;
  border: 0;
}

legend {
  grid-column: 1 / -1;
  margin-bottom: 2px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 800;
}

.choice-card {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
}

.choice-card input {
  min-height: auto;
}

.dialog-actions {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

@media (max-width: 1080px) {
  .settings {
    grid-template-columns: 1fr 1fr;
    width: min(560px, 100%);
  }

  .system-prompt-control {
    grid-column: 1 / -1;
  }
}

@media (max-width: 820px) {
  .app-shell {
    grid-template-columns: 1fr;
    height: auto;
    min-height: 100vh;
  }

  .sidebar {
    max-height: 280px;
    border-right: 0;
    border-bottom: 1px solid var(--line);
  }

  .chat-panel {
    height: calc(100vh - 280px);
    min-height: 640px;
  }

  .chat-topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .settings {
    grid-template-columns: 1fr 1fr;
    width: 100%;
  }

  .messages,
  .chat-topbar,
  .composer {
    padding-left: 16px;
    padding-right: 16px;
  }

  .message {
    max-width: 94%;
  }

  fieldset {
    grid-template-columns: 1fr;
  }
}
