/* ════════════════════════════════════════════════════════════════
   GlobalCRM — Mobile & Tablet Optimization
   대상: 안드로이드폰/패드, 아이폰/아이패드
   ════════════════════════════════════════════════════════════════ */

/* ── iOS Safari 안전 영역 ──────────────────────────────────────── */
:root {
    --safe-top:    env(safe-area-inset-top,    0px);
    --safe-bottom: env(safe-area-inset-bottom, 0px);
    --safe-left:   env(safe-area-inset-left,   0px);
    --safe-right:  env(safe-area-inset-right,  0px);
    --bottom-nav-h: 60px;
}

/* ── 전체 body 터치 최적화 ────────────────────────────────────── */
body {
    -webkit-text-size-adjust: 100%;
    -webkit-tap-highlight-color: transparent;
    touch-action: manipulation;
}

/* ── 하단 네비게이션 바 (모바일 전용) ────────────────────────── */
#mobileBottomNav {
    display: none;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: calc(var(--bottom-nav-h) + var(--safe-bottom));
    background: #1e293b;
    border-top: 1px solid rgba(255,255,255,.08);
    z-index: 1050;
    padding-bottom: var(--safe-bottom);
}

.bottom-nav-items {
    display: flex;
    height: var(--bottom-nav-h);
}

.bottom-nav-item {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 3px;
    color: #64748b;
    text-decoration: none;
    font-size: .62rem;
    font-weight: 500;
    transition: color .15s;
    -webkit-tap-highlight-color: transparent;
    min-height: 44px;
    cursor: pointer;
    border: none;
    background: none;
    padding: 0;
}
.bottom-nav-item i { font-size: 1.15rem; line-height: 1; }
.bottom-nav-item.active { color: #818cf8; }
.bottom-nav-item:active { background: rgba(255,255,255,.05); }

/* 더보기 드로어 */
#mobileMoreDrawer {
    display: none;
    position: fixed;
    bottom: calc(var(--bottom-nav-h) + var(--safe-bottom));
    left: 0; right: 0;
    background: #1e293b;
    border-top: 1px solid rgba(255,255,255,.12);
    border-radius: 16px 16px 0 0;
    z-index: 1049;
    padding: 1rem;
    max-height: 70vh;
    overflow-y: auto;
    transform: translateY(100%);
    transition: transform .25s ease;
}
#mobileMoreDrawer.show {
    display: block;
    transform: translateY(0);
}
.drawer-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    padding-bottom: .5rem;
}
.drawer-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    gap: 5px;
    padding: 12px 4px;
    border-radius: 10px;
    color: #94a3b8;
    text-decoration: none;
    font-size: .68rem;
    text-align: center;
    background: rgba(255,255,255,.04);
    -webkit-tap-highlight-color: transparent;
}
.drawer-item i { font-size: 1.3rem; }
.drawer-item:active { background: rgba(99,102,241,.2); color: #a5b4fc; }
.drawer-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.5);
    z-index: 1048;
}
.drawer-overlay.show { display: block; }

@media (max-width: 991px) {
    #mobileBottomNav { display: block; }

    /* 하단 네비 높이만큼 본문 패딩 추가 */
    .page-content {
        padding-bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 1rem) !important;
    }

    /* 사이드바 숨기기 (하단 네비로 대체) */
    .sidebar { display: none !important; }
    .main-content { margin-left: 0 !important; }

    /* 상단바 iOS notch 대응 */
    .topbar {
        padding-top: var(--safe-top);
        height: calc(60px + var(--safe-top));
    }

    /* 좌우 여백 최소화 */
    .page-content { padding: .75rem !important; }

    /* 카드 여백 */
    .card { border-radius: 10px; }
    .card-body { padding: .875rem; }
    .card-header { padding: .75rem 1rem; font-size: .85rem; }
}

/* ── 폰 최적화 (576px 이하) ────────────────────────────────────── */
@media (max-width: 575px) {

    /* 터치 타겟 최소 44px */
    .btn, a.btn, button.btn {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }
    .btn-sm { min-height: 36px; font-size: .82rem; }
    .btn-xs { min-height: 32px; }

    /* 폼 입력 */
    .form-control, .form-select {
        min-height: 44px;
        font-size: 16px !important; /* iOS 줌 방지 */
        border-radius: 8px;
    }
    .form-control-sm, .form-select-sm {
        min-height: 38px;
        font-size: 15px !important;
    }

    /* 모달 전체화면 */
    .modal-dialog {
        margin: 0 !important;
        max-width: 100% !important;
    }
    .modal-content {
        border-radius: 16px 16px 0 0 !important;
        min-height: 40vh;
    }
    .modal-dialog-centered {
        align-items: flex-end !important;
        min-height: 100%;
    }

    /* 테이블 → 카드 변환 */
    .table-mobile-card thead { display: none; }
    .table-mobile-card tbody tr {
        display: block;
        background: #fff;
        border-radius: 10px;
        margin-bottom: .75rem;
        padding: .875rem;
        box-shadow: 0 1px 4px rgba(0,0,0,.08);
        border: 1px solid #e2e8f0;
    }
    .table-mobile-card tbody td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: .3rem 0;
        border: none !important;
        font-size: .85rem;
    }
    .table-mobile-card tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #64748b;
        font-size: .75rem;
        flex-shrink: 0;
        margin-right: .5rem;
    }
    .table-mobile-card tbody td:first-child {
        font-size: .92rem;
        font-weight: 600;
        padding-bottom: .5rem;
        border-bottom: 1px solid #f1f5f9 !important;
        margin-bottom: .3rem;
    }

    /* 페이지 헤더 */
    .page-header { padding-bottom: .75rem; }
    .page-header h4, .page-header h5 { font-size: 1rem; }

    /* 통계 카드 */
    .db-stat { padding: .875rem !important; border-radius: 10px !important; }
    .db-stat-value { font-size: 1.5rem !important; }
    .db-stat-label { font-size: .7rem !important; }

    /* 뱃지 */
    .badge { font-size: .68rem !important; }

    /* 검색 바 */
    .input-group .form-control { font-size: 16px !important; }

    /* 플로팅 액션 버튼 */
    .fab {
        position: fixed;
        bottom: calc(var(--bottom-nav-h) + var(--safe-bottom) + 16px);
        right: 16px;
        width: 52px; height: 52px;
        border-radius: 50%;
        z-index: 1040;
        box-shadow: 0 4px 16px rgba(0,0,0,.3);
        display: flex;
        align-items: center;
        justify-content: center;
        font-size: 1.3rem;
    }

    /* 고객 목록 카드형 */
    .client-row-mobile {
        display: block;
        padding: .875rem;
        background: #fff;
        border-radius: 10px;
        margin-bottom: .6rem;
        border: 1px solid #e2e8f0;
        text-decoration: none;
        color: inherit;
    }
}

/* ── 태블릿 최적화 (iPad, 768-1199px) ─────────────────────────── */
@media (min-width: 768px) and (max-width: 1199px) {

    :root { --sidebar-width: 200px; }

    /* 사이드바 좁히기 */
    .sidebar { width: 200px; }
    .main-content { margin-left: 200px; }
    .menu-link { font-size: .82rem; padding: .55rem 1rem; }
    .sidebar-brand { padding: .875rem 1rem; }
    .brand-text { font-size: 1rem; }

    /* 콘텐츠 패딩 */
    .page-content { padding: 1.25rem; }

    /* 2컬럼 그리드 */
    .row-tablet-2 > [class*="col-"] { flex: 0 0 50%; max-width: 50%; }

    /* 폼 */
    .form-control, .form-select { font-size: 15px; }
}

/* ── iOS 전용 ──────────────────────────────────────────────────── */
@supports (-webkit-touch-callout: none) {
    /* iOS 고무밴드 스크롤 방지 */
    .modal-open { overflow: hidden; position: fixed; width: 100%; }

    /* iOS input zoom 방지 */
    input[type="text"],
    input[type="email"],
    input[type="tel"],
    input[type="number"],
    input[type="search"],
    textarea,
    select { font-size: 16px !important; }

    /* iOS 하단 홈 바 */
    #mobileBottomNav {
        padding-bottom: max(var(--safe-bottom), 8px);
    }
}

/* ── 다크 모드 지원 ────────────────────────────────────────────── */
@media (prefers-color-scheme: dark) {
    /* 이미 다크 테마이므로 추가 조정 불필요 */
}

/* ── 가로 모드 폰 ──────────────────────────────────────────────── */
@media (max-width: 767px) and (orientation: landscape) {
    #mobileBottomNav { height: calc(50px + var(--safe-bottom)); }
    .bottom-nav-item { font-size: .58rem; }
    .bottom-nav-item i { font-size: 1rem; }
    .page-content {
        padding-bottom: calc(50px + var(--safe-bottom) + .75rem) !important;
    }
}

/* ── 스크롤바 숨기기 (모바일) ─────────────────────────────────── */
@media (max-width: 991px) {
    ::-webkit-scrollbar { width: 0; height: 0; }
    .table-responsive { -webkit-overflow-scrolling: touch; }
}

/* ── 애니메이션 줄이기 (접근성) ──────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
    * { transition: none !important; animation: none !important; }
}
