* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Segoe UI', -apple-system, sans-serif; display: flex; min-height: 100vh; background: #f8f9fa; color: #333; }

/* Sidebar */
#sidebar { width: 200px; background: #1a1a2e; color: #eee; flex-shrink: 0; position: fixed; height: 100vh; overflow-y: auto; }
.sidebar-header { padding: 16px; font-size: 15px; font-weight: 700; border-bottom: 1px solid #333; }
#sidebar ul { list-style: none; padding: 8px 0; }
#sidebar li { margin: 0; }
.nav-link { display: block; padding: 8px 16px; color: #ccc; text-decoration: none; font-size: 13px; transition: background 0.15s; }
.nav-link:hover { background: #16213e; color: #fff; }
.nav-link.active { background: #0f3460; color: #fff; font-weight: 600; }
.nav-group { padding: 12px 16px 4px; font-size: 11px; color: #888; text-transform: uppercase; letter-spacing: 1px; }
.nav-group-spacer { height: 8px; }

/* Main */
main { margin-left: 200px; flex: 1; padding: 20px; min-height: 100vh; }

/* Metrics */
.metrics { display: flex; gap: 12px; margin-bottom: 16px; flex-wrap: wrap; }
.metric { background: #fff; border-radius: 8px; padding: 12px 16px; flex: 1; min-width: 120px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
.metric-label { font-size: 12px; color: #888; }
.metric-value { font-size: 22px; font-weight: 700; color: #1a1a2e; }

/* Tables */
.table-wrap { overflow-x: auto; background: #fff; border-radius: 8px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th { background: #f1f3f5; padding: 8px 10px; text-align: left; font-weight: 600; border-bottom: 2px solid #dee2e6; position: sticky; top: 0; }
td { padding: 7px 10px; border-bottom: 1px solid #eee; }
tr:hover { background: #f8f9ff; }
tr.selected { background: #bbdefb !important; font-weight: 600; box-shadow: inset 3px 0 0 #1976d2; }
tr { cursor: pointer; }

/* Forms */
.search-bar { display: flex; gap: 8px; margin-bottom: 16px; align-items: center; }
.search-bar input, .search-bar select { padding: 8px 12px; border: 1px solid #ddd; border-radius: 6px; font-size: 13px; }
.search-bar input { flex: 1; }
.btn { padding: 8px 16px; border: none; border-radius: 6px; cursor: pointer; font-size: 13px; font-weight: 600; transition: background 0.15s; }
.btn-primary { background: #0f3460; color: #fff; }
.btn-primary:hover { background: #1a4a8a; }
.btn-danger { background: #e74c3c; color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 12px; }

/* Layout */
.row { display: flex; gap: 16px; margin-bottom: 16px; }
.col { flex: 1; min-width: 0; }
.col-4 { flex: 0 0 40%; }
.col-6 { flex: 0 0 50%; }
.col-8 { flex: 0 0 60%; }

/* Cards */
.card { background: #fff; border-radius: 8px; padding: 16px; box-shadow: 0 1px 3px rgba(0,0,0,0.08); margin-bottom: 16px; }
.card h3 { font-size: 15px; margin-bottom: 12px; }

/* Chart container */
.chart { width: 100%; }

/* Alerts */
.alert { padding: 10px 14px; border-radius: 6px; font-size: 13px; margin-bottom: 12px; }
.alert-info { background: #e8f4fd; color: #0c5460; }
.alert-success { background: #d4edda; color: #155724; }
.alert-warning { background: #fff3cd; color: #856404; }
.alert-error { background: #f8d7da; color: #721c24; }

/* Section */
h2 { font-size: 20px; margin-bottom: 16px; }
h3 { font-size: 16px; margin-bottom: 10px; }
.subtitle { color: #666; font-size: 13px; margin-bottom: 12px; }
.divider { border: none; border-top: 1px solid #eee; margin: 16px 0; }

/* Scrollable table */
.table-scroll { max-height: 400px; overflow-y: auto; }

/* Tabs */
.tabs { display: flex; gap: 0; margin-bottom: 16px; border-bottom: 2px solid #eee; }
.tab { padding: 8px 16px; cursor: pointer; font-size: 13px; font-weight: 600; color: #888; border-bottom: 2px solid transparent; margin-bottom: -2px; }
.tab:hover { color: #333; }
.tab.active { color: #0f3460; border-bottom-color: #0f3460; }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ================= 상단 페이지 탭바 (Keep-alive MDI) ================= */
/* 우측 margin은 우상단 고정 user-bar와의 겹침 방지 */
#page-tabs {
    display: flex; align-items: flex-end; gap: 4px;
    margin: 0 170px 14px 0;
    border-bottom: 2px solid #dee2e6;
    overflow-x: auto; overflow-y: hidden;
    scrollbar-width: thin;
}
#page-tabs:empty { display: none; }
.ptab {
    display: inline-flex; align-items: center; gap: 4px;
    padding: 7px 8px 7px 12px;
    background: #e9ecef; color: #666;
    border: 1px solid #dee2e6; border-bottom: none;
    border-radius: 6px 6px 0 0;
    font-size: 12.5px; white-space: nowrap; flex-shrink: 0;
    cursor: pointer; user-select: none;
    transition: background 0.15s;
}
.ptab:hover { background: #f1f3f5; }
.ptab.active { background: #fff; color: #0f3460; font-weight: 600; }
.ptab-refresh, .ptab-close {
    border: none; background: none; cursor: pointer;
    font-size: 14px; line-height: 1; color: #adb5bd;
    padding: 1px 4px; border-radius: 4px;
}
.ptab-refresh:hover { color: #0f3460; background: #e7f5ff; }
.ptab-close:hover { color: #fff; background: #ff6b6b; }
.ptab:not(.active) .ptab-refresh { display: none; }

/* Spinner */
.spinner { display: inline-block; width: 20px; height: 20px; border: 3px solid #ddd; border-top-color: #0f3460; border-radius: 50%; animation: spin 0.6s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.loading { text-align: center; padding: 40px; color: #888; }

/* Expander */
.expander { border: 1px solid #eee; border-radius: 6px; margin-bottom: 8px; }
.expander-header { padding: 8px 12px; cursor: pointer; font-size: 13px; font-weight: 600; background: #fafafa; }
.expander-header:hover { background: #f0f0f0; }
.expander-body { padding: 10px 12px; display: none; border-top: 1px solid #eee; }
.expander.open .expander-body { display: block; }

/* ================= 햄버거 버튼 (기본 숨김) ================= */
#mobile-menu-btn {
    display: none;
    position: fixed; top: 10px; left: 10px; z-index: 1001;
    width: 40px; height: 40px; border: none; border-radius: 6px;
    background: #1a1a2e; color: #fff; font-size: 20px; cursor: pointer;
    box-shadow: 0 2px 6px rgba(0,0,0,0.2);
}
#sidebar-backdrop {
    display: none;
    position: fixed; inset: 0; background: rgba(0,0,0,0.4); z-index: 999;
}

/* ================= Level 1 반응형 (768px 이하) ================= */
@media (max-width: 768px) {
    body { display: block; }

    /* 사이드바: 기본 숨김, 햄버거로 토글 */
    #sidebar {
        width: 220px;
        transform: translateX(-100%);
        transition: transform 0.25s ease;
        z-index: 1000;
    }
    #sidebar.open { transform: translateX(0); }
    #sidebar-backdrop.open { display: block; }

    #mobile-menu-btn { display: block; }

    /* 본문: 사이드바 margin 제거 + 상단 여백(버튼 공간) */
    main { margin-left: 0; padding: 58px 12px 16px; }

    /* 검색바: 세로 스택 + 입력창 풀폭 */
    .search-bar { flex-direction: column; align-items: stretch; }
    .search-bar input, .search-bar select, .search-bar button { width: 100%; max-width: none; }

    /* 행/컬럼 세로 스택 */
    .row { flex-direction: column; }
    .col, .col-4, .col-6, .col-8 { flex: 1 1 100%; }

    /* 메트릭 카드 */
    .metrics { gap: 8px; }
    .metric { flex: 1 1 45%; min-width: 0; }
    .metric-value { font-size: 18px; }

    /* 테이블: 가로 스크롤 유지 + 힌트 */
    .table-wrap { -webkit-overflow-scrolling: touch; }
    table { font-size: 12px; }
    th, td { padding: 6px 8px; }

    /* 버튼 터치 크기 */
    .btn { padding: 10px 14px; font-size: 14px; min-height: 40px; }
    .btn-sm { padding: 6px 10px; font-size: 12px; min-height: 32px; }

    /* 제목 축소 */
    h2 { font-size: 17px; }
    h3 { font-size: 15px; }

    /* 탭 가로 스크롤 */
    .tabs { overflow-x: auto; white-space: nowrap; }
    .tab { flex-shrink: 0; }

    /* 테이블 행 터치 여유 */
    tr { cursor: default; }

    /* 페이지 탭바: 전폭 + 가로 스크롤 (user-bar 여백 해제) */
    #page-tabs { margin-right: 0; -webkit-overflow-scrolling: touch; }
}

/* ================= 초소형(480px 이하) 추가 보정 ================= */
@media (max-width: 480px) {
    .metric { flex: 1 1 100%; }
    main { padding: 54px 8px 12px; }
    table { font-size: 11px; }
}

/* ================= 모바일 공통 보정 (2026-10-15) ================= */
@media (max-width: 768px) {
    /* 우상단 사용자 표시줄: 한 줄 고정·축소, 햄버거(좌)와 같은 높이로 정렬 → 본문 가림 방지 */
    #user-bar {
        top: 10px !important; right: 10px !important;
        height: 40px; padding: 0 10px !important;
        font-size: 12px !important; gap: 6px !important;
        white-space: nowrap; max-width: calc(100vw - 64px); overflow: hidden;
    }
    #user-bar #pwchange-link, #user-bar .ub-sep { display: none; }

    /* 상단 페이지 탭: 닫기·새로고침 버튼 터치 크기 확보 */
    .ptab { padding: 9px 6px 9px 12px; font-size: 13px; }
    .ptab-refresh, .ptab-close { min-width: 32px; min-height: 32px; font-size: 16px; }

    /* 사이드바 메뉴 링크 터치 크기 */
    .nav-link { padding: 12px 16px; font-size: 15px; }

    /* iOS Safari 포커스 자동확대 방지: 입력요소 16px 이상 */
    input, select, textarea { font-size: 16px !important; min-height: 44px; }
    .search-bar input, .search-bar select { padding: 10px 12px; }
    input[type="checkbox"], input[type="radio"] { min-height: 0; width: 20px; height: 20px; }

    /* 표: 폭 압축 대신 진짜 가로 스크롤 (셀이 27px로 쪼개지던 문제) */
    .table-wrap table { width: auto; min-width: 100%; }
    .table-wrap th, .table-wrap td { white-space: nowrap; max-width: 260px; overflow: hidden; text-overflow: ellipsis; }
    .table-wrap { overflow-x: auto; }
    .table-wrap::after { content: '← 좌우로 밀어서 보세요 →'; display: block; text-align: center; font-size: 11px; color: #adb5bd; padding: 4px 0; }

    /* 2단 고정 그리드(P20 등) 세로 스택 */
    .ci-root .two, .two-col, [style*="grid-template-columns: 1fr 1fr"] { grid-template-columns: 1fr !important; }

    /* 모달 폭 */
    #pwchange-overlay > div { width: 92vw; }
}

/* PC↔모바일 전환 링크 (사이드바 하단) */
.nav-ext { display: block; padding: 10px 16px; color: #8fb3ff; font-size: 12px; text-decoration: none; }
.nav-ext:hover { background: #16213e; color: #fff; }

/* 모바일 화면 전환 링크 — 데스크톱은 사이드바 맨 아래, 폰은 맨 위(스크롤 없이 보이도록) + 로그인 화면 */
.nav-ext-top, .login-ext { display: none; }
@media (max-width: 768px) {
    #sidebar { height: 100dvh; }
    .nav-ext-bottom { display: none; }
    .nav-ext-top { display: block; padding: 13px 16px; font-size: 14px; font-weight: 600; color: #fff; background: #0f3460; border-bottom: 1px solid #333; }
    .login-ext { display: block; text-align: center; margin-top: 14px; padding: 12px; font-size: 14px; color: #0f3460; text-decoration: none; border: 1px solid #d0d5db; border-radius: 8px; }
}
