/* ============================================
   MOBILE OPTIMIZATIONS - Small Screen (<= 480px)
   ============================================ */

/* 1. 소형 모바일 대응 - 폰트 크기 축소 */
@media (max-width: 480px) {
    :root {
        --font-size-xs: 0.65rem;
        --font-size-sm: 0.75rem;
        --font-size-base: 0.875rem;
        --font-size-lg: 1rem;
        --font-size-xl: 1.125rem;
        --font-size-2xl: 1.375rem;
        --font-size-3xl: 1.625rem;
    }

    /* 본문 텍스트 크기 조정 */
    body {
        font-size: var(--font-size-sm);
    }

    /* 제목 크기 축소 */
    h1 {
        font-size: var(--font-size-2xl);
        line-height: 1.3;
    }

    h2 {
        font-size: var(--font-size-xl);
        line-height: 1.4;
    }

    h3 {
        font-size: var(--font-size-lg);
        line-height: 1.4;
    }

    h4, h5, h6 {
        font-size: var(--font-size-base);
        line-height: 1.5;
    }

    /* 네비게이션 바 - 모바일에서 숨김 (하단 네비로 대체) */
    .navbar {
        padding: var(--space-sm) 0;
    }

    .nav-container {
        padding: 0 var(--space-sm);
    }

    .nav-brand h1 {
        font-size: var(--font-size-lg);
    }

    .nav-links {
        display: none; /* 하단 네비게이션으로 대체 */
    }

    /* 컨테이너 패딩 축소 */
    .container {
        padding: var(--space-sm);
        margin-bottom: 80px; /* 하단 네비 공간 확보 */
    }

    /* 카드 패딩 축소 */
    .card,
    .news-card,
    .project-card,
    .agent-card,
    .backup-card,
    .log-card,
    .panel-card,
    .ops-card {
        padding: var(--space-sm);
        margin-bottom: var(--space-sm);
    }

    /* 그리드 간격 축소 */
    .grid,
    .news-grid,
    .project-grid,
    .agent-grid,
    .ops-card-grid {
        gap: var(--space-sm);
        grid-template-columns: 1fr; /* 단일 컬럼 */
    }

    /* Hero 섹션 */
    .hero {
        padding: var(--space-md) 0;
        text-align: center;
    }

    .hero h2 {
        font-size: var(--font-size-xl);
    }

    .hero .subtitle {
        font-size: var(--font-size-sm);
    }

    /* 테이블 → 카드형 변환 */
    .table-container {
        overflow-x: visible; /* 스크롤 제거 */
    }

    table {
        display: block;
    }

    thead {
        display: none; /* 헤더 숨김 */
    }

    tbody {
        display: block;
    }

    tr {
        display: block;
        margin-bottom: var(--space-md);
        padding: var(--space-sm);
        background: var(--bg-secondary);
        border-radius: var(--radius-md);
        box-shadow: var(--shadow-sm);
    }

    td {
        display: block;
        text-align: left;
        padding: var(--space-xs) 0;
        border: none;
    }

    td:before {
        content: attr(data-label);
        font-weight: 600;
        color: var(--text-secondary);
        display: inline-block;
        min-width: 80px;
        margin-right: var(--space-sm);
    }

    /* 긴 텍스트 말줄임 */
    .text-truncate,
    .news-card h3,
    .project-card h3,
    .agent-card h3 {
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }

    .text-truncate-multi {
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
        text-overflow: ellipsis;
    }

    /* 뉴스 카드 - 이미지 숨기기 */
    .news-card img,
    .news-card .news-thumbnail {
        display: none;
    }

    .news-card {
        display: flex;
        flex-direction: column;
    }

    .news-card-content {
        width: 100%;
    }

    /* 에이전트 카드 - 모델 스택 아코디언 */
    .agent-models {
        max-height: 0;
        overflow: hidden;
        transition: max-height 0.3s ease-out;
    }

    .agent-card.expanded .agent-models {
        max-height: 500px;
    }

    .agent-expand-btn {
        display: block;
        width: 100%;
        margin-top: var(--space-sm);
        padding: var(--space-xs);
        background: var(--bg-tertiary);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-sm);
        cursor: pointer;
        font-size: var(--font-size-sm);
        color: var(--text-secondary);
        transition: transform var(--transition-base), opacity var(--transition-base), background-color var(--transition-base);
    }

    .agent-expand-btn:hover {
        background: var(--bg-secondary);
        color: var(--text-primary);
    }

    /* 설정 페이지 - 세로 스택 */
    .settings-grid,
    .settings-section {
        display: flex;
        flex-direction: column;
        gap: var(--space-sm);
    }

    .settings-card {
        width: 100%;
    }

    /* 로그/백업 페이지 - 카드 리스트 */
    .log-list,
    .backup-list {
        display: flex;
        flex-direction: column;
        gap: var(--space-sm);
    }

    .log-item,
    .backup-item {
        display: block;
        padding: var(--space-sm);
        background: var(--bg-secondary);
        border-radius: var(--radius-md);
        border-left: 3px solid var(--accent-color);
    }

    /* 홈 레이아웃 - 단일 컬럼 */
    .home-ultra-layout {
        display: flex;
        flex-direction: column;
        gap: var(--space-sm);
    }

    .home-left-panel,
    .home-center-panel,
    .home-right-panel {
        width: 100%;
    }

    /* 성능 최적화 - backdrop-filter 제거/축소 */
    .card,
    .navbar,
    .panel-card,
    .glass-effect {
        backdrop-filter: none;
        -webkit-backdrop-filter: none;
        background: var(--bg-secondary);
    }

    /* 무거운 애니메이션 제거 */
    * {
        animation-duration: 0.2s !important;
        transition-duration: 0.2s !important;
    }

    .fade-in,
    .slide-up,
    .pulse {
        animation: none;
    }

    /* 터치 타겟 최소 44x44px */
    button,
    .btn,
    a.card,
    .clickable {
        min-height: 44px;
        min-width: 44px;
        padding: var(--space-sm) var(--space-md);
    }

    /* 작은 버튼도 터치 가능하게 */
    .btn-sm,
    .icon-btn {
        min-height: 44px;
        min-width: 44px;
        padding: var(--space-sm);
    }

    /* 프로젝트 카드 */
    .project-card {
        min-height: auto;
    }

    .project-card-header {
        flex-direction: column;
        align-items: flex-start;
        gap: var(--space-xs);
    }

    /* Ops 레이아웃 */
    .ops-ultra-layout {
        display: flex;
        flex-direction: column;
    }

    .ops-left-panel,
    .ops-right-panel {
        display: none; /* 모바일에서는 중앙 패널만 표시 */
    }

    /* 필터 그리드 */
    .ops-filter-grid,
    .filter-grid {
        grid-template-columns: 1fr;
    }

    .filter-search-wide {
        grid-column: auto;
    }

    /* 사이드바 오버레이 */
    .sidebar-overlay {
        backdrop-filter: blur(4px); /* 약한 블러 */
    }
}

/* 2. 터치 친화적 UX - 호버 효과 분리 */
@media (hover: hover) and (pointer: fine) {
    /* 데스크톱에서만 호버 효과 활성화 */
    .card:hover,
    .news-card:hover,
    .project-card:hover,
    .agent-card:hover {
        transform: translateY(-4px);
        box-shadow: var(--shadow-lg);
    }

    .btn:hover {
        transform: translateY(-2px);
        box-shadow: var(--shadow-md);
    }
}

/* 터치 디바이스용 - 탭 효과 */
@media (hover: none) and (pointer: coarse) {
    .card:active,
    .news-card:active,
    .project-card:active,
    .agent-card:active {
        transform: scale(0.98);
        opacity: 0.9;
    }

    .btn:active {
        transform: scale(0.95);
    }

    /* 호버 효과 제거 */
    *:hover {
        transform: none;
    }
}

/* 3. 하단 네비게이션 바 */
.mobile-bottom-nav {
    display: none;
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 60px;
    background: var(--bg-primary);
    border-top: 1px solid var(--border-color);
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    z-index: 1000;
    padding-bottom: env(safe-area-inset-bottom, 0);
}

@media (max-width: 768px) {
    .mobile-bottom-nav {
        display: flex;
        justify-content: space-around;
        align-items: center;
    }
}

.mobile-nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    flex: 1;
    height: 100%;
    text-decoration: none;
    color: var(--text-secondary);
    font-size: var(--font-size-xs);
    transition: transform var(--transition-base), opacity var(--transition-base), background-color var(--transition-base);
    border-radius: var(--radius-sm);
    margin: 0 2px;
}

.mobile-nav-item .nav-icon {
    font-size: 1.5rem;
    margin-bottom: 2px;
}

.mobile-nav-item .nav-label {
    font-size: 0.65rem;
    font-weight: 500;
}

.mobile-nav-item.active {
    color: var(--accent-color);
    background: var(--bg-secondary);
}

.mobile-nav-item:active {
    transform: scale(0.9);
    background: var(--bg-tertiary);
}

/* Safe Area 대응 */
@supports (padding: env(safe-area-inset-bottom)) {
    .mobile-bottom-nav {
        padding-bottom: calc(env(safe-area-inset-bottom) + 4px);
        height: calc(60px + env(safe-area-inset-bottom));
    }

    .container {
        margin-bottom: calc(80px + env(safe-area-inset-bottom));
    }

    body {
        padding-bottom: env(safe-area-inset-bottom);
    }
}

/* 4. Pull-to-refresh 시각 피드백 */
.pull-to-refresh {
    position: fixed;
    top: 0;
    left: 50%;
    transform: translateX(-50%) translateY(-60px);
    transition: transform 0.3s ease-out;
    padding: var(--space-sm);
    background: var(--bg-secondary);
    border-radius: 0 0 var(--radius-md) var(--radius-md);
    box-shadow: var(--shadow-md);
    z-index: 999;
    font-size: var(--font-size-sm);
    color: var(--text-secondary);
}

.pull-to-refresh.active {
    transform: translateX(-50%) translateY(0);
}

.pull-to-refresh .refresh-icon {
    display: inline-block;
    animation: spin 1s linear infinite;
    margin-right: var(--space-xs);
}

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

/* 5. 모바일 전용 유틸리티 */
.mobile-only {
    display: none;
}

.desktop-only {
    display: block;
}

@media (max-width: 768px) {
    .mobile-only {
        display: block;
    }

    .desktop-only {
        display: none;
    }
}

/* 스와이프 가능한 카드 (선택 사항) */
.swipeable {
    touch-action: pan-y;
    user-select: none;
}

.swipeable.swiping {
    transition: none;
}

/* ============================================
   MOBILE RESCUE OVERRIDES - keep layout stable
   ============================================ */

@media (max-width: 768px) {
    .container,
    .detail-container,
    .app-main > .container,
    .app-main > .detail-container {
        width: 100%;
        max-width: 100%;
        padding-left: var(--space-md);
        padding-right: var(--space-md);
    }

    .dashboard-grid,
    .news-grid-cards,
    .top-news-grid,
    .stats-news-grid,
    .stats-history-grid,
    .ops-card-grid,
    .ops-filter-grid,
    .settings-grid,
    .password-grid,
    .project-grid-view,
    .projects-center-panel .dashboard-grid,
    .home-right-panel,
    .agent-cards,
    .subteam-members {
        display: grid;
        grid-template-columns: 1fr !important;
        gap: var(--space-md);
    }

    .home-ultra-layout,
    .agents-ultra-layout,
    .projects-ultra-layout,
    .outputs-ultra-layout,
    .ops-ultra-layout {
        display: grid;
        grid-template-columns: 1fr;
        gap: var(--space-md);
    }

    .ops-left-panel,
    .ops-right-panel,
    .agents-left-panel,
    .agents-right-panel,
    .projects-left-panel,
    .projects-right-panel,
    .outputs-left-panel,
    .outputs-right-panel,
    .home-left-panel,
    .home-right-panel {
        display: grid;
        gap: var(--space-md);
    }

    .ops-table-wrap,
    .table-container {
        overflow-x: auto;
        overflow-y: visible;
    }

    .ops-table,
    .logs-table,
    .backups-table,
    .table {
        display: table;
        min-width: 560px;
    }

    thead {
        display: table-header-group;
    }

    tbody {
        display: table-row-group;
    }

    tr {
        display: table-row;
        margin-bottom: 0;
        padding: 0;
        background: transparent;
        border-radius: 0;
        box-shadow: none;
    }

    td,
    th {
        display: table-cell;
        padding: var(--space-sm);
        border: 0;
    }

    td:before {
        content: none;
        display: none;
    }

    .news-title,
    .project-card h3,
    .agent-card-header h3,
    .top-news-card h4 {
        white-space: normal;
        overflow: hidden;
        text-overflow: ellipsis;
        display: -webkit-box;
        -webkit-box-orient: vertical;
        -webkit-line-clamp: 2;
    }

    .news-summary,
    .card-desc,
    .ops-mini-meta,
    .detail-content,
    .project-card p,
    .agent-principle,
    .member-tasks li {
        white-space: normal;
        overflow-wrap: anywhere;
        word-break: break-word;
    }
}

@media (max-width: 480px) {
    .container,
    .detail-container,
    .app-main > .container,
    .app-main > .detail-container {
        padding-left: var(--space-sm);
        padding-right: var(--space-sm);
    }

    .panel-card,
    .card,
    .agent-card,
    .project-card,
    .news-item,
    .top-news-card,
    .stat-card,
    .detail-article,
    .activity-list,
    .ops-table-wrap {
        padding: var(--space-sm);
    }
}

/* ============================================
   TOUCH TARGET MINIMUM SIZE (모바일 터치 영역)
   ============================================ */
@media (max-width: 768px) {
    /* 최소 터치 대상 44px 확보 */
    .card-link,
    .news-link,
    .detail-back,
    .detail-action-btn,
    .breadcrumb a,
    .related-article-card,
    .detail-nav-btn,
    .cat-btn,
    .fact-filter-btn,
    .panel-expand-btn {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        padding: 0.5rem 0.75rem;
    }

    /* 백업 테이블 가로 스크롤 */
    .ops-table-wrap,
    .backup-table-wrap {
        display: block;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        max-width: 100vw;
    }

    /* summary-value 코드 블록 줄바꿈 */
    .summary-value.summary-code,
    .summary-value code,
    code {
        word-break: break-all;
        overflow-wrap: break-word;
        max-width: 100%;
    }

    /* breadcrumb 간격 확대 */
    .breadcrumb a {
        padding: 0.5rem 0.25rem;
    }

    /* 원문 링크 터치 영역 */
    .news-link {
        padding: 0.5rem 0.75rem;
        font-size: 0.8rem;
    }
}
