:root {
    --header-height: 60px;
    --col-header-height: 40px;
    --row-height: 60px; /* 漢字を大きく見せるため少し高く */
    --spacer-height: 10px;
    --col-width: 100px; /* 年号幅は少し狭くてもOK */
    --header-bg: #1a1a1a; /* 墨色 */
    --accent-color: #cc0000; /* 朱色 */
    --text-color: #333;
    --border-color: #ddd;
    --washi-bg: #fcfaf5; /* 和紙っぽい生成り色 */
}

/* ★修正: 全要素にborder-boxを適用して、サイズ計算のズレを根本的に防ぐ */
*, *::before, *::after {
    box-sizing: border-box;
}

body {
    font-family: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "Times New Roman", "HGS明朝E", "MS PMincho", serif; /* 明朝体優先 */
    margin: 0;
    padding: 0;
    background-color: var(--washi-bg);
    height: 100vh;
    display: flex;
    flex-direction: column;
    overflow: hidden;
    color: var(--text-color);
}

/* --- ヘッダー関連 --- */
.page-header {
    background-color: var(--header-bg);
    color: #fff;
    height: var(--header-height);
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 0 20px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.3);
    z-index: 100;
    flex-shrink: 0;
    font-family: sans-serif; /* ヘッダーUIはゴシックで見やすく */
}

.header-left { display: flex; align-items: center; }
h1 { margin: 0; font-size: 20px; font-weight: bold; color: #fff; letter-spacing: 1px; }
h1 span { color: var(--accent-color); font-size: 14px; margin-left: 5px; font-weight: normal;}

.header-center { flex-grow: 1; display: flex; justify-content: center; }
.search-container { position: relative; width: 300px; }
#search-input {
    width: 100%; padding: 8px 10px; border-radius: 2px; border: none; font-size: 14px;
    background-color: #eee; font-family: sans-serif;
}

.header-right { display: flex; align-items: center; }
.header-btn {
    margin-right: 20px;
    padding: 6px 12px;
    background-color: var(--accent-color);
    color: #fff;
    border: none;
    border-radius: 2px;
    cursor: pointer;
    font-size: 12px;
    font-weight: bold;
    transition: background 0.2s;
}
.header-btn:hover { background-color: #a80000; }

.legend { display: flex; gap: 15px; font-size: 11px; color: #ccc; font-family: sans-serif; }
.legend-item { display: flex; align-items: center; }
.dot { width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; border: 1px solid #666; }
.winner-example { background-color: #FFD700; border-color: #b8860b; } /* 1位は金 */
.normal-dot { background-color: #fff; }

/* --- ハンバーガーメニュー (和風アレンジ) --- */
.menu-container { display: flex; align-items: center; margin-right: 15px; }
.hamburger-btn {
    position: relative; z-index: 2000; width: 40px; height: 40px;
    background: none; border: none; cursor: pointer; padding: 0;
    display: flex; flex-direction: column; justify-content: center; align-items: center; gap: 8px;
}
.hamburger-btn span {
    display: block; width: 30px; height: 2px;
    background-color: #fff; /* シンプルな白 */
    transition: all 0.4s ease;
}
.hamburger-btn:hover span { background-color: var(--accent-color); }
.hamburger-btn.open span:nth-child(1) { transform: translateY(5px) rotate(45deg); background-color: #fff; }
.hamburger-btn.open span:nth-child(2) { transform: translateY(-5px) rotate(-45deg); background-color: #fff; }

/* ナビゲーションオーバーレイ */
.nav-overlay {
    position: fixed; top: 0; left: 0; width: 100%; height: 100vh;
    background-color: rgba(0, 0, 0, 0.95);
    z-index: 1999; opacity: 0; visibility: hidden;
    transition: opacity 0.3s ease, visibility 0.3s;
    display: flex; align-items: center; justify-content: center;
}
.nav-overlay.open { opacity: 1; visibility: visible; }
.nav-list { list-style: none; padding: 0; text-align: center; }
.nav-list li { margin: 25px 0; opacity: 0; transform: translateY(20px); transition: 0.4s ease; }
.nav-overlay.open .nav-list li { opacity: 1; transform: translateY(0); }
.nav-overlay.open .nav-list li:nth-child(1) { transition-delay: 0.1s; }
.nav-overlay.open .nav-list li:nth-child(2) { transition-delay: 0.2s; }
.nav-overlay.open .nav-list li:nth-child(3) { transition-delay: 0.3s; }
.nav-list li a {
    text-decoration: none; color: #fff; font-size: 24px;
    font-family: serif; letter-spacing: 2px; display: block;
}
.nav-list li a:hover { color: var(--accent-color); }
.nav-list li a span { display: block; font-size: 12px; color: #888; margin-top: 5px; font-family: sans-serif; letter-spacing: 0; }
.nav-divider { width: 40px; height: 1px; background-color: #444; margin: 20px auto; }

/* --- マトリックスレイアウト --- */
.matrix-wrapper { flex-grow: 1; overflow: hidden; position: relative; display: flex; background-color: var(--washi-bg); }
.matrix-container { display: flex; width: 100%; height: 100%; }

/* 左側固定：順位ヘッダー */
.row-headers {
    width: 60px; flex-shrink: 0; background-color: #fff;
    border-right: 1px solid #ccc; z-index: 20;
    box-shadow: 2px 0 5px rgba(0,0,0,0.05);
    display: flex; flex-direction: column; height: 100%;
}
.row-header-static {
    height: var(--col-header-height); background-color: #eee; color: #333;
    display: flex; align-items: center; justify-content: center;
    font-weight: bold; font-size: 11px; border-bottom: 1px solid #ccc; flex-shrink: 0; z-index: 25; font-family: sans-serif;
    /* ★修正: box-sizingは全体指定により自動適用されるため、これで高さが正確に40pxになります */
}
.row-header-mask { flex-grow: 1; overflow: hidden; position: relative; }
#row-header-scroll-content { width: 100%; }
.row-header-item {
    height: var(--row-height); display: flex; align-items: center; justify-content: center;
    border-bottom: 1px solid var(--border-color); font-size: 12px; font-weight: bold; color: #555;
    background-color: #fafafa; font-family: serif;
    /* ★修正: 全体指定によりここもborder込みで60pxになり、右側のセルと一致します */
}
.spacer-row { height: var(--spacer-height); background-color: #eee; border: none; }

/* 右側：スクロールエリア */
.matrix-scroll-area { flex-grow: 1; overflow: auto; position: relative; background-color: var(--washi-bg); }

/* 列ヘッダー (年) */
.column-headers {
    display: flex; height: var(--col-header-height); position: sticky; top: 0; z-index: 10;
    background-color: #eee; width: max-content; border-bottom: 1px solid #ccc;
    /* ★修正: ここもborder込みで40pxになり、左側のヘッダーと一致します */
}
.column-header-item {
    width: var(--col-width); flex-shrink: 0; display: flex; flex-direction: column;
    align-items: center; justify-content: center; border-right: 1px solid #ddd;
    font-size: 14px; font-weight: bold; cursor: pointer; transition: background 0.2s;
    background-color: #eee; font-family: serif;
}
.column-header-item:hover { background-color: #ddd; color: #000; }
.col-era { font-size: 10px; color: #666; font-family: sans-serif; margin-top: 2px;}

/* グリッド本体 */
.matrix-grid { display: flex; width: max-content; }
.grid-column { width: var(--col-width); border-right: 1px solid var(--border-color); flex-shrink: 0; }
.grid-cell {
    height: var(--row-height); border-bottom: 1px solid var(--border-color);
    padding: 4px; display: flex; align-items: center; justify-content: center;
    /* ★修正: box-sizingは全体指定でカバー */
}
.grid-cell.spacer-cell { height: var(--spacer-height); background-color: #eee; border-bottom: none; }

/* 漢字パネル */
.kanji-panel {
    width: 100%; height: 100%;
    display: flex; flex-direction: column; align-items: center; justify-content: center;
    border-radius: 4px; cursor: pointer;
    background-color: #fff;
    border: 1px solid #e0e0e0;
    transition: all 0.15s;
    position: relative;
    box-shadow: 0 1px 2px rgba(0,0,0,0.05);
}
.kanji-panel:hover {
    transform: scale(1.1); box-shadow: 0 4px 10px rgba(0,0,0,0.2); z-index: 50; border-color: #999;
}
.kanji-char {
    font-size: 28px; /* 漢字を大きく */
    font-weight: bold;
    line-height: 1;
    color: #333;
}
/* 1位の特別スタイル */
.rank-1-panel {
    background-color: #fffde7; /* 薄い金/黄色 */
    border: 2px solid #FFD700;
}
.rank-1-panel .kanji-char {
    font-size: 34px;
    color: #000;
    text-shadow: 0 1px 0 rgba(255,215,0,0.5);
}
.kanji-score {
    font-size: 10px; color: #888; margin-top: 2px; font-family: sans-serif;
}

/* Dimmed effect for search */
.dimmed { opacity: 0.1; pointer-events: none; filter: grayscale(100%); }

/* --- サイドバー --- */
#details-sidebar {
    position: fixed; top: 0; right: -600px; width: 600px; height: 100%;
    background-color: #fff; 
    box-shadow: -5px 0 15px rgba(0,0,0,0.3);
    z-index: 2100;
    transition: right 0.3s cubic-bezier(0.16, 1, 0.3, 1);
    display: flex; flex-direction: column;
}
#details-sidebar.open { right: 0; }

.sidebar-header { 
    padding: 15px 20px; background: #333; color: #fff; 
    display: flex; align-items: center; flex-shrink: 0; gap: 15px;
}
#sidebar-back-btn {
    background: none; border: 1px solid rgba(255,255,255,0.3); color: inherit;
    font-size: 12px; cursor: pointer; padding: 4px 10px; border-radius: 2px;
    display: none; align-items: center;
}
#sidebar-title { flex-grow: 1; font-size: 18px; margin: 0; font-weight: normal; font-family: serif; }
#close-sidebar-btn { background: none; border: none; color: inherit; font-size: 28px; cursor: pointer; }

.sidebar-content { flex-grow: 1; overflow-y: auto; padding: 25px; font-size: 14px; line-height: 1.6; font-family: "Yu Gothic", sans-serif; }

/* サイドバー内の要素 */
.kanji-large-display {
    text-align: center; margin-bottom: 20px; padding-bottom: 20px; border-bottom: 1px solid #eee;
}
.big-kanji {
    font-size: 100px; font-family: serif; line-height: 1; color: #333;
    display: inline-block; background: #fff; padding: 10px;
    border: 1px solid #ccc; box-shadow: 2px 2px 5px rgba(0,0,0,0.1);
}
.kanji-readings { font-size: 14px; color: #666; margin-top: 10px; }

.detail-section h3 {
    border-left: 4px solid var(--accent-color); padding-left: 10px;
    font-size: 16px; margin: 25px 0 15px; color: #333;
}
.history-table {
    width: 100%; border-collapse: collapse; font-size: 13px;
}
.history-table th, .history-table td {
    border: 1px solid #ddd; padding: 8px; text-align: center;
}
.history-table th { background-color: #f5f5f5; font-weight: bold; }
.history-table .rank-1-row { background-color: #fff8e1; font-weight: bold; }

/* --- ツールチップ --- */
#custom-tooltip {
    position: absolute; display: none; background: rgba(0,0,0,0.85); color: #fff;
    padding: 8px 12px; border-radius: 4px; font-size: 12px; z-index: 3000;
    pointer-events: none; white-space: pre-line; line-height: 1.4;
    box-shadow: 0 2px 5px rgba(0,0,0,0.2); font-family: sans-serif;
}

/* --- オーバーレイ --- */
#global-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.5);
    display: none; z-index: 2000;
}
#global-overlay.open { display: block; }

/* --- モーダル (ランキング等) --- */
.modal-overlay {
    position: fixed; inset: 0; background: rgba(0,0,0,0.8);
    z-index: 2200; display: none; justify-content: center; align-items: center;
}
.modal-overlay.open { display: flex; }
.modal-container {
    width: 90%; height: 90%; background: #fff; border-radius: 4px;
    display: flex; flex-direction: column; overflow: hidden;
    box-shadow: 0 10px 25px rgba(0,0,0,0.5);
}
.modal-header {
    background: #333; color: #fff; padding: 15px 20px;
    display: flex; justify-content: space-between; align-items: center;
}
.modal-header h2 { margin: 0; font-size: 18px; color: #fff; font-family: serif; }
.close-modal-btn { background: none; border: none; color: #fff; font-size: 28px; cursor: pointer; }

.modal-tabs { display: flex; background: #eee; border-bottom: 1px solid #ccc; }
.tab-btn {
    flex: 1; padding: 12px; border: none; background: #eee; cursor: pointer;
    font-weight: bold; color: #666; font-size: 14px; transition: 0.2s; font-family: sans-serif;
}
.tab-btn:hover { background: #e0e0e0; }
.tab-btn.active { background: #fff; color: #333; border-top: 3px solid var(--accent-color); }

.modal-body { flex-grow: 1; overflow-y: auto; padding: 20px; background: #fff; }
.tab-content { display: none; }
.tab-content.active { display: block; }

/* ランキンググリッド */
.ranking-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(300px, 1fr)); gap: 20px; }
.ranking-card { border: 1px solid #ddd; border-radius: 4px; overflow: hidden; }
.ranking-card-header { background: #333; color: #fff; padding: 10px; font-weight: bold; text-align: center; }
.ranking-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.ranking-table th { background: #f9f9f9; text-align: left; padding: 6px; }
.ranking-table td { border-bottom: 1px solid #eee; padding: 6px; }
.rank-num { width: 30px; text-align: center; font-weight: bold; color: #888; }
.rank-1 { color: #d4af37; font-size: 14px; }

/* グラフエリア */
.chart-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.chart-card {
    background: #fff; border: 1px solid #ddd; border-radius: 4px; padding: 15px;
    height: 350px; display: flex; flex-direction: column;
}
.chart-card h3 { margin: 0 0 10px; font-size: 14px; color: #333; text-align: center; }
.chart-card canvas { flex-grow: 1; }

/* レスポンシブ */
@media (max-width: 800px) {
    #details-sidebar { width: 100%; right: -100%; }
    .chart-grid { grid-template-columns: 1fr; }
}
@media (max-width: 600px) {
    .header-center { display: none; } /* スマホで検索窓を隠す（必要ならメニュー内へ） */
    .header-right .legend { display: none; }
    :root { --col-width: 70px; --row-headers-width: 40px; }
    .row-headers { width: 40px; }
    .kanji-char { font-size: 20px; }
}