:root {
    --bg: #f3f6fb;
    --panel: #ffffff;
    --line: #d9e1ec;
    --text: #1e293b;
    --muted: #64748b;
    --primary: #1d4ed8;
    --primary-dark: #173ea6;
    --accent: #eff6ff;
    --danger: #dc2626;
    --success: #0f766e;
    --shadow: 0 18px 48px rgba(15, 23, 42, 0.08);
    --radius: 18px;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; font-family: "Pretendard", "Noto Sans KR", Arial, sans-serif; background: var(--bg); color: var(--text); }
a { color: inherit; text-decoration: none; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
.hidden { display: none !important; }
.badge { display: inline-flex; align-items: center; justify-content: center; min-width: 56px; height: 32px; border-radius: 999px; background: var(--accent); color: var(--primary); font-weight: 700; }
.primary-btn, .secondary-btn, .danger-btn, .inline-link, .template-download { display: inline-flex; align-items: center; justify-content: center; border: 0; border-radius: 12px; padding: 12px 18px; font-weight: 600; transition: 0.2s ease; }
.primary-btn { background: var(--primary); color: #fff; }
.primary-btn:hover { background: var(--primary-dark); }
.secondary-btn { background: #e2e8f0; color: var(--text); }
.secondary-btn:hover { background: #cbd5e1; }
.danger-btn { background: #fee2e2; color: var(--danger); }
.danger-btn:hover { background: #fecaca; }
.full-width { width: 100%; }
.inline-link, .template-download { padding: 8px 12px; background: #eef2ff; color: var(--primary); }
.alert { border-radius: 12px; padding: 14px 16px; margin-bottom: 18px; }
.alert.error { background: #fef2f2; color: #991b1b; border: 1px solid #fecaca; }
.helper-text { color: var(--muted); font-size: 0.95rem; line-height: 1.6; }
.status-chip { display: inline-flex; align-items: center; border-radius: 999px; padding: 8px 12px; background: #ecfeff; color: var(--success); font-weight: 700; font-size: 0.92rem; }
.eyebrow { margin: 0 0 4px; color: var(--muted); font-size: 0.85rem; }

.landing-page, .auth-page { min-height: 100vh; display: flex; align-items: center; justify-content: center; padding: 24px; background: linear-gradient(135deg, #eef4ff, #f8fafc); }
.landing-card, .auth-card, .auth-brand, .table-card, .detail-card, .preview-rendered-card, .upload-box { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); }
.landing-card { width: min(560px, 100%); padding: 48px; text-align: center; }
.auth-shell { display: grid; grid-template-columns: 1.1fr 0.9fr; gap: 24px; width: min(1120px, 100%); }
.auth-brand { padding: 40px; }
.auth-brand h1 { font-size: 2.4rem; margin: 18px 0 12px; }
.auth-brand p, .auth-brand li { color: var(--muted); line-height: 1.7; }
.auth-card { padding: 36px; }
.auth-form { display: grid; gap: 16px; }
.auth-form label span, .field-row label span { display: block; font-weight: 700; margin-bottom: 8px; }
.auth-form input, .field-row input { width: 100%; padding: 14px 16px; border: 1px solid var(--line); border-radius: 12px; background: #fff; }

.editor-page { min-height: 100vh; }
.app-shell { display: grid; grid-template-columns: 320px 1fr; min-height: 100vh; }
.sidebar { background: #0f172a; color: #e2e8f0; padding: 28px 22px; display: flex; flex-direction: column; gap: 20px; }
.sidebar-head h1 { font-size: 1.5rem; margin: 14px 0 12px; color: #fff; }
.sidebar-head p { color: #cbd5e1; line-height: 1.6; }
.template-nav { display: grid; gap: 12px; }
.template-card { border: 1px solid rgba(255,255,255,0.1); border-radius: 16px; padding: 12px; background: rgba(255,255,255,0.04); }
.template-card.active { border-color: #60a5fa; background: rgba(96,165,250,0.12); }
.template-select { width: 100%; text-align: left; background: transparent; border: 0; color: #fff; padding: 6px 0 10px; font-weight: 700; }
.sidebar-link { margin-top: auto; }
.workspace { padding: 28px; display: grid; gap: 20px; }
.workspace-header, .toolbar-panel, .preview-section, .list-header { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); }
.workspace-header, .list-header { padding: 20px 24px; display: flex; align-items: center; justify-content: space-between; gap: 16px; }
.header-actions { display: flex; gap: 12px; flex-wrap: wrap; }
.mobile-menu-btn { display: none; background: #e2e8f0; border: 0; border-radius: 12px; padding: 12px 14px; }
.toolbar-panel { padding: 18px 22px; display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; }
.mode-switch { display: flex; gap: 12px; flex-wrap: wrap; }
.mode-btn { background: #e2e8f0; border: 0; border-radius: 12px; padding: 12px 16px; font-weight: 700; }
.mode-btn.active { background: var(--primary); color: #fff; }
.current-template { color: var(--muted); }
.content-grid { display: grid; grid-template-columns: 1fr; gap: 20px; }
.composer-panel, .preview-section { background: var(--panel); border-radius: var(--radius); box-shadow: var(--shadow); }
.document-form { padding: 24px; }
.field-row { margin-bottom: 18px; }
.editor-wrapper { border: 1px solid var(--line); border-radius: 16px; overflow: hidden; background: #fff; }
.submit-row { display: flex; justify-content: flex-end; margin-top: 18px; }
.upload-panel .upload-box { padding: 24px; }
.file-picker { display: inline-flex; align-items: center; gap: 12px; margin: 14px 0; padding: 14px 18px; background: #eff6ff; color: var(--primary); border-radius: 14px; font-weight: 700; }
.file-picker input { display: none; }
.preview-section { padding: 20px 24px; }
.preview-header { display: flex; align-items: center; justify-content: space-between; gap: 12px; margin-bottom: 14px; }
.preview-area { min-height: 380px; border: 1px dashed var(--line); border-radius: 16px; background: #fff; overflow: hidden; }
.preview-placeholder { padding: 40px 24px; color: var(--muted); }
.rendered-content { padding: 28px; line-height: 1.8; }
.rendered-content table { width: 100%; border-collapse: collapse; }
.rendered-content table th, .rendered-content table td { border: 1px solid #cbd5e1; padding: 10px; }
.preview-frame { width: 100%; min-height: 760px; border: 0; background: #f8fafc; }
.file-preview-meta { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; background: #f8fafc; border-bottom: 1px solid var(--line); }
.doc-preview-text { padding: 20px; }
.doc-preview-text pre { margin: 0; white-space: pre-wrap; word-break: break-word; font-family: inherit; line-height: 1.7; }

.list-page, .preview-page { min-height: 100vh; padding: 28px; }
.list-shell, .preview-shell { display: grid; gap: 20px; width: min(1320px, 100%); margin: 0 auto; }
.table-card, .detail-card, .preview-rendered-card { padding: 24px; }
.document-table { width: 100%; border-collapse: collapse; }
.document-table th, .document-table td { padding: 16px 14px; border-bottom: 1px solid var(--line); text-align: left; vertical-align: middle; }
.document-table th { background: #f8fafc; }
.empty-cell { text-align: center !important; color: var(--muted); }
.detail-grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px 24px; margin: 0; }
.detail-grid dt { font-weight: 700; color: var(--muted); margin-bottom: 6px; }
.detail-grid dd { margin: 0; }

.ck-editor__editable_inline { min-height: 460px; }
.fullscreen-mode { grid-template-columns: 0 1fr; }
.fullscreen-mode .sidebar { display: none; }

@media (max-width: 1024px) {
    .auth-shell, .app-shell { grid-template-columns: 1fr; }
    .sidebar { position: fixed; left: 0; top: 0; bottom: 0; width: min(320px, 88vw); transform: translateX(-100%); z-index: 40; transition: transform 0.25s ease; }
    .app-shell.sidebar-open .sidebar { transform: translateX(0); }
    .mobile-menu-btn { display: inline-flex; }
    .workspace { padding: 18px; }
    .detail-grid { grid-template-columns: 1fr; }
}

/* 공통: 오버플로우 방지 및 박스sizing 강화 */
* {
    box-sizing: border-box;
    min-width: 0;
}

/* min-width: 0 추가로 flex item 삐져나감 방지 */
body {
    overflow-x: hidden;
}

/* 전체 페이지 좌우 스크롤 원천 차단 */

/* 모바일 대응 미디어 쿼리 구간 */
@media (max-width: 768px) {

    /* 1. 패딩 및 여백 축소로 공간 확보 */
    .auth-page,
    .list-page,
    .preview-page,
    .workspace {
        padding: 12px !important;
    }

    /* 2. 카드 컴포넌트들이 화면 너비를 초과하지 않도록 설정 */
    .landing-card,
    .auth-card,
    .auth-brand,
    .table-card,
    .detail-card,
    .preview-rendered-card,
    .upload-box,
    .workspace-header,
    .toolbar-panel,
    .preview-section {
        width: 100% !important;
        max-width: 100% !important;
        padding: 14px !important;
    }

    /* 3. 헤더 및 액션 버튼 영역 수직 정렬 및 100% 채우기 */
    .workspace-header,
    .list-header,
    .preview-header,
    .toolbar-panel {
        align-items: stretch;
        flex-direction: column;
    }

    .header-actions,
    .mode-switch {
        width: 100%;
        display: grid;
        grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
    }

    .header-actions a,
    .header-actions button,
    .mode-switch button,
    .primary-btn,
    .secondary-btn,
    .danger-btn {
        width: 100%;
        justify-content: center;
        text-align: center;
    }

    /* 4. 테이블 가로 스크롤 및 삐져나감 방지 */
    .document-table {
        display: block;
        width: 100%;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* 5. 긴 텍스트나 프리뷰 영역 줄바꿈 처리 */
    .doc-preview-text pre,
    .rendered-content {
        word-break: break-all;
        overflow-x: auto;
    }

    .preview-frame {
        min-height: 400px;
    }
}