/* App shell: top bar + collapsible sidebar + content area. */

.app-shell {
    min-height: 100vh;
    display: flex;
    flex-direction: column;
}

.sidebar-toggle-checkbox {
    /* Holds open/closed state via :checked - never rendered itself. */
    position: absolute;
    width: 0;
    height: 0;
    opacity: 0;
    pointer-events: none;
}

.topbar {
    height: var(--topbar-height);
    display: flex;
    align-items: center;
    gap: 1rem;
    padding: 0 1rem;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    position: sticky;
    top: 0;
    z-index: 20;
    min-width: 0;
}

.topbar-brand {
    font-weight: 600;
    font-size: 1.05rem;
    color: var(--color-text);
    text-decoration: none;
    letter-spacing: 0.01em;
    white-space: nowrap;
}

.topbar-spacer {
    flex: 1 1 auto;
}

.topbar-menu-toggle {
    display: none;
    align-items: center;
    justify-content: center;
    background: none;
    border: none;
    color: var(--color-text);
    cursor: pointer;
    padding: 0.4rem;
}

.app-body {
    display: flex;
    flex: 1 1 auto;
    min-height: 0;
    position: relative;
}

.sidebar {
    width: var(--sidebar-width);
    flex: 0 0 auto;
    background: var(--color-surface);
    border-right: 1px solid var(--color-border);
    padding: 1rem 0.75rem;
}

.sidebar-overlay {
    display: none;
}

.sidebar-nav {
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.sidebar-nav a {
    display: flex;
    align-items: center;
    gap: 0.65rem;
    padding: 0.6rem 0.75rem;
    border-radius: var(--radius-sm);
    color: var(--color-text-muted);
    text-decoration: none;
    font-size: 0.92rem;
}

.sidebar-nav a:hover {
    background: var(--color-surface-alt);
    color: var(--color-text);
}

.sidebar-nav a.active {
    background: var(--color-primary);
    color: #fff;
}

/* Mobile bottom tab bar - hidden by default (desktop keeps the sidebar); shown and given its
   fixed position only inside the 900px breakpoint below, where it replaces the sidebar entirely. */
.bottom-nav {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    height: var(--bottomnav-height);
    padding-bottom: env(safe-area-inset-bottom, 0px);
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    z-index: 20;
}

.bottom-nav-item {
    flex: 1 1 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 0.15rem;
    padding: 0.25rem 0.15rem;
    color: var(--color-text-muted);
    text-decoration: none;
    font-size: 0.65rem;
    line-height: 1.05;
    text-align: center;
    cursor: pointer;
}

.bottom-nav-item .icon {
    width: 20px;
    height: 20px;
}

.bottom-nav-item.active {
    color: var(--color-primary);
}

.bottomnav-more-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.35);
    z-index: 24;
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.2s ease;
}

.bottomnav-more-sheet {
    display: none;
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    background: var(--color-surface);
    border-top: 1px solid var(--color-border);
    border-radius: var(--radius-lg) var(--radius-lg) 0 0;
    box-shadow: var(--shadow-md);
    padding: 0.75rem 0.5rem calc(0.75rem + env(safe-area-inset-bottom, 0px));
    z-index: 25;
    transform: translateY(100%);
    transition: transform 0.2s ease;
}

.bottomnav-more-link {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 0.75rem 1rem;
    border-radius: var(--radius-sm);
    color: var(--color-text);
    text-decoration: none;
    font-size: 0.95rem;
}

.bottomnav-more-link:hover {
    background: var(--color-surface-alt);
}

.bottomnav-more-link.active {
    color: var(--color-primary);
}

#bottomnav-more-toggle:checked ~ .bottomnav-more-overlay {
    opacity: 1;
    pointer-events: auto;
}

#bottomnav-more-toggle:checked ~ .bottomnav-more-sheet {
    transform: translateY(0);
}

.content {
    flex: 1 1 auto;
    min-width: 0;
    padding: 1.5rem;
}

.icon {
    width: 18px;
    height: 18px;
    flex: 0 0 auto;
}

.theme-toggle, .user-menu-button, .language-switcher a {
    background: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text);
    padding: 0.4rem 0.6rem;
    cursor: pointer;
    font-size: 0.85rem;
    text-decoration: none;
}

.language-switcher {
    display: flex;
    gap: 0.25rem;
}

.user-menu {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.user-menu-name {
    display: flex;
    align-items: center;
    gap: 0.4rem;
    font-size: 0.9rem;
    color: var(--color-text);
}

.user-menu form {
    margin: 0;
}

.context-switcher {
    position: relative;
}

.context-switcher-button {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    background: var(--color-surface-alt);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    color: var(--color-text);
    padding: 0.4rem 0.75rem;
    cursor: pointer;
    font-size: 0.9rem;
    max-width: 200px;
}

.context-switcher-button span {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.context-switcher-panel {
    position: absolute;
    top: calc(100% + 0.4rem);
    left: 0;
    min-width: 240px;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-md);
    padding: 0.6rem;
    z-index: 30;
}

/* Generic inline error text - originally scoped to ContextSwitcher only, now the one error-message
   style every page reuses (Home, Catalog, Portfolio, Orders, Watchlist, Alerts, Settings, AssetDetail). */
.error-message {
    color: var(--color-negative);
    font-size: 0.85rem;
    margin: 0 0 0.5rem;
}

.context-switcher-list {
    list-style: none;
    margin: 0;
    padding: 0;
    display: flex;
    flex-direction: column;
    gap: 0.15rem;
}

.context-switcher-list li {
    display: flex;
    align-items: center;
    gap: 0.25rem;
}

.context-switcher-item {
    flex: 1 1 auto;
    text-align: left;
    background: none;
    border: none;
    color: var(--color-text);
    padding: 0.45rem 0.5rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
}

.context-switcher-item:hover {
    background: var(--color-surface-alt);
}

.context-switcher-list li.active .context-switcher-item {
    background: var(--color-primary);
    color: #fff;
}

.context-switcher-star {
    background: none;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    font-size: 0.95rem;
    line-height: 1;
    padding: 0.25rem 0.15rem;
}

.context-switcher-star:hover {
    color: var(--color-accent);
}

.context-switcher-star-active {
    color: var(--color-accent);
    cursor: default;
}

.context-switcher-remove {
    background: none;
    border: none;
    color: var(--color-text-muted);
    cursor: pointer;
    font-size: 1rem;
    line-height: 1;
    padding: 0.25rem 0.4rem;
}

.context-switcher-remove:hover {
    color: var(--color-negative);
}

.context-switcher-add {
    display: flex;
    gap: 0.4rem;
    margin-top: 0.6rem;
    padding-top: 0.6rem;
    border-top: 1px solid var(--color-border);
}

.context-switcher-add input {
    flex: 1 1 auto;
    min-width: 0;
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
}

.context-switcher-add button {
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    padding: 0.4rem 0.75rem;
    cursor: pointer;
}

.language-switcher a.active {
    background: var(--color-primary);
    color: #fff;
    border-color: var(--color-primary);
}

@media (max-width: 900px) {
    /* The bottom tab bar (below) replaces the hamburger + drawer entirely at this width, rather
       than sitting alongside it - so the toggle button and sidebar just stay hidden here instead
       of gaining their old "slide-out drawer" behavior. */
    .sidebar,
    .sidebar-overlay {
        display: none;
    }

    .content {
        padding: 1rem;
        /* Leave room so page content never sits under the fixed bottom-nav bar. */
        padding-bottom: calc(1rem + var(--bottomnav-height) + env(safe-area-inset-bottom, 0px));
    }

    .bottom-nav {
        display: flex;
    }

    /* Both stay display:none at their base rule (desktop) - only #bottomnav-more-toggle:checked
       toggles their opacity/transform below, so they must actually render (display: block) here
       first, or that toggle has nothing visible to reveal. */
    .bottomnav-more-overlay,
    .bottomnav-more-sheet {
        display: block;
    }
}

@media (max-width: 560px) {
    .topbar {
        gap: 0.5rem;
        padding: 0 0.5rem;
        /* Safety net for extreme narrow widths (e.g. a long-named context alongside FR/EN, theme
           toggle and user menu all at once): wrap to a second line instead of clipping/overlapping.
           Only kicks in here, at widths already narrow enough that a taller sticky topbar is the
           lesser problem. */
        flex-wrap: wrap;
        row-gap: 0.35rem;
    }

    .topbar-brand {
        display: none;
    }

    .context-switcher-button {
        max-width: 90px;
        padding: 0.4rem 0.5rem;
    }

    .user-menu-name-text {
        display: none;
    }

    .user-menu {
        gap: 0.4rem;
    }
}

/* Auth pages (login / external login) */
.auth-card {
    max-width: 380px;
    margin: 10vh auto;
    padding: 2rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    text-align: center;
}

.auth-hint {
    color: var(--color-text-muted);
    font-size: 0.9rem;
}

.external-login-form {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    margin-top: 1rem;
}

.btn-external-provider {
    padding: 0.65rem 1rem;
    border-radius: var(--radius-sm);
    border: 1px solid var(--color-border);
    background: var(--color-surface-alt);
    color: var(--color-text);
    font-size: 0.95rem;
    cursor: pointer;
}

.btn-external-provider:hover {
    background: var(--color-primary);
    color: #fff;
    border-color: var(--color-primary);
}

.btn-external-provider:disabled {
    opacity: 0.6;
    cursor: not-allowed;
}

/* Settings admin sections */
.settings-section {
    margin-bottom: 2rem;
    max-width: 900px;
}

.settings-section h2 {
    font-size: 1.1rem;
    margin-bottom: 0.75rem;
}

.settings-table {
    width: 100%;
    border-collapse: collapse;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-md);
    overflow: hidden;
    font-size: 0.9rem;
}

.settings-table th,
.settings-table td {
    padding: 0.5rem 0.75rem;
    text-align: left;
    border-bottom: 1px solid var(--color-border);
}

.settings-table th {
    color: var(--color-text-muted);
    font-weight: 600;
    background: var(--color-surface-alt);
}

.settings-table tr:last-child td {
    border-bottom: none;
}

/* Every table on the site (Catalog/Orders/Portfolio/Watchlist/Alerts/Settings) wraps in this so a
   wide table (Orders has 9 columns) scrolls horizontally within its own box on a narrow screen
   instead of forcing the whole page wider than the viewport. */
.table-responsive {
    width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
}

.table-responsive .settings-table {
    min-width: 600px;
}

.settings-url {
    max-width: 260px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.settings-add-form {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

.settings-add-form input,
.settings-add-form select,
.settings-add-form textarea {
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
    font-family: inherit;
}

.settings-add-form button {
    background: var(--color-primary);
    color: #fff;
    border: none;
    border-radius: var(--radius-sm);
    padding: 0.4rem 0.9rem;
    cursor: pointer;
}

.settings-config-json {
    width: 100%;
    max-width: 420px;
    font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
    font-size: 0.8rem;
    resize: vertical;
}

/* Catalog & asset detail */
.catalog-toolbar {
    display: flex;
    flex-wrap: wrap;
    gap: 0.5rem;
    margin-bottom: 1rem;
}

.catalog-search {
    flex: 1 1 260px;
    min-width: 0;
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
}

.catalog-toolbar select {
    padding: 0.45rem 0.7rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
}

.catalog-change-positive {
    color: var(--color-positive);
    font-weight: 600;
}

.catalog-change-negative {
    color: var(--color-negative);
    font-weight: 600;
}

.catalog-pager {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin-top: 1rem;
}

.kpi-row {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin: 1rem 0;
}

.kpi-card {
    padding: 1rem 1.25rem;
    min-width: 180px;
}

.kpi-label {
    color: var(--color-text-muted);
    font-size: 0.85rem;
    margin-bottom: 0.25rem;
}

.kpi-value {
    font-size: 1.4rem;
    font-weight: 600;
}

.range-selector {
    display: flex;
    flex-wrap: wrap;
    gap: 0.4rem;
    margin-bottom: 1rem;
}

.range-selector .range-active {
    background: var(--color-primary);
    color: #fff;
    border-color: var(--color-primary);
}

.chart-card {
    padding: 1rem;
}

/* Dashboard */
.dashboard-columns {
    display: flex;
    flex-wrap: wrap;
    gap: 1rem;
    margin: 1rem 0;
}

.dashboard-panel {
    flex: 1 1 320px;
    padding: 1.25rem;
}

.dashboard-panel h2 {
    font-size: 1rem;
    margin: 0 0 0.75rem;
}

.allocation-row {
    display: flex;
    align-items: center;
    gap: 0.6rem;
    margin-bottom: 0.5rem;
}

.allocation-label {
    flex: 0 0 90px;
    font-size: 0.85rem;
    color: var(--color-text-muted);
}

.allocation-bar-track {
    flex: 1 1 auto;
    height: 8px;
    border-radius: 4px;
    background: var(--color-surface-alt);
    overflow: hidden;
}

.allocation-bar-fill {
    height: 100%;
    background: var(--color-primary);
    border-radius: 4px;
}

.allocation-percent {
    flex: 0 0 48px;
    text-align: right;
    font-size: 0.85rem;
}

.dashboard-mover {
    display: flex;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.25rem 0;
    font-size: 0.9rem;
}

/* Modal (general rule: every add/save form opens in one of these) */
.modal-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 50;
    padding: 1rem;
}

.modal-dialog {
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
    width: 100%;
    max-width: 520px;
    max-height: 90vh;
    overflow-y: auto;
}

.modal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    border-bottom: 1px solid var(--color-border);
}

.modal-header h2 {
    margin: 0;
    font-size: 1.1rem;
}

.modal-close {
    background: none;
    border: none;
    color: var(--color-text-muted);
    font-size: 1.4rem;
    line-height: 1;
    cursor: pointer;
    padding: 0.1rem 0.3rem;
}

.modal-close:hover {
    color: var(--color-text);
}

.modal-body {
    padding: 1.25rem;
}

/* Every modal form: one labeled field per row (or a .form-row of a few side by side). */
.modal-form {
    display: flex;
    flex-direction: column;
    gap: 1rem;
}

.form-row {
    display: flex;
    gap: 0.75rem;
}

.form-row .form-field {
    flex: 1 1 0;
}

.form-field {
    display: flex;
    flex-direction: column;
    gap: 0.35rem;
}

.form-field label {
    font-size: 0.85rem;
    font-weight: 600;
    color: var(--color-text-muted);
}

.form-field input,
.form-field select,
.form-field textarea,
.form-field .autocomplete-input {
    width: 100%;
    padding: 0.5rem 0.6rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
    font-family: inherit;
    box-sizing: border-box;
}

.form-field-checkbox label {
    flex-direction: row;
    align-items: center;
    gap: 0.5rem;
    font-weight: 400;
    color: var(--color-text);
}

.form-total {
    font-weight: 600;
}

.modal-actions {
    display: flex;
    justify-content: flex-end;
    gap: 0.5rem;
    margin-top: 0.75rem;
}

/* Autocomplete */
.autocomplete {
    position: relative;
}

.autocomplete-input {
    width: 100%;
    padding: 0.4rem 0.5rem;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    background: var(--color-bg);
    color: var(--color-text);
}

.autocomplete-list {
    /* position/top/left/width are set inline by autocomplete.js (fixed, viewport-relative) -
       see AssetAutocomplete.razor's comment for why this isn't plain `position: absolute`. */
    max-height: 220px;
    overflow-y: auto;
    list-style: none;
    margin: 0;
    padding: 0.25rem;
    background: var(--color-surface);
    border: 1px solid var(--color-border);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    z-index: 1000;
}

.autocomplete-list li {
    padding: 0.4rem 0.5rem;
    border-radius: var(--radius-sm);
    cursor: pointer;
    font-size: 0.9rem;
}

.autocomplete-list li:hover {
    background: var(--color-surface-alt);
}
