:root {
    --color-primary: #04509f;
    --color-secondary: #0057a8;
    --color-user-background: #ebf2fa;
    --color-text: #3e3c44;
    --color-border: #e0e0e0;
    --color-border-blue: #dfeaf7;
    --color-surface: #ffffff;
    --color-surface-blue: #f8fbff;
    --color-surface-light: #f4f8ff;
}

html,
body {
    width: 100%;
    height: 100%;
    margin: 0;
    overflow: visible;
    background: transparent;
    color: var(--color-text);
    font-family: inherit;
}

#chat-toggle {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: 64px;
    height: 64px;
    border: 2px solid var(--color-primary);
    border-radius: 50%;
    background: #ffffff;
    color: var(--color-surface);
    cursor: pointer;
    font: inherit;
    font-weight: 700;
    box-shadow: 0 6px 18px rgba(4, 80, 159, 0.24);
    transition: background-color 160ms ease, transform 160ms ease;
}

#chat-toggle:hover,
#chat-toggle:focus-visible {
    background: #ffffff;
    border-color: var(--color-secondary);
    transform: translateY(-1px);
}

#chat-toggle img {
    display: block;
    width: 100%;
    height: 100%;
    object-fit: contain;
    border-radius: 50%;
}

#chat-window {
    position: fixed;
    right: 20px;
    bottom: 20px;
    width: min(420px, calc(100vw - 40px));
    height: min(650px, calc(100vh - 40px));
    overflow: hidden;
    border: 1px solid var(--color-border-blue);
    border-radius: 18px;
    background: linear-gradient(135deg, var(--color-surface) 0%, var(--color-surface-blue) 100%);
    box-shadow: 0 8px 28px rgba(4, 80, 159, 0.14);
    box-sizing: border-box;
}

#chat-window::before {
    position: absolute;
    inset: 0;
    z-index: 0;
    pointer-events: none;
    content: "";
    background: radial-gradient(circle at top left, rgba(223, 234, 247, 0.42), transparent 42%);
}

#webchat,
.chat-header {
    position: relative;
    z-index: 1;
}

#webchat {
    width: 100%;
    height: calc(100% - 52px);
    background: transparent;
}

/* Contenido interno de Web Chat: nombres, burbujas y horas. */
#webchat .webchat__transcript {
    background: transparent;
}

#webchat,
#webchat * {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

#webchat .webchat__bubble {
    color: var(--color-text);
    font-size: 14px;
    line-height: 1.45;
    box-shadow: none;
}

#webchat .webchat__bubble::before {
    display: block;
    margin: 0 0 4px 4px;
    color: var(--color-primary);
    content: "Alan";
    font-weight: 700;
}

#webchat .webchat__bubble--from-user {
    margin-left: auto;
    text-align: right;
}

#webchat .webchat__bubble--from-user::before {
    margin: 0 4px 4px 0;
    content: "Tú:";
}

#webchat .webchat__bubble__content {
    box-shadow: none;
}

#webchat .webchat__bubble__nub {
    box-shadow: 0 1px 3px rgba(4, 80, 159, 0.08);
}

#webchat .webchat__bubble--from-user .webchat__bubble__content {
    text-align: left;
}

#webchat .webchat__avatar--from-user,
#webchat .webchat__bubble--from-user + .webchat__avatar {
    display: none;
}

#webchat .webchat__timestamp {
    color: #9a98a2;
}

#webchat .webchat__send-box-text-box,
#webchat textarea,
#webchat input {
    color: #3e3c44;
}

#webchat textarea::placeholder,
#webchat input::placeholder {
    color: #9a98a2;
    opacity: 1;
}

#webchat button:hover,
#webchat button:focus-visible {
    border-radius: 6px;
    background: #ebf2fa;
    color: #04509f;
}

/* Selectores confirmados por el DOM de Web Chat 4.17.0. */
#webchat .webchat__basic-transcript {
    display: flex;
    flex-direction: column;
    background: transparent;
}

#webchat .webchat__basic-transcript__scrollable {
    box-sizing: border-box;
    width: 100%;
    padding: 18px 24px 12px;
    background: transparent;
}

#webchat .webchat__stacked-layout {
    margin: 8px 0;
}

#webchat .webchat__stacked-layout__avatar-gutter {
    width: 55px !important;
}

#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__stacked-layout__avatar-gutter {
    align-items: flex-start !important;
    padding-top: 0 !important;
}

#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__stacked-layout__status {
    margin-left: 55px !important;
}

#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__defaultAvatar {
    margin-top: 0 !important;
    border-radius: 50% !important;
    box-shadow: 0 2px 8px rgba(62, 60, 68, 0.18);
}

/* Web Chat deja vacío el gutter de algunos mensajes consecutivos del bot.
   Pintamos el avatar en cada gutter para mantenerlo visible en todos ellos. */
#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__stacked-layout__avatar-gutter {
    position: relative;
}

#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__stacked-layout__avatar-gutter::before {
    display: block;
    width: 44px;
    height: 44px;
    content: "";
    background: #ffffff url('/static/Alan.png') center / contain no-repeat;
    border: 1px solid rgba(4, 80, 159, 0.08);
    border-radius: 50%;
    box-shadow: 0 2px 8px rgba(62, 60, 68, 0.18);
}

#webchat .webchat__stacked-layout:not(.webchat__stacked-layout--from-user)
    .webchat__stacked-layout__avatar-gutter .webchat__defaultAvatar {
    display: none !important;
}

#webchat .webchat__stacked-layout__content {
    margin-left: 6px;
}

#webchat .webchat__stacked-layout__content {
    min-width: 0;
    max-width: 100%;
    overflow: hidden;
    overflow-wrap: anywhere;
    word-break: break-word;
    box-sizing: border-box;
}

#webchat .webchat__stacked-layout,
#webchat .webchat__stacked-layout__main,
#webchat .webchat__bubble,
#webchat .webchat__bubble__content {
    min-width: 0;
    max-width: 100%;
    box-sizing: border-box;
    overflow-wrap: anywhere;
    word-break: break-word;
}

#webchat .webchat__stacked-layout__content::before {
    display: block;
    margin-bottom: 6px;
    color: var(--color-primary);
    content: attr(data-label);
    font-size: 12px;
    font-weight: 700;
    line-height: 16px;
}

#webchat .webchat__bubble::before,
#webchat .webchat__bubble--from-user::before {
    display: none;
    content: none;
}

#webchat .webchat__stacked-layout__status {
    margin-top: 6px;
    color: #9a98a2 !important;
    font-size: 12px;
    line-height: 15px;
}

#webchat .webchat__stacked-layout--from-user .webchat__stacked-layout__main {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    width: fit-content;
    max-width: calc(100% - 60px);
    margin-left: auto;
}

#webchat .webchat__stacked-layout--from-user .webchat__stacked-layout__content {
    text-align: right;
}

#webchat .webchat__stacked-layout--from-user .webchat__stacked-layout__content > * {
    text-align: left;
}

#webchat .webchat__stacked-layout--from-user .webchat__bubble__content {
    border-radius: 12px !important;
}

#webchat .webchat__stacked-layout--from-user .webchat__stacked-layout__status {
    width: fit-content;
    margin-left: auto;
    text-align: right;
    white-space: nowrap;
}

#webchat .webchat__defaultAvatar {
    overflow: hidden;
    background: #ffffff !important;
    border: 1px solid rgba(4, 80, 159, 0.08);
    border-radius: 50% !important;
    box-shadow: 0 2px 8px rgba(62, 60, 68, 0.18);
}

#webchat .webchat__stacked-layout--from-user .webchat__defaultAvatar {
    display: none;
}

#webchat .webchat__send-box {
    padding: 16px 20px 20px;
    background: transparent !important;
}

#webchat .webchat__send-box__main {
    box-sizing: border-box;
    min-height: 56px;
    align-items: center;
    padding: 7px 12px 7px 16px;
    border: 1px solid #04509f !important;
    border-radius: 14px;
    background: #ffffff !important;
    box-shadow: 0 10px 24px rgba(0, 52, 106, 0.08);
}

#webchat .webchat__send-box-text-box__input::placeholder {
    color: #9a98a2 !important;
    opacity: 1;
}

#webchat .webchat__icon-button {
    border-radius: 10px !important;
}

#webchat .webchat__send-button svg,
#webchat .webchat__upload-button svg {
    fill: #3e3c44 !important;
}

#webchat .webchat__send-button:hover svg,
#webchat .webchat__upload-button:hover svg {
    fill: #04509f !important;
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 52px;
    padding: 0 16px;
    box-sizing: border-box;
    background: linear-gradient(90deg, var(--color-surface-light), #edf4ff);
    border-bottom: 1px solid var(--color-border-blue);
    color: var(--color-primary);
    font-family: Arial, sans-serif;
    font-weight: 700;
}

.chat-header button {
    border: 0;
    background: transparent;
    color: var(--color-text);
    cursor: pointer;
    font: inherit;
    font-size: 28px;
    line-height: 1;
    transition: color 160ms ease, background-color 160ms ease;
}

.chat-header button:hover,
.chat-header button:focus-visible {
    border-radius: 6px;
    background: var(--color-border-blue);
    color: var(--color-primary);
}

#webchat > * {
    height: 100%;
    border-radius: inherit;
    background: transparent;
}

.chat-error {
    display: grid;
    height: 100%;
    margin: 0;
    place-items: center;
    color: #b42318;
    text-align: center;
}

@media (max-width: 480px) {
    #chat-window {
        right: 10px;
        bottom: 10px;
        width: calc(100vw - 20px);
        height: calc(100vh - 20px);
    }

    #chat-toggle {
        right: 16px;
        bottom: 16px;
    }
}
