:root {
    --primary-color: #9333ea;
    --primary-light: #c084fc;
    --bg-dark: #0b0b0f;
    --bg-header: #14141c;
    --bubble-sent: linear-gradient(135deg, #7c3aed, #6d28d9);
    --bubble-sent-alt: #6d28d9;
    --bubble-received: #1c1c26;
    --surface: #1c1c26;
    --text-primary: #f3f4f6;
    --text-secondary: #9ca3af;
    --text-dim: #6b7280;
    --danger: #ff3b30;
    --success: #34c759;
    --warning: #ff9f0a;
    --info: #00d4ff;
    --gold: #ffd700;
    --shadow-msg: 0 2px 8px rgba(0,0,0,0.3);
    --safe-top: env(safe-area-inset-top, 0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
}

* {
    box-sizing: border-box;
    -webkit-tap-highlight-color: transparent;
    margin: 0;
    padding: 0;
}

html, body {
    height: 100%;
}

body {
    background-color: var(--bg-dark);
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Helvetica, Arial, sans-serif;
    overflow: hidden;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-rendering: optimizeLegibility;
    color: var(--text-primary);
    color-scheme: dark;
    -webkit-text-size-adjust: 100%;
    text-size-adjust: 100%;
}

button {
    font-family: inherit;
    cursor: pointer;
    touch-action: manipulation;
}

input, textarea, select {
    font-family: inherit;
}

::selection {
    background: rgba(147, 51, 234, 0.55);
    color: #fff;
}

:focus-visible {
    outline: 2px solid var(--primary-light);
    outline-offset: 2px;
}

button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

input[type="number"]::-webkit-outer-spin-button,
input[type="number"]::-webkit-inner-spin-button {
    -webkit-appearance: none;
    margin: 0;
}

input[type="number"] {
    appearance: textfield;
    -moz-appearance: textfield;
}

select {
    -webkit-appearance: none;
    -moz-appearance: none;
    appearance: none;
}

@keyframes popIn {
    0% { transform: scale(0.92) translateY(6px); opacity: 0; }
    100% { transform: scale(1) translateY(0); opacity: 1; }
}

@keyframes slideDown {
    0% { transform: translateY(-20px); opacity: 0; }
    100% { transform: translateY(0); opacity: 1; }
}

@keyframes fadeOut {
    to { opacity: 0; transform: translateY(-10px); }
}

@keyframes heartBeat {
    0%, 100% { transform: scale(1); }
    25% { transform: scale(1.15); }
    40% { transform: scale(0.95); }
    60% { transform: scale(1.1); }
}

@keyframes pulseText {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.4; }
}

@keyframes orbitSpin {
    to { transform: translate(-50%, -50%) rotate(360deg); }
}

@keyframes orbitSpinReverse {
    to { transform: translate(-50%, -50%) rotate(-360deg); }
}

@keyframes corePulse {
    0%, 100% { transform: scale(0.9); box-shadow: 0 0 0 0 rgba(192,132,252,0.15), 0 0 22px rgba(0,212,255,0.4); }
    50% { transform: scale(1.08); box-shadow: 0 0 0 12px rgba(192,132,252,0), 0 0 38px rgba(0,212,255,0.8); }
}

@keyframes sparkFloat {
    0%, 100% { opacity: 0.35; transform: scale(0.75) translateY(5px); }
    50% { opacity: 1; transform: scale(1) translateY(-5px); }
}

* {
    scrollbar-width: thin;
    scrollbar-color: rgba(255,255,255,0.15) transparent;
}

*::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

*::-webkit-scrollbar-track {
    background: transparent;
}

*::-webkit-scrollbar-thumb {
    background: rgba(255,255,255,0.15);
    border-radius: 10px;
    border: 2px solid var(--bg-dark);
}

*::-webkit-scrollbar-thumb:hover {
    background: var(--primary-color);
}

.chat-window, #contacts-list, .emoji-panel, .modal-content, #bottom-sheet {
    -webkit-overflow-scrolling: touch;
}

.app-container {
    height: 100vh;
    height: 100dvh;
    width: 100vw;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    position: relative;
    background: var(--bg-dark);
}

.screen {
    height: 100%;
    width: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

.hidden {
    display: none !important;
}

#active-chat-screen.hidden {
    display: none !important;
}

#login-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    z-index: 2000;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(ellipse at 50% 0%, #1e1b4b 0%, #0b0b0f 60%);
    padding: 20px;
}

.auth-box {
    width: 100%;
    max-width: 360px;
    background: rgba(20, 20, 28, 0.92);
    border: 1px solid rgba(147, 51, 234, 0.25);
    border-radius: 24px;
    padding: 35px 28px;
    text-align: center;
    box-shadow: 0 25px 60px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.05);
    animation: popIn 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.275);
    -webkit-backdrop-filter: blur(20px);
    backdrop-filter: blur(20px);
}

.auth-box h1 {
    background: linear-gradient(135deg, #c084fc, #7c3aed, #00d4ff);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
    font-size: 32px;
    margin: 0 0 5px 0;
    letter-spacing: -0.5px;
}

.auth-box .auth-subtitle {
    color: var(--text-secondary);
    font-size: 13px;
    margin-bottom: 22px;
}

.auth-box .auth-logo {
    font-size: 42px;
    margin-bottom: 10px;
    display: block;
    animation: heartBeat 2s ease-in-out infinite;
}

.auth-input {
    width: 100%;
    padding: 14px 18px;
    margin: 8px 0;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.1);
    background: rgba(0,0,0,0.35);
    color: white;
    font-size: 15px;
    outline: none;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.auth-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(147, 51, 234, 0.15);
}

.auth-input::placeholder {
    color: var(--text-dim);
}

.legal-check {
    display: flex;
    align-items: flex-start;
    gap: 8px;
    margin: 12px 0;
    text-align: left;
    font-size: 12px;
    color: var(--text-secondary);
}

.legal-check input {
    margin-top: 3px;
    accent-color: var(--primary-color);
    cursor: pointer;
}

.legal-check a {
    color: var(--primary-light);
    text-decoration: none;
}

.legal-check a:hover {
    text-decoration: underline;
}

.auth-btn {
    width: 100%;
    padding: 14px;
    margin-top: 10px;
    border: none;
    border-radius: 12px;
    font-size: 15px;
    font-weight: 700;
    transition: transform 0.2s, opacity 0.2s;
}

.auth-btn:hover {
    transform: translateY(-1px);
}

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

.auth-btn-primary {
    background: linear-gradient(135deg, #7c3aed, #6d28d9);
    color: white;
    box-shadow: 0 8px 20px rgba(147, 51, 234, 0.35);
}

.auth-btn-secondary {
    background: rgba(255,255,255,0.06);
    color: var(--text-primary);
    border: 1px solid rgba(255,255,255,0.12);
}

#auth-msg {
    color: var(--danger);
    font-size: 12px;
    font-weight: 600;
    margin-top: 12px;
    min-height: 16px;
}

.main-header, .chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    padding-top: calc(12px + var(--safe-top));
    background: rgba(18, 18, 26, 0.92);
    -webkit-backdrop-filter: blur(20px);
    backdrop-filter: blur(20px);
    border-bottom: 1px solid rgba(255,255,255,0.06);
    z-index: 10;
    flex-shrink: 0;
}

.chat-header {
    padding: 10px 14px;
    padding-top: calc(10px + var(--safe-top));
}

.mobile-visible {
    display: flex;
}

.chat-window {
    flex: 1;
    overflow-y: auto;
    padding: 20px 12px;
    padding-bottom: calc(20px + var(--safe-bottom));
    display: flex;
    flex-direction: column;
    gap: 3px;
    scroll-behavior: smooth;
    overscroll-behavior: contain;
    position: relative;
    background-color: var(--bg-dark);
    background-image: radial-gradient(circle at 20% 30%, rgba(147,51,234,0.03) 0%, transparent 40%), radial-gradient(circle at 80% 70%, rgba(0,212,255,0.02) 0%, transparent 40%);
}

.date-separator {
    align-self: center;
    background: rgba(255,255,255,0.06);
    color: var(--text-secondary);
    font-size: 11px;
    font-weight: 600;
    padding: 4px 14px;
    border-radius: 12px;
    margin: 12px 0 8px 0;
    letter-spacing: 0.3px;
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    border: 1px solid rgba(255,255,255,0.04);
}

.message {
    padding: 9px 14px;
    border-radius: 18px;
    max-width: 82%;
    font-size: 14.5px;
    line-height: 1.45;
    word-wrap: break-word;
    overflow-wrap: break-word;
    position: relative;
    box-shadow: var(--shadow-msg);
    animation: popIn 0.25s ease-out;
    margin: 2px 0;
}

.message.sent {
    align-self: flex-end;
    background: var(--bubble-sent);
    color: white;
    border-bottom-right-radius: 6px;
    border-top-right-radius: 18px;
    box-shadow: 0 4px 14px rgba(147, 51, 234, 0.25);
}

.message.sent::after {
    content: '';
    position: absolute;
    bottom: 0;
    right: -7px;
    width: 12px;
    height: 12px;
    background: var(--bubble-sent-alt);
    border-bottom-left-radius: 50%;
    border-top-right-radius: 0;
}

.message.received {
    align-self: flex-start;
    background: var(--bubble-received);
    color: var(--text-primary);
    border-bottom-left-radius: 6px;
    border: 1px solid rgba(255,255,255,0.04);
}

.message.received::before {
    content: '';
    position: absolute;
    bottom: 0;
    left: -7px;
    width: 12px;
    height: 12px;
    background: var(--bubble-received);
    border-bottom-right-radius: 50%;
    border-left: 1px solid rgba(255,255,255,0.04);
}

.message.system {
    align-self: center;
    background: rgba(255, 159, 10, 0.08);
    color: var(--warning);
    border: 1px dashed rgba(255, 159, 10, 0.4);
    font-size: 12px;
    text-align: center;
    border-radius: 14px;
    padding: 6px 16px;
    font-weight: 700;
    max-width: 90%;
    letter-spacing: 0.3px;
    box-shadow: 0 0 15px rgba(255, 159, 10, 0.08);
    margin: 8px 0;
}

.message.system::before, .message.system::after {
    display: none;
}

.message-meta {
    display: flex;
    align-items: center;
    justify-content: flex-end;
    gap: 3px;
    margin-top: 4px;
    font-size: 10px;
    color: rgba(255,255,255,0.55);
    line-height: 1;
}

.message.sent .message-meta {
    color: rgba(255,255,255,0.75);
}

.tick-icon {
    font-size: 11px;
    line-height: 1;
}

.tick-read {
    color: #4fc3f7;
}

.thought-text {
    color: #ff8a80;
    font-style: italic;
    font-weight: 500;
}

.quote-block {
    background: rgba(0, 0, 0, 0.25);
    border-left: 3px solid var(--primary-color);
    padding: 6px 10px;
    margin-bottom: 8px;
    border-radius: 6px;
    font-size: 12.5px;
    color: rgba(255,255,255,0.7);
}

.image-message {
    position: relative;
    width: 100%;
    min-height: 260px;
    background: #000;
    border-radius: 14px;
    overflow: hidden;
    margin-top: 5px;
    border: 1px solid rgba(255,255,255,0.08);
}

.image-message img {
    width: 100%;
    height: auto;
    display: block;
    border-radius: 14px;
    cursor: zoom-in;
}

.image-actions {
    display: flex;
    gap: 6px;
    position: absolute;
    bottom: 8px;
    right: 8px;
}

.image-action-btn {
    background: rgba(0,0,0,0.8);
    border: 1px solid rgba(255,255,255,0.2);
    border-radius: 10px;
    padding: 7px 12px;
    color: white;
    font-size: 11px;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    gap: 4px;
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
    transition: background 0.2s;
}

.image-action-btn:hover {
    background: rgba(255,255,255,0.2);
}

.chat-input-area {
    display: flex;
    padding: 10px 10px;
    padding-bottom: calc(10px + var(--safe-bottom));
    background: rgba(18, 18, 26, 0.95);
    -webkit-backdrop-filter: blur(20px);
    backdrop-filter: blur(20px);
    align-items: flex-end;
    gap: 6px;
    border-top: 1px solid rgba(255,255,255,0.06);
    flex-shrink: 0;
    position: relative;
    z-index: 5;
}

.input-wrapper {
    flex: 1;
    display: flex;
    background: rgba(255,255,255,0.06);
    border-radius: 24px;
    padding: 4px 6px;
    border: 1px solid rgba(255,255,255,0.06);
    transition: border-color 0.3s, box-shadow 0.3s;
    align-items: center;
}

.input-wrapper:focus-within {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(147, 51, 234, 0.1);
}

.input-text {
    width: 100%;
    padding: 9px 10px;
    border: none;
    background: transparent;
    color: white;
    outline: none;
    font-size: 15px;
    resize: none;
}

.input-text::placeholder {
    color: var(--text-dim);
}

.emoji-btn {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    padding: 8px;
    border-radius: 50%;
    cursor: pointer;
    flex-shrink: 0;
    font-size: 20px;
    line-height: 1;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s, color 0.2s;
}

.emoji-btn:hover {
    color: var(--primary-light);
    transform: scale(1.15);
}

.emoji-panel {
    display: none;
    position: absolute;
    bottom: calc(100% + 8px);
    left: 10px;
    right: 10px;
    background: var(--surface);
    border: 1px solid rgba(255,255,255,0.08);
    border-radius: 16px;
    padding: 12px;
    z-index: 50;
    box-shadow: 0 -10px 40px rgba(0,0,0,0.5);
    max-height: 220px;
    overflow-y: auto;
    overscroll-behavior: contain;
}

.emoji-panel.active {
    display: grid;
    grid-template-columns: repeat(8, 1fr);
    gap: 4px;
}

.emoji-panel button {
    background: transparent;
    border: none;
    font-size: 22px;
    padding: 6px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 0.15s, transform 0.15s;
}

.emoji-panel button:hover {
    background: rgba(147,51,234,0.2);
    transform: scale(1.2);
}

.icon-button, .action-button, .send-button {
    background: transparent;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 10px;
    border-radius: 50%;
    transition: all 0.2s;
    flex-shrink: 0;
}

.icon-button:hover, .action-button:hover {
    background: rgba(255,255,255,0.08);
    color: white;
    transform: scale(1.08);
}

.send-button {
    color: var(--primary-light);
}

.send-button:hover {
    background: rgba(147, 51, 234, 0.15);
    transform: scale(1.1);
}

.nsfw-btn-clean {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 5px 11px;
    border-radius: 20px;
    font-weight: 800;
    font-size: 10.5px;
    background: rgba(0,0,0,0.4);
    border: 1px solid #555;
    color: #fff;
    cursor: pointer;
    transition: transform 0.2s, box-shadow 0.2s;
    letter-spacing: 0.5px;
    flex-shrink: 0;
}

.nsfw-btn-clean:hover {
    transform: scale(1.05);
}

#reply-preview-area {
    display: none;
    position: absolute;
    top: -1px;
    left: 0;
    right: 0;
    transform: translateY(-100%);
    background: rgba(147, 51, 234, 0.1);
    border-left: 4px solid var(--primary-color);
    padding: 10px 16px;
    font-size: 13px;
    color: var(--text-primary);
    -webkit-backdrop-filter: blur(10px);
    backdrop-filter: blur(10px);
    z-index: 6;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    border-radius: 10px 10px 0 0;
}

#reply-preview-area button {
    background: none;
    border: none;
    color: var(--text-secondary);
    font-size: 18px;
    cursor: pointer;
    flex-shrink: 0;
}

#contacts-list {
    flex: 1;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding-top: 4px;
}

.contact-item {
    display: flex;
    padding: 14px 18px;
    align-items: center;
    gap: 14px;
    cursor: pointer;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    transition: background 0.2s;
    position: relative;
}

.contact-item:hover {
    background: rgba(147, 51, 234, 0.06);
}

.contact-item:active {
    background: rgba(147, 51, 234, 0.1);
}

.contact-avatar {
    width: 52px;
    height: 52px;
    border-radius: 50%;
    object-fit: cover;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    flex-shrink: 0;
    border: 2px solid rgba(147, 51, 234, 0.3);
}

.contact-details {
    flex: 1;
    overflow: hidden;
    min-width: 0;
}

.contact-name {
    margin: 0;
    color: var(--text-primary);
    font-size: 15.5px;
    font-weight: 700;
    letter-spacing: 0.3px;
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
}

.contact-last-msg {
    margin: 4px 0 0 0;
    color: var(--text-secondary);
    font-size: 13px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.ruta-badge {
    background: rgba(255,159,10,0.12);
    color: var(--warning);
    font-size: 9.5px;
    border: 1px solid rgba(255,159,10,0.3);
    border-radius: 12px;
    padding: 2px 7px;
    white-space: nowrap;
}

.header-info {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.header-avatar-wrap {
    position: relative;
    flex-shrink: 0;
}

.header-avatar-wrap img {
    width: 42px;
    height: 42px;
    border-radius: 50%;
    object-fit: cover;
    box-shadow: 0 4px 12px rgba(0,0,0,0.4);
    border: 2px solid rgba(255,255,255,0.15);
}

.online-dot {
    position: absolute;
    bottom: 1px;
    right: 1px;
    width: 11px;
    height: 11px;
    background: var(--success);
    border-radius: 50%;
    border: 2px solid var(--bg-header);
}

.header-title {
    color: white;
    font-size: 15.5px;
    font-weight: 700;
    margin: 0;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.header-status {
    color: var(--success);
    font-size: 11.5px;
    display: block;
    margin-top: 2px;
}

.header-status.typing-status {
    color: var(--primary-light);
    animation: pulseText 1.5s infinite ease-in-out;
}

.header-affection {
    color: #f472b6;
    font-size: 11px;
    font-weight: 600;
}

#custom-modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: rgba(0,0,0,0.85);
    z-index: 3000; /* ¡AQUÍ ESTÁ LA MAGIA! Antes decía 1000 */
    align-items: center;
    justify-content: center;
    padding: 20px;
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
}

#custom-modal.active {
    display: flex;
}

.modal-sheet-full {
    align-items: flex-end;
    padding: 0;
}

.modal-sheet-full .modal-content {
    border-radius: 24px 24px 0 0;
    max-width: 100%;
    max-height: 90vh;
    border-bottom: none;
}

.modal-content {
    background: var(--surface);
    padding: 24px;
    border-radius: 22px;
    width: 100%;
    max-width: 380px;
    color: var(--text-primary);
    border: 1px solid rgba(147, 51, 234, 0.3);
    box-shadow: 0 20px 60px rgba(0,0,0,0.7);
    max-height: 85vh;
    overflow-y: auto;
    overscroll-behavior: contain;
    display: flex;
    flex-direction: column;
    animation: popIn 0.3s cubic-bezier(0.175, 0.885, 0.32, 1.275);
}

#modal-title {
    font-size: 18px;
    font-weight: 800;
    margin-bottom: 8px;
    background: linear-gradient(135deg, #c084fc, #f472b6);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

#modal-text {
    font-size: 14px;
    color: var(--text-secondary);
    line-height: 1.5;
}

.modal-input {
    width: 100%;
    padding: 14px;
    margin: 6px 0;
    border-radius: 12px;
    border: 1px solid rgba(255,255,255,0.12);
    background: rgba(0,0,0,0.35);
    color: white;
    font-size: 15px;
    outline: none;
    transition: border-color 0.3s, box-shadow 0.3s;
}

.modal-input:focus {
    border-color: var(--primary-color);
    box-shadow: 0 0 0 3px rgba(147, 51, 234, 0.12);
}

.modal-input::placeholder {
    color: var(--text-dim);
}

.modal-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 10px;
    margin-top: 18px;
    position: sticky;
    bottom: 0;
    background: var(--surface);
    padding-top: 14px;
    border-top: 1px solid rgba(255,255,255,0.06);
    flex-shrink: 0;
}

.modal-buttons button {
    padding: 12px 20px;
    border: none;
    border-radius: 12px;
    cursor: pointer;
    font-weight: 700;
    font-size: 14px;
    transition: transform 0.2s, opacity 0.2s;
}

.modal-buttons button:hover {
    transform: scale(1.03);
}

.modal-buttons button:active {
    transform: scale(0.97);
}

#modal-cancel {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid rgba(255,255,255,0.15);
}

#modal-confirm {
    background: linear-gradient(135deg, #7c3aed, #6d28d9);
    color: white;
    box-shadow: 0 6px 18px rgba(147, 51, 234, 0.35);
}

#toast-container {
    position: fixed;
    top: calc(15px + var(--safe-top));
    left: 50%;
    transform: translateX(-50%);
    z-index: 9999999;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
    width: 92%;
    max-width: 400px;
}

.toast {
    background: rgba(25, 25, 34, 0.97);
    color: white;
    padding: 14px 20px;
    border-radius: 14px;
    font-size: 13.5px;
    font-weight: 700;
    text-align: center;
    box-shadow: 0 12px 40px rgba(0,0,0,0.7);
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    animation: slideDown 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
    border-left: 4px solid var(--primary-color);
    line-height: 1.4;
}

.toast.success { border-color: var(--success); }
.toast.error { border-color: var(--danger); }
.toast.vip { border-color: var(--warning); }
.toast.diamond { border-color: var(--info); }
.toast.levelup { border-color: var(--warning); background: linear-gradient(135deg, rgba(255,159,10,0.15), rgba(255,59,48,0.15)); }
.toast.xp { border-color: var(--info); }
.toast.achievement { border-color: var(--gold); background: linear-gradient(135deg, rgba(255,215,0,0.12), rgba(255,159,10,0.12)); }
.toast.streak { border-color: var(--danger); }

.lightbox {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100vh;
    height: 100dvh;
    background: rgba(0,0,0,0.96);
    z-index: 10000;
    justify-content: center;
    align-items: center;
    overflow: hidden;
    touch-action: none;
}

.lightbox.active {
    display: flex;
}

.lightbox img {
    max-width: 100vw;
    max-height: 100vh;
    max-height: 100dvh;
    transition: transform 0.1s ease-out;
    object-fit: contain;
    cursor: grab;
    user-select: none;
    -webkit-user-select: none;
    border-radius: 8px;
}

.lightbox img:active {
    cursor: grabbing;
}

#zoom-controls {
    position: fixed;
    bottom: calc(30px + var(--safe-bottom));
    left: 50%;
    transform: translateX(-50%);
    display: flex;
    gap: 10px;
    padding: 10px 16px;
    background: rgba(20, 20, 28, 0.9);
    border-radius: 30px;
    box-shadow: 0 8px 30px rgba(0,0,0,0.6);
    z-index: 10002;
    -webkit-backdrop-filter: blur(12px);
    backdrop-filter: blur(12px);
    border: 1px solid rgba(255,255,255,0.1);
}

#zoom-controls button {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    border: 1px solid rgba(255,255,255,0.15);
    background: rgba(255,255,255,0.06);
    color: white;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: background 0.2s, transform 0.2s;
}

#zoom-controls button:hover {
    background: var(--primary-color);
    transform: scale(1.1);
}

@keyframes chatSlideIn {
    from { transform: translateX(100%); opacity: 0.5; }
    to { transform: translateX(0); opacity: 1; }
}

#close-lightbox {
    position: absolute;
    top: calc(20px + var(--safe-top));
    right: 20px;
    color: white;
    font-size: 36px;
    font-weight: 300;
    cursor: pointer;
    z-index: 10001;
    background: rgba(0,0,0,0.5);
    width: 44px;
    height: 44px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s, transform 0.2s;
    line-height: 1;
    -webkit-backdrop-filter: blur(5px);
    backdrop-filter: blur(5px);
}

#close-lightbox:hover {
    background: rgba(255,255,255,0.2);
    transform: rotate(90deg);
}

#sheet-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    height: 100dvh;
    background: rgba(0,0,0,0.7);
    z-index: 999;
    display: none;
    opacity: 0;
    transition: opacity 0.3s;
    -webkit-backdrop-filter: blur(3px);
    backdrop-filter: blur(3px);
}

#sheet-overlay.active {
    display: block;
    opacity: 1;
}

#bottom-sheet {
    position: fixed;
    bottom: -100%;
    left: 0;
    width: 100%;
    background: var(--surface);
    border-top-left-radius: 25px;
    border-top-right-radius: 25px;
    z-index: 1000;
    transition: bottom 0.35s cubic-bezier(0.1, 0.8, 0.2, 1);
    padding: 16px 20px calc(30px + var(--safe-bottom)) 20px;
    border-top: 1px solid rgba(147, 51, 234, 0.25);
    box-shadow: 0 -10px 50px rgba(0,0,0,0.7);
    max-height: 80vh;
    overflow-y: auto;
    overscroll-behavior: contain;
}

#bottom-sheet.active {
    bottom: 0;
}

.sheet-handle {
    width: 50px;
    height: 5px;
    background: rgba(255,255,255,0.2);
    border-radius: 5px;
    margin: 0 auto 18px auto;
}

.sheet-option {
    width: 100%;
    padding: 16px;
    background: transparent;
    border: none;
    border-bottom: 1px solid rgba(255,255,255,0.05);
    color: var(--text-primary);
    font-size: 15px;
    text-align: center;
    cursor: pointer;
    transition: background 0.2s;
    border-radius: 12px;
    margin-bottom: 2px;
}

.sheet-option:hover {
    background: rgba(255,255,255,0.05);
}

.sheet-option:last-child {
    border-bottom: none;
    margin-bottom: 0;
}

.level-container {
    display: flex;
    align-items: center;
    gap: 4px;
    flex-shrink: 0;
}

.level-badge {
    background: linear-gradient(135deg, #9333ea, #7e22ce);
    color: white;
    font-size: 10px;
    font-weight: 800;
    padding: 3px 6px;
    border-radius: 12px;
    white-space: nowrap;
    box-shadow: 0 2px 8px rgba(147, 51, 234, 0.3);
}

#level-bar-bg, .level-bar-bg {
    width: 35px !important;
    min-width: 35px !important;
    height: 6px;
    background: rgba(255,255,255,0.1);
    border-radius: 3px;
    overflow: hidden;
    flex-shrink: 0;
}

.level-bar-fill, #level-bar {
    height: 100%;
    background: linear-gradient(90deg, #a855f7, #00d4ff);
    border-radius: 3px;
    transition: width 0.6s ease;
}

#xp-text, .xp-text {
    display: none !important;
}

.main-header > div:last-child {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.streak-badge {
    background: rgba(255, 159, 10, 0.12);
    color: var(--warning);
    font-size: 11px;
    font-weight: 700;
    padding: 4px 9px;
    border-radius: 12px;
    border: 1px solid rgba(255, 159, 10, 0.3);
    white-space: nowrap;
}

.coin-text {
    color: var(--primary-light);
    font-weight: 800;
    font-size: 13.5px;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 3px;
}

.fab-btn {
    position: absolute;
    bottom: calc(85px + var(--safe-bottom));
    right: 24px;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    background: linear-gradient(135deg, #7c3aed, #00d4ff);
    color: white;
    border: none;
    box-shadow: 0 8px 25px rgba(147, 51, 234, 0.5);
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 100;
    transition: transform 0.2s, box-shadow 0.2s;
}

.fab-btn:hover {
    transform: scale(1.08) translateY(-2px);
    box-shadow: 0 12px 30px rgba(147, 51, 234, 0.6);
}

.fab-btn:active {
    transform: scale(0.95);
}

.app-bottom-nav {
    display: flex;
    justify-content: space-around;
    padding: 10px;
    background: rgba(18, 18, 26, 0.95);
    border-top: 1px solid rgba(255,255,255,0.06);
    z-index: 10;
    margin-top: auto; /* Esto la empuja siempre al fondo de la lista de chats */
    flex-shrink: 0;
}

.app-bottom-nav button {
    flex: 1;
    min-height: 44px;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    padding: 8px 12px;
    border: 0;
    border-radius: 12px;
    background: transparent;
    color: var(--text-secondary);
    font-size: 12px;
    font-weight: 700;
}

.app-bottom-nav button:hover,
.app-bottom-nav button:focus-visible {
    color: var(--text-primary);
    background: rgba(147,51,234,0.2);
}

.app-bottom-nav svg {
    width: 17px;
    height: 17px;
    fill: none;
    stroke: currentColor;
    stroke-width: 2;
    stroke-linecap: round;
    stroke-linejoin: round;
}

.gallery-item {
    position: relative;
    overflow: hidden;
    border: 1px solid #333;
    border-radius: 8px;
    background: #000;
}

.gallery-item > img {
    width: 100%;
    display: block;
    cursor: zoom-in;
    aspect-ratio: 2 / 3;
    object-fit: cover;
}

.gallery-item-footer {
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 5px 6px 5px 8px;
    color: var(--warning);
    background: rgba(0,0,0,0.75);
    font-size: 12px;
    font-weight: 700;
}

.gallery-delete-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    border: 1px solid rgba(255,255,255,0.25);
    border-radius: 7px;
    color: white;
    background: rgba(255,59,48,0.8);
}

.gallery-delete-btn svg {
    width: 15px;
    height: 15px;
}

@supports (padding: env(safe-area-inset-top)) {
    .main-header, .chat-header {
        padding-top: calc(12px + env(safe-area-inset-top));
    }
    .chat-input-area {
        padding-bottom: calc(10px + env(safe-area-inset-bottom));
    }
}

@media (min-width: 768px) {
    .app-container {
        flex-direction: row;
        background: #060608;
        height: 100vh;
    }
    #chat-list-screen {
        width: 340px;
        border-right: 1px solid rgba(255,255,255,0.08);
        display: flex !important;
        background: #0e0e14;
        flex-shrink: 0;
    }
    #active-chat-screen {
        flex: 1;
        display: flex !important;
        background: var(--bg-dark);
    }

    #active-chat-screen.hidden {
        display: none !important;
    }

    .modal-content {
        max-width: 400px;
    }
}

@media (min-width: 1200px) {
    .app-container {
        max-width: 1400px;
        max-height: 95vh;
        margin: 0 auto;
        height: 95vh;
        border-radius: 20px;
        border: 1px solid rgba(255,255,255,0.1);
        box-shadow: 0 0 100px rgba(147, 51, 234, 0.08), 0 30px 80px rgba(0,0,0,0.6);
        margin-top: 2.5vh;
        overflow: hidden;
    }
    #chat-list-screen {
        width: 380px;
    }
}

@media (prefers-reduced-motion: reduce) {
    * {
        animation-duration: 0.01ms !important;
        transition-duration: 0.01ms !important;
    }
}

@media (max-width: 480px) {
    .main-header {
        padding: 10px 8px !important;
        padding-top: calc(10px + var(--safe-top)) !important;
    }
    .level-bar-bg, .xp-text {
        display: none !important;
    }
    .main-header h2 {
        font-size: 20px !important;
    }
    .main-header > div:nth-child(2) {
        gap: 3px !important;
    }
    .main-header .icon-button {
        padding: 6px !important;
    }
    .streak-badge, .coin-text {
        font-size: 11px !important;
        padding: 3px 6px !important;
    }
    #admin-panel-btn {
        padding: 4px 6px !important;
        font-size: 10px !important;
        margin-right: 2px !important;
    }

    .app-bottom-nav {
        left: 8px;
        right: 8px;
        bottom: max(8px, var(--safe-bottom));
        transform: none;
        justify-content: space-around;
    }

    .app-bottom-nav button {
        flex: 1;
        min-width: 0;
        padding-inline: 8px;
    }
}

.loading-message {
    width: min(100%, 360px);
    padding: 0;
    overflow: hidden;
    background: transparent !important;
    border: 0 !important;
    box-shadow: none !important;
}

.loading-message::before {
    display: none;
}

.image-loading-art {
    position: relative;
    min-height: 250px;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    overflow: hidden;
    border: 1px solid rgba(192,132,252,0.2);
    border-radius: 22px;
    background: radial-gradient(circle at 50% 42%, rgba(0,212,255,0.16), transparent 24%), radial-gradient(circle at 50% 50%, rgba(147,51,234,0.18), transparent 58%), #101019;
    box-shadow: inset 0 1px 0 rgba(255,255,255,0.08), 0 18px 42px rgba(0,0,0,0.25);
}

.image-loading-art::before,
.image-loading-art::after {
    content: '';
    position: absolute;
    width: 180px;
    height: 180px;
    border: 1px solid rgba(255,255,255,0.06);
    border-radius: 50%;
}

.image-loading-art::after {
    width: 240px;
    height: 240px;
    border-color: rgba(0,212,255,0.05);
}

.loading-starfield {
    position: absolute;
    inset: 0;
    opacity: 0.7;
    background-image: radial-gradient(circle, rgba(255,255,255,0.85) 0 1px, transparent 1.5px), radial-gradient(circle, rgba(0,212,255,0.65) 0 1px, transparent 1.5px);
    background-position: 18% 28%, 76% 65%;
    background-size: 88px 76px, 121px 108px;
    animation: sparkFloat 3.5s ease-in-out infinite alternate;
}

.loading-core {
    position: relative;
    z-index: 3;
    width: 62px;
    height: 62px;
    display: grid;
    place-items: center;
    border: 1px solid rgba(255,255,255,0.35);
    border-radius: 20px;
    background: linear-gradient(135deg, rgba(192,132,252,0.95), rgba(0,212,255,0.85));
    box-shadow: 0 0 28px rgba(0,212,255,0.4);
    animation: corePulse 2.2s ease-in-out infinite;
}

.loading-core span {
    width: 24px;
    height: 24px;
    display: block;
    border: 2px solid rgba(255,255,255,0.9);
    border-radius: 7px;
    transform: rotate(45deg);
}

.loading-orbit {
    position: absolute;
    left: 50%;
    top: 43%;
    z-index: 2;
    width: 132px;
    height: 58px;
    border: 1px solid rgba(0,212,255,0.65);
    border-radius: 50%;
    transform: translate(-50%, -50%) rotate(20deg);
    animation: orbitSpin 3.8s linear infinite;
}

.loading-orbit-two {
    width: 155px;
    height: 72px;
    border-color: rgba(192,132,252,0.7);
    transform: translate(-50%, -50%) rotate(-42deg);
    animation: orbitSpinReverse 4.8s linear infinite;
}

.loading-spark {
    position: absolute;
    z-index: 4;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #fff;
    box-shadow: 0 0 14px 3px var(--info);
    animation: sparkFloat 1.8s ease-in-out infinite;
}

.loading-spark-one { top: 30%; left: 31%; }
.loading-spark-two { right: 29%; bottom: 42%; animation-delay: 0.65s; background: var(--primary-light); box-shadow: 0 0 14px 3px var(--primary-color); }
.loading-spark-three { top: 23%; right: 40%; width: 4px; height: 4px; animation-delay: 0.25s; }
.loading-spark-four { bottom: 30%; left: 39%; width: 5px; height: 5px; animation-delay: 1s; background: var(--info); }

.image-loading-text {
    position: relative;
    z-index: 5;
    margin: 30px 0 3px;
    color: var(--text-primary);
    font-size: 14px;
    font-weight: 700;
    text-shadow: 0 2px 12px rgba(0,0,0,0.7);
}

.image-loading-caption {
    position: relative;
    z-index: 5;
    color: var(--text-secondary);
    font-size: 11px;
}