:root {
    --primary: #5B5FC7;
    --primary-dark: #4547A9;
    --primary-light: #6E72C8;
    --secondary: #7B7FD4;
    --hover-bg: #EDEDF8;
    --page-bg: #F4F4FB;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body { font-family: 'Noto Sans KR', 'Malgun Gothic', '맑은 고딕', sans-serif; background: var(--page-bg); color: #333; }

#loading-overlay {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0; bottom: 0;
    background: rgba(255,255,255,0.8);
    z-index: 9999;
    justify-content: center;
    align-items: center;
    flex-direction: column;
    gap: 12px;
}
#loading-overlay.show { display: flex; }
#loading-overlay p {
    font-size: 13px;
    font-weight: 600;
    color: var(--primary);
}
.loading-spinner {
    width: 36px;
    height: 36px;
    border: 4px solid #e0e0e0;
    border-top: 4px solid var(--primary);
    border-radius: 50%;
    animation: spin 0.8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

#btn-top {
    position: fixed;
    bottom: 30px;
    right: 30px;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    border: none;
    background: var(--primary);
    color: #fff;
    font-size: 11px;
    font-weight: 700;
    cursor: pointer;
    box-shadow: 0 3px 10px rgba(0,0,0,0.2);
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s, transform 0.3s;
    z-index: 999;
}
#btn-top.show {
    opacity: 1;
    pointer-events: auto;
}
#btn-top:hover {
    transform: scale(1.1);
}

header {
    background: var(--primary);
    color: white;
    padding: 16px 24px;
    display: flex;
    align-items: center;
    justify-content: space-between;
}
header h1 { font-size: 23px; }
.header-logo { height: 36px; width: auto; }
.header-left { display: flex; align-items: center; gap: 12px; }
.data-date {
    font-size: 11px;
    font-weight: 500;
    color: rgba(255,255,255,0.6);
}

.header-right { display: flex; align-items: center; gap: 12px; }

.layout-toggle-btn {
    background: rgba(255,255,255,0.2);
    border: none; border-radius: 50%;
    width: 36px; height: 36px;
    color: white; cursor: pointer;
    transition: all 0.2s;
    display: flex; align-items: center; justify-content: center;
}
.layout-toggle-btn:hover { background: rgba(255,255,255,0.4); }

.tab-bar {
    display: flex;
    justify-content: center;
    padding: 10px 24px;
    background: white;
    border-bottom: 1px solid #eee;
}
.tabs { display: flex; gap: 20px; }
.tab {
    padding: 6px 40px;
    border: 1px solid var(--secondary);
    border-radius: 16px;
    cursor: pointer;
    font-size: 13px;
    font-weight: 600;
    background: white;
    color: var(--secondary);
    transition: all 0.2s;
}
.tab.active { background: var(--primary); color: white; border-color: var(--primary); }
.tab:hover { background: var(--hover-bg); }
.tab.active:hover { background: var(--primary-light); }

.filters {
    padding: 12px 24px;
    margin-bottom: 10px;
    background: white;
    border-bottom: 1px solid #ddd;
    display: flex;
    gap: 10px;
    align-items: center;
    flex-wrap: wrap;
}
.filter-row {
    display: contents;
}
.filter-row-info { margin-left: auto; }
.filters label {
    font-size: 13px;
    font-weight: 600;
    color: #555;
    margin-left: 0;
}
.filters select, .filters button {
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 13px;
}
#btn-refresh {
    background: var(--primary);
    color: white;
    border: none;
    cursor: pointer;
    font-weight: 600;
}
#btn-refresh:hover { background: var(--primary-dark); }

.info-rate-wrap {
    margin-left: auto;
    margin-right: 12px;
}
.info-icon {
    position: relative;
    display: inline-flex;
    align-items: center;
    gap: 2px;
    font-size: 13px;
    font-weight: 600;
    color: #888;
    cursor: pointer;
}
.info-icon:hover { color: var(--primary); }
.info-tooltip {
    display: none;
    position: absolute;
    top: 32px;
    right: 0;
    background: #333;
    color: #fff;
    padding: 12px 14px;
    border-radius: 8px;
    font-size: 12px;
    line-height: 1.6;
    width: 260px;
    z-index: 100;
    box-shadow: 0 4px 12px rgba(0,0,0,0.2);
}
.info-icon:hover .info-tooltip { display: block; }

.page { padding: 20px 54px; display: flex; flex-direction: column; gap: 20px; max-width: 960px; margin: 0 auto; }
.page.hidden { display: none; }

.summary-cards {
    display: flex;
    gap: 16px;
    width: 100%;
}
.summary-card {
    flex: 1;
    background: #fff;
    border-radius: 12px;
    padding: 18px 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    text-align: center;
    border-top: 3px solid var(--secondary);
}
.card-label {
    font-size: 12px;
    font-weight: 600;
    color: #888;
    margin-bottom: 6px;
}
.card-value {
    font-size: 24px;
    font-weight: 800;
    color: var(--primary);
    line-height: 1.2;
}
.card-value.up { color: #C05050; }
.card-value.down { color: #2B7CB5; }
.card-sub {
    font-size: 11px;
    color: #aaa;
    margin-top: 4px;
}

.chart-container {
    background: white;
    border-radius: 12px;
    padding: 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.06);
    width: 100%;
}
.chart-container.wide { width: 100%; }
.chart-container h3 {
    font-size: 15px;
    color: #555;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--primary);
}
canvas { max-height: 350px; }

.butterfly-header {
    display: flex;
}
.butterfly-header-item {
    flex: 1;
    text-align: center;
    font-size: 13px;
    font-weight: 700;
}
.butterfly-header-item.female { color: #E0607E; }
.butterfly-header-item.male { color: #4A90D9; }
.butterfly-footer {
    display: flex;
}
.butterfly-footer-item {
    flex: 1;
    text-align: center;
    font-size: 15px;
    font-weight: 800;
    padding: 4px 0 0;
}
.butterfly-footer-item.female { color: #E0607E; }
.butterfly-footer-item.male { color: #4A90D9; }
.butterfly-canvas-wrap {
    width: 100%;
    position: relative;
}
.butterfly-canvas-wrap canvas {
    height: 380px !important;
    max-height: none;
}

/* 5개구 뷰 전환 헤더 — 세로 모드에서 숨김 */
.district-view-header {
    display: none;
    align-items: center;
    justify-content: space-between;
    padding: 0 0 12px 0;
    border-bottom: 2px solid var(--primary);
    margin-bottom: 16px;
    width: 100%;
}
body.horizontal-layout .district-view-header {
    display: flex;
    grid-column: 1 / -1;
}
.district-view-right {
    display: flex;
    align-items: center;
    gap: 12px;
}
.district-view-toggle {
    flex-shrink: 0;
}
.district-section.hidden { display: none; }
body:not(.horizontal-layout) #district-section-detail.hidden { display: block; }
body.horizontal-layout #district-section-detail.hidden { display: none; }
body.horizontal-layout #district-section-all.hidden { display: none; }
body.horizontal-layout .section-header-vertical { display: none; }

/* 5개구 페이지 섹션 구분 */
.district-section {
    width: 100%;
    margin-bottom: 12px;
}
.section-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0 0 12px 0;
    border-bottom: 2px solid var(--primary);
    margin-bottom: 16px;
}
.section-title {
    font-size: 17px;
    font-weight: 700;
    color: #333;
    margin: 0;
}
.section-charts {
    display: flex;
    flex-direction: column;
    gap: 20px;
}
.section-filter {
    display: flex;
    align-items: center;
    gap: 8px;
}
.section-filter label {
    font-size: 13px;
    font-weight: 600;
    color: #555;
}
.section-filter select {
    padding: 6px 12px;
    border: 1px solid #ccc;
    border-radius: 6px;
    font-size: 13px;
    font-weight: 600;
    color: #333;
    cursor: pointer;
}
.section-filter select:focus {
    border-color: var(--primary);
    outline: none;
}

.chart4-layout {
    display: flex;
    gap: 0;
    align-items: center;
    justify-content: flex-start;
    padding-right: 60px;
}
.chart4-ranking {
    flex: 0 0 240px;
    display: flex;
    flex-direction: column;
    gap: 10px;
    justify-content: center;
}
.rank-item {
    display: flex;
    align-items: center;
    gap: 10px;
}
.rank-num {
    font-size: 14px;
    font-weight: 800;
    color: #999;
    width: 20px;
    text-align: center;
    flex-shrink: 0;
}
.rank-color {
    width: 6px;
    height: 32px;
    border-radius: 3px;
    flex-shrink: 0;
}
.rank-info {
    flex: 1;
    min-width: 0;
}
.rank-name {
    font-size: 13px;
    font-weight: 700;
    color: #333;
}
.rank-bar-wrap {
    height: 6px;
    background: #f0f0f0;
    border-radius: 3px;
    margin-top: 3px;
    overflow: hidden;
}
.rank-bar {
    height: 100%;
    border-radius: 3px;
    transition: width 0.4s ease;
}
.rank-count {
    font-size: 13px;
    font-weight: 700;
    color: #333;
    flex-shrink: 0;
    white-space: nowrap;
}
.rank-pct {
    font-size: 11px;
    color: #999;
    margin-left: 2px;
}
.chart4-map {
    flex: 1;
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
}
.chart4-map-svg {
    width: 100%;
    max-width: 380px;
}
.chart4-map svg {
    width: 100%;
    height: auto;
}
.district {
    stroke: #fff;
    stroke-width: 1.5;
    cursor: pointer;
    transition: opacity 0.2s;
}
.district:hover {
    opacity: 0.8 !important;
    stroke-width: 2.5;
}
.district-label {
    font-size: 11px;
    font-weight: 700;
    fill: #fff;
    text-anchor: middle;
    pointer-events: none;
}
.district-count {
    font-size: 14px;
    font-weight: 600;
    fill: #fff;
    text-anchor: middle;
    pointer-events: none;
}
.map-tooltip {
    position: absolute;
    background: rgba(26, 26, 46, 0.9);
    color: #fff;
    padding: 8px 12px;
    border-radius: 6px;
    font-size: 12px;
    pointer-events: none;
    opacity: 0;
    transition: opacity 0.15s;
    z-index: 10;
    white-space: nowrap;
}

.chart-header-row {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
    padding-bottom: 8px;
    border-bottom: 2px solid var(--primary);
}
.chart-header-row h3 {
    font-size: 15px;
    color: #555;
    margin: 0;
    padding: 0;
    border: none;
}
.chart-inline-wrap { display: flex; align-items: center; gap: 6px; }
.chart-inline-label { font-size: 12px; font-weight: 600; color: #888; white-space: nowrap; margin-left: 0; }
.chart-inline-filter {
    padding: 4px 8px; border: 1px solid #ddd; border-radius: 4px;
    font-size: 12px; color: #555; background: white; cursor: pointer;
}
body.dark-mode .chart-inline-filter { background: #2a3a5c; color: #ddd; border-color: #3a4a6c; }

.multi-select {
    position: relative;
    display: flex;
    align-items: center;
    gap: 6px;
}
.multi-select-label {
    font-size: 12px;
    color: #888;
}
.multi-select-btn {
    padding: 4px 12px;
    font-size: 12px;
    border: 1px solid #ccc;
    border-radius: 6px;
    background: white;
    cursor: pointer;
    color: #555;
    font-weight: 600;
}
.multi-select-btn:hover { border-color: var(--primary); }

.toggle-wrap {
    display: flex;
    gap: 0;
    border: 1px solid #ccc;
    border-radius: 6px;
    overflow: hidden;
}
.toggle-btn {
    padding: 4px 12px;
    font-size: 12px;
    font-weight: 600;
    border: none;
    background: #fff;
    color: #888;
    cursor: pointer;
    transition: all 0.2s;
}
.toggle-btn.active {
    background: var(--primary);
    color: #fff;
}
.toggle-btn:not(.active):hover {
    background: #f0f0f0;
}
.multi-select-dropdown {
    display: none;
    position: absolute;
    top: 100%;
    right: 0;
    background: white;
    border: 1px solid #ddd;
    border-radius: 8px;
    padding: 8px 10px;
    z-index: 50;
    box-shadow: 0 4px 12px rgba(0,0,0,0.12);
    max-height: 240px;
    overflow-y: auto;
    min-width: 100px;
}
.multi-select-dropdown.open { display: block; }
.multi-select-dropdown label {
    display: block;
    font-size: 12px;
    margin-bottom: 4px;
    cursor: pointer;
    white-space: nowrap;
    font-weight: normal;
    margin-left: 0;
}
.multi-select-dropdown input[type="checkbox"] {
    margin-right: 4px;
}

/* ========== 설정 메뉴 ========== */
.settings-wrap { position: relative; }
.settings-btn {
    background: rgba(255,255,255,0.2);
    border: none; border-radius: 50%;
    width: 36px; height: 36px;
    font-size: 18px; color: white;
    cursor: pointer; transition: all 0.2s;
    display: flex; align-items: center; justify-content: center;
}
.settings-btn:hover { background: rgba(255,255,255,0.4); }
.settings-menu {
    display: none;
    position: absolute; top: 44px; right: 0;
    background: white; border-radius: 8px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.15);
    min-width: 240px; z-index: 1000;
    padding: 8px 0;
}
.settings-menu.open { display: block; }
.settings-section { padding: 8px 16px; }
.settings-title {
    font-size: 11px; font-weight: 700;
    color: #888; text-transform: uppercase;
    letter-spacing: 0.5px; margin-bottom: 6px;
}
.settings-item {
    display: block; width: 100%;
    padding: 8px 16px; border: none;
    background: none; font-size: 13px;
    color: #333; cursor: pointer;
    text-align: left; border-radius: 4px;
    transition: background 0.15s;
}
.settings-item:hover { background: #f5f5f5; }
.settings-divider { height: 1px; background: #eee; margin: 4px 0; }
.settings-toggle {
    display: flex; align-items: center; gap: 8px;
    padding: 6px 0; font-size: 13px; color: #333;
    cursor: pointer; margin-left: 0;
}
.settings-toggle input[type="checkbox"] {
    width: 16px; height: 16px; cursor: pointer;
    margin-right: 0;
}

/* ========== 차트 선택 모달 ========== */
.modal-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 2000;
    justify-content: center; align-items: center;
}
.modal-overlay.open { display: flex; }
.modal-box {
    background: white; border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    width: 420px; max-height: 80vh;
    display: flex; flex-direction: column;
}
.modal-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 16px 20px; border-bottom: 1px solid #eee;
}
.modal-header h3 { margin: 0; font-size: 16px; color: #333; }
.modal-close {
    background: none; border: none;
    font-size: 22px; color: #999; cursor: pointer;
}
.modal-close:hover { color: #333; }
.modal-body { padding: 16px 20px; overflow-y: auto; }
.modal-options {
    display: flex; gap: 16px; margin-bottom: 12px;
    font-size: 13px; color: #555;
}
.modal-options label { display: flex; align-items: center; gap: 4px; cursor: pointer; }
.modal-filter-info {
    background: #f0f4f8; border-radius: 6px;
    padding: 8px 12px; font-size: 12px; color: #555;
    margin-bottom: 12px; border-left: 3px solid #4A6FA5;
}
.modal-chart-list .select-all-label {
    font-weight: 600; border-bottom: 1px solid #eee;
    padding-bottom: 8px; margin-bottom: 4px;
}
.modal-chart-list { display: flex; flex-direction: column; gap: 6px; }
.modal-chart-list label {
    display: flex; align-items: center; gap: 8px;
    padding: 8px 10px; border-radius: 6px;
    font-size: 13px; color: #333; cursor: pointer;
    transition: background 0.15s; margin-left: 0;
}
.modal-chart-list label:hover { background: #f5f5f5; }
.modal-chart-list input[type="checkbox"] { width: 16px; height: 16px; }
.modal-footer {
    display: flex; justify-content: flex-end; gap: 8px;
    padding: 12px 20px; border-top: 1px solid #eee;
}
.modal-btn {
    padding: 8px 20px; border-radius: 6px; border: none;
    font-size: 13px; font-weight: 600; cursor: pointer;
}
.modal-btn.cancel { background: #eee; color: #555; }
.modal-btn.confirm { background: var(--primary); color: white; }
.modal-btn.confirm:hover { background: #3d5a8a; }

/* ========== 커스텀 팔레트 ========== */
.palette-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,0.4);
    z-index: 2000;
    justify-content: center; align-items: center;
}
.palette-overlay.open { display: flex; }
.palette-panel {
    background: white; border-radius: 12px;
    box-shadow: 0 8px 32px rgba(0,0,0,0.2);
    width: 440px; max-height: 80vh;
    display: flex; flex-direction: column;
}
.palette-header {
    display: flex; justify-content: space-between; align-items: center;
    padding: 16px 20px; border-bottom: 1px solid #eee;
}
.palette-header h3 { margin: 0; font-size: 16px; color: #333; }
.palette-body { padding: 16px 20px; overflow-y: auto; }
.palette-section { margin-bottom: 16px; }
.palette-section-title {
    font-size: 12px; font-weight: 700; color: #888;
    margin-bottom: 10px; text-transform: uppercase; letter-spacing: 0.5px;
}
.palette-grid {
    display: flex; flex-direction: column; gap: 8px;
}
.palette-row {
    display: flex; align-items: center; gap: 10px;
}
.palette-label {
    font-size: 13px; color: #333; width: 80px;
    flex-shrink: 0; white-space: nowrap; overflow: hidden;
    text-overflow: ellipsis;
}
.palette-color-input {
    width: 36px; height: 28px; border: 1px solid #ddd;
    border-radius: 4px; cursor: pointer; padding: 1px;
}
.palette-hex {
    font-size: 12px; color: #888; font-family: monospace;
    width: 70px;
}
.palette-footer {
    display: flex; justify-content: flex-end; gap: 8px;
    padding: 12px 20px; border-top: 1px solid #eee;
}

body.dark-mode .palette-panel { background: #1f2940; }
body.dark-mode .palette-header { border-color: #2a3a5c; }
body.dark-mode .palette-header h3 { color: #ddd; }
body.dark-mode .palette-label { color: #ccc; }
body.dark-mode .palette-section-title { color: #666; }
body.dark-mode .palette-footer { border-color: #2a3a5c; }

/* ========== 다크 모드 ========== */
body.dark-mode { background: #1a1a2e; color: #e0e0e0; }
body.dark-mode header { background: #16213e; }

/* 탭 바 */
body.dark-mode .tab-bar { background: #1f2940; border-color: #2a3a5c; }
body.dark-mode .tab { background: #2a3a5c; color: #8ab4f8; border-color: #3a5a8c; }
body.dark-mode .tab.active { background: #4A6FA5; color: white; border-color: var(--secondary); }
body.dark-mode .tab:hover { background: #3a4a6c; }

/* 필터 */
body.dark-mode .filters { background: #1f2940; border-color: #2a3a5c; }
body.dark-mode .filters label { color: #aaa; }
body.dark-mode .filters select,
body.dark-mode .filters button { background: #2a3a5c; color: #ddd; border-color: #3a4a6c; }

/* 페이지, 차트 */
body.dark-mode .page { background: #1a1a2e; }
body.dark-mode .chart-container { background: #1f2940; border-color: #2a3a5c; }
body.dark-mode .chart-container h3 { color: #ccc; }
body.dark-mode .chart-header-row h3 { color: #ccc; }

/* KPI 카드 */
body.dark-mode .summary-card { background: #1f2940; border-color: #2a3a5c; }
body.dark-mode .summary-card .card-label { color: #888; }
body.dark-mode .summary-card .card-value { color: #ddd; }
body.dark-mode .summary-card .card-sub { color: #666; }

/* 섹션 구분 */
body.dark-mode .section-divider { border-color: #2a3a5c; }
body.dark-mode .section-header { border-color: #2a3a5c; }
body.dark-mode .section-title { color: #ddd; }
body.dark-mode .section-filter label { color: #aaa; }
body.dark-mode .section-filter select { background: #2a3a5c; color: #ddd; border-color: #3a4a6c; }

/* 5개구 뷰 헤더 */
body.dark-mode .district-view-header { border-color: #2a3a5c; }
body.dark-mode .district-view-header .section-title { color: #ddd; }

/* 랭킹 */
body.dark-mode .rank-item { background: #1f2940; }
body.dark-mode .rank-name { color: #ccc; }
body.dark-mode .rank-count { color: #ddd; }
body.dark-mode .rank-num { color: #666; }
body.dark-mode .rank-bar-wrap { background: #2a3a5c; }

/* 토글 버튼 */
body.dark-mode .toggle-btn { background: #2a3a5c; color: #aaa; }
body.dark-mode .toggle-btn.active { background: var(--primary); color: #fff; }
body.dark-mode .toggle-btn:not(.active):hover { background: #3a4a6c; }
body.dark-mode .toggle-wrap { border-color: #3a4a6c; }

/* 멀티 셀렉트 / 인라인 필터 */
body.dark-mode .multi-select-btn { background: #2a3a5c; color: #ddd; border-color: #3a4a6c; }
body.dark-mode .multi-select-dropdown { background: #1f2940; border-color: #2a3a5c; box-shadow: 0 4px 12px rgba(0,0,0,0.4); }
body.dark-mode .multi-select-dropdown label { color: #ccc; }
body.dark-mode .multi-select-label { color: #888; }
body.dark-mode .chart-inline-label { color: #888; }

/* 로딩 오버레이 */
body.dark-mode #loading-overlay { background: rgba(26,26,46,0.85); }
body.dark-mode .loading-spinner { border-color: #2a3a5c; border-top-color: var(--primary); }

/* 설정 메뉴 */
body.dark-mode .settings-menu { background: #1f2940; }
body.dark-mode .settings-item { color: #ddd; }
body.dark-mode .settings-item:hover { background: #2a3a5c; }
body.dark-mode .settings-toggle { color: #ddd; }
body.dark-mode .settings-title { color: #666; }
body.dark-mode .settings-divider { background: #2a3a5c; }

/* 모달 */
body.dark-mode .modal-box { background: #1f2940; }
body.dark-mode .modal-header { border-color: #2a3a5c; }
body.dark-mode .modal-header h3 { color: #ddd; }
body.dark-mode .modal-close { color: #666; }
body.dark-mode .modal-close:hover { color: #ccc; }
body.dark-mode .modal-body { color: #ccc; }
body.dark-mode .modal-filter-info { background: #2a3a5c; color: #bbb; border-color: #3a5a8c; }
body.dark-mode .modal-chart-list label { color: #ccc; }
body.dark-mode .modal-chart-list label:hover { background: #2a3a5c; }
body.dark-mode .modal-footer { border-color: #2a3a5c; }
body.dark-mode .modal-btn.cancel { background: #2a3a5c; color: #ccc; }
body.dark-mode .modal-options { color: #bbb; }

/* TOP 버튼 */
body.dark-mode #btn-top { background: #2a3a5c; box-shadow: 0 3px 10px rgba(0,0,0,0.4); }

/* 발생률 정보 툴팁 */
body.dark-mode .info-icon { color: #666; }
body.dark-mode .info-icon:hover { color: #8ab4f8; }

/* ========== 가로(수평) 레이아웃 모드 ========== */
body.horizontal-layout .page {
    max-width: 1600px;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
    padding: 20px 32px;
}
body.horizontal-layout .page.hidden { display: none; }

/* 전체 너비 차지 요소 */
body.horizontal-layout .summary-cards,
body.horizontal-layout .district-section,
body.horizontal-layout .chart-container.wide {
    grid-column: 1 / -1;
}

/* 5개구 섹션 내부도 2열 그리드 */
body.horizontal-layout .section-charts {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 20px;
}
body.horizontal-layout .section-charts .chart-container.wide {
    grid-column: 1 / -1;
}

/* 가로 모드에서 나란히 배치할 차트 */
body.horizontal-layout .chart-container.half-horizontal,
body.horizontal-layout .chart-container.wide.half-horizontal,
body.horizontal-layout .section-charts .chart-container.half-horizontal,
body.horizontal-layout .section-charts .chart-container.wide.half-horizontal {
    grid-column: span 1;
}

/* 세로 모드: 분포현황을 월별 차트 뒤로 이동 */
#chart-district-all-ratio-wrap { order: 10; }
body.horizontal-layout #chart-district-all-ratio-wrap { order: 0; }

/* 가로 모드 차트 높이 조정 */
body.horizontal-layout canvas { max-height: 320px; }
body.horizontal-layout .butterfly-canvas-wrap canvas { height: 320px !important; }

/* 가로 모드 KPI 카드 */
body.horizontal-layout .summary-cards {
    display: flex;
    gap: 16px;
}

/* 1200px 미만에서는 가로 모드여도 세로 폴백 */
@media (max-width: 1199px) {
    body.horizontal-layout .page {
        display: flex;
        flex-direction: column;
        max-width: 960px;
    }
    body.horizontal-layout .page.hidden { display: none; }
    body.horizontal-layout .section-charts {
        display: flex;
        flex-direction: column;
    }
}

/* 모바일에서 레이아웃 버튼 숨김 */
@media (max-width: 768px) {
    .layout-toggle-btn { display: none; }
}

/* ========== 고대비 모드 ========== */
body.high-contrast .chart-container { border: 2px solid #000; }
body.high-contrast .summary-card { border: 2px solid #000; }
body.high-contrast .tab { border: 1px solid rgba(255,255,255,0.5); }
body.high-contrast .filters { border-bottom: 2px solid #000; }

/* ========== 모바일 반응형 ========== */
@media (max-width: 768px) {
    /* 헤더 */
    header { padding: 12px 16px; }
    header h1 { font-size: 17px; }
    .data-date { font-size: 10px; }

    /* 필터: 여러 줄 구조 */
    .filters { padding: 10px 16px; gap: 6px; flex-direction: column; align-items: stretch; }
    .filter-row { display: flex; align-items: center; gap: 6px; flex-wrap: nowrap; }
    .filter-row-controls label { flex-shrink: 0; }
    .filter-row-controls button { flex-shrink: 0; }
    .filter-row-info { margin-left: 0; }
    .filter-row-tabs { justify-content: center; }
    .filter-row-tabs .tabs { gap: 4px; }
    .tab { padding: 5px 12px; font-size: 12px; }
    .filters label { font-size: 12px; }
    .filters select, .filters button { padding: 5px 8px; font-size: 12px; }
    #filter-disease { max-width: calc(100vw - 130px); text-overflow: ellipsis; }

    /* 페이지 */
    .page { padding: 12px 12px; gap: 12px; }

    /* KPI 카드: 2열 그리드 */
    .summary-cards {
        display: grid;
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }
    .summary-card { padding: 12px 10px; }
    .card-value { font-size: 18px; }
    .card-label { font-size: 11px; }

    /* 차트: 1열 */
    .chart-container { padding: 14px 12px; }
    .chart-container h3 { font-size: 13px; margin-bottom: 8px; padding-bottom: 6px; }
    canvas { max-height: 280px; }

    /* 나비차트 */
    .butterfly-canvas-wrap canvas { height: 300px !important; }

    /* 5개구 지도+랭킹 */
    .chart4-layout { flex-direction: column; padding-right: 0; gap: 12px; }
    .chart4-ranking { flex: none; width: 100%; }

    /* 차트 헤더 — 제목 크기를 일반 h3와 동일하게 */
    .chart-header-row { flex-direction: row; align-items: center; }
    .chart-header-row h3 { flex: 1; min-width: 0; }
    .chart-inline-wrap { flex-shrink: 0; }
    .chart-inline-filter { font-size: 11px; padding: 3px 6px; }
    .chart-inline-label { font-size: 11px; }

    /* 설정 */
    .settings-menu { min-width: 200px; right: 0; }

    /* 모달 */
    .modal-box { width: 95vw; max-height: 85vh; }
    .palette-panel { width: 95vw; }

    /* 차트 내 인라인 필터 — 제목 좌측, 필터 우측 끝 유지 */
    .section-header { flex-direction: row; align-items: center; }
    .section-title { font-size: 14px; flex: 1; min-width: 0; }
    .section-filter { flex-shrink: 0; }
}

@media (max-width: 480px) {
    header h1 { font-size: 15px; }
    .tab { padding: 4px 10px; font-size: 11px; }
    .page { padding: 8px 8px; gap: 10px; }
    .summary-cards { gap: 8px; }
    .card-value { font-size: 16px; }
    canvas { max-height: 240px; }
    .butterfly-canvas-wrap canvas { height: 260px !important; }
}

/* ========== 인쇄 ========== */
@media print {
    header, .tab-bar, .filters, #btn-top, .settings-wrap,
    .layout-toggle-btn, .toggle-wrap, #chart2-multi-select, .info-rate-wrap,
    .modal-overlay { display: none !important; }
    body { background: white !important; }
    .page { display: block !important; }
    .page.hidden { display: none !important; }
    .chart-container { break-inside: avoid; border: 1px solid #ddd; margin-bottom: 16px; }
    .summary-card { break-inside: avoid; }
}
