/* Japanese reading and operation type share the native fallback stack. */
:root {
  --ui-font: "Yu Gothic Medium", "Yu Gothic", "Hiragino Kaku Gothic ProN", "Noto Sans JP", sans-serif;
}
#home-mypage button,
#home-setup button,
#home-setup legend,
#home-setup .note,
#home-setup .setup-summary,
#home-setup .setup-readiness,
#game .game-heading button,
#game>.note,
#game>#turn,
#replay-actions,
#home-mypage .note {
  font-family: var(--ui-font);
}
#home-mypage #new-match span,
#home-mypage[data-has-active=true] #resume>span,
#home-mypage #open-history span,
#home-setup .home-subheading h1 {
  font-family: var(--serif);
  font-weight: 700;
}

/* A single setup surface replaces the nested frame, with clear choice groups. */
dialog#home-setup {
  background: #192e40;
  border: 1px solid #bea16a;
  border-radius: 10px;
  padding: 20px;
  max-height: calc(100vh - 32px);
  max-height: calc(100dvh - 32px);
  overflow-y: auto;
  overscroll-behavior: contain;
}
dialog#home-setup::backdrop {
  background: #07121bd9;
}
dialog#home-setup .home-subheading {
  padding-bottom: 14px;
  border-bottom: 1px solid #a58d6155;
}
dialog#home-setup .home-subheading h1 {
  font-size: 25px;
  letter-spacing: .05em;
  margin: 0;
}
dialog#home-setup .match-setup {
  padding: 4px 0 0;
  border: 0;
  background: transparent;
  box-shadow: none;
}
dialog#home-setup fieldset {
  border: 0;
  padding: 0;
  margin: 0 0 20px;
  min-width: 0;
}
dialog#home-setup legend {
  padding: 0;
  margin-bottom: 10px;
  font-size: 15px;
  font-weight: 700;
  color: #f1e4cb;
}
dialog#home-setup #levels {
  gap: 8px;
}
dialog#home-setup .choices button {
  background: #223b4f;
  border: 1px solid #688297;
  border-radius: 6px;
  font-size: 15px;
  font-weight: 700;
}
dialog#home-setup .choices button small {
  color: #c9d6df;
  font-size: 12px;
  font-weight: 400;
}
dialog#home-setup .choices button[aria-pressed=true] {
  color: #203447;
  background: #f0dfbb;
  border: 1px solid #f3d69c;
  box-shadow: inset 0 -4px 0 #b18c4e;
}
dialog#home-setup .choices button[aria-pressed=true] small {
  color: #3d5261;
}
dialog#home-setup .choices button[aria-pressed=true]:after {
  color: #28465b;
  font-size: 13px;
  font-weight: 700;
}
dialog#home-setup .choices button[aria-pressed=true] .level-emblem i {
  background: #876330;
}
dialog#home-setup .setup-summary {
  background: #223b4f;
  border: 0;
  border-radius: 5px;
  padding: 10px 8px;
  font-size: 14px;
  font-weight: 700;
  color: #f3e4c7;
}
dialog#home-setup #start:not(:disabled) {
  background: #9d3f31;
  color: #fff1dd;
  border: 1px solid #d9ae77;
  border-bottom: 3px solid #6e2a21;
  border-radius: 6px;
  font-size: 16px;
  font-weight: 700;
}
dialog#home-setup .match-setup>.note {
  font-size: 12px;
  line-height: 1.7;
}
@media (max-height: 760px) {
  dialog#home-setup { padding: 12px; }
  dialog#home-setup .home-subheading { padding-bottom: 8px; }
  dialog#home-setup fieldset { margin-bottom: 12px; }
  dialog#home-setup #levels .level-emblem { display: none; }
  dialog#home-setup #levels button { min-height: 54px; padding: 6px 4px; font-size: 14px; }
  dialog#home-setup #levels button small { margin-top: 2px; line-height: 1.4; }
  dialog#home-setup .setup-summary { padding: 8px 4px; }
}

/* A calmer illustrated lobby: a clear welcome, one centered match action. */
#home-mypage .mypage-heading {
  padding: 32px 24px;
  border: 1px solid #b9a17466;
  border-radius: 10px;
  background: #172d3e;
  box-shadow: inset 0 3px 0 #c7aa78;
}
#home-mypage .lobby-art {
  opacity: .72;
  background-position: center 45%;
}
#home-mypage .mypage-heading:after {
  background: #10263885;
}
#home-mypage .mypage-heading .eyebrow {
  color: #e3c58f;
  font-size: 13px;
  letter-spacing: .18em;
  margin-bottom: 14px;
}
#home-mypage .mypage-heading h1 {
  font-size: clamp(28px, 6vw, 36px);
  font-weight: 650;
  letter-spacing: .12em;
  margin-bottom: 14px;
  color: #fff1d6;
}
#home-mypage .mypage-heading>p:last-child {
  font-size: 15px;
  color: #e0e8ee;
}
#home-mypage .home-menu-actions {
  padding: 0;
  border: 0;
  background: transparent;
  box-shadow: none;
  gap: 16px;
}
#home-mypage #new-match {
  background: #9d3f31;
  border: 1px solid #d9ae77;
  border-bottom: 3px solid #6e2a21;
  border-radius: 8px;
  box-shadow: 0 4px 12px #0003;
}
#home-mypage #new-match span {
  letter-spacing: .1em;
}
#home-mypage #new-match small {
  color: #ffe4cb;
  font-size: 14px;
}
#home-mypage[data-has-active=true] #resume {
  background: #ecdbb7;
  border: 1px solid #f7e8c9;
  border-bottom: 3px solid #ad8d58;
  border-radius: 8px;
  box-shadow: 0 4px 12px #0003;
}
#home-mypage[data-has-active=true] #new-match {
  background: #243a4a;
  border: 1px solid #6c8190;
  box-shadow: none;
}
#home-mypage[data-has-active=true] #new-match small {
  color: #d2dde5;
}
#home-mypage #open-history {
  min-height: 64px;
  padding: 12px 20px;
  background: transparent;
  border: 1px solid #7890a266;
  border-radius: 8px;
}
#home-mypage #open-history span {
  font-size: 18px;
  color: #eee2cc;
}
#home-mypage #open-history small {
  margin-top: 4px;
  font-size: 13px;
  color: #c6d3dd;
}

/* Turn feedback is the instruction; the last move and player bands support it.
   Change only paint and weight, keeping the reserved slot heights intact. */
#game>#turn {
  border-radius: 4px;
  font-weight: 750;
  letter-spacing: .02em;
}
#game>#turn[data-state=player] {
  background: #f0e3c7;
  color: #183347;
  box-shadow: inset 0 0 0 1px #d1b681;
}
#game>#turn[data-state=thinking], #game>#turn[data-state=ai] {
  background: #26455b;
  color: #f0f6fa;
  box-shadow: inset 0 0 0 1px #7496ad;
}
#game>#last-move {
  color: #c5d1db;
  font-weight: 500;
}
#game .player-band {
  background: #603c35;
  color: #f5dfd0;
  box-shadow: inset 0 0 0 1px #a57b6955;
}
#game #player-label {
  background: #274b63;
  color: #e4f0f8;
  box-shadow: inset 0 0 0 1px #8badc355;
}
body[data-turn=player] #game #player-label {
  background: #335f7b;
  color: #fff4de;
  box-shadow: inset 0 0 0 1px #cdb687;
}
body[data-turn=ai] #game #opponent-label {
  background: #815044;
  color: #fff1de;
  box-shadow: inset 0 0 0 1px #cdb687;
}

/* Interaction polish preserves the centered lobby and reserved board geometry. */
#home-mypage .home-menu-actions button,
#home-setup .choices button,
#game .game-heading button,
#game .hand button,
#replay-actions button {
  transition: background-color 120ms ease, border-color 120ms ease, box-shadow 120ms ease;
}

/* Selected choices remain recognizable without relying on color alone. */
#home-setup .choices button[aria-pressed=true] {
  box-shadow: inset 0 -4px 0 var(--gold);
  font-weight: 750;
}
#home-setup .choices button[aria-pressed=true] small {
  color: #f4ecdc;
}

/* Disabled actions retain legible copy, with a quiet surface and no elevation. */
#home-setup #start:disabled,
#home-mypage #resume:disabled,
#game .game-heading button:disabled,
#game #replay-actions button:disabled {
  opacity: 1;
  color: #acb8c4;
  background: #263746;
  border-color: #465a6c;
  box-shadow: none;
}
#home-mypage[data-has-active=true] #resume:disabled small,
#home-mypage #resume:disabled small {
  color: #acb8c4;
}

/* Captured pieces remain game information when their buttons cannot be used. */
#game .hand button:disabled {
  opacity: 1;
  cursor: default;
}

/* Board focus sits inside a cell so its neighbours cannot obscure it. */
#game #board button:focus-visible {
  outline: 3px solid #17486b;
  outline-offset: -4px;
  z-index: 3;
}
#game .hand button:focus-visible {
  outline: 3px solid #17486b;
  outline-offset: -3px;
}
#move-count, #replay-count, #game .hand button small {
  font-variant-numeric: tabular-nums;
}

@media (hover: hover) and (pointer: fine) {
  #home-mypage #new-match:hover:not(:disabled) {
    border-color: #ffe1a6;
    box-shadow: inset 0 0 0 1px #ffe1a6, 0 4px 16px #0005;
  }
  #home-mypage #resume:hover:not(:disabled) {
    background: #f7e4bd;
    border-color: #fff0cd;
  }
  #home-mypage #open-history:hover:not(:disabled),
  #home-setup .choices button:hover:not(:disabled),
  #game .game-heading button:hover:not(:disabled),
  #replay-actions button:hover:not(:disabled) {
    border-color: var(--gold);
    box-shadow: inset 0 0 0 1px var(--gold);
  }
  #game .hand button:hover:not(:disabled) {
    border-color: #17486b;
    box-shadow: inset 0 0 0 1px #17486b;
  }
  #home-setup .choices button[aria-pressed=true]:hover:not(:disabled) {
    box-shadow: inset 0 -4px 0 var(--gold), inset 0 0 0 1px var(--gold);
  }
}

@media (prefers-reduced-motion: reduce) {
  #home-mypage .home-menu-actions button,
  #home-setup .choices button,
  #game .game-heading button,
  #game .hand button,
  #replay-actions button {
    transition: none;
  }
}

/* Phone surfaces use the available viewport, including notched safe areas.
   Boards keep their natural aspect ratio and short screens can still scroll. */
@media (max-width: 767px) {
  body[data-view=home] main {
    width: 100%;
    max-width: none;
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(12px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }
  #home-mypage {
    max-width: none;
  }
  #home-mypage .mypage-heading {
    padding: 28px 18px;
  }
  body[data-view=game] main,
  body[data-view=replay] main {
    width: 100%;
    max-width: none;
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(4px, env(safe-area-inset-top)) max(2px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(2px, env(safe-area-inset-left));
  }
  dialog#home-setup {
    inset: 0;
    width: 100vw;
    max-width: none;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    margin: 0;
    padding: max(16px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    border: 0;
    border-radius: 0;
    overflow-y: auto;
  }
  dialog#home-setup .home-subheading {
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 2;
    background: #192e40;
  }
  dialog#home-setup .match-setup {
    padding-bottom: 32px;
  }
}

/* Rotated phones put controls beside the board and hands beside its edges.
   The board keeps its own aspect ratio and fits the available screen height. */
@media (min-width: 600px) and (max-width: 1023px) and (max-height: 600px) and (orientation: landscape) {
  body[data-view=game] main,
  body[data-view=replay] main {
    width: 100%;
    max-width: none;
    padding: max(8px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) max(8px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  }
  body[data-view=game] #game,
  body[data-view=replay] #game {
    display: grid;
    max-width: none;
    width: 100%;
    grid-template-columns: minmax(180px, 220px) minmax(0, 1fr);
    grid-template-rows: auto auto auto auto 1fr;
    gap: 8px 12px;
    align-items: start;
  }
  #game>.game-heading { grid-column: 1; grid-row: 1; }
  #game>#replay-actions { grid-column: 1; grid-row: 2; gap: 3px; }
  #game>#turn { grid-column: 1; grid-row: 3; }
  #game>#last-move { grid-column: 1; grid-row: 4; }
  #game>#result-panel { grid-column: 1; grid-row: 5; }
  #game>.board-wrap {
    --landscape-board-width: min(calc((100dvh - 52px - env(safe-area-inset-top) - env(safe-area-inset-bottom)) * .921875), calc(100% - 112px));
    display: grid;
    grid-column: 2;
    grid-row: 1 / 6;
    grid-template-columns: minmax(50px, 1fr) var(--landscape-board-width) minmax(50px, 1fr);
    grid-template-rows: auto 1fr;
    gap: 6px;
    width: 100%;
    max-width: none;
    margin: 0;
    align-items: start;
  }
  #game .board-grid { grid-column: 2; grid-row: 2; }
  #game .board-files { grid-column: 2; grid-row: 1; }
  #game #opponent-label { grid-column: 1; grid-row: 1; }
  #game #player-label { grid-column: 3; grid-row: 1; }
  #game #hand-top { grid-column: 1; grid-row: 2; }
  #game #hand-bottom { grid-column: 3; grid-row: 2; }
  #game .player-band {
    min-width: 0;
    max-width: 100%;
    min-height: 24px;
    padding: 3px;
    font-size: 11px;
    line-height: 1.5;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  #game .hand,
  #game .hand:has(button),
  #game .hand:not(:has(button)) {
    flex-direction: column;
    align-items: center;
    justify-content: flex-start;
    height: auto;
    min-height: 52px;
    max-height: calc(100dvh - 64px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
    padding: 4px 2px;
    margin: 0;
    overflow-x: hidden;
    overflow-y: auto;
  }
  #game .hand button {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    min-width: 44px;
    max-width: 100%;
    padding: 3px;
    line-height: 1;
  }
  #game .hand button small {
    display: block;
    margin: 2px 0 0;
    font-size: 11px;
    line-height: 1;
  }
  #game>.game-heading .opponent-heading {
    min-height: 42px;
    height: auto;
    max-height: none;
    flex-direction: column;
    flex-wrap: nowrap;
    gap: 2px;
    overflow: visible;
  }
  #game>.game-heading {
    grid-template-rows: 44px auto;
  }
  #game>.game-heading .opponent-heading::before {
    display: none;
  }
}

@media (min-width: 600px) and (max-width: 760px) and (max-height: 600px) and (orientation: landscape) {
  body[data-view=game] #game,
  body[data-view=replay] #game {
    grid-template-columns: 180px minmax(0, 1fr);
  }
  #game>#replay-actions {
    grid-template-columns: repeat(2, minmax(44px, 1fr));
    grid-template-rows: 44px 44px 22px;
    gap: 4px;
  }
  #game>#replay-actions #replay-first { grid-column: 1; grid-row: 1; }
  #game>#replay-actions #previous { grid-column: 2; grid-row: 1; }
  #game>#replay-actions #next { grid-column: 1; grid-row: 2; }
  #game>#replay-actions #replay-last { grid-column: 2; grid-row: 2; }
  #game>#replay-actions #replay-count {
    grid-column: 1 / 3;
    grid-row: 3;
    height: 22px;
    min-height: 22px;
  }
}
@media (max-width: 767px) and (orientation: portrait) {
  html:has(body[data-view=game]),
  html:has(body[data-view=replay]) {
    overflow-y: auto;
  }
  body[data-view=home] main {
    padding-left: max(4px, env(safe-area-inset-left, 0px));
    padding-right: max(4px, env(safe-area-inset-right, 0px));
  }
  body[data-view=game] main,
  body[data-view=replay] main {
    padding-left: env(safe-area-inset-left, 0px);
    padding-right: env(safe-area-inset-right, 0px);
  }
  #game .board-wrap {
    max-width: none;
    width: 100%;
  }
  /* Keep the board stationary when captures populate either tray. */
  #game .hand,
  #game .hand:not(:has(button)),
  #game .hand:has(button) {
    height: 32px;
    min-height: 32px;
    max-height: 32px;
  }
  #game .hand {
    margin: 0;
    padding: 0 4px;
    border-top-width: 0;
    border-bottom-width: 0;
  }
  body[data-view=game] #game,
  body[data-view=replay] #game {
    row-gap: 0;
  }
  #game>.game-heading {
    row-gap: 0;
  }
  #game>#turn {
    height: 28px;
    min-height: 28px;
    max-height: 28px;
    margin: 0;
    padding: 3px 8px;
  }
  #game>#last-move {
    margin: 0;
  }
  #game .player-band {
    padding-top: 0;
    padding-bottom: 0;
  }
  #game .hand button {
    min-width: 32px;
    min-height: 32px;
    height: 32px;
    padding: 2px 4px;
    font-size: 14px;
  }
}

#game .hand button,
#game .hand button.selected {
  position: relative;
  background: transparent;
  border-color: transparent;
  box-shadow: none;
}
#game .hand .hand-piece {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 28px;
  padding-top: 3px;
  flex: 0 0 auto;
  clip-path: polygon(50% 0, 84% 17%, 100% 100%, 0 100%, 16% 17%);
  background: linear-gradient(115deg, #fff0cc, #f5d8a2 48%, #ebc991);
  color: #160d04;
  font-family: var(--serif);
  font-size: 14px;
  line-height: 1;
  box-shadow: inset 0 -2px #96632e;
}
#game .hand button.selected .hand-piece {
  background: #e3edf2;
}
#game .hand button small {
  position: absolute;
  right: 0;
  bottom: 0;
  margin: 0;
  padding: 0 2px;
  min-width: 12px;
  border-radius: 2px;
  background: #f8e6c4;
  color: #35200c;
  font-size: 11px;
  line-height: 13px;
  font-weight: 700;
}

/* Tall phones let the preparation breathe, with the start action near the
   bottom in normal flow. Content still grows and scrolls when it needs room. */
@media (max-width: 767px) and (min-height: 720px) and (orientation: portrait) {
  dialog#home-setup[open] {
    display: flex;
    flex-direction: column;
  }
  dialog#home-setup .home-subheading {
    flex: 0 0 auto;
  }
  dialog#home-setup .match-setup {
    display: flex;
    flex-direction: column;
    flex: 1 0 auto;
  }
  dialog#home-setup .match-setup>* {
    flex: 0 0 auto;
  }
  #home-mypage .mypage-heading {
    min-height: clamp(172px, 28dvh, 240px);
    display: flex;
    flex-direction: column;
    justify-content: center;
  }
}

/* Result actions stay legible without requiring precision taps. */
#home-history .history {
  padding: 0;
  border: 0;
  background: transparent;
}
#home-history .history button {
  position: relative;
  padding: 16px 36px 16px 16px;
  border-color: #647d8f;
  background: #203748;
  font-family: var(--ui-font);
  font-size: 14px;
  line-height: 1.7;
}
#home-history .history button::after {
  content: "";
  position: absolute;
  right: 17px;
  top: calc(50% - 4px);
  width: 7px;
  height: 7px;
  border-top: 2px solid #d0bd96;
  border-right: 2px solid #d0bd96;
  transform: rotate(45deg);
  pointer-events: none;
}
#home-history .history-date {
  color: #f2e5cd;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
#home-history .history-details {
  color: #d0dce4;
}
body[data-view=replay] #game>.game-heading #replay-history {
  grid-column: 1 / 3;
  grid-row: 1;
  min-height: 44px;
  padding: 8px 12px;
  font-size: 14px;
  text-align: left;
}
#history-title:focus-visible,
#game-title:focus-visible {
  outline: 2px solid #e3c58f;
  outline-offset: 4px;
}
.result-actions #result-replay,
.result-actions #result-home {
  font-size: 15px;
  line-height: 1.5;
  padding: 12px 16px;
}
@media (max-width: 767px) {
  .result-actions {
    grid-template-columns: minmax(0, 1fr);
    gap: 10px;
  }
  .result-actions #result-replay,
  .result-actions #result-home {
    min-height: 52px;
  }
}

/* A rotated phone is still a phone surface, even when wider than 767px. */
#home-menu #title-start {
  position: absolute;
  z-index: 2;
  inset: 0;
  width: 100%;
  height: 100%;
  transform: none;
  min-height: 48px;
  padding: 0;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  font-size: 16px;
  white-space: nowrap;
  color: #f4ecdc;
  background: transparent;
  border: 0;
  border-radius: 0;
  text-shadow: none;
  text-align: center;
}
#home-menu #title-start:focus-visible {
  outline-offset: -4px;
}
#home-menu #title-start .title-start-prompt {
  margin-bottom: max(24px, env(safe-area-inset-bottom, 0px));
  min-height: 48px;
  padding: 12px 24px;
  border: 0;
  background: transparent;
  text-shadow: 0 2px 5px #071b2b, 0 0 12px #071b2b;
  font-size: 22px;
  font-weight: 700;
  letter-spacing: .1em;
}
@media (prefers-reduced-motion: no-preference) {
  #home-menu #title-start .title-start-prompt {
    animation: title-start-pulse 1.5s ease-in-out infinite;
  }
  @keyframes title-start-pulse {
    0%, 100% { opacity: 1; }
    50% { opacity: .2; }
  }
}
@media (min-width: 900px) {
  #home-menu #title-start .title-start-prompt { font-size: 24px; }
}
html:has(body[data-view=home]) { overflow-y: auto; }
body[data-view=home] { min-height: 100dvh; }
body[data-view=home][data-home-panel=menu] main {
  padding: 0;
  min-height: 100svh;
  min-height: 100dvh;
}
body[data-home-panel=menu] #home-menu {
  height: 100svh;
  min-height: 100svh;
  height: 100dvh;
  min-height: 100dvh;
}
@media (prefers-reduced-motion: reduce) {
  #home-menu #title-start .title-start-prompt {
    animation: none;
  }
}

@media (min-width: 768px) and (max-width: 1023px) and (max-height: 600px) and (orientation: landscape) {
  dialog#home-setup {
    inset: 0;
    width: 100vw;
    max-width: none;
    height: 100vh;
    height: 100dvh;
    max-height: 100vh;
    max-height: 100dvh;
    margin: 0;
    padding: max(12px, env(safe-area-inset-top)) max(16px, env(safe-area-inset-right)) max(20px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left));
    border: 0;
    border-radius: 0;
    overflow-y: auto;
  }
  dialog#home-setup .home-subheading {
    position: sticky;
    top: env(safe-area-inset-top, 0px);
    z-index: 2;
    background: #192e40;
  }
  dialog#home-setup #levels {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
  dialog#home-setup .match-setup {
    padding-bottom: 32px;
  }
  body[data-view=home] main {
    width: 100%;
    max-width: none;
    min-height: 100vh;
    min-height: 100dvh;
    padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) max(12px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left));
  }
}
