/* =========================================================
   XERION — LAYOUT
========================================================== */


/* =========================================================
   APP SHELL
========================================================== */

#xerion-app {
    position: relative;

    width: 100%;
    min-height: 100vh;

    display: flex;

    overflow: hidden;

    background: var(--background);
}


/* =========================================================
   SIDEBAR
========================================================== */

.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;

    width: var(--sidebar-width);

    display: flex;
    flex-direction: column;

    border-right: 1px solid var(--border);

    background: var(--white);

    z-index: 100;

    transition:
        width var(--transition-slow),
        transform var(--transition-slow);
}


/* =========================================================
   SIDEBAR HEADER
========================================================== */

.sidebar-header {
    height: var(--topbar-height);

    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 0 16px;

    border-bottom: 1px solid var(--border);
}

.sidebar-brand {
    min-width: 0;

    display: inline-flex;
    align-items: center;

    color: var(--black);

    transition: opacity var(--transition);
}

.sidebar-brand:hover {
    opacity: 0.72;
}

.sidebar-brand-logo {
    width: 104px;
    height: auto;
    max-height: 32px;

    object-fit: contain;
}

.sidebar-collapse {
    width: 32px;
    height: 32px;

    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 5px;

    border: 1px solid transparent;
    border-radius: var(--radius-small);

    background: transparent;
    color: var(--muted);

    cursor: pointer;

    transition:
        background var(--transition),
        color var(--transition),
        border-color var(--transition);
}

.sidebar-collapse:hover {
    border-color: var(--border);
    background: var(--surface-soft);
    color: var(--text);
}

.sidebar-collapse span {
    width: 13px;
    height: 1.5px;

    border-radius: 999px;

    background: currentColor;
}


/* =========================================================
   SIDEBAR NEW CHAT
========================================================== */

.sidebar-new-chat {
    padding: 14px 12px 8px;
}

.new-chat-button {
    width: 100%;
    min-height: 42px;

    display: flex;
    align-items: center;
    gap: 10px;

    padding: 0 13px;

    border: 1px solid var(--border-strong);
    border-radius: var(--radius-small);

    background: var(--white);
    color: var(--text);

    font-size: 13px;
    font-weight: 600;

    cursor: pointer;

    transition:
        background var(--transition),
        border-color var(--transition),
        box-shadow var(--transition),
        transform var(--transition);
}

.new-chat-button:hover {
    border-color: #b5c1d0;
    background: var(--surface-soft);
    box-shadow: var(--shadow-xs);

    transform: translateY(-1px);
}

.new-chat-button:active {
    transform: translateY(0);
}

.new-chat-icon {
    width: 18px;
    height: 18px;

    display: flex;
    align-items: center;
    justify-content: center;

    color: var(--blue);
}

.new-chat-icon svg {
    width: 17px;
    height: 17px;
}


/* =========================================================
   SIDEBAR NAVIGATION
========================================================== */

.sidebar-navigation {
    flex: 1;

    min-height: 0;

    padding: 8px 10px 12px;

    overflow-y: auto;
    overflow-x: hidden;
}

.sidebar-section {
    margin-bottom: 18px;
}

.sidebar-section:last-child {
    margin-bottom: 0;
}

.sidebar-section-label {
    display: block;

    padding: 0 10px 7px;

    color: var(--muted-light);

    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.11em;
    line-height: 1.2;

    text-transform: uppercase;
}


/* =========================================================
   SIDEBAR ITEMS
========================================================== */

.sidebar-item {
    width: 100%;
    min-height: 40px;

    display: flex;
    align-items: center;
    gap: 11px;

    padding: 0 10px;

    border: 1px solid transparent;
    border-radius: var(--radius-small);

    background: transparent;
    color: var(--text-soft);

    font-size: 13px;
    font-weight: 500;

    text-align: left;

    cursor: pointer;

    transition:
        background var(--transition),
        border-color var(--transition),
        color var(--transition);
}

.sidebar-item:hover {
    background: var(--surface-hover);
    color: var(--text);
}

.sidebar-item.is-active {
    border-color: #e1e7f0;
    background: var(--blue-light);
    color: var(--blue);
    font-weight: 600;
}

.sidebar-item-icon {
    width: 19px;
    height: 19px;

    display: flex;
    align-items: center;
    justify-content: center;

    flex-shrink: 0;

    color: currentColor;
}

.sidebar-item-icon svg {
    width: 18px;
    height: 18px;
}

.sidebar-item-label {
    min-width: 0;

    overflow: hidden;

    white-space: nowrap;
    text-overflow: ellipsis;
}


/* =========================================================
   SIDEBAR BOTTOM
========================================================== */

.sidebar-bottom {
    padding: 10px;

    border-top: 1px solid var(--border);

    background: var(--white);
}


/* =========================================================
   SIDEBAR USER
========================================================== */

.sidebar-user-wrap {
    position: relative;
}

.sidebar-user {
    width: 100%;
    min-height: 48px;

    display: flex;
    align-items: center;
    gap: 10px;

    margin-top: 6px;
    padding: 5px 7px;

    border: 1px solid transparent;
    border-radius: var(--radius-small);

    background: transparent;
    color: var(--text);

    text-align: left;

    cursor: pointer;

    transition:
        background var(--transition),
        border-color var(--transition);
}

.sidebar-user:hover {
    border-color: var(--border);
    background: var(--surface-soft);
}

.sidebar-user-avatar {
    width: 32px;
    height: 32px;

    display: flex;
    align-items: center;
    justify-content: center;

    flex-shrink: 0;

    border: 1px solid var(--border);
    border-radius: 50%;

    background: var(--blue-light);
    color: var(--blue);

    font-size: 12px;
    font-weight: 700;
}

.sidebar-user-info {
    min-width: 0;

    flex: 1;

    display: flex;
    flex-direction: column;
}

.sidebar-user-name,
.sidebar-user-email {
    overflow: hidden;

    white-space: nowrap;
    text-overflow: ellipsis;
}

.sidebar-user-name {
    color: var(--text);
    font-size: 12px;
    font-weight: 600;
    line-height: 1.35;
}

.sidebar-user-email {
    margin-top: 2px;

    color: var(--muted);
    font-size: 10px;
    line-height: 1.3;
}

.sidebar-user-chevron {
    width: 18px;
    height: 18px;

    display: flex;
    align-items: center;
    justify-content: center;

    flex-shrink: 0;

    color: var(--muted-light);
}

.sidebar-user-chevron svg {
    width: 16px;
    height: 16px;
}


/* =========================================================
   MAIN AREA
========================================================== */

.app-main {
    width: calc(100% - var(--sidebar-width));
    min-width: 0;
    min-height: 100vh;

    margin-left: var(--sidebar-width);

    display: flex;
    flex-direction: column;

    transition:
        width var(--transition-slow),
        margin-left var(--transition-slow);
}


/* =========================================================
   TOPBAR
========================================================== */

.app-topbar {
    position: sticky;
    top: 0;

    width: 100%;
    height: var(--topbar-height);

    display: flex;
    align-items: center;
    justify-content: space-between;

    padding: 0 22px;

    border-bottom: 1px solid var(--border);

    background: var(--surface-translucent);

    /* Quick fix: was 50, well below .sidebar's mobile-drawer z-index
       (1000) — the topbar (and the hamburger button inside it) was
       being visually covered by the open drawer, so a second tap on
       the same button to close it never reached the button at all.
       Harmless at desktop widths, where .sidebar isn't position:fixed
       and this never collides with anything. */
    z-index: 1001;
}

.topbar-left,
.topbar-right {
    display: flex;
    align-items: center;
}

.topbar-left {
    min-width: 0;
}

.topbar-right {
    gap: 8px;
}

.view-title {
    color: var(--text);
    font-size: 14px;
    font-weight: 600;
    line-height: 1;
}


/* =========================================================
   MOBILE MENU
========================================================== */

.mobile-menu-button {
    width: 34px;
    height: 34px;

    display: none;

    align-items: center;
    justify-content: center;
    flex-direction: column;
    gap: 4px;

    margin-right: 8px;

    border: 1px solid var(--border);
    border-radius: var(--radius-small);

    background: var(--white);
    color: var(--text-soft);

    cursor: pointer;
}

.mobile-menu-button span {
    width: 14px;
    height: 1.5px;

    border-radius: 999px;

    background: currentColor;
}


/* =========================================================
   TOPBAR ACTIONS
========================================================== */

.topbar-action {
    min-height: 36px;

    display: inline-flex;
    align-items: center;
    gap: 7px;

    padding: 0 10px;

    border: 1px solid transparent;
    border-radius: var(--radius-small);

    background: transparent;
    color: var(--text-soft);

    font-size: 12px;
    font-weight: 600;

    cursor: pointer;

    transition:
        background var(--transition),
        border-color var(--transition),
        color var(--transition);
}

.topbar-action:hover {
    border-color: var(--border);
    background: var(--surface-soft);
    color: var(--text);
}

.topbar-action-icon {
    width: 17px;
    height: 17px;

    display: flex;
    align-items: center;
    justify-content: center;
}

.topbar-action-icon svg {
    width: 17px;
    height: 17px;
}


/* =========================================================
   TOPBAR ACCOUNT
========================================================== */

.topbar-account {
    width: 36px;
    height: 36px;

    display: flex;
    align-items: center;
    justify-content: center;

    padding: 0;

    border: 1px solid transparent;
    border-radius: 50%;

    background: transparent;

    cursor: pointer;

    transition:
        background var(--transition),
        border-color var(--transition);
}

.topbar-account:hover {
    border-color: var(--border);
    background: var(--surface-soft);
}

.topbar-avatar {
    width: 28px;
    height: 28px;

    display: flex;
    align-items: center;
    justify-content: center;

    border: 1px solid var(--border);
    border-radius: 50%;

    background: var(--blue-light);
    color: var(--blue);

    font-size: 10px;
    font-weight: 700;
}


/* =========================================================
   VIEW CONTAINER
========================================================== */

.view-container {
    position: relative;

    flex: 1;
    min-height: 0;

    width: 100%;
}


/* =========================================================
   APP VIEWS
========================================================== */

.app-view {
    width: 100%;
    min-height: calc(100vh - var(--topbar-height));

    display: none;
}

.app-view.is-active {
    display: block;
}


/* =========================================================
   STANDARD PAGE CONTENT
========================================================== */

.page-content {
    width: 100%;
    max-width: 1240px;

    margin: 0 auto;

    padding: 42px 40px 64px;
}


/* =========================================================
   PAGE HEADER
========================================================== */

.page-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 28px;

    margin-bottom: 32px;
}

.page-header > div:first-child {
    min-width: 0;
}

.page-eyebrow {
    display: inline-block;

    margin-bottom: 8px;

    color: var(--blue);

    font-size: 10px;
    font-weight: 700;
    letter-spacing: 0.13em;
    line-height: 1.2;

    text-transform: uppercase;
}

.page-header h1 {
    margin-bottom: 7px;

    color: var(--black);

    font-size: 30px;
    font-weight: 700;
    line-height: 1.12;
    letter-spacing: -0.035em;
}

.page-header p {
    max-width: 600px;

    color: var(--muted);

    font-size: 13px;
    line-height: 1.55;
}


/* =========================================================
   SECTION HEADER
========================================================== */

.section-header {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 20px;

    margin-bottom: 20px;
}

.section-header h2 {
    margin-bottom: 4px;

    color: var(--text);

    font-size: 17px;
    font-weight: 650;
    line-height: 1.3;
}

.section-header p {
    color: var(--muted);

    font-size: 12px;
    line-height: 1.5;
}


/* =========================================================
   GENERAL GRID
========================================================== */

.grid {
    display: grid;
}

.grid-2 {
    grid-template-columns: repeat(2, minmax(0, 1fr));
}

.grid-3 {
    grid-template-columns: repeat(3, minmax(0, 1fr));
}

.grid-4 {
    grid-template-columns: repeat(4, minmax(0, 1fr));
}


/* =========================================================
   OVERLAY / DRAWER SUPPORT
========================================================== */

.sidebar-overlay {
    position: fixed;
    inset: 0;

    display: none;

    background: rgba(15, 23, 42, 0.22);

    z-index: 90;
}

.sidebar-overlay.is-visible {
    display: block;
}


/* =========================================================
   COLLAPSED SIDEBAR
========================================================== */

#xerion-app.sidebar-collapsed .sidebar {
    width: var(--sidebar-collapsed-width);
}

#xerion-app.sidebar-collapsed .app-main {
    width: calc(100% - var(--sidebar-collapsed-width));
    margin-left: var(--sidebar-collapsed-width);
}

#xerion-app.sidebar-collapsed .sidebar-brand {
    width: 100%;

    justify-content: center;
}

#xerion-app.sidebar-collapsed .sidebar-brand-logo {
    width: 32px;
    height: 32px;

    object-fit: contain;
    object-position: left;
}

#xerion-app.sidebar-collapsed .sidebar-header {
    padding: 0 12px;
}

#xerion-app.sidebar-collapsed .sidebar-collapse {
    position: absolute;
    right: -16px;

    border-color: var(--border);
    background: var(--white);

    box-shadow: var(--shadow-xs);
}

#xerion-app.sidebar-collapsed .sidebar-new-chat {
    padding: 12px 10px;
}

#xerion-app.sidebar-collapsed .new-chat-button {
    justify-content: center;

    padding: 0;
}

#xerion-app.sidebar-collapsed .new-chat-button > span:last-child {
    display: none;
}

#xerion-app.sidebar-collapsed .sidebar-section-label,
#xerion-app.sidebar-collapsed .sidebar-item-label,
#xerion-app.sidebar-collapsed .sidebar-user-info,
#xerion-app.sidebar-collapsed .sidebar-user-chevron {
    display: none;
}

#xerion-app.sidebar-collapsed .sidebar-navigation {
    padding-left: 10px;
    padding-right: 10px;
}

#xerion-app.sidebar-collapsed .sidebar-item {
    justify-content: center;

    padding-left: 0;
    padding-right: 0;
}

#xerion-app.sidebar-collapsed .sidebar-bottom {
    padding-left: 10px;
    padding-right: 10px;
}

#xerion-app.sidebar-collapsed .sidebar-user {
    justify-content: center;

    padding: 5px;
}


/* =========================================================
   FULL WIDTH HELPER
========================================================== */

.full-width {
    width: 100%;
}


/* =========================================================
   RESPONSIVE — LARGE TABLETS / SMALL DESKTOP
========================================================== */

@media (max-width: 1100px) {

    .page-content {
        padding-left: 30px;
        padding-right: 30px;
    }

    .grid-4 {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }
}


/* =========================================================
   RESPONSIVE — TABLET
========================================================== */

@media (max-width: 900px) {

    :root {
        --sidebar-width: 240px;
    }

    .sidebar {
        transform: translateX(-100%);
        box-shadow: var(--shadow);
    }

    .sidebar.is-open {
        transform: translateX(0);
    }

    .app-main {
        width: 100%;
        margin-left: 0;
    }

    #xerion-app.sidebar-collapsed .sidebar {
        width: var(--sidebar-width);

        transform: translateX(-100%);
    }

    #xerion-app.sidebar-collapsed .sidebar.is-open {
        transform: translateX(0);
    }

    #xerion-app.sidebar-collapsed .app-main {
        width: 100%;
        margin-left: 0;
    }

    #xerion-app.sidebar-collapsed .sidebar-brand {
        width: auto;
        justify-content: flex-start;
    }

    #xerion-app.sidebar-collapsed .sidebar-brand-logo {
        width: 104px;
        height: auto;
    }

    #xerion-app.sidebar-collapsed .sidebar-section-label,
    #xerion-app.sidebar-collapsed .sidebar-item-label,
    #xerion-app.sidebar-collapsed .sidebar-user-info,
    #xerion-app.sidebar-collapsed .sidebar-user-chevron {
        display: initial;
    }

    #xerion-app.sidebar-collapsed .sidebar-item {
        justify-content: flex-start;
        padding-left: 10px;
        padding-right: 10px;
    }

    #xerion-app.sidebar-collapsed .new-chat-button {
        justify-content: flex-start;
        padding: 0 13px;
    }

    #xerion-app.sidebar-collapsed .new-chat-button > span:last-child {
        display: inline;
    }

    #xerion-app.sidebar-collapsed .sidebar-user {
        justify-content: flex-start;
        padding: 5px 7px;
    }

    .mobile-menu-button {
        display: flex;
    }

    .sidebar-overlay {
        display: none;
    }

    #xerion-app.sidebar-open .sidebar-overlay {
        display: block;
    }
}


/* =========================================================
   RESPONSIVE — MOBILE
========================================================== */

@media (max-width: 640px) {

    .app-topbar {
        padding: 0 14px;
    }

    .topbar-action span:last-child {
        display: none;
    }

    .topbar-action {
        width: 36px;

        justify-content: center;

        padding: 0;
    }

    .page-content {
        padding: 30px 18px 48px;
    }

    .page-header {
        flex-direction: column;
        gap: 18px;

        margin-bottom: 26px;
    }

    .page-header h1 {
        font-size: 27px;
    }

    .page-header .button {
        width: 100%;
    }

    .grid-2,
    .grid-3,
    .grid-4 {
        grid-template-columns: 1fr;
    }
}


/* =========================================================
   RESPONSIVE — SMALL MOBILE
========================================================== */

@media (max-width: 400px) {

    .app-topbar {
        height: 58px;
    }

    .app-view {
        min-height: calc(100vh - 58px);
    }

    .view-title {
        font-size: 13px;
    }

    .page-content {
        padding-left: 14px;
        padding-right: 14px;
    }

    .page-header h1 {
        font-size: 25px;
    }
}