:root {
    --primary-bg: #ffffff;
    --secondary-bg: #f8f9fa;
    --text-color: #343a40;
    --accent-color: #0d6efd;
    --hover-bg: #e9ecef;
    --sidebar-width-expanded: 260px;
    --transition-speed: 0.3s;
}

body {
    margin: 0;
    font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, 'Open Sans', 'Helvetica Neue', sans-serif;
    background-color: var(--secondary-bg);
}

.app-container {
    display: flex;
}

/* --- Sidebar --- */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    z-index: 1000;
    height: 100vh;
    background-color: var(--primary-bg);
    border-right: 1px solid #dee2e6;
    padding: 1rem;
    padding-top: 5rem;
    box-sizing: border-box;

    width: var(--sidebar-width-expanded);
    transform: translateX(-100%);
    transition: transform var(--transition-speed) ease-in-out;

    overflow-x: hidden;
    white-space: nowrap;
}

.sidebar.expanded {
    transform: translateX(0);
}

.sidebar-toggle {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-color);
    padding: 8px;
    border-radius: 6px;

    position: fixed;
    top: 1rem;
    left: 1rem;
    z-index: 2000;
}

.sidebar-toggle:hover {
    background-color: var(--hover-bg);
}

/* --- Navigation Links --- */
.sidebar-links {
    list-style: none;
    padding: 0;
    margin: 0;
}

.sidebar-links a {
    display: flex;
    align-items: center;
    color: var(--text-color);
    text-decoration: none;
    padding: 0.8rem;
    border-radius: 8px;
    margin-bottom: 0.5rem;
    transition: background-color var(--transition-speed) ease;
}

.sidebar-links a:hover {
    background-color: var(--hover-bg);
}

.sidebar-links a.active {
    background-color: var(--accent-color);
    color: white;
}

.sidebar-links .icon {
    font-size: 1.5rem;
    min-width: 40px;
    display: inline-block;
    text-align: center;
}

.sidebar-links .text {
    margin-left: 1rem;
    font-weight: 500;
}

/* --- Saved Chats --- */
.saved-chat {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    padding: 0.3rem 0;
    position: relative;
}

.saved-chat a.sidebar-link {
    flex: 1;
    display: flex;
    align-items: center;
    overflow: hidden;
}

.chat-title {
    flex: 1;
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.chat-menu {
    position: relative;
    flex-shrink: 0;
    margin-left: 4px;
    opacity: 0;
    transition: opacity 0.2s ease;
}

.saved-chat:hover .chat-menu {
    opacity: 1;
}

.ellipsis-btn {
    background: none;
    border: none;
    font-size: 18px;
    cursor: pointer;
    padding: 4px 8px;
    color: var(--text-color);
}

.ellipsis-btn:hover {
    background-color: var(--hover-bg);
    border-radius: 4px;
}

.ellipsis-dropdown {
    position: absolute;
    right: 0;
    top: 100%;
    background: #fff;
    border: 1px solid #dee2e6;
    border-radius: 4px;
    box-shadow: 0 2px 6px rgba(0,0,0,0.1);
    min-width: 120px;
    z-index: 1500;
}

.ellipsis-dropdown button {
    display: block;
    width: 100%;
    background: none;
    border: none;
    text-align: left;
    padding: 8px 12px;
    cursor: pointer;
    font-size: 0.9rem;
}

.ellipsis-dropdown button:hover {
    background-color: var(--hover-bg);
}

.hidden { display: none; }

/* --- Dropdown Arrow --- */
.sidebar-links .arrow {
    margin-left: auto;
    transition: transform var(--transition-speed) ease;
}

.sidebar-dropdown.open > a .arrow {
    transform: rotate(180deg);
}

.submenu {
    list-style: none;
    padding-left: 1rem;
    overflow: hidden;
    max-height: 0;
    transition: max-height var(--transition-speed) ease-in-out;
}

.sidebar-dropdown.open .submenu {
    max-height: 200px;
}

.submenu-link {
    font-size: 0.9em;
    padding-left: calc(40px + 0.5rem);
}

/* --- Main Content --- */
.main-content {
    flex: 1;
    margin-left: var(--sidebar-width-expanded);
    transition: margin-left var(--transition-speed) ease-in-out;
    display: flex;
    flex-direction: column;
}

.sidebar:not(.expanded) + .main-content {
    margin-left: 0;
}

/* ========================= */
/* Responsive adjustments    */
/* ========================= */
@media (max-width: 768px) {
    .sidebar.expanded + .main-content { margin-left: 0; }
    .main-content { padding: 1rem; }
    .card-header { padding: 0.75rem 3rem; font-size: 0.95rem; }
    .message-form .message-input { font-size: 1rem; }
    .card-body.messages-box { padding: 0.5rem; }
}

@media (max-width: 480px) {
    .sidebar { --sidebar-width-expanded: 360px; }
    .main-content { padding: 0.5rem; }
    .message-text { padding: 8px 12px; font-size: 0.9rem; }
    .card-header strong, .card-header a { font-size: 0.9rem; }
}

/* --- Mobile: always show ellipsis --- */
@media (hover: none), (pointer: coarse), (max-width: 768px) {
    .chat-menu,
    .ellipsis-btn {
        opacity: 1 !important;
        visibility: visible !important;
    }

    .ellipsis-btn {
        display: inline-block !important;
        width: 32px;
        height: 32px;
        border-radius: 6px;
    }
}
