/* ===========================================================================
   feedback.css — badges, skeletons, toasts, modals, tooltips, progress.
   ---------------------------------------------------------------------------
   Everything the product uses to tell the user "this is the state", "this is
   loading", "this happened", or "confirm before I do something costly".
   =========================================================================== */

/* ===========================================================================
   BADGE — status pills.
   ---------------------------------------------------------------------------
   Used for deposit/withdrawal status, KYC status, order side, position state.
   Colour is never the only signal: every badge carries its own text, and the
   status set maps 1:1 to the server's enums so no UI string invents a state
   the backend cannot produce.
   =========================================================================== */
.badge {
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    padding-block: 2px;
    padding-inline: var(--space-2);
    border-radius: var(--radius-sm);
    border: 1px solid transparent;
    font-size: var(--text-xs);
    font-weight: var(--weight-semibold);
    line-height: 1.5;
    letter-spacing: var(--tracking-wide);
    white-space: nowrap;
    text-transform: uppercase;
    vertical-align: middle;
}

.badge--neutral {
    background: var(--flat-dim);
    color: var(--text-muted);
    border-color: var(--border-subtle);
}

.badge--success {
    background: var(--success-dim);
    color: var(--success);
    border-color: var(--success);
}

.badge--danger {
    background: var(--danger-dim);
    color: var(--danger);
    border-color: var(--danger);
}

.badge--warning {
    background: var(--warning-dim);
    color: var(--warning);
    border-color: var(--warning);
}

.badge--info {
    background: var(--info-dim);
    color: var(--accent-text);
    border-color: var(--info);
}

.badge--pending {
    background: var(--pending-dim);
    color: var(--pending);
    border-color: var(--pending);
}

/* Order side. Inherits the direction tokens, same as the buy/sell buttons. */
.badge--buy {
    background: var(--up-dim);
    color: var(--up);
    border-color: var(--up);
}

.badge--sell {
    background: var(--down-dim);
    color: var(--down);
    border-color: var(--down);
}

/* Leverage marker — factual, not a warning, until it is high. */
.badge--leverage {
    background: var(--surface-sunken);
    color: var(--text-secondary);
    border-color: var(--border);
    font-family: var(--font-mono);
    direction: ltr;
}

.badge--leverage[data-high="true"] {
    background: var(--warning-dim);
    color: var(--warning);
    border-color: var(--warning);
}

/* A live/pulsing dot for "connected" / "streaming". */
.badge--live {
    background: var(--up-dim);
    color: var(--up);
    border-color: var(--up);
}

.badge--live::before {
    content: "";
    width: 6px;
    height: 6px;
    border-radius: 50%;
    background: currentColor;
    animation: badge-pulse 2s var(--ease-in-out) infinite;
}

@keyframes badge-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.35; }
}

/* DEMO / SAMPLE marker. Product policy: anything that is not a real balance,
   real price or real trade must carry this where it is displayed. Dashed
   border so it is unmistakably not a normal status. */
.badge--demo {
    background: var(--pending-dim);
    color: var(--pending);
    border-color: var(--pending);
    border-style: dashed;
}

.badge--sm {
    font-size: var(--text-2xs);
    padding-inline: var(--space-1);
}

/* ===========================================================================
   SKELETON — loading placeholders.
   ---------------------------------------------------------------------------
   Used instead of a spinner wherever the shape of the incoming content is
   known, which is everywhere in this product. A skeleton that matches the
   final layout prevents the content jump that the old pages had (spinner,
   then a table appears and pushes everything down).

   Accessibility: the container carries aria-busy="true" and the real content
   replaces it wholesale, so assistive tech announces the result once rather
   than narrating grey boxes.
   =========================================================================== */
.skeleton {
    display: block;
    background: linear-gradient(
        90deg,
        var(--surface-sunken) 0%,
        var(--surface-hover) 50%,
        var(--surface-sunken) 100%
    );
    background-size: 200% 100%;
    border-radius: var(--radius-sm);
    animation: skeleton-sweep 1.4s var(--ease-in-out) infinite;
    /* Never let a skeleton be read as text. */
    color: transparent;
    user-select: none;
    pointer-events: none;
}

@keyframes skeleton-sweep {
    from { background-position: 200% 0; }
    to { background-position: -200% 0; }
}

/* In RTL the sweep should travel the other way, matching reading direction. */
[dir="rtl"] .skeleton {
    animation-direction: reverse;
}

.skeleton--text {
    height: 0.9em;
    margin-block: 0.25em;
    border-radius: var(--radius-xs);
}

.skeleton--title { height: 1.4em; width: 45%; }
.skeleton--line { width: 100%; }
.skeleton--line-short { width: 60%; }

.skeleton--value {
    height: var(--text-3xl);
    width: 8ch;
    border-radius: var(--radius-sm);
}

.skeleton--pill {
    height: 22px;
    width: 72px;
    border-radius: var(--radius-sm);
}

.skeleton--circle {
    width: 40px;
    height: 40px;
    border-radius: 50%;
}

.skeleton--block {
    height: 100%;
    min-height: 120px;
    border-radius: var(--radius-md);
}

/* A skeleton row that mirrors .table's cell rhythm, so the swap to real rows
   does not shift the layout by a pixel. */
.skeleton-row {
    display: flex;
    align-items: center;
    gap: var(--pad-cell-x);
    padding: var(--pad-cell-y) var(--pad-cell-x);
    border-block-end: 1px solid var(--border-subtle);
}

.skeleton-row > .skeleton {
    height: 0.9em;
    flex: 1 1 0;
}

/* ===========================================================================
   SPINNER — only for actions with no known result shape (a submit in flight).
   =========================================================================== */
.spinner {
    display: inline-block;
    width: 1em;
    height: 1em;
    border: 2px solid var(--border-strong);
    border-block-start-color: var(--accent);
    border-radius: 50%;
    animation: spinner-rotate 600ms linear infinite;
}

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

.spinner--lg {
    width: 32px;
    height: 32px;
    border-width: 3px;
}

/* Covers a card while it refreshes, keeping the stale data visible but
   clearly inert. Better than blanking the panel on every poll. */
.is-refreshing {
    position: relative;
}

.is-refreshing::after {
    content: "";
    position: absolute;
    inset: 0;
    background: var(--canvas);
    opacity: 0.45;
    z-index: var(--z-raised);
    pointer-events: none;
}

/* ===========================================================================
   TOAST — transient confirmations and errors.
   ---------------------------------------------------------------------------
   The region is a live region so announcements reach screen readers without
   moving focus. Errors use role="alert" (assertive); successes use
   role="status" (polite) so a filled order does not interrupt a user who is
   mid-way through typing another one.
   =========================================================================== */
.toast-region {
    position: fixed;
    inset-block-end: var(--space-5);
    inset-inline-end: var(--space-5);
    z-index: var(--z-toast);
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    width: min(380px, calc(100vw - var(--space-8)));
    pointer-events: none;
}

.toast {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--space-3) var(--space-4);
    background: var(--surface-overlay);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-md);
    box-shadow: var(--shadow-lg);
    font-size: var(--text-sm);
    line-height: var(--leading-snug);
    pointer-events: auto;
    animation: toast-in var(--duration-base) var(--ease-out);
}

/* Slides in from the inline end — so it enters from the right in LTR and
   from the left in RTL, following the region's own position. */
@keyframes toast-in {
    from {
        opacity: 0;
        transform: translateY(8px) scale(0.98);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

.toast[data-leaving="true"] {
    animation: toast-out var(--duration-fast) var(--ease-out) forwards;
}

@keyframes toast-out {
    to {
        opacity: 0;
        transform: translateY(4px);
    }
}

.toast__icon {
    flex-shrink: 0;
    font-size: var(--text-md);
    line-height: 1.3;
}

.toast__body {
    flex: 1 1 auto;
    min-width: 0;
}

.toast__title {
    font-weight: var(--weight-semibold);
    color: var(--text);
}

.toast__text {
    max-width: none;
    color: var(--text-secondary);
    overflow-wrap: anywhere;
}

.toast__close {
    flex-shrink: 0;
    padding: var(--space-1);
    margin: calc(-1 * var(--space-1));
    color: var(--text-muted);
    border-radius: var(--radius-xs);
    line-height: 1;
}

.toast__close:hover {
    color: var(--text);
    background: var(--surface-hover);
}

.toast--success { border-color: var(--success); }
.toast--success .toast__icon { color: var(--success); }

.toast--danger { border-color: var(--danger); }
.toast--danger .toast__icon { color: var(--danger); }

.toast--warning { border-color: var(--warning); }
.toast--warning .toast__icon { color: var(--warning); }

.toast--info { border-color: var(--info); }
.toast--info .toast__icon { color: var(--accent-text); }

/* On phones the region spans the width and sits above the bottom nav. */
@media (max-width: 768px) {
    .toast-region {
        inset-inline: var(--space-3);
        inset-block-end: calc(var(--bottom-nav-height) + var(--space-3));
        width: auto;
    }
}

/* ===========================================================================
   MODAL — built on <dialog> so focus trapping, Esc-to-close and inertness of
   the background come from the platform rather than from JS.
   =========================================================================== */
.modal {
    /* <dialog> defaults to a white box with a black border. */
    padding: 0;
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-lg);
    background: var(--surface-overlay);
    color: var(--text);
    box-shadow: var(--shadow-overlay);
    width: min(520px, calc(100vw - var(--space-8)));
    max-height: min(85vh, 780px);
    max-width: none;
    overflow: hidden;
    /* The element is a flex column so the body scrolls and header/footer
       stay put. */
    display: flex;
    flex-direction: column;
}

.modal:not([open]) {
    display: none;
}

.modal::backdrop {
    background: rgba(3, 7, 15, 0.72);
    backdrop-filter: blur(3px);
}

.modal[open] {
    animation: modal-in var(--duration-base) var(--ease-out);
}

@keyframes modal-in {
    from {
        opacity: 0;
        transform: translateY(10px) scale(0.985);
    }
    to {
        opacity: 1;
        transform: none;
    }
}

.modal__header {
    display: flex;
    align-items: flex-start;
    gap: var(--space-3);
    padding: var(--pad-card);
    border-block-end: 1px solid var(--border-subtle);
}

.modal__title {
    font-size: var(--text-lg);
    font-weight: var(--weight-bold);
    min-width: 0;
}

.modal__close {
    margin-inline-start: auto;
    flex-shrink: 0;
}

.modal__body {
    padding: var(--pad-card);
    overflow-y: auto;
    flex: 1 1 auto;
    display: flex;
    flex-direction: column;
    gap: var(--gap-stack);
}

.modal__footer {
    padding: var(--pad-card-sm) var(--pad-card);
    border-block-start: 1px solid var(--border-subtle);
    background: var(--surface-sunken);
    display: flex;
    align-items: center;
    gap: var(--space-3);
    justify-content: flex-end;
    flex-wrap: wrap;
}

.modal--sm { width: min(420px, calc(100vw - var(--space-8))); }
.modal--lg { width: min(780px, calc(100vw - var(--space-8))); }

/* A destructive confirmation reads differently from an informational dialog. */
.modal--confirm .modal__header {
    background: var(--danger-dim);
    border-block-end-color: var(--danger);
}

.modal--confirm .modal__title {
    color: var(--danger);
}

/* On phones a dialog becomes a bottom sheet — reachable by thumb. */
@media (max-width: 560px) {
    .modal {
        width: 100%;
        max-width: 100%;
        max-height: 90dvh;
        margin: 0;
        margin-block-start: auto;
        border-radius: var(--radius-xl) var(--radius-xl) 0 0;
        border-inline: 0;
        border-block-end: 0;
    }

    .modal[open] {
        animation: sheet-in var(--duration-base) var(--ease-out);
    }

    @keyframes sheet-in {
        from { transform: translateY(100%); }
        to { transform: none; }
    }

    .modal__footer {
        flex-direction: column-reverse;
    }

    .modal__footer > .btn {
        width: 100%;
    }
}

/* ===========================================================================
   TOOLTIP — for abbreviations and metric definitions ("free margin", "MMR").
   CSS-only, keyboard reachable because the trigger is focusable.
   Never used for essential information: it is unavailable on touch.
   =========================================================================== */
.tip {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: var(--space-1);
    border-block-end: 1px dotted var(--text-faint);
    cursor: help;
}

.tip__bubble {
    position: absolute;
    inset-block-end: calc(100% + var(--space-2));
    inset-inline-start: 50%;
    transform: translateX(-50%) translateY(4px);
    z-index: var(--z-tooltip);
    width: max-content;
    max-width: 260px;
    padding: var(--space-2) var(--space-3);
    background: var(--surface-overlay);
    border: 1px solid var(--border-strong);
    border-radius: var(--radius-sm);
    box-shadow: var(--shadow-md);
    font-size: var(--text-xs);
    font-weight: var(--weight-regular);
    line-height: var(--leading-snug);
    color: var(--text-secondary);
    text-transform: none;
    letter-spacing: normal;
    white-space: normal;
    text-align: start;
    opacity: 0;
    visibility: hidden;
    transition:
        opacity var(--duration-fast) var(--ease-out),
        transform var(--duration-fast) var(--ease-out),
        visibility var(--duration-fast);
}

.tip:hover .tip__bubble,
.tip:focus-visible .tip__bubble,
.tip:focus-within .tip__bubble {
    opacity: 1;
    visibility: visible;
    transform: translateX(-50%);
}

[dir="rtl"] .tip__bubble {
    transform: translateX(50%) translateY(4px);
}

[dir="rtl"] .tip:hover .tip__bubble,
[dir="rtl"] .tip:focus-within .tip__bubble {
    transform: translateX(50%);
}

/* On a phone a 260px bubble centred on a small "?" near a tile's edge cannot
   fit: in the two-column stat grid the hidden bubbles alone pushed the page to
   402px in a 360px screen, and the browser zoomed the whole page out to fit.
   There the explanation is a full-width strip above the bottom nav instead -
   fixed, so it never widens the page, and always inside the screen. */
@media (max-width: 480px) {
    .tip__bubble,
    [dir="rtl"] .tip__bubble,
    .tip:hover .tip__bubble,
    .tip:focus-within .tip__bubble,
    [dir="rtl"] .tip:hover .tip__bubble,
    [dir="rtl"] .tip:focus-within .tip__bubble {
        position: fixed;
        inset-inline: var(--space-4);
        inset-block-start: auto;
        inset-block-end: calc(var(--bottom-nav-height, 62px) + env(safe-area-inset-bottom, 0px) + var(--space-3));
        width: auto;
        max-width: none;
        transform: none;
    }
}

/* ===========================================================================
   PROGRESS — margin usage, KYC steps, upload progress.
   =========================================================================== */
.meter {
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
}

.meter__head {
    display: flex;
    align-items: baseline;
    gap: var(--space-2);
    font-size: var(--text-xs);
}

.meter__label {
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: var(--tracking-caps);
    font-weight: var(--weight-semibold);
}

.meter__value {
    margin-inline-start: auto;
    font-family: var(--font-mono);
    font-variant-numeric: tabular-nums;
    font-weight: var(--weight-semibold);
    color: var(--text);
    direction: ltr;
}

.meter__track {
    height: 6px;
    background: var(--surface-sunken);
    border-radius: var(--radius-pill);
    overflow: hidden;
}

.meter__fill {
    height: 100%;
    width: 0;
    background: var(--accent);
    border-radius: var(--radius-pill);
    transition:
        width var(--duration-slow) var(--ease-out),
        background-color var(--duration-base) var(--ease-out);
}

/* Margin usage thresholds. The colour shift is a warning in its own right,
   and the numeric value beside it carries the same information as text. */
.meter[data-level="warning"] .meter__fill { background: var(--warning); }
.meter[data-level="danger"] .meter__fill { background: var(--danger); }
.meter[data-level="danger"] .meter__value { color: var(--danger); }

/* ===========================================================================
   STEPS — KYC submission, deposit flow, withdrawal review.
   Gives the user the "where am I" the old status pages lacked.
   =========================================================================== */
.steps {
    display: flex;
    align-items: flex-start;
    gap: var(--space-2);
    list-style: none;
    padding: 0;
    counter-reset: step;
}

.step {
    flex: 1 1 0;
    min-width: 0;
    display: flex;
    flex-direction: column;
    gap: var(--space-2);
    font-size: var(--text-xs);
    color: var(--text-muted);
    text-align: center;
}

.step__marker {
    position: relative;
    display: grid;
    place-content: center;
    width: 28px;
    height: 28px;
    margin-inline: auto;
    border-radius: 50%;
    background: var(--surface-sunken);
    border: 1.5px solid var(--border);
    font-family: var(--font-mono);
    font-size: var(--text-xs);
    font-weight: var(--weight-bold);
    color: var(--text-muted);
    counter-increment: step;
}

.step__marker::before {
    content: counter(step);
}

/* The connector line between markers. Uses inset-inline so it runs the
   correct way in RTL. */
.step:not(:last-child) .step__marker::after {
    content: "";
    position: absolute;
    inset-block-start: 50%;
    inset-inline-start: calc(100% + 1.5px);
    width: calc(100% + var(--space-2) * 2);
    height: 1.5px;
    background: var(--border);
}

.step__label {
    font-weight: var(--weight-medium);
    overflow-wrap: anywhere;
}

.step[data-state="done"] .step__marker {
    background: var(--success);
    border-color: var(--success);
    color: #04160d;
}

.step[data-state="done"] .step__marker::before {
    content: "✓";
}

.step[data-state="done"] .step__label {
    color: var(--text-secondary);
}

.step[data-state="current"] .step__marker {
    background: var(--accent);
    border-color: var(--accent);
    color: var(--text-on-accent);
    box-shadow: 0 0 0 4px var(--accent-dim);
}

.step[data-state="current"] .step__label {
    color: var(--text);
    font-weight: var(--weight-semibold);
}

.step[data-state="failed"] .step__marker {
    background: var(--danger);
    border-color: var(--danger);
    color: #fff;
}

.step[data-state="failed"] .step__marker::before {
    content: "!";
}

/* Vertical on phones: four horizontal labels do not fit at 360px. */
@media (max-width: 560px) {
    .steps {
        flex-direction: column;
        gap: var(--space-3);
    }

    .step {
        flex-direction: row;
        align-items: center;
        gap: var(--space-3);
        text-align: start;
    }

    .step__marker {
        margin-inline: 0;
    }

    .step:not(:last-child) .step__marker::after {
        display: none;
    }
}

/* ---------------------------------------------------------------------------
   VIEWER — a modal whose job is to show one image as large as it fits.

   Reuses the <dialog> from .modal so Esc, the backdrop, focus trapping and
   scroll locking all keep working; only the box is bigger and the body loses
   its padding so the image can reach the edges.
   --------------------------------------------------------------------------- */
.modal--viewer {
    width: min(1400px, calc(100vw - var(--space-6)));
    max-height: calc(100vh - var(--space-6));
}

.modal--viewer .modal__body {
    display: flex;
    align-items: center;
    justify-content: center;
    padding: var(--space-2);
    background: var(--surface-sunken);
    min-height: 0;
}

.modal--viewer .viewer__img {
    max-width: 100%;
    /* Leaves room for the header and footer inside the dialog's max-height. */
    max-height: calc(100vh - 220px);
    object-fit: contain;
    border-radius: var(--radius-sm);
}

/* The counter sits with the paging buttons and must not be pushed off by them
   on a phone, where the footer wraps. */
.modal--viewer .viewer__count {
    font-size: var(--text-xs);
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
    margin-inline-end: auto;
}
