/* chat/chat.css */

:root {
    --chat-bg: var(--bg-secondary); /* Note: Consider replacing with --bg-secondary if they match */
}

/* Containeren - strammet opp */
.chat-container {
    display: flex;
    flex-direction: column; /* Viktig: Header øverst, body i midten, input nederst */
    height: 500px;
    width: 350px;
    position: fixed;
    bottom: 0;
    right: 10px;    
    background: var(--bg); /* Refactored to variable */
    box-shadow: var(--shadow-xl); /* Refactored to variable */
    display: flex;
    z-index: 10001;
    border-radius: 12px 12px 0 0; /* Kun avrundet på topp */
    overflow: hidden;
    transition: transform 0.3s cubic-bezier(0.4, 0, 0.2, 1);
}

.chat-container.closed {
    transform: translateY(100%);
}

/* Header - Mye penere og mørk */
.chat-header {
    background: var(--brand);
    color: var(--white); /* Refactored to variable */
    padding: 10px 14px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 50px; /* Fast høyde for stabilitet */
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 4px; /* Mellomrom mellom prikkene og minus-tegnet */
}

#chat-minimize {
    background: var(--white-t-10); /* Refactored to variable */
    border: none;
    color: var(--white); /* Refactored to variable */
    width: 28px;
    height: 28px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
}

#chat-minimize:hover {
    background: var(--white-t-20); /* Refactored to variable */
}

/* Meldingsområdet */
.chat-body {
    flex: 1;
    overflow-y: auto;
    padding: 15px;
    background: var(--chat-bg);
    display: flex;
    flex-direction: column;
}

/* Input-området - Ligger nå helt i bunn */
.chat-input-area {
    flex-shrink: 0; /* Hindrer feltet i å bli klemt sammen */
    background: var(--bg); /* Refactored to variable */
    padding: 12px;
    border-top: 1px solid var(--border); /* Refactored to variable */
    display: flex;
    gap: 8px;
    position: sticky; /* Sikrer at den holder seg der */
    bottom: 0px;
}

#chat-input {
    flex: 1;
    padding: 10px 14px;
    border: 1px solid var(--border); /* Refactored to variable */
    border-radius: 20px; /* Mer moderne "pille"-form */
    outline: none;    
    background: var(--card-bg); /* Refactored to variable */
    transition: border-color 0.2s;
}

#chat-input:focus {
    border-color: var(--action);
}

#chat-send {
    background: var(--action);
    color: var(--white); /* Refactored to variable */
    border: none;
    width: 36px;
    height: 36px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-shrink: 0;
}

#chat-send:hover {
    background: var(--action-hover);
}



#chat-trigger-btn {
    position: fixed;
    bottom: 20px;
    right: 20px;
    width: 40px;
    height: 40px;
    border-radius: 50%; /* Rund */
    background: var(--action); /* Bruk din primærfarge */
    color: var(--white); /* Refactored to variable */
    border: none;
    cursor: pointer;
    z-index: 9998; /* Må være under chat-vinduet (10001) */
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: var(--shadow-md); /* Refactored to variable */
    transition: transform 0.2s ease-out, background 0.2s ease-out;
    padding: 0;
    overflow: hidden;
}

#chat-trigger-btn svg {
    width: 24px;   /* Juster størrelsen her */
    height: 24px;
    margin: auto;  /* Sørger for perfekt sentrering i flex */
    display: inline-block;
    vertical-align: middle;
}

#chat-trigger-btn:hover {
    transform: translateY(-3px); /* Liten "løfte"-effekt */
    background: var(--action-hover); /* Litt mørkere ved hover */
}

/* Mobil-tilpasning for knappen */
@media (max-width: 600px) {
    #chat-trigger-btn {
        bottom: 15px; /* Litt mindre avstand på mobil */
        right: 15px;
        width: 50px; /* Litt mindre på mobil */
        height: 50px;
        font-size: 24px;
        /* Viktig for iPhone bunnlinje */
        bottom: calc(15px + env(safe-area-inset-bottom, 0px));
    }
}

/* Mobil-tilpasning: Fullskjerm og helt ned til kanten */
@media (max-width: 600px) {
    .chat-container {
        right: 0;
        width: 100vw;
        height: 100vh;
        height: -webkit-fill-available; /* Fikser Safari mobil høyde */
        border-radius: 0;
    }
    
    .chat-input-area {
        padding-bottom: env(safe-area-inset-bottom, 12px); /* Viktig for iPhone bunnlinje */
        bottom: 20px;
    }
}

/* Endre farge på input når vi redigerer */
#chat-input.editing-mode {
    background-color: var(--warning-bg); /* Refactored to variable */
    border: 1px solid var(--warning); /* Refactored to variable */
    color: var(--text); /* Refactored to variable */
}

/* Legg til en liten (redigert)-tekst hvis du vil */
.edited-tag {
    font-size: 0.7rem;
    opacity: 0.5;
    margin-left: 5px;
    font-style: italic;
}

/* Meldingsmeta */

/* Dato-skille (Sentert) */
.chat-date-separator {
    text-align: center;
    margin: 20px 0;
    position: relative;
}

.chat-date-separator span {
    background: var(--bg-secondary); /* Refactored to variable */
    padding: 4px 12px;
    border-radius: 12px;
    font-size: 0.7rem;
    color: var(--text-muted); /* Refactored to variable */
    font-weight: 700;
    letter-spacing: 0.5px;
}

/* Meldinger */
#chat-messages {
    display: flex !important;
    flex-direction: column !important;
    gap: 8px;
    width: 100%;
}

.msg { 
    margin-bottom: 12px; 
    max-width: 85%; 
}

.msg.new-message {
    animation: fadeIn 0.3s ease-out;
}

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

.msg.removing {
    opacity: 0;
    transform: scale(0.95);
    transition: all 0.25s ease;
}

.msg p { 
    padding: 8px 14px; 
    border-radius: 18px; 
    font-size: 14px; 
    line-height: 1.4;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.msg.me {
    align-self: flex-end; /* Dytter boblen til høyre */
    align-items: flex-end; /* Dytter teksten/navnet til høyre inni boblen */
}

.msg.me p {
    background: var(--action);
    color: var(--white); /* Refactored to variable */
    border-radius: 18px 18px 4px 18px; /* Avrundet, men spiss mot høyre bunn */
}

/* ANDRES meldinger (venstre) */
.msg.others {
    align-self: flex-start; /* Dytter boblen til venstre */
    align-items: flex-start;
}
.msg.others p {
    background: var(--bg-secondary); /* Refactored to variable */
    color: var(--text); /* Refactored to variable */
    border-radius: 18px 18px 18px 4px; /* Avrundet, men spiss mot venstre bunn */
}
.msg small { font-size: 11px; margin-bottom: 4px; display: block; margin-left: 0px; }
.msg.me small { text-align: right; margin-right: 10px; }

/* Containeren må være flex, men gjennomsiktig */
.msg-actions {
    display: flex !important; /* Vi tvinger den til å eksistere i layouten */
    gap: 8px;
    opacity: 0 !important; /* Men vi gjør den helt usynlig */
    transition: opacity 0.2s ease-in-out;
    pointer-events: none; /* Umulig å klikke på når usynlig */
}

/* NÅR DU HOVRER BOBLE-VEGGEN (.msg.me) */
.msg.me:hover .msg-actions {
    opacity: 1 !important;
    pointer-events: auto !important;
}

/* Stil på knappene */
.msg-actions button {
    background: none !important;
    border: none !important;
    color: var(--action);
    cursor: pointer !important;
    font-size: 14px !important;
    padding: 2px !important;
}

/* Farge ved direkte hovring på ikon */
.edit-msg:hover { color: var(--brand) !important; }
.delete-msg:hover { color: var(--brand) !important; }

.inbox-item {
    display: flex;
    align-items: center;
    padding: 12px;
    border-bottom: 1px solid var(--border); /* Refactored to variable */
    cursor: pointer;
    transition: background 0.2s;
}

.inbox-item:hover {
    background: var(--bg-secondary); /* Refactored to variable */
}

.user-avatar {
    width: 30px;
    height: 30px;
    background: var(--action);
    color: var(--white); /* Refactored to variable */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
    font-weight: bold;
    font-size: 0.8rem;
}

#chat-back {
    background: none;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    margin-right: 10px;
    color: var(--white); /* Refactored to variable */
}


/* Rød prikk i innboksen */
.unread-dot {
    width: 10px;
    height: 10px;
    background-color: var(--danger); /* Refactored to variable */
    border-radius: 50%;
    margin-left: auto;
}

#chat-menu-btn, #chat-minimize {
    background: transparent; /* Gjennomsiktig som standard */
    border: none;
    color: var(--white); /* Refactored to variable */
    width: 32px;
    height: 32px;
    border-radius: 50%;
    cursor: pointer;
    font-size: 18px;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background 0.2s;
    outline: none;
}

/* Finere hovringseffekt */
#chat-menu-btn:hover, #chat-minimize:hover {
    background: var(--white-t-15); /* Refactored to variable */
}

/* Spesifikk justering for de tre prikkene for å sentrere dem perfekt */
#chat-menu-btn {
    font-size: 20px;
    line-height: 1;
    padding-bottom: 4px; /* Optisk justering */
}

/* Selve menyen (dropdown) som dukker opp */
.chat-menu {
    position: absolute;
    top: 55px; /* Rett under headeren */
    right: 10px;
    background: var(--bg); /* Refactored to variable */
    border-radius: 8px;    
    box-shadow: var(--shadow-lg); /* Refactored to variable */
    border: 1px solid var(--border); /* Refactored to variable */
    padding: 5px 0;
    min-width: 160px;
    z-index: 10005; /* Over alt annet */
}

.chat-menu button {
    padding: 10px 15px;
    font-size: 14px;
    color: var(--text); /* Refactored to variable */
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
}

.chat-menu button {
    padding: 10px 15px;
    border: none;
    background: none;
    cursor: pointer;
    text-align: left;
}
.chat-menu button:hover { background: var(--bg-secondary); /* Refactored to variable */ }

.chat-modal {
    position: absolute;
    top: 0; left: 0; width: 100%; height: 100%;
    background: var(--bg); /* Refactored to variable */
    z-index: 10002;
    padding: 15px;
    display: flex;
    flex-direction: column;
}

.modal-content { height: 100%; display: flex; flex-direction: column; }

#selector-user-list {
    flex: 1;
    overflow-y: auto;
    margin: 10px 0;
}

.selector-item {
    display: flex;
    align-items: center;
    padding: 10px;
    border-bottom: 1px solid var(--border); /* Refactored to variable */
    font-size: 14px;
}

.selector-item input {
    margin-right: 12px;
    width: 18px;
    height: 18px;
}

.modal-actions {
    display: flex;
    justify-content: space-between;
    padding-top: 10px;
}

.modal-actions button {
    padding: 8px 15px;
    border-radius: 5px;
    border: none;
    cursor: pointer;
}

#confirm-selection { background: var(--action); color: var(--white); /* Refactored to variable */ }
#cancel-selection { background: var(--border); /* Refactored to variable */ }

.user-avatar-small {
    width: 30px;
    height: 30px;    
    background: var(--action); /* Refactored to variable */
    color: var(--white); /* Refactored to variable */
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
    font-weight: bold;
    flex-shrink: 0;
    object-fit: cover; /* Viktig for å ikke strekke bildene */
}




/* Skjul de gamle knappene helt */
.msg-actions { display: none !important; }

/* Den nye kontekstmenyen */
.chat-context-menu {
    position: fixed;
    background: var(--bg); /* Refactored to variable */
    border-radius: 12px;    
    box-shadow: var(--shadow-xl); /* Refactored to variable */
    display: flex;
    flex-direction: column;
    padding: 6px;
    z-index: 100000;
    min-width: 120px;
    animation: menuPop 0.15s cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes menuPop {
    from { opacity: 0; transform: scale(0.9); }
    to { opacity: 1; transform: scale(1); }
}

.chat-context-menu button {
    padding: 10px 14px;
    border: none;
    background: none;
    text-align: left;
    font-size: 14px;
    cursor: pointer;
    border-radius: 8px;
    display: flex;
    align-items: center;
    gap: 8px;
}

.chat-context-menu button:hover { background: var(--bg-secondary); /* Refactored to variable */ }
.chat-context-menu .del-btn { color: var(--danger); /* Refactored to variable */ }

/* Containeren for boble + menyknapp */
.msg.me .msg-content-wrapper {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: flex-end;
    gap: 6px;
}

/* Stil på selve horisontal-prikk-knappen */
.msg-menu-trigger {
    background: var(--bg-secondary); /* Refactored to variable */
    border: none;    
    color: var(--text-muted); /* Refactored to variable */
    cursor: pointer;
    width: 26px;
    height: 26px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 18px;
    opacity: 0; /* VIKTIG: Start som usynlig */
    transition: opacity 0.2s;
    order: -1; /* Tvinger knappen til venstre for teksten */
}

/* Vis knappen når man hovrer over meldingen */
.msg.me:hover .msg-menu-trigger {
    opacity: 1;
}

.msg-menu-trigger:hover {
    background: var(--border); /* Refactored to variable */
}

/* Sørg for at boblen ikke krymper når knappen dukker opp */
.msg.me .msg-text {
    margin: 0;
}


/* Meldingsboble-justering */
.msg-bubble {
    position: relative;
    padding: 8px 12px 18px 12px; /* Ekstra padding i bunn for metadata */
    border-radius: 14px;
    max-width: 85%;
    display: inline-block;
}

.msg.me .msg-bubble {
    background: var(--action);
    color: var(--white); /* Refactored to variable */
    border-bottom-right-radius: 4px;
}

.msg.others .msg-bubble {
    background: var(--card-bg);
    color: var(--text); /* Refactored to variable */
    border-bottom-left-radius: 4px;
}

/* Metadata (Tid + Hake) */
.msg-metadata {
    position: absolute;
    bottom: 4px;
    right: 8px;
    display: flex;
    align-items: center;
    gap: 3px;
    font-size: 0.65rem;
    opacity: 0.8;
}

.msg.me .msg-metadata {
    color: var(--white-t-90); /* Refactored to variable */
}

.msg.others .msg-metadata {
    color: var(--text-muted); /* Refactored to variable */
}

.msg-status-icon {
    display: flex;
    align-items: center;
    color: var(--white-t-60); /* Refactored to variable */
}

.msg-status-icon svg {
    display: block;
}

/* Hakene når meldingen er lest (to haker) */
.msg-status-icon.read {
    color: var(--white); /* Refactored to variable */
}

/* Spesifikk justering for dobbelthaken så den ikke tar for mye plass */
.double-check {
    margin-left: 2px;
}

.chat-avatar-msg {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    margin-right: 8px;
    align-self: flex-end; /* Plasser bildet nederst ved meldingen */
}

/* Justering for meldings-layout hvis du legger til bilde */
.msg.others {
    display: flex;
    align-items: flex-end;
}