/*
 * 三目並べ（claude_direct_haiku-5.5）。このページでしか読み込まれない。クラスは ttt- で統一。
 *
 * 3つの見せ方（上から、後のものが優先）:
 *  - 基本（幅 601px〜900px、または高さ 501px 未満の広い画面）: 見出し・設定・盤を縦に並べる。
 *  - 広い画面（幅 901px 以上・高さ 501px 以上。PC）: 盤を左、設定と状態を右に並べる。盤は画面の高さに収まる。
 *  - スマホなど（幅 600px 以下、または高さ 500px 以下）: 1画面に収める。状態ごとに見せるものを切り替える。
 *    使える高さは共通の枠の --yz-stage-h（画面の高さ − 上下のバー）。盤は幅と高さの小さいほうに合わせる。
 * 操作（ボタン・キー・戦績・遊び方）は、どの見せ方でも同じ。
 */

body:has(.ttt) {
  background:
    radial-gradient(circle at 18% 0%, #e7e0ff 0%, rgba(231, 224, 255, 0) 42%),
    linear-gradient(180deg, #f3f0ff 0%, #f8f7fc 60%);
}
body:has(.ttt) > header a {
  color: #4c2fa8;
}
body:has(.ttt) main {
  background: #ffffff;
  box-shadow: 0 14px 40px -18px rgba(76, 47, 168, 0.25);
}

.ttt {
  --ttt-accent: #6d4fd6;
  --ttt-accent-dark: #4c2fa8;
  --ttt-ink: #202735;
  --ttt-muted: #5f6878;
  --ttt-line: #e4defa;
  --ttt-o: #2563eb;
  --ttt-x: #e11d48;
  --ttt-win: #fde68a;
  max-width: 560px;
  margin: 0 auto;
  color: var(--ttt-ink);
}

/* ---- 見出し ---- */
.ttt-head {
  text-align: center;
}
.ttt-eyebrow {
  margin: 0;
  font-size: 0.72rem;
  letter-spacing: 0.18em;
  color: var(--ttt-accent);
  font-weight: 700;
}
.ttt h1 {
  margin: 4px 0 0;
  font-size: 1.8rem;
  letter-spacing: 0.02em;
}
.ttt h1 span {
  display: block;
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.28em;
  color: var(--ttt-muted);
}
.ttt-tag {
  margin: 6px 0 0;
  color: var(--ttt-muted);
  font-size: 0.95rem;
}

/* ---- 設定（強さ・手番） ---- */
.ttt-side {
  display: contents;
}
.ttt-setup {
  display: grid;
  gap: 14px;
  margin: 20px 0 16px;
}
.ttt-field {
  padding: 12px;
  border: 1px solid var(--ttt-line);
  border-radius: 16px;
  background: #fcfbff;
  box-shadow: 0 1px 0 rgba(76, 47, 168, 0.04);
}
.ttt-label {
  margin: 0 0 8px;
  font-size: 0.85rem;
  font-weight: 700;
  color: var(--ttt-muted);
}
.ttt-choices {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}
.ttt-field:has(> .ttt-choices [data-side]) .ttt-choices {
  grid-template-columns: repeat(2, 1fr);
}
.ttt-hint {
  margin: 8px 0 0;
  min-height: 1.4em;
  font-size: 0.85rem;
  color: var(--ttt-muted);
}

/* ---- ボタン（共通の見た目と、押したときの動き） ---- */
.ttt-choice,
.ttt-primary,
.ttt-secondary,
.ttt-reset,
.ttt-close,
.ttt-tool {
  min-height: 44px;
  padding: 8px 12px;
  border: 2px solid var(--ttt-accent);
  border-radius: 12px;
  background: #ffffff;
  color: var(--ttt-accent-dark);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  transition: background-color 0.15s ease, color 0.15s ease, transform 0.1s ease, box-shadow 0.15s ease, filter 0.15s ease;
}
.ttt-choice:hover:not(:disabled),
.ttt-tool:hover,
.ttt-secondary:hover,
.ttt-reset:hover,
.ttt-close:hover {
  background: #f4f1ff;
}
.ttt-choice:active:not(:disabled),
.ttt-tool:active,
.ttt-secondary:active,
.ttt-primary:active:not(:disabled),
.ttt-reset:active,
.ttt-close:active {
  transform: scale(0.97);
}
.ttt-choice:focus-visible,
.ttt-tool:focus-visible,
.ttt-primary:focus-visible,
.ttt-secondary:focus-visible,
.ttt-reset:focus-visible,
.ttt-close:focus-visible {
  outline: 3px solid rgba(109, 79, 214, 0.55);
  outline-offset: 2px;
}
.ttt-choice[aria-pressed="true"] {
  background: var(--ttt-accent);
  color: #ffffff;
  box-shadow: 0 6px 14px -6px rgba(109, 79, 214, 0.7);
}
.ttt-choice:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}
.ttt-primary {
  min-width: 128px;
  border-color: transparent;
  background: linear-gradient(135deg, #7c5ce6 0%, #5a3fd0 100%);
  color: #ffffff;
  box-shadow: 0 10px 18px -10px rgba(90, 63, 208, 0.9);
}
.ttt-primary:hover:not(:disabled) {
  filter: brightness(1.06);
  transform: translateY(-1px);
}
.ttt-primary:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  box-shadow: none;
}
.ttt-tool {
  min-width: 112px;
  background: #ffffff;
  font-size: 0.95rem;
}
/* 「強さ・手番を変える」は、結果の画面（スマホなど）でだけ出す */
.ttt-secondary {
  display: none;
}

/* ---- 状態（結果・手番）と、開始ボタンの行 ---- */
.ttt-toolbar {
  display: flex;
  justify-content: center;
  gap: 8px;
  margin: 0 0 12px;
}
.ttt-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 14px;
}
.ttt-status {
  flex: 1 1 220px;
  margin: 0;
  padding: 10px 14px;
  min-height: 1.6em;
  border-radius: 12px;
  background: #f6f4ff;
  font-weight: 700;
  line-height: 1.5;
}
.ttt-status[data-result="win"] {
  color: #15803d;
  background: #ecfdf3;
}
.ttt-status[data-result="lose"] {
  color: var(--ttt-x);
  background: #fff1f3;
}
.ttt-status[data-result="draw"] {
  color: #b45309;
  background: #fff8eb;
}
.ttt-actions {
  display: flex;
  gap: 8px;
}

/* ---- 盤面（台のような見た目。盤の上の指の操作で、ページが動かないようにする） ---- */
.ttt-board {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  width: min(100%, 400px);
  aspect-ratio: 1;
  margin: 0 auto;
  padding: 10px;
  border-radius: 22px;
  background: linear-gradient(145deg, #ece7ff 0%, #dcd3fb 100%);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.7),
    0 22px 40px -22px rgba(76, 47, 168, 0.55);
  font-size: clamp(1.8rem, 9vw, 3.2rem);
  touch-action: none;
}
.ttt-board.is-waiting {
  cursor: progress;
}
.ttt-cell {
  position: relative;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 44px;
  padding: 0;
  border: 0;
  border-radius: 14px;
  background: #ffffff;
  box-shadow: inset 0 -3px 0 rgba(76, 47, 168, 0.07);
  cursor: pointer;
  touch-action: none;
  transition: background-color 0.15s ease, transform 0.12s ease;
}
.ttt-cell[aria-disabled="true"] {
  cursor: default;
}
.ttt-cell.is-taken {
  cursor: not-allowed;
}
.ttt-cell:not(.is-taken):not([aria-disabled="true"]):hover {
  background: #f6f3ff;
}
.ttt-cell:not(.is-taken):not([aria-disabled="true"]):active {
  transform: scale(0.97);
}
.ttt-cell:focus-visible {
  outline: 3px solid var(--ttt-accent);
  outline-offset: 2px;
}
.ttt-cell.is-win {
  background: var(--ttt-win);
  box-shadow: inset 0 -3px 0 rgba(180, 83, 9, 0.15);
}

/* 置く前の予告: 乗せたマスに、自分の印をうっすら出す（マウスのみ。自分の番のときだけ） */
.ttt-board[data-ghost="O"] .ttt-cell:not(.is-taken):not([aria-disabled="true"]):hover::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1.4em;
  height: 1.4em;
  box-sizing: border-box;
  border: 0.15em solid rgba(37, 99, 235, 0.3);
  border-radius: 50%;
  transform: translate(-50%, -50%);
}
.ttt-board[data-ghost="X"] .ttt-cell:not(.is-taken):not([aria-disabled="true"]):hover::before,
.ttt-board[data-ghost="X"] .ttt-cell:not(.is-taken):not([aria-disabled="true"]):hover::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 1.4em;
  height: 0.16em;
  border-radius: 0.1em;
  background: rgba(225, 29, 72, 0.3);
  transform: translate(-50%, -50%) rotate(45deg);
}
.ttt-board[data-ghost="X"] .ttt-cell:not(.is-taken):not([aria-disabled="true"]):hover::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}

/* ○ と × は CSS で描く（画像・文字フォントに頼らない） */
.ttt-mark {
  position: relative;
  display: block;
  width: 1.4em;
  height: 1.4em;
  animation: ttt-pop 0.22s ease-out;
}
.ttt-mark-o {
  border: 0.15em solid var(--ttt-o);
  border-radius: 50%;
  box-sizing: border-box;
}
.ttt-mark-x::before,
.ttt-mark-x::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 100%;
  height: 0.16em;
  border-radius: 0.1em;
  background: var(--ttt-x);
  transform: translate(-50%, -50%) rotate(45deg);
}
.ttt-mark-x::after {
  transform: translate(-50%, -50%) rotate(-45deg);
}
@keyframes ttt-pop {
  from { transform: scale(0.4); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

/* ---- 戦績・遊び方（ダイアログ。ボタンで開く。中身が長いときは、その中だけスクロールする） ---- */
.ttt-modal {
  box-sizing: border-box;
  width: min(92vw, 440px);
  max-width: none;
  max-height: calc(100dvh - 32px);
  margin: auto;
  padding: 20px;
  border: 0;
  border-radius: 20px;
  background: #ffffff;
  color: var(--ttt-ink);
  box-shadow: 0 30px 60px -20px rgba(32, 39, 53, 0.35);
  overflow: auto;
  overscroll-behavior: contain;
  touch-action: pan-y;
}
.ttt-modal[open] {
  animation: ttt-modal-in 0.2s ease-out;
}
.ttt-modal::backdrop {
  background: rgba(20, 16, 40, 0.4);
  backdrop-filter: blur(3px);
}
@keyframes ttt-modal-in {
  from { opacity: 0; transform: translateY(10px) scale(0.98); }
  to { opacity: 1; transform: none; }
}
.ttt-modal-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 14px;
}
.ttt-modal-tools {
  display: flex;
  gap: 8px;
}
.ttt-modal h2 {
  margin: 0;
  font-size: 1.15rem;
}
.ttt-close {
  min-height: 40px;
  padding: 6px 14px;
  font-size: 0.9rem;
}
.ttt-reset {
  min-height: 40px;
  padding: 6px 12px;
  font-size: 0.9rem;
  border-color: #c4b8f0;
  color: var(--ttt-muted);
}
.ttt-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
}
.ttt-table th,
.ttt-table td {
  padding: 9px 6px;
  border-bottom: 1px solid var(--ttt-line);
  text-align: center;
}
.ttt-table thead th {
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  color: var(--ttt-muted);
}
.ttt-table tbody th {
  text-align: left;
}
.ttt-table tbody tr:nth-child(even) {
  background: #faf9ff;
}
.ttt-table td {
  font-weight: 700;
}
.ttt-note {
  margin: 10px 0 0;
  font-size: 0.85rem;
  color: #b45309;
}
.ttt-how dl {
  margin: 0;
}
.ttt-how dl > div {
  display: grid;
  grid-template-columns: 9em 1fr;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px dashed var(--ttt-line);
}
.ttt-how dt {
  font-weight: 700;
}
.ttt-how dd {
  margin: 0;
  color: var(--ttt-muted);
}

/* ---- 広い画面（PC）: 盤を左、設定と状態を右に。盤は画面の高さに収まる大きさ ---- */
@media (min-width: 901px) and (min-height: 621px), (min-width: 601px) and (min-height: 621px) and (orientation: landscape) {
  body:has(.ttt) main {
    max-width: 1120px;
    padding: 24px 32px;
  }
  .ttt {
    max-width: none;
    height: calc(var(--yz-stage-h) - 48px);
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(300px, 380px);
    grid-template-rows: auto minmax(0, 1fr);
    column-gap: 48px;
    align-items: center;
    --ttt-board: min(calc(var(--yz-stage-h) - 150px), 520px, calc(100vw - 500px));
  }
  .ttt-head {
    grid-column: 2;
    grid-row: 1;
    align-self: start;
    text-align: left;
  }
  .ttt-head h1 {
    font-size: 2.1rem;
  }
  .ttt-side {
    display: flex;
    flex-direction: column;
    gap: 14px;
    grid-column: 2;
    grid-row: 2;
    align-self: stretch;
    min-height: 0;
    overflow-y: auto;
    overscroll-behavior: contain;
    padding: 4px 2px;
  }
  .ttt-setup {
    display: flex;
    flex-direction: column;
    gap: 12px;
    margin: 0;
  }
  .ttt-toolbar {
    justify-content: stretch;
    margin: 0;
  }
  .ttt-tool {
    flex: 1 1 0;
  }
  .ttt-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
    margin: 0;
  }
  .ttt-status {
    flex: none;
  }
  .ttt-actions .ttt-primary {
    width: 100%;
  }
  .ttt-board {
    grid-column: 1;
    grid-row: 1 / span 2;
    align-self: center;
    width: var(--ttt-board);
    height: var(--ttt-board);
    margin: 0 auto;
    aspect-ratio: auto;
    font-size: calc(var(--ttt-board) * 0.12);
  }
}

/* ---- スマホなど（1画面）: 高さは、共通の枠が渡す --yz-stage-h（画面の高さ − 上下のバー）に収める。
 *      状態（data-phase）ごとに、見せるものを切り替える: idle（開始前）・playing（対局中）・over（結果） ---- */
@media (max-width: 600px), (max-height: 620px), (max-width: 900px) and (orientation: portrait) {
  body:has(.ttt) {
    overflow: hidden;
    /* 1画面では、薄い紫の背景を画面全体（ヘッダーとフッターの間）に見せる */
    background:
      radial-gradient(circle at 18% 0%, #ddd2ff 0%, rgba(221, 210, 255, 0) 48%),
      linear-gradient(180deg, #ebe5ff 0%, #f4f1ff 100%);
  }
  body:has(.ttt) main {
    padding: 0;
    margin: 0;
    max-width: none;
    border-radius: 0;
    box-shadow: none;
    background: transparent;
  }

  .ttt {
    box-sizing: border-box;
    width: 100%;
    max-width: none;
    height: var(--yz-stage-h, calc(100dvh - 76px));
    margin: 0;
    padding: 8px max(12px, env(safe-area-inset-right, 0px)) 8px max(12px, env(safe-area-inset-left, 0px));
    display: flex;
    flex-direction: column;
    gap: 8px;
    overflow: hidden;
    --ttt-board: min(calc(100vw - 24px), calc(var(--yz-stage-h, 600px) - 170px), 420px);
  }
  .ttt-head,
  .ttt-setup,
  .ttt-bar {
    margin: 0;
  }
  .ttt[data-phase] .ttt-head {
    display: none;
  }
  .ttt-setup {
    gap: 8px;
  }
  .ttt-field {
    padding: 10px;
  }
  .ttt-tag {
    display: none;
  }
  .ttt-toolbar {
    margin: 0;
  }
  .ttt-bar {
    flex-direction: column;
    align-items: stretch;
    gap: 6px;
  }
  .ttt-status {
    flex: none;
    text-align: center;
    font-size: 0.95rem;
  }
  .ttt-actions {
    justify-content: center;
  }
  .ttt-primary,
  .ttt-secondary {
    flex: 1 1 0;
    min-width: 0;
  }
  .ttt-secondary {
    background: #ffffff;
  }

  /* 薄い紫の背景の上では、白は「面」（設定のカード・状態の帯・盤の周り・ボタン・ダイアログ）だけに使う。
   盤の台は、背景に埋もれないよう、少し濃い紫にする。文字は、紫の上でも読めるよう濃くする */
  .ttt-field,
  .ttt-status {
    background: #ffffff;
  }
  .ttt-status {
    box-shadow: 0 6px 16px -10px rgba(76, 47, 168, 0.45);
  }
  .ttt-eyebrow {
    color: var(--ttt-accent-dark);
  }
  .ttt h1 span {
    color: #4f5667;
  }

  /* 盤: 幅と高さの小さいほうに合わせる（残りの高さは、盤の上下で均等に空ける） */
  .ttt-board {
    flex: none;
    width: var(--ttt-board);
    height: var(--ttt-board);
    margin: auto;
    padding: 8px;
    border-radius: 18px;
    font-size: calc(var(--ttt-board) * 0.12);
    background: linear-gradient(145deg, #d6c9fb 0%, #bfb0f2 100%);
    box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.5), 0 18px 36px -18px rgba(76, 47, 168, 0.6);
  }

  /* 結果（over）の時だけ「強さ・手番を変える」を出す。もう一度は、対局の開始と同じボタン */
  .ttt[data-phase="over"] .ttt-secondary {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }
  .ttt[data-phase="playing"] .ttt-primary,
  .ttt[data-phase="playing"] .ttt-secondary {
    display: none;
  }
  .ttt[data-phase="playing"] .ttt-setup,
  .ttt[data-phase="over"] .ttt-setup {
    display: none;
  }
  .ttt[data-phase="idle"] .ttt-head {
    display: block;
  }
  .ttt[data-phase="idle"] .ttt-head h1 {
    font-size: 1.4rem;
  }
  .ttt[data-phase="idle"] .ttt-board {
    display: none;
  }

  .ttt-modal {
    width: min(92vw, 420px);
    padding: 16px;
    border-radius: 16px;
  }
  .ttt-modal-tools .ttt-close {
    min-height: 40px;
  }
  .ttt-how dl > div {
    grid-template-columns: 1fr;
    gap: 2px;
  }
}

/* 横持ちのスマホ（高さが低い、幅は広い）: 盤を左、操作と状態を右に並べる。開始前は、設定を中央に出す */
@media (min-width: 601px) and (max-height: 500px), (min-width: 601px) and (max-height: 620px) and (orientation: landscape) {
  .ttt {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    align-items: center;
    column-gap: 24px;
    --ttt-board: min(calc(var(--yz-stage-h, 300px) - 16px), 56vw);
  }
  .ttt[data-phase] .ttt-head {
    display: none;
  }
  .ttt-side {
    display: flex;
    flex-direction: column;
    gap: 8px;
    grid-column: 2;
    grid-row: 1;
    min-width: 0;
  }
  .ttt-setup {
    margin: 0;
  }
  .ttt-board {
    grid-column: 1;
    grid-row: 1;
    margin: 0;
  }
  .ttt[data-phase="idle"] .ttt-side {
    grid-column: 1 / -1;
    justify-self: center;
    width: min(100%, 640px);
  }
  .ttt[data-phase="idle"] .ttt-setup {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
  }
  .ttt[data-phase="idle"] .ttt-hint {
    font-size: 0.8rem;
  }
}

/* 広い画面でも高さが低いとき（ノートPCなど）: 見出しと、設定の説明を省いて、1画面に収める */
@media (min-width: 901px) and (min-height: 621px) and (max-height: 780px) {
  .ttt-eyebrow,
  .ttt-tag {
    display: none;
  }
  .ttt-head h1 {
    font-size: 1.7rem;
  }
  .ttt-setup > .ttt-field:last-child .ttt-hint {
    display: none;
  }
  .ttt-side {
    gap: 10px;
  }
  .ttt-setup {
    gap: 8px;
  }
  .ttt-field {
    padding: 10px;
  }
  .ttt-choice,
  .ttt-tool {
    min-height: 40px;
  }
  .ttt-bar {
    gap: 8px;
  }
  .ttt {
    grid-template-columns: minmax(0, 1fr) minmax(300px, 440px);
  }
  .ttt-status {
    padding: 8px 12px;
  }
}

/* 押さえる動き（点滅・拡大縮小）を減らす設定のとき */
@media (prefers-reduced-motion: reduce) {
  .ttt-mark,
  .ttt-cell,
  .ttt-modal[open] {
    animation: none;
    transition: none;
  }
}
