/*
 * 파일명: main.css
 * 위치: /assets/css/
 * 기능: 마크다운 에디터 메인 스타일시트
 * 버전: 2.0.0 (전체 리팩토링)
 * 수정일: 2026-03-04
 */

/* ===================================
 CSS 변수
=================================== */

:root {
    --header-h: 44px;
    --toolbar-h: 42px;
    --footer-h: 36px;
    --ad-h: 90px;
    --panel-header-h: 34px;

    --editor-bg: #ffffff;
    --editor-text: #212529;
    --preview-bg: #ffffff;
    --border-color: #dee2e6;
    --toolbar-bg: #f8f9fa;
    --panel-editor-hd: #495057;
    --panel-preview-hd: #495057;
}

[data-bs-theme="dark"] {
    --editor-bg: #1e2433;
    --editor-text: #e2e8f0;
    --preview-bg: #1a1f2e;
    --border-color: #3a4055;
    --toolbar-bg: #252b3b;
    --panel-editor-hd: #3a4055;
    --panel-preview-hd: #3a4055;
}

/* ===================================
 레이아웃 기반 (Flexbox 수직 구조)
=================================== */

html, body {
    height: 100%;
    margin: 0;
    padding: 0;
}

body {
    display: flex;
    flex-direction: column;
    min-height: 100vh;
    overflow: hidden;
}

/* ===================================
 헤더 (슬림)
=================================== */

.app-header {
    height: var(--header-h);
    flex-shrink: 0;
    padding-top: 0;
    padding-bottom: 0;
}

.app-header .navbar-brand {
    font-size: 1.15rem;
    font-weight: 700;
    letter-spacing: -0.3px;
    padding: 0;
}

.app-header .nav-link {
    font-size: 0.875rem;
}

.app-header .navbar-toggler-sm {
    padding: 0.2rem 0.5rem;
    font-size: 0.85rem;
    border-color: rgba(255, 255, 255, 0.6) !important;
    border-width: 2px !important;
}

/* ===================================
 메인 영역
=================================== */

#appMain {
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    /* 헤더 + 푸터 높이 제외 */
    height: calc(100vh - var(--header-h) - var(--footer-h));
}

/* ===================================
 도구바
=================================== */

.toolbar {
    height: var(--toolbar-h);
    flex-shrink: 0;
    background: var(--toolbar-bg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    padding: 0 0.5rem;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
}

.toolbar::-webkit-scrollbar {
    display: none;
}

.toolbar-inner {
    display: flex;
    align-items: center;
    gap: 2px;
    white-space: nowrap;
    width: 100%;
}

.toolbar-group {
    display: flex;
    align-items: center;
    gap: 2px;
}

.toolbar-sep {
    width: 1px;
    height: 20px;
    background: var(--border-color);
    margin: 0 4px;
    flex-shrink: 0;
}

.toolbar-right {
    margin-left: auto;
}

/* 도구바 버튼 */
.tbtn {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    min-width: 30px;
    height: 30px;
    padding: 0 6px;
    font-size: 0.8rem;
    font-weight: 500;
    line-height: 1;
    border: 1px solid transparent;
    border-radius: 4px;
    cursor: pointer;
    background: transparent;
    color: var(--bs-body-color);
    transition: background 0.15s, border-color 0.15s, color 0.15s;
    white-space: nowrap;
}

.tbtn:hover {
    background: var(--bs-secondary-bg);
    border-color: var(--border-color);
}

.tbtn:active {
    background: var(--bs-tertiary-bg);
}

.tbtn-text {
    font-size: 0.7rem;
    font-weight: 700;
    min-width: 26px;
}

.tbtn-danger {
    color: var(--bs-danger);
}

.tbtn-danger:hover {
    background: rgba(var(--bs-danger-rgb), 0.1);
    border-color: rgba(var(--bs-danger-rgb), 0.3);
}

/* 모바일 미리보기 전환 버튼 (툴바 맨 앞) */
.tbtn-preview-toggle {
    background: var(--bs-primary);
    color: #fff !important;
    border-color: var(--bs-primary);
    padding: 0 10px;
    font-size: 0.8rem;
    font-weight: 600;
    min-width: auto;
    white-space: nowrap;
}

.tbtn-preview-toggle:hover {
    background: #0b5ed7;
    border-color: #0b5ed7;
    color: #fff !important;
}

[data-bs-theme="dark"] .tbtn-preview-toggle {
    background: #3d8bfd;
    border-color: #3d8bfd;
}

.tbtn-active {
    background: var(--bs-primary);
    color: #fff;
    border-color: var(--bs-primary);
}

.tbtn-active:hover {
    background: var(--bs-primary);
    color: #fff;
}

/* ===================================
 에디터 레이아웃 (split-pane)
=================================== */

#editorLayout {
    display: flex;
    flex: 1;
    overflow: hidden;
    height: calc(100vh - var(--header-h) - var(--toolbar-h) - var(--ad-h) - var(--footer-h));
}

/* 패널 공통 */
.panel {
    display: flex;
    flex-direction: column;
    flex: 1;
    overflow: hidden;
    min-width: 0;
}

#editorPanel {
    border-right: 1px solid var(--border-color);
}

.panel-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: var(--panel-header-h);
    padding: 0 0.75rem;
    font-size: 0.8rem;
    font-weight: 600;
    flex-shrink: 0;
    color: #fff;
}

.editor-panel-header {
    background: var(--panel-editor-hd);
}

.preview-panel-header {
    background: var(--panel-preview-hd);
}

.panel-status {
    font-weight: 400;
    opacity: 0.8;
    font-size: 0.75rem;
}

.panel-body {
    flex: 1;
    overflow-y: auto;
    overflow-x: hidden;
}

/* ===================================
 CodeMirror 에디터 스타일
=================================== */

#cmEditor {
    height: 100%;
    display: flex;
    flex-direction: column;
}

/* CodeMirror 컨테이너가 패널을 꽉 채우도록 */
.CodeMirror {
    flex: 1;
    height: 100%;
    font-family: 'Consolas', 'Monaco', 'Courier New', monospace;
    font-size: 14px;
    line-height: 1.6;
    background: var(--editor-bg);
    color: var(--editor-text);
    border: none;
    border-radius: 0;
}

.CodeMirror-scroll {
    min-height: 100%;
}

/* 라이트모드: CodeMirror 기본 테마 조정 */
[data-bs-theme="light"] .CodeMirror {
    background: var(--editor-bg);
    color: var(--editor-text);
}

/* 다크모드: dracula 테마가 자동 적용되므로 배경만 조정 */
[data-bs-theme="dark"] .CodeMirror {
    background: var(--editor-bg);
}

/* 거터(줄번호) */
.CodeMirror-gutters {
    background: var(--toolbar-bg);
    border-right: 1px solid var(--border-color);
}

[data-bs-theme="dark"] .CodeMirror-gutters {
    background: #1a1f2e;
    border-right-color: var(--border-color);
}

.CodeMirror-linenumber {
    color: #999;
    font-size: 0.8rem;
}

/* 플레이스홀더 */
.CodeMirror-placeholder {
    color: #9ca3af;
    font-style: italic;
}

/* 커서 색 (라이트모드) */
[data-bs-theme="light"] .CodeMirror-cursor {
    border-left-color: #333;
}

/* ===================================
 미리보기 콘텐츠 스타일
=================================== */

#previewArea {
    padding: 1.25rem 1.5rem;
    background: var(--preview-bg);
}

.preview-placeholder {
    padding-top: 4rem;
}

/* 마크다운 렌더링 */
#previewArea h1,
#previewArea h2,
#previewArea h3,
#previewArea h4,
#previewArea h5,
#previewArea h6 {
    margin-top: 1.5rem;
    margin-bottom: 0.75rem;
    font-weight: 600;
    line-height: 1.3;
}

#previewArea h1 {
    font-size: 1.875rem;
    border-bottom: 2px solid var(--border-color);
    padding-bottom: 0.4rem;
}

#previewArea h2 {
    font-size: 1.5rem;
    border-bottom: 1px solid var(--border-color);
    padding-bottom: 0.3rem;
}

#previewArea h3 { font-size: 1.25rem; }
#previewArea h4 { font-size: 1.1rem; }

#previewArea p {
    margin-bottom: 1rem;
    line-height: 1.7;
}

#previewArea blockquote {
    border-left: 4px solid #0d6efd;
    margin: 1rem 0;
    padding: 0.5rem 1rem;
    background: rgba(13, 110, 253, 0.06);
    border-radius: 0 0.375rem 0.375rem 0;
    color: var(--bs-secondary-color);
}

#previewArea ul,
#previewArea ol {
    padding-left: 1.75rem;
    margin-bottom: 1rem;
}

#previewArea li {
    margin-bottom: 0.25rem;
    line-height: 1.6;
}

#previewArea code {
    background: rgba(var(--bs-secondary-rgb), 0.1);
    color: #e83e8c;
    padding: 0.125rem 0.35rem;
    border-radius: 0.25rem;
    font-size: 0.875em;
    font-family: 'Consolas', 'Monaco', monospace;
}

#previewArea pre {
    background: var(--toolbar-bg);
    border: 1px solid var(--border-color);
    border-radius: 0.5rem;
    padding: 1rem 1.25rem;
    overflow-x: auto;
    margin-bottom: 1rem;
}

#previewArea pre code {
    background: transparent;
    color: inherit;
    padding: 0;
    border-radius: 0;
    font-size: inherit;
}

#previewArea table {
    width: 100%;
    border-collapse: collapse;
    margin-bottom: 1rem;
    font-size: 0.9rem;
}

#previewArea th,
#previewArea td {
    border: 1px solid var(--border-color);
    padding: 0.5rem 0.75rem;
    text-align: left;
}

#previewArea th {
    background: var(--toolbar-bg);
    font-weight: 600;
}

#previewArea tr:hover td {
    background: rgba(var(--bs-secondary-rgb), 0.04);
}

#previewArea img {
    max-width: 100%;
    height: auto;
    border-radius: 0.375rem;
    box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
}

#previewArea a {
    color: #0d6efd;
    text-decoration: none;
}

#previewArea a:hover {
    text-decoration: underline;
}

#previewArea hr {
    border: none;
    border-top: 2px solid var(--border-color);
    margin: 2rem 0;
}

/* 다크모드 미리보기 조정 */
[data-bs-theme="dark"] #previewArea {
    color: #dee2e6;
}

[data-bs-theme="dark"] #previewArea h1,
[data-bs-theme="dark"] #previewArea h2,
[data-bs-theme="dark"] #previewArea h3,
[data-bs-theme="dark"] #previewArea h4,
[data-bs-theme="dark"] #previewArea h5,
[data-bs-theme="dark"] #previewArea h6 {
    color: #e9ecef;
}

[data-bs-theme="dark"] #previewArea p,
[data-bs-theme="dark"] #previewArea li {
    color: #dee2e6;
}

[data-bs-theme="dark"] #previewArea code {
    color: #f8a5c2;
    background: rgba(255, 255, 255, 0.1);
}

[data-bs-theme="dark"] #previewArea blockquote {
    background: rgba(13, 110, 253, 0.15);
    border-left-color: #6ea8fe;
    color: #adb5bd;
}

[data-bs-theme="dark"] #previewArea table {
    color: #dee2e6;
}

[data-bs-theme="dark"] #previewArea th {
    color: #e9ecef;
    background: #2d3748;
}

[data-bs-theme="dark"] #previewArea td {
    color: #dee2e6;
}

/* ===================================
 광고 배너 (툴바 하단)
=================================== */

.ad-banner {
    flex-shrink: 0;
    width: 100%;
    height: var(--ad-h);
    display: flex;
    align-items: center;
    justify-content: center;
    background: var(--toolbar-bg);
    border-bottom: 1px solid var(--border-color);
    overflow: hidden;
}

.ad-banner:empty,
.ad-banner ins.adsbygoogle[data-ad-status="unfilled"] {
    display: none;
}

/* 전체화면 시 광고 숨김 */
:fullscreen .ad-banner,
:-webkit-full-screen .ad-banner,
.pseudo-fullscreen .ad-banner {
    display: none;
}

:fullscreen #editorLayout,
:-webkit-full-screen #editorLayout,
.pseudo-fullscreen #editorLayout {
    height: calc(100vh - var(--header-h) - var(--toolbar-h));
}

/* ===================================
 푸터 (슬림)
=================================== */

.app-footer {
    height: var(--footer-h);
    flex-shrink: 0;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 0.78rem;
    background: var(--toolbar-bg);
    border-top: 1px solid var(--border-color);
    color: var(--bs-secondary-color);
    padding: 0 1rem;
    position: relative;
    z-index: 10;
}

.app-footer a {
    color: var(--bs-body-color);
    text-decoration: none;
}

.app-footer a:hover {
    text-decoration: underline;
}

/* ===================================
 스크롤바 (webkit)
=================================== */

#previewArea::-webkit-scrollbar,
.CodeMirror-vscrollbar::-webkit-scrollbar {
    width: 6px;
}

#previewArea::-webkit-scrollbar-track {
    background: transparent;
}

#previewArea::-webkit-scrollbar-thumb {
    background: var(--border-color);
    border-radius: 3px;
}

#previewArea::-webkit-scrollbar-thumb:hover {
    background: #adb5bd;
}

/* ===================================
 파일 메뉴 드롭다운 개선
=================================== */

.dropdown-item .small {
    display: block;
}

.dropdown-header {
    font-size: 0.7rem;
    font-weight: 700;
    letter-spacing: 0.05em;
    text-transform: uppercase;
    color: var(--bs-secondary-color);
    padding: 0.25rem 1rem;
}

/* ===================================
 전체화면 모드
=================================== */

:fullscreen #appMain,
:-webkit-full-screen #appMain,
.pseudo-fullscreen #appMain {
    height: 100vh;
}

:fullscreen .app-footer,
:-webkit-full-screen .app-footer,
.pseudo-fullscreen .app-footer {
    display: none;
}

.pseudo-fullscreen {
    position: fixed;
    top: 0;
    left: 0;
    width: 100vw;
    height: 100vh;
    z-index: 9999;
    background: var(--bs-body-bg);
    overflow: hidden;
}

/* ===================================
 반응형 - 태블릿 (992px 이하, 768px 초과)
=================================== */

@media (min-width: 768px) and (max-width: 991.98px) {
    #editorLayout {
        flex-direction: column;
        height: calc(100vh - var(--header-h) - var(--toolbar-h) - var(--ad-h) - var(--footer-h));
    }

    /* 태블릿: 에디터·미리보기 각 50% */
    .panel {
        flex: 1;
        height: 50%;
        min-height: 0;
    }

    #editorPanel {
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    /* 태블릿: 미리보기 항상 표시 */
    #previewPanel {
        display: flex !important;
    }

    /* 태블릿: 모바일 토글 버튼 숨김 (양쪽 항상 보임) */
    #mobileTogglePreview {
        display: none !important;
    }
}

/* ===================================
 반응형 - 모바일 (767px 이하)
=================================== */

@media (max-width: 767.98px) {
    #editorLayout {
        flex-direction: column;
        height: calc(100vh - var(--header-h) - var(--toolbar-h) - var(--ad-h) - var(--footer-h));
    }

    /* 폰: 패널이 전체 높이를 차지 */
    .panel {
        flex: none;
        height: 100%;
    }

    #editorPanel {
        border-right: none;
        border-bottom: 1px solid var(--border-color);
    }

    /* 기본값: 에디터 보이고 미리보기 숨김 (토글로 전환) */
    #previewPanel {
        display: none;
    }
}

/* ===================================
 반응형 - 모바일 (768px 이하)
=================================== */

@media (max-width: 767.98px) {
    :root {
        --header-h: 48px;
        --toolbar-h: 44px;
        --footer-h: 32px;
        --ad-h: 50px;
    }

    .toolbar {
        padding: 0 0.25rem;
    }

    .tbtn {
        min-width: 28px;
        height: 28px;
        font-size: 0.75rem;
        padding: 0 4px;
    }

    .tbtn-text {
        font-size: 0.65rem;
        min-width: 24px;
    }

    #previewArea {
        padding: 1rem;
    }

    .CodeMirror {
        font-size: 13px;
    }

    /* 모바일 네비게이션 */
    .navbar-collapse {
        background: var(--bs-primary);
        padding: 0.5rem;
    }

    /* 모바일: navbar 내 드롭다운을 static으로 (overflow 잘림 방지) */
    .navbar-collapse .dropdown-menu {
        position: static;
        float: none;
        margin-top: 0.25rem;
        border-radius: 0.375rem;
    }
}

/* ===================================
 반응형 - 소형 모바일 (576px 이하)
=================================== */

@media (max-width: 575.98px) {
    .app-header .navbar-brand {
        font-size: 1rem;
    }

    .tbtn {
        min-width: 26px;
        height: 26px;
        font-size: 0.7rem;
        padding: 0 3px;
    }

    .tbtn-text {
        min-width: 22px;
        font-size: 0.6rem;
    }

    #previewArea h1 { font-size: 1.5rem; }
    #previewArea h2 { font-size: 1.25rem; }
}

/* ===================================
 전체화면 스크롤바 숨김
=================================== */

:fullscreen,
:-webkit-full-screen,
.pseudo-fullscreen {
    scrollbar-width: none;
    -ms-overflow-style: none;
}

/* ===================================
 프린트 스타일
=================================== */

@media print {
    .app-header,
    .toolbar,
    .app-footer,
    #editorPanel {
        display: none;
    }

    #editorLayout,
    #previewPanel {
        display: block;
        width: 100%;
        height: auto;
    }

    #previewArea {
        overflow: visible;
        height: auto;
        padding: 0;
    }
}

/* ===================================
 애니메이션
=================================== */

.panel {
    transition: opacity 0.15s ease;
}

.tbtn {
    transition: background 0.15s, border-color 0.15s, color 0.15s;
}
