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

:root {
    --primary: #6366f1;
    --primary-dark: #4f46e5;
    --secondary: #8b5cf6;
    --success: #10b981;
    --danger: #ef4444;
    --warning: #f59e0b;
    --bg-dark: #0f172a;
    --bg-darker: #020617;
    --bg-card: #1e293b;
    --text-primary: #f1f5f9;
    --text-secondary: #94a3b8;
    --border: #334155;
}

html,
body {
    height: 100%;
    width: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
    /* Prevent body scroll */
    position: fixed;
    /* Fix body to prevent rubber-banding on iOS */

    /* Visual Styles */
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
    background: linear-gradient(135deg, var(--bg-darker) 0%, var(--bg-dark) 100%);
    color: var(--text-primary);
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

.container {
    display: flex;
    flex-direction: column;
    height: 100%;
    /* Fill available space (respects keyboard resize) */
    width: 100%;
}

/* Header */
.header {
    background: rgba(30, 41, 59, 0.95);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
    padding: 0.75rem 1rem;
    z-index: 100;
}

.header-content {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.logo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: absolute;
    left: 50%;
    transform: translateX(-50%);
}

.logo-icon {
    font-size: 1.5rem;
    animation: float 3s ease-in-out infinite;
}

@keyframes float {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-4px);
    }
}

.logo-text {
    font-size: 1.25rem;
    font-weight: 700;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    background-clip: text;
}

.expenses-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.3s ease;
    -webkit-tap-highlight-color: transparent;
}

.expenses-toggle:active {
    background: var(--bg-card);
    transform: scale(0.95);
}

.auth-button {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    border: none;
    padding: 0.5rem 1rem;
    border-radius: 10px;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    font-size: 0.8rem;
    white-space: nowrap;
}

.auth-button:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
}

.user-badge {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--bg-card);
    padding: 0.4rem 0.8rem;
    border-radius: 10px;
    border: 1px solid var(--border);
}

.user-avatar {
    width: 28px;
    height: 28px;
    border-radius: 50%;
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1rem;
}

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

.logout-btn {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 32px;
    height: 32px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: var(--text-secondary);
    padding: 0;
    -webkit-tap-highlight-color: transparent;
}

.logout-btn:hover {
    background: var(--bg-card);
    color: var(--danger);
    border-color: var(--danger);
}

.logout-btn:active {
    transform: scale(0.95);
}

#userInfo {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    position: relative;
}

/* Main Content - Mobile First */
.main-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 0;
    overflow: hidden;
    min-height: 0;
    /* Crucial for nested scrolling */
}

/* Chat Container */
.chat-container {
    flex: 1;
    display: flex;
    flex-direction: column;
    background: transparent;
    overflow: hidden;
    min-height: 0;
    /* Crucial for nested scrolling */
}

.chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    -webkit-overflow-scrolling: touch;
}

.chat-messages::-webkit-scrollbar {
    width: 4px;
}

.chat-messages::-webkit-scrollbar-track {
    background: transparent;
}

.chat-messages::-webkit-scrollbar-thumb {
    background: var(--border);
    border-radius: 2px;
}

.chat-messages::-webkit-scrollbar-thumb:hover {
    background: #475569;
}

/* Welcome Message */
.welcome-message {
    text-align: center;
    padding: 2rem 1rem;
    animation: fadeIn 0.5s ease;
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(15px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.welcome-icon {
    font-size: 3rem;
    margin-bottom: 0.75rem;
    animation: bounce 2s ease infinite;
}

@keyframes bounce {

    0%,
    100% {
        transform: translateY(0);
    }

    50% {
        transform: translateY(-8px);
    }
}

.welcome-message h2 {
    font-size: 1.5rem;
    margin-bottom: 0.5rem;
}

.welcome-message p {
    color: var(--text-secondary);
    font-size: 0.95rem;
    margin-bottom: 1.5rem;
    line-height: 1.5;
}

.examples {
    display: flex;
    flex-direction: column;
    gap: 0.6rem;
}

.example-item {
    background: var(--bg-card);
    padding: 0.85rem 1rem;
    border-radius: 10px;
    border: 1px solid var(--border);
    text-align: left;
    color: var(--text-secondary);
    transition: all 0.3s ease;
    cursor: pointer;
    font-size: 0.9rem;
}

.example-item:hover,
.example-item:active {
    border-color: var(--primary);
    color: var(--text-primary);
    transform: translateX(3px);
    background: rgba(99, 102, 241, 0.1);
}

/* Chat Messages */
.message {
    display: flex;
    gap: 0.6rem;
    animation: slideIn 0.3s ease;
}

@keyframes slideIn {
    from {
        opacity: 0;
        transform: translateX(-15px);
    }

    to {
        opacity: 1;
        transform: translateX(0);
    }
}

.message.user {
    flex-direction: row-reverse;
}

.message-avatar {
    width: 36px;
    height: 36px;
    border-radius: 50%;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.25rem;
}

.message.user .message-avatar {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
}

.message.assistant .message-avatar {
    background: var(--bg-card);
    border: 2px solid var(--border);
}

.message-content {
    max-width: 75%;
    padding: 0.85rem 1rem;
    border-radius: 14px;
    line-height: 1.5;
    font-size: 0.95rem;
}

.message.user .message-content {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
    border-bottom-right-radius: 4px;
}

.message.assistant .message-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-bottom-left-radius: 4px;
    color: var(--text-primary);
}

/* Input Area - Sticky Bottom */
.input-container {
    padding: 1rem;
    background: rgba(30, 41, 59, 0.95);
    backdrop-filter: blur(10px);
    border-top: 1px solid var(--border);
    /* Safe area inset for iPhone notch/home indicator */
    padding-bottom: calc(1rem + env(safe-area-inset-bottom));
}

.chat-form {
    display: flex;
    gap: 0.6rem;
    align-items: center;
}

.message-input {
    flex: 1;
    background: var(--bg-dark);
    border: 2px solid var(--border);
    border-radius: 10px;
    padding: 0.85rem 1rem;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    transition: all 0.3s ease;
    -webkit-appearance: none;
    appearance: none;
}

.message-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.message-input:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.send-button {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    border: none;
    border-radius: 10px;
    width: 46px;
    height: 46px;
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: white;
    -webkit-tap-highlight-color: transparent;
}

.send-button:active:not(:disabled) {
    transform: scale(0.95);
}

.send-button:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

/* Sidebar Overlay - Mobile Only */
.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    backdrop-filter: blur(4px);
    z-index: 998;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.sidebar-overlay.active {
    display: block;
    opacity: 1;
}

/* Sidebar - Mobile Drawer */
.sidebar {
    position: fixed;
    top: 0;
    left: -100%;
    width: 85%;
    max-width: 400px;
    height: 100vh;
    height: 100dvh;
    background: var(--bg-darker);
    z-index: 999;
    display: flex;
    flex-direction: column;
    transition: left 0.3s ease;
    overflow: hidden;
}

.sidebar.active {
    left: 0;
}

.sidebar-header {
    padding: 1rem;
    border-bottom: 1px solid var(--border);
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h3 {
    font-size: 1.1rem;
    font-weight: 600;
}

.sidebar-actions {
    display: flex;
    gap: 0.5rem;
}

.close-sidebar {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: var(--text-secondary);
    -webkit-tap-highlight-color: transparent;
}

.close-sidebar:active {
    background: var(--bg-card);
    transform: scale(0.95);
}

.refresh-button {
    background: transparent;
    border: 1px solid var(--border);
    border-radius: 8px;
    width: 36px;
    height: 36px;
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    transition: all 0.3s ease;
    color: var(--text-secondary);
    -webkit-tap-highlight-color: transparent;
}

.refresh-button:active {
    background: var(--bg-card);
    transform: scale(0.95) rotate(90deg);
}

.expenses-list {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    -webkit-overflow-scrolling: touch;
}

.empty-state {
    text-align: center;
    padding: 3rem 1rem;
    color: var(--text-secondary);
}

.empty-icon {
    font-size: 3rem;
    display: block;
    margin-bottom: 1rem;
    opacity: 0.5;
}

/* Expense Item */
.expense-item {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 0.85rem;
    transition: all 0.3s ease;
    animation: slideIn 0.3s ease;
}

.expense-item:hover,
.expense-item:active {
    border-color: var(--primary);
    transform: translateX(2px);
}

.expense-header {
    display: flex;
    justify-content: space-between;
    align-items: start;
    margin-bottom: 0.4rem;
}

.expense-category {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.8rem;
    font-weight: 500;
}

.expense-amount {
    font-size: 1rem;
    font-weight: 700;
    color: var(--text-primary);
}

.expense-description {
    color: var(--text-secondary);
    font-size: 0.85rem;
    margin-bottom: 0.4rem;
}

.expense-date {
    color: var(--text-secondary);
    font-size: 0.7rem;
}

/* Loading State */
.loading {
    display: flex;
    gap: 0.4rem;
    padding: 0.75rem;
}

.loading-dot {
    width: 7px;
    height: 7px;
    background: var(--text-secondary);
    border-radius: 50%;
    animation: loading 1.4s ease-in-out infinite;
}

.loading-dot:nth-child(1) {
    animation-delay: 0s;
}

.loading-dot:nth-child(2) {
    animation-delay: 0.2s;
}

.loading-dot:nth-child(3) {
    animation-delay: 0.4s;
}

@keyframes loading {

    0%,
    60%,
    100% {
        transform: translateY(0);
    }

    30% {
        transform: translateY(-8px);
    }
}

/* Profile Dropdown (Moved from bottom) */
.profile-dropdown {
    position: absolute;
    top: calc(100% + 0.5rem);
    right: 0;
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 12px;
    padding: 0.5rem;
    min-width: 220px;
    z-index: 1000;
    display: none;
    box-shadow: 0 4px 20px rgba(0, 0, 0, 0.3);
}

.profile-dropdown.active {
    display: block;
}

.profile-info {
    padding: 0.75rem;
}

.profile-name {
    font-weight: 600;
    color: var(--text-primary);
    margin-bottom: 0.25rem;
}

.profile-email {
    font-size: 0.85rem;
    color: var(--text-secondary);
}

.dropdown-divider {
    height: 1px;
    background: var(--border);
    margin: 0.5rem 0;
}

.dropdown-item {
    width: 100%;
    padding: 0.75rem;
    background: transparent;
    border: none;
    border-radius: 8px;
    color: var(--text-primary);
    cursor: pointer;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.95rem;
    text-align: left;
    font-family: inherit;
}

.dropdown-item:hover {
    background: var(--bg-dark);
}

.dropdown-item.logout {
    color: var(--danger);
}

/* Modals (Moved from bottom) */
.modal {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    z-index: 2000;
    align-items: center;
    justify-content: center;
}

.modal.active {
    display: flex;
}

.modal-content {
    background: var(--bg-card);
    border: 1px solid var(--border);
    border-radius: 16px;
    padding: 2rem;
    max-width: 400px;
    width: 90%;
    text-align: center;
}

.modal-icon {
    font-size: 3rem;
    margin-bottom: 1rem;
}

.modal-content h3 {
    margin-bottom: 1rem;
    color: var(--text-primary);
}

.modal-content p {
    margin-bottom: 1.5rem;
    color: var(--text-secondary);
    line-height: 1.5;
}

.modal-input {
    width: 100%;
    background: var(--bg-dark);
    border: 2px solid var(--border);
    border-radius: 10px;
    padding: 0.85rem 1rem;
    color: var(--text-primary);
    font-size: 1rem;
    font-family: inherit;
    margin-bottom: 1.5rem;
    transition: all 0.3s ease;
}

.modal-input:focus {
    outline: none;
    border-color: var(--primary);
    box-shadow: 0 0 0 3px rgba(99, 102, 241, 0.1);
}

.modal-buttons {
    display: flex;
    gap: 0.75rem;
}

.btn-primary-modal,
.btn-secondary-modal {
    flex: 1;
    padding: 0.75rem 1.5rem;
    border-radius: 10px;
    font-size: 0.95rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    border: none;
    font-family: inherit;
}

.btn-primary-modal {
    background: linear-gradient(135deg, var(--primary), var(--secondary));
    color: white;
}

.btn-primary-modal:hover {
    transform: translateY(-2px);
    box-shadow: 0 8px 20px rgba(99, 102, 241, 0.3);
}

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

.btn-secondary-modal:hover {
    background: var(--bg-dark);
    color: var(--text-primary);
}

/* Tablet and Desktop Overrides */
@media (min-width: 768px) {
    .header {
        padding: 1rem 2rem;
    }

    .header-content {
        max-width: 1600px;
        margin: 0 auto;
    }

    .logo {
        position: static;
        transform: none;
    }

    .logo-icon {
        font-size: 2rem;
    }

    .logo-text {
        font-size: 1.5rem;
    }

    .expenses-toggle {
        display: none;
        /* Hide on desktop */
    }

    .auth-button {
        padding: 0.75rem 1.5rem;
        font-size: 0.95rem;
    }

    .main-content {
        grid-template-columns: 1fr 350px;
        gap: 1.5rem;
        padding: 1.5rem 2rem;
        max-width: 1600px;
        margin: 0 auto;
        width: 100%;
        display: grid;
    }

    .chat-container {
        background: rgba(30, 41, 59, 0.5);
        backdrop-filter: blur(10px);
        border-radius: 24px;
        border: 1px solid var(--border);
    }

    .chat-messages {
        padding: 2rem;
        gap: 1rem;
    }

    .welcome-message {
        padding: 3rem 2rem;
    }

    .welcome-icon {
        font-size: 4rem;
    }

    .welcome-message h2 {
        font-size: 2rem;
    }

    .welcome-message p {
        font-size: 1.1rem;
        margin-bottom: 2rem;
    }

    .examples {
        max-width: 500px;
        margin: 0 auto;
        gap: 0.75rem;
    }

    .example-item {
        padding: 1rem 1.5rem;
    }

    .message-content {
        max-width: 70%;
        padding: 1rem 1.25rem;
        border-radius: 16px;
    }

    .input-container {
        padding: 1.5rem;
        background: var(--bg-card);
        border-top: 1px solid var(--border);
    }

    .chat-form {
        gap: 0.75rem;
    }

    .message-input {
        padding: 1rem 1.25rem;
    }

    .send-button {
        width: 52px;
        height: 52px;
    }

    .send-button:hover:not(:disabled) {
        transform: translateY(-2px);
        box-shadow: 0 10px 25px rgba(99, 102, 241, 0.3);
    }

    /* Show Sidebar on Desktop */
    .sidebar-overlay {
        display: none !important;
        /* No overlay on desktop */
    }

    .sidebar {
        position: static;
        left: auto;
        width: auto;
        max-width: none;
        height: auto;
        display: flex;
        background: rgba(30, 41, 59, 0.5);
        backdrop-filter: blur(10px);
        border-radius: 24px;
        border: 1px solid var(--border);
        padding: 1.5rem;
        flex-direction: column;
        overflow: hidden;
    }

    .sidebar.active {
        left: auto;
        /* Override mobile active state */
    }

    .close-sidebar {
        display: none;
        /* Hide close button on desktop */
    }

    .sidebar-header {
        padding: 0;
        border-bottom: none;
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-bottom: 1.5rem;
    }

    .sidebar-header h3 {
        font-size: 1.1rem;
        font-weight: 600;
    }

    .refresh-button {
        background: transparent;
        border: 1px solid var(--border);
        border-radius: 8px;
        width: 36px;
        height: 36px;
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        transition: all 0.3s ease;
        color: var(--text-secondary);
    }

    .refresh-button:hover {
        background: var(--bg-card);
        color: var(--text-primary);
        transform: rotate(90deg);
    }

    .expenses-list {
        flex: 1;
        overflow-y: auto;
        display: flex;
        flex-direction: column;
        gap: 0.75rem;
    }

    .expenses-list::-webkit-scrollbar {
        width: 6px;
    }

    .expenses-list::-webkit-scrollbar-track {
        background: transparent;
    }

    .expenses-list::-webkit-scrollbar-thumb {
        background: var(--border);
        border-radius: 3px;
    }

    .empty-state {
        text-align: center;
        padding: 3rem 1rem;
        color: var(--text-secondary);
    }

    .empty-icon {
        font-size: 3rem;
        display: block;
        margin-bottom: 1rem;
        opacity: 0.5;
    }
}