/* ============================================================
   P.O.P.E.Y.E. v1.0 - Sistema de Diseño Global
   Desarrollado e implementado por Luis Briones - Especialista Técnico
   Mobile-First | Dark Mode Alto Contraste | Telecomunicaciones FTTH
============================================================ */

/* ============================================================
   1. VARIABLES CSS (Design Tokens)
============================================================ */
:root {
    --bg-primary:   #0b132b;
    --bg-secondary: #1c2541;
    --bg-card:      #16213e;
    --bg-bubble-bot: #1a2d4a;
    --bg-bubble-user: #0d3a2b;

    --cyan:    #00f5d4;
    --green:   #10b981;
    --amber:   #f59e0b;
    --red:     #ef4444;
    --blue:    #3b82f6;
    --purple:  #8b5cf6;

    --text:         #e2e8f0;
    --text-muted:   #94a3b8;
    --text-faint:   #475569;

    --border:       rgba(0, 245, 212, 0.15);
    --border-subtle: rgba(255, 255, 255, 0.07);

    --shadow-glow:  0 0 30px rgba(0, 245, 212, 0.08);
    --shadow-card:  0 4px 24px rgba(0, 0, 0, 0.4);
    --shadow-deep:  0 20px 60px rgba(0, 0, 0, 0.6);

    --radius-sm: 8px;
    --radius-md: 12px;
    --radius-lg: 20px;

    --font-sans: 'Segoe UI', system-ui, -apple-system, BlinkMacSystemFont, 'Helvetica Neue', sans-serif;
    --font-mono: 'Consolas', 'Courier New', monospace;

    --header-h: 56px;
    --footer-h: auto;
}

/* ============================================================
   2. RESET & BASE
============================================================ */
*, *::before, *::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

html {
    scroll-behavior: smooth;
    -webkit-text-size-adjust: 100%;
}

body {
    font-family: var(--font-sans);
    background-color: var(--bg-primary);
    color: var(--text);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    overscroll-behavior: none;
}

/* Scrollbar personalizada */
::-webkit-scrollbar { width: 4px; height: 4px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: rgba(0,245,212,0.3); border-radius: 99px; }

/* ============================================================
   3. LAYOUT DE PÁGINA DE CHAT
============================================================ */
body.chat-page {
    display: flex;
    flex-direction: column;
    height: 100dvh;
    overflow: hidden;
}

/* ============================================================
   4. HEADER DE LA APP
============================================================ */
.app-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 14px;
    height: var(--header-h);
    background: var(--bg-card);
    border-bottom: 1px solid var(--border);
    box-shadow: var(--shadow-glow);
    flex-shrink: 0;
    z-index: 50;
    gap: 8px;
}

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

.header-branding {
    display: flex;
    flex-direction: column;
    justify-content: center;
}

.header-logo {
    font-size: 1.1rem;
    font-weight: 900;
    letter-spacing: 3px;
    color: var(--cyan);
    text-shadow: 0 0 20px rgba(0,245,212,0.3);
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 6px;
    line-height: 1.1;
}

.header-version {
    font-size: 0.65rem;
    font-weight: 600;
    color: var(--green);
    background: rgba(16,185,129,0.12);
    border: 1px solid rgba(16,185,129,0.3);
    border-radius: 99px;
    padding: 1px 6px;
    letter-spacing: 0.5px;
}

.header-author-sub {
    font-size: 0.62rem;
    color: var(--text-muted);
    letter-spacing: 0.2px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: none;
}

@media (min-width: 600px) {
    .header-author-sub { display: block; }
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 5px;
    background: rgba(16,185,129,0.08);
    border: 1px solid rgba(16,185,129,0.2);
    border-radius: 99px;
    padding: 3px 8px;
    flex-shrink: 0;
}

.status-dot {
    width: 7px;
    height: 7px;
    background: var(--green);
    border-radius: 50%;
    animation: pulse-status 2s ease-in-out infinite;
    flex-shrink: 0;
}

@keyframes pulse-status {
    0%, 100% { opacity: 1; transform: scale(1); box-shadow: 0 0 0 0 rgba(16,185,129,0.4); }
    50%       { opacity: 0.8; transform: scale(1.15); box-shadow: 0 0 0 4px rgba(16,185,129,0); }
}

.status-text {
    font-size: 0.68rem;
    font-weight: 600;
    color: var(--green);
    white-space: nowrap;
}

.header-right {
    display: flex;
    align-items: center;
    gap: 6px;
    flex-shrink: 0;
}

.header-btn {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 6px 10px;
    background: rgba(0,245,212,0.08);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    color: var(--cyan);
    font-size: 0.72rem;
    font-weight: 700;
    cursor: pointer;
    text-decoration: none;
    white-space: nowrap;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}

.header-btn:hover,
.header-btn:active { background: rgba(0,245,212,0.15); }

.header-btn-admin {
    background: rgba(245,158,11,0.08);
    border-color: rgba(245,158,11,0.3);
    color: var(--amber);
}

.header-btn-admin:hover { background: rgba(245,158,11,0.15); }

.header-user {
    display: flex;
    align-items: center;
    gap: 6px;
}

.user-name {
    font-size: 0.78rem;
    font-weight: 700;
    color: var(--text);
    display: none;
}

@media (min-width: 400px) { .user-name { display: inline; } }

.logout-btn {
    font-size: 1.1rem;
    text-decoration: none;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
    transition: transform 0.2s;
}
.logout-btn:hover { transform: scale(1.1); }

/* ============================================================
   5. ÁREA DE MENSAJES (CHAT MAIN)
============================================================ */
.chat-main {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 16px 12px;
    display: flex;
    flex-direction: column;
    gap: 0;
    -webkit-overflow-scrolling: touch;
}

.chat-messages {
    display: flex;
    flex-direction: column;
    gap: 12px;
    max-width: 780px;
    margin: 0 auto;
    width: 100%;
}

/* ============================================================
   6. BURBUJAS DE MENSAJE
============================================================ */
.message {
    display: flex;
    gap: 10px;
    align-items: flex-end;
    animation: slideInMsg 0.25s ease-out;
}

@keyframes slideInMsg {
    from { opacity: 0; transform: translateY(10px); }
    to   { opacity: 1; transform: translateY(0); }
}

.message-avatar {
    width: 34px;
    height: 34px;
    border-radius: 50%;
    background: var(--bg-secondary);
    border: 1.5px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
    flex-shrink: 0;
    align-self: flex-start;
    margin-top: 4px;
}

.message-content {
    max-width: calc(100% - 50px);
    min-width: 60px;
}

.message-bubble {
    padding: 12px 16px;
    border-radius: var(--radius-lg);
    font-size: 0.9rem;
    line-height: 1.6;
    word-break: break-word;
    position: relative;
}

/* Bot messages */
.message-bot .message-bubble {
    background: var(--bg-bubble-bot);
    border: 1px solid rgba(0,245,212,0.1);
    border-bottom-left-radius: 6px;
    color: var(--text);
}

/* User messages */
.message-user {
    flex-direction: row-reverse;
}
.message-user .message-bubble {
    background: var(--bg-bubble-user);
    border: 1px solid rgba(16,185,129,0.15);
    border-bottom-right-radius: 6px;
    color: var(--text);
    text-align: right;
}
.message-user .message-content {
    align-items: flex-end;
    display: flex;
    flex-direction: column;
}

.message-meta {
    font-size: 0.68rem;
    color: var(--text-faint);
    margin-top: 4px;
    padding: 0 4px;
}

.message-user .message-meta {
    text-align: right;
}

/* Botón copiar */
.copy-btn {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    margin-top: 8px;
    padding: 5px 10px;
    background: rgba(0,245,212,0.08);
    border: 1px solid var(--border);
    border-radius: 99px;
    color: var(--cyan);
    font-size: 0.72rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}
.copy-btn:hover { background: rgba(0,245,212,0.15); }
.copy-btn.copied { background: rgba(16,185,129,0.15); color: var(--green); border-color: rgba(16,185,129,0.3); }

/* Fuentes del manual */
.sources-bar {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin-top: 10px;
}
.source-tag {
    background: rgba(0,245,212,0.06);
    border: 1px solid rgba(0,245,212,0.15);
    border-radius: 99px;
    padding: 2px 10px;
    font-size: 0.68rem;
    color: var(--text-muted);
}

/* ============================================================
   7. INDICADOR DE CARGA (TYPING)
============================================================ */
.typing-indicator {
    display: none;
    align-items: flex-end;
    gap: 10px;
}
.typing-indicator.visible { display: flex; }

.typing-bubble {
    background: var(--bg-bubble-bot);
    border: 1px solid rgba(0,245,212,0.1);
    border-radius: var(--radius-lg);
    border-bottom-left-radius: 6px;
    padding: 12px 16px;
    display: flex;
    align-items: center;
    gap: 10px;
}

.typing-text {
    font-size: 0.82rem;
    color: var(--text-muted);
    font-style: italic;
}

.typing-dots {
    display: flex;
    gap: 4px;
    align-items: center;
}

.typing-dot {
    width: 6px;
    height: 6px;
    background: var(--cyan);
    border-radius: 50%;
    animation: typing-bounce 1.4s infinite ease-in-out;
}
.typing-dot:nth-child(1) { animation-delay: 0s; }
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

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

/* ============================================================
   8. FORMATO DEL TEXTO DE LA IA
============================================================ */
.ai-content p { margin-bottom: 10px; }
.ai-content p:last-child { margin-bottom: 0; }
.ai-content strong { color: var(--cyan); font-weight: 700; }
.ai-content em { color: var(--amber); font-style: normal; }

.ai-content ol, .ai-content ul {
    padding-left: 20px;
    margin: 10px 0;
    display: flex;
    flex-direction: column;
    gap: 6px;
}
.ai-content ol li, .ai-content ul li { color: var(--text); }

.ai-content .step-number {
    display: inline-block;
    background: rgba(0,245,212,0.15);
    color: var(--cyan);
    border-radius: 50%;
    width: 22px; height: 22px;
    font-size: 0.75rem;
    font-weight: 700;
    text-align: center;
    line-height: 22px;
    margin-right: 6px;
    flex-shrink: 0;
}

.ai-content table {
    width: 100%;
    border-collapse: collapse;
    font-size: 0.82rem;
    margin: 10px 0;
    overflow-x: auto;
    display: block;
}
.ai-content th {
    background: var(--bg-secondary);
    color: var(--text-muted);
    padding: 8px 10px;
    text-align: left;
    font-size: 0.75rem;
    border-bottom: 1px solid var(--border-subtle);
}
.ai-content td {
    padding: 8px 10px;
    border-bottom: 1px solid rgba(255,255,255,0.04);
    color: var(--text);
}
.ai-content tr:hover td { background: rgba(255,255,255,0.02); }

.ai-content code {
    font-family: var(--font-mono);
    background: rgba(0,245,212,0.08);
    color: var(--cyan);
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 0.85em;
}

/* ============================================================
   9. CHIPS DE CONSULTA RÁPIDA
============================================================ */
.chips-container {
    background: var(--bg-card);
    border-top: 1px solid var(--border-subtle);
    padding: 10px 0;
}

.chips-scroll {
    display: flex;
    gap: 8px;
    overflow-x: auto;
    padding: 0 12px;
    scrollbar-width: none;
    -webkit-overflow-scrolling: touch;
}
.chips-scroll::-webkit-scrollbar { display: none; }

.chip {
    display: inline-flex;
    align-items: center;
    gap: 5px;
    padding: 7px 14px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 99px;
    color: var(--text);
    font-size: 0.78rem;
    font-weight: 600;
    cursor: pointer;
    white-space: nowrap;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
    flex-shrink: 0;
}
.chip:hover,
.chip:active {
    background: rgba(0,245,212,0.1);
    border-color: var(--cyan);
    color: var(--cyan);
}

/* ============================================================
   10. ÁREA DE INPUT
============================================================ */
.input-area {
    padding: 10px 12px;
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    background: var(--bg-card);
    border-top: 1px solid var(--border-subtle);
}

.input-wrapper {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    background: var(--bg-secondary);
    border: 1.5px solid rgba(255,255,255,0.1);
    border-radius: var(--radius-lg);
    padding: 8px 8px 8px 14px;
    transition: border-color 0.2s, box-shadow 0.2s;
}

.input-wrapper:focus-within {
    border-color: var(--cyan);
    box-shadow: 0 0 0 3px rgba(0,245,212,0.06);
}

.message-input {
    flex: 1;
    background: transparent;
    border: none;
    outline: none;
    color: var(--text);
    font-size: 0.95rem;
    font-family: var(--font-sans);
    resize: none;
    max-height: 120px;
    line-height: 1.5;
    padding: 0;
}

.message-input::placeholder { color: var(--text-muted); opacity: 0.7; }

.send-btn {
    width: 40px;
    height: 40px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--cyan), var(--green));
    border: none;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
    transition: all 0.2s;
    -webkit-tap-highlight-color: transparent;
}
.send-btn:hover { box-shadow: 0 4px 15px rgba(0,245,212,0.4); transform: scale(1.05); }
.send-btn:active { transform: scale(0.95); }
.send-btn:disabled { opacity: 0.4; cursor: not-allowed; transform: none; }

.send-btn svg {
    width: 18px;
    height: 18px;
    color: #0b132b;
    stroke: #0b132b;
}

.input-hint {
    font-size: 0.65rem;
    color: var(--text-faint);
    text-align: center;
    margin-top: 6px;
}

/* ============================================================
   11. MODAL: CALCULADORA ÓPTICA
============================================================ */
.modal-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,0.7);
    z-index: 1000;
    align-items: flex-end;
    justify-content: center;
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
}

.modal-overlay.open {
    display: flex;
}

@media (min-width: 600px) {
    .modal-overlay { align-items: center; }
    .modal-content { border-radius: var(--radius-lg); max-width: 520px; }
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    width: 100%;
    max-height: 90dvh;
    overflow-y: auto;
    box-shadow: var(--shadow-deep);
    animation: slideUp 0.3s ease-out;
}

@keyframes slideUp {
    from { transform: translateY(100%); opacity: 0; }
    to   { transform: translateY(0); opacity: 1; }
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 18px 20px;
    border-bottom: 1px solid var(--border-subtle);
    position: sticky;
    top: 0;
    background: var(--bg-card);
    z-index: 1;
}

.modal-header h3 {
    font-size: 1rem;
    font-weight: 700;
    color: var(--cyan);
}

.modal-close {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: rgba(255,255,255,0.06);
    border: none;
    color: var(--text-muted);
    font-size: 1rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
}
.modal-close:hover { background: rgba(255,255,255,0.12); color: var(--text); }

.modal-body {
    padding: 20px;
}

.modal-desc {
    font-size: 0.85rem;
    color: var(--text-muted);
    margin-bottom: 18px;
}

.calc-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 12px;
    margin-bottom: 18px;
}

@media (max-width: 380px) {
    .calc-grid { grid-template-columns: 1fr; }
}

.calc-field {
    display: flex;
    flex-direction: column;
    gap: 4px;
}

.calc-field label {
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
}

.calc-input {
    width: 100%;
    padding: 10px 12px;
    background: var(--bg-secondary);
    border: 1.5px solid rgba(255,255,255,0.1);
    border-radius: var(--radius-sm);
    color: var(--text);
    font-size: 1rem;
    font-weight: 700;
    outline: none;
    transition: border-color 0.2s;
    text-align: center;
}
.calc-input:focus { border-color: var(--cyan); }
.calc-hint { font-size: 0.65rem; color: var(--text-faint); }

.btn-calc {
    width: 100%;
    padding: 14px;
    background: linear-gradient(135deg, var(--cyan), var(--green));
    color: #0b132b;
    border: none;
    border-radius: var(--radius-md);
    font-size: 1rem;
    font-weight: 800;
    letter-spacing: 0.5px;
    cursor: pointer;
    transition: all 0.2s;
    margin-bottom: 18px;
}
.btn-calc:hover { box-shadow: 0 6px 20px rgba(0,245,212,0.3); }
.btn-calc:active { transform: scale(0.98); }

.calc-result {
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    padding: 16px;
    text-align: center;
    margin-bottom: 16px;
}

.result-value {
    font-size: 2.5rem;
    font-weight: 900;
    line-height: 1;
    margin-bottom: 8px;
}
.result-status {
    font-size: 1.1rem;
    font-weight: 700;
    margin-bottom: 8px;
}
.result-detail {
    font-size: 0.82rem;
    color: var(--text-muted);
    line-height: 1.5;
}

.calc-reference {
    background: rgba(0,245,212,0.03);
    border: 1px solid rgba(0,245,212,0.08);
    border-radius: var(--radius-sm);
    padding: 12px;
}
.calc-reference p {
    font-size: 0.82rem;
    color: var(--text-muted);
    margin-bottom: 10px;
    font-weight: 600;
}
.ref-grid {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
}
.ref-grid span {
    font-size: 0.75rem;
    padding: 5px 8px;
    border-radius: 6px;
    font-weight: 600;
}
.ref-good { background: rgba(16,185,129,0.12); color: var(--green); }
.ref-warn { background: rgba(245,158,11,0.12); color: var(--amber); }
.ref-bad  { background: rgba(239,68,68,0.12);  color: #f87171; }
.ref-sat  { background: rgba(139,92,246,0.12); color: #a78bfa; }

/* ============================================================
   12. UTILIDADES GENERALES
============================================================ */

/* Toast de notificación */
.toast {
    position: fixed;
    bottom: 80px;
    left: 50%;
    transform: translateX(-50%) translateY(20px);
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: 99px;
    padding: 8px 18px;
    font-size: 0.82rem;
    color: var(--cyan);
    font-weight: 600;
    box-shadow: var(--shadow-card);
    opacity: 0;
    transition: all 0.3s;
    z-index: 9999;
    white-space: nowrap;
    pointer-events: none;
}
.toast.show {
    opacity: 1;
    transform: translateX(-50%) translateY(0);
}

/* Skeleton loader */
.skeleton {
    background: linear-gradient(90deg, var(--bg-secondary) 25%, rgba(255,255,255,0.04) 50%, var(--bg-secondary) 75%);
    background-size: 200% 100%;
    animation: skeleton-loading 1.5s infinite;
    border-radius: 6px;
}
@keyframes skeleton-loading {
    0% { background-position: 200% 0; }
    100% { background-position: -200% 0; }
}

/* Responsive: ocultar en móvil */
@media (max-width: 480px) {
    .hide-mobile { display: none !important; }
}

/* Error state */
.message-error .message-bubble {
    background: rgba(239,68,68,0.08);
    border-color: rgba(239,68,68,0.2);
}
