:root {
    --bg-color: #000000;
    --bg-panel: #111111;
    --accent: #007AFF;
    --accent-glow: rgba(0, 122, 255, 0.3);
    --text-primary: #ffffff;
    --text-muted: #888888;
    --bubble-user: #007AFF;
    --bubble-ai: #1c1c1e;
    --border: #333;
    --code-bg: #2d2d2d;
}

* {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

body {
    font-family: 'Inter', sans-serif;
    background-color: var(--bg-color);
    color: var(--text-primary);
    height: 100dvh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* PWA Safe Area - only top/sides, NOT bottom (input-area handles that) */
    padding-top: env(safe-area-inset-top);
    padding-left: env(safe-area-inset-left);
    padding-right: env(safe-area-inset-right);
}

/* SIDEBAR */
.sidebar {
    position: fixed;
    left: 0;
    top: 0;
    bottom: 0;
    width: 280px;
    background: #0a0a0a;
    border-right: 1px solid #222;
    display: flex;
    flex-direction: column;
    z-index: 100;
    transition: transform 0.3s ease;
}

.sidebar-header {
    padding: 16px;
    border-bottom: 1px solid #222;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.sidebar-header h2 {
    font-size: 1rem;
    font-weight: 500;
    color: #888;
}

.new-conv-btn {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 8px 14px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    transition: opacity 0.2s;
}

.new-conv-btn:hover {
    opacity: 0.85;
}

.conversation-list {
    flex: 1;
    overflow-y: auto;
    padding: 8px;
}

.conv-item {
    padding: 12px 14px;
    border-radius: 10px;
    cursor: pointer;
    transition: background 0.2s;
    margin-bottom: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.conv-item:hover {
    background: rgba(255, 255, 255, 0.05);
}

.conv-item.active {
    background: rgba(0, 122, 255, 0.15);
    border: 1px solid rgba(0, 122, 255, 0.3);
}

.conv-item-title {
    font-size: 0.9rem;
    color: #ddd;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    flex: 1;
}

.conv-item-date {
    font-size: 0.7rem;
    color: #666;
    margin-left: 8px;
    white-space: nowrap;
}

.conv-delete-btn {
    background: none;
    border: none;
    color: #666;
    cursor: pointer;
    padding: 4px;
    opacity: 0;
    transition: opacity 0.2s, color 0.2s;
}

.conv-item:hover .conv-delete-btn {
    opacity: 1;
}

.conv-delete-btn:hover {
    color: #ff4444;
}

.sidebar-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.6);
    z-index: 99;
}

.sidebar-overlay.active {
    display: block;
}

/* PWA Install Button */
.install-pwa-btn {
    width: calc(100% - 30px);
    margin: 15px;
    padding: 12px 16px;
    background: linear-gradient(135deg, var(--accent) 0%, #6d28d9 100%);
    border: none;
    border-radius: 10px;
    color: white;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 8px;
    transition: all 0.3s;
    box-shadow: 0 4px 12px rgba(139, 92, 246, 0.3);
}

.install-pwa-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(139, 92, 246, 0.4);
}

.install-pwa-btn:active {
    transform: translateY(0);
}

.install-pwa-btn svg {
    flex-shrink: 0;
}

/* Hamburger button */
.hamburger-btn {
    display: none;
    background: none;
    border: none;
    color: white;
    cursor: pointer;
    padding: 8px;
    margin-right: 8px;
}

/* Desktop: sidebar visible, push content */
@media (min-width: 769px) {
    .app-container {
        margin-left: 280px;
    }

    .hamburger-btn {
        display: none !important;
    }
}

/* Mobile: sidebar hidden by default, overlay mode */
@media (max-width: 768px) {
    .sidebar {
        transform: translateX(-100%);
    }

    .sidebar.open {
        transform: translateX(0);
    }

    .hamburger-btn {
        display: block;
    }

    .app-container {
        margin-left: 0;
    }
}

/* LOGIN */
.login-container {
    height: 100%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: radial-gradient(circle at center, #222 0%, #000 70%);
}

.login-box {
    background: var(--bg-panel);
    padding: 40px;
    border-radius: 20px;
    text-align: center;
    width: 90%;
    max-width: 340px;
    border: 1px solid var(--border);
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.5);
}

.login-icon {
    font-size: 3rem;
    margin-bottom: 20px;
}

.login-box h2 {
    font-weight: 500;
    margin-bottom: 10px;
}

.login-box p {
    color: var(--text-muted);
    font-size: 0.9rem;
    margin-bottom: 30px;
}

.login-form input {
    background: #222;
    border: 1px solid #333;
    color: white;
    font-size: 1.5rem;
    letter-spacing: 5px;
    text-align: center;
    padding: 15px;
    border-radius: 12px;
    width: 100%;
    margin-bottom: 20px;
    outline: none;
    transition: border 0.2s;
}

.login-form input:focus {
    border-color: var(--accent);
}

.login-form button {
    width: 100%;
    padding: 15px;
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 12px;
    font-size: 1rem;
    font-weight: 600;
    cursor: pointer;
}

.error-msg {
    color: #ff4444;
    margin-top: 15px;
    font-size: 0.9rem;
}

/* CHAT APP */
.app-container {
    display: flex;
    flex-direction: column;
    height: 100%;
    max-width: 800px;
    margin: 0 auto;
    width: 100%;
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
}

header {
    padding: calc(15px + env(safe-area-inset-top)) 20px 15px 20px;
    background: rgba(17, 17, 17, 0.8);
    backdrop-filter: blur(10px);
    border-bottom: 1px solid var(--border);
    display: flex;
    align-items: center;
    justify-content: space-between;
    z-index: 10;
}

.header-left {
    display: flex;
    align-items: center;
    gap: 12px;
}

.status-dot {
    width: 10px;
    height: 10px;
    background: #00ff88;
    border-radius: 50%;
    box-shadow: 0 0 10px #00ff88;
}

.header-info h1 {
    font-size: 1rem;
    font-weight: 600;
}

.header-info span {
    font-size: 0.75rem;
    color: var(--text-muted);
    display: block;
}

/* Model Selector */
.model-selector {
    background: #1a1a1a;
    border: 1px solid #333;
    color: var(--text-muted);
    font-size: 0.75rem;
    padding: 4px 8px;
    border-radius: 6px;
    cursor: pointer;
    outline: none;
    transition: border-color 0.2s, color 0.2s;
    max-width: 140px;
}

.model-selector:hover,
.model-selector:focus {
    border-color: var(--accent);
    color: white;
}

.model-selector option {
    background: #1a1a1a;
    color: white;
}

.header-actions {
    display: flex;
    align-items: center;
    gap: 15px;
}

.icon-btn {
    background: transparent;
    border: none;
    font-size: 1.2rem;
    cursor: pointer;
    opacity: 0.6;
    transition: opacity 0.2s, transform 0.2s;
    color: white !important;
}

.icon-btn svg {
    stroke: white !important;
    fill: none;
}

.icon-btn:hover {
    opacity: 1;
    transform: scale(1.1);
}

.logout-btn {
    color: var(--text-muted);
    transition: color 0.2s;
    display: flex;
    align-items: center;
}

.logout-btn:hover {
    color: white;
}

/* MESSAGES */
#chat-messages {
    flex: 1;
    overflow-y: auto;
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

/* PWA Standalone: Add massive bottom padding so content is scrollable above input (MOBILE ONLY) */
@media (display-mode: standalone) and (max-width: 768px) {
    #chat-messages {
        padding-bottom: 160px;
    }
}

.message {
    display: flex;
    width: 100%;
}

.message.user {
    justify-content: flex-end;
}

.message.assistant {
    justify-content: flex-start;
}

.message.system {
    justify-content: center;
}

.message.system p {
    font-size: 0.75rem;
    color: #444;
    background: #111;
    padding: 5px 12px;
    border-radius: 20px;
}

.bubble {
    padding: 10px 14px;
    max-width: 85%;
    border-radius: 16px;
    font-size: 0.92rem;
    line-height: 1.45;
    position: relative;
    word-wrap: break-word;
    white-space: pre-wrap;
}

.message.user .bubble {
    background: var(--bubble-user);
    color: white;
    border-bottom-right-radius: 4px;
}

.message.assistant .bubble {
    background: var(--bubble-ai);
    color: #eee;
    border-bottom-left-radius: 4px;
}

/* CODE BLOCKS & COPY BUTTONS */
.code-block {
    background: #151515;
    border-radius: 12px;
    margin: 12px 0;
    overflow: hidden;
    /* Ensures header stays inside rounded corners */
    border: 1px solid #333;
    box-shadow: 0 4px 15px rgba(0, 0, 0, 0.3);
    position: relative;
    transition: border-color 0.2s;
}

.code-block:hover {
    border-color: #444;
}

.code-header {
    background: rgba(255, 255, 255, 0.03);
    padding: 8px 12px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    /* Space for lang label if added later */
    border-bottom: 1px solid #2a2a2a;
}

/* Optional label to make it look techy */
.code-header::before {
    content: 'Code / Prompt';
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 1px;
    color: #666;
    font-weight: 600;
}

.copy-btn {
    background: rgba(0, 122, 255, 0.1);
    border: 1px solid rgba(0, 122, 255, 0.3);
    color: var(--accent);
    font-size: 0.75rem;
    padding: 4px 12px;
    border-radius: 20px;
    /* Pill shape */
    cursor: pointer;
    font-weight: 500;
    transition: all 0.2s ease;
    display: flex;
    align-items: center;
    gap: 6px;
}

.copy-btn:hover {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
    box-shadow: 0 0 10px var(--accent-glow);
}

.code-content {
    padding: 15px;
    font-family: 'Menlo', 'Monaco', 'Consolas', 'Courier New', monospace;
    font-size: 0.9rem;
    line-height: 1.6;
    overflow-x: auto;
    background: #0d0d0d;
    /* Slightly darker than block for depth */
    color: #e0e0e0;
    margin: 0;
    white-space: pre-wrap;
}

/* Scrollbar for code blocks */
.code-content::-webkit-scrollbar {
    height: 8px;
}

.code-content::-webkit-scrollbar-track {
    background: #111;
}

.code-content::-webkit-scrollbar-thumb {
    background: #333;
    border-radius: 4px;
}

.code-content::-webkit-scrollbar-thumb:hover {
    background: #555;
}

/* LINKS */
.chat-link {
    color: var(--accent);
    text-decoration: none;
    border-bottom: 1px dashed rgba(0, 122, 255, 0.4);
    transition: border-bottom-style 0.2s;
}

.chat-link:hover {
    border-bottom-style: solid;
    text-shadow: 0 0 8px var(--accent-glow);
}

/* INPUT AREA */
.input-area {
    padding: 15px;
    padding-bottom: calc(15px + env(safe-area-inset-bottom));
    background: var(--bg-panel);
    border-top: 1px solid var(--border);
    position: sticky;
    bottom: 0;
    z-index: 50;
}

/* PWA Standalone Mode: Android nav bar overlays content, need MASSIVE padding (MOBILE ONLY) */
@media (display-mode: standalone) and (max-width: 768px) {
    .input-area {
        padding-bottom: 20px;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
    }
}

#chat-form {
    display: flex;
    gap: 10px;
    background: #222;
    padding: 8px;
    border-radius: 25px;
}

#message-input {
    flex: 1;
    background: transparent;
    border: none;
    color: white;
    padding: 10px 15px;
    font-size: 1rem;
    outline: none;
}

#send-btn {
    background: var(--accent);
    color: white;
    border: none;
    width: 40px;
    height: 40px;
    border-radius: 50%;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    transition: transform 0.2s;
}

#send-btn:hover {
    transform: scale(1.05);
}

/* Typing Dots */
.typing .dot {
    animation: bounce 1.4s infinite ease-in-out both;
    display: inline-block;
    color: var(--text-muted);
}

.typing .dot:nth-child(1) {
    animation-delay: -0.32s;
}

.typing .dot:nth-child(2) {
    animation-delay: -0.16s;
}

@keyframes bounce {

    0%,
    80%,
    100% {
        transform: scale(0);
    }

    40% {
        transform: scale(1);
    }
}

@media (max-width: 600px) {
    .app-container {
        border: none;
    }
}

/* Link Wrapper & Copy Button */
.link-wrapper {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    /* Increased gap */
    background: rgba(255, 255, 255, 0.05);
    padding: 4px 8px;
    border-radius: 6px;
    margin: 2px 0;
}

.copy-link-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    cursor: pointer;
    font-size: 1.1rem;
    /* Slightly bigger icon */
    padding: 4px;
    border-radius: 4px;
    line-height: 1;
    transition: all 0.2s;
    display: flex;
    /* Fix vertical align */
}

.copy-link-btn:hover {
    background: rgba(255, 255, 255, 0.2);
    transform: scale(1.1);
}

/* Ensure links truncate if too long on mobile */
.chat-link {
    max-width: 250px;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    display: inline-block;
    vertical-align: middle;
}

/* LIBRARY / BOOKMARKS */
.star-btn {
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 1.1rem;
    opacity: 0.4;
    transition: all 0.2s;
    padding: 2px;
}

.star-btn:hover,
.star-btn.saved {
    opacity: 1;
    transform: scale(1.2);
    filter: drop-shadow(0 0 5px gold);
}

.library-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
    background: #252525;
    width: 92%;
    max-width: 500px;
    max-height: 75vh;
    border-radius: 16px;
    border: 1px solid #444;
    box-shadow: 0 10px 40px rgba(0, 0, 0, 0.6);
    z-index: 1000;
    display: none;
    flex-direction: column;
}

.library-modal.active {
    display: flex;
}

.library-header {
    padding: 16px 20px;
    border-bottom: 1px solid #333;
    display: flex;
    justify-content: space-between;
    align-items: center;
    background: rgba(255, 255, 255, 0.02);
    border-radius: 16px 16px 0 0;
}

.library-header h2 {
    margin: 0;
    font-size: 1.1rem;
    color: var(--accent);
}

.close-modal {
    background: transparent;
    border: none;
    color: #888;
    font-size: 1.8rem;
    cursor: pointer;
    padding: 5px 10px;
    line-height: 1;
}

.close-modal:active {
    color: white;
}

.library-content {
    overflow-y: auto;
    padding: 12px;
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    flex: 1;
    -webkit-overflow-scrolling: touch;
}

.bookmark-item {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid #555;
    border-radius: 10px;
    padding: 14px;
    margin-bottom: 12px;
    position: relative;
    transition: border-color 0.2s;
}

.bookmark-item:hover,
.bookmark-item:active {
    border-color: #555;
}

.bookmark-text {
    font-family: 'SF Mono', 'Monaco', 'Consolas', monospace;
    font-size: 0.8rem;
    color: #ddd;
    margin-bottom: 35px;
    white-space: pre-wrap;
    word-break: break-word;
    line-height: 1.5;
}

.bookmark-actions {
    position: absolute;
    bottom: 10px;
    right: 10px;
    display: flex;
    gap: 10px;
}

.bookmark-btn {
    background: rgba(255, 255, 255, 0.1);
    border: none;
    border-radius: 6px;
    padding: 8px 14px;
    font-size: 0.8rem;
    cursor: pointer;
    color: #aaa;
    min-height: 36px;
}

.bookmark-btn:hover,
.bookmark-btn:active {
    background: var(--accent);
    color: white;
}

.bookmark-btn.delete:hover,
.bookmark-btn.delete:active {
    background: #ff4444;
}

.empty-library {
    text-align: center;
    color: #666;
    padding: 50px 20px;
    font-size: 0.95rem;
}

/* Add Overlay */
.modal-overlay {
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(2px);
    z-index: 999;
    display: none;
}

.modal-overlay.active {
    display: block;
}

/* Library Input */
.library-add {
    display: flex;
    gap: 10px;
    padding: 12px 15px;
    border-bottom: 1px solid #333;
    background: rgba(0, 0, 0, 0.2);
}

.library-add input {
    flex: 1;
    background: #222;
    border: 1px solid #444;
    border-radius: 8px;
    padding: 10px 14px;
    color: white;
    font-size: 0.9rem;
    outline: none;
}

.library-add input:focus {
    border-color: var(--accent);
}

.library-add button {
    background: var(--accent);
    color: white;
    border: none;
    border-radius: 8px;
    padding: 10px 18px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
}

.library-add button:active {
    opacity: 0.8;
}

/* PROFILE SWITCHER */
.profile-switcher {
    display: flex;
    justify-content: center;
    gap: 15px;
    margin-bottom: 10px;
}

.profile-option {
    position: relative;
    cursor: pointer;
}

.profile-option input {
    display: none;
}

.profile-label {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 8px 16px;
    background: rgba(255, 255, 255, 0.05);
    border: 1px solid #333;
    border-radius: 20px;
    font-size: 0.85rem;
    color: #888;
    transition: all 0.3s;
}

.profile-option input:checked+.profile-label {
    background: var(--accent);
    color: white;
    border-color: var(--accent);
    box-shadow: 0 0 10px rgba(83, 189, 235, 0.3);
}

.profile-label:hover {
    background: rgba(255, 255, 255, 0.1);
}

/* TAG BUILDER */
.tag-bar {
    display: flex;
    overflow-x: auto;
    gap: 8px;
    padding: 10px 15px;
    background: #0a0a0a;
    border-top: 1px solid #222;
    border-bottom: 1px solid #222;
    white-space: nowrap;
    scrollbar-width: none;
    /* Firefox */
}

.tag-bar::-webkit-scrollbar {
    display: none;
}

/* Chrome */

.tag-category {
    font-size: 0.75rem;
    color: #555;
    margin-right: 5px;
    display: flex;
    align-items: center;
    font-weight: bold;
    text-transform: uppercase;
}

.tag-chip {
    background: rgba(255, 255, 255, 0.08);
    border: 1px solid #333;
    padding: 5px 12px;
    border-radius: 15px;
    font-size: 0.8rem;
    color: #aaa;
    cursor: pointer;
    transition: all 0.2s;
    user-select: none;
}

.tag-chip:hover {
    background: var(--accent);
    color: white;
    transform: translateY(-1px);
    box-shadow: 0 4px 10px rgba(0, 0, 0, 0.3);
}

.tag-chip:active {
    transform: scale(0.95);
}

/* ROLE MODAL */
.role-modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.7);
    z-index: 999;
    opacity: 0 !important;
    visibility: hidden !important;
    transition: all 0.3s ease;
}

.role-modal-overlay.active {
    opacity: 1 !important;
    visibility: visible !important;
}

.role-modal {
    position: fixed;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%) scale(0.9);
    background: #1a1a1a;
    border: 1px solid #333;
    border-radius: 16px;
    width: 90%;
    max-width: 450px;
    margin: 0 10px;
    /* Prevent touching edges */
    z-index: 1000;
    opacity: 0 !important;
    visibility: hidden !important;
    transition: all 0.3s ease;
    /* PWA safe-area support */
    max-height: calc(100vh - env(safe-area-inset-top) - env(safe-area-inset-bottom) - 40px);
    overflow-y: auto;
}

.role-modal.active {
    opacity: 1 !important;
    visibility: visible !important;
    transform: translate(-50%, -50%) scale(1);
}

.role-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 16px 20px;
    border-bottom: 1px solid #333;
}

.role-header h2 {
    font-size: 1.1rem;
    font-weight: 600;
}

.role-content {
    padding: 20px;
}

.role-help {
    font-size: 0.85rem;
    color: #888;
    margin-bottom: 15px;
}

.role-preset {
    width: 100%;
    background: #111;
    border: 1px solid #333;
    border-radius: 10px;
    padding: 14px 12px;
    /* Increased for better touch targets */
    min-height: 44px;
    /* Apple minimum touch target */
    color: white;
    font-size: 0.95rem;
    font-family: inherit;
    outline: none;
    margin-bottom: 15px;
    cursor: pointer;
    transition: border-color 0.2s;
}

.role-preset:hover {
    border-color: #555;
}

.role-preset:focus {
    border-color: var(--accent);
}

.role-preset option {
    background: #111;
    color: white;
    padding: 10px;
    /* Better touch for options */
}



#role-input {
    width: 100%;
    background: #111;
    border: 1px solid #333;
    border-radius: 10px;
    padding: 12px;
    color: white;
    font-size: 0.95rem;
    font-family: inherit;
    resize: vertical;
    min-height: 80px;
    outline: none;
    transition: border-color 0.2s;
}

#role-input:focus {
    border-color: var(--accent);
}

#role-input::placeholder {
    color: #555;
}

.role-actions {
    display: flex;
    gap: 10px;
    margin-top: 15px;
    justify-content: flex-end;
}

.role-clear {
    background: #333;
    color: #aaa;
    border: none;
    padding: 10px 20px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    transition: background 0.2s;
}

.role-clear:hover {
    background: #444;
}

.role-save {
    background: var(--accent);
    color: white;
    border: none;
    padding: 10px 25px;
    border-radius: 8px;
    cursor: pointer;
    font-size: 0.9rem;
    font-weight: 500;
    transition: opacity 0.2s;
}

.role-save:hover {
    opacity: 0.9;
}

/* Role button indicator when role is active */
.role-btn.has-role {
    color: var(--accent) !important;
}

.role-btn.has-role svg {
    stroke: var(--accent) !important;
}

/* Mobile optimization for very small screens */
@media (max-width: 400px) {
    .role-modal {
        width: calc(100% - 40px);
        /* 20px margin on each side */
        max-width: none;
    }

    .role-header h2 {
        font-size: 1rem;
        /* Slightly smaller on tiny screens */
    }

    .role-preset,
    #role-input {
        font-size: 0.9rem;
        /* Better fit on small screens */
    }
}