/* ============================================================
   関連図みほん帳 第1版の見た目（docs/design/関連図みほん帳_デザイン.md に合わせている）
   ・スマホ（横 360〜430px）で崩れないようにしている
   ・14px より小さい文字は使わない
   ============================================================ */

/* ---------- 色の決まり（ライト） ---------- */
:root {
  --bg: #F5F7FA;          /* 背景 */
  --surface: #FFFFFF;     /* バー・カード・説明欄 */
  --surface2: #EEF1F5;    /* 凡例の帯・押した瞬間 */
  --graph-bg: #FAFBFC;    /* 図の背景 */
  --text: #1F2328;        /* 文字 */
  --sub: #4B5563;         /* 補足の文字 */
  --line: #C9D1DA;        /* 区切り線 */
  --primary: #1F5FA8;     /* 主ボタン */
  --on-primary: #FFFFFF;  /* 主ボタンの文字 */
  --disabled-bg: #C9D1DA; --disabled-text: #4B5563;
  --choice-bg: #FFFFFF; --choice-border: #8C96A3; --choice-text: #1F2328;
  --notice-bg: #FFF4D6; --notice-border: #B7791F; --notice-text: #1F2328;
  --focus: #F59E0B;       /* キーボード操作のときの枠・タップした箱の光の輪 */
  --edge: #4B5563;        /* 矢印 */
  /* 箱の種類ごとの枠の色 */
  --k-center: #243B53;
  --k-cause: #2F6DB5;
  --k-patho: #2E7D4F;
  --k-symptom: #8A5A00;
  --k-test: #5B6573;
  --k-treatment: #0E7490;
  --k-complication: #B42318;
  /* 追加された部分 */
  --history-fill: #FFE6CC; --history-border: #C2410C;
  --aging-fill: #EEE4FA;   --aging-border: #6D3FB0;
}

/* ---------- 色の決まり（ダークモード：スマホの設定に合わせて自動） ---------- */
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #121417;
    --surface: #1C1F24;
    --surface2: #262A31;
    --graph-bg: #16191D;
    --text: #E8EAED;
    --sub: #A8B0BA;
    --line: #3A404A;
    --primary: #8AB8F0;
    --on-primary: #0B1B2E;
    --disabled-bg: #2E333B; --disabled-text: #8A929C;
    --choice-bg: #1C1F24; --choice-border: #6B7380; --choice-text: #E8EAED;
    --notice-bg: #3A2E12; --notice-border: #E0B050; --notice-text: #F3F4F6;
    --focus: #FBBF24;
    --edge: #9AA4B2;
    --k-center: #D6E4F5;
    --k-cause: #7FB0EE;
    --k-patho: #6CC799;
    --k-symptom: #E0B050;
    --k-test: #A9B4C2;
    --k-treatment: #5CC8DE;
    --k-complication: #FF8A80;
    --history-fill: #4A2C12; --history-border: #FFA35C;
    --aging-fill: #35264F;   --aging-border: #C6A8F5;
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", sans-serif;
  font-size: 16px;
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
body { overflow: hidden; }

button, a { font-family: inherit; }
button { color: inherit; }
a { color: var(--primary); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }
h1:focus, h2:focus { outline: none; }

/* ---------- 画面（S1〜S4）：1つずつ表示する ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh; /* スマホのアドレスバーの分を考えた高さ */
  background: var(--bg);
}
/* 上の部分だけがスクロールし、下のボタンはいつも見えている */
.scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px;
  padding-top: calc(16px + env(safe-area-inset-top));
}
.bar + .scroll { padding-top: 16px; }
.foot {
  flex: none;
  padding: 8px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

/* ---------- 上のバー ---------- */
.bar {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 56px;
  padding: 4px 4px 4px 16px;
  padding-top: calc(4px + env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}
.bar h1 { flex: 1; margin: 0; font-size: 18px; line-height: 1.4; }
.bar .center-title { text-align: center; }
.bar-spacer { width: 72px; flex: none; }
.graph-bar { padding-left: 4px; }
.bar-title { flex: 1; min-width: 0; text-align: center; }
.bar-title h1 { font-size: 18px; }
.bar-title .sub { margin: 0; font-size: 14px; line-height: 1.4; color: var(--sub); }

.icon-btn, .back-btn, .text-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 48px;
  min-height: 48px;
  border: none;
  background: none;
  cursor: pointer;
  border-radius: 12px;
}
.icon-btn:active, .back-btn:active, .text-btn:active { background: var(--surface2); }
.icon-btn svg, .back-btn svg, .bottom svg, .close svg {
  width: 24px; height: 24px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.back-btn { padding: 0 8px 0 0; font-size: 16px; color: var(--primary); flex: none; }
.text-btn { padding: 0 12px; font-size: 16px; color: var(--primary); }
.text-btn.small { font-size: 16px; flex: none; }
.scroll > .text-btn { display: flex; margin: 0 auto; }

/* ---------- S1 最初の注意 ---------- */
/* 注意の中身は減らさずに、余白と行の間隔を詰めて、なるべく1画面に収める */
#s1 .scroll { padding-top: calc(8px + env(safe-area-inset-top)); padding-bottom: 4px; }
#s1 .brand { margin: 0 0 8px; }
#s1 .brand h1 { margin: 2px 0 2px; }
#s1 .brand p { line-height: 1.5; }
#s1 .brand .edition { font-size: 14px; line-height: 1.4; color: var(--sub); margin: 0 0 2px; }
#s1 .notice { padding: 8px 12px; margin-bottom: 8px; line-height: 1.5; }
#s1 .notice h2 { line-height: 1.4; }
#s1 .note { margin: 0; }
#s1 .scroll > .text-btn { min-height: 44px; }
.more-hint { margin: 0 0 6px; font-size: 14px; line-height: 1.4; text-align: center; color: var(--sub); }
.brand { text-align: center; margin: 8px 0 16px; }
.brand img { border-radius: 16px; }
.brand h1 { margin: 8px 0 4px; font-size: 24px; line-height: 1.4; }
.brand p { margin: 0; }

/* 注意の囲み（3つとも同じ形） */
.notice {
  display: flex;
  gap: 12px;
  align-items: flex-start;
  margin: 0 0 12px;
  padding: 12px;
  background: var(--notice-bg);
  border: 2px solid var(--notice-border);
  border-radius: 12px;
  color: var(--notice-text);
}
.notice h2 { margin: 0; font-size: 18px; line-height: 1.5; }
.notice p { margin: 0; }
.notice.band { padding: 8px 12px; }
.ico {
  flex: none;
  width: 24px; height: 24px; margin-top: 2px;
  fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round;
}
.note { margin: 4px 0 8px; font-size: 14px; line-height: 1.5; color: var(--sub); }
.small { font-size: 14px; line-height: 1.5; }
.sub-text { color: var(--sub); }

/* 主ボタン */
.primary {
  display: block;
  width: 100%;
  min-height: 52px;
  padding: 8px 16px;
  border: none;
  border-radius: 12px;
  background: var(--primary);
  color: var(--on-primary);
  font-size: 18px;
  font-weight: bold;
  line-height: 1.3;
  cursor: pointer;
}
.primary:disabled { background: var(--disabled-bg); color: var(--disabled-text); cursor: default; }

/* ---------- S2 条件を選ぶ ---------- */
.q { margin: 16px 0 8px; font-size: 18px; line-height: 1.5; }
.choices {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.choices button {
  min-height: 48px;
  padding: 4px;
  border: 2px solid var(--choice-border);
  border-radius: 12px;
  background: var(--choice-bg);
  color: var(--choice-text);
  font-size: 16px;
  line-height: 1.3;
  cursor: pointer;
}
.choices.big button { min-height: 64px; font-size: 18px; font-weight: bold; }
/* 選ばれている選択肢：塗りつぶし＋✓（色だけで区別しない） */
.choices button[aria-pressed="true"] {
  background: var(--primary);
  border-color: var(--primary);
  color: var(--on-primary);
}
.choices button[aria-pressed="true"]::before { content: "✓ "; }
.summary { margin: 0 0 8px; font-size: 16px; text-align: center; }
.load-error {
  margin: 0 0 12px;
  padding: 12px;
  border: 2px solid var(--k-complication);
  border-radius: 12px;
  background: var(--surface);
}

/* ---------- S3 凡例の帯 ---------- */
.legend {
  flex: none;
  display: block;
  width: 100%;
  text-align: left;
  background: var(--surface2);
  border: none;
  border-bottom: 1px solid var(--line);
  padding: 4px 16px;
  font-size: 14px;
  line-height: 1.5;
  cursor: pointer;
}
.legend-row {
  display: flex;
  flex-wrap: wrap;
  column-gap: 12px;
}
.legend-row:empty { display: none; }
.lg { display: inline-flex; align-items: center; gap: 4px; white-space: nowrap; }
.legend svg, .legend-list svg, .kind svg { flex: none; overflow: visible; }
.legend-note { display: block; color: var(--sub); }

/* 形のアイコン（色は種類ごと） */
.shape { fill: none; stroke: currentColor; stroke-width: 2; }
.k-center { color: var(--k-center); }
.k-cause { color: var(--k-cause); }
.k-patho { color: var(--k-patho); }
.k-symptom { color: var(--k-symptom); }
.k-test { color: var(--k-test); }
.k-treatment { color: var(--k-treatment); }
.k-complication { color: var(--k-complication); }
.lg-line { stroke: var(--edge); stroke-width: 2.5; }
.lg-line.dashed { stroke-dasharray: 8 5; }
.lg-box { fill: none; stroke: var(--sub); stroke-width: 2; }
.lg-box.dashed { stroke-dasharray: 5 3; }
.chip {
  display: inline-block;
  flex: none;
  width: 18px; height: 12px;
  border: 2px solid; border-radius: 2px;
}
.chip-history { background: var(--history-fill); border-color: var(--history-border); }
.chip-aging { background: var(--aging-fill); border-color: var(--aging-border); }

/* ---------- S3 図の部分 ---------- */
.graph-area {
  position: relative;
  flex: 1;
  min-height: 0;
  background: var(--graph-bg);
}
#cy {
  position: absolute;
  inset: 0;
  touch-action: none; /* 図の上での指の動きは、ページではなく図の拡大・移動に使う */
}
.hint {
  position: absolute;
  left: 16px;
  right: 16px;
  bottom: 12px;
  margin: 0 auto;
  max-width: 340px;
  padding: 8px 12px;
  font-size: 14px;
  line-height: 1.5;
  text-align: center;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 12px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .15);
  pointer-events: none; /* 図の操作のじゃまをしない */
  transition: opacity .4s;
}
.hint.hide { opacity: 0; }

/* ---------- S3 下のバー ---------- */
.bottom {
  flex: none;
  display: flex;
  background: var(--surface);
  border-top: 1px solid var(--line);
  padding-bottom: env(safe-area-inset-bottom);
}
.bottom button {
  flex: 1;
  min-height: 64px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  border: none;
  background: none;
  font-size: 14px;
  line-height: 1.5;
  cursor: pointer;
}
.bottom button:active { background: var(--surface2); }

/* ---------- S4 このアプリについて ---------- */
.about h2 { margin: 16px 0 4px; font-size: 18px; line-height: 1.5; }
.about h2:first-child { margin-top: 0; }
.about p { margin: 0 0 8px; }
.about summary { min-height: 48px; display: flex; align-items: center; color: var(--primary); cursor: pointer; }
.license {
  white-space: pre-wrap;
  font-size: 14px;
  line-height: 1.5;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px;
  overflow-x: auto;
}

/* ---------- 下から出る板（説明欄・凡例・出典） ---------- */
.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  height: 45vh;
  height: 45dvh;            /* 開いたときは画面の高さの45% */
  display: flex;
  flex-direction: column;
  background: var(--surface);
  color: var(--text);
  border-radius: 16px 16px 0 0;
  box-shadow: 0 -4px 16px rgba(0, 0, 0, .25);
  transform: translateY(105%);   /* ふだんは画面の下に隠しておく */
  visibility: hidden;
  transition: transform .25s ease, height .25s ease, visibility 0s linear .25s;
  z-index: 10;
}
.sheet.open { transform: translateY(0); visibility: visible; transition: transform .25s ease, height .25s ease; }
.sheet.tall { height: 85vh; height: 85dvh; }  /* つまみを上に引くと85%まで広がる */
#sheetSources, #sheetLegend { height: 70vh; height: 70dvh; }
#sheetSources.tall, #sheetLegend.tall { height: 85vh; height: 85dvh; }
.sheet.dragging { transition: none; }

/* つまみ（上下に引っぱって高さを変える） */
.grip {
  flex: none;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
  cursor: grab;
}
.grip span { width: 40px; height: 4px; border-radius: 2px; background: var(--line); }

.sheet-body {
  flex: 1;
  overflow-y: auto;          /* 長い説明は欄の中でスクロール */
  padding: 0 16px 16px;
  padding-bottom: calc(16px + env(safe-area-inset-bottom));
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}
.sheet-head h2 { margin: 0; font-size: 18px; line-height: 1.4; }
.kind {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 14px;
  font-weight: bold;
}
.kind .poss { font-weight: normal; color: var(--sub); }
.close {
  display: inline-flex;
  align-items: center;
  gap: 2px;
  flex: none;
  min-height: 48px;
  min-width: 48px;
  border: none;
  background: var(--surface2);
  border-radius: 12px;
  font-size: 14px;
  padding: 0 8px 0 12px;
  cursor: pointer;
}
#boxTitle { margin: 4px 0 8px; font-size: 20px; line-height: 1.4; }
.sheet h3 { margin: 16px 0 4px; font-size: 16px; line-height: 1.5; }
.sheet p { margin: 0 0 8px; font-size: 16px; line-height: 1.7; }
.origin-tag {
  display: inline-block;
  font-size: 14px !important;
  line-height: 1.5 !important;
  padding: 2px 8px;
  border: 2px solid;
  border-radius: 8px;
}
.origin-tag.history { background: var(--history-fill); border-color: var(--history-border); }
.origin-tag.aging { background: var(--aging-fill); border-color: var(--aging-border); }

/* つながり（押すとその箱へ移る） */
.links { list-style: none; margin: 0; padding: 0; }
.links button {
  width: 100%;
  min-height: 56px;
  display: flex;
  align-items: center;
  gap: 8px;
  text-align: left;
  border: 1px solid var(--line);
  background: var(--surface);
  border-radius: 12px;
  margin-bottom: 8px;
  padding: 8px 12px;
  cursor: pointer;
}
.links button:active { background: var(--surface2); }
.links .txt { flex: 1; min-width: 0; }
.links .main { display: block; font-size: 16px; font-weight: bold; line-height: 1.5; }
.links .why { display: block; font-size: 14px; line-height: 1.5; color: var(--sub); }
.links .mini-tag {
  display: inline-block;
  margin-left: 4px;
  padding: 0 6px;
  border: 1px solid;
  border-radius: 6px;
  font-size: 14px;
  font-weight: normal;
}
.links .mini-tag.history { background: var(--history-fill); border-color: var(--history-border); }
.links .mini-tag.aging { background: var(--aging-fill); border-color: var(--aging-border); }
.links .go { flex: none; font-size: 18px; color: var(--sub); }
.source { font-size: 14px; line-height: 1.5; color: var(--sub); margin-top: 8px; }
.source p { font-size: 14px !important; line-height: 1.5 !important; margin: 0 0 4px !important; }

/* 凡例くわしく */
.legend-list { list-style: none; margin: 0; padding: 0; }
.legend-list li { display: flex; align-items: center; gap: 12px; padding: 4px 0; line-height: 1.5; }
.legend-list.text li { display: list-item; list-style: disc; margin-left: 20px; padding: 2px 0; }
.legend-list .k-name { flex: none; width: 5.5em; font-weight: bold; }
#sheetLegend .notice { margin-top: 16px; }

/* 出典と確認日 */
.checked-date { font-weight: bold; margin-top: 8px !important; }
.src-group { margin: 12px 0 4px; font-size: 16px; }
.src-item { margin: 0 0 12px; font-size: 16px; line-height: 1.6; overflow-wrap: anywhere; }
.src-item .meta { display: block; font-size: 14px; line-height: 1.5; color: var(--sub); }
.src-item a { font-size: 14px; }

/* ---------- 横向きなど、画面の高さが低いとき ----------
   凡例の帯を1行にたたみ、上下のバーを細くして、図の部分を広くする（くわしい凡例は帯をタップ） */
.legend-compact { display: none; }
@media (max-height: 500px) {
  .legend-row, .legend-note { display: none; }
  .legend-compact {
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    color: var(--sub);
  }
  .legend { padding-top: 2px; padding-bottom: 2px; min-height: 0; }
  .bar { min-height: 48px; padding-top: calc(2px + env(safe-area-inset-top)); padding-bottom: 2px; }
  .graph-bar .bar-title { display: flex; align-items: baseline; justify-content: center; gap: 12px; }
  .bottom button { min-height: 48px; flex-direction: row; gap: 6px; }
  .bottom svg { width: 20px; height: 20px; }
  .hint { bottom: 8px; }
}

/* スマホの「視差効果を減らす」設定がオンなら、動きをなくす */
@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet.open, .hint { transition: none; }
}
