/* ============================================================
   Trading Grid + Deal Modal — встроено в index.html
   Палитра наследуется от style.css (TradingView Terminal)
   ============================================================ */

/* --- Trading section wrapper --- */
.trading-section {
    display: none;
    padding: 0 22px 16px;
}

.trading-section.visible {
    display: block;
}

.trading-toolbar {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    flex-wrap: wrap;
    gap: 8px;
}

.trading-title {
    font-size: 14px;
    font-weight: 600;
    color: #787b86;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.trading-sort {
    display: flex;
    gap: 4px;
}

.trading-sort-btn {
    padding: 4px 10px;
    background: #1e222d;
    border: 1px solid #2a2e39;
    border-radius: 4px;
    color: #787b86;
    font-size: 12px;
    cursor: pointer;
    transition: all 0.15s;
}

.trading-sort-btn:hover {
    color: #d1d4dc;
    border-color: #3a3e49;
}

.trading-sort-btn.active {
    background: #2a2e39;
    color: #d1d4dc;
    border-color: #8b5cf6;
}

.trading-auth-hint {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 10px 14px;
    background: rgba(139, 92, 246, 0.08);
    border: 1px solid rgba(139, 92, 246, 0.2);
    border-radius: 8px;
    margin-bottom: 12px;
    font-size: 13px;
    color: #a78bfa;
}

.trading-auth-hint a {
    color: #f5a623;
    text-decoration: none;
    font-weight: 600;
}

/* --- Grid of lot cards --- */
.trading-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
    gap: 10px;
}

.tg-card {
    background: #131722;
    border: 1px solid #2a2e39;
    border-radius: 10px;
    overflow: hidden;
    cursor: pointer;
    transition: border-color 0.15s, transform 0.1s;
    animation: tgCardIn 0.25s ease both;
}

@keyframes tgCardIn {
    from { opacity: 0; transform: translateY(8px); }
    to { opacity: 1; transform: translateY(0); }
}

.tg-card:hover {
    border-color: #8b5cf6;
    transform: translateY(-2px);
}

.tg-card-img-wrap {
    position: relative;
    width: 100%;
    aspect-ratio: 1;
    background: #1e222d;
    display: flex;
    align-items: center;
    justify-content: center;
}

.tg-card-img {
    width: 100%;
    height: 100%;
    object-fit: contain;
}

.tg-card-discount {
    position: absolute;
    top: 6px;
    right: 6px;
    padding: 2px 6px;
    border-radius: 4px;
    font-size: 11px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5);
}

.tg-card-discount.positive {
    background: rgba(22, 26, 37, 0.85);
    color: #26a69a;
}

.tg-card-discount.negative {
    background: rgba(22, 26, 37, 0.85);
    color: #ef5350;
}

.tg-card-discount.low-confidence {
    opacity: 0.35;
}

.tg-card-confidence {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 3px;
    background: #2a2e39;
    z-index: 2;
}

.tg-card-confidence-fill {
    height: 100%;
    border-radius: 0 1px 1px 0;
    background: linear-gradient(90deg, #ef5350, #ffb74d, #26a69a);
    transition: width 0.3s ease;
}

.tg-card-body {
    padding: 8px 10px 10px;
}

.tg-card-coll {
    font-size: 12px;
    color: #787b86;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.tg-card-model {
    font-size: 13px;
    font-weight: 600;
    color: #d1d4dc;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    margin-top: 1px;
}

.tg-card-number {
    font-size: 11px;
    color: #787b86;
    font-family: 'SF Mono', 'Fira Code', monospace;
    margin-top: 1px;
}

.tg-card-price {
    font-size: 14px;
    font-weight: 700;
    color: #a78bfa;
    margin-top: 4px;
    font-variant-numeric: tabular-nums;
}

.tg-card-meta {
    font-size: 11px;
    color: #787b86;
    margin-top: 2px;
}

/* --- Quick-buy button on card --- */
.tg-card-buy-wrap {
    position: relative;
    padding: 4px 6px 6px;
}

.tg-card-buy-btn {
    width: 100%;
    padding: 6px 0;
    border: none;
    border-radius: 6px;
    font-size: 11px;
    font-weight: 600;
    color: #fff;
    background: #8b5cf6;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    -webkit-user-select: none;
    user-select: none;
    touch-action: manipulation;
    transition: background 0.15s;
}

.tg-card-buy-btn:active {
    background: #7c3aed;
}

.tg-card-buy-btn .tg-buy-progress {
    position: absolute;
    left: 0;
    top: 0;
    height: 100%;
    width: 0;
    background: rgba(255, 255, 255, 0.18);
    border-radius: 6px;
    pointer-events: none;
    transition: none;
}

.tg-card-buy-btn.holding .tg-buy-progress {
    width: 100%;
    transition: width 2s linear;
}

.tg-card-buy-btn.buying {
    background: #6d28d9;
    pointer-events: none;
}

.tg-card-buy-btn.success {
    background: #22c55e;
    pointer-events: none;
}

.tg-card-buy-btn.error {
    background: #ef4444;
    pointer-events: none;
}

.tg-card.removing {
    animation: tgCardOut 0.35s ease forwards;
}

@keyframes tgCardOut {
    to { opacity: 0; transform: scale(0.85); height: 0; margin: 0; padding: 0; border-width: 0; overflow: hidden; }
}

/* --- Scroll loader / empty --- */
.trading-loader {
    display: none;
    justify-content: center;
    padding: 20px;
}

.trading-loader.visible {
    display: flex;
}

.trading-loader .tl-spinner {
    width: 24px;
    height: 24px;
    border: 2px solid #2a2e39;
    border-top-color: #8b5cf6;
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}

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

.trading-empty {
    display: none;
    text-align: center;
    padding: 40px 20px;
    color: #787b86;
    font-size: 14px;
}

.trading-empty.visible {
    display: block;
}

/* ============================================================
   Deal Modal — overlay + popup
   ============================================================ */

.tm-overlay {
    display: none;
    position: fixed;
    inset: 0;
    z-index: 9000;
    background: rgba(0, 0, 0, 0.7);
    backdrop-filter: blur(4px);
    -webkit-backdrop-filter: blur(4px);
    justify-content: center;
    align-items: flex-start;
    padding: 40px 16px;
    overflow-y: auto;
}

.tm-overlay.open {
    display: flex;
}

.tm-popup {
    width: 100%;
    max-width: 520px;
    background: #0f0f0f;
    border: 1px solid #2a2a2a;
    border-radius: 16px;
    position: relative;
    animation: tmSlideIn 0.2s ease;
}

@keyframes tmSlideIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.tm-close {
    position: absolute;
    top: 12px;
    right: 12px;
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: #1a1a1a;
    border: 1px solid #2a2a2a;
    color: #b0b0b0;
    font-size: 18px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    z-index: 1;
    transition: background 0.15s;
}

.tm-close:hover {
    background: #2a2a2a;
    color: #e0e0e0;
}

/* --- Hero --- */
.tm-hero {
    display: flex;
    align-items: center;
    gap: 14px;
    padding: 16px;
    border-bottom: 1px solid #2a2a2a;
}

.tm-hero-img {
    width: 80px;
    height: 80px;
    border-radius: 10px;
    object-fit: contain;
    background: #1a1a1a;
    flex-shrink: 0;
}

.tm-hero-info {
    flex: 1;
    min-width: 0;
}

.tm-hero-coll {
    font-size: 18px;
    font-weight: 700;
    color: #f5a623;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
}

.tm-hero-model {
    font-size: 14px;
    color: #e0e0e0;
    margin-top: 2px;
}

.tm-hero-number {
    font-size: 12px;
    color: #666;
    font-family: 'SF Mono', 'Fira Code', monospace;
    margin-top: 1px;
}

.tm-hero-props {
    display: flex;
    flex-wrap: wrap;
    gap: 4px;
    margin-top: 6px;
}

.tm-prop-tag {
    padding: 1px 7px;
    background: #2a2a2a;
    border: 1px solid #333;
    border-radius: 5px;
    font-size: 10px;
    color: #b0b0b0;
}

/* --- Price block --- */
.tm-price-block {
    padding: 16px;
    border-bottom: 1px solid #2a2a2a;
}

.tm-price-main {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.tm-price-val {
    font-size: 28px;
    font-weight: 800;
    color: #a78bfa;
    font-variant-numeric: tabular-nums;
}

.tm-price-currency {
    font-size: 16px;
    font-weight: 500;
    color: #787b86;
    margin-left: 4px;
}

.tm-buy-btn {
    padding: 10px 24px;
    background: #8b5cf6;
    color: #fff;
    border: none;
    border-radius: 8px;
    font-size: 14px;
    font-weight: 600;
    cursor: pointer;
    transition: opacity 0.15s, background 0.15s;
    white-space: nowrap;
}

.tm-buy-btn:hover {
    background: #7c3aed;
}

.tm-buy-btn:active {
    opacity: 0.8;
}

.tm-buy-btn.loading {
    background: #6d28d9;
    pointer-events: none;
}

.tm-buy-btn.success {
    background: #22c55e;
    pointer-events: none;
}

.tm-buy-btn.error {
    background: #ef4444;
}

/* --- Price compare (visual lot vs average) --- */
.tm-pc {
    margin-top: 10px;
    padding: 10px 0 4px;
}

.tm-pc-cols {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    margin-bottom: 6px;
}

.tm-pc-col {
    display: flex;
    flex-direction: column;
}

.tm-pc-col.left {
    align-items: flex-start;
}

.tm-pc-col.center {
    align-items: center;
    flex: 1;
    padding: 0 8px;
}

.tm-pc-col.right {
    align-items: flex-end;
}

.tm-pc-price {
    font-size: 15px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    color: #d1d4dc;
}

.tm-pc-col.left .tm-pc-price {
    color: #a78bfa;
}

.tm-pc-col.right .tm-pc-price {
    color: #f5a623;
}

.tm-pc-sub {
    font-size: 10px;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.3px;
    margin-top: 1px;
}

.tm-pc-diff-val {
    font-size: 13px;
    font-weight: 700;
    font-variant-numeric: tabular-nums;
    text-align: center;
    white-space: nowrap;
}

.tm-pc-diff-val.positive {
    color: #26a69a;
}

.tm-pc-diff-val.negative {
    color: #ef5350;
}

.tm-pc-bar {
    position: relative;
    height: 6px;
    background: #1e222d;
    border-radius: 3px;
    margin: 2px 0;
}

.tm-pc-dot {
    position: absolute;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    top: -3px;
    transform: translateX(-50%);
    z-index: 2;
}

.tm-pc-dot.lot {
    background: #8b5cf6;
    border: 2px solid #a78bfa;
}

.tm-pc-dot.avg {
    background: #f5a623;
    border: 2px solid #fbbf24;
}

.tm-pc-range {
    position: absolute;
    height: 6px;
    background: linear-gradient(90deg, rgba(139,92,246,0.3), rgba(245,166,35,0.3));
    border-radius: 3px;
    top: 0;
}

/* --- Floor / Order sections --- */
.tm-section {
    padding: 12px 16px;
    border-bottom: 1px solid #2a2a2a;
}

.tm-section-title {
    font-size: 12px;
    font-weight: 600;
    color: #666;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.tm-floor-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.tm-floor-adv {
    font-size: 12px;
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.tm-floor-adv.positive {
    color: #26a69a;
}

.tm-floor-adv.negative {
    color: #ef5350;
}

.tm-floor-list {
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
}

.tm-floor-item {
    padding: 3px 8px;
    background: #1a1a1a;
    border: 1px solid #2a2a2a;
    border-radius: 6px;
    font-size: 12px;
    font-variant-numeric: tabular-nums;
    color: #d1d4dc;
}

.tm-floor-item.first {
    border-color: #26a69a;
    color: #26a69a;
}

.tm-order-row {
    display: flex;
    align-items: center;
    gap: 10px;
}

.tm-order-price {
    font-size: 15px;
    font-weight: 700;
    color: #f5a623;
}

.tm-order-detail {
    font-size: 12px;
    color: #787b86;
}

/* --- Chart --- */
.tm-chart-wrap {
    padding: 12px 16px;
    border-bottom: 1px solid #2a2a2a;
}

.tm-chart-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 8px;
}

.tm-chart-point {
    font-size: 12px;
    font-weight: 600;
    color: #a78bfa;
    font-variant-numeric: tabular-nums;
}

.tm-chart-canvas-wrap {
    position: relative;
    height: 320px;
    zoom: 0.909091;
}

.tm-chart-footer {
    display: flex;
    gap: 16px;
    justify-content: center;
    padding: 6px 0 0;
    font-size: 11px;
    color: #787b86;
    font-variant-numeric: tabular-nums;
}

/* ============================================================
   Responsive
   ============================================================ */

@media (max-width: 768px) {
    .trading-section {
        padding: 0 10px 12px;
    }

    .trading-grid {
        grid-template-columns: repeat(3, 1fr);
        gap: 6px;
    }

    .tg-card {
        border-radius: 8px;
    }

    .tg-card-body {
        padding: 5px 6px 6px;
    }

    .tg-card-coll {
        font-size: 10px;
    }

    .tg-card-model {
        font-size: 11px;
    }

    .tg-card-number {
        font-size: 10px;
    }

    .tg-card-price {
        font-size: 12px;
        margin-top: 3px;
    }

    .tg-card-discount {
        font-size: 10px;
        padding: 1px 4px;
    }

    .tg-card-meta {
        font-size: 9px;
    }

    .tg-card-buy-wrap {
        padding: 3px 5px 5px;
    }

    .tg-card-buy-btn {
        font-size: 10px;
        padding: 5px 0;
    }

    .tm-overlay {
        padding: 16px 6px;
    }

    .tm-price-val {
        font-size: 22px;
    }

    .tm-hero-img {
        width: 60px;
        height: 60px;
    }

    .tm-hero-coll {
        font-size: 15px;
    }

    .tm-hero-model {
        font-size: 13px;
    }

    .trading-toolbar {
        margin-bottom: 8px;
    }

    .trading-title {
        font-size: 12px;
    }

    .trading-auth-hint {
        font-size: 12px;
        padding: 8px 10px;
    }
}

@media (max-width: 420px) {
    .trading-grid {
        grid-template-columns: repeat(2, 1fr);
        gap: 5px;
    }

    .tg-card-body {
        padding: 4px 5px 5px;
    }

    .tg-card-model {
        font-size: 10px;
    }

    .tg-card-price {
        font-size: 11px;
    }

    .tm-overlay {
        padding: 10px 4px;
    }
}

/* TG WebApp — сдвигаем модалку ниже панели Telegram */
@media (max-width: 768px) {
    body.tg-webapp .tm-overlay,
    html.tg-webapp .tm-overlay {
        padding-top: calc(var(--tg-safe-area-inset-top, 0px) + var(--tg-content-safe-area-inset-top, 0px) + 10px);
    }
}
