/* Shared components used across pages: toasts, confirm dialog, focus ring, privacy mode,
   app shell links, and the dashboard's loading / failure / allocation pieces.
   Load after the page's other stylesheets. Colors come from the tokens in theme.css. */

/* ---------- Chart series (allocation). Validated categorical order, light / dark steps ---------- */
:root {
    --series-1: #2a78d6;
    --series-2: #eb6834;
    --series-3: #1baf7a;
    --series-4: #eda100;
    --series-5: #e87ba4;
    --series-6: #008300;
    --series-other: #a3a29c;
}
[data-theme="dark"] {
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #008300;
    --series-other: #6e6d68;
}

/* ---------- Keyboard focus ---------- */
:where(a, button, input, select, textarea, [data-a11y], [tabindex]):focus-visible {
    outline: 2px solid var(--accent-color);
    outline-offset: 2px;
}

/* ---------- Privacy mode: blur amounts, keep percentages ---------- */
[data-privacy="on"] :is(
    #totalPortfolioBalance, #portfolioPnl, #portfolioTrendChart,
    .account-card .balance-amount, .account-card .balance-pnl, .category-subtotal-amount,
    .position-value, .position-pnl,
    #totalBalance, #balancePnl, #balanceChart, #totalPnl,
    #assets-list td:nth-child(2), #assets-list td:nth-child(3),
    #positions td:nth-child(5), #positions td:nth-child(6), #positions td:nth-child(7),
    #pendingOrders td:nth-child(6), #pendingOrders td:nth-child(7),
    #tradeHistory td:nth-child(6), #tradeHistory td:nth-child(7), #tradeHistory td:nth-child(8)
) {
    filter: blur(7px);
    user-select: none;
    transition: filter 0.2s ease;
}

/* ---------- App shell (layout.js) ---------- */
.top-nav .brand-link {
    color: inherit;
    text-decoration: none;
}
.side-menu a.menu-item,
.side-menu button.menu-item,
.side-menu a.user-profile {
    color: inherit;
    text-decoration: none;
}
.side-menu button.menu-item {
    width: 100%;
    background: none;
    border: none;
    font: inherit;
    text-align: left;
    cursor: pointer;
}
.side-menu a.user-profile {
    display: flex;
    align-items: center;
    gap: inherit;
}
.side-menu .user-avatar img {
    width: 100%;
    height: 100%;
    border-radius: 50%;
    object-fit: cover;
}
.side-menu [hidden] {
    display: none !important;
}

/* ---------- Toasts (notifications.js) ---------- */
#notification-container {
    position: fixed;
    top: 20px;
    right: 20px;
    z-index: 3000;
    display: flex;
    flex-direction: column;
    gap: 10px;
    pointer-events: none;
}
#notification-container .notification {
    pointer-events: auto;
    display: flex;
    align-items: center;
    gap: 12px;
    min-width: 260px;
    max-width: min(380px, calc(100vw - 40px));
    margin: 0;
    padding: 12px 12px 12px 14px;
    background: var(--card-bg);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-left: 4px solid var(--accent-color);
    border-radius: 10px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.16);
    animation: toast-in 0.25s ease-out;
    transition: opacity 0.3s ease, transform 0.3s ease;
}
#notification-container .notification.fade-out {
    opacity: 0;
    transform: translateX(16px);
    animation: none;
}
#notification-container .notification-icon {
    flex: 0 0 auto;
    width: auto;
    height: auto;
    margin: 0;
    background: none;
    font-size: 1.15rem;
    color: var(--accent-color);
}
#notification-container .notification-content {
    flex: 1;
    min-width: 0;
}
#notification-container .notification-content p {
    margin: 0;
    color: var(--text-color);
    font-size: 0.92rem;
    font-weight: 500;
    line-height: 1.4;
    overflow-wrap: anywhere;
}
#notification-container .notification-close {
    flex: 0 0 auto;
    padding: 4px 6px;
    background: none;
    border: none;
    color: var(--text-secondary);
    cursor: pointer;
    border-radius: 4px;
    width: auto;
    margin: 0;
}
#notification-container .notification-close:hover {
    color: var(--text-color);
    background: var(--hover-bg);
}
#notification-container .notification.success { border-left-color: var(--positive-color); }
#notification-container .notification.success .notification-icon { color: var(--positive-color); }
#notification-container .notification.error { border-left-color: var(--negative-color); }
#notification-container .notification.error .notification-icon { color: var(--negative-color); }
#notification-container .notification.warning { border-left-color: var(--warning-color, #f29900); }
#notification-container .notification.warning .notification-icon { color: var(--warning-color, #f29900); }
@keyframes toast-in {
    from { opacity: 0; transform: translateY(-8px); }
    to { opacity: 1; transform: none; }
}
@media (max-width: 600px) {
    #notification-container {
        top: auto;
        bottom: calc(16px + env(safe-area-inset-bottom));
        left: 16px;
        right: 16px;
    }
    #notification-container .notification {
        max-width: none;
        min-width: 0;
    }
}

/* ---------- Confirm dialog (showConfirm) ---------- */
.app-dialog-overlay {
    position: fixed;
    inset: 0;
    z-index: 2500;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 20px;
    background: rgba(0, 0, 0, 0.5);
    opacity: 0;
    transition: opacity 0.2s ease;
}
.app-dialog-overlay.show {
    opacity: 1;
}
.app-dialog {
    text-align: left;
    width: 100%;
    max-width: 420px;
    padding: 24px;
    background: var(--card-bg);
    color: var(--text-color);
    border-radius: 12px;
    box-shadow: 0 16px 40px rgba(0, 0, 0, 0.25);
    transform: translateY(8px);
    transition: transform 0.2s ease;
}
.app-dialog-overlay.show .app-dialog {
    transform: none;
}
.app-dialog h3 {
    margin: 0 0 8px;
    font-size: 1.1rem;
    color: var(--text-color);
}
.app-dialog p {
    margin: 0 0 20px;
    color: var(--text-secondary);
    line-height: 1.5;
    white-space: pre-line;
}
.app-dialog .app-dialog-code {
    margin: 0 0 20px;
    max-height: 50vh;
    overflow: auto;
    padding: 12px;
    border-radius: 8px;
    background: var(--hover-bg);
    border: 1px solid var(--border-color);
    color: var(--text-color);
    font-size: 0.8rem;
    line-height: 1.45;
    white-space: pre-wrap;
    word-break: break-word;
}
.app-dialog:has(.app-dialog-code) {
    max-width: 560px;
}
/* Page styles center some headings/paragraphs; dialogs read left to right */
.app-dialog h3,
.app-dialog p {
    text-align: left;
}

/* showPrompt: text input with suggestion chips */
.app-prompt .app-prompt-message {
    margin: 0 0 12px;
}
.app-prompt .app-prompt-input {
    display: block;
    width: 100%;
    box-sizing: border-box;
    margin: 0 0 12px;
    padding: 10px 12px;
    border: 1px solid var(--input-border, var(--border-color));
    border-radius: 8px;
    background: var(--input-bg, var(--card-bg));
    color: var(--text-color);
    font-size: 0.95rem;
}
.app-prompt .app-prompt-input:focus {
    outline: 2px solid var(--accent-color);
    outline-offset: 0;
    border-color: transparent;
}
.app-prompt-suggestions {
    display: flex;
    flex-wrap: wrap;
    gap: 6px;
    margin: 0 0 18px;
}
.app-prompt-chip {
    width: auto;
    margin: 0;
    padding: 4px 10px;
    border: 1px solid var(--border-color);
    border-radius: 999px;
    background: var(--card-bg);
    color: var(--text-color);
    font-size: 0.82rem;
    cursor: pointer;
}
.app-prompt-chip:hover {
    border-color: var(--accent-color);
    color: var(--accent-color);
}
.app-dialog-spacer {
    flex: 1;
}
.app-dialog-buttons .app-dialog-clear {
    padding-left: 4px;
    padding-right: 4px;
    background: none;
    border: none;
    color: var(--negative-color);
}
.app-dialog-buttons {
    display: flex;
    justify-content: flex-end;
    gap: 8px;
}
.app-dialog-buttons button {
    width: auto;
    margin: 0;
    padding: 9px 18px;
    border-radius: 8px;
    font-size: 0.92rem;
    font-weight: 500;
    cursor: pointer;
}
.app-dialog-cancel {
    background: var(--button-bg);
    color: var(--button-text);
    border: 1px solid var(--border-color);
}
.app-dialog-confirm {
    background: var(--accent-color);
    color: #fff;
    border: 1px solid transparent;
}
.app-dialog-confirm.danger {
    background: var(--negative-color);
}
.app-dialog-buttons button:hover {
    filter: brightness(0.95);
}

/* ---------- Small shared buttons ---------- */
.retry-btn {
    display: inline-flex;
    align-items: center;
    gap: 6px;
    width: auto;
    margin: 0;
    padding: 6px 12px;
    background: var(--card-bg);
    color: var(--text-color);
    border: 1px solid var(--border-color);
    border-radius: 6px;
    font-size: 0.85rem;
    font-weight: 500;
    cursor: pointer;
    white-space: nowrap;
}
.retry-btn:hover {
    background: var(--hover-bg);
    border-color: var(--accent-color);
    color: var(--accent-color);
}
.link-btn {
    width: auto;
    margin: 0;
    padding: 0;
    background: none;
    border: none;
    color: var(--accent-color);
    font: inherit;
    font-weight: 600;
    cursor: pointer;
    text-decoration: underline;
    text-underline-offset: 2px;
}

/* ---------- Dashboard: page width ---------- */
/* body is a centered flex column (styles.css), so without an explicit width the container took
   the width of whatever was inside it: skeletons, then loading cards, then full cards. The grid
   jumped narrow/wide while loading. Fix the width; 1280px matches what desktops showed before. */
.dashboard-container.dashboard-page {
    width: 100%;
    max-width: 1280px;
    box-sizing: border-box;
}

/* ---------- Dashboard: summary header ---------- */
.portfolio-actions {
    display: flex;
    align-items: center;
    gap: 0.5rem;
}
/* Four controls next to the title: let them drop below it on narrow screens
   instead of squeezing the title onto three lines */
.portfolio-summary .portfolio-header {
    flex-wrap: wrap;
    row-gap: 10px;
}
.portfolio-summary .portfolio-header h3 {
    white-space: nowrap;
}
.portfolio-actions .calendar-btn {
    width: 34px;
    height: 34px;
    padding: 0;
}
.portfolio-actions .calendar-btn:disabled {
    cursor: default;
    opacity: 0.8;
}
#refreshBtn.spinning i {
    animation: refresh-spin 0.9s linear infinite;
}
@keyframes refresh-spin {
    to { transform: rotate(360deg); }
}
.portfolio-meta {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 6px;
    min-height: 1.1em;
}
.last-updated,
.period-caption {
    font-size: 0.78rem;
    color: var(--text-secondary);
}
.period-caption:not(:empty) + .last-updated:not(:empty)::before {
    content: '·';
    margin: 0 6px;
}

/* "Excludes +$12,000 net deposits" under the period selector */
.flow-note {
    margin-top: 6px;
    font-size: 0.78rem;
    color: var(--text-secondary);
    text-align: center;
}
.flow-note[hidden] {
    display: none;
}
.flow-note-warning-text {
    color: var(--warning-color, #b06000);
}
/* Account detail: period controls sit under the balance card's header */
.balance-card .period-selector {
    margin-top: 14px;
}
.flow-note-warning {
    width: auto;
    margin: 0;
    padding: 0;
    background: none;
    border: none;
    color: var(--warning-color, #b06000);
    font: inherit;
    cursor: pointer;
    text-decoration: underline dotted;
    text-underline-offset: 2px;
}

/* Return period: 24H / 7D / 30D / All */
.period-selector {
    display: flex;
    justify-content: center;
    gap: 2px;
    width: fit-content;
    margin: 10px auto 0;
    padding: 2px;
    border: 1px solid var(--border-color);
    border-radius: 8px;
    background: var(--card-bg);
}
.period-selector button {
    width: auto;
    margin: 0;
    padding: 3px 12px;
    background: none;
    border: none;
    border-radius: 6px;
    color: var(--text-secondary);
    font-size: 0.8rem;
    font-weight: 600;
    cursor: pointer;
}
.period-selector button:hover {
    color: var(--text-color);
    background: var(--hover-overlay, rgba(0, 0, 0, 0.05));
}
.period-selector button.active {
    background: var(--accent-color);
    color: #fff;
}
.last-updated.stale {
    color: var(--warning-color, #b06000);
}
.portfolio-warning {
    display: flex;
    align-items: center;
    gap: 8px;
    margin: 10px auto 0;
    padding: 8px 12px;
    max-width: 560px;
    border-radius: 8px;
    background: var(--warning-bg, rgba(242, 153, 0, 0.12));
    color: var(--text-color);
    font-size: 0.85rem;
    line-height: 1.4;
    text-align: left;
}
.portfolio-warning > span {
    flex: 1;
    min-width: 0;
}
.portfolio-warning[hidden] {
    display: none;
}
.portfolio-warning > i {
    color: var(--warning-color, #f29900);
}

/* ---------- Dashboard: allocation ---------- */
.allocation {
    margin-top: 16px;
    padding-top: 14px;
    border-top: 1px solid var(--border-color);
}
.allocation[hidden] {
    display: none;
}
.allocation-title {
    margin-bottom: 8px;
    font-size: 0.8rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    color: var(--text-secondary);
}
.allocation-bar {
    display: flex;
    gap: 2px;
    height: 10px;
}
.allocation-segment {
    flex-basis: 0;
    flex-shrink: 1;
    min-width: 3px;
    border-radius: 2px;
}
.allocation-segment:first-child {
    border-top-left-radius: 4px;
    border-bottom-left-radius: 4px;
}
.allocation-segment:last-child {
    border-top-right-radius: 4px;
    border-bottom-right-radius: 4px;
}
.allocation-legend {
    display: flex;
    flex-wrap: wrap;
    gap: 6px 16px;
    margin: 10px 0 0;
    padding: 0;
    list-style: none;
}
.allocation-legend li {
    display: flex;
    align-items: center;
    gap: 6px;
    font-size: 0.82rem;
    color: var(--text-color);
    min-width: 0;
}
.allocation-swatch {
    flex: 0 0 auto;
    width: 10px;
    height: 10px;
    border-radius: 2px;
}
.allocation-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    max-width: 180px;
}
.allocation-pct {
    color: var(--text-secondary);
    font-variant-numeric: tabular-nums;
}

/* ---------- Dashboard: account cards while loading / after a failure ---------- */
.account-balance.account-balance-loading {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 12px;
    padding-top: 12px;
}
.account-balance-loading .skeleton-amount {
    width: 45%;
    height: 1.6rem;
}
.account-balance-loading .skeleton-stats {
    width: 30%;
    height: 1.1rem;
}
.account-fetch-error {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-top: 12px;
    padding: 10px 12px;
    border-radius: 8px;
    background: var(--warning-bg, rgba(242, 153, 0, 0.12));
}
.account-fetch-error > i {
    color: var(--warning-color, #f29900);
}
.account-fetch-error-text {
    flex: 1;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: 2px;
    font-size: 0.85rem;
    line-height: 1.35;
}
.account-fetch-error-text strong {
    color: var(--text-color);
    font-weight: 600;
}
.account-fetch-error-text span {
    color: var(--text-secondary);
    overflow-wrap: anywhere;
    display: -webkit-box;
    -webkit-line-clamp: 3;
    -webkit-box-orient: vertical;
    overflow: hidden;
}
.error-message .retry-btn {
    margin-top: 10px;
}

/* ---------- Dashboard: touch reorder ---------- */
#accountsList .account-card[data-account-id] {
    -webkit-touch-callout: none;
}
#accountsList .account-card.touch-dragging {
    transform: scale(1.02);
    box-shadow: 0 12px 28px rgba(0, 0, 0, 0.22) !important;
    opacity: 0.95;
    z-index: 5;
    position: relative;
}
body.reordering,
body.reordering * {
    user-select: none;
    -webkit-user-select: none;
}

/* ---------- Account detail: exchange unreachable ---------- */
.fetch-error-banner {
    display: flex;
    align-items: center;
    gap: 10px;
    margin: 0 0 16px;
    padding: 12px 14px;
    border-radius: 10px;
    background: var(--warning-bg, rgba(242, 153, 0, 0.12));
    color: var(--text-color);
    font-size: 0.9rem;
    line-height: 1.4;
}
.fetch-error-banner[hidden] {
    display: none;
}
.fetch-error-banner > i {
    color: var(--warning-color, #f29900);
}
.fetch-error-banner span {
    flex: 1;
    min-width: 0;
    overflow-wrap: anywhere;
}

/* ---------- Account detail: header on narrow screens ---------- */
/* Back button + name + exchange + four icon buttons didn't fit on a phone: the name got a few
   pixels ("B."). Stack the exchange under the name and tighten the buttons. */
@media (max-width: 768px) {
    .detail-header {
        gap: 0.5rem;
    }
    .detail-header .account-info {
        flex-direction: column;
        gap: 2px;
        min-width: 0;
    }
    .detail-header .account-info h2 {
        max-width: 100%;
        font-size: 1.1rem;
    }
    .detail-header .account-info p {
        padding-left: 0;
    }
    .detail-header .account-info p::before {
        content: none;
    }
    .detail-actions {
        gap: 2px;
    }
    .detail-actions .memo-edit-btn,
    .detail-actions .menu-button {
        width: 34px;
        min-width: 34px;
        min-height: 34px;
        padding: 0;
        margin-left: 0;
    }
}

/* ---------- Dashboard: category sections ---------- */
#accountsList.accounts-sectioned {
    display: block;
}
.account-section {
    margin-bottom: 1.75rem;
}
.account-section-header {
    display: flex;
    align-items: center;
    flex-wrap: wrap;
    gap: 4px 10px;
    margin: 0 0 12px;
    padding: 0 4px 8px;
    border-bottom: 1px solid var(--border-color);
}
.account-section-toggle {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    width: auto;
    min-width: 0;
    margin: 0;
    padding: 4px 2px;
    background: none;
    border: none;
    color: var(--text-color);
    font: inherit;
    font-size: 1.05rem;
    font-weight: 600;
    cursor: pointer;
}
.account-section-name {
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}
.account-section-chevron {
    font-size: 0.75rem;
    color: var(--text-secondary);
    transition: transform 0.2s ease;
}
.account-section.collapsed .account-section-chevron {
    transform: rotate(-90deg);
}
.account-section-count {
    padding: 1px 8px;
    border-radius: 999px;
    background: var(--hover-overlay, rgba(0, 0, 0, 0.05));
    color: var(--text-secondary);
    font-size: 0.75rem;
    font-weight: 600;
}
.account-section-stats {
    display: inline-flex;
    align-items: baseline;
    gap: 8px;
    margin-left: auto;
    font-variant-numeric: tabular-nums;
}
.category-subtotal-amount {
    color: var(--text-color);
    font-weight: 600;
}
.account-section-change {
    color: var(--positive-color);
    font-size: 0.85rem;
}
.account-section-change.negative {
    color: var(--negative-color);
}
.account-section-note {
    color: var(--text-secondary);
    font-size: 0.8rem;
}
.account-section-warning {
    color: var(--warning-color, #f29900);
    font-size: 0.8rem;
    align-self: center;
}
.account-section-skeleton {
    display: inline-block;
    width: 120px;
    height: 1rem;
}
.account-section-menu-wrap {
    position: relative;
}
.account-section-menu-btn {
    width: auto;
    margin: 0;
    padding: 6px 10px;
    background: none;
    border: none;
    border-radius: 6px;
    color: var(--text-secondary);
    cursor: pointer;
}
.account-section-menu-btn:hover,
.account-section-menu-btn[aria-expanded="true"] {
    background: var(--hover-overlay, rgba(0, 0, 0, 0.05));
    color: var(--text-color);
}
.section-menu {
    display: none;
    position: absolute;
    right: 0;
    top: 100%;
    z-index: 50;
    min-width: 160px;
    margin-top: 4px;
    padding: 4px 0;
    background: var(--card-bg);
    border: 1px solid var(--border-color);
    border-radius: 8px;
    box-shadow: 0 6px 18px rgba(0, 0, 0, 0.15);
}
.section-menu.show {
    display: block;
}
.section-menu-item {
    display: flex;
    align-items: center;
    gap: 10px;
    width: 100%;
    margin: 0;
    padding: 9px 14px;
    background: none;
    border: none;
    border-radius: 0;
    color: var(--text-color);
    font: inherit;
    font-size: 0.9rem;
    text-align: left;
    cursor: pointer;
}
.section-menu-item i {
    width: 14px;
    color: var(--text-secondary);
}
.section-menu-item:hover:not(:disabled) {
    background: var(--hover-overlay, rgba(0, 0, 0, 0.05));
}
.section-menu .section-menu-item:disabled {
    opacity: 0.4;
    cursor: default;
}
/* theme.css fills every disabled button in dark mode (with !important); menu items stay flat */
[data-theme="dark"] .section-menu .section-menu-item:disabled {
    background: none !important;
}
.account-section-grid[hidden] {
    display: none;
}
