/**
 * Grid Layout System for BitScalp Dashboard
 * 반응형 그리드 레이아웃 구현
 */

/* ========== ROOT GRID CONTAINER ========== */
.grid-container {
    display: grid;
    height: 100vh;
    max-width: 1400px;
    margin: 0 auto;
    background-color: #000;

    /* Desktop Grid Layout (1366px+) */
    grid-template-rows: 48px 40px 1fr 24px;
    grid-template-columns: 320px 1fr 320px; /* Equal width panels */
    grid-template-areas:
        "header header header"
        "ticker ticker ticker"
        "sidebar main rightpanel"
        "footer footer footer";
}

/* ========== GRID AREAS ========== */
.grid-header {
    grid-area: header;
    background: rgb(3 7 18);
    border-bottom: 1px solid rgb(31 41 55);
    display: flex;
    align-items: center;
    padding: 0 16px;
}

.grid-ticker {
    grid-area: ticker;
    background: rgb(3 7 18);
    border-bottom: 1px solid rgb(17 24 39);
    display: flex;
    align-items: center;
    padding: 0 16px;
    overflow: hidden;
}

.grid-sidebar {
    grid-area: sidebar;
    background: rgb(17 24 39);
    border-right: 1px solid rgb(31 41 55);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.grid-main {
    grid-area: main;
    background: rgb(11 14 17);
    position: relative;
    min-width: 0; /* Prevent overflow */
    display: flex;
    flex-direction: column;
}

.grid-rightpanel {
    grid-area: rightpanel;
    background: rgb(17 24 39);
    border-left: 1px solid rgb(31 41 55);
    overflow-y: auto;
    display: flex;
    flex-direction: column;
}

.grid-footer {
    grid-area: footer;
    background: rgb(0 0 0);
    border-top: 1px solid rgb(31 41 55);
    display: flex;
    align-items: center;
    padding: 0 16px;
    font-size: 0.75rem;
    color: rgb(107 114 128);
}

/* ========== RESPONSIVE BREAKPOINTS ========== */

/* Tablet-Large & Laptop (1024px - 1365px) - 패널 모두 표시 */
@media (min-width: 1024px) and (max-width: 1365px) {
    .grid-container {
        grid-template-columns: 280px 1fr 280px; /* 양쪽 패널 모두 표시 */
        grid-template-areas:
            "header header header"
            "ticker ticker ticker"
            "sidebar main rightpanel"
            "footer footer footer";
    }

    /* 양쪽 패널 모두 기본 표시 */
    .grid-sidebar,
    .grid-rightpanel {
        display: flex !important;
        position: static !important;
        transform: none !important;
    }

    /* 토글 버튼 숨김 (패널이 항상 보이므로) */
    .panel-toggle-btn {
        display: none !important;
    }
}

/* Tablet (768px - 1023px) - 패널 숨김 + 토글 버튼 */
@media (min-width: 768px) and (max-width: 1023px) {
    .grid-container {
        grid-template-columns: 1fr; /* 메인 콘텐츠만 */
        grid-template-areas:
            "header"
            "ticker"
            "main"
            "footer";
    }

    /* 양쪽 패널 모두 기본 숨김 - ID와 클래스 모두 포함 */
    #left-panel,
    #right-panel,
    .grid-sidebar,
    .grid-rightpanel {
        display: none !important;
    }

    /* 토글 버튼 표시 */
    .panel-toggle-btn {
        display: flex !important;
    }

    /* 왼쪽 패널 토글 시 오버레이 */
    #left-panel.panel-open,
    .grid-sidebar.panel-open {
        display: flex !important;  /* flex로 변경하여 내부 구조 제어 */
        flex-direction: column !important;  /* 세로 방향 배치 */
        position: fixed !important;
        left: 0;
        top: 88px;
        bottom: 24px;
        width: 280px;
        z-index: 100;
        overflow-y: hidden !important;  /* 패널 자체는 스크롤 금지 */
        box-shadow: 2px 0 8px rgba(0,0,0,0.3);
        animation: slideIn 0.3s ease-out;
    }

    /* 오른쪽 패널 토글 시 오버레이 */
    #right-panel.panel-open,
    .grid-rightpanel.panel-open {
        display: flex !important;  /* flex로 변경하여 내부 구조 제어 */
        flex-direction: column !important;  /* 세로 방향 배치 */
        position: fixed !important;
        right: 0;
        top: 88px;
        bottom: 24px;
        width: 280px;
        z-index: 100;
        overflow-y: hidden !important;  /* 패널 자체는 스크롤 금지 */
        box-shadow: -2px 0 8px rgba(0,0,0,0.3);
        animation: slideInRight 0.3s ease-out;
    }

    /* 탭 버튼 영역 - Sticky로 상단 고정 */
    #left-panel.panel-open > .flex.border-b,
    #right-panel.panel-open > .flex.border-b,
    .grid-sidebar.panel-open > .flex.border-b,
    .grid-rightpanel.panel-open > .flex.border-b {
        flex-shrink: 0 !important;  /* 크기 고정 (축소 방지) */
        position: sticky !important;  /* 스크롤 시 상단 고정 */
        top: 0 !important;
        z-index: 10 !important;  /* 콘텐츠 위에 표시 */
        background: rgb(17 24 39) !important;  /* 스크롤 시 콘텐츠 가림 */
    }

    /* 콘텐츠 영역만 스크롤 허용 */
    #left-panel.panel-open .panel-content,
    #right-panel.panel-open .panel-content,
    .grid-sidebar.panel-open .panel-content,
    .grid-rightpanel.panel-open .panel-content {
        flex: 1 !important;  /* 남은 공간 모두 차지 */
        min-height: 0 !important;  /* Flexbox 높이 제약 (스크롤 작동에 필수) */
        overflow-y: auto !important;  /* 이 영역만 스크롤 */
        overflow-x: hidden !important;
    }

    /* 오버레이 배경 */
    .mobile-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 90;
    }

    .mobile-overlay.active {
        display: block;
    }
}

/* Mobile (767px and below) */
@media (max-width: 767px) {
    .grid-container {
        grid-template-columns: 1fr;
        grid-template-rows: 48px 40px 1fr 60px 24px;
        grid-template-areas:
            "header"
            "ticker"
            "main"
            "mobile-nav"
            "footer";
    }

    .grid-sidebar,
    .grid-rightpanel {
        display: none;
    }

    /* Mobile Navigation Bar */
    .grid-mobile-nav {
        grid-area: mobile-nav;
        background: rgb(17 24 39);
        border-top: 1px solid rgb(31 41 55);
        display: flex;
        justify-content: space-around;
        align-items: center;
        padding: 8px 0;
    }

    /* Sliding panels for mobile */
    .grid-sidebar.panel-open,
    .grid-rightpanel.panel-open {
        display: flex !important;  /* flex로 변경하여 내부 구조 제어 */
        flex-direction: column !important;  /* 세로 방향 배치 */
        position: fixed;
        top: 88px; /* header + ticker height */
        bottom: 84px; /* mobile-nav + footer height */
        width: 85%;
        max-width: 320px;
        z-index: 100;
        overflow-y: hidden !important;  /* 패널 자체는 스크롤 금지 */
    }

    .grid-sidebar.panel-open {
        left: 0;
        box-shadow: 2px 0 8px rgba(0,0,0,0.3);
        animation: slideIn 0.3s ease-out;
    }

    .grid-rightpanel.panel-open {
        right: 0;
        box-shadow: -2px 0 8px rgba(0,0,0,0.3);
        animation: slideInRight 0.3s ease-out;
    }

    /* 탭 버튼 영역 - Sticky로 상단 고정 (Mobile) */
    .grid-sidebar.panel-open > .flex.border-b,
    .grid-rightpanel.panel-open > .flex.border-b {
        flex-shrink: 0 !important;  /* 크기 고정 (축소 방지) */
        position: sticky !important;  /* 스크롤 시 상단 고정 */
        top: 0 !important;
        z-index: 10 !important;  /* 콘텐츠 위에 표시 */
        background: rgb(17 24 39) !important;  /* 스크롤 시 콘텐츠 가림 */
    }

    /* 콘텐츠 영역만 스크롤 허용 (Mobile) */
    .grid-sidebar.panel-open .panel-content,
    .grid-rightpanel.panel-open .panel-content {
        flex: 1 !important;  /* 남은 공간 모두 차지 */
        min-height: 0 !important;  /* Flexbox 높이 제약 (스크롤 작동에 필수) */
        overflow-y: auto !important;  /* 이 영역만 스크롤 */
        overflow-x: hidden !important;
    }

    /* Mobile overlay */
    .mobile-overlay {
        display: none;
        position: fixed;
        inset: 0;
        background: rgba(0,0,0,0.5);
        z-index: 90;
    }

    .mobile-overlay.active {
        display: block;
    }
}

/* ========== CHART CONTAINER SPECIFIC ========== */
#main-chart-container {
    flex: 1;
    position: relative;
    min-height: 300px;
    width: 100%;
}

/* Ensure chart fills container properly */
.grid-main .chart-wrapper {
    flex: 1;
    position: relative;
    padding: 8px;
}

/* ========== PANEL TRANSITIONS ========== */
.panel-transition {
    transition: transform 0.3s ease, opacity 0.3s ease;
}

/* Slide animations */
@keyframes slideIn {
    from {
        transform: translateX(-100%);
    }
    to {
        transform: translateX(0);
    }
}

@keyframes slideInRight {
    from {
        transform: translateX(100%);
    }
    to {
        transform: translateX(0);
    }
}

/* ========== SCROLLBAR STYLING ========== */
.grid-sidebar::-webkit-scrollbar,
.grid-rightpanel::-webkit-scrollbar {
    width: 6px;
}

.grid-sidebar::-webkit-scrollbar-track,
.grid-rightpanel::-webkit-scrollbar-track {
    background: rgb(31 41 55);
}

.grid-sidebar::-webkit-scrollbar-thumb,
.grid-rightpanel::-webkit-scrollbar-thumb {
    background: rgb(75 85 99);
    border-radius: 3px;
}

.grid-sidebar::-webkit-scrollbar-thumb:hover,
.grid-rightpanel::-webkit-scrollbar-thumb:hover {
    background: rgb(107 114 128);
}

/* ========== UTILITY CLASSES ========== */
.hide-on-mobile {
    @media (max-width: 767px) {
        display: none !important;
    }
}

.hide-on-tablet {
    @media (max-width: 1023px) {
        display: none !important;
    }
}

.show-on-mobile {
    display: none !important;
    @media (max-width: 767px) {
        display: block !important;
    }
}

/* ========== PANEL TOGGLE BUTTONS ========== */
.panel-toggle-btn {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    width: 24px;
    height: 48px;
    background: rgb(31 41 55);
    border: 1px solid rgb(75 85 99);
    color: #fff; /* 흰색 화살표 */
    display: flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    z-index: 80;
    transition: background 0.2s;
}

.panel-toggle-btn:hover {
    background: rgb(55 65 81);
}

.panel-toggle-btn.left {
    left: 0;
    border-radius: 0 4px 4px 0;
}

.panel-toggle-btn.right {
    right: 0;
    border-radius: 4px 0 0 4px;
}

/* Desktop (1366px+) - 토글 버튼 숨김 */
@media (min-width: 1366px) {
    .panel-toggle-btn {
        display: none !important;
    }
}

/* 중복 제거됨 - 1024-1365px 설정은 위 84행에 이미 정의됨 */

/* ========== RESPONSIVE FONT SIZES ========== */
@media (max-width: 767px) {
    .grid-header {
        font-size: 0.875rem;
    }

    .grid-ticker {
        font-size: 0.75rem;
    }

    .grid-footer {
        font-size: 0.625rem;
    }
}

/* ========== GRID GAP MANAGEMENT ========== */
@media (min-width: 1366px) {
    .grid-container {
        gap: 0;
    }
}

@media (min-width: 768px) and (max-width: 1365px) {
    .grid-container {
        gap: 0;
    }
}

/* ========== PREVENT LAYOUT SHIFT ========== */
.grid-container > * {
    min-width: 0;
    min-height: 0;
}

/* ========== LOADING STATE ========== */
.grid-loading {
    display: flex;
    align-items: center;
    justify-content: center;
    height: 100%;
    color: rgb(107 114 128);
}

.grid-loading::after {
    content: "Loading...";
    animation: pulse 2s infinite;
}

@keyframes pulse {
    0%, 100% {
        opacity: 1;
    }
    50% {
        opacity: 0.5;
    }
}