/*
 * Mechart production layout contract
 * Owns only page geometry and responsive behavior.
 * Feature styling remains in market.css, portfolio.css and components.css.
 */
:root {
  --app-header-height: 60px;
  --app-sidebar-width: 230px;
  --app-secondary-width: 270px;
  --app-content-max: 1920px;
}

html { min-width: 320px; background: var(--bg, #070c18); }
body { min-width: 320px; overflow-x: clip; }
.app { min-height: 100dvh; }

.topbar {
  position: sticky;
  top: 0;
  z-index: 300;
}

.body-grid {
  display: grid;
  grid-template-columns: var(--app-sidebar-width) minmax(0, 1fr);
  min-height: calc(100dvh - var(--app-header-height));
  align-items: start;
}

.left-sidebar {
  position: sticky;
  top: var(--app-header-height);
  z-index: 80;
  height: calc(100dvh - var(--app-header-height));
  min-height: 0;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.workspace,
.app-view,
.main-content,
.right-sidebar,
.portfolio-main,
.portfolio-content-grid,
.asset-column,
.register-column {
  min-width: 0;
}

/* Desktop secondary navigation: a real adjacent panel, never an arbitrary flyout. */
.market-secondary-panel {
  position: fixed;
  top: var(--app-header-height);
  bottom: 0;
  left: var(--app-sidebar-width);
  z-index: 160;
  width: var(--app-secondary-width);
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr);
  overflow: hidden;
  border-right: 1px solid rgba(255,255,255,.08);
  background: rgba(9,16,29,.985);
  box-shadow: 18px 0 48px rgba(0,0,0,.28);
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform: translateX(-12px);
  transition: opacity .16s ease, transform .18s ease, visibility .16s ease;
}

.market-secondary-panel.open {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  transform: translateX(0);
}

.market-item-list-panel {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
}

/* The old hover flyout must never reactivate. */
.market-item-list,
.market-menu:hover .market-item-list,
.market-menu:focus-within .market-item-list {
  display: none !important;
}

#view-market.active {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 300px;
  align-items: start;
}

.chart-area { min-width: 0; overflow-x: auto; }
.chart-scroll-stage { min-width: 0; width: 100%; }

@media (max-width: 1280px) {
  :root {
    --app-sidebar-width: 190px;
    --app-secondary-width: 250px;
  }

  #view-market.active,
  #view-events.active { display: block; }

  /* At laptop widths the calendar cannot keep a 300px parent sidebar while
     that sidebar itself becomes a two-column grid. Put the teaser cards below
     the calendar so the 7-column month grid keeps its usable width. */
  #view-events .calendar-main { width: 100%; }

  #view-market .right-sidebar,
  #view-events .right-sidebar {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    padding: 0 16px 28px;
  }
  #view-market .right-sidebar .ad-card,
  #view-events .right-sidebar .ad-card { display: none; }

  .portfolio-content-grid { grid-template-columns: minmax(0, 1fr); }
  .register-column { position: static; }
}

@media (max-width: 960px) {
  :root { --app-header-height: 108px; }

  .topbar {
    height: auto;
    min-height: var(--app-header-height);
    flex-wrap: wrap;
    gap: 10px;
    padding-block: 10px;
  }
  .search-box-wrap { order: 3; flex: 1 0 100%; min-width: 0; }
  .search-box { width: 100%; }
  .topbar-spacer { display: none; }

  .body-grid { display: block; }
  .left-sidebar {
    position: sticky;
    top: var(--app-header-height);
    width: 100%;
    height: auto;
    overflow: visible;
    border-right: 0;
    border-bottom: 1px solid rgba(255,255,255,.06);
  }
  .main-nav {
    display: flex;
    gap: 6px;
    overflow-x: auto;
    scrollbar-width: none;
  }
  .main-nav::-webkit-scrollbar { display: none; }
  .main-nav > * { flex: 0 0 auto; }
  .nav-item, .market-toggle { width: auto; white-space: nowrap; }
  .favorite-section, .sidebar-ad { display: none; }

  .market-secondary-panel {
    top: calc(var(--app-header-height) + 56px);
    bottom: 12px;
    left: 12px;
    width: min(340px, calc(100vw - 24px));
    border: 1px solid rgba(96,165,250,.18);
    border-radius: 14px;
    box-shadow: 0 24px 64px rgba(0,0,0,.5);
  }

  #view-market .right-sidebar,
  #view-events .right-sidebar { grid-template-columns: 1fr; }
  .stats-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .summary-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 640px) {
  .topbar { padding-inline: 12px; }
  .brand small, .profile-copy, .search-box kbd { display: none; }
  .main-content, .calendar-main, .portfolio-main { padding-inline: 12px; }

  .item-header { align-items: stretch; flex-direction: column; }
  .headline-price { text-align: left; }
  .chart-toolbar { flex-wrap: wrap; }
  .event-toggle { margin-left: 0; }
  .stats-grid, .summary-grid, .event-grid { grid-template-columns: 1fr; }

  .market-secondary-panel {
    right: 8px;
    bottom: 8px;
    left: 8px;
    width: auto;
  }

  .asset-table-wrap, .sales-history-wrap {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  .asset-detail-panel { grid-template-columns: 1fr; }
  .portfolio-modal { align-items: end; padding: 8px; }
  .portfolio-modal-card { max-height: calc(100dvh - 16px); overflow-y: auto; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: .01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: .01ms !important;
  }
}


/* =========================================================
   Production menu interaction — hover + nested categories
   ========================================================= */

.market-secondary-panel {
    max-width: calc(100vw - var(--app-sidebar-width) - 24px);
}

.market-category-list {
    min-height: 0;
    overflow-y: auto !important;
    overscroll-behavior: contain;
    padding: 12px 4px 20px !important;
}

.market-category-group {
    display: grid;
    min-width: 0;
    border-bottom: 1px solid rgba(255, 255, 255, .055);
}

.market-category-group:last-child {
    border-bottom: 0;
}

.market-category {
    position: sticky;
    top: 0;
    z-index: 3;
    background: rgba(9, 16, 29, .985);
}

.market-category-group .category-panel {
    display: none;
    min-width: 0;
    padding: 2px 4px 10px 12px;
}

.market-category-group .category-panel.active {
    display: grid;
    gap: 4px;
    animation: market-panel-in .16s ease both;
}

.market-category-group .flyout-head {
    position: static;
    margin: 0 0 4px;
    padding: 6px 8px 8px;
    border-bottom: 1px solid rgba(255,255,255,.05);
    background: transparent;
}

.market-category-group .item-button {
    min-width: 0;
    min-height: 38px;
    padding: 8px 10px;
}

.market-category-group .item-button span:last-child {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Narrow desktop and tablet: panel is pinned to the right, not over the nav. */
@media (max-width: 960px) {
    .market-secondary-panel {
        top: calc(var(--app-header-height) + 58px);
        right: 12px;
        bottom: 12px;
        left: auto;
        width: min(340px, calc(100vw - 24px));
        max-width: calc(100vw - 24px);
        transform: translateX(14px);
    }

    .market-secondary-panel.open {
        transform: translateX(0);
    }
}

@media (max-width: 640px) {
    .market-secondary-panel {
        top: calc(var(--app-header-height) + 54px);
        right: 8px;
        bottom: 8px;
        left: auto;
        width: min(330px, calc(100vw - 16px));
        max-width: calc(100vw - 16px);
    }
}

/* Keep the panel usable in short windows. */
@media (max-height: 620px) {
    .market-secondary-panel {
        overflow: hidden;
    }

    .market-category-list {
        padding-bottom: 10px !important;
    }
}


/* =========================================================
   Three-step item chart navigation
   ========================================================= */

.market-secondary-panel {
    position: fixed !important;
    top: var(--market-panel-top, calc(var(--app-header-height) + 8px)) !important;
    left: var(--market-panel-left, calc(var(--app-sidebar-width) + 10px)) !important;
    right: auto !important;
    bottom: auto !important;
    width: min(320px, calc(100vw - 24px)) !important;
    max-width: calc(100vw - 24px) !important;
    max-height: var(--market-panel-max-height, calc(100dvh - 72px)) !important;
    display: grid !important;
    grid-template-rows: auto auto minmax(0, 1fr) !important;
    overflow: hidden !important;
    contain: layout paint;
}

.market-category-list {
    display: grid !important;
    gap: 5px !important;
    padding: 12px 12px 10px !important;
    overflow: visible !important;
}

.market-category {
    position: static !important;
    width: 100% !important;
    min-height: 42px;
    padding: 9px 11px;
    border: 1px solid rgba(255,255,255,.06);
    border-radius: 9px;
    background: rgba(255,255,255,.02) !important;
}

.market-category:hover,
.market-category:focus-visible,
.market-category.active {
    color: #dbeafe;
    border-color: rgba(96,165,250,.24);
    background: rgba(59,130,246,.10) !important;
}

.market-category > span:last-child {
    display: inline-flex;
    transition: transform .16s ease;
}

.market-category.active > span:last-child {
    transform: rotate(90deg);
}

.market-item-list-panel {
    min-height: 0;
    overflow-y: auto !important;
    overscroll-behavior: contain;
    padding: 0 12px 14px;
}

.market-item-list-panel .category-panel {
    display: none !important;
}

.market-item-list-panel .category-panel.active {
    display: grid !important;
    gap: 4px;
    animation: market-panel-in .16s ease both;
}

.market-item-list-panel .flyout-head {
    position: sticky;
    top: 0;
    z-index: 2;
    margin: 0 0 6px;
    padding: 9px 8px 8px;
    border-bottom: 1px solid rgba(255,255,255,.06);
    background: rgba(9,16,29,.98);
}

.market-item-list-panel .item-button {
    min-height: 38px;
    padding: 8px 10px;
}

@media (max-width: 960px) {
    .market-secondary-panel {
        width: min(300px, calc(100vw - 24px)) !important;
    }
}

@media (max-width: 640px) {
    .market-secondary-panel {
        width: min(286px, calc(100vw - 16px)) !important;
        max-width: calc(100vw - 16px) !important;
    }
}


/* =========================================================
   Category accordion — item list opens directly below category
   ========================================================= */

.market-secondary-panel {
    grid-template-rows: auto minmax(0, 1fr) !important;
}

.market-category-accordion {
    display: grid !important;
    align-content: start;
    gap: 6px !important;
    min-height: 0;
    overflow-y: auto !important;
    overscroll-behavior: contain;
    padding: 12px !important;
}

.market-category-group {
    display: grid;
    min-width: 0;
    border: 1px solid rgba(255,255,255,.055);
    border-radius: 10px;
    overflow: hidden;
    background: rgba(255,255,255,.014);
}

.market-category-group .market-category {
    border: 0 !important;
    border-radius: 0 !important;
    background: transparent !important;
}

.market-category-group .market-category:hover,
.market-category-group .market-category:focus-visible,
.market-category-group .market-category.active {
    background: rgba(59,130,246,.10) !important;
}

.market-category-group .category-panel,
.market-category-group .category-panel[hidden] {
    display: none !important;
}

.market-category-group .category-panel.active {
    display: grid !important;
    gap: 4px;
    padding: 6px 8px 9px;
    border-top: 1px solid rgba(255,255,255,.055);
    background: rgba(7,12,24,.34);
    animation: market-accordion-open .16s ease both;
}

.market-category-group .flyout-head {
    position: static !important;
    margin: 0 !important;
    padding: 5px 7px 7px !important;
    border: 0 !important;
    background: transparent !important;
}

.market-category-group .flyout-head strong {
    color: #8ea1bb;
    font-size: 10px;
}

.market-category-group .flyout-head small {
    display: none;
}

.market-category-group .item-button {
    min-height: 36px;
    padding: 7px 9px;
    border-radius: 7px;
}

.market-category-group .item-button span:last-child {
    font-size: 12px;
}

@keyframes market-accordion-open {
    from {
        opacity: 0;
        transform: translateY(-3px);
    }
    to {
        opacity: 1;
        transform: translateY(0);
    }
}


/* =========================================================
   Final compact panel + responsive topbar alignment
   ========================================================= */

:root {
    --app-secondary-width: 210px;
}

.market-secondary-panel {
    top: var(--app-header-height) !important;
    bottom: 0 !important;
    left: var(--market-panel-left, var(--app-sidebar-width)) !important;
    right: auto !important;

    width: var(--app-secondary-width) !important;
    max-width: min(var(--app-secondary-width), calc(100vw - 8px)) !important;

    height: calc(100dvh - var(--app-header-height)) !important;
    max-height: none !important;

    border-top: 0 !important;
    border-bottom: 0 !important;
    border-radius: 0 !important;

    box-shadow:
        14px 0 34px rgba(0, 0, 0, .24),
        inset -1px 0 rgba(255, 255, 255, .035) !important;
}

.market-secondary-head {
    min-height: 74px;
    padding: 13px 11px 12px !important;
}

.market-category-accordion {
    height: 100%;
    padding: 8px !important;
    gap: 4px !important;
}

.market-category {
    min-height: 38px !important;
    padding: 7px 9px !important;
}

.market-category-group .category-panel.active {
    max-height: none;
    padding: 5px 7px 8px !important;
}

.market-category-group .item-button {
    min-height: 31px !important;
    padding: 5px 7px !important;
}

/* Mid-size windows: brand left, server and profile stay visible on the right,
   search moves cleanly to a full-width second row. */
@media (max-width: 960px) {
    :root {
        --app-secondary-width: 200px;
    }

    .topbar {
        display: grid !important;
        grid-template-columns: auto minmax(0, 1fr) auto auto;
        grid-template-rows: auto auto;
        align-items: center;
        column-gap: 8px;
        row-gap: 8px;
        min-height: var(--app-header-height);
        padding: 8px 10px !important;
    }

    .brand {
        grid-column: 1;
        grid-row: 1;
        min-width: 0;
    }

    .topbar-spacer {
        display: block !important;
        grid-column: 2;
        grid-row: 1;
        min-width: 0;
    }

    .control-select {
        display: inline-flex !important;
        grid-column: 3;
        grid-row: 1;
        justify-self: end;
        max-width: 116px;
        min-width: 0;
    }

    .profile-button,
    .guest-profile {
        display: inline-flex !important;
        grid-column: 4;
        grid-row: 1;
        justify-self: end;
        min-width: 0;
        margin-left: 0 !important;
    }

    .search-box-wrap {
        grid-column: 1 / -1;
        grid-row: 2;
        width: 100%;
        min-width: 0;
        order: initial !important;
    }

    .search-box {
        width: 100% !important;
    }
}

/* Small windows: keep profile at the far right and preserve server selection. */
@media (max-width: 640px) {
    :root {
        --app-secondary-width: 194px;
    }

    .topbar {
        grid-template-columns: auto minmax(82px, 1fr) auto;
        column-gap: 6px;
        padding-inline: 8px !important;
    }

    .brand {
        grid-column: 1;
    }

    .control-select {
        grid-column: 2;
        justify-self: end;
        width: min(112px, 100%);
        max-width: 112px;
        padding-inline: 8px !important;
    }

    .profile-button,
    .guest-profile {
        grid-column: 3;
        justify-self: end;
    }

    .profile-copy {
        display: none !important;
    }

    .topbar-spacer {
        display: none !important;
    }
}

@media (max-width: 420px) {
    :root {
        --app-secondary-width: 188px;
    }

    .brand strong {
        font-size: 13px;
    }

    .control-select {
        max-width: 96px;
        font-size: 11px;
    }

    .profile-button,
    .guest-profile {
        padding-inline: 6px !important;
    }
}


/* =========================================================
   Responsive stability pass
   ========================================================= */

body.market-menu-open {
    overflow: hidden;
}

/* Keep the working area from disappearing at intermediate widths. */
.workspace,
#view-market,
#view-events,
#view-portfolio {
    width: 100%;
    max-width: 100%;
}

/* Consistent stacking order. */
.left-sidebar {
    z-index: 90;
}

.market-secondary-panel {
    z-index: 220 !important;
}

.topbar {
    isolation: isolate;
}

/* Topbar controls must never shrink to zero or fall outside the viewport. */
.control-select,
.profile-button,
.guest-profile {
    flex: 0 0 auto;
}

.control-select {
    text-overflow: ellipsis;
}

.profile-button,
.guest-profile {
    max-width: min(180px, 34vw);
}

.profile-copy {
    min-width: 0;
}

.profile-copy strong,
.profile-copy small {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

/* Medium desktop: retain all controls on one row when there is enough space. */
@media (min-width: 961px) and (max-width: 1180px) {
    .topbar {
        gap: 10px;
        padding-inline: 14px;
    }

    .search-box-wrap {
        flex: 1 1 320px;
        min-width: 180px;
    }

    .search-box {
        width: 100%;
        max-width: 430px;
    }

    .control-select {
        max-width: 120px;
    }

    .profile-button,
    .guest-profile {
        max-width: 150px;
    }
}

/* Tablet / compact desktop: stable two-row header. */
@media (max-width: 960px) {
    .topbar {
        position: sticky;
        width: 100%;
    }

    .search-box-wrap,
    .search-box {
        max-width: none !important;
    }

    .body-grid,
    .workspace {
        min-height: 0;
    }

    .market-secondary-panel {
        will-change: transform;
    }
}

/* Very small screens: preserve server and profile while preventing overflow. */
@media (max-width: 520px) {
    .topbar {
        grid-template-columns: auto minmax(72px, 1fr) auto !important;
    }

    .brand {
        max-width: 90px;
    }

    .brand > span:last-child {
        min-width: 0;
    }

    .brand strong {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .control-select {
        width: 100%;
        max-width: 104px !important;
    }

    .profile-button,
    .guest-profile {
        max-width: 46px;
    }

    .profile-button > span:last-child,
    .guest-profile > span:last-child {
        display: none;
    }
}

/* Accessibility: a visible keyboard focus ring for all menu controls. */
.market-toggle:focus-visible,
.market-secondary-close:focus-visible,
.market-category:focus-visible,
.item-button:focus-visible,
.control-select:focus-visible,
.profile-button:focus-visible,
.guest-profile:focus-visible {
    outline: 2px solid rgba(96, 165, 250, .85);
    outline-offset: 2px;
}

@media (hover: hover) and (pointer: fine) {
    body.market-menu-open {
        overflow: auto;
    }
}


/* Hover preview must never look like a page navigation change. */
.market-menu.preview-open .market-toggle {
    color: #b8c7db;
    border-color: rgba(96, 165, 250, .18);
    background: rgba(59, 130, 246, .07);
}

.market-menu.preview-open .market-toggle.active {
    color: #b8c7db;
    background: rgba(59, 130, 246, .07);
}


/* =========================================================
   Favorites
   ========================================================= */

.item-title-block {
    min-width: 0;
}

.item-title-row {
    display: flex;
    align-items: center;
    gap: 8px;
    min-width: 0;
}

.item-title-row h1 {
    min-width: 0;
    overflow-wrap: anywhere;
}

.favorite-toggle {
    display: inline-grid;
    place-items: center;
    flex: 0 0 auto;
    width: 36px;
    height: 36px;
    padding: 0;
    border: 1px solid rgba(255, 255, 255, .14);
    border-radius: 10px;
    color: #f8fafc;
    background: rgba(255, 255, 255, .025);
    cursor: pointer;
    transition:
        color .16s ease,
        background .16s ease,
        border-color .16s ease,
        transform .16s ease,
        box-shadow .16s ease;
}

.favorite-toggle span {
    display: block;
    font-size: 25px;
    line-height: 1;
    transform: translateY(-1px);
}

.favorite-toggle:hover:not(:disabled),
.favorite-toggle:focus-visible {
    border-color: rgba(255, 255, 255, .34);
    background: rgba(255, 255, 255, .07);
    transform: translateY(-1px);
}

.favorite-toggle.active {
    color: #ffffff;
    border-color: rgba(255, 255, 255, .30);
    background: rgba(255, 255, 255, .10);
    box-shadow: 0 0 0 1px rgba(255, 255, 255, .035);
}

.favorite-toggle:disabled {
    opacity: .36;
    cursor: not-allowed;
}

.favorite-section-head {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    margin-bottom: 10px;
}

.favorite-section-head h2 {
    margin: 0;
}

.favorite-count {
    display: inline-grid;
    place-items: center;
    min-width: 22px;
    height: 22px;
    padding-inline: 6px;
    border: 1px solid rgba(96, 165, 250, .16);
    border-radius: 999px;
    color: #93c5fd;
    background: rgba(59, 130, 246, .08);
    font-size: 10px;
    font-weight: 700;
}

.favorite-list {
    display: grid;
    gap: 5px;
}

.favorite-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 28px;
    align-items: center;
    gap: 4px;
    min-width: 0;
    border: 1px solid rgba(255,255,255,.05);
    border-radius: 8px;
    background: rgba(255,255,255,.018);
}

.favorite-item-open,
.favorite-item-remove {
    border: 0;
    color: inherit;
    background: transparent;
    cursor: pointer;
}

.favorite-item-open {
    display: flex;
    align-items: center;
    gap: 7px;
    min-width: 0;
    padding: 8px 4px 8px 9px;
    text-align: left;
}

.favorite-item-open:hover {
    color: #dbeafe;
}

.favorite-item-star {
    flex: 0 0 auto;
    color: #ffffff;
    font-size: 13px;
}

.favorite-item-label {
    min-width: 0;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    font-size: 11px;
}

.favorite-item-remove {
    display: grid;
    place-items: center;
    width: 26px;
    height: 26px;
    border-radius: 6px;
    color: #64748b;
    font-size: 16px;
}

.favorite-item-remove:hover,
.favorite-item-remove:focus-visible {
    color: #fca5a5;
    background: rgba(248, 113, 113, .08);
}

@media (max-width: 960px) {
    .favorite-section {
        display: none;
    }
}

@media (max-width: 520px) {
    .favorite-toggle {
        width: 33px;
        height: 33px;
        border-radius: 9px;
    }

    .favorite-toggle span {
        font-size: 22px;
    }
}


/* =========================================================
   Minimal commercial favorite button
   ========================================================= */

.favorite-toggle {
    width: 32px !important;
    height: 32px !important;
    padding: 0 !important;
    border: 0 !important;
    border-radius: 8px !important;
    color: #f8fafc;
    background: transparent !important;
    box-shadow: none !important;
}

.favorite-toggle:hover:not(:disabled),
.favorite-toggle:focus-visible {
    background: rgba(255, 255, 255, .055) !important;
    transform: none !important;
}

.favorite-toggle:active:not(:disabled) {
    transform: scale(.94) !important;
}

.favorite-icon {
    width: 21px;
    height: 21px;
    display: block;
    overflow: visible;
}

.favorite-icon-shape {
    fill: transparent;
    stroke: currentColor;
    stroke-width: 1.7;
    stroke-linejoin: round;
    transition:
        fill .16s ease,
        stroke .16s ease,
        transform .16s ease;
    transform-origin: center;
}

.favorite-toggle.active .favorite-icon-shape {
    fill: #ffffff;
    stroke: #ffffff;
}

.favorite-toggle:hover:not(:disabled) .favorite-icon-shape,
.favorite-toggle:focus-visible .favorite-icon-shape {
    transform: scale(1.06);
}

.favorite-toggle:disabled {
    opacity: .30 !important;
}

@media (max-width: 520px) {
    .favorite-toggle {
        width: 30px !important;
        height: 30px !important;
        border-radius: 7px !important;
    }

    .favorite-icon {
        width: 20px;
        height: 20px;
    }
}


/* =========================================================
   Brand visibility stability — authoritative override
   ========================================================= */

/* Keep the brand label visible at every desktop and tablet width. */
@media (min-width: 521px) {
    .brand {
        display: inline-flex !important;
        align-items: center !important;
        gap: 8px !important;
        min-width: max-content !important;
        max-width: none !important;
        overflow: visible !important;
    }

    .brand > span:last-child {
        display: block !important;
        min-width: max-content !important;
        overflow: visible !important;
    }

    .brand strong {
        display: block !important;
        min-width: max-content !important;
        overflow: visible !important;
        text-overflow: clip !important;
        white-space: nowrap !important;
    }
}

/* Compact widths: keep "메차트", hide only the subtitle. */
@media (max-width: 960px) {
    .brand {
        flex: 0 0 auto !important;
    }

    .brand strong {
        display: block !important;
        font-size: 14px !important;
    }

    .brand small {
        display: none !important;
    }
}

/* Very small widths: keep icon and product name together. */
@media (max-width: 520px) {
    .brand {
        display: inline-flex !important;
        align-items: center !important;
        gap: 7px !important;
        min-width: 94px !important;
        max-width: 116px !important;
        overflow: visible !important;
    }

    .brand > span:last-child {
        display: block !important;
        min-width: 0 !important;
        overflow: visible !important;
    }

    .brand strong {
        display: block !important;
        overflow: visible !important;
        text-overflow: clip !important;
        white-space: nowrap !important;
    }

    .brand small {
        display: none !important;
    }
}


/* =========================================================
   Breakpoint harmonization
   Prevent the 961–1024px hybrid state where the desktop sidebar
   contains the compact horizontal navigation and clips its labels.
   ========================================================= */

/* Keep the desktop sidebar/navigation intact until 960px. */
@media (min-width: 961px) and (max-width: 1024px) {
    .body-grid {
        display: grid !important;
        grid-template-columns:
            var(--app-sidebar-width)
            minmax(0, 1fr) !important;
    }

    .left-sidebar {
        position: sticky !important;
        top: var(--app-header-height) !important;
        width: auto !important;
        height: calc(100dvh - var(--app-header-height)) !important;
        padding: 16px 12px !important;
        overflow-x: hidden !important;
        overflow-y: auto !important;
        border-right: 1px solid rgba(255,255,255,.05) !important;
        border-bottom: 0 !important;
    }

    .main-nav {
        display: grid !important;
        grid-template-columns: minmax(0, 1fr) !important;
        gap: 4px !important;
        width: 100% !important;
        overflow: visible !important;
    }

    .main-nav > *,
    .main-nav .nav-item,
    .main-nav .market-toggle {
        width: 100% !important;
        min-width: 0 !important;
        justify-content: flex-start !important;
        overflow: visible !important;
        white-space: nowrap !important;
        text-overflow: clip !important;
    }

    .main-nav .pro-badge {
        margin-left: auto !important;
    }

    .workspace {
        grid-column: 2 !important;
        width: auto !important;
        margin-left: 0 !important;
        min-width: 0 !important;
    }
}

/* Switch once, at the same 960px breakpoint used by the page layout. */
@media (max-width: 960px) {
    .main-nav {
        display: grid !important;
        grid-template-columns: repeat(4, minmax(0, 1fr)) !important;
        gap: 6px !important;
        width: 100% !important;
        overflow: visible !important;
    }

    .main-nav > *,
    .main-nav .nav-item,
    .main-nav .market-toggle {
        width: 100% !important;
        min-width: 0 !important;
        justify-content: center !important;
        overflow: hidden !important;
        white-space: nowrap !important;
        text-overflow: ellipsis !important;
    }

    .main-nav .pro-badge {
        flex: 0 0 auto;
        margin-left: 6px !important;
    }
}

@media (max-width: 640px) {
    .main-nav {
        grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    }
}


/* responsive optimization patch */
@media (max-width: 960px){
  .main-nav{flex-wrap:wrap;overflow-x:visible;}
  .main-nav > *{flex:1 1 calc(50% - 6px);} 
  .nav-item,.market-toggle{width:100%;justify-content:center;}
}
@media (max-width:640px){
  .main-nav > *{flex:1 1 100%;}
  .beta-badge,.coming-soon-badge,.pro-badge{font-size:8px;padding:2px 6px;}
}
