:root {
  --gray: #c0c0c0;
  --gray-light: #dfdfdf;
  --gray-dark: #808080;
  --gray-darker: #404040;
  --white: #ffffff;
  --black: #000000;
  --navy: #000080;
  --title-end: #1084d0;
  --teal: #008080;
  --text: #000000;
  --muted: #555555;
  --danger: #cc0000;
  --danger-bg: #fff0f0;
  --selected-bg: #000080;
  --selected-text: #ffffff;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  overflow: hidden;
  background: var(--teal);
  color: var(--text);
  font-family: "Tahoma", "MS Sans Serif", Arial, sans-serif;
  font-size: 13px;
}

button,
input,
select,
textarea {
  font-family: inherit;
  font-size: 13px;
}

button {
  cursor: pointer;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

/* ── Layout ── */
.app-shell {
  display: grid;
  grid-template-columns: 220px minmax(0, 1fr);
  height: 100vh;
  background: var(--gray);
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark);
}

/* ── Sidebar ── */
.sidebar {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--gray);
  border-right: 1px solid var(--gray-dark);
  box-shadow: 1px 0 0 var(--white);
}

.sidebar-top {
  padding: 6px 6px 4px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 3px 6px;
  margin-bottom: 6px;
  background: linear-gradient(to right, var(--navy), var(--title-end));
  border-top: 1px solid var(--white);
  border-left: 1px solid var(--white);
  border-bottom: 1px solid var(--gray-darker);
  border-right: 1px solid var(--gray-darker);
}

.brand-mark {
  width: 18px;
  height: 18px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 10px;
  font-weight: 900;
  color: white;
  background: #4488dd;
  border: 1px solid #88aaff;
}

.brand-title {
  font-size: 12px;
  font-weight: bold;
  color: white;
  white-space: nowrap;
}

.brand-subtitle {
  display: none;
}

.full-width {
  width: 100%;
}

/* ── Buttons ── */
.primary-btn,
.secondary-btn {
  min-height: 26px;
  padding: 3px 12px;
  background: var(--gray);
  color: var(--black);
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark);
  border-radius: 0;
  transition: none;
  font-size: 12px;
}

.primary-btn:active:not(:disabled),
.secondary-btn:active:not(:disabled) {
  border-top-color: var(--gray-darker);
  border-left-color: var(--gray-darker);
  border-bottom-color: var(--white);
  border-right-color: var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-dark);
  padding: 4px 11px 2px 13px;
}

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

/* ── Sidebar internals ── */
.sidebar-section-label {
  padding: 4px 8px 2px;
  font-size: 11px;
  font-weight: bold;
  color: var(--muted);
  text-transform: none;
  letter-spacing: 0;
}

.chat-list {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 2px;
  background: var(--white);
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
  margin: 0 6px 4px;
}

.chat-item {
  width: 100%;
  text-align: left;
  padding: 3px 6px;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text);
  border-radius: 0;
  font-size: 12px;
}

.chat-item:hover {
  background: #cce0ff;
}

.chat-item.active {
  background: var(--selected-bg);
  color: var(--selected-text);
}

.chat-item-title {
  font-size: 12px;
  font-weight: bold;
  line-height: 1.3;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-item-preview {
  margin-top: 1px;
  font-size: 11px;
  color: var(--muted);
  line-height: 1.3;
  display: -webkit-box;
  -webkit-line-clamp: 1;
  line-clamp: 1;
  -webkit-box-orient: vertical;
  overflow: hidden;
}

.chat-item.active .chat-item-preview {
  color: #88aaee;
}

.sidebar-footer {
  border-top: 1px solid var(--gray-dark);
  box-shadow: 0 -1px 0 var(--white);
  padding: 6px 8px 8px;
}

.footer-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 11px;
  margin-top: 4px;
}

.footer-label {
  color: var(--muted);
  font-weight: bold;
}

.footer-value {
  color: var(--text);
}

.model-badge {
  max-width: 120px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  padding: 2px 6px;
  font-size: 11px;
  background: var(--white);
  color: var(--navy);
  border: 1px solid var(--gray-dark);
}

/* ── Main panel ── */
.main-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  overflow: hidden;
}

/* Title bar */
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 8px;
  align-items: center;
  padding: 3px 4px 3px 6px;
  background: linear-gradient(to right, var(--navy), var(--title-end));
  flex-shrink: 0;
}

.topbar-info {
  flex: 1;
  min-width: 0;
}

.topbar-info .chat-title {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.chat-title {
  margin: 0;
  font-size: 12px;
  font-weight: bold;
  color: var(--white);
  line-height: 1.5;
}

.chat-subtitle {
  margin: 0;
  color: #aaccff;
  font-size: 11px;
  line-height: 1.3;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  flex-shrink: 0;
}

.topbar .primary-btn,
.topbar .secondary-btn {
  min-height: 20px;
  padding: 1px 8px;
  font-size: 11px;
}

/* ── Messages ── */
.messages {
  flex: 1;
  min-height: 0;
  overflow: auto;
  padding: 8px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--white);
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
  margin: 6px;
}

.message-row {
  display: flex;
  align-items: flex-start;
  gap: 8px;
}

.message-avatar {
  width: 26px;
  height: 26px;
  flex-shrink: 0;
  display: grid;
  place-items: center;
  font-size: 10px;
  font-weight: bold;
  background: var(--gray);
  color: var(--text);
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark);
}

.message-row.user .message-avatar {
  background: #c8d8f8;
  color: var(--navy);
}

.message-row.assistant .message-avatar {
  background: var(--gray-light);
}

.message-body {
  min-width: 0;
  max-width: min(700px, 100%);
}

.message-meta {
  margin-bottom: 3px;
  font-size: 11px;
  color: var(--muted);
}

.message-card {
  overflow-wrap: anywhere;
  border-radius: 0;
}

.user-card {
  padding: 8px 10px;
  color: var(--text);
  white-space: pre-wrap;
  background: #dce8ff;
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark);
}

.assistant-card {
  padding: 8px 10px;
  background: var(--white);
  line-height: 1.6;
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
}

.message-card.pending {
  opacity: 0.7;
  font-style: italic;
}

.message-card.error {
  background: var(--danger-bg);
  border-color: var(--danger);
}

.assistant-card > :first-child {
  margin-top: 0;
}
.assistant-card > :last-child {
  margin-bottom: 0;
}
.assistant-card p {
  margin: 0.5em 0;
}

.assistant-card h1,
.assistant-card h2,
.assistant-card h3,
.assistant-card h4 {
  margin: 0.8em 0 0.3em;
  line-height: 1.3;
}

.assistant-card ul,
.assistant-card ol {
  padding-left: 1.4em;
  margin: 0.5em 0;
}

.assistant-card li + li {
  margin-top: 0.2em;
}

.assistant-card blockquote {
  margin: 0.6em 0;
  padding-left: 10px;
  border-left: 3px solid var(--gray-dark);
  color: var(--muted);
}

.assistant-card hr {
  border: none;
  border-top: 1px solid var(--gray-dark);
  margin: 10px 0;
}

.assistant-card a {
  color: var(--navy);
  text-decoration: underline;
}

.assistant-card code {
  padding: 0.1em 0.3em;
  background: #f0f0f0;
  border: 1px solid var(--gray-dark);
  font-family: "Courier New", Courier, monospace;
  font-size: 0.9em;
}

.assistant-card pre {
  margin: 0.6em 0;
  padding: 6px 8px;
  overflow: auto;
  background: #f8f8f8;
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
}

.assistant-card pre code {
  background: transparent;
  border: none;
  padding: 0;
}

.assistant-card table {
  width: 100%;
  border-collapse: collapse;
  margin: 0.6em 0;
}

.assistant-card th,
.assistant-card td {
  border: 1px solid var(--gray-dark);
  padding: 3px 8px;
  text-align: left;
  font-size: 12px;
}

.assistant-card th {
  background: var(--gray-light);
  font-weight: bold;
}

/* ── Composer ── */
.composer-section {
  padding: 6px 6px 8px;
  background: var(--gray);
  border-top: 2px solid var(--gray-dark);
  box-shadow: 0 -1px 0 var(--white);
}

.option-row {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}

.toggle-chip,
.select-chip {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 26px;
  padding: 2px 8px;
  background: var(--gray);
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
  border-radius: 0;
  font-size: 12px;
}

.toggle-chip input {
  width: 13px;
  height: 13px;
  accent-color: var(--navy);
}

.select-chip select {
  padding: 1px 4px;
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  background: var(--white);
  color: var(--text);
  outline: none;
  font-size: 12px;
  border-radius: 0;
}

.composer-box {
  display: flex;
  align-items: flex-end;
  gap: 6px;
}

#messageInput {
  flex: 1;
  min-height: 48px;
  max-height: 160px;
  resize: none;
  padding: 4px 6px;
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  box-shadow: inset 1px 1px 0 var(--gray-darker);
  background: var(--white);
  color: var(--text);
  line-height: 1.5;
  outline: none;
  border-radius: 0;
}

#messageInput::placeholder {
  color: var(--gray-dark);
}

.send-btn {
  min-width: 72px;
  align-self: stretch;
}

.composer-hint {
  margin-top: 4px;
  color: var(--muted);
  font-size: 11px;
}

/* ── Toast ── */
.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  z-index: 50;
  max-width: min(92vw, 400px);
  padding: 8px 16px;
  background: var(--gray);
  color: var(--danger);
  font-weight: bold;
  font-size: 12px;
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark),
    2px 2px 0 rgba(0, 0, 0, 0.3);
}

.hidden {
  display: none;
}

/* ── Model select（JS 注入到 topbar） ── */
.model-select {
  max-width: 120px;
  padding: 1px 4px;
  border-top: 2px solid var(--gray-dark);
  border-left: 2px solid var(--gray-dark);
  border-bottom: 2px solid var(--white);
  border-right: 2px solid var(--white);
  background: var(--white);
  color: var(--navy);
  font-size: 11px;
  cursor: pointer;
  border-radius: 0;
  appearance: auto;
}

.model-select:focus {
  outline: 1px dotted var(--navy);
  outline-offset: 1px;
}

/* ── Scrollbars ── */
::-webkit-scrollbar {
  width: 16px;
  height: 16px;
}

::-webkit-scrollbar-track {
  background: var(--gray-light);
  border-left: 1px solid var(--gray-dark);
}

::-webkit-scrollbar-thumb {
  background: var(--gray);
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  box-shadow:
    inset 1px 1px 0 var(--gray-light),
    inset -1px -1px 0 var(--gray-dark);
}

::-webkit-scrollbar-button {
  background: var(--gray);
  border-top: 2px solid var(--white);
  border-left: 2px solid var(--white);
  border-bottom: 2px solid var(--gray-darker);
  border-right: 2px solid var(--gray-darker);
  display: block;
  height: 16px;
  width: 16px;
}

/* ── Responsive ── */
@media (max-width: 960px) {
  body {
    overflow: hidden;
  }

  .app-shell {
    display: flex;
    flex-direction: column;
    height: 100vh;
    height: 100dvh;
    border: none;
    box-shadow: none;
  }

  .sidebar {
    display: none;
  }

  .main-panel {
    flex: 1;
    min-height: 0;
    height: 100%;
  }

  .topbar {
    padding: 4px 6px;
    gap: 6px;
  }

  .messages {
    margin: 4px;
    padding: 6px;
  }

  .composer-section {
    padding: 4px 4px 6px;
  }

  .message-body {
    max-width: 100%;
  }
}

@media (max-width: 640px) {
  .option-row {
    flex-direction: row;
    gap: 4px;
  }

  .toggle-chip,
  .select-chip {
    flex: 1;
    min-height: 34px;
    padding: 4px 8px;
    justify-content: space-between;
  }

  .composer-box {
    flex-direction: row;
    align-items: flex-end;
  }

  .send-btn {
    min-width: 56px;
    width: auto;
  }
}
