/* Reset and Base Styles */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --blue-bubble: rgb(89, 166, 246);
    --gray-bubble: rgb(233, 233, 235);
    --gray-text: #000000;
    --blue-text: #FFFFFF;
    --background: #FFFFFF;
    --nav-bg: rgba(249, 249, 249, 0.6);
    --nav-border: rgba(0, 0, 0, 0.1);
    --icon-color: #3A3A3C;
    --safe-area-top: env(safe-area-inset-top, 0px);
    --safe-area-bottom: env(safe-area-inset-bottom, 0px);
}

/* Light mode - class-based for manual toggle */
[data-theme="light"] {
    --blue-bubble: rgb(89, 166, 246);
    --gray-bubble: rgb(233, 233, 235);
    --gray-text: #000000;
    --blue-text: #FFFFFF;
    --background: #FFFFFF;
    --nav-bg: rgba(249, 249, 249, 0.6);
    --nav-border: rgba(0, 0, 0, 0.1);
    --icon-color: #3A3A3C;
}

/* Dark mode - class-based for manual toggle */
[data-theme="dark"] {
    --blue-bubble: rgb(66, 143, 247);
    --gray-bubble: rgb(38, 37, 42);
    --gray-text: #FFFFFF;
    --background: #000000;
    --nav-bg: rgba(28, 28, 30, 0.6);
    --nav-border: rgba(255, 255, 255, 0.1);
    --icon-color: #FFFFFF;
}

/* Auto dark mode when no preference is set */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) {
        --blue-bubble: rgb(66, 143, 247);
        --gray-bubble: rgb(38, 37, 42);
        --gray-text: #FFFFFF;
        --background: #000000;
        --nav-bg: rgba(28, 28, 30, 0.6);
        --nav-border: rgba(255, 255, 255, 0.1);
        --icon-color: #FFFFFF;
    }
}

html {
    font-family: -apple-system, BlinkMacSystemFont, 'SF Pro Display', 'Segoe UI', Roboto, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

body {
    background: var(--background);
    height: 100vh;
    height: 100dvh;
    overflow: hidden;
}

/* Main Container */
.imessage-container {
    max-width: 100%;
    height: 100vh;
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

/* Translucent Navigation Bar */
.nav-bar {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    z-index: 100;
    background: var(--nav-bg);
    backdrop-filter: blur(20px);
    -webkit-backdrop-filter: blur(20px);
    border-bottom: 0.5px solid var(--nav-border);
    padding-top: var(--safe-area-top);
}

.nav-content {
    position: relative;
    padding: 12px 16px 16px;
}

/* Theme Toggle Button */
.theme-toggle {
    position: absolute;
    top: 12px;
    right: 16px;
    width: 36px;
    height: 36px;
    border: none;
    background: transparent;
    cursor: pointer;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: background-color 0.2s ease;
    -webkit-tap-highlight-color: transparent;
}

.theme-toggle:hover {
    background: rgba(128, 128, 128, 0.1);
}

.theme-toggle:active {
    background: rgba(128, 128, 128, 0.2);
}

.theme-toggle svg {
    width: 22px;
    height: 22px;
    color: var(--icon-color);
    transition: transform 0.3s ease, opacity 0.3s ease;
}

.theme-toggle .icon-sun {
    position: absolute;
}

.theme-toggle .icon-moon {
    position: absolute;
    opacity: 0;
    transform: rotate(-90deg);
}

/* Show moon in dark mode */
[data-theme="dark"] .theme-toggle .icon-sun {
    opacity: 0;
    transform: rotate(90deg);
}

[data-theme="dark"] .theme-toggle .icon-moon {
    opacity: 1;
    transform: rotate(0deg);
}

/* Auto dark mode icon states */
@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .theme-toggle .icon-sun {
        opacity: 0;
        transform: rotate(90deg);
    }

    :root:not([data-theme="light"]) .theme-toggle .icon-moon {
        opacity: 1;
        transform: rotate(0deg);
    }
}

/* Light mode explicit - ensure sun shows */
[data-theme="light"] .theme-toggle .icon-sun {
    opacity: 1;
    transform: rotate(0deg);
}

[data-theme="light"] .theme-toggle .icon-moon {
    opacity: 0;
    transform: rotate(-90deg);
}

.profile-section {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 6px;
}

.profile-pic {
    width: 64px;
    height: 64px;
    border-radius: 50%;
    overflow: hidden;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.profile-pic img {
    width: 100%;
    height: 100%;
    object-fit: cover;
}

.profile-pic .initials {
    color: white;
    font-size: 24px;
    font-weight: 600;
}

.profile-name {
    font-size: 14px;
    font-weight: 600;
    color: var(--gray-text);
    text-align: center;
}

/* Messages Container */
.messages-container {
    flex: 1;
    padding-top: 140px; /* Space for fixed nav */
    padding-bottom: calc(40px + var(--safe-area-bottom));
    overflow-y: auto;
    scrollbar-width: none; /* Firefox */
    -ms-overflow-style: none; /* IE/Edge */
    -webkit-overflow-scrolling: touch; /* Native iOS bounce */
}

.messages-container::-webkit-scrollbar {
    display: none; /* Chrome, Safari, Edge */
}

.messages-wrapper {
    max-width: 800px;
    margin: 0 auto;
    padding: 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
}

/* Message Wrapper */
.message-wrapper {
    display: flex;
    flex-direction: column;
    max-width: 75%;
}

.message-wrapper.received {
    align-self: flex-start;
}

.message-wrapper.sent {
    align-self: flex-end;
}

/* Message Status (Delivered/Read) */
.message-status {
    font-size: 11px;
    font-weight: 600;
    color: #8E8E93;
    text-align: right;
    margin-top: 4px;
    margin-right: 12px;
    opacity: 0;
    animation: messageAppear 0.3s ease forwards;
    animation-delay: 0.2s;
    transition: opacity 0.15s ease, transform 0.15s ease;
}

.message-status.status-changing {
    opacity: 0;
    transform: translateY(-2px);
}

/* Message Bubbles */
.message {
    position: relative;
    max-width: 100%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 18px;
    line-height: 1.4;
    word-wrap: break-word;
    opacity: 0;
    transform: translateY(20px) scale(0.95);
    animation: messageAppear 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

/* Gray bubbles - received (left side) */
.message.received {
    align-self: flex-start;
    background: var(--gray-bubble);
    color: var(--gray-text);
    margin-right: auto;
    margin-left: 10px;
}

/* Blue bubbles - sent (right side) */
.message.sent {
    align-self: flex-end;
    background: var(--blue-bubble);
    color: var(--blue-text);
    margin-left: auto;
    margin-right: 10px;
}

/* Bubble tail for received messages */
.message.received::before {
    content: "";
    position: absolute;
    bottom: 0;
    left: -8px;
    height: 20px;
    width: 20px;
    background-color: var(--gray-bubble);
    border-bottom-right-radius: 16px;
}

.message.received::after {
    content: "";
    position: absolute;
    bottom: 0;
    left: -10px;
    width: 10px;
    height: 20px;
    background-color: var(--background);
    border-bottom-right-radius: 10px;
}

/* Bubble tail for sent messages */
.message.sent::before {
    content: "";
    position: absolute;
    bottom: 0;
    right: -8px;
    height: 20px;
    width: 20px;
    background-color: var(--blue-bubble);
    border-bottom-left-radius: 16px;
}

.message.sent::after {
    content: "";
    position: absolute;
    bottom: 0;
    right: -10px;
    width: 10px;
    height: 20px;
    background-color: var(--background);
    border-bottom-left-radius: 10px;
}

/* Hide tail when NOT the last message in a group */
.message-wrapper.received:has(+ .message-wrapper.received) .message::before,
.message-wrapper.received:has(+ .message-wrapper.received) .message::after {
    display: none;
}

.message-wrapper.sent:has(+ .message-wrapper.sent) .message::before,
.message-wrapper.sent:has(+ .message-wrapper.sent) .message::after {
    display: none;
}

/* Consecutive messages - tighter corner radius (only for non-tail messages) */
.message-wrapper.received + .message-wrapper.received:has(+ .message-wrapper.received) .message {
    border-top-left-radius: 4px;
}

.message-wrapper.sent + .message-wrapper.sent:has(+ .message-wrapper.sent) .message {
    border-top-right-radius: 4px;
}

/* Gap between different senders */
.message-wrapper.received + .message-wrapper.sent,
.message-wrapper.sent + .message-wrapper.received {
    margin-top: 12px;
}

/* Message appear animation */
@keyframes messageAppear {
    0% {
        opacity: 0;
        transform: translateY(20px) scale(0.95);
    }
    100% {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

/* Wave animation for hand emoji */
.wave {
    display: inline-block;
    animation: wave 2s infinite;
    transform-origin: 70% 70%;
    cursor: url('./img/waving_hand_cursor.png') 12 12, pointer;
    -webkit-tap-highlight-color: transparent;
}

/*
@keyframes wave {
    0% { transform: rotate(0deg); }
    10% { transform: rotate(14deg); }
    20% { transform: rotate(-8deg); }
    30% { transform: rotate(14deg); }
    40% { transform: rotate(-4deg); }
    50% { transform: rotate(10deg); }
    60% { transform: rotate(0deg); }
    100% { transform: rotate(0deg); }
}*/

/* Typing indicator (thought bubble style) */
.typing-indicator {
    position: relative;
    display: flex;
    align-items: center;
    gap: 5px;
    padding: 14px 18px;
    background: var(--gray-bubble);
    border-radius: 20px;
    align-self: flex-start;
    width: fit-content;
    margin-left: 20px;
    margin-bottom: 32px;
    opacity: 0;
    animation: messageAppear 0.3s ease forwards;
}

.typing-indicator::before {
    content: "";
    position: absolute;
    bottom: -20px;
    left: 2px;
    width: 16px;
    height: 16px;
    background: var(--gray-bubble);
    border-radius: 50%;
}

.typing-indicator::after {
    content: "";
    position: absolute;
    bottom: -28px;
    left: -4px;
    width: 7px;
    height: 7px;
    background: var(--gray-bubble);
    border-radius: 50%;
}

.typing-indicator span {
    width: 8px;
    height: 8px;
    background: #8E8E93;
    border-radius: 50%;
    animation: typingBounce 1.4s infinite ease-in-out;
}

.typing-indicator span:nth-child(1) {
    animation-delay: 0s;
}

.typing-indicator span:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-indicator span:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typingBounce {
    0%, 60%, 100% {
        /* transform: translateY(0); */
        opacity: 0.4;
    }
    30% {
        /* transform: translateY(-4px); */
        opacity: 1;
    }
}

/* Sent typing indicator (blue) */
.typing-indicator.sent {
    background: var(--blue-bubble);
    align-self: flex-end;
    margin-left: auto;
    margin-right: 20px;
}

.typing-indicator.sent::before {
    left: auto;
    right: 2px;
    background: var(--blue-bubble);
}

.typing-indicator.sent::after {
    left: auto;
    right: -4px;
    background: var(--blue-bubble);
}

.typing-indicator.sent span {
    background: rgba(255, 255, 255, 0.6);
}

/* Links in messages */
.message a {
    color: inherit;
    text-decoration: none;
}

.message.sent a {
    color: #FFFFFF;
}

.message.received a {
    color: #147EFB;
}

/* Subscript in messages */
.message sub {
    font-size: 0.7em;
    vertical-align: baseline;
    position: relative;
    top: 0.3em;
    opacity: 0.8;
}

/* Message menu (poll style) */
.message-menu {
    display: flex;
    flex-direction: column;
    gap: 8px;
    align-self: flex-start;
    margin-left: 10px;
    margin-top: 12px;
    opacity: 0;
    animation: messageAppear 0.4s cubic-bezier(0.175, 0.885, 0.32, 1.275) forwards;
}

.menu-option {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 8px;
    width: fit-content;
    padding: 16px;
    background: rgb(72, 49, 16);
    border: none;
    border-radius: 9999px;
    color: rgb(239, 173, 80);
    font-size: 17px;
    font-weight: 600;
    text-decoration: none;
    transition: all 0.2s ease;
    cursor: pointer;
}

.menu-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 20px;
    line-height: 1;
}

.menu-icon svg {
    width: 20px;
    height: 20px;
    fill: currentColor;
    stroke: currentColor;
}

.menu-icon img {
    width: 20px;
    height: 20px;
    object-fit: contain;
}

.menu-option:hover {
    background: rgb(92, 69, 36);
}

.menu-option:active {
    transform: scale(0.98);
}

/* Menu option wrapper for reaction positioning */
.menu-option-wrapper {
    position: relative;
    display: block;
    width: fit-content;
}

/* Add space when wrapper has a reaction */
.menu-option-wrapper:has(.menu-reaction) {
    margin-top: 24px;
}

.menu-option-wrapper .menu-option {
    display: inline-flex;
}

/* Reaction bubble base styles */
.menu-reaction {
    position: absolute;
    top: -22px;
    z-index: 10;
    opacity: 0;
    transform: scale(0) translateY(10px);
    animation: reactionPop 0.5s cubic-bezier(0.175, 0.885, 0.32, 1.5) 0.6s forwards;
}

/* Reaction positioned on right (for received messages - tail points down-right) */
.menu-reaction.reaction-left {
    right: -16px;
    left: auto;
}

/* Reaction positioned on left (for sent messages - tail points down-left) */
.menu-reaction.reaction-right {
    left: -16px;
    right: auto;
}

.reaction-bubble {
    filter: drop-shadow(0 2px 8px rgba(0, 0, 0, 0.15));
}

/* Reaction stroke - dark mode (default): black */
.reaction-stroke {
    fill: #000000;
}

/* Reaction stroke - light mode: white */
[data-theme="light"] .reaction-stroke {
    fill: #ffffff;
}

@media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) .reaction-stroke {
        fill: #ffffff;
    }
}

/* iOS-style reaction pop animation */
@keyframes reactionPop {
    0% {
        opacity: 0;
        transform: scale(0) translateY(10px);
    }
    50% {
        opacity: 1;
        transform: scale(1.2) translateY(-2px);
    }
    70% {
        transform: scale(0.9) translateY(1px);
    }
    85% {
        transform: scale(1.05) translateY(-1px);
    }
    100% {
        opacity: 1;
        transform: scale(1) translateY(0);
    }
}

/* Light mode menu */
[data-theme="light"] .menu-option {
    background: rgb(251, 242, 225);
    color: rgb(246, 164, 79);
}

[data-theme="light"] .menu-option:hover {
    background: rgb(241, 232, 215);
}

@media (prefers-color-scheme: light) {
    :root:not([data-theme="dark"]) .menu-option {
        background: rgb(251, 242, 225);
        color: rgb(246, 164, 79);
    }

    :root:not([data-theme="dark"]) .menu-option:hover {
        background: rgb(241, 232, 215);
    }
}

/* Responsive Design */
@media (max-width: 768px) {
    .messages-container {
        padding-top: 130px;
    }

    .message-wrapper {
        max-width: 75%;
    }

    .message {
        max-width: 100%;
        font-size: 17px;
    }

    .profile-pic {
        width: 56px;
        height: 56px;
    }

    .profile-pic .initials {
        font-size: 20px;
    }

    .profile-name {
        font-size: 16px;
    }
}

@media (max-width: 480px) {
    .messages-wrapper {
        padding: 12px;
    }

    .message-wrapper {
        max-width: 85%;
    }

    .message {
        max-width: 100%;
        padding: 9px 12px;
        font-size: 16px;
    }

    .nav-content {
        padding: 10px 12px 14px;
    }

    .profile-pic {
        width: 52px;
        height: 52px;
    }

    .theme-toggle {
        top: 10px;
        right: 12px;
        width: 32px;
        height: 32px;
    }

    .theme-toggle svg {
        width: 20px;
        height: 20px;
    }
}

/* Smooth scrolling */
html {
    scroll-behavior: smooth;
}

/* Selection color */
::selection {
    background: rgba(0, 122, 255, 0.3);
}

/* Timestamp styles */
.timestamp {
    font-size: 12px;
    font-weight: 400;
    color: #8E8E93;
    text-align: center;
    margin: 8px 0 16px;
    opacity: 1;
}

/* Carbon footer */
.carbon-footer {
    font-size: 14px;
    font-weight: 400;
    color: #8E8E93;
    text-align: center;
    margin: 24px 0 16px;
    opacity: 0;
    animation: messageAppear 0.4s ease forwards;
}

.carbon-footer a {
    color: #8E8E93;
    text-decoration: none;
}

.carbon-footer:hover a {
    border-bottom: 1px solid #8E8E93;
}
