:root {
    --primary-color: #2E86AB;
    --accent-color: #E63946;
    --bg-color: #F4F7F6;
    --card-bg: #FFFFFF;
    --text-main: #333333;
    --text-sub: #666666;
    --border-color: #EEEEEE;
    --radius-md: 8px;
    --radius-lg: 12px;
    --shadow-sm: 0 2px 4px rgba(0,0,0,0.05);
    --shadow-md: 0 4px 12px rgba(0,0,0,0.08);
    --shadow-lg: 0 8px 24px rgba(0,0,0,0.12);
}

body {
    font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
    margin: 0;
    padding: 0;
    background: var(--bg-color);
    color: var(--text-main);
    line-height: 1.6;
}

header {
    background: var(--primary-color);
    color: #fff;
    padding: 16px 24px;
    box-shadow: var(--shadow-sm);
}

.header-top {
    max-width: 1200px;
    margin: 0 auto;
    display: flex;
    align-items: center;
}

header h1 {
    font-size: 1.4rem;
    font-weight: 700;
    margin: 0;
}

.title-link {
    text-decoration: none;
    color: inherit;
    transition: opacity 0.2s;
}

.title-link:hover {
    opacity: 0.85;
}

main {
    max-width: 1200px;
    margin: 30px auto;
    padding: 0 20px;
}

section {
    background: var(--card-bg);
    padding: 30px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-md);
}

h2 {
    margin-top: 0;
    margin-bottom: 10px;
    font-size: 1.5rem;
    color: var(--text-main);
}

p {
    color: var(--text-sub);
    margin-bottom: 20px;
}

/* 凡例 */
.legend-container {
    background: #FAFAFA;
    padding: 15px;
    border-radius: var(--radius-md);
    border: 1px solid var(--border-color);
    margin-bottom: 20px;
}

/* ボタン共通スタイル */
button {
    font-family: inherit;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.2s ease;
    border-radius: var(--radius-md);
    border: none;
    outline: none;
}

button:active {
    transform: translateY(1px);
}

/* ツールバー */
.chart-toolbar {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 12px;
    margin-bottom: 20px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

/* 各種ボタン */
.download-btn, .reverse-btn, .back-btn-bottom {
    display: inline-flex;
    align-items: center;
    gap: 8px;
    padding: 10px 16px;
    font-size: 14px;
    box-shadow: var(--shadow-sm);
}

.download-btn {
    background: var(--primary-color);
    color: white;
}
.download-btn:hover {
    background: #236a8a;
    box-shadow: var(--shadow-md);
}
.download-btn:disabled {
    background: #ccc;
    cursor: not-allowed;
    box-shadow: none;
}

.reverse-btn {
    background: white;
    color: var(--text-main);
    border: 1px solid #ddd;
}
.reverse-btn:hover {
    background: #f9f9f9;
    border-color: #ccc;
    box-shadow: var(--shadow-sm);
}

/* ソートグループ (Segmented Control style) */
.sort-group {
    display: flex;
    align-items: center;
    background: #f0f0f0;
    padding: 4px;
    border-radius: var(--radius-md);
    border: 1px solid #ddd;
}

.sort-label {
    font-size: 13px;
    font-weight: 600;
    color: var(--text-sub);
    padding: 0 8px 0 4px;
}

.sort-toggle-btn {
    background: transparent;
    color: var(--text-sub);
    padding: 6px 12px;
    font-size: 13px;
    border-radius: 6px; /* 少し小さめの角丸 */
    box-shadow: none;
}

.sort-toggle-btn:hover {
    color: var(--text-main);
}

.sort-toggle-btn.active {
    background: white;
    color: var(--primary-color);
    box-shadow: 0 1px 3px rgba(0,0,0,0.1);
    font-weight: 700;
}

/* グラフエリア */
.chart-wrapper {
    overflow-x: auto;
    overflow-y: hidden;
    width: 100%;
    padding: 20px 0;
    background: #fff; /* png保存のため白背景 */
    position: relative;
    border-radius: var(--radius-md);
}

/* Chart Title Overlay (Badge style) */
#chart-title-overlay {
    position: sticky;
    left: 20px;
    top: 0;
    display: inline-block;
    background: rgba(255, 255, 255, 0.95);
    padding: 6px 12px;
    border-radius: 20px; /* Capsule shape */
    box-shadow: var(--shadow-sm);
    z-index: 10;
    font-size: 14px;
    font-weight: 700;
    color: var(--primary-color);
    border: 1px solid var(--border-color);
    pointer-events: none;
    margin-bottom: -30px;
    backdrop-filter: blur(4px);
}

.chart {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    height: 450px;
    border-bottom: 2px solid #ddd;
    padding: 0 20px;
    width: max-content;
    min-width: 100%;
}

.station {
    width: 64px;
    display: flex;
    flex-direction: column;
    align-items: center;
    flex-shrink: 0;
    cursor: pointer;
    transition: transform 0.2s, opacity 0.2s;
    position: relative;
}

.station:hover {
    transform: translateY(-2px);
    opacity: 1;
    z-index: 5;
}
.station:hover .bar {
    filter: brightness(1.1);
}

/* Bar Area */
.bar-area {
    height: 280px;
    width: 100%;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

/* Bar Style */
.bar {
    width: 44px;
    background: var(--accent-color);
    position: relative;
    height: max(4px, var(--bar-percent, 0%));
    min-height: 4px;
    border-radius: 4px 4px 0 0;
    transition: all 0.4s cubic-bezier(0.2, 0.8, 0.2, 1);
}

/* Value Label */
.bar .value {
    position: absolute;
    top: -22px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 11px;
    font-weight: 600;
    color: var(--text-main);
    white-space: nowrap;
    opacity: 0.8;
}

/* Mobile Layout (Horizontal Bars) */
@media (max-width: 768px) {
    .chart-wrapper {
        overflow-x: auto; /* 横スクロール許可 */
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch; /* スムーズスクロール */
    }
    
    .chart {
        flex-direction: column !important;
        align-items: stretch !important;
        height: auto !important;
        min-height: auto !important;
        padding: 10px 0;
        border-bottom: none;
        width: 140% !important; /* 画面幅より広くしてバーを長く見せる */
        min-width: 350px;
    }
    
    .station {
        display: flex !important;
        flex-direction: row !important;
        flex-wrap: nowrap !important;
        width: 100% !important;
        min-height: 36px;
        height: auto;
        margin-bottom: 12px;
        align-items: center !important;
        position: relative; /* Stickyのコンテキスト */
    }
    
    .station:hover {
        transform: none; /* Stickyとtransformは相性が悪いので無効化 */
    }

    .station-name {
        order: -1;
        writing-mode: horizontal-tb !important;
        height: auto !important;
        width: 90px !important;
        flex: 0 0 90px !important;
        text-align: left;
        margin-top: 0 !important;
        margin-right: 0 !important; /* マージンなし */
        padding-right: 10px;
        font-size: 11px;
        white-space: normal;
        line-height: 1.2;
        
        /* Sticky設定 */
        position: sticky;
        left: 0;
        background: rgba(255,255,255,0.95);
        z-index: 10;
        border-right: 1px solid #eee;
    }
    
    .bar-area {
        height: 100% !important;
        min-height: 24px;
        flex: 1 !important;
        display: flex !important;
        align-items: center !important;
        justify-content: flex-start !important;
        border-left: none; /* 駅名のボーダーに任せる */
        padding: 0 0 0 10px !important; /* 左に少し余白 */
        padding-right: 50px !important; /* 数値用余白 */
    }
    
    .bar {
        height: 20px !important;
        width: max(4px, var(--bar-percent, 0%)) !important;
        max-width: none !important; /* 制限解除 */
        min-height: auto !important;
        min-width: 4px !important;
        border-radius: 0 4px 4px 0 !important;
    }
    
    .bar .value {
        top: 50% !important;
        left: 100% !important;
        right: auto !important;
        transform: translateY(-50%) !important;
        margin-left: 8px;
        font-size: 10px;
    }
    
    /* ツールバー調整 */
    .chart-toolbar {
        flex-direction: column;
        align-items: stretch;
    }
    
    .download-btn, .reverse-btn, .back-btn-bottom {
        justify-content: center;
    }
    
    .sort-group {
        justify-content: center;
    }
}

/* Station Name (Vertical) */
.station-name {
    margin-top: 10px;
    font-size: 12px;
    text-align: center;
    writing-mode: vertical-rl;
    height: 110px;
    color: var(--text-main);
    letter-spacing: 0.05em;
    user-select: none;
}

/* Navigation Footer */
.navigation-footer {
    margin-top: 30px;
    display: flex;
    justify-content: flex-start;
}

.back-btn-bottom {
    background: white;
    color: var(--primary-color);
    border: 1px solid var(--primary-color);
    font-weight: 600;
}
.back-btn-bottom:hover {
    background: var(--primary-color);
    color: white;
}

/* Footer */
footer {
    max-width: 1200px;
    margin: 0 auto 40px;
    padding: 0 20px;
    text-align: center;
    color: #999;
    font-size: 12px;
}

/* Modal Window */
.modal {
    display: none;
    position: fixed;
    z-index: 2000;
    left: 0;
    top: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0, 0, 0, 0.4);
    backdrop-filter: blur(4px);
    align-items: center;
    justify-content: center;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.modal.active {
    display: flex;
    opacity: 1;
}

.modal-content {
    background-color: white;
    padding: 40px;
    border-radius: var(--radius-lg);
    box-shadow: var(--shadow-lg);
    max-width: 640px;
    width: 90%;
    max-height: 85vh;
    overflow-y: auto;
    position: relative;
    transform: scale(0.95);
    transition: transform 0.3s cubic-bezier(0.2, 0.8, 0.2, 1);
}

.modal.active .modal-content {
    transform: scale(1);
}

.modal-close {
    position: absolute;
    top: 15px;
    right: 20px;
    font-size: 24px;
    color: #ccc;
    background: transparent;
    padding: 5px;
    line-height: 1;
    border-radius: 50%;
    box-shadow: none;
}

.modal-close:hover {
    color: var(--text-main);
    background: #f0f0f0;
}

/* Station Info in Modal */
.station-info {
    margin-bottom: 25px;
    padding-bottom: 20px;
    border-bottom: 1px solid var(--border-color);
}

.station-info h3 {
    margin: 0 0 8px 0;
    font-size: 1.8rem;
    color: var(--text-main);
}

.station-info p {
    margin: 4px 0;
    font-size: 14px;
    color: var(--text-sub);
    display: inline-block;
    margin-right: 15px;
    background: #f0f0f0;
    padding: 4px 10px;
    border-radius: 20px;
}

/* Trend Chart in Modal */
.trend-chart {
    margin-top: 30px;
    height: 280px;
    display: flex;
    align-items: flex-end;
    gap: 15px;
    border-bottom: 1px solid #ddd;
    padding: 0 10px;
}

.trend-bar-wrapper {
    flex: 1;
    display: flex;
    flex-direction: column;
    align-items: center;
}

.trend-bar-area {
    width: 100%;
    height: 240px;
    display: flex;
    align-items: flex-end;
    justify-content: center;
}

.trend-bar {
    width: 60%;
    background: var(--primary-color);
    position: relative;
    min-height: 4px;
    border-radius: 4px 4px 0 0;
    transition: height 0.5s ease;
}

.trend-bar .trend-value {
    position: absolute;
    top: -24px;
    left: 50%;
    transform: translateX(-50%);
    font-size: 13px;
    white-space: nowrap;
    font-weight: 700;
    color: var(--text-main);
}

.trend-year {
    margin-top: 10px;
    font-size: 13px;
    font-weight: 600;
    color: var(--text-sub);
}

/* Scrollbar customization */
.chart-wrapper::-webkit-scrollbar {
    height: 8px;
}
.chart-wrapper::-webkit-scrollbar-track {
    background: #f1f1f1;
    border-radius: 4px;
}
.chart-wrapper::-webkit-scrollbar-thumb {
    background: #ccc;
    border-radius: 4px;
}
.chart-wrapper::-webkit-scrollbar-thumb:hover {
    background: #bbb;
}
