/* ============================================================
   歯式チャート（FDI）— 再利用コンポーネント
   components/toothChart.js 専用のスタイル。

   他の歯科アプリへ移植できるよう、このファイルだけで完結させる。
   デザイントークンが無い環境でも壊れないよう、var() には既定値を書く。
   ============================================================ */

.tooth-chart {
  /* 1歯あたりのタップ領域。Apple HIGの最小44pxを下回らせない。 */
  --tooth-min: 2.75rem;
  --tooth-gap: 0.125rem;
  /* 歯列弓のふくらみの強さ。折り返し表示のときは0にして平坦にする。 */
  --tooth-curve-scale: 1;

  display: flex;
  flex-direction: column;
  /* 上顎と下顎の間は広めに取る。歯列弓のふくらみで両端が近づくため。 */
  gap: var(--space-3, 0.75rem);
}

/* ---------- 上顎・下顎の1列 ---------- */

/* ラベルは歯列の上に置く。横に並べると1歯あたりの幅をその分だけ失い、
   iPad横向きで44pxを確保できなくなるため。 */
.tooth-chart__arch {
  display: flex;
  flex-direction: column;
  gap: 0.125rem;
}

/* 下顎はラベルを歯列の下へ回し、上下の咬合面を中央で向かい合わせる */
.tooth-chart__arch--lower {
  flex-direction: column-reverse;
}

.tooth-chart__arch-label {
  font-size: var(--fs-caption, 0.875rem);
  font-weight: var(--fw-bold, 700);
  line-height: 1.2;
  color: var(--color-ink-muted, #5f6b7a);
}

/* 16本を等幅で並べる。歯科のチャートと同じく左右をつなげて見せる。 */
.tooth-chart__teeth {
  display: grid;
  grid-template-columns: repeat(16, minmax(0, 1fr));
  gap: var(--tooth-gap);
  min-width: 0;
}

/* 正中に細い線を引き、左右どちら側かを一目で分かるようにする。
   余白で表すと、その1本だけタップ領域が狭くなってしまうため線で示す。 */
.tooth-chart__teeth > .tooth:nth-child(8) {
  position: relative;
}

.tooth-chart__teeth > .tooth:nth-child(8)::after {
  content: "";
  position: absolute;
  inset-block: 0;
  inset-inline-end: calc(var(--tooth-gap) / -2);
  width: 1px;
  background: var(--color-border-strong, #c6d0dd);
}

/* ---------- 1本の歯 ---------- */

.tooth {
  display: flex;
  flex-direction: column;
  align-items: center;
  /* 番号は歯列の外側で揃え、歯形は咬合面側（中央寄り）で揃える。
     歯の大きさが種類ごとに違っても、番号の行と咬合面の線がまっすぐ通る。 */
  justify-content: space-between;
  gap: 0.125rem;
  min-height: var(--tooth-min);
  padding: 0.125rem 0;
  background: none;
  border: none;
  border-radius: var(--radius-sm, 0.5rem);
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  transform: translateY(calc(var(--tooth-curve, 0px) * var(--tooth-curve-scale)));
  transition: transform var(--duration-fast, 120ms) var(--ease-standard, ease);
}

/* 下顎は歯形を上下反転し、上顎と咬合面を向かい合わせる。
   番号は歯列の外側（下側）へ回す。 */
.tooth-chart__arch--lower .tooth {
  flex-direction: column-reverse;
  transform: translateY(calc(var(--tooth-curve, 0px) * var(--tooth-curve-scale) * -1));
}

.tooth-chart__arch--lower .tooth__shape {
  transform: scaleY(-1);
}

.tooth__no {
  font-size: var(--fs-caption, 0.8125rem);
  font-weight: var(--fw-bold, 700);
  font-variant-numeric: tabular-nums;
  line-height: 1;
  color: var(--color-ink-subtle, #7b8794);
  transition: color var(--duration-fast, 120ms) var(--ease-standard, ease);
}

/* 歯の大きさを種類で変える。奥ほど大きく前ほど小さくなり、
   番号を読まなくても歯列のどのあたりかが分かる。 */
.tooth__shape {
  display: block;
  width: 100%;
  height: auto;
}

.tooth--molar .tooth__shape {
  max-width: 2.3rem;
}

.tooth--premolar .tooth__shape {
  max-width: 1.9rem;
}

.tooth--incisor .tooth__shape {
  max-width: 1.45rem;
}

.tooth__body {
  fill: var(--color-surface, #fff);
  stroke: var(--color-ink-subtle, #7b8794);
  stroke-width: 1.5;
  transition: fill var(--duration-fast, 120ms) var(--ease-standard, ease),
    stroke var(--duration-fast, 120ms) var(--ease-standard, ease);
}

.tooth__groove {
  fill: none;
  stroke: var(--color-border-strong, #c6d0dd);
  stroke-width: 1.25;
  stroke-linecap: round;
  transition: stroke var(--duration-fast, 120ms) var(--ease-standard, ease);
}

/* ---------- 選択状態 ----------
   歯を塗りつぶし、番号も濃くする。色だけに頼らず、番号の太さでも差が出る。 */

.tooth[aria-pressed="true"] .tooth__body {
  fill: var(--color-primary, #1565c0);
  stroke: var(--color-primary-active, #0d47a1);
}

.tooth[aria-pressed="true"] .tooth__groove {
  stroke: var(--color-primary-soft, #e8f1fc);
}

.tooth[aria-pressed="true"] .tooth__no {
  color: var(--color-primary-active, #0d47a1);
}

.tooth:not([aria-pressed="true"]):active .tooth__body {
  fill: var(--color-surface-sunken, #f8fafc);
}

.tooth:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus, 0 0 0 3px rgba(21, 101, 192, 0.35));
}

/* ---------- まとめ選択 ---------- */

.tooth-chart__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2, 0.5rem);
}

.tooth-chart__action {
  min-height: var(--tap-comfortable, 3.5rem);
  padding: 0 var(--space-4, 1rem);
  font-family: inherit;
  font-size: var(--fs-label, 1rem);
  font-weight: var(--fw-bold, 700);
  color: var(--color-ink-strong, #0f2c4d);
  background: var(--color-surface, #fff);
  border: 1.5px solid var(--color-border-strong, #c6d0dd);
  border-radius: var(--radius-md, 0.75rem);
  cursor: pointer;
  touch-action: manipulation;
}

.tooth-chart__action:active {
  background: var(--color-surface-sunken, #f8fafc);
}

/* 打ち消し操作。他より控えめにしつつ、押せることは枠で示す。 */
.tooth-chart__action--subtle {
  color: var(--color-ink-muted, #5f6b7a);
  background: none;
  border-style: dashed;
}

.tooth-chart__action:focus-visible {
  outline: none;
  box-shadow: var(--shadow-focus, 0 0 0 3px rgba(21, 101, 192, 0.35));
}

/* ---------- 狭い幅（iPad縦など） ----------
   16本を1列に並べると1歯が44pxを下回るため、8本ずつ2段へ折り返す。
   横スクロールは発生させない。 */

@media (max-width: 52rem) {
  .tooth-chart {
    /* 2段に分かれると弧が意味を成さないため平坦にする */
    --tooth-curve-scale: 0;
    --tooth-gap: 0.25rem;
  }

  .tooth-chart__teeth {
    grid-template-columns: repeat(8, minmax(0, 1fr));
  }

  /* 折り返すと8本目が行末になるため、正中の線は不要 */
  .tooth-chart__teeth > .tooth:nth-child(8)::after {
    content: none;
  }
}
