:root {
  --bg: #f4f6f9;
  --info: #0ea5e9;
  --text: #1a2744;
  --brand: #0f2042;
  --muted: #64748b;
  --panel: #ffffff;
  --accent: #00c9a7;
  --border: #d0d8e4;
  --danger: #ef4444;
  --panel2: #edf1f7;
  --success: #10b981;
  --warning: #f59e0b;
  --brand-light: #3b6cb5;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", system-ui, -apple-system, sans-serif;
  background: var(--bg);
  color: var(--text);
}

/* ==========================================================================
   ACCESS SCREEN (Login con cedula)
   ========================================================================== */

.access-screen {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  background: #ffffff;
}

.access-topbar {
  background: var(--brand);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 24px;
}

.topbar-logo {
  height: 30px;
  object-fit: contain;
}

.topbar-status {
  display: flex;
  align-items: center;
  gap: 8px;
  color: #ffffff;
  font-size: 13px;
  font-weight: 500;
}

.topbar-status .status-dot {
  width: 8px;
  height: 8px;
  flex-shrink: 0;
}

.access-body {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 32px 24px 16px;
  text-align: center;
}

.sofia-avatar {
  width: 180px;
  height: 180px;
  object-fit: contain;
  margin-bottom: 16px;
  filter: drop-shadow(0 8px 24px rgba(15, 32, 66, 0.15));
}

.access-greeting {
  font-size: 26px;
  font-weight: 700;
  color: var(--brand);
  margin: 0 0 6px;
}

.access-instruction {
  font-size: 15px;
  color: var(--muted);
  margin: 0 0 28px;
}

.access-form {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
  width: 100%;
  max-width: 360px;
}

.cedula-input-group {
  display: flex;
  width: 100%;
  border-radius: 28px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: #ffffff;
  box-shadow: 0 2px 12px rgba(15, 32, 66, 0.06);
  transition: box-shadow 0.2s, border-color 0.2s;
}

.cedula-input-group:focus-within {
  border-color: var(--brand-light);
  box-shadow: 0 2px 16px rgba(59, 108, 181, 0.15);
}

.cedula-prefix {
  width: 62px;
  min-width: 62px;
  border: none;
  border-right: 1px solid var(--border);
  background: var(--panel2);
  color: var(--brand);
  font-weight: 700;
  font-size: 15px;
  padding: 12px 8px 12px 16px;
  cursor: pointer;
  appearance: none;
  -webkit-appearance: none;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%230f2042'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
}

.cedula-prefix:focus {
  outline: none;
}

.cedula-input-group input {
  flex: 1;
  border: none;
  border-radius: 0;
  padding: 12px 16px;
  font-size: 16px;
  color: var(--text);
  background: transparent;
}

.cedula-input-group input:focus {
  outline: none;
  border-color: transparent;
}

.access-btn {
  width: 100%;
  max-width: 360px;
  padding: 13px 24px;
  border: none;
  border-radius: 28px;
  background: var(--brand);
  color: #ffffff;
  font-size: 16px;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, box-shadow 0.2s;
  box-shadow: 0 4px 14px rgba(15, 32, 66, 0.2);
}

.access-btn:hover {
  background: var(--accent);
  box-shadow: 0 6px 20px rgba(15, 32, 66, 0.28);
}

.access-btn:active {
  transform: scale(0.98);
}

.access-footer {
  padding: 20px 24px;
  text-align: center;
}

.footer-logo {
  height: 36px;
  object-fit: contain;
  opacity: 0.85;
}

/* ==========================================================================
   FONDO ANIMADO (canvas compartido entre loading y chat)
   ========================================================================== */

.network-canvas {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 0;
  display: none;
  pointer-events: none;
}

body.chat-mode {
  background: var(--brand);
}

body.chat-mode .network-canvas {
  display: block;
}

/* ==========================================================================
   LOADING / SPLASH SCREEN
   ========================================================================== */

.loading-screen {
  position: fixed;
  inset: 0;
  z-index: 900;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(15, 32, 66, 0.92);
  overflow: hidden;
}

.loading-content {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 24px;
}

.loading-logo {
  height: 40px;
  object-fit: contain;
  margin-bottom: 32px;
  opacity: 0.9;
}

.loading-avatar {
  width: 160px;
  height: 160px;
  object-fit: contain;
  margin-bottom: 20px;
  filter: drop-shadow(0 6px 28px rgba(0, 0, 0, 0.35));
  animation: loading-avatar-float 3s ease-in-out infinite;
}

@keyframes loading-avatar-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

.loading-title {
  font-size: 28px;
  font-weight: 700;
  color: #ffffff;
  margin: 0 0 24px;
}

.loading-spinner {
  margin-bottom: 20px;
}

.spinner-ring {
  width: 40px;
  height: 40px;
  border: 3px solid rgba(255, 255, 255, 0.15);
  border-top-color: #7dd3fc;
  border-left-color: #86efac;
  border-radius: 50%;
  animation: spinner-rotate 1s linear infinite;
}

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

.loading-status {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin: 0;
  transition: opacity 0.3s;
}

.loading-error {
  color: #fca5a5;
  font-size: 14px;
  margin: 10px 0 0;
  max-width: 320px;
  line-height: 1.4;
}

.loading-screen.error-state .spinner-ring {
  animation: none;
  border-color: rgba(252, 165, 165, 0.3);
  border-top-color: #fca5a5;
}

.loading-screen.error-state .loading-status {
  color: rgba(255, 255, 255, 0.5);
}

@media (max-width: 480px) {
  .loading-avatar {
    width: 120px;
    height: 120px;
  }

  .loading-title {
    font-size: 24px;
  }
}

/* ==========================================================================
   APP SHELL (Chat)
   ========================================================================== */

main {
  position: relative;
  z-index: 1;
}

body.chat-mode main {
  min-height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}

.app-shell {
  max-width: 920px;
  margin: 0 auto;
  padding: 24px;
  width: 100%;
}

body.chat-mode .app-shell {
  margin: 0;
}

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 20px;
  box-shadow: 0 8px 30px rgba(15, 32, 66, 0.06);
}

body.chat-mode .panel {
  box-shadow: 0 24px 64px rgba(0, 0, 0, 0.5), 0 8px 30px rgba(0, 0, 0, 0.3);
  border-color: rgba(125, 211, 252, 0.18);
}

.stack {
  display: grid;
  gap: 10px;
}

label {
  font-weight: 600;
}

input,
button {
  font: inherit;
}

input {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  color: var(--text);
  background: var(--panel);
}

input:focus {
  outline: 2px solid var(--brand-light);
  border-color: var(--brand-light);
}

button {
  border: 1px solid var(--brand);
  background: var(--brand);
  color: #fff;
  border-radius: 10px;
  padding: 10px 14px;
  cursor: pointer;
}

button:hover {
  background: var(--accent);
}

.ghost-btn {
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--brand);
}

.ghost-btn:hover {
  background: var(--panel2);
  border-color: var(--accent);
  color: var(--accent);
}

/* ==========================================================================
   RESPONSIVE - Access Screen
   ========================================================================== */

@media (max-width: 480px) {
  .access-topbar {
    padding: 8px 16px;
  }

  .topbar-logo {
    height: 24px;
  }

  .sofia-avatar {
    width: 140px;
    height: 140px;
  }

  .access-greeting {
    font-size: 22px;
  }

  .access-body {
    padding: 24px 16px 12px;
  }
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
  margin-bottom: 12px;
}

.chat-header > div {
  display: flex;
  align-items: center;
  gap: 12px;
}

.chat-header h2 {
  margin: 0;
  line-height: 1;
}

#chat-meta {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin: 0;
  line-height: 1;
}

#chat-meta .status-dot {
  position: relative;
  top: 0;
}

/* Botón logout (icono) */
.logout-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--muted);
  font-size: 18px;
  cursor: pointer;
  transition: background 0.2s, color 0.2s, border-color 0.2s;
  flex-shrink: 0;
}

.logout-btn:hover {
  background: var(--danger);
  border-color: var(--danger);
  color: #fff;
}

.status-dot {
  width: 10px;
  height: 10px;
  border-radius: 999px;
  display: inline-block;
  vertical-align: middle;
  flex-shrink: 0;
  border: 1px solid rgba(0, 0, 0, 0.08);
}

.status-dot.live {
  background: var(--success);
}

.status-dot.fallback {
  background: var(--warning);
}

.status-dot.offline {
  background: var(--muted);
}

.messages {
  height: 56vh;
  overflow: auto;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(180deg, var(--panel2), var(--panel));
  padding: 14px;
  display: grid;
  gap: 10px;
  justify-items: start;
}

.message {
  width: fit-content;
  max-width: 82%;
  min-width: 72px;
  padding: 10px 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  overflow-wrap: anywhere;
}

.message.message-file {
  max-width: min(360px, 82%);
}

.message.user {
  justify-self: end;
  border-color: var(--brand-light);
  background: #e8eef6;
}

.message.agent {
  justify-self: start;
}

.message.system {
  justify-self: center;
  background: #fffbeb;
  border-color: #fde68a;
}

.message .meta {
  display: block;
  color: var(--muted);
  font-size: 12px;
  margin-bottom: 4px;
}

.message-text > :first-child {
  margin-top: 0;
}

.message-text > :last-child {
  margin-bottom: 0;
}

.message-text p,
.message-text ul,
.message-text ol,
.message-text pre,
.message-text blockquote {
  margin: 0 0 8px;
}

.message-text code {
  font-family: Consolas, "Courier New", monospace;
  background: #e2e8f0;
  padding: 1px 4px;
  border-radius: 4px;
}

.message-text pre {
  background: #0f172a;
  color: #e2e8f0;
  padding: 8px;
  border-radius: 8px;
  overflow: auto;
}

.message-text pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

.file-preview {
  width: min(320px, 70vw);
  border: 1px solid var(--border);
  background: var(--panel2);
  border-radius: 10px;
  padding: 8px;
  display: grid;
  gap: 8px;
}

.thumb-image,
.thumb-video,
.thumb-pdf {
  width: 100%;
  max-width: 320px;
  border-radius: 8px;
  border: 1px solid var(--border);
  background: #fff;
}

.thumb-image {
  max-height: 240px;
  object-fit: cover;
}

.thumb-video {
  max-height: 240px;
}

.thumb-pdf {
  height: 240px;
}

.thumb-audio {
  width: min(320px, 100%);
}

.file-preview-meta {
  color: var(--muted);
  font-size: 12px;
}

.file-preview-actions {
  display: flex;
  gap: 8px;
}

.file-preview-btn {
  padding: 6px 10px;
  border-radius: 8px;
}

.send-box {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr auto auto;
  gap: 10px;
  align-items: center;
}

/* ---------- Menú "+" de opciones ---------- */

.options-menu-wrapper {
  position: relative;
}

.options-toggle-btn {
  width: 40px;
  height: 40px;
  border-radius: 50%;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--panel2);
  color: var(--brand);
  transition: background 0.2s, transform 0.2s;
}

.options-toggle-btn svg {
  display: block;
}

.options-toggle-btn:hover {
  background: var(--accent);
  color: #fff;
}

.options-toggle-btn.open {
  transform: rotate(45deg);
  background: var(--accent);
  color: #fff;
}

.options-dropdown {
  position: absolute;
  bottom: calc(100% + 8px);
  right: 0;
  min-width: 180px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: 0 8px 24px rgba(15, 32, 66, 0.14);
  padding: 6px 0;
  z-index: 50;
  opacity: 0;
  transform: translateY(6px) scale(0.95);
  pointer-events: none;
  transition: opacity 0.15s, transform 0.15s;
}

.options-dropdown.open {
  opacity: 1;
  transform: translateY(0) scale(1);
  pointer-events: auto;
}

.options-dropdown button {
  width: 100%;
  background: transparent;
  border: none;
  color: var(--text);
  text-align: left;
  padding: 10px 16px;
  font-size: 14px;
  cursor: pointer;
  border-radius: 0;
  display: flex;
  align-items: center;
  gap: 10px;
  transition: background 0.15s;
}

.options-dropdown button:hover {
  background: var(--panel2);
}

.options-dropdown button .opt-icon {
  width: 20px;
  text-align: center;
  font-size: 16px;
}

#record-btn.recording {
  background: #fee2e2 !important;
  color: #b91c1c !important;
}

/* ---------- Indicador de escritura (typing) ---------- */

.typing-indicator {
  width: fit-content;
  padding: 12px 16px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: #fff;
  display: flex;
  align-items: center;
  gap: 5px;
}

.typing-indicator .dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  animation: typing-bounce 1.4s ease-in-out infinite;
}

.typing-indicator .dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-indicator .dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    transform: translateY(0);
    opacity: 0.4;
  }
  30% {
    transform: translateY(-6px);
    opacity: 1;
  }
}

.muted {
  color: var(--muted);
}

.tiny {
  font-size: 12px;
}

.error {
  color: var(--danger);
  margin-top: 10px;
}

.hidden {
  display: none !important;
}

.preview-modal {
  position: fixed;
  inset: 0;
  background: rgba(2, 6, 23, 0.55);
  display: grid;
  place-items: center;
  padding: 16px;
  z-index: 1000;
}

.preview-panel {
  width: min(1000px, 96vw);
  max-height: 92vh;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 12px;
}

.preview-close {
  margin-left: auto;
  display: block;
}

.preview-content > img,
.preview-content > video,
.preview-content > iframe {
  width: 100%;
  max-height: 76vh;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: #fff;
}

.preview-content > audio {
  width: 100%;
}

.location-panel {
  width: min(700px, 96vw);
}

.location-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 10px;
}

.location-header h3 {
  margin: 0;
}

.location-search-box {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  margin-bottom: 8px;
}

.location-search-results {
  max-height: 160px;
  overflow-y: auto;
  border: 1px solid var(--border);
  border-radius: 8px;
  margin-bottom: 8px;
  background: var(--panel);
}

.location-result-item {
  padding: 8px 12px;
  cursor: pointer;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
}

.location-result-item:last-child {
  border-bottom: none;
}

.location-result-item:hover {
  background: var(--panel2);
}

.location-map {
  height: 350px;
  border-radius: 10px;
  border: 1px solid var(--border);
  margin-bottom: 8px;
}

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

.location-preview {
  width: min(280px, 70vw);
  border: 1px solid var(--border);
  background: var(--panel2);
  border-radius: 10px;
  overflow: hidden;
}

.location-preview-map {
  width: 100%;
  height: 150px;
  border-bottom: 1px solid var(--border);
}

.location-preview-info {
  padding: 8px 10px;
  font-size: 13px;
  color: var(--text);
}

.location-preview-coords {
  font-size: 11px;
  color: var(--muted);
  margin-top: 2px;
}
