:root {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #141414;
  --surface-raised: #181818;
  --border: rgba(182, 154, 91, 0.14);
  --border-strong: rgba(182, 154, 91, 0.24);
  --text: #e7e2d8;
  --text-soft: #a69c89;
  --muted: #817a6c;
  --faint: #625d54;
  --accent: #b69a5b;
  --accent-strong: #d6c28f;
  font-family: Inter, "SF Pro Display", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; }

html,
body {
  min-width: 320px;
  min-height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

button,
input { font: inherit; }

button:focus-visible,
input:focus-visible,
a:focus-visible {
  outline: 2px solid rgba(182, 154, 91, 0.7);
  outline-offset: 2px;
}

.settings-shell { min-height: 100dvh; }

.settings-header {
  position: sticky;
  z-index: 10;
  top: 0;
  display: grid;
  grid-template-columns: 36px minmax(0, 1fr) 36px;
  align-items: center;
  height: 54px;
  padding: 0 14px;
  border-bottom: 1px solid rgba(182, 154, 91, 0.09);
  background: rgba(13, 13, 13, 0.94);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.settings-header h1 {
  margin: 0;
  color: var(--text);
  font-size: 14px;
  font-weight: 620;
  text-align: center;
}

.back-button {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border-radius: 7px;
  color: var(--muted);
  text-decoration: none;
}

.back-button:hover {
  background: rgba(182, 154, 91, 0.07);
  color: var(--accent-strong);
}

.back-button svg,
.reset-button svg,
.upload-button svg,
.remove-image-button svg {
  width: 17px;
  height: 17px;
  stroke-width: 1.8;
}

.header-balance {
  width: 36px;
  height: 36px;
}

.settings-main {
  width: min(720px, calc(100% - 36px));
  margin: 0 auto;
  padding: 42px 0 72px;
}

.settings-section { width: 100%; }

.section-heading {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 20px;
  margin-bottom: 18px;
}

.section-heading h2 {
  margin: 0 0 6px;
  color: var(--text);
  font-size: 17px;
  font-weight: 620;
}

.section-heading p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.6;
}

.save-status {
  min-width: 48px;
  padding-top: 3px;
  color: var(--accent);
  font-size: 11px;
  text-align: right;
}

.background-preview {
  --preview-bg: #0d0d0d;
  --preview-image: none;
  --preview-image-opacity: 0.45;
  position: relative;
  isolation: isolate;
  display: flex;
  min-height: 238px;
  flex-direction: column;
  overflow: hidden;
  padding: 18px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--preview-bg);
  transition: background 160ms ease;
}

.background-preview::before {
  position: absolute;
  z-index: -1;
  inset: 0;
  background-image: var(--preview-image);
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  content: "";
  opacity: var(--preview-image-opacity);
  pointer-events: none;
}

.preview-brand {
  align-self: flex-start;
  padding: 6px 10px;
  border: 1px solid rgba(182, 154, 91, 0.2);
  border-radius: 15px;
  background: rgba(24, 24, 24, 0.72);
  color: var(--text);
  font-size: 11px;
  font-weight: 620;
}

.preview-messages {
  display: flex;
  flex: 1;
  flex-direction: column;
  justify-content: center;
  gap: 12px;
  padding: 16px 6px;
}

.preview-message {
  width: fit-content;
  max-width: 76%;
  padding: 6px 10px;
  border: 1px solid rgba(182, 154, 91, 0.25);
  border-radius: 17px;
  background: #181818;
  color: #e2ddd3;
  font-size: 12px;
  line-height: 1.5;
}

.preview-user {
  align-self: flex-end;
  border-color: rgba(182, 154, 91, 0.42);
  background: #181713;
  color: #eee8dc;
}

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

.preview-composer {
  padding: 8px 10px;
  border: 1px solid rgba(182, 154, 91, 0.16);
  border-radius: 8px;
  background: #141414;
  color: var(--faint);
  font-size: 11px;
}

.background-options {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 9px;
  margin-top: 18px;
}

.background-option {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 9px;
  min-height: 46px;
  padding: 6px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-soft);
  font-size: 11px;
  text-align: left;
  cursor: pointer;
}

.background-option:hover {
  border-color: rgba(182, 154, 91, 0.3);
  background: rgba(182, 154, 91, 0.045);
}

.background-option[aria-pressed="true"] {
  border-color: rgba(182, 154, 91, 0.58);
  background: rgba(182, 154, 91, 0.09);
  color: var(--text);
}

.color-swatch {
  width: 27px;
  height: 27px;
  flex: 0 0 auto;
  border: 1px solid rgba(231, 226, 216, 0.14);
  border-radius: 7px;
  background: var(--swatch);
}

.custom-background-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 54px;
  margin-top: 12px;
  padding: 0 10px 0 2px;
  border-bottom: 1px solid var(--border);
}

.custom-background-row label {
  display: flex;
  min-width: 0;
  flex-direction: column;
  gap: 3px;
  color: var(--text-soft);
  font-size: 12px;
}

.custom-background-row code {
  color: var(--faint);
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
  font-size: 10px;
}

.custom-background-row input[type="color"] {
  width: 38px;
  height: 32px;
  padding: 3px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: var(--surface);
  cursor: pointer;
}

.image-background-settings {
  margin-top: 18px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(20, 20, 20, 0.72);
}

.image-setting-heading {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 18px;
}

.image-setting-heading h3 {
  margin: 0 0 4px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.image-setting-heading p,
.image-note {
  margin: 0;
  color: var(--faint);
  font-size: 10px;
  line-height: 1.55;
}

.image-setting-heading p {
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

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

.upload-button,
.remove-image-button {
  display: inline-flex;
  min-height: 34px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 10px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  background: transparent;
  color: var(--text-soft);
  font-size: 11px;
  cursor: pointer;
}

.upload-button:hover,
.remove-image-button:hover:not(:disabled) {
  border-color: rgba(182, 154, 91, 0.42);
  background: rgba(182, 154, 91, 0.06);
  color: var(--text);
}

.remove-image-button:disabled {
  color: rgba(129, 122, 108, 0.42);
  cursor: not-allowed;
  opacity: 0.58;
}

.image-opacity-setting {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-top: 18px;
  color: var(--text-soft);
  font-size: 11px;
}

.image-opacity-setting output {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

#backgroundImageOpacity {
  width: 100%;
  height: 16px;
  margin: 7px 0 8px;
  accent-color: var(--accent);
  cursor: pointer;
}

#backgroundImageOpacity:disabled {
  cursor: not-allowed;
  opacity: 0.42;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  clip-path: inset(50%);
  white-space: nowrap;
}

.reset-button {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 36px;
  margin-top: 14px;
  padding: 0 9px;
  border: 0;
  border-radius: 7px;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  cursor: pointer;
}

.reset-button:hover {
  background: rgba(182, 154, 91, 0.06);
  color: var(--text-soft);
}

.avatar-section {
  margin-top: 46px;
  padding-top: 38px;
  border-top: 1px solid var(--border);
}

.avatar-settings {
  border-top: 1px solid var(--border);
}

.avatar-setting-row {
  display: flex;
  min-height: 78px;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  padding: 14px 2px;
  border-bottom: 1px solid var(--border);
}

.avatar-summary {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 12px;
}

.avatar-summary h3 {
  margin: 0 0 4px;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.avatar-summary p {
  max-width: 280px;
  margin: 0;
  overflow: hidden;
  color: var(--faint);
  font-size: 10px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.avatar-preview {
  display: grid;
  width: 44px;
  height: 44px;
  flex: 0 0 auto;
  overflow: hidden;
  place-items: center;
  border: 1px solid rgba(182, 154, 91, 0.34);
  border-radius: 50%;
  background-color: #151515;
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  color: var(--accent-strong);
}

.avatar-preview.has-image > span { visibility: hidden; }

.avatar-preview-assistant {
  grid-template-columns: repeat(3, 2px);
  gap: 3px;
}

.avatar-preview-assistant span {
  display: block;
  width: 2px;
  border-radius: 1px;
  background: var(--accent);
}

.avatar-preview-assistant span:nth-child(1) { height: 8px; opacity: 0.65; }
.avatar-preview-assistant span:nth-child(2) { height: 14px; }
.avatar-preview-assistant span:nth-child(3) { height: 5px; opacity: 0.82; }

.avatar-preview-user {
  background-color: #181713;
  font-size: 12px;
  font-weight: 650;
}

.avatar-note {
  margin: 12px 2px 0;
  color: var(--faint);
  font-size: 10px;
  line-height: 1.55;
}

.api-section {
  margin-top: 46px;
  padding-top: 38px;
  border-top: 1px solid var(--border);
}

.api-settings {
  display: grid;
  gap: 12px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: rgba(20, 20, 20, 0.72);
}

.api-connection-summary {
  display: grid;
  gap: 9px;
  padding: 12px 13px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: rgba(13, 13, 13, 0.62);
}

.api-connection-summary[data-state="connected"] {
  border-color: rgba(182, 154, 91, 0.32);
  background: rgba(182, 154, 91, 0.055);
}

.api-connection-summary[data-state="error"] {
  border-color: rgba(182, 143, 132, 0.3);
}

.api-connection-main {
  display: flex;
  min-width: 0;
  align-items: center;
  gap: 9px;
}

.api-connection-dot {
  width: 7px;
  height: 7px;
  flex: 0 0 auto;
  border-radius: 50%;
  background: var(--faint);
  box-shadow: 0 0 0 3px rgba(98, 93, 84, 0.1);
}

.api-connection-summary[data-state="connected"] .api-connection-dot {
  background: var(--accent);
  box-shadow: 0 0 0 3px rgba(182, 154, 91, 0.12);
}

.api-connection-summary[data-state="error"] .api-connection-dot {
  background: #b68f84;
  box-shadow: 0 0 0 3px rgba(182, 143, 132, 0.1);
}

.api-connection-main strong {
  display: block;
  color: var(--text);
  font-size: 12px;
  font-weight: 620;
}

.api-connection-main p {
  margin: 3px 0 0;
  overflow: hidden;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.4;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.api-connection-details {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding-left: 16px;
  color: var(--text-soft);
  font-size: 10px;
  line-height: 1.4;
}

.api-connection-details span + span { color: var(--faint); }

.api-field {
  display: grid;
  gap: 7px;
  color: var(--text-soft);
  font-size: 11px;
}

.api-field input,
.api-field select {
  width: 100%;
  min-height: 38px;
  padding: 0 10px;
  border: 1px solid var(--border);
  border-radius: 7px;
  outline: 0;
  background: rgba(13, 13, 13, 0.76);
  color: var(--text);
  font-size: 12px;
}

.api-field select { cursor: pointer; }

.api-field input::placeholder { color: var(--faint); }

.api-field input:focus,
.api-field select:focus {
  border-color: rgba(182, 154, 91, 0.42);
  background: rgba(13, 13, 13, 0.92);
}

.api-action-row {
  display: flex;
  gap: 8px;
}

.secondary-settings-button,
.danger-settings-button,
.primary-settings-button {
  display: inline-flex;
  min-height: 36px;
  align-items: center;
  justify-content: center;
  gap: 7px;
  padding: 0 11px;
  border: 1px solid var(--border-strong);
  border-radius: 7px;
  font-size: 11px;
  cursor: pointer;
}

.secondary-settings-button,
.danger-settings-button {
  background: transparent;
  color: var(--text-soft);
}

.secondary-settings-button:hover,
.danger-settings-button:hover:not(:disabled) {
  border-color: rgba(182, 154, 91, 0.42);
  background: rgba(182, 154, 91, 0.06);
  color: var(--text);
}

.danger-settings-button:disabled {
  cursor: not-allowed;
  opacity: 0.45;
}

.primary-settings-button {
  border-color: rgba(182, 154, 91, 0.34);
  background: rgba(182, 154, 91, 0.14);
  color: var(--accent-strong);
}

.primary-settings-button:hover {
  border-color: rgba(182, 154, 91, 0.52);
  background: rgba(182, 154, 91, 0.2);
}

.secondary-settings-button svg,
.danger-settings-button svg,
.primary-settings-button svg {
  width: 16px;
  height: 16px;
  stroke-width: 1.8;
}

.api-status {
  min-height: 16px;
  margin: 0;
  color: var(--muted);
  font-size: 10px;
  line-height: 1.55;
}

.api-status.is-success { color: var(--accent); }
.api-status.is-error { color: #b68f84; }

.api-note {
  margin: 12px 2px 0;
  color: var(--faint);
  font-size: 10px;
  line-height: 1.55;
}

.icon-fallback {
  display: inline-block;
  color: currentColor;
  font-size: 15px;
  line-height: 1;
}

.icons-ready .icon-fallback { display: none; }

@media (max-width: 620px) {
  .settings-header { padding: 0 9px; }
  .settings-main { width: calc(100% - 24px); padding: 28px 0 48px; }
  .section-heading { gap: 12px; }
  .background-preview { min-height: 220px; padding: 14px; }
  .background-options { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .image-setting-heading { align-items: flex-start; flex-direction: column; }
  .image-actions { width: 100%; }
  .upload-button,
  .remove-image-button { flex: 1; }
  .avatar-section { margin-top: 34px; padding-top: 28px; }
  .avatar-setting-row { align-items: flex-start; flex-direction: column; gap: 12px; }
  .avatar-setting-row .image-actions { padding-left: 56px; }
  .api-section { margin-top: 34px; padding-top: 28px; }
  .api-action-row { flex-direction: column; }
  .api-connection-main p { white-space: normal; }
  .secondary-settings-button,
  .danger-settings-button,
  .primary-settings-button { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}
