* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

:root {
    --primary: #007AFF;
    --primary-dark: #0051D5;
    --bg-light: #FFFFFF;
    --bg-secondary-light: #F5F5F7;
    --text-light: #000000;
    --text-secondary-light: #65656B;
    --border-light: #E5E5EA;
    --message-sent: #007AFF;
    --message-received: #E5E5EA;
    --text-on-sent: #FFFFFF;
    --text-on-received: #000000;
    --link-on-sent: #39FF14;
    
    --bg-dark: #000000;
    --bg-secondary-dark: #1C1C1E;
    --text-dark: #FFFFFF;
    --text-secondary-dark: #A1A1A6;
    --border-dark: #424245;
}

@media (prefers-color-scheme: dark) {
    :root {
        --bg-light: var(--bg-dark);
        --bg-secondary-light: var(--bg-secondary-dark);
        --text-light: var(--text-dark);
        --text-secondary-light: var(--text-secondary-dark);
        --border-light: var(--border-dark);
        --message-received: #2C2C2E;
        --text-on-received: var(--text-dark);
    }
}

html, body {
    width: 100%;
    height: 100%;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "Helvetica Neue", sans-serif;
    background: var(--bg-light);
    color: var(--text-light);
    overflow: hidden;
}

/* ===== PAGES ===== */

.page {
    display: none;
    width: 100%;
    height: 100%;
}

.page.active {
    display: flex;
}

/* ===== AUTH PAGE ===== */

#authPage {
    align-items: center;
    justify-content: center;
    background: var(--bg-light);
}

.auth-container {
    width: 100%;
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
}

.auth-card {
    width: 100%;
    max-width: 400px;
    background: var(--bg-light);
}

.auth-card h1 {
    font-size: 32px;
    font-weight: 700;
    margin-bottom: 30px;
    text-align: center;
    color: var(--text-light);
}

.form-section {
    display: none;
}

.form-section.active {
    display: block;
}

.form-section h2 {
    font-size: 20px;
    font-weight: 600;
    margin-bottom: 20px;
    color: var(--text-light);
}

.input {
    width: 100%;
    padding: 12px 16px;
    margin-bottom: 12px;
    border: 1px solid var(--border-light);
    border-radius: 8px;
    font-size: 16px;
    background: var(--bg-secondary-light);
    color: var(--text-light);
    transition: all 0.2s;
}

.input:focus {
    outline: none;
    border-color: var(--primary);
    background: var(--bg-light);
    box-shadow: 0 0 0 3px rgba(0, 122, 255, 0.1);
}

.input::placeholder {
    color: var(--text-secondary-light);
}

.btn {
    padding: 12px 16px;
    border: none;
    border-radius: 8px;
    font-size: 16px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s;
}

.btn-primary {
    width: 100%;
    background: var(--primary);
    color: white;
    margin-bottom: 16px;
}

.btn-primary:active {
    background: var(--primary-dark);
    transform: scale(0.98);
}

.btn-secondary {
    background: var(--bg-secondary-light);
    color: var(--text-light);
    padding: 8px 16px;
    font-size: 14px;
}

.btn-secondary:active {
    background: var(--border-light);
}

.toggle-text {
    text-align: center;
    color: var(--text-secondary-light);
    font-size: 14px;
    margin-bottom: 16px;
}

.link {
    color: var(--primary);
    cursor: pointer;
    font-weight: 600;
}

.link:active {
    opacity: 0.7;
}

.error-message {
    color: #FF3B30;
    font-size: 14px;
    margin-top: 12px;
    text-align: center;
}

/* ===== CHAT PAGE ===== */

#chatPage {
    flex-direction: column;
}

.chat-container {
    display: flex;
    flex-direction: column;
    width: 100%;
    height: 100%;
    background: var(--bg-light);
}

.chat-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px 16px;
    border-bottom: 1px solid var(--border-light);
    background: var(--bg-light);
}

.header-content h1 {
    font-size: 20px;
    font-weight: 700;
    margin: 0;
    color: var(--text-light);
}

.user-info {
    font-size: 13px;
    color: var(--text-secondary-light);
    margin-top: 2px;
}

.messages-container {
    flex: 1;
    overflow-y: auto;
    background: var(--bg-light);
    padding: 12px 16px;
}

.messages-list {
    display: flex;
    flex-direction: column;
    gap: 8px;
}

/* ===== MESSAGES ===== */

.message {
    display: flex;
    flex-direction: column;
    max-width: 85%;
    word-wrap: break-word;
    animation: fadeIn 0.3s ease-in;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.message.sent {
    align-self: flex-end;
    align-items: flex-end;
}

.message.received {
    align-self: flex-start;
    align-items: flex-start;
}

.message-name {
    font-size: 12px;
    font-weight: 600;
    color: var(--text-secondary-light);
    margin-bottom: 4px;
    padding: 0 12px;
}

.message-text {
    background: var(--message-sent);
    color: var(--text-on-sent);
    padding: 8px 12px;
    border-radius: 18px;
    font-size: 15px;
    line-height: 1.4;
    word-break: break-word;
}

.message.received .message-text {
    background: var(--message-received);
    color: var(--text-on-received);
}

.message-link {
    color: var(--primary);
    text-decoration: none;
    word-break: break-all;
}

/* Sent bubbles are blue, so a blue link disappears — use a bright green there.
   Saturated neon green on purpose: paler limes antialias toward blue-white
   against this bubble and end up reading as plain white text. */
.message.sent .message-link {
    color: var(--link-on-sent);
    font-weight: 600;
}

.message.received .message-link {
    color: var(--primary);
}

/* Images live inside their message, so the list only downloads one when it is
   asked for. This is what stands in until then. */
.load-image-btn {
    background: var(--bg-secondary-light);
    color: var(--primary);
    border: 1px dashed var(--border-light);
    border-radius: 18px;
    padding: 9px 14px;
    font-size: 14px;
    font-weight: 600;
    font-family: inherit;
    cursor: pointer;
}

.message.sent .load-image-btn {
    background: rgba(255, 255, 255, 0.18);
    border-color: rgba(255, 255, 255, 0.55);
    color: var(--text-on-sent);
}

.load-image-btn:disabled {
    opacity: 0.7;
    cursor: wait;
}

.message-image {
    max-width: 250px;
    max-height: 300px;
    border-radius: 12px;
    margin: 4px 0;
}

.message-time {
    font-size: 12px;
    color: var(--text-secondary-light);
    margin-top: 4px;
    padding: 0 12px;
}

/* ===== INPUT AREA ===== */

.chat-input-area {
    display: flex;
    align-items: flex-end;
    gap: 8px;
    padding: 12px 16px;
    border-top: 1px solid var(--border-light);
    background: var(--bg-light);
}

.input-toolbar {
    display: flex;
    gap: 8px;
}

.toolbar-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--bg-secondary-light);
    color: var(--primary);
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.toolbar-btn:active {
    background: var(--border-light);
    transform: scale(0.9);
}

.message-input {
    flex: 1;
    padding: 10px 16px;
    border: 1px solid var(--border-light);
    border-radius: 20px;
    font-size: 15px;
    background: var(--bg-secondary-light);
    color: var(--text-light);
    resize: none;
    max-height: 100px;
    font-family: inherit;
}

.message-input:focus {
    outline: none;
    border-color: var(--primary);
}

.message-input::placeholder {
    color: var(--text-secondary-light);
}

.send-btn {
    width: 36px;
    height: 36px;
    border: none;
    background: var(--primary);
    color: white;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: all 0.2s;
    flex-shrink: 0;
}

.send-btn:active {
    background: var(--primary-dark);
    transform: scale(0.9);
}

/* ===== IMAGE MODAL ===== */

.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.9);
    z-index: 1000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    position: relative;
    max-width: 90vw;
    max-height: 90vh;
    display: flex;
    align-items: center;
    justify-content: center;
}

.modal-image {
    max-width: 100%;
    max-height: 100%;
    border-radius: 8px;
}

.modal-close {
    position: absolute;
    top: -40px;
    right: 0;
    width: 44px;
    height: 44px;
    background: none;
    border: none;
    color: white;
    font-size: 32px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
}

/* ===== TYPING INDICATOR ===== */

.typing-indicator {
    display: flex;
    gap: 4px;
    padding: 8px 12px;
}

.typing-indicator.hidden {
    display: none;
}

.typing-dot {
    width: 8px;
    height: 8px;
    background: var(--text-secondary-light);
    border-radius: 50%;
    animation: typing 1.4s infinite;
}

.typing-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes typing {
    0%, 60%, 100% {
        opacity: 0.3;
        transform: translateY(0);
    }
    30% {
        opacity: 1;
        transform: translateY(-10px);
    }
}

/* ===== SCROLLBAR ===== */

.messages-container::-webkit-scrollbar {
    width: 6px;
}

.messages-container::-webkit-scrollbar-track {
    background: var(--bg-light);
}

.messages-container::-webkit-scrollbar-thumb {
    background: var(--border-light);
    border-radius: 3px;
}

.messages-container::-webkit-scrollbar-thumb:hover {
    background: var(--text-secondary-light);
}

/* ===== RESPONSIVE ===== */

@media (max-width: 600px) {
    .auth-card {
        max-width: 100%;
    }

    .message {
        max-width: 90%;
    }

    .message-image {
        max-width: 200px;
        max-height: 250px;
    }

    .chat-header {
        padding: 10px 12px;
    }

    .messages-container {
        padding: 8px 12px;
    }

    .chat-input-area {
        padding: 8px 12px;
        gap: 6px;
    }

    .toolbar-btn,
    .send-btn {
        width: 32px;
        height: 32px;
    }

    .message-input {
        padding: 8px 12px;
    }

    .header-content h1 {
        font-size: 18px;
    }
}

@media (max-width: 400px) {
    .auth-card h1 {
        font-size: 28px;
    }

    .message {
        max-width: 95%;
    }

    .message-image {
        max-width: 180px;
        max-height: 220px;
    }

    .message-text {
        font-size: 14px;
    }

    .input {
        font-size: 16px;
    }
}
