:root {
    --color-bg: #f0f2f5;
    --color-surface: #ffffff;
    --color-primary: #2563eb;
    --color-primary-dark: #1d4ed8;
    --color-danger: #dc2626;
    --color-danger-dark: #b91c1c;
    --color-text: #111827;
    --color-text-muted: #6b7280;
    --color-border: #e5e7eb;
    --color-bubble-mine: #2563eb;
    --color-bubble-mine-text: #ffffff;
    --color-bubble-theirs: #ffffff;
    --color-bubble-theirs-text: #111827;
    --color-online: #22c55e;
    --radius: 12px;
    --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
}

* { box-sizing: border-box; }

html, body {
    height: 100%;
    margin: 0;
}

body {
    font-family: var(--font);
    color: var(--color-text);
    background: var(--color-bg);
}

button, input, textarea {
    font: inherit;
    color: inherit;
}

a { color: var(--color-primary); }

/* ---- Auth ---- */

.auth-body {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 100%;
    padding: 24px;
}

.auth-card {
    width: 100%;
    max-width: 360px;
    background: var(--color-surface);
    border-radius: var(--radius);
    padding: 32px 28px;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 8px 24px rgba(0, 0, 0, 0.06);
}

.auth-title {
    margin: 0 0 4px;
    font-size: 1.5rem;
}

.auth-subtitle {
    margin: 0 0 24px;
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.auth-form {
    display: flex;
    flex-direction: column;
    gap: 16px;
}

.field {
    display: flex;
    flex-direction: column;
    gap: 6px;
}

.field-label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-muted);
}

.field input {
    padding: 10px 12px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    font-size: 1rem;
}

.field input:focus {
    outline: 2px solid var(--color-primary);
    outline-offset: -1px;
}

.form-error {
    margin: 0;
    color: var(--color-danger);
    font-size: 0.85rem;
}

.btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    padding: 10px 16px;
    border: 1px solid var(--color-border);
    border-radius: 8px;
    background: var(--color-surface);
    cursor: pointer;
    font-weight: 600;
    font-size: 0.95rem;
}

.btn:hover { background: var(--color-bg); }

.btn-primary {
    background: var(--color-primary);
    border-color: var(--color-primary);
    color: #fff;
}

.btn-primary:hover { background: var(--color-primary-dark); }

.btn-danger {
    background: var(--color-danger);
    border-color: var(--color-danger);
    color: #fff;
}

.btn-danger:hover { background: var(--color-danger-dark); }

.btn-small {
    padding: 6px 10px;
    font-size: 0.8rem;
}

.btn[disabled] { opacity: 0.6; cursor: default; }

/* ---- Chat shell ----
   Scoped custom properties so this palette stays local to the chat
   window and doesn't bleed into login/admin, which keep the plain
   --color-primary tokens above. */

.chat-body {
    height: 100%;
    --chat-grad: linear-gradient(135deg, #2563eb 0%, #0ea5e9 100%);
    --chat-primary: #2563eb;
    --chat-primary-dark: #1d4ed8;
    --chat-accent: #f59e0b;
    --chat-online: #10b981;
    --chat-danger: #ef4444;
    --chat-danger-dark: #dc2626;
    --chat-surface: #ffffff;
    --chat-text: #1f2937;
    --chat-text-muted: #6b7280;
    --chat-border: #dbeafe;
    --chat-radius: 20px;
    background: linear-gradient(135deg, #eff6ff 0%, #e0f2fe 50%, #eef2ff 100%);
}

.chat-shell {
    position: relative;
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 720px;
    margin: 0 auto;
    background: var(--chat-surface);
    color: var(--chat-text);
}

.chat-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding: 14px 18px;
    background: var(--chat-grad);
    color: #fff;
    box-shadow: 0 2px 12px rgba(37, 99, 235, 0.25);
    position: relative;
    z-index: 2;
}

.chat-header-peer {
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 0;
}

.avatar {
    width: 42px;
    height: 42px;
    flex: none;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.22);
    border: 2px solid rgba(255, 255, 255, 0.55);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-weight: 700;
    text-transform: uppercase;
}

.chat-header-info { min-width: 0; }

.chat-header-name {
    font-weight: 700;
    letter-spacing: 0.01em;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.chat-header-status {
    display: flex;
    align-items: center;
    gap: 5px;
    font-size: 0.8rem;
    color: rgba(255, 255, 255, 0.8);
}

.chat-header-status::before {
    content: "";
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: rgba(255, 255, 255, 0.5);
    flex: none;
}

.chat-header-status[data-online="1"] {
    color: #fff;
}

.chat-header-status[data-online="1"]::before {
    background: #4ade80;
    box-shadow: 0 0 0 rgba(74, 222, 128, 0.6);
    animation: chatPulse 2s infinite;
}

@keyframes chatPulse {
    0% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0.55); }
    70% { box-shadow: 0 0 0 6px rgba(74, 222, 128, 0); }
    100% { box-shadow: 0 0 0 0 rgba(74, 222, 128, 0); }
}

.chat-header-actions {
    display: flex;
    gap: 2px;
}

.chat-header-actions .icon-btn {
    color: #fff;
}

.chat-header-actions .icon-btn:hover {
    background: rgba(255, 255, 255, 0.18);
}

.icon-btn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 40px;
    height: 40px;
    border: none;
    background: transparent;
    border-radius: 50%;
    cursor: pointer;
    font-size: 1.1rem;
    line-height: 1;
    transition: background 0.15s ease, transform 0.1s ease;
}

.icon-btn:hover { background: var(--color-bg); }
.icon-btn:active { transform: scale(0.92); }

.message-list {
    flex: 1;
    overflow-y: auto;
    padding: 18px 16px;
    display: flex;
    flex-direction: column;
    gap: 4px;
    background:
        radial-gradient(circle at 1px 1px, rgba(37, 99, 235, 0.09) 1px, transparent 0) 0 0 / 22px 22px,
        linear-gradient(180deg, #f5faff, #eff8ff);
}

.message-list::-webkit-scrollbar { width: 8px; }
.message-list::-webkit-scrollbar-thumb {
    background: rgba(37, 99, 235, 0.25);
    border-radius: 8px;
}
.message-list::-webkit-scrollbar-thumb:hover { background: rgba(37, 99, 235, 0.4); }

.message-list-loading {
    text-align: center;
    color: var(--chat-text-muted);
    font-size: 0.85rem;
    padding: 12px;
}

.message-list-empty {
    text-align: center;
    color: var(--chat-text-muted);
    font-size: 0.9rem;
    margin: auto;
}

.bubble-row {
    display: flex;
    min-width: 0;
    margin: 3px 0;
    animation: messageIn 0.22s ease-out;
}

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

.bubble-row.mine { justify-content: flex-end; }
.bubble-row.theirs { justify-content: flex-start; }

.bubble {
    max-width: 75%;
    min-width: 0;
    padding: 9px 13px;
    border-radius: 18px;
    font-size: 0.95rem;
    line-height: 1.35;
    overflow-wrap: break-word;
    white-space: pre-wrap;
}

.bubble-row.mine .bubble {
    background: var(--chat-grad);
    color: #fff;
    border-bottom-right-radius: 5px;
    box-shadow: 0 3px 10px rgba(37, 99, 235, 0.28);
}

.bubble-row.theirs .bubble {
    background: var(--chat-surface);
    color: var(--chat-text);
    border: 1px solid var(--chat-border);
    border-bottom-left-radius: 5px;
    box-shadow: 0 1px 2px rgba(31, 41, 55, 0.05);
}

.bubble img {
    max-width: 100%;
    border-radius: 10px;
    display: block;
}

.bubble-meta {
    display: flex;
    gap: 4px;
    align-items: center;
    font-size: 0.68rem;
    opacity: 0.8;
    margin-top: 4px;
    justify-content: flex-end;
}

.bubble-status.seen { color: #fde68a; }

.day-divider {
    align-self: center;
    background: #fff;
    border: 1px solid var(--chat-border);
    color: var(--chat-primary);
    font-weight: 600;
    font-size: 0.72rem;
    letter-spacing: 0.02em;
    padding: 5px 14px;
    border-radius: 20px;
    margin: 12px 0;
    box-shadow: 0 1px 2px rgba(37, 99, 235, 0.08);
}

.typing-indicator {
    display: flex;
    align-items: center;
    gap: 6px;
    padding: 0 18px 6px;
    font-size: 0.8rem;
    color: var(--chat-text-muted);
}

.typing-indicator[hidden] { display: none; }

.typing-indicator .dot {
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: var(--chat-primary);
    opacity: 0.6;
    animation: typingBounce 1.2s infinite ease-in-out;
}

.typing-indicator .dot:nth-child(2) { animation-delay: 0.15s; }
.typing-indicator .dot:nth-child(3) { animation-delay: 0.3s; }

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

.composer {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    padding: 12px;
    background: var(--chat-surface);
    border-top: 1px solid var(--chat-border);
}

.composer-attach { cursor: pointer; }

.composer-emoji { font-size: 1.2rem; }
.composer-emoji.active { background: var(--color-bg); }

.emoji-picker {
    position: absolute;
    left: 10px;
    right: 10px;
    bottom: 66px;
    max-height: 220px;
    overflow-y: auto;
    background: var(--chat-surface);
    border: 1px solid var(--chat-border);
    border-radius: var(--chat-radius);
    box-shadow: 0 4px 12px rgba(37, 99, 235, 0.12), 0 12px 32px rgba(37, 99, 235, 0.14);
    padding: 8px;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(38px, 1fr));
    gap: 2px;
    z-index: 5;
}

.emoji-picker[hidden] { display: none; }

.emoji-picker button {
    border: none;
    background: transparent;
    font-size: 1.3rem;
    line-height: 1;
    padding: 6px 0;
    border-radius: 8px;
    cursor: pointer;
    transition: transform 0.1s ease;
}

.emoji-picker button:hover { background: #e6f2ff; transform: scale(1.15); }

.composer-input {
    flex: 1;
    resize: none;
    max-height: 120px;
    border: 1px solid var(--chat-border);
    background: #f4f9ff;
    border-radius: 22px;
    padding: 11px 16px;
    font-size: 0.95rem;
    line-height: 1.3;
    transition: box-shadow 0.15s ease, border-color 0.15s ease;
}

.composer-input:focus {
    outline: none;
    border-color: var(--chat-primary);
    background: #fff;
    box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.14);
}

.composer-send {
    background: var(--chat-grad);
    color: #fff;
    box-shadow: 0 2px 8px rgba(37, 99, 235, 0.35);
}

.composer-send:hover { transform: scale(1.06); }
.composer-send:active { transform: scale(0.95); }

/* ---- Modal ---- */

.modal {
    position: fixed;
    inset: 0;
    background: rgba(8, 20, 45, 0.45);
    backdrop-filter: blur(3px);
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    z-index: 10;
}

.modal[hidden] { display: none; }

.modal-card {
    background: var(--chat-surface, var(--color-surface));
    border-radius: var(--chat-radius, var(--radius));
    padding: 24px;
    max-width: 360px;
    width: 100%;
    box-shadow: 0 20px 50px rgba(8, 20, 45, 0.25);
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
    margin-top: 20px;
}

.modal .btn-danger {
    background: var(--chat-danger, var(--color-danger));
    border-color: var(--chat-danger, var(--color-danger));
}

.modal .btn-danger:hover { background: var(--chat-danger-dark, var(--color-danger-dark)); }

@media (min-width: 640px) {
    .chat-body { padding: 24px; }
    .chat-shell {
        height: calc(100vh - 48px);
        border-radius: var(--chat-radius);
        overflow: hidden;
        box-shadow: 0 10px 40px rgba(37, 99, 235, 0.18), 0 2px 8px rgba(0, 0, 0, 0.05);
    }
}
