*,
*::before,
*::after {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

:root {
    --bg-page: #0d0d0f;
    --bg-primary: #111113;
    --bg-secondary: #18181c;
    --bg-tertiary: #1f1f24;
    --bg-input: #0a0a0c;
    --border: #2a2a30;
    --border-hover: #3d3d45;
    --text-primary: #f0ede8;
    --text-secondary: #9a9590;
    --text-muted: #5c5855;
    --accent: #c9a962;
    --accent-hover: #d4b872;
    --accent-soft: rgba(201, 169, 98, 0.12);
    --gold: #c9a962;
    --gold-soft: rgba(201, 169, 98, 0.15);
    --error: #d4635a;
    --error-soft: rgba(212, 99, 90, 0.12);
    --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
    --shadow-md: 0 4px 12px rgba(0, 0, 0, 0.4);
    --shadow-lg: 0 8px 24px rgba(0, 0, 0, 0.5);
    --radius-sm: 6px;
    --radius-md: 10px;
    --radius-lg: 16px;
    --font-display: "Cormorant Garamond", Georgia, serif;
    --font-body: "DM Sans", -apple-system, sans-serif;
    --transition: 0.25s cubic-bezier(0.4, 0, 0.2, 1);
}

::selection {
    background: var(--gold-soft);
    color: var(--text-primary);
}

html {
    scroll-behavior: smooth;
}

body {
    font-family: var(--font-body);
    background: var(--bg-page);
    color: var(--text-primary);
    min-height: 100vh;
    display: flex;
    line-height: 1.6;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.noise-overlay {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    pointer-events: none;
    opacity: 0.025;
    z-index: 9999;
    background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)'/%3E%3C/svg%3E");
}

.container {
    flex: 1;
    display: flex;
    flex-direction: column;
    max-width: 900px;
    width: 100%;
    margin: 0 auto;
    background: var(--bg-primary);
    min-height: 100vh;
    box-shadow: var(--shadow-lg);
    position: relative;
}

header {
    background: rgba(17, 17, 19, 0.85);
    padding: 1.25rem 2rem;
    display: flex;
    justify-content: space-between;
    align-items: center;
    border-bottom: 1px solid var(--border);
    position: sticky;
    top: 0;
    z-index: 100;
    backdrop-filter: blur(16px);
}

.header-brand {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.brand-ornament {
    font-size: 0.75rem;
    color: var(--gold);
    animation: pulse-glow 3s ease-in-out infinite;
}

@keyframes pulse-glow {
    0%,
    100% {
        opacity: 0.5;
        text-shadow: 0 0 8px var(--gold-soft);
    }
    50% {
        opacity: 1;
        text-shadow: 0 0 16px var(--gold);
    }
}

h1 {
    font-family: var(--font-display);
    font-size: 1.5rem;
    font-weight: 500;
    letter-spacing: 0.04em;
    color: var(--text-primary);
}

.header-right {
    display: flex;
    align-items: center;
    gap: 0.875rem;
}

#session-selector {
    display: none;
    padding: 0.5rem 0.75rem;
    background: var(--bg-input);
    color: var(--text-primary);
    border: 1px solid var(--border);
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
}

#session-selector:hover {
    border-color: var(--border-hover);
}

#session-selector:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 3px var(--accent-soft);
}

#new-session-btn,
#delete-session-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0.5rem;
    background: transparent;
    border: 1px solid transparent;
    color: var(--text-muted);
    cursor: pointer;
    border-radius: var(--radius-sm);
    transition: var(--transition);
}

#new-session-btn:hover,
#delete-session-btn:hover {
    color: var(--text-primary);
    background: var(--bg-secondary);
    border-color: var(--border);
}

#new-session-btn:focus-visible,
#delete-session-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#login-btn,
#logout-btn {
    padding: 0.5rem 1.25rem;
    background: var(--accent);
    color: #0d0d0f;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.85rem;
    font-weight: 500;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
}

#login-btn:hover,
#logout-btn:hover {
    background: var(--accent-hover);
    transform: translateY(-1px);
    box-shadow: 0 0 20px var(--accent-soft);
}

#logout-btn {
    background: transparent;
    color: var(--text-secondary);
    border: 1px solid var(--border);
}

#logout-btn:hover {
    border-color: var(--border-hover);
    color: var(--text-primary);
    background: transparent;
    box-shadow: none;
}

.user-info {
    display: none;
    align-items: center;
    gap: 0.625rem;
    padding: 0.375rem 0.75rem 0.375rem 0.375rem;
    background: var(--bg-secondary);
    border-radius: 100px;
    border: 1px solid var(--border);
    position: relative;
    cursor: pointer;
}

.user-info:hover {
    border-color: var(--border-hover);
}

.user-dropdown {
    position: absolute;
    top: calc(100% + 8px);
    right: 0;
    min-width: 160px;
    background: var(--bg-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    overflow: hidden;
    z-index: 200;
}

.user-dropdown.show {
    display: block;
}

.dropdown-item {
    display: flex;
    align-items: center;
    gap: 0.625rem;
    width: 100%;
    padding: 0.75rem 1rem;
    background: transparent;
    border: none;
    color: var(--text-secondary);
    font-size: 0.875rem;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
    text-align: left;
}

.dropdown-item:hover {
    background: var(--bg-tertiary);
    color: var(--text-primary);
}

.user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    object-fit: cover;
    border: 1px solid var(--border);
}

.user-name {
    font-size: 0.85rem;
    color: var(--text-secondary);
    font-weight: 500;
}

#chat-container {
    flex: 1;
    overflow-y: auto;
    padding: 2rem;
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    scroll-behavior: smooth;
}

#chat-container::-webkit-scrollbar {
    width: 6px;
}

#chat-container::-webkit-scrollbar-track {
    background: transparent;
}

#chat-container::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 3px;
}

#chat-container::-webkit-scrollbar-thumb:hover {
    background: var(--border-hover);
}

.message {
    max-width: 72%;
    padding: 1rem 1.25rem;
    border-radius: var(--radius-lg);
    line-height: 1.7;
    font-size: 0.95rem;
    animation: message-enter 0.4s cubic-bezier(0.16, 1, 0.3, 1);
    position: relative;
}

@keyframes message-enter {
    from {
        opacity: 0;
        transform: translateY(12px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: translateY(0) scale(1);
    }
}

.message.user {
    align-self: flex-end;
    background: linear-gradient(135deg, var(--accent) 0%, #b89858 100%);
    color: #0d0d0f;
    border-bottom-right-radius: var(--radius-sm);
    box-shadow:
        var(--shadow-md),
        0 0 30px var(--accent-soft);
}

.message.user::before {
    content: "";
    position: absolute;
    bottom: -5px;
    right: 20px;
    width: 10px;
    height: 10px;
    background: inherit;
    clip-path: polygon(0 0, 100% 0, 100% 100%);
    transform: rotate(45deg);
}

.message.assistant {
    align-self: flex-start;
    background: var(--bg-secondary);
    color: var(--text-primary);
    border-bottom-left-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.message.assistant::before {
    content: "";
    position: absolute;
    bottom: -5px;
    left: 20px;
    width: 10px;
    height: 10px;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border);
    border-bottom: 1px solid var(--border);
    clip-path: polygon(0 0, 100% 100%, 0 100%);
    transform: rotate(-45deg);
}

.message .image-container {
    margin-top: 1rem;
    border-radius: var(--radius-md);
    overflow: hidden;
    box-shadow: var(--shadow-md);
}

.message img {
    max-width: 100%;
    display: block;
    cursor: pointer;
    transition: var(--transition);
}

.message img:hover {
    transform: scale(1.01);
}

.message.error {
    background: var(--error-soft);
    color: var(--error);
    border: 1px solid rgba(212, 99, 90, 0.2);
    font-size: 0.9rem;
}

.message.loading {
    color: var(--text-muted);
    font-style: italic;
    display: flex;
    align-items: center;
    gap: 0.75rem;
    background: transparent;
    border: none;
    box-shadow: none;
}

.message.loading::before {
    display: none;
}

.loading-dots {
    display: flex;
    gap: 4px;
}

.loading-dots span {
    width: 6px;
    height: 6px;
    background: var(--accent);
    border-radius: 50%;
    animation: bounce-dot 1.4s infinite ease-in-out both;
}

.loading-dots span:nth-child(1) {
    animation-delay: -0.32s;
}
.loading-dots span:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce-dot {
    0%,
    80%,
    100% {
        transform: scale(0.6);
        opacity: 0.4;
    }
    40% {
        transform: scale(1);
        opacity: 1;
    }
}

#input-container {
    background: rgba(17, 17, 19, 0.9);
    padding: 1.25rem 2rem 1.5rem;
    border-top: 1px solid var(--border);
    position: sticky;
    bottom: 0;
    backdrop-filter: blur(16px);
}

.input-wrapper {
    max-width: 720px;
    margin: 0 auto;
}

.input-row {
    display: flex;
    gap: 0.75rem;
    align-items: flex-end;
}

#attached-images-container {
    margin-bottom: 0.75rem;
}

.attached-images {
    display: flex;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.attached-image {
    position: relative;
    width: 56px;
    height: 56px;
    animation: image-attach 0.3s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes image-attach {
    from {
        opacity: 0;
        transform: scale(0.8);
    }
    to {
        opacity: 1;
        transform: scale(1);
    }
}

.attached-image img {
    width: 100%;
    height: 100%;
    object-fit: cover;
    border-radius: var(--radius-sm);
    border: 1px solid var(--border);
}

.attached-image .remove-btn {
    position: absolute;
    top: -6px;
    right: -6px;
    width: 20px;
    height: 20px;
    background: var(--accent);
    color: #0d0d0f;
    border: none;
    border-radius: 50%;
    font-size: 11px;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: var(--transition);
    transform: scale(0.8);
}

.attached-image:hover .remove-btn {
    opacity: 1;
    transform: scale(1);
}

.attached-image .remove-btn:hover {
    background: var(--error);
    color: #fff;
}

#image-btn {
    padding: 0;
    width: 50px;
    height: 50px;
    background: var(--bg-secondary);
    color: var(--text-secondary);
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
}

#image-btn:hover {
    background: var(--bg-tertiary);
    border-color: var(--border-hover);
    color: var(--text-primary);
    transform: translateY(-1px);
}

#image-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#image-input {
    display: none;
}

#prompt-input {
    flex: 1;
    padding: 0.875rem 1.125rem;
    border: 1px solid var(--border);
    border-radius: var(--radius-md);
    background: var(--bg-input);
    color: var(--text-primary);
    font-size: 0.95rem;
    font-family: var(--font-body);
    resize: none;
    height: 50px;
    min-height: 50px;
    max-height: 160px;
    line-height: 1.3;
    transition: var(--transition);
}

#prompt-input::placeholder {
    color: var(--text-muted);
    font-style: italic;
}

#prompt-input:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow:
        0 0 0 3px var(--accent-soft),
        0 0 20px var(--accent-soft);
}

#send-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    padding: 0.875rem 1.375rem;
    height: 50px;
    background: linear-gradient(135deg, var(--accent) 0%, #b89858 100%);
    color: #0d0d0f;
    border: none;
    border-radius: var(--radius-md);
    font-size: 0.95rem;
    font-weight: 500;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
    flex-shrink: 0;
}

#send-btn:hover:not(:disabled) {
    background: linear-gradient(
        135deg,
        var(--accent-hover) 0%,
        var(--accent) 100%
    );
    transform: translateY(-1px);
    box-shadow: 0 0 24px var(--accent-soft);
}

#send-btn:active:not(:disabled) {
    transform: translateY(0);
}

#send-btn:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

#send-btn:focus-visible,
#login-btn:focus-visible,
#logout-btn:focus-visible,
#auth-login-btn:focus-visible {
    outline: 2px solid var(--accent);
    outline-offset: 2px;
}

#send-btn .btn-text {
    display: inline;
}

#send-btn svg {
    transition: transform 0.2s;
}

#send-btn:hover:not(:disabled) svg {
    transform: translateX(2px);
}

.auth-prompt {
    flex: 1;
    display: none;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 3rem 2rem;
    text-align: center;
    background: radial-gradient(
        ellipse at center,
        var(--bg-secondary) 0%,
        var(--bg-primary) 70%
    );
}

.auth-content {
    max-width: 400px;
    animation: fade-up 0.6s cubic-bezier(0.16, 1, 0.3, 1);
}

@keyframes fade-up {
    from {
        opacity: 0;
        transform: translateY(20px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.auth-ornament {
    display: block;
    font-size: 2rem;
    color: var(--gold);
    margin-bottom: 1.5rem;
    animation: float 4s ease-in-out infinite;
    text-shadow: 0 0 30px var(--gold);
}

@keyframes float {
    0%,
    100% {
        transform: translateY(0);
    }
    50% {
        transform: translateY(-8px);
    }
}

.auth-prompt h2 {
    font-family: var(--font-display);
    font-size: 2rem;
    font-weight: 500;
    color: var(--text-primary);
    margin-bottom: 1rem;
    letter-spacing: 0.02em;
}

.auth-prompt p {
    font-size: 1rem;
    color: var(--text-secondary);
    line-height: 1.7;
    margin-bottom: 2rem;
}

#auth-login-btn {
    padding: 0.875rem 2rem;
    background: linear-gradient(135deg, var(--accent) 0%, #b89858 100%);
    color: #0d0d0f;
    border: none;
    border-radius: var(--radius-sm);
    font-size: 0.95rem;
    font-weight: 500;
    font-family: var(--font-body);
    cursor: pointer;
    transition: var(--transition);
}

#auth-login-btn:hover {
    background: linear-gradient(
        135deg,
        var(--accent-hover) 0%,
        var(--accent) 100%
    );
    transform: translateY(-2px);
    box-shadow: 0 0 30px var(--accent-soft);
}

.hidden {
    display: none !important;
}

@media (max-width: 768px) {
    .container {
        box-shadow: none;
    }

    header {
        padding: 1rem 1.25rem;
    }

    h1 {
        font-size: 1.25rem;
    }

    .user-name {
        display: none;
    }

    #chat-container {
        padding: 1.25rem;
    }

    .message {
        max-width: 85%;
        padding: 0.875rem 1rem;
        font-size: 0.9rem;
    }

    #input-container {
        padding: 1rem 1.25rem 1.25rem;
    }

    #prompt-input {
        font-size: 0.9rem;
        padding: 0.75rem 1rem;
    }

    #send-btn {
        padding: 0.75rem 1rem;
        font-size: 0.9rem;
    }

    #send-btn .btn-text {
        display: none;
    }

    #send-btn svg {
        margin: 0;
    }

    #image-btn {
        padding: 0.625rem;
    }

    .auth-prompt h2 {
        font-size: 1.5rem;
    }

    .auth-prompt p {
        font-size: 0.9rem;
    }
}

@media (max-width: 480px) {
    header {
        padding: 0.875rem 1rem;
    }

    h1 {
        font-size: 1.1rem;
    }

    #login-btn,
    #logout-btn {
        padding: 0.4rem 0.875rem;
        font-size: 0.8rem;
    }

    #session-selector {
        padding: 0.4rem 0.5rem;
        font-size: 0.8rem;
    }

    #chat-container {
        padding: 1rem;
        gap: 1.25rem;
    }

    .message {
        max-width: 88%;
        padding: 0.75rem 0.875rem;
        font-size: 0.875rem;
    }

    #input-container {
        padding: 0.875rem 1rem 1rem;
    }

    #prompt-input {
        min-height: 46px;
        font-size: 0.875rem;
        padding: 0.625rem 0.875rem;
    }

    #send-btn {
        padding: 0.625rem 0.875rem;
    }

    #image-btn {
        padding: 0.5rem;
    }

    .attached-image {
        width: 48px;
        height: 48px;
    }
}

@media (min-width: 1024px) {
    .container {
        max-width: 960px;
    }
}
