: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;
  min-height: 100%;
  font-family: Roboto, 'Segoe UI', sans-serif;
  background: linear-gradient(145deg, var(--bg) 0%, var(--bg-alt) 100%);
  color: var(--text);
  overflow-x: hidden;
}

html,
body {
  height: 100%;
}

body.sidebar-scroll-lock {
  overflow: hidden;
}

button,
textarea,
input {
  font: inherit;
}

.app-shell {
  display: grid;
  grid-template-columns: 1fr;
  min-height: var(--app-height, 100dvh);
  height: var(--app-height, 100dvh);
  position: relative;
  isolation: isolate;
  overflow: hidden;
}

.sidebar {
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
  height: var(--app-height, 100dvh);
  width: min(88vw, 320px);
  max-width: 320px;
  position: fixed;
  top: 0;
  left: 0;
  z-index: 70;
  transform: translateX(-105%);
  transition: transform 0.24s ease;
  box-shadow: 2px 0 18px rgba(0, 0, 0, 0.06);
}

.app-shell.sidebar-open .sidebar {
  transform: translateX(0);
}

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

.sidebar-close-btn {
  position: absolute;
  top: 0;
  left: 0;
  width: 44px;
  height: 44px;
  min-width: 44px;
  display: inline-grid;
  place-items: center;
}

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

.brand-logo {
  width: 108px;
  height: 108px;
  max-width: 100%;
  border-radius: 50%;
  object-fit: cover;
  border: 1px solid var(--border);
  background: var(--panel-soft);
}

.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 {
  min-height: 44px;
  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;
  min-height: 44px;
  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: 44px;
  height: 44px;
  min-width: 0;
  padding: 0;
  border-radius: 999px;
  display: inline-grid;
  place-items: center;
  font-size: 1.5rem;
  line-height: 1;
}

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

.sidebar-toggle-btn {
  width: 44px;
  height: 44px;
  display: inline-grid;
  place-items: center;
  padding: 0;
  margin-right: 10px;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  border: 0;
  padding: 0;
  margin: 0;
  z-index: 60;
  opacity: 0;
  pointer-events: none;
  background: rgba(2, 8, 20, 0.45);
  transition: opacity 0.2s ease;
}

.sidebar-backdrop.is-visible {
  opacity: 1;
  pointer-events: auto;
}

.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;
  min-height: 44px;
  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;
  min-height: 44px;
  padding: 0 2px;
  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%;
  min-height: 44px;
  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;
  min-width: 0;
  min-height: var(--app-height, 100dvh);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  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);
  position: sticky;
  top: 0;
  z-index: 30;
}

.theme-toggle-btn {
  margin-left: auto;
}

.chat-heading {
  flex: 1;
  min-width: 0;
}

.chat-title-row {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  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;
  overflow-x: hidden;
  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-central-image {
  width: min(100%, 420px);
  max-height: 220px;
  object-fit: contain;
  margin: 0 auto 12px;
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--panel-soft);
}

.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(94%, 800px);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 20px;
  padding: 14px 16px;
  box-shadow: 0 6px 16px rgba(0, 0, 0, 0.08);
  display: flex;
  flex-direction: column;
  gap: 10px;
  position: relative;
  font-size: 1rem;
  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: 44px;
}

.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 12px 8px;
  color: var(--accent);
  font-style: italic;
}

.pdf-routing-status {
  margin: 0 12px 8px;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: var(--surface-muted);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}

.pdf-routing-status[hidden] {
  display: none !important;
}

.pdf-routing-status p {
  margin: 0;
  font-size: 0.9rem;
  flex: 1;
  min-width: 0;
}

.pdf-routing-status.is-error {
  border-color: #cc6f67;
  background: color-mix(in srgb, #cc6f67 14%, var(--surface) 86%);
}

.pdf-routing-status.is-success {
  border-color: #5ca874;
  background: color-mix(in srgb, #5ca874 14%, var(--surface) 86%);
}

.pdf-routing-status.is-pending {
  border-color: var(--border-strong);
}

.status-close-btn {
  border: 0;
  background: transparent;
  color: var(--muted);
  width: 28px;
  height: 28px;
  min-width: 28px;
  border-radius: 999px;
  padding: 0;
  cursor: pointer;
  font-size: 1rem;
  line-height: 1;
  display: inline-grid;
  place-items: center;
}

.status-close-btn:hover {
  color: var(--text);
  background: color-mix(in srgb, var(--panel-soft) 75%, transparent);
}

.status-close-btn:focus-visible {
  outline: 2px solid color-mix(in srgb, var(--accent) 55%, transparent);
  outline-offset: 1px;
}

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

.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: 74px;
  resize: none;
  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: 44px;
}

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

#attachBtn,
#sendBtn,
#stopBtn {
  width: 44px;
  height: 44px;
  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);
  }
}

.sidebar-backdrop[hidden] {
  display: none !important;
}

@media (min-width: 600px) {
  .app-shell {
    grid-template-columns: 300px minmax(0, 1fr);
  }

  .sidebar {
    position: sticky;
    top: 0;
    left: 0;
    width: auto;
    max-width: none;
    transform: none;
    z-index: 20;
    height: var(--app-height, 100dvh);
  }

  .sidebar-close-btn {
    display: none;
  }

  .app-shell.sidebar-collapsed-tablet {
    grid-template-columns: 104px minmax(0, 1fr);
  }

  .app-shell.sidebar-collapsed-tablet .brand-wrap {
    padding-inline: 0;
  }

  .app-shell.sidebar-collapsed-tablet .brand-logo {
    width: 58px;
    height: 58px;
  }

  .app-shell.sidebar-collapsed-tablet .config-label,
  .app-shell.sidebar-collapsed-tablet .config-input,
  .app-shell.sidebar-collapsed-tablet .archived-toggle-btn,
  .app-shell.sidebar-collapsed-tablet .archived-panel {
    display: none !important;
  }

  .app-shell.sidebar-collapsed-tablet .chat-item {
    grid-template-columns: 1fr;
    padding: 8px;
  }

  .app-shell.sidebar-collapsed-tablet .chat-actions {
    display: none;
  }

  .app-shell.sidebar-collapsed-tablet .chat-title-btn {
    text-align: center;
    font-size: 0.78rem;
  }

  .sidebar-backdrop {
    display: none !important;
  }

  .chat-header {
    padding: 12px 14px;
  }

  .messages {
    padding: 20px;
  }

  .message {
    max-width: min(88%, 900px);
    padding: 16px 18px;
    font-size: 1.02rem;
  }

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

@media (min-width: 1024px) {
  .app-shell,
  .app-shell.sidebar-collapsed-tablet {
    grid-template-columns: 320px minmax(0, 1fr);
  }

  .sidebar-toggle-btn {
    display: none;
  }

  .chat-header {
    padding: 14px 18px;
  }

  .messages {
    padding: 22px;
  }

  .message {
    max-width: min(82%, 800px);
    padding: 18px 24px;
    font-size: 1.05rem;
  }

  .composer {
    padding: 12px 14px;
  }
}

@media (max-height: 520px) and (orientation: landscape) {
  .chat-header {
    padding: 8px 10px;
  }

  #chatTitle {
    font-size: 1rem;
  }

  .message-counter {
    font-size: 0.72rem;
  }

  .messages {
    padding: 10px;
    gap: 8px;
  }

  .composer {
    padding: 8px 10px;
    gap: 6px;
  }

  textarea {
    min-height: 58px;
  }
}
