/* backtest.css — Strategy Backtest page styles */

/* ── Form layout ───────────────────────────────────────────────────────── */

.backtest-form {
    display: flex;
    flex-direction: column;
    gap: 1.25rem;
}

.form-section {
    display: flex;
    flex-direction: column;
    gap: 0.5rem;
}

.form-section h3 {
    margin: 0 0 0.25rem;
    font-size: 0.8rem;
    color: var(--text-secondary, var(--text-muted));
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
}

.form-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(140px, 1fr));
    gap: 0.75rem;
}

.form-group {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    min-width: 0;
}

.form-group label {
    font-size: 0.72rem;
    color: var(--text-muted);
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 600;
    margin: 0;
}

.form-group input,
.form-group select {
    width: 100%;
    min-width: 0;
    box-sizing: border-box;
    padding: 0.5rem 0.6rem;
    background: var(--surface-1);
    border: 1px solid var(--border-subtle, var(--border));
    border-radius: 0.4rem;
    color: var(--text-primary);
    font-size: 0.9rem;
    font-family: inherit;
}

.form-group input[type="number"] {
    font-family: monospace;
}

.form-group input:focus,
.form-group select:focus {
    border-color: var(--primary);
    outline: none;
}

.field-description {
    font-size: 0.72rem;
    color: var(--text-muted);
    line-height: 1.35;
}

.section-hint {
    font-size: 0.8rem;
    color: var(--text-muted);
    margin: 0;
    line-height: 1.45;
}

/* ── Advanced tuning toggle ────────────────────────────────────────────── */

.advanced-toggle {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: 100%;
    background: none;
    border: 1px solid var(--border-subtle, var(--border));
    border-radius: 0.4rem;
    padding: 0.5rem 0.75rem;
    cursor: pointer;
    color: var(--text-secondary, var(--text-muted));
    font-family: inherit;
    font-size: inherit;
    text-align: left;
    transition: background-color 0.15s;
}

.advanced-toggle:hover {
    background: var(--surface-2);
}

.advanced-toggle h3 {
    margin: 0;
    font-size: 0.8rem;
    color: inherit;
    text-transform: uppercase;
    letter-spacing: 0.05em;
}

.advanced-toggle .toggle-icon {
    font-size: 1.1rem;
    transition: transform 0.2s;
}

.advanced-toggle[aria-expanded="true"] .toggle-icon {
    transform: rotate(180deg);
}

.collapsible-content {
    overflow: hidden;
    transition: max-height 0.3s ease, opacity 0.2s ease, margin-top 0.2s ease;
    max-height: 800px;
    opacity: 1;
    margin-top: 0.75rem;
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.collapsible-content.collapsed {
    max-height: 0;
    opacity: 0;
    margin-top: 0;
}

/* ── Form actions ──────────────────────────────────────────────────────── */

.form-actions {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.75rem;
    flex-wrap: wrap;
    margin-top: 0.25rem;
}

.form-actions-primary {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    flex-wrap: wrap;
}

.form-actions-secondary {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    flex-wrap: wrap;
}

.form-actions .button {
    min-width: 0;
}

/* ── Spinner ───────────────────────────────────────────────────────────── */

.htmx-indicator {
    display: none;
}

.htmx-request .htmx-indicator,
.htmx-request.htmx-indicator {
    display: flex;
}

.spinner-container {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-size: 0.85rem;
    color: var(--text-muted);
}

.spinner {
    display: inline-block;
    width: 14px;
    height: 14px;
    border: 2px solid var(--border);
    border-top-color: var(--text-secondary, var(--text-muted));
    border-radius: 50%;
    animation: spin 0.6s linear infinite;
}

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

/* ── Mobile ────────────────────────────────────────────────────────────── */

@media (max-width: 640px) {
    .form-row {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .form-actions,
    .form-actions-primary,
    .form-actions-secondary {
        align-items: stretch;
        width: 100%;
    }

    .form-actions-primary,
    .form-actions-secondary {
        display: grid;
        grid-template-columns: 1fr;
    }

    .form-actions .button {
        width: 100%;
        justify-content: center;
    }
}

/* ── Results panel ─────────────────────────────────────────────────────── */

.bt-results {
    display: flex;
    flex-direction: column;
    gap: 1.5rem;
    animation: page-fade-in 0.25s ease both;
    min-width: 0;
}

.bt-results section {
    max-width: none;
    margin: 0;
    padding: 0;
    background: transparent;
    border-radius: 0;
    box-shadow: none;
    text-align: initial;
    box-sizing: border-box;
}

.bt-results-header h2 {
    margin: 0;
    font-size: 1.1rem;
    font-weight: 700;
    color: var(--text-primary);
}

/* ── Stat cards grid ───────────────────────────────────────────────────── */

.bt-stat-grid {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
    gap: 0.75rem;
}

.bt-stat-card {
    background: var(--surface-2);
    border: 1px solid var(--border-subtle, var(--border));
    border-radius: 0.5rem;
    padding: 0.85rem 1rem;
    display: flex;
    flex-direction: column;
    gap: 0.2rem;
    position: relative;
    overflow: hidden;
}

.bt-stat-card::before {
    content: '';
    position: absolute;
    inset: 0 auto 0 0;
    width: 3px;
    background: var(--border-subtle, var(--border));
    border-radius: 3px 0 0 3px;
}

.bt-stat-card.bt-stat--positive::before {
    background: var(--success);
}

.bt-stat-card.bt-stat--negative::before {
    background: var(--error);
}

.bt-stat-card.bt-stat--warn::before {
    background: var(--warning, #d69e2e);
}

.bt-stat-label {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
    color: var(--text-muted);
}

.bt-stat-value {
    font-size: 1.35rem;
    font-weight: 700;
    color: var(--text-primary);
    line-height: 1.15;
    font-variant-numeric: tabular-nums;
}

.bt-stat-card.bt-stat--positive .bt-stat-value {
    color: var(--success-light, var(--success));
}

.bt-stat-card.bt-stat--negative .bt-stat-value {
    color: var(--error-light, var(--error));
}

.bt-stat-sub {
    font-size: 0.75rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

/* ── Wallet strip ──────────────────────────────────────────────────────── */

.bt-wallet-strip {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 0.6rem 1rem;
    background: var(--surface-2);
    border: 1px solid var(--border-subtle, var(--border));
    border-radius: 0.5rem;
    padding: 0.65rem 1rem;
    font-size: 0.85rem;
}

.bt-wallet-label {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.06em;
    font-weight: 600;
    color: var(--text-muted);
    margin-right: 0.25rem;
}

.bt-wallet-pair {
    display: inline-flex;
    align-items: center;
    gap: 0.35rem;
    min-width: 0;
}

.bt-wallet-asset {
    font-weight: 700;
    color: var(--text-secondary);
    font-size: 0.78rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
}

.bt-wallet-from {
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.bt-wallet-arrow {
    color: var(--text-muted);
}

.bt-wallet-to {
    color: var(--text-primary);
    font-weight: 600;
    font-variant-numeric: tabular-nums;
}

.bt-wallet-to.bt-wallet-up {
    color: var(--success-light, var(--success));
}

.bt-wallet-to.bt-wallet-down {
    color: var(--error-light, var(--error));
}

.bt-wallet-sep {
    color: var(--border);
}

/* ── Chart ─────────────────────────────────────────────────────────────── */

.bt-chart-section {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
}

.bt-section-title {
    margin: 0;
    font-size: 0.8rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
    color: var(--text-muted);
}

.bt-chart-wrap {
    position: relative;
    background: var(--surface-2);
    border: 1px solid var(--border-subtle, var(--border));
    border-radius: 0.5rem;
    padding: 1rem;
    max-height: 320px;
}

.bt-chart-wrap canvas {
    max-height: 280px;
}

/* ── Trade log ─────────────────────────────────────────────────────────── */

.bt-tradelog-section {
    display: flex;
    flex-direction: column;
    gap: 0.75rem;
    min-width: 0;
}

.table-scroll {
    max-width: 100%;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
}

.bt-section-header {
    display: flex;
    align-items: center;
    gap: 0.75rem;
}

.bt-trade-table {
    width: 100%;
    min-width: max-content;
    border-collapse: collapse;
    font-size: 0.83rem;
}

.bt-trade-table th {
    font-size: 0.68rem;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    font-weight: 600;
    color: var(--text-muted);
    text-align: left;
    padding: 0.5rem 0.6rem;
    border-bottom: 1px solid var(--border-subtle, var(--border));
    white-space: nowrap;
}

.bt-trade-table td {
    padding: 0.45rem 0.6rem;
    color: var(--text-secondary);
    border-bottom: 1px solid color-mix(in srgb, var(--border-subtle, var(--border)) 50%, transparent);
    vertical-align: middle;
}

.bt-trade-row:hover td {
    background: var(--surface-2);
}

.bt-trade-row--open td {
    background: color-mix(in srgb, var(--primary, #4299e1) 5%, transparent);
}

.bt-td-time {
    white-space: nowrap;
    color: var(--text-muted);
    font-size: 0.78rem;
}

.bt-td-mono {
    font-family: monospace;
    font-variant-numeric: tabular-nums;
}

/* Direction badges */
.bt-dir-badge {
    display: inline-block;
    padding: 0.2rem 0.55rem;
    border-radius: 9999px;
    font-size: 0.72rem;
    font-weight: 600;
    letter-spacing: 0.02em;
    white-space: nowrap;
}

.bt-dir-badge.bt-dir-sell {
    background: color-mix(in srgb, var(--error) 18%, transparent);
    color: var(--error-light, var(--error));
    border: 1px solid color-mix(in srgb, var(--error) 35%, transparent);
}

.bt-dir-badge.bt-dir-buy {
    background: color-mix(in srgb, var(--success) 18%, transparent);
    color: var(--success-light, var(--success));
    border: 1px solid color-mix(in srgb, var(--success) 35%, transparent);
}

/* Delta colouring */
.bt-delta--pos {
    color: var(--success-light, var(--success));
}

.bt-delta--neg {
    color: var(--error-light, var(--error));
}

/* Expand button */
.bt-expand-cell {
    width: 2rem;
    padding-right: 0;
}

.bt-expand-btn {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.85rem;
    padding: 0.1rem 0.3rem;
    border-radius: 0.25rem;
    transition: color 0.15s, background 0.15s;
    line-height: 1;
}

.bt-expand-btn:hover {
    color: var(--text-primary);
    background: var(--surface-3);
}

/* Detail row */
.bt-detail-row td {
    padding: 0.5rem 0.6rem 0.75rem 2.5rem;
    background: color-mix(in srgb, var(--surface-2) 60%, transparent);
    border-bottom: 1px solid var(--border-subtle, var(--border));
}

.bt-detail-dl {
    display: grid;
    grid-template-columns: auto 1fr;
    gap: 0.25rem 1rem;
    margin: 0;
    font-size: 0.8rem;
}

.bt-detail-dl dt {
    color: var(--text-muted);
    font-size: 0.7rem;
    text-transform: uppercase;
    letter-spacing: 0.04em;
    font-weight: 600;
    align-self: center;
}

.bt-detail-dl dd {
    margin: 0;
    color: var(--text-secondary);
    font-family: monospace;
    font-variant-numeric: tabular-nums;
}

.bt-trade-truncated {
    margin: 0;
    font-size: 0.78rem;
    color: var(--text-muted);
    text-align: right;
}

/* ── Results mobile ────────────────────────────────────────────────────── */

@media (max-width: 640px) {
    .bt-stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .bt-results {
        gap: 1rem;
    }

    .bt-results-header {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 0.5rem;
    }

    .bt-stat-value {
        font-size: 1.1rem;
    }

    .bt-wallet-strip {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 0.5rem;
        padding: 0.75rem;
    }

    .bt-wallet-label {
        grid-column: 1 / -1;
        margin-right: 0;
    }

    .bt-wallet-sep {
        display: none;
    }

    .bt-wallet-pair {
        justify-content: space-between;
        border: 1px solid var(--border-subtle, var(--border));
        border-radius: 0.4rem;
        padding: 0.45rem 0.55rem;
        background: var(--surface-1);
    }

    .table-scroll {
        margin-inline: -0.25rem;
        padding-bottom: 0.35rem;
    }

    .bt-trade-table {
        font-size: 0.78rem;
    }

    .bt-trade-table th,
    .bt-trade-table td {
        padding: 0.4rem 0.45rem;
    }

    .bt-detail-row td {
        padding-left: 0.75rem;
    }

    .opt-use-btn {
        padding: 0.25rem 0.5rem;
        font-size: 0.75rem;
    }

    .bt-chart-wrap {
        padding: 0.6rem;
    }
}

/* ── Workspace (history sidebar + results) ─────────────────────────────── */

.bt-workspace {
    display: flex;
    gap: 1rem;
    align-items: flex-start;
}

.bt-history-sidebar {
    flex: 0 0 220px;
    min-width: 0;
    position: sticky;
    top: 4.5rem;
}

.bt-results-area {
    flex: 1 1 0;
    min-width: 0;
}

/* ── History panel ─────────────────────────────────────────────────────── */

.history-panel {
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 0.5rem;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}

.history-panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.65rem 0.75rem;
    border-bottom: 1px solid var(--border);
}

.history-panel-title {
    font-size: 0.78rem;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 0.05em;
    color: var(--text-muted);
}

.history-panel-close {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 0.8rem;
    padding: 0.1rem 0.25rem;
    border-radius: 0.25rem;
    line-height: 1;
    transition: color 0.15s;
}

.history-panel-close:hover {
    color: var(--text-primary);
}

.history-tabs {
    display: flex;
    border-bottom: 1px solid var(--border);
}

.history-tab {
    flex: 1;
    background: none;
    border: none;
    border-bottom: 2px solid transparent;
    padding: 0.5rem 0.25rem;
    font-size: 0.75rem;
    font-weight: 600;
    color: var(--text-muted);
    cursor: pointer;
    transition: color 0.15s, border-color 0.15s;
    font-family: inherit;
    margin-bottom: -1px;
}

.history-tab:hover {
    color: var(--text-secondary);
}

.history-tab--active {
    color: var(--text-primary);
    border-bottom-color: var(--primary, #4299e1);
}

.history-list {
    display: flex;
    flex-direction: column;
    overflow-y: auto;
    max-height: 480px;
}

.history-empty {
    font-size: 0.78rem;
    color: var(--text-muted);
    text-align: center;
    padding: 1.25rem 0.75rem;
    margin: 0;
}

.history-item {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    padding: 0.55rem 0.75rem;
    border-bottom: 1px solid color-mix(in srgb, var(--border) 50%, transparent);
    cursor: pointer;
    transition: background 0.12s;
    position: relative;
}

.history-item:last-child {
    border-bottom: none;
}

.history-item:hover {
    background: var(--surface-3, var(--surface-1));
}

.history-item--active {
    background: color-mix(in srgb, var(--primary, #4299e1) 8%, transparent);
}

.history-item-label {
    font-size: 0.78rem;
    color: var(--text-secondary);
    font-weight: 500;
    padding-right: 1.5rem;
    line-height: 1.3;
}

.history-item-ttl {
    font-size: 0.68rem;
    color: var(--text-muted);
    font-variant-numeric: tabular-nums;
}

.history-item-ttl--urgent {
    color: var(--warning, #d69e2e);
}

.history-item-delete {
    position: absolute;
    top: 0.45rem;
    right: 0.5rem;
    background: none;
    border: none;
    color: var(--text-muted);
    font-size: 0.75rem;
    cursor: pointer;
    padding: 0.15rem 0.3rem;
    border-radius: 0.25rem;
    opacity: 0;
    transition: opacity 0.15s, color 0.15s;
    line-height: 1;
}

.history-item:hover .history-item-delete {
    opacity: 1;
}

.history-item-delete:hover {
    color: var(--error);
}

/* ── Saved configs drawer ──────────────────────────────────────────────── */

.configs-drawer-backdrop {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.45);
    z-index: 200;
    animation: fade-in 0.2s ease;
}

.configs-drawer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    width: min(380px, 90vw);
    background: var(--surface-1);
    border-left: 1px solid var(--border);
    z-index: 201;
    display: flex;
    flex-direction: column;
    animation: slide-in-right 0.22s ease;
    box-shadow: -4px 0 20px rgba(0, 0, 0, 0.25);
}

@keyframes slide-in-right {
    from { transform: translateX(100%); }
    to   { transform: translateX(0); }
}

@keyframes fade-in {
    from { opacity: 0; }
    to   { opacity: 1; }
}

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

.configs-drawer-title {
    font-size: 0.9rem;
    font-weight: 700;
    color: var(--text-primary);
}

.configs-drawer-close {
    background: none;
    border: none;
    cursor: pointer;
    color: var(--text-muted);
    font-size: 1rem;
    padding: 0.2rem 0.4rem;
    border-radius: 0.25rem;
    transition: color 0.15s;
    line-height: 1;
}

.configs-drawer-close:hover {
    color: var(--text-primary);
}

.configs-save-row {
    display: flex;
    gap: 0.5rem;
    padding: 0.85rem 1.25rem;
    border-bottom: 1px solid var(--border);
    flex-shrink: 0;
}

.configs-name-input {
    flex: 1;
    min-width: 0;
    padding: 0.5rem 0.6rem;
    background: var(--surface-2);
    border: 1px solid var(--border);
    border-radius: 0.4rem;
    color: var(--text-primary);
    font-size: 0.875rem;
    font-family: inherit;
}

.configs-name-input:focus {
    border-color: var(--primary);
    outline: none;
}

.configs-save-btn {
    flex-shrink: 0;
    white-space: nowrap;
}

.configs-list {
    flex: 1;
    overflow-y: auto;
    padding: 0.5rem 0;
}

.configs-empty {
    font-size: 0.82rem;
    color: var(--text-muted);
    text-align: center;
    padding: 2rem 1.25rem;
    margin: 0;
}

.configs-item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 0.5rem;
    padding: 0.65rem 1.25rem;
    border-bottom: 1px solid color-mix(in srgb, var(--border) 50%, transparent);
    transition: background 0.12s;
}

.configs-item:last-child {
    border-bottom: none;
}

.configs-item:hover {
    background: var(--surface-2);
}

.configs-item-info {
    display: flex;
    flex-direction: column;
    gap: 0.1rem;
    min-width: 0;
}

.configs-item-name {
    font-size: 0.875rem;
    font-weight: 500;
    color: var(--text-primary);
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
}

.configs-item-date {
    font-size: 0.7rem;
    color: var(--text-muted);
}

.configs-item-actions {
    display: flex;
    gap: 0.35rem;
    flex-shrink: 0;
}

.configs-load-btn {
    font-size: 0.75rem;
    padding: 0.25rem 0.6rem;
}

.configs-delete-btn {
    background: none;
    border: 1px solid transparent;
    color: var(--text-muted);
    font-size: 0.78rem;
    cursor: pointer;
    padding: 0.25rem 0.4rem;
    border-radius: 0.35rem;
    transition: color 0.15s, border-color 0.15s;
    font-family: inherit;
    line-height: 1;
}

.configs-delete-btn:hover {
    color: var(--error);
    border-color: color-mix(in srgb, var(--error) 40%, transparent);
}

/* ── Workspace mobile ──────────────────────────────────────────────────── */

@media (max-width: 640px) {
    .bt-workspace {
        flex-direction: column;
        width: 100%;
        align-items: stretch;
    }

    .bt-history-sidebar {
        flex: none;
        width: 100%;
        position: static;
    }

    .bt-results-area {
        width: 100%;
    }

    .bt-stat-grid {
        grid-template-columns: repeat(2, minmax(0, 1fr));
    }

    .bt-stat-value {
        font-size: 1.1rem;
    }

    .bt-chart-wrap {
        padding: 0.6rem;
    }
}
