/* ==================================================================
 * 年齢と暦 / Age & Calendar — スタイル
 * ------------------------------------------------------------------
 * デザイン方針: 静か・知的・水彩・余白重視・中性的・実用品。
 *  - 単一の白いコンテナ(.tool)。区切りは細いヘアラインのみ。
 *  - ヘルプ／ツールチップは世界観に合わせた「吹き出し」に統一（絶対配置）。
 *  - 年齢入力は3桁が収まる幅。一覧は干支12行が見える高さ。
 * ================================================================== */

:root {
  font-family:
    "Noto Sans JP",
    "Hiragino Sans",
    "Yu Gothic",
    "Meiryo",
    system-ui,
    -apple-system,
    BlinkMacSystemFont,
    "Segoe UI",
    sans-serif;

  color: #2a3140;
  background-color: #f3f6fb;
  font-synthesis: none;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;

  --background: #f3f6fb;
  --surface: #ffffff;
  --surface-soft: #eef2f8;
  --surface-strong: #e4eaf3;
  --text: #2a3140;
  --text-muted: #5f6875;
  --text-faint: #78818f;
  --border: #e2e8f1;
  --border-strong: #b9c2d2;
  --hairline: #edf1f7;

  --accent: #5b7aa6;
  --accent-strong: #3f5c86;
  --accent-soft: #e9f0f9;
  --accent-tint: #f2f7fc;

  --focus: #3f5c86;
  --radius: 16px;
  --radius-sm: 11px;

  --shadow: 0 1px 2px rgb(38 50 74 / 4%), 0 16px 40px -20px rgb(38 50 74 / 16%);
  --bubble-shadow: 0 8px 22px -8px rgb(38 50 74 / 30%);
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  min-width: 320px;
  background: var(--background);
  color: var(--text);
}

body {
  min-width: 320px;
  min-height: 100vh;
  margin: 0;
  background:
    radial-gradient(
      40rem 26rem at 12% -6%,
      rgb(198 216 240 / 50%) 0,
      transparent 60%
    ),
    radial-gradient(
      34rem 24rem at 92% 2%,
      rgb(214 210 236 / 42%) 0,
      transparent 62%
    ),
    radial-gradient(
      46rem 30rem at 50% 108%,
      rgb(204 226 228 / 38%) 0,
      transparent 60%
    ),
    var(--background);
  background-attachment: fixed;
}

button,
input,
select {
  font: inherit;
}

button {
  color: inherit;
}

button,
input,
select,
.table-wrap {
  outline: none;
}

button:focus-visible,
input:focus-visible,
select:focus-visible,
a:focus-visible,
.table-wrap:focus-visible,
th[data-sort-key]:focus-visible {
  outline: 2.5px solid var(--focus);
  outline-offset: 2px;
}

/* スキップリンク */
.skip-link {
  position: absolute;
  left: 12px;
  top: -48px;
  z-index: 20;
  padding: 8px 14px;
  border-radius: 8px;
  background-color: var(--accent-strong);
  color: #ffffff;
  font-size: 0.85rem;
  text-decoration: none;
  transition: top 0.15s ease;
}

.skip-link:focus {
  top: 12px;
}

/* ---- レイアウト全体 ---- */
.app {
  position: relative;
  width: min(calc(100% - 40px), 720px);
  margin-inline: auto;
  padding-block: 28px 46px;
}

.app__header {
  margin-bottom: 18px;
  padding-top: 4px;
  text-align: center;
}

.app__topbar {
  position: absolute;
  top: 24px;
  right: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 8px;
}

.language-field {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.language-field label {
  color: var(--text-faint);
  font-size: 0.68rem;
  font-weight: 600;
}

.language-field select {
  min-height: 30px;
  padding: 3px 24px 3px 9px;
  border: 1px solid var(--border);
  border-radius: 7px;
  background-color: rgb(255 255 255 / 70%);
  color: var(--text-muted);
  font-size: 0.78rem;
  cursor: pointer;
}

.app-help-wrap {
  position: relative;
}

.app-help {
  position: absolute;
  top: calc(100% + 8px);
  right: 0;
  z-index: 9;
  display: none;
  width: max-content;
  max-width: min(86vw, 40ch);
  margin: 0;
  padding: 10px 12px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background-color: var(--surface);
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.6;
  text-align: left;
  box-shadow: var(--bubble-shadow);
}

.app-help.is-open {
  display: block;
}

.app-help::before {
  content: "";
  position: absolute;
  top: -6px;
  right: 8px;
  width: 11px;
  height: 11px;
  background-color: inherit;
  border-left: 1px solid var(--border);
  border-top: 1px solid var(--border);
  transform: rotate(45deg);
}

.app__eyebrow {
  margin: 0 0 6px;
  color: var(--accent);
  font-size: 0.68rem;
  font-weight: 700;
  letter-spacing: 0.22em;
}

.app__title {
  margin: 0;
  font-size: clamp(1.8rem, 3.4vw, 2.4rem);
  line-height: 1.15;
  letter-spacing: 0.02em;
}

/* ==================================================================
 * .tool
 * ================================================================== */
.tool {
  padding: 26px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background-color: rgb(255 255 255 / 93%);
  backdrop-filter: blur(2px);
  box-shadow: var(--shadow);
}

.tool:focus-visible {
  outline: none;
}

.tool__block + .tool__block {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--hairline);
}

.tool__block + .tool__block--table {
  margin-top: 14px;
  padding-top: 0;
  border-top: 0;
}

/* ---- 丸いヘルプボタン ---- */
.label-with-help {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.help-toggle {
  width: 18px;
  height: 18px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border-strong);
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  font-size: 0.7rem;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  padding: 0;
  transition:
    border-color 0.15s ease,
    color 0.15s ease,
    background-color 0.15s ease;
}

.help-toggle:hover,
.help-toggle[aria-expanded="true"] {
  border-color: var(--accent);
  color: var(--accent);
  background-color: var(--accent-tint);
}

/* ---- 入力欄 ---- */
.date-fields {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  gap: 14px;
  align-items: start;
}

.form-field {
  display: grid;
  gap: 5px;
  min-width: 0;
}

.form-field--anchor {
  position: relative;
}

.form-field label {
  font-size: 0.82rem;
  font-weight: 700;
  color: var(--text-muted);
}

.form-field__label-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 12px;
}

input,
select {
  width: 100%;
  min-height: 40px;
  border: 1px solid #cbd3e0;
  border-radius: var(--radius-sm);
  padding: 7px 11px;
  background-color: var(--surface);
  color: var(--text);
  transition:
    border-color 0.15s ease,
    box-shadow 0.15s ease;
}

input::placeholder {
  color: #8b93a2;
}

input:hover,
select:hover {
  border-color: #9aa4b6;
}

input:focus,
select:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgb(91 122 166 / 15%);
}

/* ---- 共通の吹き出し ---- */
.reference-hint,
.table-note,
.control-tip {
  position: absolute;
  z-index: 7;
  display: none;
  margin: 0;
  padding: 8px 11px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background-color: var(--surface);
  color: var(--text-muted);
  font-size: 0.76rem;
  line-height: 1.55;
  box-shadow: var(--bubble-shadow);
}

.reference-hint::before,
.table-note::before,
.control-tip::before {
  content: "";
  position: absolute;
  top: -6px;
  left: 14px;
  width: 11px;
  height: 11px;
  background-color: inherit;
  border-left: 1px solid var(--border);
  border-top: 1px solid var(--border);
  transform: rotate(45deg);
}

.reference-hint {
  top: calc(100% + 6px);
  left: 0;
  max-width: min(90vw, 34ch);
}

.reference-hint.is-open {
  display: block;
}

.table-note {
  top: calc(100% + 6px);
  left: 0;
  z-index: 6;
  max-width: min(90vw, 40ch);
  font-size: 0.77rem;
  line-height: 1.6;
}

.table-note.is-open {
  display: block;
}

.field-tip {
  position: relative;
  display: inline-flex;
  align-items: center;
}

.control-tip {
  top: calc(100% + 6px);
  left: 0;
  z-index: 8;
  max-width: min(90vw, 34ch);
  width: max-content;
}

.control-tip--right {
  left: auto;
  right: 0;
}

.control-tip--right::before {
  left: auto;
  right: 12px;
}

.field-tip:hover > .control-tip,
.field-tip:focus-within > .control-tip {
  display: block;
}

.text-button {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--accent);
  font-size: 0.78rem;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

.text-button:hover {
  color: var(--accent-strong);
}

/* ---- 結果 ---- */
.result[hidden] {
  display: none;
}

.result__age {
  margin: 0;
  font-size: clamp(1.9rem, 5vw, 2.6rem);
  font-weight: 800;
  letter-spacing: 0.01em;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
}

.result__facts {
  margin: 8px 0 0;
  color: var(--text);
  font-size: clamp(1rem, 2.2vw, 1.2rem);
  font-weight: 600;
  line-height: 1.6;
  overflow-wrap: anywhere;
}

.result__meta {
  margin: 8px 0 0;
  color: var(--text-muted);
  font-size: 0.82rem;
  line-height: 1.6;
}

.lookup-message {
  margin: 0;
  padding: 13px 16px;
  border-radius: var(--radius-sm);
  background-color: var(--surface-soft);
  color: var(--text-muted);
  line-height: 1.6;
}

.lookup-message[hidden] {
  display: none;
}

.lookup-message--error {
  border: 1px solid #b5726c;
  background-color: #fbf1f0;
  color: #7c322b;
}

/* ---- 年齢から探す（検索欄・全幅） ---- */
.finder__search {
  display: block;
  width: 100%;
}

.finder__search input {
  width: 100%;
}

/* ---- 早見表セクション ---- */
.table-section__head {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  margin-bottom: 10px;
}

.table-section__title {
  margin: 0;
  font-size: 0.98rem;
  font-weight: 700;
  color: var(--text);
}

.table-section__heading-group {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

/* 開始年齢 〜 最高年齢（手入力）＋ 印刷 */
.range-controls {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.range-controls__sep {
  color: var(--text-faint);
  font-size: 0.8rem;
}

.range-field {
  display: inline-flex;
  align-items: center;
  gap: 3px;
}

/* 数値入力: 3桁（+スピナー）が収まる幅。印刷ボタンと高さ・丸みを揃える */
.range-input {
  width: 5.2em;
  min-height: 34px;
  padding: 5px 4px 5px 12px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  background-color: var(--surface);
  color: var(--text);
  font-size: 0.82rem;
  font-weight: 600;
  text-align: center;
  font-variant-numeric: tabular-nums;
}

.range-input:hover {
  border-color: var(--accent);
}

.range-unit {
  color: var(--text-faint);
  font-size: 0.78rem;
}

.ghost-button {
  min-height: 34px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 6px 14px;
  background-color: transparent;
  color: var(--text-muted);
  font-size: 0.8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition:
    border-color 0.15s ease,
    color 0.15s ease,
    background-color 0.15s ease;
}

.ghost-button:hover {
  border-color: var(--accent);
  color: var(--accent-strong);
  background-color: var(--accent-tint);
}

.filter-hint {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 0 0 10px;
  padding: 9px 12px;
  border-left: 3px solid var(--accent);
  border-radius: 8px;
  background-color: var(--accent-tint);
  color: var(--text-muted);
  font-size: 0.8rem;
  line-height: 1.5;
}

.filter-hint[hidden] {
  display: none;
}

.filter-hint__clear {
  border: 0;
  padding: 0;
  background: transparent;
  color: var(--accent-strong);
  font-weight: 700;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* 一覧: 干支12行が見える高さ。行は低めに揃える。 */
.table-wrap {
  overflow: auto;
  max-height: 30rem;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background-color: var(--surface);
  scrollbar-gutter: stable;
}

table {
  width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
}

th,
td {
  padding: 7px 12px;
  text-align: left;
  white-space: nowrap;
  font-size: 0.9rem;
  line-height: 1.4;
}

td {
  border-bottom: 1px solid var(--hairline);
}

thead th {
  position: sticky;
  top: 0;
  z-index: 3;
  background-color: var(--surface-strong);
  color: #454e5c;
  font-size: 0.76rem;
  letter-spacing: 0.03em;
  box-shadow: inset 0 -1px 0 var(--border);
}

th[data-sort-key] {
  cursor: pointer;
  user-select: none;
}

th[data-sort-key]:hover {
  color: var(--accent-strong);
}

th[data-sort-key]::after {
  content: " ⇅";
  font-size: 0.72em;
  opacity: 0.32;
}

th[data-sort-key][aria-sort="ascending"]::after {
  content: " ▲";
  opacity: 0.9;
  color: var(--accent);
}

th[data-sort-key][aria-sort="descending"]::after {
  content: " ▼";
  opacity: 0.9;
  color: var(--accent);
}

/* 一致マーカー列（見出しは視覚的に非表示・最小限の幅） */
.col-marker,
.cell-marker {
  width: 20px;
  min-width: 20px;
  padding-left: 6px;
  padding-right: 2px;
  text-align: center;
}

.cell-marker {
  color: var(--accent);
  font-size: 0.7rem;
  line-height: 1;
}

/* うるう年マーク（控えめ・小さめ・上付き） */
.leap-mark {
  margin-left: 4px;
  padding: 0 4px;
  border-radius: 6px;
  background-color: var(--accent-tint);
  color: var(--accent-strong);
  font-size: 0.62rem;
  font-weight: 700;
  vertical-align: 1px;
}

tbody tr {
  cursor: pointer;
}

td:nth-child(2) {
  font-weight: 700;
}

td:nth-child(3) {
  font-weight: 650;
}

tbody tr:hover {
  background-color: var(--surface-soft);
}

tbody tr:last-child td {
  border-bottom: 0;
}

tbody tr.is-match {
  background-color: var(--accent-soft);
  box-shadow: inset 3px 0 0 var(--accent);
}

tbody tr.is-match:hover {
  background-color: #dfeaf7;
}

tbody tr.is-match td:nth-child(2) {
  color: var(--accent-strong);
}

tbody tr.is-query-match:not(.is-match) {
  background-color: var(--accent-tint);
  box-shadow: inset 3px 0 0 var(--border-strong);
}

.empty-state {
  margin: 12px 0 0;
  padding: 24px 20px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background-color: var(--surface);
  color: var(--text-muted);
  text-align: center;
}

.empty-state[hidden] {
  display: none;
}

.app__footer {
  padding-top: 22px;
  color: var(--text-faint);
  font-size: 0.75rem;
  line-height: 1.7;
  text-align: center;
}

.app__footer p {
  margin: 4px 0;
}

.noscript-message {
  margin: 16px;
  padding: 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  background-color: var(--surface-soft);
  color: var(--text-muted);
  line-height: 1.7;
  text-align: center;
}

.visually-hidden {
  position: absolute;
  overflow: hidden;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  border: 0;
  clip-path: inset(50%);
  white-space: nowrap;
}

[hidden] {
  display: none !important;
}

/* ==================================================================
 * スマートフォン
 * ================================================================== */
@media (max-width: 620px) {
  .app {
    width: min(calc(100% - 24px), 720px);
    padding-block: 22px 30px;
  }

  .app__topbar {
    position: static;
    justify-content: flex-end;
    margin-bottom: 8px;
  }

  .app__header {
    padding-top: 0;
  }

  .app-help {
    right: 0;
    max-width: min(84vw, 40ch);
  }

  .tool {
    padding: 18px;
  }

  .date-fields {
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    gap: 10px;
  }

  input,
  select {
    padding: 7px 8px;
  }

  .form-field label {
    font-size: 0.78rem;
  }

  .table-section__head {
    flex-wrap: wrap;
  }

  .range-controls {
    width: 100%;
    justify-content: flex-start;
    gap: 6px;
  }

  .range-input {
    min-height: 32px;
    width: 4.6em;
  }

  .ghost-button {
    min-height: 32px;
    margin-left: auto;
  }

  th,
  td {
    padding: 7px 9px;
    font-size: 0.84rem;
  }

  .col-marker,
  .cell-marker {
    width: 18px;
    min-width: 18px;
    padding-left: 5px;
    padding-right: 1px;
  }

  th:nth-child(4),
  td:nth-child(4),
  th:nth-child(5),
  td:nth-child(5) {
    white-space: normal;
    word-break: break-word;
  }
}

/* ---- モーション配慮 ---- */
@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    transition-duration: 0.01ms !important;
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
  }
}

/* ---- ハイコントラスト設定への配慮 ---- */
@media (prefers-contrast: more) {
  :root {
    --text-muted: #3c4453;
    --text-faint: #4b5563;
    --border: #aab4c4;
    --hairline: #c9d2df;
  }
}

/* ---- 印刷 ---- */
@media print {
  :root {
    --background: #ffffff;
    --surface: #ffffff;
    --surface-soft: #ffffff;
    --surface-strong: #eeeeee;
    --text: #000000;
    --text-muted: #333333;
    --text-faint: #444444;
    --border: #bdbdbd;
    --hairline: #d8d8d8;
    --shadow: none;
    --bubble-shadow: none;
  }

  body {
    background: #ffffff;
  }

  .app {
    width: 100%;
    padding: 0;
  }

  .skip-link,
  .app__topbar,
  .date-fields,
  .finder,
  .text-button,
  .ghost-button,
  .range-controls,
  .help-toggle,
  .lookup-message,
  .filter-hint,
  .reference-hint,
  .control-tip {
    display: none !important;
  }

  .table-note {
    position: static;
    display: block !important;
    max-width: none;
    border: 0;
    box-shadow: none;
    padding: 0 0 8px;
    color: #333333;
  }

  .table-note::before {
    display: none;
  }

  .tool {
    padding: 0;
    border: 0;
    box-shadow: none;
    background: #ffffff;
  }

  .tool__block + .tool__block {
    margin-top: 14px;
    padding-top: 14px;
    border-top: 1px solid #d8d8d8;
  }

  .table-wrap {
    overflow: visible;
    max-height: none;
    border-radius: 0;
  }

  .leap-mark {
    background: transparent;
    color: #333333;
  }

  tbody tr.is-match {
    box-shadow: none;
    background: #eef2f8 !important;
  }

  thead th {
    position: static;
  }

  tr {
    break-inside: avoid;
  }
}
