/* Global */
* {
    margin: 0;
    padding: 0;
    box-sizing: border-box;
}

body {
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen', 'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue', sans-serif;
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: #333;
    min-height: 100vh;
    padding: 20px;
}

.dashboard-container {
    max-width: 1800px;
    margin: 0 auto;
    background: white;
    border-radius: 12px;
    box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
    overflow: hidden;
}

/* Header */
.dashboard-header {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 20px 30px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    flex-wrap: wrap;
    gap: 15px;
}

.error-banner {
    width: 100%;
    background: rgba(244, 67, 54, 0.18);
    border: 1px solid rgba(244, 67, 54, 0.35);
    color: white;
    padding: 10px 12px;
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.3;
    white-space: pre-wrap;
}

.dashboard-header h1 {
    font-size: 24px;
    font-weight: 600;
}

.header-controls {
    display: flex;
    align-items: center;
    gap: 15px;
    flex-wrap: wrap;
}

.market-selector {
    padding: 8px 15px;
    border: none;
    border-radius: 6px;
    background: rgba(255, 255, 255, 0.2);
    color: white;
    font-size: 14px;
    cursor: pointer;
    min-width: 200px;
}

.market-selector option {
    background: white;
    color: #333;
}

.btn {
    padding: 8px 20px;
    border: none;
    border-radius: 6px;
    font-size: 14px;
    cursor: pointer;
    transition: all 0.3s;
}

.btn-primary {
    background: white;
    color: #667eea;
    font-weight: 600;
}

.btn-primary:hover {
    background: #f0f0f0;
    transform: translateY(-1px);
}

.status-indicator {
    display: flex;
    align-items: center;
    gap: 8px;
    font-size: 12px;
}

.status-dot {
    width: 10px;
    height: 10px;
    border-radius: 50%;
    background: #4caf50;
    animation: pulse 2s infinite;
}

.status-dot.disconnected {
    background: #f44336;
    animation: none;
}

@keyframes pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: 0.5; }
}

/* Main content */
.dashboard-content {
    padding: 20px;
    min-height: 600px;
}

.dashboard-window {
    display: flex;
    flex-direction: column;
    gap: 20px;
    min-width: 0;
    min-height: 0;
}

.window-title {
    font-size: 12px;
    font-weight: 700;
    letter-spacing: 0.08em;
    text-transform: uppercase;
    color: rgba(0, 0, 0, 0.55);
    padding: 0 4px;
}

.window-body {
    flex: 1;
    min-height: 0;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Bottom (Analysis) should always be scrollable if it overflows */
.dashboard-bottom .window-body {
    overflow-y: scroll;
    scrollbar-gutter: stable;
}

.dashboard-layout {
    display: grid;
    /* minmax(0, …) is critical so grid children can shrink and scroll */
    grid-template-rows: minmax(0, 4fr) minmax(0, 1fr); /* ~80% / ~20% */
    gap: 20px;
    min-height: 600px;
    height: calc(100vh - 200px);
}

.dashboard-top {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 20px;
    min-height: 0;
}

.dashboard-bottom {
    min-height: 0;
}

/* Panels */
.panel {
    background: white;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
    overflow: hidden;
}

.panel-header {
    background: #f8f9fa;
    padding: 15px 20px;
    border-bottom: 1px solid #e9ecef;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.panel-header h2 {
    font-size: 18px;
    font-weight: 600;
    color: #333;
}

.market-info {
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    gap: 5px;
    font-size: 12px;
    color: #666;
}

.spot-price {
    font-weight: 600;
    color: #667eea;
}

/* Orderbook */
.orderbook-panel {
    display: flex;
    flex-direction: column;
    min-height: 520px;
}

.orderbook-container {
    display: flex;
    flex-direction: column;
    min-height: 520px;
}

.orderbook-side {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
}

.orderbook-side-label {
    padding: 8px 15px 0 15px;
    font-size: 11px;
    font-weight: 700;
    letter-spacing: 0.06em;
    text-transform: uppercase;
    color: rgba(0, 0, 0, 0.55);
    background: #f8f9fa;
}

.asks {
    border-bottom: 2px solid #e9ecef;
}

.orderbook-header {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    padding: 10px 15px;
    background: #f8f9fa;
    font-weight: 600;
    font-size: 12px;
    color: #666;
    border-bottom: 1px solid #e9ecef;
}

.orderbook-list {
    flex: 1;
    overflow-y: auto;
    max-height: 300px;
}

.orderbook-row {
    display: grid;
    grid-template-columns: 1fr 1fr 1fr;
    padding: 8px 15px;
    font-size: 13px;
    border-bottom: 1px solid #f0f0f0;
    transition: background 0.2s;
    cursor: pointer;
}

.orderbook-row:hover {
    background: #f8f9fa;
}

.asks .orderbook-row {
    color: #f44336;
}

.bids .orderbook-row {
    color: #4caf50;
}

.orderbook-row .price {
    font-weight: 600;
}

.orderbook-row .quantity {
    text-align: center;
}

.orderbook-row .total {
    text-align: right;
    color: #999;
}

/* Spread info */
.orderbook-spread {
    padding: 15px;
    background: #f8f9fa;
    border-top: 2px solid #e9ecef;
    border-bottom: 2px solid #e9ecef;
}

.spread-info {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 15px;
}

.spread-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.spread-item label {
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
}

.spread-item .price,
.spread-item .spread-value {
    font-size: 16px;
    font-weight: 600;
    color: #333;
}

.spread-value {
    color: #667eea;
}

/* Right panels */
.panel-group {
    display: flex;
    flex-direction: column;
    gap: 20px;
}

/* Position */
.position-panel {
    min-height: 300px;
}

.gauge-panel {
    min-height: 220px;
}

.position-content {
    padding: 20px;
    display: flex;
    flex-direction: column;
    gap: 15px;
}

.position-item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 12px;
    background: #f8f9fa;
    border-radius: 6px;
}

.position-item label {
    font-size: 13px;
    color: #666;
    font-weight: 500;
}

.position-value {
    font-size: 16px;
    font-weight: 600;
    color: #333;
}

.position-item.highlight {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
}

.position-item.highlight label {
    color: rgba(255, 255, 255, 0.9);
}

.highlight-value {
    color: #667eea;
    font-size: 18px;
}

.position-item.highlight .highlight-value {
    color: white;
}

.pnl.positive {
    color: #4caf50;
}

.pnl.negative {
    color: #f44336;
}

/* Trades */
.trades-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.orders-panel {
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.matching-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

.analysis-panel {
    flex: 1;
    min-height: 0;
    display: flex;
    flex-direction: column;
}

/* Analysis should scroll as a single pane (avoid multiple nested flex+scroll areas) */
.analysis-panel .trades-list-container {
    flex: 0 0 auto;
    overflow: visible;
}

.analysis-panel .trades-content {
    scrollbar-gutter: stable;
}

.trades-controls {
    display: flex;
    gap: 10px;
}

.time-range-selector {
    padding: 5px 10px;
    border: 1px solid #ddd;
    border-radius: 4px;
    font-size: 12px;
}

.trades-content {
    flex: 1;
    display: flex;
    flex-direction: column;
    padding: 20px;
    /* Allow panel content to scroll within fixed-height windows */
    overflow-y: scroll;
    min-height: 0;
}

.trades-summary {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 15px;
    margin-bottom: 15px;
    padding-bottom: 15px;
    border-bottom: 1px solid #e9ecef;
}

@media (max-width: 1400px) {
    .trades-summary {
        grid-template-columns: repeat(2, 1fr);
    }
}

.summary-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
}

.summary-item label {
    font-size: 11px;
    color: #666;
    text-transform: uppercase;
}

.summary-item span {
    font-size: 16px;
    font-weight: 600;
    color: #333;
}

.summary-item.highlight {
    background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
    color: white;
    padding: 10px;
    border-radius: 6px;
}

.summary-item.highlight label {
    color: rgba(255, 255, 255, 0.9);
}

.summary-item.highlight span {
    color: white;
}

.trades-list-container {
    flex: 1;
    overflow-y: auto;
}

.trades-table {
    width: 100%;
    border-collapse: collapse;
    font-size: 12px;
}

.trades-table thead {
    background: #f8f9fa;
    position: sticky;
    top: 0;
    z-index: 10;
}

.trades-table th {
    padding: 10px;
    text-align: left;
    font-weight: 600;
    color: #666;
    font-size: 11px;
    text-transform: uppercase;
    border-bottom: 2px solid #e9ecef;
}

.trades-table td {
    padding: 10px;
    border-bottom: 1px solid #f0f0f0;
}

.trades-table tbody tr:hover {
    background: #f8f9fa;
}

.trades-table .trade-buy {
    color: #4caf50;
}

.trades-table .trade-sell {
    color: #f44336;
}

.empty-state {
    text-align: center;
    color: #999;
    padding: 40px !important;
}

/* Responsive */
@media (max-width: 1400px) {
    .dashboard-layout {
        height: auto;
    }
}

@media (max-width: 1100px) {
    .dashboard-layout {
        grid-template-rows: auto auto;
        height: auto;
    }

    .dashboard-top {
        grid-template-columns: 1fr;
    }
}

@media (max-width: 768px) {
    .dashboard-header {
        flex-direction: column;
        align-items: flex-start;
    }

    .spread-info {
        grid-template-columns: repeat(2, 1fr);
    }

    .trades-summary {
        grid-template-columns: 1fr;
    }
}

/* Scrollbar */
::-webkit-scrollbar {
    width: 6px;
    height: 6px;
}

::-webkit-scrollbar-track {
    background: #f1f1f1;
}

::-webkit-scrollbar-thumb {
    background: #888;
    border-radius: 3px;
}

::-webkit-scrollbar-thumb:hover {
    background: #555;
}
