:root {
  color-scheme: light;
  --bg: #f7f4ed;
  --surface: #fffdf7;
  --surface-soft: #eeebe2;
  --surface-quiet: #faf8f1;
  --text: #23201b;
  --muted: #716b61;
  --line: #e5ded0;
  --line-strong: #d5cabb;
  --brand: #527665;
  --brand-dark: #355747;
  --brand-soft: #e8f0e8;
  --accent: #b46546;
  --danger: #9b453a;
  --danger-soft: #f5e7e2;
  --shadow: 0 16px 38px rgba(52, 45, 35, 0.07);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Hiragino Sans GB", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

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

button {
  border: 0;
  cursor: pointer;
}

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

a {
  color: var(--brand-dark);
  font-weight: 650;
}

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

.login-panel {
  width: min(420px, 100%);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 30px;
}

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

.brand-mark {
  width: 38px;
  height: 38px;
  border-radius: 8px;
  display: grid;
  place-items: center;
  color: #fff;
  background: var(--brand);
  font-weight: 800;
  letter-spacing: 0;
  flex: 0 0 auto;
}

.brand h1,
.brand h2,
.page-title h1 {
  margin: 0;
  letter-spacing: 0;
}

.brand h1,
.brand h2 {
  font-size: 20px;
}

.brand p,
.page-title p,
.muted {
  margin: 4px 0 0;
  color: var(--muted);
}

.login-subtitle {
  font-size: 13px;
}

.field {
  display: grid;
  gap: 7px;
  margin: 14px 0;
}

.field span,
.select-field span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

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

.field input,
.key-create input,
.select-field select {
  min-height: 40px;
  padding: 0 12px;
}

.field input:focus,
.key-create input:focus,
.select-field select:focus,
.composer textarea:focus {
  border-color: var(--brand);
  box-shadow: 0 0 0 3px rgba(79, 118, 95, 0.13);
}

.primary,
.secondary,
.danger,
.icon-button {
  min-height: 38px;
  border-radius: 8px;
  padding: 0 14px;
  font-weight: 700;
  white-space: nowrap;
}

.primary {
  background: var(--text);
  color: #fff;
}

.primary:hover {
  background: #3a322a;
}

.primary.compact {
  min-height: 34px;
  padding: 0 12px;
}

.secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--line);
}

.secondary:hover {
  border-color: var(--line-strong);
  background: var(--surface-soft);
}

.danger {
  background: var(--danger-soft);
  color: var(--danger);
}

.danger-ghost {
  background: transparent;
  color: var(--muted);
}

.danger-ghost:hover {
  background: var(--danger-soft);
  color: var(--danger);
}

.icon-button {
  width: 32px;
  min-height: 32px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
}

.login-panel .primary {
  width: 100%;
  margin-top: 12px;
}

.error,
.chat-error {
  margin: 0;
  color: var(--danger);
  font-size: 13px;
}

.success {
  margin: 10px 0 0;
  color: var(--brand-dark);
  font-size: 13px;
}

.shell {
  min-height: 100vh;
  display: grid;
  grid-template-columns: clamp(196px, 17vw, 244px) minmax(0, 1fr);
  transition: grid-template-columns 160ms ease;
}

.shell.nav-collapsed {
  grid-template-columns: 66px minmax(0, 1fr);
}

.sidebar {
  border-right: 1px solid var(--line);
  background: rgba(255, 253, 247, 0.86);
  padding: 16px 12px;
  min-width: 0;
}

.sidebar .brand {
  padding: 0 6px 14px;
  border-bottom: 1px solid var(--line);
}

.sidebar-brand {
  min-width: 0;
}

.brand-toggle {
  width: 38px;
  height: 38px;
  padding: 0;
  border-radius: 8px;
  background: transparent;
  flex: 0 0 auto;
}

.brand-toggle .brand-mark {
  width: 38px;
  height: 38px;
}

.nav-collapsed .sidebar {
  padding: 16px 8px;
}

.nav-collapsed .sidebar .brand {
  justify-content: center;
  padding: 0 0 14px;
}

.nav-collapsed .sidebar .brand > div,
.nav-collapsed .nav-label {
  display: none;
}

.nav {
  display: grid;
  gap: 4px;
  margin-top: 14px;
}

.nav button {
  min-height: 36px;
  text-align: left;
  border-radius: 8px;
  padding: 9px 11px;
  background: transparent;
  color: var(--muted);
  font-weight: 650;
}

.nav-collapsed .nav button {
  width: 42px;
  min-height: 42px;
  padding: 0;
  justify-self: center;
}

.nav-collapsed .nav button::before {
  content: attr(title);
  display: block;
  width: 1.6em;
  overflow: hidden;
  text-align: center;
}

.nav button.active,
.nav button:hover {
  background: var(--surface-soft);
  color: var(--text);
}

.main {
  min-width: 0;
  width: 100%;
  max-width: 1500px;
  padding: clamp(16px, 2vw, 28px);
}

.chat-shell .main {
  max-width: none;
  padding: 0;
}

.topbar {
  display: flex;
  justify-content: space-between;
  gap: 16px;
  align-items: flex-start;
  margin-bottom: 16px;
}

.page-title h1 {
  font-size: 24px;
  line-height: 1.15;
}

.top-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.grid {
  display: grid;
  gap: 12px;
}

.cards {
  grid-template-columns: repeat(4, minmax(0, 1fr));
}

.card,
.table-panel,
.doc-panel,
.chat-history,
.chat-panel {
  background: rgba(255, 253, 247, 0.9);
  border: 1px solid var(--line);
  border-radius: 10px;
  box-shadow: 0 10px 28px rgba(59, 46, 31, 0.04);
}

.card {
  padding: 18px;
}

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

.card strong {
  display: block;
  margin-top: 8px;
  font-size: 26px;
}

.table-panel {
  margin-top: 14px;
  overflow: auto;
}

.panel-head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--line);
  background: var(--surface-quiet);
}

.panel-head h2 {
  margin: 0;
  font-size: 16px;
}

.panel-body {
  padding: 14px 16px;
  color: var(--muted);
  line-height: 1.55;
}

.panel-body p {
  margin: 0;
}

table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
}

th,
td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  text-align: left;
  font-size: 14px;
}

th {
  color: var(--muted);
  background: var(--surface-quiet);
  font-weight: 750;
}

tr:last-child td {
  border-bottom: 0;
}

.key-create {
  display: flex;
  gap: 10px;
}

.key-create input {
  min-width: 260px;
}

.secret {
  display: grid;
  gap: 6px;
  margin: 14px 16px 0;
  padding: 12px;
  border-radius: 8px;
  background: var(--brand-soft);
  border: 1px solid #cfddcf;
  overflow-wrap: anywhere;
}

.secret span,
.secret em {
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
}

.secret strong,
code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}

.secret strong {
  font-size: 13px;
}

.account-form {
  max-width: 460px;
  padding: 16px;
}

.account-form .primary {
  margin-top: 4px;
}

.chat-workspace {
  height: 100vh;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(214px, 282px) minmax(0, 1fr);
  gap: 0;
}

.chat-history {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  overflow: hidden;
  border-width: 0 1px 0 0;
  border-radius: 0;
  box-shadow: none;
  background: rgba(250, 248, 241, 0.66);
}

.chat-history-head {
  padding: 12px 12px 8px;
  border-bottom: 1px solid var(--line);
  background: transparent;
}

.chat-history-list {
  overflow: auto;
  padding: 8px 8px 14px;
}

.history-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 4px;
  align-items: center;
  border-radius: 8px;
  padding: 4px;
}

.history-item.active,
.history-item:hover {
  background: var(--surface-soft);
}

.history-select {
  min-width: 0;
  text-align: left;
  background: transparent;
  color: var(--text);
  padding: 8px;
  border-radius: 7px;
}

.history-select strong {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}

.history-select span {
  display: block;
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.empty-line {
  margin: 12px;
  color: var(--muted);
  font-size: 13px;
}

.chat-panel {
  min-width: 0;
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  overflow: hidden;
  border: 0;
  border-radius: 0;
  box-shadow: none;
  background: transparent;
}

.chat-panel-head {
  min-height: 55px;
  display: flex;
  align-items: center;
  padding: 0 clamp(18px, 7vw, 112px);
  border-bottom: 1px solid var(--line);
  background: rgba(250, 248, 241, 0.64);
}

.chat-panel-head h2 {
  margin: 0;
  max-width: 100%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--text);
  font-size: 16px;
  font-weight: 800;
  line-height: 1.25;
}

.select-field {
  min-width: 180px;
  display: grid;
  gap: 5px;
}

.compact-select {
  min-width: 150px;
}

.compact-select span {
  display: none;
}

.compact-select select {
  min-height: 34px;
  border-radius: 8px;
  background: var(--surface);
}

.chat-thread {
  min-height: 0;
  overflow: auto;
  padding: clamp(26px, 5vw, 72px) clamp(18px, 7vw, 112px) 24px;
  display: flex;
  flex-direction: column;
  gap: 28px;
}

.chat-empty {
  margin: auto;
  width: min(420px, 100%);
  text-align: center;
  color: var(--muted);
}

.chat-empty .brand-mark {
  margin: 0 auto 14px;
}

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

.message {
  width: min(780px, 100%);
  max-width: 100%;
  padding: 0;
  border: 0;
  border-radius: 0;
  background: transparent;
}

.message.from-user {
  align-self: flex-end;
  width: fit-content;
  max-width: min(620px, 78%);
  margin-left: auto;
  text-align: left;
}

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

.message.failed {
  color: var(--danger);
}

.message.pending .message-body {
  color: var(--muted);
}

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

.message.from-user .message-meta {
  text-align: right;
}

.message-body {
  line-height: 1.72;
  overflow-wrap: anywhere;
  color: var(--text);
}

.message.from-user .message-body {
  border: 1px solid #d6e1d8;
  border-radius: 14px 14px 4px 14px;
  background: var(--brand-soft);
  padding: 10px 13px;
  box-shadow: 0 5px 16px rgba(52, 45, 35, 0.04);
}

.message.failed.from-user .message-body,
.message.failed .message-body {
  color: var(--danger);
}

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

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

.message-body h2,
.message-body h3,
.message-body h4,
.message-body h5 {
  margin: 18px 0 8px;
  line-height: 1.3;
}

.message-body h2:first-child,
.message-body h3:first-child,
.message-body h4:first-child,
.message-body h5:first-child {
  margin-top: 0;
}

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

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

.message-body blockquote {
  border-left: 3px solid var(--brand);
  padding: 2px 0 2px 12px;
  color: var(--muted);
}

.message-body code {
  padding: 2px 5px;
  border-radius: 5px;
  background: var(--surface-soft);
  color: var(--brand-dark);
  font-size: 0.92em;
}

.message-body pre {
  overflow: auto;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #2d2a24;
  padding: 12px;
}

.message-body pre code {
  display: block;
  padding: 0;
  background: transparent;
  color: #fff8eb;
  white-space: pre;
}

.md-table-wrap {
  overflow-x: auto;
}

.md-table {
  width: 100%;
  min-width: 480px;
  border-collapse: collapse;
  background: var(--surface);
}

.md-table th,
.md-table td {
  padding: 9px 10px;
  border: 1px solid var(--line);
  font-size: 13px;
  vertical-align: top;
}

.md-table th {
  background: var(--surface-soft);
  color: var(--text);
}

.message-body a {
  color: var(--brand-dark);
}

.composer {
  width: min(900px, calc(100% - 36px));
  margin: 0 auto 18px;
  display: grid;
  gap: 10px;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: rgba(255, 253, 247, 0.96);
  box-shadow: 0 12px 32px rgba(52, 45, 35, 0.08);
}

.composer textarea {
  min-height: 78px;
  max-height: 210px;
  resize: vertical;
  padding: 12px 13px;
  line-height: 1.55;
  border: 0;
  background: transparent;
  box-shadow: none;
}

.composer-actions {
  min-height: 38px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}

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

.tool-button,
.toggle-pill {
  min-height: 34px;
  border-radius: 8px;
}

.toggle-pill {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 0 10px;
  border: 1px solid var(--line);
  background: var(--surface);
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
  white-space: nowrap;
}

.toggle-pill input {
  accent-color: var(--brand);
}

.pending-files,
.message-files {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.pending-file,
.file-chip {
  min-width: 0;
  max-width: min(100%, 360px);
  display: inline-flex;
  align-items: center;
  gap: 8px;
  border-radius: 8px;
  background: var(--brand-soft);
  color: var(--brand-dark);
  padding: 7px 9px;
  text-decoration: none;
}

.pending-file span,
.file-chip strong {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 13px;
}

.pending-file em,
.file-chip em,
.file-chip span {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 12px;
  font-style: normal;
  font-weight: 650;
}

.pending-file button {
  width: 22px;
  height: 22px;
  border-radius: 6px;
  padding: 0;
  background: transparent;
  color: var(--muted);
  font-size: 16px;
}

.file-chip.output-file {
  background: #e4eee7;
}

.message-files {
  margin-top: 12px;
}

.send-button {
  min-width: 76px;
}

.doc-panel {
  max-width: 1120px;
  padding: 22px;
}

.doc-panel h2 {
  margin-top: 0;
}

.doc-panel h3 {
  margin: 0 0 12px;
  font-size: 18px;
}

.doc-panel h4 {
  margin: 18px 0 8px;
  font-size: 14px;
  color: var(--muted);
}

.doc-panel p {
  color: var(--muted);
  line-height: 1.58;
}

.doc-panel p code,
.doc-panel li code,
.doc-panel dd code {
  padding: 2px 5px;
  border-radius: 5px;
  background: var(--surface-soft);
  color: var(--brand-dark);
}

.doc-section > code {
  display: block;
  white-space: pre-wrap;
  overflow-x: auto;
  background: #2d2821;
  color: #fff7ea;
  padding: 12px;
  border-radius: 8px;
  margin: 10px 0 18px;
}

.doc-hero {
  padding-bottom: 18px;
  border-bottom: 1px solid var(--line);
}

.doc-hero > span {
  color: var(--brand-dark);
  font-size: 12px;
  font-weight: 800;
  text-transform: uppercase;
}

.doc-hero h2 {
  margin: 6px 0 8px;
  font-size: 30px;
}

.doc-hero p {
  margin: 0;
}

.doc-section {
  padding: 20px 0;
  border-top: 1px solid var(--line);
}

.doc-section:first-of-type {
  border-top: 0;
}

.doc-steps,
.doc-list {
  margin: 10px 0 0;
  padding-left: 22px;
  color: var(--muted);
  line-height: 1.72;
}

.doc-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin: 12px 0;
}

.doc-grid-compact {
  margin: 18px 0 4px;
}

.doc-grid article,
.principle-list article {
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--surface-quiet);
  padding: 13px;
}

.doc-grid span {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 7px;
}

.doc-grid strong {
  display: block;
  font-size: 15px;
  line-height: 1.35;
  overflow-wrap: break-word;
}

.doc-faq {
  margin: 0;
}

.doc-faq dt {
  margin-top: 14px;
  font-weight: 800;
  color: var(--text);
}

.doc-faq dd {
  margin: 5px 0 0;
  color: var(--muted);
  line-height: 1.58;
}

.principle-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin-top: 12px;
}

.principle-list article {
  display: grid;
  grid-template-columns: 42px minmax(0, 1fr);
  gap: 12px;
}

.principle-list article > span {
  width: 34px;
  height: 34px;
  display: grid;
  place-items: center;
  border-radius: 8px;
  background: var(--brand-soft);
  color: var(--brand-dark);
  font-size: 12px;
  font-weight: 900;
}

.principle-list strong {
  display: block;
  margin: 1px 0 5px;
}

.principle-list p {
  margin: 0;
}

@media (max-width: 1180px) {
  .cards {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .chat-workspace {
    grid-template-columns: minmax(190px, 236px) minmax(0, 1fr);
  }

  .chat-thread {
    padding-inline: clamp(18px, 5vw, 72px);
  }
}

@media (max-width: 900px) {
  .shell,
  .shell.nav-collapsed {
    grid-template-columns: 1fr;
  }

  .sidebar {
    position: sticky;
    top: 0;
    z-index: 5;
    border-right: 0;
    border-bottom: 1px solid var(--line);
    padding: 10px 12px;
  }

  .sidebar .brand {
    padding: 0 0 9px;
  }

  .nav {
    grid-auto-flow: column;
    grid-auto-columns: minmax(84px, max-content);
    overflow-x: auto;
  }

  .nav button,
  .nav-collapsed .nav button {
    width: auto;
    min-height: 34px;
    justify-self: stretch;
    text-align: center;
    padding: 7px 10px;
  }

  .nav-collapsed .nav button::before {
    width: auto;
  }

  .chat-workspace {
    height: calc(100dvh - 91px);
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .chat-history {
    max-height: 142px;
    border-width: 0 0 1px;
  }

  .chat-history-list {
    display: grid;
    grid-auto-flow: column;
    grid-auto-columns: minmax(210px, 280px);
    overflow-x: auto;
    overflow-y: hidden;
  }

  .history-item {
    min-width: 0;
  }

  .doc-grid,
  .principle-list {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 640px) {
  .login-shell,
  .main {
    padding: 12px;
  }

  .chat-shell .main {
    padding: 0;
  }

  .login-panel,
  .doc-panel {
    padding: 18px;
  }

  .topbar,
  .panel-head,
  .key-create {
    align-items: stretch;
    flex-direction: column;
  }

  .top-actions {
    justify-content: flex-start;
  }

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

  .nav {
    grid-auto-columns: minmax(74px, max-content);
  }

  .nav button {
    min-height: 34px;
    padding: 7px 8px;
  }

  .key-create input {
    min-width: 0;
  }

  .chat-workspace {
    height: calc(100dvh - 146px);
  }

  .chat-history {
    max-height: 82px;
  }

  .chat-history-list {
    grid-auto-columns: minmax(178px, 238px);
  }

  .chat-thread {
    padding: 18px 14px 12px;
    gap: 22px;
  }

  .message {
    width: 100%;
  }

  .message.from-user {
    width: fit-content;
    max-width: 88%;
  }

  .composer {
    width: calc(100% - 20px);
    margin-bottom: 8px;
    gap: 6px;
    padding: 8px;
  }

  .composer textarea {
    min-height: 52px;
    max-height: 96px;
  }

  .composer-actions {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
  }

  .composer-tools {
    gap: 6px;
  }

  .tool-button,
  .toggle-pill,
  .compact-select select {
    min-height: 32px;
  }

  .send-button {
    min-height: 36px;
    width: 100%;
  }

  .compact-select {
    flex: 1 1 160px;
  }

  table {
    min-width: 620px;
  }
}
