/**
 * ArzPaya Price Chart V2 - Styles
 *
 * Layout:
 *  - Title (top)
 *  - Stats row (efficiency / highest / lowest / points) — justify-between, items-center
 *  - Controls row: period tabs + calendar (right), market tabs (left)
 *  - Chart canvas (bottom)
 * Includes the Jalali range picker (style derived from crypto-date-profit).
 */

.apc2-container {
    --apc2-accent: #3D72FE;
    --apc2-accent-soft: #eff6ff;
    --apc2-bg: #ffffff;
    --apc2-surface: #f8fafc;
    --apc2-border: #e5e7eb;
    --apc2-text: #1f2937;
    --apc2-text-muted: #64748b;
    --apc2-up: #16a34a;
    --apc2-down: #dc2626;

    box-sizing: border-box;
    width: 100%;
    background: var(--apc2-bg);
    border: 1px solid var(--apc2-border);
    border-radius: 16px;
    padding: 20px;
    font-family: IRANSansWeb, -apple-system, BlinkMacSystemFont, "Segoe UI", Tahoma, sans-serif;
    color: var(--apc2-text);
    direction: rtl;
}

.apc2-container *,
.apc2-container *::before,
.apc2-container *::after {
    box-sizing: border-box;
}

/* ---------------------------------------------------------------- */
/* Header + stats                                                   */
/* ---------------------------------------------------------------- */

/* Desktop: title (right) and the compact stats card (left) sit on one row. */
.apc2-header {
    display: flex;
    flex-direction: row;
    align-items: center;
    justify-content: space-between;
    gap: 12px 20px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.apc2-title {
    margin: 0;
    flex: 0 1 auto;
    font-size: 1.5rem !important;
    font-weight: 700;
    color: var(--apc2-text);
    line-height: 1.5;
}

/* Compact: hugs its content next to the title instead of stretching full-width. */
.apc2-stats {
    display: flex;
    align-items: stretch;
    justify-content: flex-end;
    gap: 0;
    flex: 0 0 auto;
    flex-wrap: nowrap;
    background: var(--apc2-surface);
    border: 1px solid var(--apc2-border);
    border-radius: 12px;
    padding: 8px 6px;
}

.apc2-stat {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    min-width: 0;
    flex: 0 0 auto;
    text-align: center;
    padding: 0 14px;
}

/* Subtle vertical dividers between the inline stats (RTL-aware). */
.apc2-stat + .apc2-stat {
    border-inline-start: 1px solid var(--apc2-border);
}

.apc2-stat-label {
    font-size: 12px;
    font-weight: 500;
    color: var(--apc2-text-muted);
    white-space: nowrap;
}

.apc2-stat-value {
    font-size: 15px;
    font-weight: 700;
    color: var(--apc2-text);
    direction: ltr;
}

.apc2-eff-badge {
    display: inline-flex;
    align-items: center;
    gap: 3px;
    padding: 3px 9px;
    border-radius: 8px;
    font-size: 13px;
    font-weight: 700;
    line-height: 1.5;
    white-space: nowrap;
}

.apc2-eff-chev {
    font-size: 10px;
    line-height: 1;
}

.apc2-eff-chev:empty {
    display: none;
}

.apc2-eff-badge.is-up {
    background: #dcfce7;
    color: #16a34a;
}

.apc2-eff-badge.is-down {
    background: #fee2e2;
    color: #dc2626;
}

.apc2-eff-badge.is-neutral {
    background: #f3f4f6;
    color: #6b7280;
}

/* ---------------------------------------------------------------- */
/* Controls (tabs)                                                  */
/* ---------------------------------------------------------------- */

.apc2-controls {
    display: flex;
    /* stretch so the market pill matches the (taller) period pill height,
       since the period pill contains the taller calendar icon. */
    align-items: stretch;
    justify-content: space-between;
    gap: 12px;
    flex-wrap: wrap;
    margin-bottom: 16px;
}

.apc2-period-tabs,
.apc2-market-tabs {
    display: flex;
    align-items: center;
    gap: 6px;
    background: var(--apc2-surface);
    border: 1px solid var(--apc2-border);
    border-radius: 12px;
    padding: 4px;
}

/* Desktop: the inner row is transparent to layout, so the period buttons,
   calendar and range badge behave exactly as direct children of the pill.
   On mobile it becomes a real flex row so the badge can sit on its own line
   below it without widening the pill (see the responsive block). */
.apc2-period-row {
    display: contents;
}

.apc2-period-btn,
.apc2-market-btn {
    border: none;
    background: transparent;
    color: var(--apc2-text-muted);
    font-family: inherit;
    font-size: 13px;
    font-weight: 600;
    padding: 7px 14px;
    border-radius: 9px;
    cursor: pointer;
    transition: background-color 0.18s ease, color 0.18s ease, box-shadow 0.18s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    line-height: 1;
    -webkit-user-select: none;
    -moz-user-select: none;
    user-select: none;
    -webkit-tap-highlight-color: transparent;
}

.apc2-period-btn:hover,
.apc2-market-btn:hover {
    color: var(--apc2-accent);
    background: var(--apc2-accent-soft);
}

.apc2-period-btn.active,
.apc2-market-btn.active {
    background: var(--apc2-accent);
    color: #fff;
    box-shadow: 0 4px 12px rgba(61, 114, 254, 0.3);
}

.apc2-period-btn:disabled,
.apc2-period-btn.apc2-tab-disabled {
    opacity: 0.4;
    cursor: not-allowed;
    pointer-events: none;
    background: transparent;
    color: var(--apc2-text-muted);
    box-shadow: none;
}

.apc2-calendar-wrap {
    position: relative;
    display: inline-flex;
}

.apc2-calendar-btn {
    padding: 7px 10px;
}

.apc2-calendar-btn svg {
    display: block;
}

/* Selected custom range badge.
   Desktop: inline inside the period-tabs pill, next to the calendar icon
   (original behavior). Mobile: wraps onto its own full-width line within the
   pill (see the responsive block) so a long Jalali range never overflows. */
.apc2-range-badge {
    display: none;
    align-items: center;
    gap: 4px;
    margin-inline-start: 6px;
    padding: 5px 10px;
    border-radius: 8px;
    background: var(--apc2-accent-soft);
    color: var(--apc2-accent);
    font-size: 12px;
    font-weight: 700;
    white-space: nowrap;
    direction: ltr;
}

.apc2-range-badge.is-visible {
    display: inline-flex;
}

.apc2-range-badge-label {
    overflow: hidden;
    text-overflow: ellipsis;
}

/* Clear (×) button: hidden on desktop (matches the original badge), shown on mobile. */
.apc2-range-clear {
    flex: 0 0 auto;
    display: none;
    align-items: center;
    justify-content: center;
    width: 18px;
    height: 18px;
    padding: 0;
    border: none;
    border-radius: 50%;
    background: rgba(61, 114, 254, 0.16);
    color: var(--apc2-accent);
    font-size: 14px;
    line-height: 1;
    cursor: pointer;
    transition: background-color 0.18s ease;
}

.apc2-range-clear:hover {
    background: rgba(61, 114, 254, 0.28);
}

/* ---------------------------------------------------------------- */
/* Range picker (Jalali)                                            */
/* ---------------------------------------------------------------- */

/* Backdrop (shared, appended to <body>). */
.apc2-range-backdrop {
    position: fixed;
    inset: 0;
    z-index: 100000;
    background: rgba(15, 23, 42, 0.5);
    animation: apc2BackdropFade 0.18s ease;
}

.apc2-range-backdrop[hidden] {
    display: none;
}

body.apc2-modal-open {
    overflow: hidden;
}

/* Desktop: centered modal. */
.apc2-range-popup {
    position: fixed;
    top: 50%;
    left: 50%;
    right: auto;
    bottom: auto;
    z-index: 100001;
    width: min(340px, calc(100vw - 32px));
    max-height: calc(100vh - 32px);
    overflow-y: auto;
    background: var(--apc2-bg);
    border: 1px solid var(--apc2-border);
    border-radius: 16px;
    box-shadow: 0 24px 60px rgba(15, 23, 42, 0.28);
    padding: 16px;
    transform: translate(-50%, -50%);
    animation: apc2ModalIn 0.2s ease;
}

.apc2-range-popup[hidden] {
    display: none;
}

@keyframes apc2BackdropFade {
    from { opacity: 0; }
    to { opacity: 1; }
}

@keyframes apc2ModalIn {
    from { opacity: 0; transform: translate(-50%, calc(-50% + 10px)); }
    to { opacity: 1; transform: translate(-50%, -50%); }
}

@keyframes apc2SheetIn {
    from { opacity: 0; transform: translateY(100%); }
    to { opacity: 1; transform: translateY(0); }
}

.apc2-range-fields {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-bottom: 12px;
}

.apc2-range-field {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 3px;
    padding: 8px 10px;
    border: 1px solid var(--apc2-border);
    border-radius: 10px;
    cursor: pointer;
    transition: border-color 0.18s ease, background-color 0.18s ease;
}

.apc2-range-field.is-active {
    border-color: var(--apc2-accent);
    background: var(--apc2-accent-soft);
}

.apc2-range-field-label {
    font-size: 11px;
    color: var(--apc2-text-muted);
}

.apc2-range-display {
    font-size: 13px;
    font-weight: 700;
    color: var(--apc2-text);
}

.apc2-range-display.is-placeholder {
    font-weight: 400;
    color: #9ca3af;
}

.apc2-range-sep {
    color: var(--apc2-text-muted);
    font-size: 14px;
}

.apc2-cal-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 6px;
    margin-bottom: 10px;
}

.apc2-cal-title {
    flex: 1;
    text-align: center;
    font-size: 14px;
    font-weight: 700;
    color: var(--apc2-text);
    white-space: nowrap;
}

.apc2-cal-nav {
    width: 30px;
    height: 30px;
    border: none;
    border-radius: 8px;
    background: var(--apc2-surface);
    color: var(--apc2-text-muted);
    font-size: 16px;
    line-height: 1;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background-color 0.18s ease, color 0.18s ease;
}

.apc2-cal-nav:hover {
    background: var(--apc2-accent-soft);
    color: var(--apc2-accent);
}

.apc2-cal-weekdays {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 3px;
    margin-bottom: 5px;
}

.apc2-cal-weekdays span {
    text-align: center;
    font-size: 11px;
    font-weight: 600;
    color: #9ca3af;
    padding: 3px 0;
}

.apc2-cal-days {
    display: grid;
    grid-template-columns: repeat(7, 1fr);
    gap: 3px;
}

.apc2-cal-day {
    width: 100%;
    aspect-ratio: 1;
    min-height: 32px;
    border: none;
    border-radius: 9px;
    background: transparent;
    color: var(--apc2-text);
    font-size: 13px;
    font-weight: 500;
    font-family: inherit;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    padding: 0;
    transition: background-color 0.15s ease, color 0.15s ease;
}

.apc2-cal-day:not(.is-disabled):not(.is-empty):hover {
    background: var(--apc2-accent-soft);
    color: var(--apc2-accent);
}

.apc2-cal-day.is-today:not(.is-selected) {
    box-shadow: inset 0 0 0 1.5px #93c5fd;
    color: var(--apc2-accent);
    font-weight: 700;
}

.apc2-cal-day.is-in-range {
    background: var(--apc2-accent-soft);
    color: var(--apc2-accent);
    border-radius: 0;
}

.apc2-cal-day.is-selected {
    background: var(--apc2-accent);
    color: #fff;
    font-weight: 700;
    box-shadow: 0 4px 12px rgba(61, 114, 254, 0.35);
}

.apc2-cal-day.is-from {
    border-radius: 0 9px 9px 0;
}

.apc2-cal-day.is-to {
    border-radius: 9px 0 0 9px;
}

.apc2-cal-day.is-disabled {
    color: #d1d5db;
    cursor: not-allowed;
}

.apc2-cal-day.is-empty {
    pointer-events: none;
}

.apc2-range-msg {
    margin: 10px 0 0;
    font-size: 12px;
    color: var(--apc2-down);
}

.apc2-range-msg[hidden] {
    display: none;
}

.apc2-range-actions {
    display: flex;
    align-items: center;
    gap: 8px;
    margin-top: 12px;
}

.apc2-range-apply,
.apc2-range-cancel {
    flex: 1;
    border: none;
    border-radius: 10px;
    font-family: inherit;
    font-size: 13px;
    font-weight: 700;
    padding: 9px 12px;
    cursor: pointer;
    transition: background-color 0.18s ease, opacity 0.18s ease;
}

.apc2-range-apply {
    background: var(--apc2-accent);
    color: #fff;
}

.apc2-range-apply:disabled {
    opacity: 0.5;
    cursor: not-allowed;
}

.apc2-range-cancel {
    background: var(--apc2-surface);
    color: var(--apc2-text-muted);
    border: 1px solid var(--apc2-border);
}

/* ---------------------------------------------------------------- */
/* Chart area                                                       */
/* ---------------------------------------------------------------- */

.apc2-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

.apc2-wrapper {
    position: relative;
    width: 100%;
}

.apc2-wrapper canvas {
    display: block;
    width: 100% !important;
}

.apc2-loading {
    position: absolute;
    inset: 0;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 10px;
    background: rgba(255, 255, 255, 0.7);
    color: var(--apc2-text-muted);
    font-size: 13px;
    z-index: 5;
}

.apc2-spinner {
    width: 32px;
    height: 32px;
    border: 3px solid var(--apc2-border);
    border-top-color: var(--apc2-accent);
    border-radius: 50%;
    animation: apc2Spin 0.8s linear infinite;
}

@keyframes apc2Spin {
    to { transform: rotate(360deg); }
}

.apc2-error {
    background: #fef2f2;
    border: 1px solid #fecaca;
    color: var(--apc2-down);
    border-radius: 10px;
    padding: 12px 14px;
    font-size: 13px;
    text-align: center;
    margin: 8px 0;
}

/* ---------------------------------------------------------------- */
/* Responsive                                                       */
/* ---------------------------------------------------------------- */

@media (max-width: 600px) {
    /* Column layout so the tabs and stats can be reordered independently. */
    .apc2-container {
        padding: 14px;
        display: flex;
        flex-direction: column;
    }

    /* Unwrap the header so the title and stats become direct flex items of the
       container and can be reordered around the controls (mobile only). */
    .apc2-header {
        display: contents;
        margin-bottom: 5px;
    }

    /* Order: title → tabs → stats → chart (tabs and stats swapped vs desktop). */
    .apc2-title {
        order: 1;
        flex: 1 1 100%;
        margin: 20px 0 !important;
        font-size: 1.25rem !important;
    }

    .apc2-controls {
        order: 2;
    }

    .apc2-stats {
        order: 3;
        flex: 1 1 100%;
        width: 100%;
        justify-content: space-between;
        padding: 10px 4px;
        margin-bottom: 16px;
    }

    .apc2-wrapper {
        order: 4;
    }

    .apc2-stat {
        flex: 1 1 0;
        padding: 0 6px;
    }

    /* Keep period + market tabs side by side on one row.
       Top-align so that when the range badge appears below the period buttons,
       only the period pill grows downward — the market pill keeps its height. */
    .apc2-controls {
        flex-wrap: nowrap;
        align-items: flex-start;
        justify-content: space-between;
        gap: 8px;
    }

    /* Each pill shares the row and may shrink; buttons inside stay on one line. */
    .apc2-period-tabs,
    .apc2-market-tabs {
        justify-content: center;
        min-width: 0;
    }

    .apc2-market-tabs {
        flex-wrap: nowrap;
    }

    /* Period pill stacks the buttons row and (when present) the range badge.
       Its width is driven solely by the buttons row; the badge sits on the
       next line and is forced to match that width (see badge rules below). */
    .apc2-period-tabs {
        display: block;
    }

    /* The buttons + calendar stay on a single inline row. */
    .apc2-period-row {
        display: flex;
        align-items: center;
        flex-wrap: nowrap;
        gap: 6px;
    }

    /* Tighter buttons so both pills fit the narrow row. The shared min-height
       (matching the calendar icon button) keeps both pills the exact same
       height at rest now that they are top-aligned instead of stretched. */
    .apc2-period-btn,
    .apc2-market-btn {
        padding: 8px 10px;
        font-size: 12px;
        min-height: 34px;
    }

    .apc2-calendar-btn {
        padding: 8px 8px;
    }

    /* Range badge drops onto its own line below the buttons row, and the
       clear (×) button appears so the custom range can be dismissed.
       width:0 makes the badge contribute nothing to the pill's intrinsic
       width (so the pill keeps the buttons-row width), while min-width:100%
       then stretches it to exactly that width. The date wraps within it. */
    .apc2-range-badge.is-visible {
        display: flex;
        width: 0;
        min-width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        justify-content: center;
        margin-inline-start: 0;
        margin-top: 4px;
        overflow: hidden;
        white-space: normal;
    }

    /* Let the date text wrap within the fixed badge width rather than overflow. */
    .apc2-range-badge-label {
        min-width: 0;
        white-space: normal;
        overflow-wrap: anywhere;
        text-align: center;
    }

    .apc2-range-clear {
        display: inline-flex;
    }

    .apc2-stat-value {
        font-size: 14px;
    }
}

/* Mobile: bottom sheet for the range picker. */
@media (max-width: 767px) {
    .apc2-range-popup {
        top: auto;
        bottom: 0;
        left: 0;
        right: 0;
        width: 100%;
        max-width: 100%;
        max-height: 85vh;
        border-radius: 18px 18px 0 0;
        padding: 16px 16px calc(16px + env(safe-area-inset-bottom, 0px)) 16px;
        transform: none;
        animation: apc2SheetIn 0.24s ease;
    }
}

/* ---------------------------------------------------------------- */
/* Dark mode                                                        */
/* ---------------------------------------------------------------- */

[data-theme="dark"] .apc2-container {
    --apc2-bg: #1e293b;
    --apc2-surface: #0f172a;
    --apc2-border: #334155;
    --apc2-text: #f1f5f9;
    --apc2-text-muted: #94a3b8;
    --apc2-accent-soft: rgba(61, 114, 254, 0.18);
}

[data-theme="dark"] .apc2-eff-badge.is-up {
    background: rgba(74, 222, 128, 0.16);
    color: #4ade80;
}

[data-theme="dark"] .apc2-eff-badge.is-down {
    background: rgba(248, 113, 113, 0.16);
    color: #f87171;
}

[data-theme="dark"] .apc2-eff-badge.is-neutral {
    background: rgba(255, 255, 255, 0.08);
    color: #9ca3af;
}

[data-theme="dark"] .apc2-loading {
    background: rgba(15, 23, 42, 0.7);
}

[data-theme="dark"] .apc2-error {
    background: rgba(220, 38, 38, 0.12);
    border-color: rgba(220, 38, 38, 0.4);
    color: #fca5a5;
}

[data-theme="dark"] .apc2-cal-day.is-disabled {
    color: #475569;
}

@media (prefers-color-scheme: dark) {
    :root:not([data-theme="light"]) .apc2-container {
        --apc2-bg: #1e293b;
        --apc2-surface: #0f172a;
        --apc2-border: #334155;
        --apc2-text: #f1f5f9;
        --apc2-text-muted: #94a3b8;
        --apc2-accent-soft: rgba(61, 114, 254, 0.18);
    }

    :root:not([data-theme="light"]) .apc2-eff-badge.is-up {
        background: rgba(74, 222, 128, 0.16);
        color: #4ade80;
    }

    :root:not([data-theme="light"]) .apc2-eff-badge.is-down {
        background: rgba(248, 113, 113, 0.16);
        color: #f87171;
    }

    :root:not([data-theme="light"]) .apc2-eff-badge.is-neutral {
        background: rgba(255, 255, 255, 0.08);
        color: #9ca3af;
    }

    :root:not([data-theme="light"]) .apc2-loading {
        background: rgba(15, 23, 42, 0.7);
    }

    :root:not([data-theme="light"]) .apc2-error {
        background: rgba(220, 38, 38, 0.12);
        border-color: rgba(220, 38, 38, 0.4);
        color: #fca5a5;
    }
}
