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

:root {
    --bg-alley: #0c0c14;
    --bg-wall: #111119;
    --text-color: #fff;
    --text-muted: rgba(180, 180, 195, 0.6);
    --sign-bg: rgba(8, 8, 14, 0.85);
    --neon-hue: 180deg;
    --app-height: 100vh;
    --transition: all 0.3s cubic-bezier(0.16, 1, 0.3, 1);
}

@property --rgb-angle {
    syntax: "<angle>";
    initial-value: 0deg;
    inherits: false;
}

html {
    height: 100%;
    overflow: hidden;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    color: var(--text-color);
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    background: var(--bg-alley);
    height: 100%;
    overflow: hidden;
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
}

/* ===============================
   DARK ALLEY BACKGROUND
   =============================== */
body::before {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 0;
    background:
        radial-gradient(ellipse 50% 40% at 50% 30%,
            rgba(30, 30, 50, 0.3) 0%,
            transparent 70%
        ),
        radial-gradient(ellipse 80% 80% at 50% 50%,
            transparent 0%,
            rgba(0, 0, 0, 0.5) 100%
        ),
        linear-gradient(180deg,
            #0e0e18 0%,
            #12121e 40%,
            #0a0a12 100%
        );
}

/* Grain / grit texture */
body::after {
    content: "";
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 9999;
    opacity: 0.035;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 200 200' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='g'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='1.1' numOctaves='3' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23g)' opacity='0.8'/%3E%3C/svg%3E");
    background-repeat: repeat;
    background-size: 200px 200px;
    mix-blend-mode: overlay;
}

/* ===============================
   APP LAYOUT
   =============================== */
#genesi-app {
    position: relative;
    z-index: 1;
    height: var(--app-height);
    display: flex;
    flex-direction: column;
    max-width: 800px;
    margin: 0 auto;
    overflow: hidden;
    background: transparent;
}

/* ===============================
   PRESENCE HEADER — NEON SIGN BOARD
   =============================== */
#presence {
    flex-shrink: 0;
    text-align: center;
    padding: 1.4rem 1.2rem 1rem;
    transition: var(--transition);
}

#presence p {
    font-family: 'Cormorant Garamond', serif;
    font-size: 1.25rem;
    font-weight: 500;
    font-style: italic;
    letter-spacing: 0.03em;
    max-width: 26ch;
    margin: 0 auto;
    line-height: 1.5;
    color: #00e5ff;
    padding: 1rem 1.6rem;
    background: var(--sign-bg);
    border-radius: 0.3rem;
    /* Double frame: outer metallic + inner neon */
    border: 3px solid #2a2a35;
    outline: 2px solid rgba(0, 229, 255, 0.4);
    outline-offset: 3px;
    text-shadow:
        0 0 4px #00e5ff,
        0 0 12px rgba(0, 229, 255, 0.7),
        0 0 28px rgba(0, 229, 255, 0.35),
        0 0 50px rgba(0, 229, 255, 0.15);
    box-shadow:
        inset 0 0 20px rgba(0, 229, 255, 0.06),
        0 0 15px rgba(0, 229, 255, 0.12),
        0 0 40px rgba(0, 229, 255, 0.06);
    animation: signFlicker 6s ease-in-out infinite;
    will-change: text-shadow, opacity;
}

@keyframes signFlicker {
    0%, 100% { opacity: 1; }
    18%      { opacity: 1; }
    19%      { opacity: 0.55; }
    20%      { opacity: 1; }
    53%      { opacity: 1; }
    54%      { opacity: 0.4; }
    55%      { opacity: 0.85; }
    56%      { opacity: 1; }
    78%      { opacity: 1; }
    79%      { opacity: 0.6; }
    80%      { opacity: 1; }
}

@keyframes neonFlicker {
    0%, 19%, 21%, 23%, 25%, 54%, 56%, 100% { opacity: 1; }
    20%, 24%, 55% { opacity: 0.3; }
}

/* ===============================
   DIALOGUE — SCROLL AREA
   =============================== */
#dialogue {
    flex: 1 1 0;
    min-height: 0;
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    overflow-x: hidden;
    padding: 0.6rem 0;
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
}

#dialogue::-webkit-scrollbar {
    width: 3px;
}
#dialogue::-webkit-scrollbar-track {
    background: transparent;
}
#dialogue::-webkit-scrollbar-thumb {
    background: rgba(255, 255, 255, 0.07);
    border-radius: 2px;
}

/* ===============================
   MESSAGES — NEON LIGHT BOXES
   =============================== */
.message {
    --neon-hue: 180deg;
    font-size: 1.05rem;
    line-height: 1.55;
    width: fit-content;
    max-width: 75%;
    padding: 0.7rem 1rem;
    margin: 0.35rem 0.8rem;
    border-radius: 0.4rem;
    position: relative;
    opacity: 0;
    animation: msgAppear 0.3s ease-out forwards;
    word-wrap: break-word;
    overflow-wrap: break-word;
    white-space: pre-wrap;
    will-change: transform, opacity;

    /* Dark sign background */
    background: var(--sign-bg);
    color: #fff;

    /* Thick neon border — the "tube" */
    border: 2px solid hsl(var(--neon-hue), 100%, 60%);

    /* Neon glow — outer + inner */
    box-shadow:
        0 0 4px hsl(var(--neon-hue), 100%, 60%, 0.5),
        0 0 12px hsl(var(--neon-hue), 100%, 55%, 0.3),
        0 0 28px hsl(var(--neon-hue), 100%, 50%, 0.12),
        inset 0 0 8px hsl(var(--neon-hue), 100%, 60%, 0.06);
}

.message.genesi {
    align-self: flex-start;
}

.message.user {
    align-self: flex-end;
}

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

/* ===============================
   STATUS — ENERGY BAR (THINKING)
   =============================== */
#status {
    flex-shrink: 0;
    font-size: 0.8rem;
    text-align: center;
    padding: 0.4rem 0;
    color: var(--text-muted);
    opacity: 0;
    transition: opacity 0.3s ease;
    position: relative;
}

#status.visible {
    opacity: 1;
}

#status.visible::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 140px;
    height: 2px;
    border-radius: 1px;
    background: linear-gradient(90deg,
        transparent,
        hsl(300, 100%, 60%),
        hsl(180, 100%, 55%),
        hsl(60, 100%, 55%),
        hsl(300, 100%, 60%),
        transparent
    );
    background-size: 300% 100%;
    animation: energyBar 2s ease-in-out infinite;
}

@keyframes energyBar {
    0%   { background-position: 300% 0; opacity: 0.5; }
    50%  { opacity: 1; }
    100% { background-position: -300% 0; opacity: 0.5; }
}

/* ===============================
   INPUT AREA — NEON CONSOLE
   =============================== */
#chat-form {
    flex-shrink: 0;
    padding: 0.5rem 0.6rem;
    padding-bottom: calc(0.5rem + env(safe-area-inset-bottom, 0px));
}

#input-container {
    background: var(--sign-bg);
    border-radius: 0.6rem;
    padding: 0.5rem 0.8rem;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    border: 2px solid #2a2a38;
    transition: border-color 0.3s ease, box-shadow 0.3s ease;
    position: relative;
}

/* RGB rotating neon border */
#input-container::before {
    content: "";
    position: absolute;
    inset: -2px;
    border-radius: inherit;
    padding: 2px;
    background: conic-gradient(
        from var(--rgb-angle, 0deg),
        hsl(0, 100%, 60%, 0.3),
        hsl(60, 100%, 55%, 0.3),
        hsl(120, 100%, 50%, 0.3),
        hsl(180, 100%, 55%, 0.3),
        hsl(240, 100%, 60%, 0.3),
        hsl(300, 100%, 55%, 0.3),
        hsl(360, 100%, 60%, 0.3)
    );
    -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
    -webkit-mask-composite: xor;
    mask-composite: exclude;
    pointer-events: none;
    animation: rgbRotate 6s linear infinite;
}

@keyframes rgbRotate {
    to { --rgb-angle: 360deg; }
}

#input-container:focus-within {
    border-color: transparent;
    box-shadow:
        0 0 8px rgba(0, 229, 255, 0.15),
        0 0 25px rgba(0, 229, 255, 0.06);
}

#input-container:focus-within::before {
    background: conic-gradient(
        from var(--rgb-angle, 0deg),
        hsl(0, 100%, 60%, 0.6),
        hsl(60, 100%, 55%, 0.6),
        hsl(120, 100%, 50%, 0.6),
        hsl(180, 100%, 55%, 0.6),
        hsl(240, 100%, 60%, 0.6),
        hsl(300, 100%, 55%, 0.6),
        hsl(360, 100%, 60%, 0.6)
    );
}

/* Send pulse */
@keyframes sendPulse {
    0%   { box-shadow: 0 0 0 0 rgba(0, 229, 255, 0.35); }
    70%  { box-shadow: 0 0 0 12px rgba(0, 229, 255, 0); }
    100% { box-shadow: 0 0 0 0 rgba(0, 229, 255, 0); }
}

#input-container.pulse {
    animation: sendPulse 0.5s ease-out;
}

/* ===============================
   PLUS BUTTON
   =============================== */
#plus-button {
    order: 1;
    flex-shrink: 0;
    width: 2.2rem;
    height: 2.2rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: var(--transition);
}

#plus-button svg {
    width: 15px;
    height: 15px;
    stroke: rgba(255, 255, 255, 0.55);
    stroke-width: 2;
    fill: none;
}

#plus-button:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(255, 255, 255, 0.2);
}

#plus-button:active {
    transform: scale(0.94);
}

/* ===============================
   TEXT INPUT
   =============================== */
#text-input {
    order: 2;
    flex: 1 1 auto;
    min-width: 0;
    background: transparent;
    border: none;
    color: #fff;
    font-size: 1rem;
    padding: 0.6rem 0;
    outline: none;
    caret-color: #00e5ff;
    -webkit-appearance: none;
    appearance: none;
}

#text-input::placeholder {
    color: var(--text-muted);
    opacity: 0.6;
}

#text-input:focus::placeholder {
    opacity: 0.3;
}

/* ===============================
   MIC BUTTON
   =============================== */
#mic-button {
    order: 3;
    flex-shrink: 0;
    width: 2.2rem;
    height: 2.2rem;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.1);
    transition: background 0.25s ease, border-color 0.25s ease, transform 0.15s ease, box-shadow 0.25s ease;
    animation: none;
}

#mic-button svg {
    width: 15px;
    height: 15px;
    stroke: rgba(255, 255, 255, 0.7);
    stroke-width: 2;
    fill: none;
}

#mic-button:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(255, 255, 255, 0.2);
}

#mic-button:active {
    transform: scale(0.94);
}

#mic-button.recording {
    animation: mic-pulse-neon 1.4s infinite ease-in-out;
    background: rgba(255, 40, 60, 0.1);
    border-color: rgba(255, 40, 60, 0.6);
}

@keyframes mic-pulse-neon {
    0%   {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(255, 40, 60, 0.5), 0 0 8px rgba(255, 40, 60, 0.25);
    }
    50%  {
        transform: scale(1.06);
        box-shadow: 0 0 0 10px rgba(255, 40, 60, 0), 0 0 16px rgba(255, 40, 60, 0.1);
    }
    100% {
        transform: scale(1);
        box-shadow: 0 0 0 0 rgba(255, 40, 60, 0), 0 0 8px rgba(255, 40, 60, 0.25);
    }
}

/* ===============================
   SEND BUTTON
   =============================== */
#send-button {
    order: 4;
    flex-shrink: 0;
    width: 2.2rem;
    height: 2.2rem;
    background: rgba(255, 255, 255, 0.03);
    border: 1px solid rgba(255, 255, 255, 0.1);
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.25s ease;
}

#send-button:disabled {
    pointer-events: none;
    opacity: 0.2;
}

#send-button svg {
    width: 16px;
    height: 16px;
    stroke: rgba(255, 255, 255, 0.7);
    stroke-width: 2;
    fill: none;
}

#send-button:hover {
    background: rgba(255, 255, 255, 0.07);
    border-color: rgba(255, 255, 255, 0.2);
}

#send-button:active {
    transform: scale(0.92);
}

/* ===============================
   ANIMATIONS
   =============================== */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(6px); }
    to   { opacity: 1; transform: translateY(0); }
}

/* ===============================
   AUTH GATE — welcome screen (non-logged)
   =============================== */
#auth-gate {
    display: flex;
    flex: 1 1 0;
    align-items: center;
    justify-content: center;
    text-align: center;
    padding: 2rem 1.2rem;
}

#auth-gate-inner {
    max-width: 340px;
}

#auth-title {
    font-family: 'Cormorant Garamond', serif;
    font-size: 1.8rem;
    font-weight: 500;
    color: #00e5ff;
    margin-bottom: 0.6rem;
    text-shadow:
        0 0 4px #00e5ff,
        0 0 12px rgba(0, 229, 255, 0.5),
        0 0 28px rgba(0, 229, 255, 0.2);
}

#auth-subtitle {
    font-size: 0.95rem;
    color: var(--text-muted);
    line-height: 1.6;
    margin-bottom: 1.8rem;
}

#auth-buttons {
    display: flex;
    flex-direction: column;
    gap: 0.7rem;
    align-items: center;
}

.auth-btn {
    display: inline-block;
    width: 220px;
    padding: 0.75rem 1.2rem;
    border-radius: 0.5rem;
    font-size: 0.95rem;
    font-weight: 600;
    text-decoration: none;
    text-align: center;
    transition: opacity 0.2s, transform 0.15s;
    cursor: pointer;
}

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

.auth-btn--primary {
    background: #00e5ff;
    color: #0c0c14;
    border: none;
    box-shadow: 0 0 12px rgba(0, 229, 255, 0.25);
}

.auth-btn--primary:hover {
    opacity: 0.9;
}

.auth-btn--secondary {
    background: transparent;
    color: #00e5ff;
    border: 1px solid rgba(0, 229, 255, 0.35);
}

.auth-btn--secondary:hover {
    background: rgba(0, 229, 255, 0.06);
}

/* ===============================
   USER BAR — logged-in header
   =============================== */
#user-bar {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.5rem 1rem;
    font-size: 0.82rem;
    color: var(--text-muted);
    border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}

#user-greeting {
    opacity: 0.7;
}

#user-bar-actions {
    display: flex;
    align-items: center;
    gap: 0.8rem;
}

#admin-link {
    color: #00e5ff;
    text-decoration: none;
    font-size: 0.78rem;
    opacity: 0.7;
    transition: opacity 0.2s;
}

#admin-link:hover {
    opacity: 1;
}

#logout-btn {
    background: none;
    border: 1px solid rgba(255, 255, 255, 0.12);
    color: rgba(255, 255, 255, 0.55);
    padding: 0.3rem 0.7rem;
    border-radius: 0.35rem;
    font-size: 0.75rem;
    cursor: pointer;
    transition: background 0.2s, color 0.2s;
}

#logout-btn:hover {
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.8);
}

/* ===============================
   MOBILE
   =============================== */
@media (max-width: 600px) {
    #presence {
        padding: 0.8rem 0.6rem 0.6rem;
    }

    #presence p {
        font-size: 1.05rem;
        padding: 0.8rem 1rem;
    }

    .message {
        max-width: 85%;
        font-size: 0.95rem;
        margin: 0.25rem 0.5rem;
        padding: 0.6rem 0.9rem;
    }

    #chat-form {
        padding: 0.3rem 0.4rem;
        padding-bottom: calc(0.3rem + env(safe-area-inset-bottom, 0px));
    }

    #input-container {
        padding: 0.4rem 0.6rem;
        border-radius: 0.5rem;
    }
}

/* ===============================
   FILE UPLOAD PREVIEW (inline chat)
   =============================== */
.upload-preview {
    margin-bottom: 0.6rem;
    border-radius: 0.5rem;
    overflow: hidden;
}

.preview-img {
    max-width: 100%;
    max-height: 300px;
    border-radius: 0.5rem;
    object-fit: contain;
    display: block;
}

.preview-pdf {
    width: 100%;
    height: 280px;
    border: none;
    border-radius: 0.5rem;
    background: rgba(255,255,255,0.05);
}

.preview-pdf-link {
    display: inline-block;
    margin-top: 0.4rem;
    color: hsl(var(--neon-hue, 180deg), 80%, 65%);
    text-decoration: underline;
    font-size: 0.85rem;
}

@media (max-width: 600px) {
    .preview-img {
        max-height: 200px;
    }
    .preview-pdf {
        height: 200px;
    }
}

/* ===============================
   FILE BUBBLE — "Genesi sta guardando"
   Mobile-first, vetro satinato, neon
   =============================== */
.file-bubble {
    flex-shrink: 0;
    display: flex;
    align-items: center;
    gap: 0.55rem;
    margin: 0 0.8rem;
    padding: 0.4rem 0.6rem;
    border-radius: 0.5rem;
    background: rgba(14, 14, 24, 0.72);
    -webkit-backdrop-filter: blur(14px) saturate(1.3);
    backdrop-filter: blur(14px) saturate(1.3);
    border: 1px solid rgba(0, 229, 255, 0.25);
    box-shadow:
        0 0 6px rgba(0, 229, 255, 0.12),
        0 0 18px rgba(0, 229, 255, 0.06),
        inset 0 0 10px rgba(0, 229, 255, 0.03);
    opacity: 0;
    transform: translateY(-6px);
    animation: bubbleIn 0.35s cubic-bezier(0.16, 1, 0.3, 1) forwards;
    will-change: transform, opacity;
    overflow: hidden;
    max-height: 56px;
    transition: max-height 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    cursor: pointer;
    position: relative;
}

.file-bubble.expanded {
    max-height: 320px;
    align-items: flex-start;
}

@keyframes bubbleIn {
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

@keyframes bubbleOut {
    from {
        opacity: 1;
        transform: translateY(0);
    }
    to {
        opacity: 0;
        transform: translateY(-6px);
    }
}

/* Thumbnail */
.file-bubble__thumb {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 0.3rem;
    object-fit: cover;
    border: 1px solid rgba(0, 229, 255, 0.15);
}

/* File icon fallback */
.file-bubble__icon {
    flex-shrink: 0;
    width: 40px;
    height: 40px;
    border-radius: 0.3rem;
    background: rgba(0, 229, 255, 0.06);
    border: 1px solid rgba(0, 229, 255, 0.15);
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.1rem;
}

/* Info text */
.file-bubble__info {
    flex: 1;
    min-width: 0;
    overflow: hidden;
}

.file-bubble__name {
    font-size: 0.78rem;
    font-weight: 500;
    color: rgba(255, 255, 255, 0.85);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.file-bubble__status {
    font-size: 0.65rem;
    color: rgba(0, 229, 255, 0.7);
    font-style: italic;
    letter-spacing: 0.02em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Close button */
.file-bubble__close {
    flex-shrink: 0;
    width: 22px;
    height: 22px;
    border-radius: 50%;
    border: none;
    background: rgba(255, 255, 255, 0.06);
    color: rgba(255, 255, 255, 0.45);
    font-size: 0.75rem;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s ease, color 0.2s ease;
    line-height: 1;
    padding: 0;
}

.file-bubble__close:hover {
    background: rgba(255, 255, 255, 0.12);
    color: rgba(255, 255, 255, 0.8);
}

/* Expanded preview */
.file-bubble__preview {
    display: none;
    width: 100%;
    margin-top: 0.4rem;
}

.file-bubble.expanded .file-bubble__preview {
    display: block;
}

.file-bubble__preview img {
    max-width: 100%;
    max-height: 220px;
    border-radius: 0.3rem;
    object-fit: contain;
    display: block;
}

.file-bubble__preview object {
    width: 100%;
    height: 200px;
    border: none;
    border-radius: 0.3rem;
}

/* ---- MOBILE: lightweight, no heavy effects ---- */
@media (max-width: 600px) {
    .file-bubble {
        margin: 0 0.5rem;
        padding: 0.35rem 0.5rem;
        gap: 0.45rem;
        max-height: 48px;
        -webkit-backdrop-filter: blur(6px);
        backdrop-filter: blur(6px);
        box-shadow: 0 0 4px rgba(0, 229, 255, 0.08);
        overflow: hidden;
    }

    .file-bubble.expanded {
        max-height: 240px;
    }

    .file-bubble__thumb,
    .file-bubble__icon {
        width: 34px;
        height: 34px;
    }

    .file-bubble__name {
        font-size: 0.7rem;
    }

    .file-bubble__status {
        font-size: 0.6rem;
    }

    .file-bubble__preview img {
        max-height: 150px;
    }

    .file-bubble__preview object {
        display: none;
    }
}

/* ---- Reduced motion ---- */
@media (prefers-reduced-motion: reduce) {
    .file-bubble {
        animation: none;
        opacity: 1;
        transform: none;
    }
}

/* ---- DESKTOP ---- */
@media (min-width: 601px) {
    .file-bubble {
        max-width: 380px;
    }
}
