/* ── CSS Variables (Dark Mode default) ───── */
:root {
  --bg:          #0f1117;
  --surface:     #1a1d27;
  --card:        #252836;
  --card-hover:  #303448;
  --border:      #2a2d3a;
  --border-lite: #3a3d4a;
  --text:        #e0e0e0;
  --text-sub:    #aaa;
  --text-muted:  #666;
  --accent:      #5b7fff;
  --accent-dark: #4a6be0;

  --chart-bg:      #0f1117;
  --chart-stripe:  #13151e;
  --chart-grid:    #181b25;
  --chart-panel:   #0f1117;
  --chart-yr-text: #707080;
  --chart-ev-line: #c0392b;
  --chart-ev-text: #e05050;
  --chart-season-bg: #131520;
  --loan-dash:     rgba(255,220,50,0.8);
}

/* ── Light Mode ───────────────────────────── */
body.light {
  --bg:          #f4f6fb;
  --surface:     #ffffff;
  --card:        #eef0f7;
  --card-hover:  #e2e6f0;
  --border:      #d8dce8;
  --border-lite: #c8cdd8;
  --text:        #1a1d2e;
  --text-sub:    #555;
  --text-muted:  #999;
  --accent:      #4a6be0;
  --accent-dark: #3a5acc;

  --chart-bg:      #f4f6fb;
  --chart-stripe:  #eaecf4;
  --chart-grid:    #dde0ec;
  --chart-panel:   #f4f6fb;
  --chart-yr-text: #888;
  --chart-ev-line: #c0392b;
  --chart-ev-text: #c0392b;
  --chart-season-bg: #eef0f8;
  --loan-dash:     rgba(200,140,0,0.9);
}

/* ── Reset & Base ─────────────────────────── */
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: 'Segoe UI', 'Hiragino Sans', 'Noto Sans JP', sans-serif;
  background: var(--bg); color: var(--text);
  overflow: hidden; height: 100vh;
  transition: background 0.3s, color 0.3s;
}
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border-lite); border-radius: 3px; }

/* ── Layout ───────────────────────────────── */
#app { display: flex; height: 100vh; }

/* ── Sidebar ──────────────────────────────── */
#sidebar {
  width: 240px;
  background: var(--surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  flex-shrink: 0; overflow: hidden;
  transition: width 0.25s ease, background 0.3s;
  position: relative; z-index: 10;
}
#sidebar.collapsed { width: 0; border-right: none; }
#sidebar-inner { width: 240px; height: 100%; display: flex; flex-direction: column; }

#sidebar h1 {
  display: flex; align-items: center; justify-content: space-between;
  padding: 12px 8px 12px 14px;
  border-bottom: 1px solid var(--border);
}
.h1-text { font-size: 14px; color: var(--text); white-space: nowrap; line-height: 1.3; }
.h1-text span { font-size: 10px; color: var(--text-muted); display: block; margin-top: 2px; }

#toggle-btn {
  flex-shrink: 0; width: 26px; height: 26px;
  background: transparent; border: 1px solid var(--border-lite);
  border-radius: 6px; cursor: pointer; color: var(--text-muted); font-size: 12px;
  display: flex; align-items: center; justify-content: center;
  transition: background 0.15s; user-select: none;
}
#toggle-btn:hover { background: var(--card); color: var(--text); }

.era-list { flex: 1; overflow-y: auto; padding: 4px 6px; }
.era-item {
  padding: 6px 9px; margin: 1px 0; border-radius: 6px; cursor: pointer;
  transition: all 0.15s; border: 1px solid transparent;
  display: flex; align-items: center; gap: 8px;
}
.era-item:hover  { background: var(--card); }
.era-item.active { background: var(--card); border-color: var(--accent); }
.era-item .era-emblem {
  width: 24px; height: 24px; object-fit: contain; flex-shrink: 0;
}
.era-item .era-emblem.hidden { display: none; }
.era-item .era-name   { font-size: 12px; font-weight: 600; white-space: nowrap; }
.era-item .era-period { font-size: 10px; color: var(--text-muted); }

/* Filters */
.filter-section { padding: 6px 10px; border-top: 1px solid var(--border); }
.filter-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 4px; }
.filter-section h3 { font-size: 10px; color: var(--text-muted); }
.filter-btns { display: flex; gap: 4px; }
.filter-btn {
  font-size: 9px; padding: 2px 6px; border-radius: 4px; cursor: pointer;
  background: var(--card); color: var(--text-muted);
  border: 1px solid var(--border-lite); transition: all 0.12s;
}
.filter-btn:hover { background: var(--card-hover); color: var(--text); }
.filter-check {
  display: flex; align-items: center; gap: 5px;
  margin: 2px 0; font-size: 11px; cursor: pointer; color: var(--text-sub);
}
.filter-check input { accent-color: var(--accent); cursor: pointer; }
.filter-dot { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }

/* ── Expand button ────────────────────────── */
#expand-btn {
  flex-shrink: 0; width: 26px; height: 26px;
  background: transparent; border: 1px solid var(--border-lite);
  border-radius: 6px; cursor: pointer; color: var(--text-muted); font-size: 12px;
  align-items: center; justify-content: center;
  transition: background 0.15s; user-select: none; margin-right: 2px;
}
#expand-btn:hover { background: var(--card); color: var(--text); }

/* ── Main panel ───────────────────────────── */
#main { flex: 1; display: flex; flex-direction: column; overflow: hidden; min-width: 0; }

/* ── Toolbar ──────────────────────────────── */
#toolbar {
  display: flex; align-items: center; gap: 8px; padding: 6px 12px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  min-height: 44px; flex-shrink: 0; position: relative;
  transition: background 0.3s;
}
.t-title { font-size: 13px; font-weight: 700; margin-right: auto; white-space: nowrap; }

.legend { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; }
.legend-item { display: flex; align-items: center; gap: 3px; font-size: 10px; color: var(--text-sub); }
.legend-dot { width: 8px; height: 8px; border-radius: 2px; }

#zoom-info { font-size: 11px; color: var(--text-muted); min-width: 36px; text-align: right; }

.tbtn {
  padding: 3px 8px; background: var(--card); border: 1px solid var(--border-lite);
  color: var(--text-sub); border-radius: 5px; cursor: pointer; font-size: 11px;
  transition: all 0.12s; white-space: nowrap;
}
.tbtn:hover { background: var(--card-hover); color: var(--text); }
.tbtn-active { background: var(--accent) !important; color: #fff !important; border-color: var(--accent) !important; }

.seg-ctrl {
  display: flex; border: 1px solid var(--border-lite);
  border-radius: 6px; overflow: hidden; flex-shrink: 0;
}
.seg-btn {
  padding: 3px 9px; background: var(--card); border: none;
  border-right: 1px solid var(--border-lite);
  color: var(--text-muted); cursor: pointer; font-size: 11px;
  transition: all 0.12s; white-space: nowrap;
}
.seg-btn:last-child { border-right: none; }
.seg-btn:hover { background: var(--card-hover); color: var(--text); }
.seg-btn.seg-active { background: var(--accent); color: #fff; font-weight: 600; }

/* ── Hamburger ────────────────────────────── */
#hamburger-btn {
  flex-shrink: 0; width: 30px; height: 30px;
  background: transparent; border: 1px solid var(--border-lite);
  border-radius: 6px; cursor: pointer; color: var(--text-muted);
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 4px; transition: background 0.15s; user-select: none; margin-left: 2px;
}
#hamburger-btn:hover { background: var(--card); color: var(--text); }
#hamburger-btn span { display: block; width: 13px; height: 1.5px; background: currentColor; border-radius: 2px; }

#hamburger-menu {
  position: absolute; top: calc(100% + 6px); right: 12px; z-index: 100;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 0;
  box-shadow: 0 8px 32px rgba(0,0,0,0.25); min-width: 190px;
  transition: background 0.3s;
}
#hamburger-menu.hidden { display: none; }
.hmenu-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px; cursor: pointer; font-size: 12px; color: var(--text-sub);
  transition: background 0.12s; user-select: none;
}
.hmenu-item:hover { background: var(--card); color: var(--text); }
.hm-icon { font-size: 15px; width: 18px; text-align: center; }
.hm-label { flex: 1; }
.hm-badge {
  font-size: 10px; padding: 1px 6px; border-radius: 20px;
  background: var(--card); color: var(--text-muted); border: 1px solid var(--border-lite);
}
.hmenu-sep { height: 1px; background: var(--border); margin: 4px 0; }
.hmenu-item.hmenu-active { color: var(--accent); }
.hmenu-item.hmenu-active .hm-icon { opacity: 1; }
.theme-toggle {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 14px; cursor: pointer; font-size: 12px; color: var(--text-sub);
  transition: background 0.12s;
}
.theme-toggle:hover { background: var(--card); }
.theme-toggle .hm-label { flex: 1; }
.pill {
  width: 34px; height: 18px; border-radius: 9px;
  background: var(--border-lite); position: relative; transition: background 0.2s; flex-shrink: 0;
}
.pill::after {
  content: ''; position: absolute; top: 3px; left: 3px;
  width: 12px; height: 12px; border-radius: 50%;
  background: #fff; transition: left 0.2s;
}
body.light .pill { background: var(--accent); }
body.light .pill::after { left: 19px; }

/* ── Chart row (chart + age panel) ───────── */
#chart-row  { flex: 1; display: flex; overflow: hidden; }
#chart-wrap { flex: 1; position: relative; overflow: hidden; min-width: 0; }
#chart-wrap svg { display: block; }

/* ── Age panel ────────────────────────────── */
#age-panel {
  width: 0; flex-shrink: 0;
  background: var(--surface); border-left: 1px solid var(--border);
  overflow: hidden; transition: width 0.25s ease;
  display: flex; flex-direction: column;
}
#age-panel.open { width: 200px; }

#age-panel-head {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 10px 6px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#age-panel-year {
  font-size: 13px; font-weight: 700; color: var(--accent); white-space: nowrap;
}
#age-panel-close {
  background: transparent; border: none; color: var(--text-muted);
  cursor: pointer; font-size: 13px; padding: 2px 5px;
  border-radius: 4px; line-height: 1;
}
#age-panel-close:hover { background: var(--card); color: var(--text); }

#age-panel-subtitle {
  font-size: 10px; color: var(--text-muted);
  padding: 4px 10px; border-bottom: 1px solid var(--border);
  flex-shrink: 0;
}
#age-panel-list { flex: 1; overflow-y: auto; padding: 2px 0; }

.age-cat-head {
  font-size: 9px; color: var(--text-muted); font-weight: 600;
  padding: 6px 10px 2px; letter-spacing: 0.5px;
}
.age-item {
  display: flex; align-items: center; gap: 6px;
  padding: 4px 10px; font-size: 11px; cursor: default;
}
.age-item:hover { background: var(--card); }
.age-dot  { width: 8px; height: 8px; border-radius: 2px; flex-shrink: 0; }
.age-name {
  flex: 1; color: var(--text);
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.age-val  { color: var(--accent); font-weight: 700; font-size: 10px; }
.age-num  { color: var(--text-muted); font-weight: 400; font-size: 10px; }
.age-loan { font-size: 9px; color: var(--loan-dash); font-weight: 600; }
.age-age  { color: var(--text-muted); font-size: 10px; flex-shrink: 0; min-width: 20px; text-align: right; }

/* ── Tooltip ──────────────────────────────── */
#tooltip {
  position: fixed; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 9px 12px; font-size: 12px; max-width: 260px;
  box-shadow: 0 6px 20px rgba(0,0,0,0.22); z-index: 200;
  opacity: 0; transition: opacity 0.1s;
}
#tooltip.show { opacity: 1; }
.tt-name       { font-size: 13px; font-weight: 700; color: var(--text); }
.tt-nat        { font-size: 10px; color: var(--text-muted); margin-top: 1px; }
.tt-pos        { font-size: 10px; margin-top: 2px; }
.tt-period     { font-size: 11px; color: var(--text-sub); margin-top: 5px; }
.tt-number     { font-size: 11px; color: var(--accent); font-weight: 700; margin-top: 2px; }
.tt-loan       { font-size: 10px; color: var(--loan-dash); margin-top: 3px; font-weight: 600; }
.tt-loan-badge { display: inline-block; font-size: 9px; font-weight: 700; color: var(--loan-dash);
                 border: 1px solid var(--loan-dash); border-radius: 3px; padding: 0 3px; margin-left: 4px; vertical-align: middle; }
.tt-birth      { font-size: 10px; color: var(--text-muted); margin-top: 3px; }
.tt-loanout    { font-size: 10px; color: var(--text-muted); margin-top: 2px; font-style: italic; }

/* ── Mobile ───────────────────────────────── */
@media (max-width: 599px) {
  #zoom-info { display: none; }
  .seg-ctrl  { display: none; }
  .tbtn      { display: none; }
  #sidebar {
    position: absolute; z-index: 50; height: 100%;
    box-shadow: 4px 0 20px rgba(0,0,0,0.4);
  }
}
