:root {
  color-scheme: dark;
  --bg: #0a0f1a;
  --bg-alt: #0f172a;
  --panel: #111b2e;
  --panel-soft: #1a2740;
  --surface: #101829;
  --surface-muted: #16253d;
  --text: #e2e8f0;
  --muted: #93a2ba;
  --accent: #d8b45c;
  --brand-1: var(--primary, #c89d42);
  --brand-2: var(--secondary, #b6882f);
  --border: #30405f;
  --border-strong: var(--brand-1);
}

body[data-theme='light'] {
  color-scheme: light;
  --bg: #ffffff;
  --bg-alt: #f7efde;
  --panel: #ffffff;
  --panel-soft: #f9eacc;
  --surface: #ffffff;
  --surface-muted: #f8f0df;
  --text: #22262a;
  --muted: #838383;
  --accent: #daa520;
  --brand-1: var(--primary, #f9eacc);
  --brand-2: var(--secondary, #d2ae6d);
  --border: #d3c09f;
  --border-strong: var(--brand-1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: Roboto, 'Segoe UI', sans-serif;
  background: linear-gradient(145deg, var(--bg) 0%, var(--bg-alt) 100%);
  color: var(--text);
}

button,
textarea,
input {
  font: inherit;
}

.floating-btn {
  position: fixed;
  top: 12px;
  right: 12px;
  z-index: 20;
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
  border-radius: 10px;
  padding: 8px 10px;
  cursor: pointer;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.14);
}

.app-shell {
  display: grid;
  grid-template-columns: 320px 1fr;
  height: 100vh;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  box-shadow: 2px 0 18px rgba(0, 0, 0, 0.06);
}

.sidebar-header {
  position: relative;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.brand-wrap {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  text-align: center;
  padding: 8px 0 2px;
}

.brand-logo {
  width: 108px;
  max-width: 100%;
  height: auto;
  border-radius: 0;
  object-fit: contain;
}

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

.config-label {
  font-size: 0.88rem;
  color: var(--muted);
}

.config-input,
textarea,
button {
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
}

.config-input {
  padding: 10px;
  width: 100%;
}

.config-input:focus,
textarea:focus {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 1px;
}

.primary-btn {
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
  border: 1px solid color-mix(in srgb, var(--brand-2) 65%, #000 35%);
  color: #1b1b1b;
  padding: 8px 12px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(190, 145, 55, 0.24);
}

#newChatBtn {
  cursor: pointer;
}

.new-chat-fab {
  position: absolute;
  top: 6px;
  right: 6px;
  width: 42px;
  height: 42px;
  min-width: 0;
  padding: 0;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  font-size: 1.5rem;
  line-height: 1;
}

.icon-btn {
  padding: 8px 10px;
  cursor: pointer;
  transition: background-color 0.2s ease, border-color 0.2s ease;
}

.icon-btn:hover {
  border-color: var(--border-strong);
  background: color-mix(in srgb, var(--panel-soft) 75%, transparent);
}

.chat-area {
  min-height: 0;
  display: flex;
  flex-direction: column;
  flex: 1;
}

.chat-list {
  margin: 0;
  padding: 0;
  list-style: none;
  overflow-y: auto;
  overflow-x: hidden;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

#chatList {
  flex: 1;
}

.chat-list::-webkit-scrollbar {
  width: 8px;
}

.chat-list::-webkit-scrollbar-track {
  background: transparent;
}

.chat-list::-webkit-scrollbar-thumb {
  background: color-mix(in srgb, var(--muted) 60%, transparent);
  border-radius: 20px;
}

.chat-list {
  scrollbar-width: thin;
  scrollbar-color: color-mix(in srgb, var(--muted) 60%, transparent) transparent;
}

.chat-item {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 12px;
  padding: 10px;
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  align-items: center;
  position: relative;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
}

.chat-item.active {
  border-color: var(--border-strong);
  box-shadow: 0 4px 14px rgba(202, 164, 97, 0.18);
}

.chat-item-empty {
  grid-template-columns: 1fr;
  color: var(--muted);
  font-style: italic;
  cursor: default;
  box-shadow: none;
}

.chat-title-btn {
  border: 0;
  background: transparent;
  color: inherit;
  text-align: left;
  padding: 0;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.actions-menu {
  position: absolute;
  right: 0;
  top: 34px;
  min-width: 120px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  display: none;
  z-index: 5;
}

.actions-menu.open {
  display: block;
}

.actions-menu button {
  width: 100%;
  border: 0;
  background: transparent;
  padding: 8px;
  text-align: left;
  cursor: pointer;
  border-radius: 8px;
}

.actions-menu button:hover {
  background: color-mix(in srgb, var(--panel-soft) 70%, transparent);
}

.archived-toggle-btn {
  margin-top: 8px;
  width: 100%;
  padding: 10px;
  cursor: pointer;
}

.archived-toggle-btn[aria-expanded='true'] {
  border-color: var(--border-strong);
  background: color-mix(in srgb, var(--panel-soft) 75%, transparent);
}

.archived-panel {
  display: none;
  margin-top: 8px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface);
  padding: 8px;
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.65);
}

.archived-panel.open {
  display: grid;
  grid-template-rows: auto 1fr;
  gap: 8px;
  max-height: 42vh;
}

.archived-panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

#archivedChatList {
  min-height: 0;
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto auto;
  min-height: 0;
}

.chat-header {
  display: flex;
  align-items: center;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--panel) 92%, transparent);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

.chat-heading {
  width: 100%;
  min-width: 0;
}

.chat-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

#chatTitle {
  margin: 0;
  font-size: 1.1rem;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#chatSubtitle {
  margin: 6px 0 0;
  color: var(--muted);
  font-size: 0.88rem;
}

.message-counter {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px 3px 6px;
  background: var(--surface-muted);
  font-size: 0.8rem;
  color: var(--text);
}

.message-counter-icon {
  width: 20px;
  height: 20px;
  display: inline-grid;
  place-items: center;
  border-radius: 999px;
  font-size: 0.72rem;
  background: color-mix(in srgb, var(--accent) 18%, #fff 82%);
}

.message-counter-value {
  min-width: 1ch;
  font-weight: 700;
}

.messages {
  overflow: auto;
  padding: 18px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.chat-empty-state {
  margin: auto;
  text-align: center;
  max-width: min(520px, 100%);
  color: var(--text);
  padding: 10px 12px;
}

.chat-empty-logo {
  width: clamp(130px, 22vw, 190px);
  max-width: 72%;
  height: auto;
  margin: 0 auto 12px;
  border: 0;
  filter: drop-shadow(0 10px 24px rgba(0, 0, 0, 0.18));
}

.chat-empty-subtitle {
  margin: 0 0 4px;
  font-size: 0.95rem;
  color: var(--muted);
}

.chat-empty-title {
  margin: 0;
  font-size: 1.2rem;
  font-weight: 600;
}

.message {
  max-width: min(85%, 800px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 18px 24px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  font-size: 1.05rem;
  line-height: 1.5;
}

.message.user {
  align-self: flex-end;
  border-color: var(--brand-2);
  background: color-mix(in srgb, var(--brand-1) 25%, var(--surface) 75%);
  border-bottom-right-radius: 4px;
}

.message.assistant {
  align-self: flex-start;
  border-bottom-left-radius: 4px;
}

.message-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: contain;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  position: absolute;
  top: -16px;
}

.message.user .message-avatar {
  right: -8px;
  display: none; /* Ocultar por defecto para el usuario si no se desea */
}

.message.assistant .message-avatar {
  left: -8px;
}

.message-meta {
  font-size: 0.75rem;
  color: var(--muted);
  margin-bottom: 4px;
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.message-content {
  white-space: pre-wrap;
  margin: 0;
}

.message-link {
  color: var(--accent);
  text-decoration: underline;
  text-underline-offset: 2px;
  font-weight: 500;
}

.message-link:hover {
  color: color-mix(in srgb, var(--accent) 72%, #fff 28%);
}

.message-file {
  display: block;
  margin-top: 8px;
  color: var(--accent);
}

.message-actions {
  margin-top: 8px;
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

.message-action-btn {
  position: relative;
  min-width: 42px;
}

.message-action-btn.is-loading {
  color: transparent !important;
  pointer-events: none;
}

.message-action-btn.is-loading::after {
  content: '';
  position: absolute;
  inset: 0;
  margin: auto;
  width: 14px;
  height: 14px;
  border: 2px solid color-mix(in srgb, var(--muted) 60%, transparent);
  border-top-color: var(--accent);
  border-radius: 999px;
  animation: action-spin 0.7s linear infinite;
}

.message-action-btn.is-done {
  border-color: var(--border-strong);
  background: color-mix(in srgb, var(--panel-soft) 75%, transparent);
}

.loading-hint {
  padding: 0 18px 10px;
  color: var(--accent);
  font-style: italic;
}

.composer {
  border-top: 1px solid var(--border);
  padding: 12px;
  display: grid;
  grid-template-columns: 44px 1fr auto;
  gap: 10px;
  align-items: end;
  background: color-mix(in srgb, var(--panel) 90%, transparent);
}

.composer-input-wrap {
  display: grid;
  gap: 8px;
  min-width: 0;
}

.file-badge {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  width: fit-content;
  padding: 6px 10px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--surface-muted);
  min-height: 34px;
}

.file-badge span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.file-badge[hidden] {
  display: none !important;
}

textarea {
  width: 100%;
  min-height: 78px;
  resize: vertical;
  padding: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  line-height: 1.45;
}

.composer-actions {
  display: flex;
  gap: 8px;
  align-items: center;
  min-height: 42px;
}

.composer-actions[hidden],
#sendBtn[hidden],
#stopBtn[hidden] {
  display: none !important;
}

#attachBtn,
#sendBtn,
#stopBtn {
  width: 42px;
  height: 42px;
  display: inline-grid;
  place-items: center;
  padding: 0;
}

.primary-icon-btn {
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
  color: #1b1b1b;
  border: 1px solid color-mix(in srgb, var(--brand-2) 65%, #000 35%);
  font-weight: 700;
  box-shadow: 0 4px 10px rgba(190, 145, 55, 0.24);
}

.app-modal {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 16px;
  background: rgba(2, 8, 20, 0.5);
  backdrop-filter: blur(3px);
}

.app-modal[hidden] {
  display: none !important;
}

.app-modal-dialog {
  width: min(460px, 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--panel);
  color: var(--text);
  padding: 16px;
  box-shadow: 0 22px 40px rgba(0, 0, 0, 0.22);
}

.app-modal-title {
  margin: 0;
  font-size: 1.05rem;
}

.app-modal-message {
  margin: 10px 0 0;
  color: var(--muted);
  line-height: 1.4;
}

.app-modal-input {
  width: 100%;
  margin-top: 12px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text);
}

.app-modal-actions {
  margin-top: 14px;
  display: flex;
  gap: 8px;
  justify-content: flex-end;
}

.app-modal-btn {
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 8px 12px;
  cursor: pointer;
}

.app-modal-btn:not(.secondary) {
  background: linear-gradient(135deg, var(--brand-1), var(--brand-2));
  border-color: color-mix(in srgb, var(--brand-2) 65%, #000 35%);
  color: #1b1b1b;
  font-weight: 600;
}

.app-modal-btn.secondary {
  background: var(--surface-muted);
  color: var(--text);
}

@keyframes action-spin {
  to {
    transform: rotate(360deg);
  }
}

@media (max-width: 900px) {
  .app-shell {
    grid-template-columns: 1fr;
    grid-template-rows: auto 1fr;
    height: 100dvh;
  }

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

  .chat-header {
    padding: 12px;
  }

  .composer {
    grid-template-columns: auto 1fr;
    gap: 8px;
  }

  .composer-actions {
    grid-column: 1 / -1;
    justify-content: flex-end;
  }
}
