/* ========================================
   모바일 가독성 개선 CSS (Updated V19 - Clean Tree)
   모바일에서 출퇴근 시 읽기 좋게 최적화
   ======================================== */

/* ========================================
   1. 기본 레이아웃 및 가로 스크롤 방지
   ======================================== */
@media (max-width: 768px) {

    /* 뷰포트 강제 설정 */
    html,
    body {
        max-width: 100vw;
        overflow-x: hidden;
    }

    /* 불필요한 장식 요소 제거 (글로우 등) */
    body::before,
    body::after {
        display: none !important;
    }

    /* 메인 컨테이너 너비 리셋 */
    .main,
    .post-single,
    .page-header,
    .post-content {
        width: auto !important;
        min-width: 0 !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
    }

    /* 헤더 겹침 방지 (중첩 구조 완벽 대응) */
    /* 1. 모든 .main 컨테이너에 상단 여백 부여 (최상위 적용용) */
    .main {
        padding: 60px 16px 0 !important;
        /* 헤더(56px) + 여유분 */
        min-height: calc(100vh - 60px);
    }

    /* 2. .main 안에 또 .main이 있는 경우, 내부 .main은 여백 리셋 */
    .main .main {
        padding-top: 0 !important;
        padding-left: 0 !important;
        padding-right: 0 !important;
        min-height: auto !important;
    }

    .post-content {
        font-size: 16px;
        line-height: 1.85;
        word-break: keep-all;
        overflow-wrap: break-word;
        padding: 0 !important;
        /* 내부 패딩 제거 */
    }

    /* 제목 크기 조정 */
    .post-title {
        font-size: 1.7rem !important;
        line-height: 1.35;
        word-break: keep-all;
        margin-bottom: 0.5rem !important;
    }

    .post-content h1 {
        font-size: 1.6rem !important;
        margin: 2rem 0 1rem;
    }

    .post-content h2 {
        font-size: 1.45rem !important;
        margin: 1.75rem 0 0.8rem;
        padding-bottom: 0.4rem;
    }

    .post-content h3 {
        font-size: 1.25rem !important;
        margin: 1.5rem 0 0.6rem;
    }
}

/* ========================================
   2. 코드 블록 가독성 향상
   ======================================== */
@media (max-width: 768px) {

    /* 코드 블록 컨테이너 */
    .post-content pre {
        margin: 1.25rem -16px;
        /* 화면 꽉 차게 */
        padding: 1rem 16px;
        border-radius: 0;
        font-size: 14px !important;
        /* 폰트 키움 */
        line-height: 1.6;
        -webkit-overflow-scrolling: touch;
        overflow-x: auto;
        background-color: var(--code-block-bg) !important;
    }

    .post-content pre code {
        font-size: 14px !important;
        font-family: 'JetBrains Mono', 'Fira Code', 'Menlo', monospace;
        line-height: 1.6;
        tab-size: 2;
    }

    /* 인라인 코드 */
    .post-content code {
        font-size: 0.9em;
        padding: 0.2em 0.4em;
        word-break: break-word;
        background-color: var(--code-bg);
    }
}

/* ========================================
   3. 테이블 가독성 향상
   ======================================== */
@media (max-width: 768px) {
    .post-content table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        font-size: 15px !important;
        /* 폰트 키움 */
        margin: 1.5rem 0;
        border-radius: 8px;
        box-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    }

    .post-content table th,
    .post-content table td {
        padding: 0.75rem 0.75rem;
        /* 터치 영역 확보 */
        min-width: 80px;
        /* 너무 좁아지지 않게 */
        vertical-align: top;
        white-space: normal;
        /* 줄바꿈 허용 */
    }

    /* 테이블 헤더 강조 */
    .post-content table th {
        background-color: var(--code-bg);
        font-weight: 600;
        white-space: nowrap;
        /* 헤더는 한 줄로 */
    }
}

/* ========================================
   4. Mermaid 다이어그램 최적화
   ======================================== */
@media (max-width: 768px) {
    .mermaid {
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
        margin: 1.5rem -16px;
        /* 화면 꽉 차게 */
        padding: 1rem 16px;
        text-align: center;
        background: var(--code-bg);
        /* 배경색 추가로 구분 */
    }

    .mermaid svg {
        max-width: none !important;
        min-width: 100%;
        height: auto;
    }

    /* Mermaid 텍스트 크기 강제 조정 */
    .mermaid g.label {
        font-size: 14px !important;
    }
}

/* ========================================
   5. 이미지 및 미디어
   ======================================== */
@media (max-width: 768px) {
    .post-content img {
        margin: 1.5rem 0;
        max-width: 100% !important;
        height: auto;
        border-radius: 8px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }

    .post-content figure {
        margin: 1.5rem 0;
    }
}

/* ========================================
   6. 리스트 및 기타 요소
   ======================================== */
@media (max-width: 768px) {

    .post-content ul,
    .post-content ol {
        padding-left: 1.25rem;
    }

    .post-content li {
        margin-bottom: 0.5rem;
        padding-left: 0.25rem;
    }

    .post-content hr {
        margin: 2.5rem 0;
        opacity: 0.5;
    }

    .post-content blockquote {
        margin: 1.5rem 0;
        padding: 1rem 1.25rem;
    }
}

/* ========================================
   7. 네비게이션 및 인터페이스
   ======================================== */
@media (max-width: 768px) {
    .site-header {
        padding: 0.75rem 16px;
    }

    .paginav {
        margin-top: 2rem;
        flex-direction: column !important;
        display: flex !important;
        gap: 12px !important;
    }

    .paginav a {
        width: 100% !important;
        margin: 0 !important;
        border: 1px solid var(--border);
        padding: 1rem;
        box-sizing: border-box;
        border-radius: 8px !important;
        text-align: left !important;
        line-height: 1.4;
    }

    .paginav .next {
        margin-inline-start: 0 !important;
        text-align: left !important;
    }
}

/* ========================================
   8. 홈 화면 카드 및 태그
   ======================================== */
@media (max-width: 768px) {
    .post-entry {
        margin-bottom: 1.5rem;
    }

    .post-entry .entry-footer {
        display: flex;
        flex-direction: column;
        align-items: flex-start;
        gap: 8px;
    }

    .share-buttons {
        justify-content: flex-start !important;
        flex-wrap: wrap !important;
    }
}

/* ========================================
   9. 홈 화면 페이지네이션 (.pagination)
   ======================================== */
@media (max-width: 768px) {
    .pagination {
        width: 100% !important;
        max-width: 100% !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
        justify-content: center !important;
        margin-top: 2rem;
    }

    .pagination a {
        padding: 8px 12px !important;
        min-width: 36px;
        text-align: center;
        box-sizing: border-box !important;
        margin: 0 !important;
        border-radius: 18px !important;
    }

    .pagination .prev,
    .pagination .next {
        padding: 8px 16px !important;
    }
}

/* ========================================
   10. 태그 잘림(Clipping) 해결
   ======================================== */
@media (max-width: 768px) {

    .post-tags,
    .post-tags-container {
        max-height: none !important;
        overflow: visible !important;
        height: auto !important;
        display: flex !important;
        flex-wrap: wrap !important;
        justify-content: flex-start !important;
        gap: 6px !important;
        padding: 4px 0 !important;
        margin-top: 4px !important;
    }

    .post-tags a {
        margin-bottom: 4px !important;
        display: inline-block !important;
    }
}

/* ========================================
   11. 그리드 오버플로우 방지
   ======================================== */
@media (max-width: 768px) {
    .post-grid {
        display: flex !important;
        flex-direction: column !important;
        width: 100% !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        gap: 1.5rem !important;
        padding: 0 !important;
    }

    .post-entry {
        width: 100% !important;
        max-width: 100% !important;
        min-width: 0 !important;
        margin: 0 !important;
    }
}

/* ========================================
   12. 모바일 전용 TOC (.mobile-toc) (NEW)
   ======================================== */
@media (max-width: 768px) {
    .mobile-toc {
        display: block !important;
        margin: 1.5rem 0;
        padding: 0;
        border: 1px solid var(--border);
        border-radius: 8px;
        background-color: var(--entry);
        /* 엔트리 배경과 동일하게 */
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
    }

    .mobile-toc summary {
        cursor: pointer;
        padding: 1rem;
        font-weight: 600;
        font-size: 1.1rem;
        list-style: none;
        /* 기본 화살표 제거 */
        display: flex;
        justify-content: space-between;
        align-items: center;
        user-select: none;
    }

    /* 커스텀 화살표 아이콘 */
    .mobile-toc summary::after {
        content: '▼';
        font-size: 0.8em;
        transition: transform 0.2s;
        opacity: 0.7;
    }

    .mobile-toc[open] summary::after {
        transform: rotate(180deg);
    }

    .mobile-toc[open] summary {
        border-bottom: 1px solid var(--border);
    }

    .mobile-toc .inner {
        padding: 1rem;
        max-height: 400px;
        overflow-y: auto;
    }

    .mobile-toc ul {
        margin: 0;
        padding: 0 0 0 1rem;
        list-style: none;
    }

    .mobile-toc li {
        margin-bottom: 0.5rem;
    }

    .mobile-toc a {
        text-decoration: none;
        color: var(--primary);
        font-size: 0.95rem;
        display: block;
        padding: 2px 0;
        line-height: 1.4;
    }

    .mobile-toc a:hover {
        color: var(--secondary);
        text-decoration: underline;
    }
}

/* ========================================
   13. 커스텀 히어로 섹션 및 헤더 최적화 (Nano-Size Flex Row)
   ======================================== */
@media (max-width: 768px) {

    /* 부모 컨테이너: 패딩 축소 */
    .learning-hero {
        padding: 12px 16px !important;
        margin-bottom: 16px !important;
        min-height: auto !important;
        border-bottom: 1px solid var(--border);
    }

    /* Flex Row 레이아웃: 가로 배치로 변경 */
    .learning-hero .hero-content {
        display: flex !important;
        flex-direction: row !important;
        align-items: center !important;
        justify-content: space-between !important;
        gap: 12px !important;
        text-align: left !important;
        width: 100% !important;
    }

    /* 아이콘: 작게 고정 (div.hero-icon 포함) */
    .learning-hero .hero-content svg,
    .learning-hero .hero-content img,
    .learning-hero .hero-content .hero-icon {
        width: 32px !important;
        height: 32px !important;
        font-size: 24px !important;
        /* 이모지 대응 */
        display: flex !important;
        align-items: center;
        justify-content: center;
        margin-bottom: 0 !important;
        flex-shrink: 0;
    }

    /* 텍스트 래퍼 (Title) */
    .hero-text-wrapper {
        flex: 1;
        min-width: 0 !important;
        /* 중요: Flex Item 축소 허용 */
    }

    /* 제목: 긴 제목 처리 (2줄 허용, 배지 제거로 공간 확보) */
    .learning-hero .hero-content h1 {
        font-size: 1.2rem !important;
        margin: 0 !important;
        line-height: 1.25 !important;
        min-width: 0 !important;
        /* 중요: Flex Item 축소 허용 */
        flex: 1;
        /* 남은 공간 차지 */
        /* 줄바꿈 허용 (white-space: normal) */
        white-space: normal !important;
        /* 넘치면 숨김 (안전망) */
        overflow: hidden;
        text-overflow: ellipsis;
        /* 최대 2줄까지만 표시 (Webkit Line Clamp) */
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
    }

    /* 설명글: 공간 절약을 위해 숨김 */
    .learning-hero .hero-content p {
        display: none !important;
    }

    /* 배지: 제목 공간 확보를 위해 모바일에서 숨김 */
    .hero-badges {
        display: none !important;
    }

    /* 버튼: 작고 인라인으로 */
    .learning-hero .hero-btn,
    .learning-hero .hero-actions {
        padding: 6px 10px !important;
        font-size: 0.8rem !important;
        margin: 0 !important;
        height: auto !important;
        white-space: nowrap;
        flex-shrink: 0;
    }

    /* 일반 리스트 페이지 헤더 축소 (.page-header) */
    .page-header h1 {
        font-size: 1.4rem !important;
    }

    .page-header {
        margin-bottom: 1rem !important;
        padding-top: 0.5rem !important;
        padding-bottom: 0.5rem !important;
    }
}

/* ========================================
   14. 타임라인 페이지 아이템 모바일 최적화 (Clean Tree V19)
   ======================================== */
@media (max-width: 768px) {

    /* 타임라인 컨테이너 - 확실한 Border 활용 */
    .timeline-container {
        padding: 0 !important;
        position: relative !important;
        margin-left: 16px !important;
        /* 화면 왼쪽에서 띄움 */
        padding-left: 24px !important;
        /* 선과 카드 사이 간격 */
        border-left: 3px solid var(--border) !important;
        /* 굵고 선명한 메인 줄기 */
    }

    /* 네비게이션 버튼 (기존 유지) */
    .timeline-nav-item {
        padding: 8px 12px !important;
        font-size: 0.9rem !important;
    }

    .timeline-nav {
        margin-bottom: 24px !important;
        gap: 8px !important;
        flex-wrap: wrap !important;
        position: relative;
        padding-left: 0 !important;
        margin-left: -26px !important;
        /* 패딩+보더 보정 */
        width: calc(100% + 26px) !important;
    }

    /* 개별 아이템 - Clean Tree 카드 (동그라미 제거) */
    .timeline-item {
        padding: 20px 24px !important;
        /* 내부 여백 대폭 확대 (시원하게) */
        margin-bottom: 20px !important;
        /* 카드 간격 */
        background: var(--entry) !important;
        box-shadow: 0 4px 6px rgba(0, 0, 0, 0.06) !important;
        /* 아주 깔끔한 그림자 */
        border: 1px solid var(--border) !important;
        border-radius: 8px !important;
        /* 조금 더 각진 느낌 (Techy) */
        position: relative !important;
    }

    /* 2. 연결선 (Horizontal Line - 가지) */
    .timeline-item::before {
        content: '';
        position: absolute;
        left: -32px;
        /* 메인 줄기에서 시작 (padding 24 + border 3 + extra 5) -> 정렬 필요 */
        /* padding-left: 24px, border: 3px. width 18px 정도면 될듯. */
        /* 정확히 border-left(3px)에 붙어야 함. */
        top: 32px;
        /* 카드 상단에서 적당한 위치 (타이틀 중앙 쯤) */
        width: 24px !important;
        /* 아주 긴 가로선 (명확한 연결) */
        height: 3px !important;
        /* 메인 줄기와 동일한 두께 */
        background-color: var(--border) !important;
        /* 색상 통일 */
        border-radius: 0 !important;
        /* 동그라미 아님 */
        border: none !important;
        z-index: 1;
        box-shadow: none !important;
        /* 그림자 제거 */
    }

    /* 아이템 제목 - 가독성 극대화 */
    .timeline-item h3,
    .timeline-item .timeline-title {
        font-size: 1.25rem !important;
        /* 폰트 큼직하게 */
        font-weight: 800 !important;
        margin-bottom: 6px !important;
        color: var(--primary) !important;
        line-height: 1.35 !important;
    }

    /* 날짜/메타 정보 */
    .timeline-item .timeline-date,
    .timeline-meta {
        font-size: 0.9rem !important;
        /* 메타 정보도 잘 보이게 */
        margin-bottom: 12px !important;
        color: var(--secondary);
        display: block !important;
        font-weight: 500 !important;
    }

    /* 태그 */
    .timeline-item .tags,
    .timeline-tags {
        margin-top: 12px !important;
        padding: 0 !important;
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
    }

    .timeline-item .tags span,
    .timeline-tags span,
    .timeline-tags a {
        font-size: 0.85rem !important;
        /* 태그도 조금 더 크게 */
        padding: 6px 10px !important;
        /* 태그 터치 영역 확보 */
        margin: 0 !important;
        border-radius: 4px !important;
        background: var(--code-bg) !important;
        color: var(--secondary) !important;
        border: 1px solid var(--border) !important;
        /* 테두리 추가로 선명하게 */
        font-weight: 500 !important;
    }

    /* 설명글 등 계속 숨김 */
    .timeline-item p,
    .timeline-item .timeline-description,
    .timeline-desc,
    .module-desc,
    .timeline-order,
    .timeline-counter {
        display: none !important;
    }

    /* 단계(Phase) 헤더 */
    .timeline-phase-title,
    .timeline-section-title {
        margin-top: 48px !important;
        margin-bottom: 24px !important;
        padding-left: 0 !important;
        position: relative !important;
        color: var(--primary) !important;
        font-size: 1.4rem !important;
        font-weight: 900 !important;
    }

    /* 단계 구분선 (T자형 분기) */
    .timeline-phase-title::before,
    .timeline-section-title::before {
        content: '';
        position: absolute;
        left: -35px;
        /* Border 위치 */
        top: 10px;
        width: 25px;
        height: 4px;
        /* 조금 더 굵게 */
        background-color: var(--primary);
        /* 강조색 */
        border-radius: 0;
        z-index: 2;
        box-shadow: none;
    }
}