/**
 * Marky Mark — Layout
 * History sidebar, app container, visual stage, chat stage, resizer.
 */

/* --- Rail Tooltip (body-level, escapes sidebar overflow:hidden) --- */
.rail-tooltip {
    position: fixed;
    background: #111;
    color: #fff;
    font-size: 0.72rem;
    font-weight: 500;
    padding: 4px 9px;
    border-radius: 6px;
    white-space: nowrap;
    pointer-events: none;
    z-index: 9999;
    transform: translateY(-50%);
    transition: opacity 0.1s ease;
}

/* --- History Sidebar (Push Layout) --- */
#history-sidebar {
    position: fixed;
    left: 0;
    top: 0;
    width: var(--sidebar-width);
    height: 100%;
    background: var(--bg-secondary);
    border-right: 1px solid var(--border-color);
    transform: translateX(-100%);
    transition: transform 200ms ease;
    z-index: 2000;
    padding: 20px;
    box-sizing: border-box;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

#history-sidebar h3 {
    font-size: 0.72rem;
    font-weight: 600;
    color: var(--text-tertiary);
    text-transform: uppercase;
    letter-spacing: 0.06em;
    margin: 30px 0 6px;
}

#history-list {
    flex: 1;
    overflow-y: auto;
    margin: 0 -20px;
    padding: 0 12px;
    scrollbar-width: thin;
    scrollbar-color: rgba(0,0,0,0.08) transparent;
}

.history-empty {
    color: var(--text-tertiary);
    font-size: 0.83rem;
    text-align: center;
    padding-top: 20px;
    margin: 0;
}

.history-item {
    display: flex;
    align-items: center;
    border-radius: 8px;
    margin-bottom: 1px;
    transition: background 150ms ease;
}

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

.history-item.active {
    background: var(--bg-hover);
}

.history-item-content {
    flex: 1;
    min-width: 0;
    background: transparent;
    border: none;
    text-align: left;
    padding: 9px 8px 9px 10px;
    cursor: pointer;
    border-radius: 8px;
    font-family: inherit;
}

.history-title {
    display: block;
    font-size: 0.86rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.history-date {
    display: block;
    font-size: 0.71rem;
    color: var(--text-tertiary);
    margin-top: 2px;
}

.history-delete {
    flex-shrink: 0;
    background: transparent;
    border: none;
    width: 28px;
    height: 28px;
    border-radius: 6px;
    cursor: pointer;
    color: var(--text-tertiary);
    font-size: 1.1rem;
    display: flex;
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s, background 0.15s;
    margin-right: 4px;
}

.history-item:hover .history-delete {
    opacity: 1;
}

.history-delete:hover {
    color: #e74c3c;
    background: #ffeaea;
}

body.is-sidebar-open #history-sidebar {
    transform: translateX(0);
}

/* --- Main Layout --- */
#app-container {
    display: flex;
    height: 100dvh;
    width: 100vw;
    position: relative;
    margin-left: 0;
    transition: margin-left 200ms ease, width 200ms ease;
}

body.is-sidebar-open #app-container {
    margin-left: var(--sidebar-width);
    width: calc(100vw - var(--sidebar-width));
}

/* --- Visual Stage (Left when active) --- */
#visual-stage {
    /* Collapsed: takes zero layout space, content clipped */
    flex: 0 0 0;
    min-width: 0;
    height: 100%;
    background: var(--bg-color);
    position: relative;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* Animate the flex-grow so the layout itself expands,
       pushing the chat panel rightward — no layout jump */
    transition: flex-grow 280ms cubic-bezier(0.4, 0, 0.2, 1);
}

/* Expanded: equal 1:1 split with chat panel */
body.is-visual-open #visual-stage {
    flex-grow: 1;
    overflow: visible;
}

#jxgbox {
    width: 100%;
    flex: 1;
    min-height: 0;
    border: none !important;
    outline: none;
    position: relative; /* Needed for equation panel absolute positioning */
}

/* --- Resizer (Draggable) --- */
#resizer {
    /* Collapsed: 0 width, invisible */
    width: 0;
    overflow: hidden;
    cursor: col-resize;
    background: transparent;
    transition: width 280ms cubic-bezier(0.4, 0, 0.2, 1);
    z-index: 100;
    position: relative;
    flex-shrink: 0;
}

/* Expanded: 10px hit target, visually borderless */
body.is-visual-open #resizer {
    width: 10px;
    overflow: visible;
}

/* Visual divider — 1px line centred in the hit target */
#resizer::before {
    content: "";
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 2px;
    background: rgba(0, 0, 0, 0.174);
    box-shadow: 1px 0 5px rgba(0, 0, 0, 0.07);
    transition: background 0.15s, box-shadow 0.15s;
}

#resizer:hover::before {
    background: rgba(74, 144, 226, 0.55);
    box-shadow: 0 0 8px rgba(74, 144, 226, 0.18);
}

/* --- Chat Stage (Centered or Right) --- */
#chat-stage {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    height: 100%;
    position: relative;
    background: var(--bg-color);
}

/* Layout adjustments when graph is open */
body.is-visual-open #chat-stage {
    min-width: 320px;
    align-items: stretch;
}

#conversation-wrapper {
    width: 100%;
    height: 100%;
    display: flex;
    flex-direction: column;
    position: relative;
}

/* Anchor content from top when idle — rows expand downward without shifting input */
body.is-idle #conversation-wrapper {
    justify-content: flex-start;
    align-items: center;
    overflow-y: auto;
}

body.is-visual-open #conversation-wrapper {
    max-width: 100%;
}

/* --- Visual-Mode Floating Card Layout (desktop only, graph open) --- */
@media (min-width: 769px) {
    body.is-visual-open { background: var(--bg-secondary); }

    body.is-visual-open #app-container {
        padding: 12px;
        box-sizing: border-box;
    }

    body.is-visual-open #visual-stage,
    body.is-visual-open #chat-stage {
        border-radius: 14px;
        background: var(--bg-color);
        box-shadow: 0 2px 20px var(--shadow-md);
        overflow: hidden;
    }

    body.is-visual-open #resizer { background: var(--bg-secondary); }
}

/* --- Sidebar Mini-Rail (desktop only) --- */
@media (min-width: 769px) {
    /* Width-animated rail instead of off-screen transform */
    #history-sidebar {
        transform: none;
        width: 56px;
        padding: 0;
        overflow: hidden;
        transition: width 220ms cubic-bezier(0.4, 0, 0.2, 1);
    }
    body.is-sidebar-open #history-sidebar {
        width: var(--sidebar-width);
        transform: none;
    }

    /* App container always offset by rail width */
    #app-container {
        margin-left: 56px;
        width: calc(100vw - 56px);
    }
    body.is-sidebar-open #app-container {
        margin-left: var(--sidebar-width);
        width: calc(100vw - var(--sidebar-width));
    }

    /* No backdrop overlay on desktop (push layout) */
    #sidebar-backdrop { display: none !important; }

    /* Rail header — logo button pinned to left edge so it stays fixed as sidebar expands */
    .rail-header {
        display: flex;
        align-items: center;
        padding: 0 8px;
        height: 60px;
        flex-shrink: 0;
        border-bottom: 1px solid var(--border-color);
        margin-bottom: 0;
    }

    /* Logo button — contains logo img (visible) and panel icon (hidden, fades in on hover) */
    #menu-btn-rail {
        position: relative;
        background: transparent;
        border: none;
        cursor: pointer;
        padding: 9px 10px;
        border-radius: 8px;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: background 0.15s;
        flex-shrink: 0;
    }

    /* Shrink the logo in rail context to match action button icon size */
    .rail-header .menu-icon {
        width: 20px;
        height: 20px;
    }
    #menu-btn-rail:hover { background: var(--bg-hover); }

    /* Logo fades out / panel icon fades in on hover — collapsed only */
    #menu-btn-rail .menu-icon {
        transition: opacity 0.15s ease, transform 0.15s ease;
    }
    .rail-panel-icon {
        position: absolute;
        opacity: 0;
        transform: scale(0.85);
        transition: opacity 0.15s ease, transform 0.15s ease;
        color: var(--text-primary);
    }
    body:not(.is-sidebar-open) #menu-btn-rail:hover .menu-icon {
        opacity: 0;
        transform: scale(0.85);
    }
    body:not(.is-sidebar-open) #menu-btn-rail:hover .rail-panel-icon {
        opacity: 1;
        transform: scale(1);
    }
    /* When open: keep logo visible, panel icon suppressed (close btn handles collapsing) */
    body.is-sidebar-open #menu-btn-rail .menu-icon { opacity: 1; }
    body.is-sidebar-open #menu-btn-rail .rail-panel-icon { opacity: 0; transform: scale(0.85); }

    /* Collapse button — hidden until sidebar is open */
    #sidebar-close-btn {
        background: none;
        border: none;
        cursor: pointer;
        padding: 6px;
        border-radius: 7px;
        display: flex;
        align-items: center;
        justify-content: center;
        color: var(--text-tertiary);
        opacity: 0;
        pointer-events: none;
        transition: opacity 0.18s ease, background 0.15s, color 0.15s;
        margin-left: auto;
        flex-shrink: 0;
    }
    #sidebar-close-btn:hover { background: var(--bg-hover); color: var(--text-primary); }
    body.is-sidebar-open #sidebar-close-btn {
        opacity: 1;
        pointer-events: auto;
    }

    /* Sidebar action buttons wrapper */
    .sidebar-actions {
        padding: 12px 8px 0;   /* top gap from logo header; horizontal inset so hover doesn't touch edges */
        flex-shrink: 0;
    }

    /* Both action buttons: centred icon in mini mode, left-aligned label when open.
       Higher specificity (#history-sidebar prefix) beats the margin-top: 60px in chat.css */
    #history-sidebar #new-chat-btn,
    #history-sidebar #search-chats-btn {
        margin-top: 0;
        margin-bottom: 2px;    /* tight gap between New Chat and Search Chats */
        justify-content: center;
        padding: 9px 10px;
    }
    body.is-sidebar-open #history-sidebar #new-chat-btn,
    body.is-sidebar-open #history-sidebar #search-chats-btn {
        justify-content: flex-start;
        padding: 9px 10px 9px 14px;
    }

    /* Sidebar labels — hidden (zero-width) in mini mode so they don't push icons off-screen */
    .sidebar-label {
        display: none;
    }
    body.is-sidebar-open .sidebar-label {
        display: inline;
        white-space: nowrap;
    }

    /* History list — hidden in mini mode */
    #history-sidebar #history-list {
        opacity: 0;
        pointer-events: none;
        transition: opacity 120ms ease;
        padding: 0 8px;
        margin: 0;
    }
    body.is-sidebar-open #history-sidebar #history-list {
        opacity: 1;
        pointer-events: auto;
        transition: opacity 180ms ease 80ms;
    }
    body.is-sidebar-open #history-sidebar h3 { padding: 0 16px 4px; }

    /* Hide floating logo button on desktop — rail replaces it */
    .top-left-controls { display: none; }
}

/* --- Search Chats Button (sidebar) --- */
#search-chats-btn {
    display: flex;
    align-items: center;
    gap: 8px;
    width: 100%;
    background: transparent;
    border: none;
    cursor: pointer;
    font-size: 0.95rem;
    font-weight: 500;
    color: var(--text-primary);
    border-radius: 8px;
    transition: background 150ms ease;
}
#search-chats-btn:hover { background: var(--bg-hover); }

@media (min-width: 769px) {
    #search-chats-btn {
        justify-content: center;
        padding: 10px;
    }
    body.is-sidebar-open #search-chats-btn {
        justify-content: flex-start;
        padding: 10px 12px 10px 16px;
    }
}

/* --- Search Panel Overlay --- */
#search-panel {
    position: fixed;
    inset: 0;
    z-index: 2500;
    display: flex;
    align-items: flex-start;
    justify-content: center;
    padding-top: 80px;
    box-sizing: border-box;
}
#search-panel[hidden] { display: none; }

#search-backdrop {
    position: absolute;
    inset: 0;
    background: rgba(0, 0, 0, 0.18);
    backdrop-filter: blur(2px);
    -webkit-backdrop-filter: blur(2px);
}

#search-panel-card {
    position: relative;
    z-index: 1;
    background: var(--bg-color);
    border-radius: 14px;
    box-shadow: 0 12px 40px var(--shadow-md);
    width: 440px;
    max-width: calc(100vw - 32px);
    max-height: 70vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    animation: searchSlideIn 160ms cubic-bezier(0.4, 0, 0.2, 1);
}

@keyframes searchSlideIn {
    from { opacity: 0; transform: translateY(-8px) scale(0.98); }
    to   { opacity: 1; transform: translateY(0) scale(1); }
}

#search-input {
    border: none;
    border-bottom: 1px solid var(--border-subtle);
    padding: 16px 20px;
    font-size: 1rem;
    font-family: inherit;
    outline: none;
    background: transparent;
    flex-shrink: 0;
}

#search-results {
    overflow-y: auto;
    padding: 8px;
    scrollbar-width: thin;
    scrollbar-color: rgba(0, 0, 0, 0.08) transparent;
}

.search-result-item {
    display: flex;
    flex-direction: column;
    padding: 10px 12px;
    border-radius: 8px;
    cursor: pointer;
    transition: background 120ms ease;
    border: none;
    background: transparent;
    width: 100%;
    text-align: left;
    font-family: inherit;
}
.search-result-item:hover { background: var(--bg-hover); }

.search-result-title {
    font-size: 0.88rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.search-result-date {
    font-size: 0.75rem;
    color: var(--text-tertiary);
    margin-top: 2px;
}

.search-empty {
    color: var(--text-tertiary);
    font-size: 0.85rem;
    text-align: center;
    padding: 20px;
    margin: 0;
}

/* --- Sidebar Footer (LinkedIn link) --- */
.sidebar-footer {
    flex-shrink: 0;
    padding: 12px 8px 16px;
    border-top: 1px solid var(--border-color);
    margin-top: 8px;
}


/* Desktop rail: show just avatar in collapsed rail, full row in open sidebar */
@media (min-width: 769px) {
    .sidebar-footer {
        padding: 8px 4px 12px;
    }
    body.is-sidebar-open .sidebar-footer {
        padding: 8px 8px 16px;
    }

    /* Collapsed: hide name/plan info and upgrade pill — show avatar only */
    body:not(.is-sidebar-open) .sidebar-user-info,
    body:not(.is-sidebar-open) .sidebar-inline-upgrade {
        display: none;
    }

    /* Collapsed: centre the avatar button */
    body:not(.is-sidebar-open) .sidebar-user-row {
        justify-content: center;
    }
    body:not(.is-sidebar-open) .sidebar-account-btn {
        flex: 0 0 auto;
        padding: 5px;
        justify-content: center;
    }
}
