/* Settings — layout, cards, appearance, memory */

.settings-scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  padding: 12px 16px;
  padding-bottom: calc(28px + var(--nav-h) + var(--safe-bottom));
}

.settings-group {
  margin-bottom: 28px;
}

.settings-group__title {
  margin: 0 0 10px 2px;
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  letter-spacing: -0.01em;
}

.settings-stack {
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.settings-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
}

.settings-card--pad {
  padding: 16px;
}

.settings-card__lead {
  margin: 0 0 12px;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
}

.settings-card__hint {
  margin: -8px 0 0;
  font-size: 13px;
  color: var(--text-muted);
  line-height: 1.4;
}

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

.settings-card__head .settings-card__lead {
  margin-bottom: 4px;
}

.settings-card__head .settings-card__hint {
  margin: 0;
}

/* Profile */
.settings-card--profile {
  padding: 18px 16px;
  background: linear-gradient(135deg, var(--surface) 0%, color-mix(in srgb, var(--accent) 4%, var(--surface)) 100%);
}

.settings-profile {
  display: flex;
  align-items: center;
  gap: 14px;
}

.settings-profile__avatar {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  overflow: hidden;
  display: grid;
  place-items: center;
  font-size: 20px;
  font-weight: 700;
  color: #fff;
  flex-shrink: 0;
  box-shadow: 0 0 0 3px var(--surface), 0 0 0 5px var(--accent-soft-strong);
}

.settings-profile__avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.settings-profile__body strong {
  display: block;
  font-size: 18px;
  font-weight: 700;
  letter-spacing: -0.02em;
}

.settings-profile__body span {
  display: block;
  margin-top: 2px;
  font-size: 14px;
  color: var(--text-muted);
}

/* Theme segment */
.seg {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  padding: 4px;
  border-radius: 14px;
  background: var(--surface-2);
}

.seg__btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 68px;
  padding: 8px 4px;
  border: none;
  border-radius: 11px;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--motion-fast), color var(--motion-fast), box-shadow var(--motion-fast);
}

.seg__btn.is-active {
  color: var(--accent);
  background: var(--surface);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
}

.seg__btn__icon {
  width: 22px;
  height: 22px;
  display: grid;
  place-items: center;
  line-height: 0;
}

.seg__btn__icon svg {
  display: block;
  width: 20px;
  height: 20px;
}

/* Chat bg preview */
.chatbg-preview {
  flex-shrink: 0;
  width: 88px;
  text-align: center;
}

.chatbg-preview__scene {
  position: relative;
  height: 56px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.06);
}

.chatbg-preview__bubble {
  position: absolute;
  border-radius: 10px;
  box-shadow: 0 1px 2px rgba(15, 23, 42, 0.06);
}

.chatbg-preview__bubble--in {
  left: 8px;
  top: 10px;
  width: 36px;
  height: 14px;
  background: var(--bubble-in);
}

.chatbg-preview__bubble--out {
  right: 8px;
  bottom: 10px;
  width: 28px;
  height: 12px;
  background: var(--bubble-out);
}

.chatbg-preview__name {
  display: block;
  margin-top: 6px;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
}

/* Bg grid — compact */
.bg-tile__swatch {
  display: block;
  box-shadow: inset 0 0 0 1px rgba(15, 23, 42, 0.06);
}

.bg-grid--compact {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.bg-tile--compact {
  border: 2px solid transparent;
  border-radius: 14px;
  padding: 6px;
  background: var(--surface-2);
  cursor: pointer;
  text-align: center;
  transition: border-color var(--motion-fast), transform 0.12s ease, background var(--motion-fast);
}

.bg-tile--compact:active {
  transform: scale(0.97);
}

.bg-tile--compact.is-active {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.chatbg-custom-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.btn-chatbg-upload,
.btn-chatbg-remove {
  flex: 1;
  min-width: 0;
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  border: none;
}

.btn-chatbg-upload {
  background: var(--accent);
  color: #fff;
}

.btn-chatbg-remove {
  background: var(--surface-2);
  color: var(--text-muted);
  border: 1px solid var(--border);
}

.bg-tile--compact .bg-tile__swatch {
  height: 40px;
  border-radius: 10px;
  margin-bottom: 5px;
}

.bg-tile--compact .bg-tile__name {
  font-size: 10px;
  font-weight: 600;
  color: var(--text-muted);
  line-height: 1.2;
}

.bg-tile--compact.is-active .bg-tile__name {
  color: var(--accent);
}

/* Memory */
.settings-card--memory .settings-card__lead {
  margin-bottom: 4px;
}

.settings-card--memory .settings-card__hint {
  margin-bottom: 16px;
}

.memory-meter {
  margin-bottom: 16px;
}

.memory-meter__bar {
  height: 8px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.memory-meter__fill {
  display: block;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, var(--accent), color-mix(in srgb, var(--accent) 70%, #6366f1));
  transition: width var(--motion-base);
}

.memory-meter__meta {
  display: flex;
  justify-content: space-between;
  margin-top: 8px;
  font-size: 13px;
  color: var(--text-muted);
}

.memory-limit {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin-bottom: 14px;
}

.memory-limit__label {
  font-size: 13px;
  font-weight: 600;
  color: var(--text-muted);
}

.cache-seg {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.cache-seg__btn {
  min-height: 44px;
  border: 1.5px solid var(--border);
  border-radius: 11px;
  background: var(--surface-2);
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  cursor: pointer;
  transition: border-color var(--motion-fast), color var(--motion-fast), background var(--motion-fast);
}

.cache-seg__btn.is-active {
  border-color: var(--accent);
  color: var(--accent);
  background: var(--accent-soft);
}

.btn-cache-clear {
  width: 100%;
  min-height: 44px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--motion-fast);
}

.btn-cache-clear:active {
  background: var(--surface-2);
}

/* Toggles */
.settings-card--rows {
  padding: 2px 16px;
}

.settings-toggle {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: center;
  gap: 16px;
  min-height: 56px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
}

.settings-toggle:last-child {
  border-bottom: none;
}

.settings-toggle input {
  position: absolute;
  opacity: 0;
  pointer-events: none;
}

.settings-toggle__text strong {
  display: block;
  font-size: 15px;
  font-weight: 600;
}

.settings-toggle__text span {
  display: block;
  font-size: 13px;
  color: var(--text-muted);
  margin-top: 2px;
  line-height: 1.35;
}

.settings-toggle__track {
  width: 50px;
  height: 30px;
  border-radius: 999px;
  background: var(--border);
  position: relative;
  flex-shrink: 0;
  transition: background var(--motion-fast);
}

.settings-toggle__track::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.15);
  transition: transform var(--motion-fast);
}

.settings-toggle input:checked + .settings-toggle__track {
  background: var(--accent);
}

.settings-toggle input:checked + .settings-toggle__track::after {
  transform: translateX(20px);
}

/* Logout + footer */
.btn-logout {
  display: grid;
  place-items: center;
  width: 100%;
  height: 48px;
  padding: 0;
  border: none;
  border-radius: 14px;
  background: rgba(220, 38, 38, 0.08);
  color: var(--danger);
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
  transition: background var(--motion-fast);
}

.btn-logout:active {
  background: rgba(220, 38, 38, 0.14);
}

.settings-footer {
  text-align: center;
  font-size: 13px;
  color: var(--text-muted);
  padding: 8px 0 12px;
}

.settings-footer__brand {
  display: block;
  font-weight: 600;
  color: var(--text);
  margin-bottom: 4px;
}

/* Dark theme tweaks */
html[data-theme="dark"] .seg__btn.is-active {
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.25);
}

html[data-theme="dark"] .bg-tile--compact {
  background: var(--surface-2);
}

html[data-theme="dark"] .chatbg-preview__scene {
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.06);
}

/* Responsive */
@media (max-width: 860px) {
  .view--settings .settings-scroll {
    padding-bottom: calc(28px + var(--nav-h) + var(--safe-bottom));
  }
}

@media (min-width: 861px) {
  .view--settings {
    max-width: 520px;
    margin: 0 auto;
    width: 100%;
  }

  .settings-scroll {
    padding: 16px 20px 32px;
  }

  .bg-grid--compact {
    grid-template-columns: repeat(6, 1fr);
  }

  .bg-tile--compact .bg-tile__name {
    font-size: 11px;
  }
}

@media (min-width: 1024px) {
  .view--settings {
    max-width: 560px;
  }
}
