:root {
  color-scheme: dark;
  --pink: rgba(255, 0, 200, 0.35);
  --cyan: rgba(0, 200, 255, 0.35);
  --line: rgba(255, 255, 255, 0.23);
  --text: #fff;
  --muted: rgba(255, 255, 255, 0.72);
  --surface: rgba(20, 19, 32, 0.68);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body { height: 100%; }

body {
  color: var(--text);
  font-family: sans-serif;
  background: #24212c;
  padding: max(12px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  display: flex;
  justify-content: center;
  -webkit-text-size-adjust: 100%;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: -1;
  background: linear-gradient(rgba(12, 12, 20, 0.44), rgba(12, 12, 20, 0.44)), url("background.webp") center / cover no-repeat;
}

button, input, textarea, select { font: inherit; }

button {
  min-height: 42px;
  padding: 9px 14px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 9px;
  background: var(--pink);
  cursor: pointer;
  transition: background 160ms ease, transform 160ms ease;
  touch-action: manipulation;
}
button:hover:not(:disabled) { background: rgba(255, 0, 200, 0.52); }
button:active:not(:disabled) { transform: translateY(1px); }
button:disabled { opacity: 0.55; cursor: not-allowed; }

.chat-container {
  width: 100%;
  max-width: 900px;
  height: 100%;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--surface);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 12px 34px rgba(0, 0, 0, 0.2);
}

.chat-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 12px 14px;
  border-bottom: 1px solid var(--line);
}
.chat-header h1 {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
  font-size: clamp(1.1rem, 4vw, 1.5rem);
}
.badge {
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 0.7rem;
  background: var(--cyan);
  white-space: nowrap;
}
.header-controls { display: flex; gap: 10px; }
.clear-button { background: rgba(220, 65, 80, 0.58); white-space: nowrap; }

/* Tabs */
.tabs-container {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px 12px;
  border-bottom: 1px solid var(--line);
  scrollbar-width: none;
}
.tabs-container::-webkit-scrollbar { display: none; }
.tabs-wrapper { display: flex; gap: 8px; align-items: center; width: max-content; }

.tab {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  min-height: 42px;
  max-width: 220px;
  padding: 8px 12px;
  border: 1px solid var(--line);
  border-radius: 9px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  font-weight: 700;
  font-size: 0.9rem;
  cursor: pointer;
}
.tab.active { border-color: rgba(0, 220, 255, 0.68); background: var(--cyan); }
.tab.personality-playful { border-top: 3px solid #667eea; }
.tab.personality-friendly { border-top: 3px solid #4CAF50; }
.tab.personality-professional { border-top: 3px solid #2196F3; }
.tab.personality-creative { border-top: 3px solid #FF9800; }
.tab.personality-seductive { border-top: 3px solid #D81B60; }
.tab.personality-philosophical { border-top: 3px solid #9C27B0; }
.tab.personality-humorous { border-top: 3px solid #FF5722; }
.tab.personality-poetic { border-top: 3px solid #E91E63; }
.tab.personality-socratic { border-top: 3px solid #795548; }

.tab-title { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.tab-message-count {
  padding: 2px 7px;
  border-radius: 12px;
  font-size: 0.72rem;
  background: rgba(255, 255, 255, 0.16);
}
.tab-close {
  min-height: 0;
  width: 28px;
  height: 28px;
  padding: 0;
  border: 0;
  border-radius: 50%;
  background: transparent;
  font-size: 1.2rem;
  line-height: 1;
}
.tab-close:hover:not(:disabled) { background: rgba(220, 65, 80, 0.58); }

.new-tab-button {
  white-space: nowrap;
  background: var(--cyan);
  font-size: 0.9rem;
}
.new-tab-button:hover:not(:disabled) { background: rgba(0, 200, 255, 0.55); }

/* Personality */
.personality-selector {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
}
.selector-label { display: flex; align-items: center; gap: 6px; font-weight: 700; font-size: 0.9rem; }
.personality-select {
  flex: 1 1 140px;
  min-width: 0;
  min-height: 42px;
  padding: 10px 12px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  outline: none;
  background: rgba(255, 0, 200, 0.24);
}
.personality-select option { color: #17131c; }
.personality-select:focus { border-color: rgba(0, 220, 255, 0.8); }
.greeting-button { background: var(--cyan); white-space: nowrap; }
.greeting-button:hover:not(:disabled) { background: rgba(0, 200, 255, 0.55); }
.greeting-button.reset { background: var(--pink); }

/* Messages */
.messages-area {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  padding: 14px 12px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.message {
  max-width: 85%;
  padding: 10px 14px;
  border: 1px solid rgba(255, 255, 255, 0.14);
  border-radius: 12px;
  animation: fadeIn 0.3s ease-out;
}
@keyframes fadeIn {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
.message.user { align-self: flex-end; background: var(--cyan); border-bottom-right-radius: 4px; }
.message.assistant { align-self: flex-start; background: var(--pink); border-bottom-left-radius: 4px; }
.message.system {
  align-self: center;
  max-width: 95%;
  background: rgba(255, 255, 255, 0.08);
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}
.message.editing {
  align-self: stretch;
  max-width: 100%;
  background: var(--surface);
  border-color: rgba(0, 220, 255, 0.65);
}

.message-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  margin-bottom: 4px;
  font-size: 0.85rem;
  color: var(--muted);
}
.message-actions { display: flex; gap: 2px; }
.action-button {
  min-height: 34px;
  min-width: 34px;
  padding: 2px 6px;
  border: 0;
  background: transparent;
}
.action-button:hover:not(:disabled) { background: rgba(255, 255, 255, 0.15); }

.message-content { overflow-wrap: anywhere; line-height: 1.45; }
.message-time { display: block; margin-top: 4px; font-size: 0.72rem; color: var(--muted); }

.edit-textarea {
  width: 100%;
  min-height: 115px;
  margin: 8px 0;
  padding: 10px 12px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  outline: none;
  resize: vertical;
  background: rgba(255, 0, 200, 0.24);
}
.edit-actions { display: flex; gap: 8px; justify-content: flex-end; }
.save-edit { background: var(--cyan); }

.edited-badge {
  margin-left: 4px;
  padding: 2px 6px;
  border-radius: 10px;
  font-size: 0.7rem;
  font-style: italic;
  background: rgba(255, 255, 255, 0.15);
}
.edit-history {
  margin-top: 8px;
  padding-top: 8px;
  border-top: 1px dashed var(--line);
  font-size: 0.78rem;
  color: var(--muted);
}
.edit-history summary { cursor: pointer; font-weight: 700; }
.edit-history-item { padding: 4px 0; overflow-wrap: anywhere; }
.edit-history-item button {
  min-height: 0;
  padding: 2px 6px;
  border: 0;
  background: transparent;
  color: #00c8ff;
  text-decoration: underline;
  font-size: 0.75rem;
}

.empty-chat { align-self: center; margin: auto; padding: 30px 16px; text-align: center; color: var(--muted); }
.empty-chat div { margin-bottom: 14px; font-size: 3.5rem; }

.typing-indicator {
  display: flex;
  gap: 5px;
  align-self: flex-start;
  padding: 14px 18px;
  border-radius: 12px;
  background: var(--pink);
}
.typing-indicator span {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #fff;
  animation: typing 1s infinite ease-in-out;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.2s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.4s; }
@keyframes typing {
  0%, 60%, 100% { transform: translateY(0); }
  30% { transform: translateY(-8px); }
}

/* Input */
.input-form {
  display: flex;
  gap: 8px;
  padding: 10px 12px;
  border-top: 1px solid var(--line);
}
.input-form input {
  flex: 1;
  min-width: 0;
  min-height: 46px;
  padding: 10px 14px;
  color: var(--text);
  border: 1px solid var(--line);
  border-radius: 7px;
  outline: none;
  background: rgba(255, 0, 200, 0.24);
  font-size: 16px; /* evita el zoom automático de iOS */
}
.input-form input::placeholder { color: var(--muted); }
.input-form input:focus { border-color: rgba(0, 220, 255, 0.8); }
.input-form button { min-height: 46px; background: var(--cyan); white-space: nowrap; }
.input-form button:hover:not(:disabled) { background: rgba(0, 200, 255, 0.55); }

.storage-info {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  border-top: 1px solid var(--line);
  font-size: 0.8rem;
  color: var(--muted);
}
.storage-badge { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; min-width: 0; }
.message-count, .personality-badge {
  padding: 3px 10px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.12);
  color: var(--text);
  white-space: nowrap;
}

@media (max-width: 600px) {
  body { padding: max(6px, env(safe-area-inset-top)) max(6px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(6px, env(safe-area-inset-left)); }
  .chat-container { border-radius: 10px; }
  .badge { display: none; }
  .message { max-width: 92%; }
  /* Acciones siempre visibles: no hay hover en táctil */
  .message-actions { opacity: 1; }
  .input-form button { padding: 9px 12px; }
  .selector-label span:last-child { display: none; }
  .storage-info { font-size: 0.72rem; }
  .personality-badge { display: none; }
}

@media (hover: hover) {
  .message-actions { opacity: 0; transition: opacity 0.2s; }
  .message:hover .message-actions { opacity: 1; }
}

@supports (height: 100dvh) {
  html, body { height: 100dvh; }
}
