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

:root {
  --board-bg: #e8c878;
  --board-line: #4a3018;
  --cell-size: 40px;
  --piece-bg: #faf0dc;
  --piece-border: #7a5a10;
  --accent: #d64535;
  --legal: rgba(39, 120, 55, 0.55);
  --selected: rgba(30, 110, 200, 0.42);
  --bg: #14100c;
  --surface: #2a2018;
  --surface-raised: #352818;
  --text: #f8f0e4;
  --text-muted: #c8b8a0;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

html {
  height: 100%;
}

body {
  font-family: 'Noto Serif JP', 'Yu Mincho', serif;
  background: var(--bg);
  color: var(--text);
  height: 100svh;
  max-height: 100svh;
  overflow: hidden;
  padding-top: var(--safe-top);
}

.app {
  width: 100%;
  min-height: 0;
  max-height: calc(100svh - var(--safe-top));
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.15rem;
  overflow: hidden;
  padding: 0.15rem max(0.15rem, var(--safe-right)) max(0.15rem, var(--safe-bottom)) max(0.15rem, var(--safe-left));
}

.top-section,
.game-area,
.controls {
  width: 100%;
}

.top-section {
  flex-shrink: 0;
  width: 100%;
}

.top-bar {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.25rem;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.top-bar::-webkit-scrollbar {
  display: none;
}

.top-bar-title {
  flex: 0 0 auto;
}

.top-bar-title h1 {
  font-size: clamp(0.82rem, 2vw, 0.95rem);
  font-weight: 700;
  line-height: 1.05;
}

.portal-home {
  margin: 4px 0 0;
  font-size: 0.68rem;
  line-height: 1.2;
}

.portal-home a {
  color: inherit;
  text-decoration: none;
  opacity: 0.85;
}

.portal-home a:hover {
  text-decoration: underline;
  opacity: 1;
}

.subtitle {
  display: none;
  font-size: clamp(0.58rem, 1.3vw, 0.68rem);
  line-height: 1.1;
  opacity: 0.9;
}

.app.ai-vs-ai-mode .subtitle {
  display: block;
  color: #f5b041;
  font-weight: 700;
}

.status {
  background: var(--surface-raised);
  border: 1px solid #6a5438;
  border-radius: 4px;
  padding: 0.18rem 0.4rem;
  font-size: clamp(0.68rem, 1.6vw, 0.82rem);
  font-weight: 700;
  white-space: nowrap;
  flex: 0 0 auto;
  text-align: center;
}

.btn-compact {
  padding: 0.15rem 0.35rem;
  font-size: clamp(0.58rem, 1.3vw, 0.68rem);
}

.clock-panel {
  display: flex;
  flex-direction: row;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.2rem;
  flex: 0 0 auto;
  width: auto;
}

.clock-rule {
  display: none;
}

.clock-item {
  display: flex;
  flex-direction: row;
  align-items: center;
  justify-content: center;
  gap: 0.2rem;
  background: var(--surface-raised);
  border: 1px solid #6a5438;
  border-radius: 4px;
  padding: 0.15rem 0.3rem;
  flex: 0 0 auto;
  white-space: nowrap;
}

.clock-item.active {
  background: rgba(230, 126, 34, 0.15);
  border-color: #e67e22;
}

.clock-item.byoyomi {
  background: rgba(241, 196, 15, 0.12);
  border-color: #f1c40f;
}

.clock-item.byoyomi .clock-label::after {
  content: ' 秒読';
  color: #f5b041;
  font-weight: 700;
}

.clock-item.low-time {
  background: rgba(192, 57, 43, 0.18);
  border-color: #c0392b;
}

.clock-item.low-time .clock-value {
  color: #ff8a80;
}

.clock-item.byoyomi .clock-value {
  color: #f5b041;
  font-weight: 800;
}

.clock-label {
  font-size: clamp(0.62rem, 1.5vw, 0.72rem);
  font-weight: 700;
  color: var(--text-muted);
}

.clock-value {
  font-size: clamp(0.78rem, 2vw, 0.95rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.clock-last {
  display: none;
}

.ai-think-panel {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.25rem;
  background: rgba(230, 126, 34, 0.2);
  border: 1px solid #e67e22;
  border-radius: 4px;
  padding: 0.12rem 0.35rem;
  white-space: nowrap;
  min-height: 1.7rem;
  box-sizing: border-box;
  animation: think-pulse-bg 1.2s ease-in-out infinite;
}

/* AI vs AI: keep reserved space so the board does not jump when thinking starts/stops */
.app.ai-vs-ai-mode .ai-think-panel.hidden {
  visibility: hidden;
  pointer-events: none;
  animation: none;
  display: flex !important;
}

.ai-think-label {
  font-size: clamp(0.62rem, 1.4vw, 0.72rem);
  font-weight: 700;
  color: #f5b041;
}

.ai-think-time {
  font-size: clamp(0.62rem, 1.4vw, 0.72rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: #f0e6d3;
}

@keyframes think-pulse-bg {
  0%, 100% { background: rgba(230, 126, 34, 0.18); }
  50% { background: rgba(230, 126, 34, 0.32); }
}

.ai-player.thinking-side .ai-icon {
  animation: ai-pulse 1s ease-in-out infinite;
  box-shadow: 0 0 0 2px rgba(245, 176, 65, 0.55);
}

.game-area {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

.board-row {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  justify-content: center;
  gap: 0.25rem;
  width: fit-content;
  max-width: 100%;
  margin-inline: auto;
  --hand-panel-width: calc(var(--cell-size) * 2.35);
}

.hands-panel {
  flex: 0 0 var(--hand-panel-width);
  width: var(--hand-panel-width);
  min-width: var(--hand-panel-width);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.15rem;
  min-height: calc(var(--cell-size) * 9 + 0.45rem);
  height: calc(var(--cell-size) * 9 + 0.45rem);
}

.match-mode-banner {
  flex-shrink: 0;
  text-align: center;
  font-size: clamp(0.5rem, 1.1vw, 0.58rem);
  font-weight: 700;
  color: #f5b041;
  background: rgba(230, 126, 34, 0.18);
  border: 1px solid #e67e22;
  border-radius: 4px;
  padding: 0.08rem 0.15rem;
  width: 100%;
  letter-spacing: 0.04em;
}

.ai-vs-divider {
  flex-shrink: 0;
  text-align: center;
  font-size: clamp(0.5rem, 1vw, 0.56rem);
  font-weight: 700;
  color: #f5b041;
  opacity: 0.85;
  line-height: 1;
}

.player-block {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.1rem;
  width: 100%;
  min-height: 0;
}

.sente-player-block {
  flex: 1;
}

.ai-player {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 0.18rem;
  flex-shrink: 0;
  width: 100%;
}

.ai-player-name {
  flex: 1;
  min-width: 0;
  font-size: clamp(0.46rem, 1vw, 0.54rem);
  font-weight: 700;
  line-height: 1.15;
  text-align: left;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.gote-ai-name {
  color: #f0a050;
}

.sente-ai-name {
  color: #7ec8e8;
}

.ai-player.active .ai-player-name {
  text-decoration: underline;
  text-underline-offset: 2px;
}

.gote-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.12rem;
  width: 100%;
  flex: 1;
  min-height: 0;
}

.ai-icon-link {
  flex-shrink: 0;
  display: block;
  line-height: 0;
  border-radius: 50%;
  text-decoration: none;
  color: inherit;
  cursor: pointer;
}

.ai-icon-link:hover .ai-icon {
  filter: brightness(1.08);
}

.ai-icon-link:focus-visible {
  outline: 2px solid #f5b041;
  outline-offset: 2px;
}

.ai-icon {
  flex-shrink: 0;
  width: clamp(20px, calc(var(--cell-size) * 0.5), 36px);
  height: clamp(20px, calc(var(--cell-size) * 0.5), 36px);
  border-radius: 50%;
  object-fit: cover;
  background: #fff;
  pointer-events: none;
}

.gote-ai-icon {
  border: 2px solid #e67e22;
}

.sente-ai-icon {
  border: 2px solid #5dade2;
}

.hands-panel.ai-vs-ai .gote-ai-icon,
.hands-panel.ai-vs-ai .sente-ai-icon {
  width: clamp(22px, calc(var(--cell-size) * 0.55), 40px);
  height: clamp(22px, calc(var(--cell-size) * 0.55), 40px);
}

.ai-player.active .ai-icon {
  box-shadow: 0 0 0 2px rgba(245, 176, 65, 0.45);
}

.ai-icon.thinking {
  animation: ai-pulse 1s ease-in-out infinite;
}

@keyframes ai-pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.05); }
}

.side-think-monitor {
  flex-shrink: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0.08rem;
  background: var(--surface-raised);
  border: 1px solid #6a5438;
  border-radius: 4px;
  padding: 0.2rem 0.25rem;
  text-align: center;
  min-height: 4.8rem;
  box-sizing: border-box;
}

.side-think-monitor.active {
  border-color: #e67e22;
  background: rgba(230, 126, 34, 0.18);
  animation: think-pulse-bg 1.2s ease-in-out infinite;
}

.side-think-title {
  font-size: clamp(0.5rem, 1.1vw, 0.58rem);
  font-weight: 700;
  color: var(--text-muted);
  line-height: 1.1;
}

.side-think-label {
  font-size: clamp(0.55rem, 1.2vw, 0.68rem);
  font-weight: 800;
  color: var(--text);
  line-height: 1.15;
}

.side-think-monitor.active .side-think-label {
  color: #f5b041;
}

.side-think-time {
  font-size: clamp(0.62rem, 1.4vw, 0.78rem);
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  color: var(--text);
  line-height: 1.15;
}

.side-think-eval,
.side-think-pv {
  display: block;
  width: 100%;
  line-height: 1.2;
  word-break: break-all;
  min-height: 1.15em;
}

.side-think-eval {
  font-size: clamp(0.52rem, 1.15vw, 0.64rem);
  font-weight: 800;
  color: #7ec8e8;
}

.side-think-pv {
  font-size: clamp(0.48rem, 1.05vw, 0.58rem);
  font-weight: 700;
  color: var(--text-muted);
  max-height: 2.4em;
  overflow: hidden;
}

.side-think-monitor.active .side-think-eval,
.side-think-monitor.active .side-think-pv {
  visibility: hidden;
}

.gote-hand {
  flex: 1;
  min-width: 0;
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.sente-hand {
  flex: 1;
  width: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.hand {
  width: 100%;
  background: var(--surface);
  border: 1px solid #8b6914;
  border-radius: 4px;
  padding: 0.2rem 0.35rem;
  min-height: 2rem;
  box-shadow: inset 0 0 0 1px rgba(220, 179, 92, 0.12);
}

.hand-label {
  font-size: clamp(0.55rem, 1.2vw, 0.65rem);
  opacity: 0.85;
  line-height: 1.2;
  margin-bottom: 0.12rem;
  color: #e8d4a8;
}

.hand-pieces {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  gap: 0.12rem;
  align-items: stretch;
  min-height: 1.35rem;
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  scrollbar-width: thin;
}

.hand-piece {
  font-family: inherit;
  font-size: clamp(0.6rem, 1.4vw, 0.72rem);
  background: var(--piece-bg);
  color: #2c1810;
  border: 1px solid var(--piece-border);
  border-radius: 3px;
  padding: 0.08rem 0.25rem;
  cursor: pointer;
  width: 100%;
  text-align: center;
}

.hand-empty {
  font-size: clamp(0.55rem, 1.2vw, 0.65rem);
  opacity: 0.45;
  padding: 0.05rem 0.15rem;
  text-align: center;
}

.hand-piece:hover:not(:disabled) {
  background: #fff3c4;
}

.hand-piece:disabled {
  opacity: 0.5;
  cursor: default;
}

.hand-piece.selected {
  background: #fff3c4;
  border-color: var(--accent);
}

.board-wrapper {
  display: flex;
  justify-content: center;
  flex: 0 0 auto;
  width: auto;
  max-width: calc(100% - var(--hand-panel-width, 0px) - 0.25rem);
}

.board-labels {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: calc(var(--cell-size) * 9 + 1rem);
  max-width: 100%;
}

.file-labels {
  display: grid;
  grid-template-columns: repeat(9, var(--cell-size));
  margin-left: 1rem;
}

.file-labels span {
  text-align: center;
  font-size: clamp(0.55rem, 1.2vw, 0.68rem);
  font-weight: 700;
  opacity: 0.8;
  color: var(--text-muted);
}

.board-with-ranks {
  display: flex;
  align-items: stretch;
}

.rank-labels {
  display: flex;
  flex-direction: column;
  justify-content: space-around;
  padding-right: 0.12rem;
  width: 0.9rem;
}

.rank-labels span {
  height: var(--cell-size);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: clamp(0.55rem, 1.2vw, 0.68rem);
  font-weight: 700;
  opacity: 0.8;
  color: var(--text-muted);
}

.board {
  display: grid;
  grid-template-columns: repeat(9, var(--cell-size));
  grid-template-rows: repeat(9, var(--cell-size));
  border: 2px solid var(--board-line);
  background: var(--board-bg);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.35);
}

.board.disabled {
  pointer-events: none;
  opacity: 0.85;
}

.cell {
  position: relative;
  border: 1px solid var(--board-line);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
}

.cell.selected {
  background: var(--selected);
}

.cell.legal::after {
  content: '';
  position: absolute;
  width: 28%;
  height: 28%;
  border-radius: 50%;
  background: var(--legal);
  pointer-events: none;
}

.piece {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 92%;
  height: 92%;
  background: linear-gradient(145deg, #fff8ec, #edd9a8);
  border: 1.5px solid var(--piece-border);
  border-radius: 2px;
  font-size: calc(var(--cell-size) * 0.54);
  font-weight: 800;
  color: #120a04;
  clip-path: polygon(50% 0%, 100% 15%, 100% 100%, 0% 100%, 0% 15%);
  line-height: 1;
  text-shadow: 0 0 1px rgba(255, 255, 255, 0.35);
}

.piece.gote-piece {
  transform: rotate(180deg);
}

.piece.promoted {
  color: var(--accent);
}

.commentary-panel {
  flex-shrink: 0;
  display: none;
  gap: 0.35rem;
  align-items: flex-start;
  background: var(--surface);
  border: 1px solid #5a4632;
  border-radius: 4px;
  padding: 0.25rem 0.45rem;
  width: 100%;
}

.app.commentary-on .commentary-panel {
  display: flex;
  min-height: 2.4rem;
  max-height: 2.4rem;
  overflow: hidden;
  align-items: center;
}

.commentary-label {
  flex-shrink: 0;
  font-size: clamp(0.58rem, 1.3vw, 0.65rem);
  font-weight: 700;
  color: #f5b041;
  line-height: 1.35;
}

.commentary-text {
  flex: 1;
  margin: 0;
  font-size: clamp(0.62rem, 1.4vw, 0.72rem);
  line-height: 1.4;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.controls {
  flex-shrink: 0;
  display: flex;
  flex-wrap: nowrap;
  align-items: center;
  gap: 0.2rem;
  width: 100%;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  scrollbar-width: none;
}

.controls::-webkit-scrollbar {
  display: none;
}

.controls > * {
  flex: 0 0 auto;
}

.level-select,
.mode-select,
.time-select,
.think-select,
.control-select {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  gap: 0.15rem;
  background: var(--surface);
  border: 1px solid #5a4632;
  border-radius: 4px;
  padding: 0.1rem 0.25rem;
  white-space: nowrap;
}

.level-select label,
.mode-select label,
.time-select label,
.think-select label,
.control-select label {
  font-size: clamp(0.65rem, 1.5vw, 0.75rem);
  font-weight: 700;
  white-space: nowrap;
  color: var(--text-muted);
}

.level-select select,
.mode-select select,
.time-select select,
.think-select select,
.control-select select {
  font-family: inherit;
  font-size: clamp(0.62rem, 1.4vw, 0.75rem);
  font-weight: 600;
  padding: 0.15rem 0.2rem;
  border-radius: 3px;
  border: 1px solid #6a5438;
  background: #3d2e1f;
  color: var(--text);
  max-width: 7.5rem;
}

.level-select select:disabled,
.mode-select select:disabled,
.time-select select:disabled,
.think-select select:disabled,
.control-select select:disabled {
  opacity: 0.5;
}

.btn-primary,
.btn-secondary {
  font-family: inherit;
  font-size: clamp(0.62rem, 1.4vw, 0.75rem);
  font-weight: 700;
  padding: 0.22rem 0.4rem;
  border-radius: 4px;
  border: none;
  cursor: pointer;
  white-space: nowrap;
  flex-shrink: 0;
  min-height: 2rem;
}

.btn-primary {
  background: var(--accent);
  color: #fff;
}

.btn-secondary {
  background: #5a4632;
  color: var(--text);
}

#bgm-toggle.active {
  background: #6b4e23;
  box-shadow: inset 0 0 0 1px #e8d4a8;
}

#commentary-toggle.active {
  background: #6b4e23;
  box-shadow: inset 0 0 0 1px #e8d4a8;
}

.hidden {
  display: none !important;
}

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.modal.hidden {
  display: none;
}

.modal-content {
  background: var(--surface);
  border: 1px solid #5a4632;
  border-radius: 12px;
  padding: 1.25rem 1.5rem;
  text-align: center;
}

.modal-buttons {
  display: flex;
  gap: 0.5rem;
  justify-content: center;
}

@media (max-width: 639px) {
  body {
    font-weight: 600;
    -webkit-font-smoothing: antialiased;
  }

  .top-section,
  .game-area,
  .controls {
    max-width: var(--layout-width, 100%);
  }

  .top-section {
    background: var(--surface);
    border: 1px solid #5a4632;
    border-radius: 6px;
    padding: 0.2rem 0.3rem;
  }

  .controls {
    background: var(--surface);
    border: 1px solid #5a4632;
    border-radius: 6px;
    padding: 0.3rem 0.35rem;
  }

  .app {
    gap: 0.08rem;
    padding: 0.05rem max(0.1rem, var(--safe-right)) max(0.05rem, var(--safe-bottom)) max(0.1rem, var(--safe-left));
  }

  .top-section {
    gap: 0.1rem;
    padding: 0.2rem 0.3rem;
    border-radius: 5px;
  }

  .top-bar {
    gap: 0.1rem;
  }

  .top-bar-title {
    display: none;
  }

  .status {
    font-size: 0.75rem;
    font-weight: 800;
    padding: 0.2rem 0.35rem;
  }

  .clock-label {
    font-size: 0.62rem;
  }

  .clock-value {
    font-size: 0.75rem;
    font-weight: 800;
  }

  .ai-think-panel {
    padding: 0.2rem 0.3rem;
    min-height: 1.75rem;
  }

  .ai-think-label,
  .ai-think-time {
    font-size: 0.78rem;
    font-weight: 800;
  }

  .btn-compact {
    min-height: 2rem;
    padding: 0.22rem 0.4rem;
    font-size: 0.68rem;
    font-weight: 700;
  }

  .game-area {
    flex: 1;
    min-height: 0;
    justify-content: center;
    touch-action: manipulation;
  }

  /* 盤を全幅、持ち駒は上下に配置 */
  .board-row {
    flex-direction: column;
    align-items: stretch;
    gap: 0.15rem;
    width: 100%;
    max-width: 100%;
    --hand-panel-width: 0px;
    touch-action: manipulation;
  }

  .hands-panel {
    display: contents;
  }

  .match-mode-banner {
    order: 0;
    font-size: 0.68rem;
    font-weight: 800;
    padding: 0.25rem 0.35rem;
  }

  .gote-area {
    order: 1;
    flex: none;
    flex-direction: row;
    align-items: stretch;
    gap: 0.25rem;
    width: 100%;
  }

  .board-wrapper {
    order: 2;
    width: 100%;
    max-width: 100%;
    padding: 0;
  }

  .ai-vs-divider {
    order: 3;
    font-size: 0.68rem;
    font-weight: 800;
    padding: 0.05rem 0;
  }

  .sente-player-block {
    order: 4;
    flex: none;
    flex-direction: row;
    align-items: stretch;
    gap: 0.25rem;
    width: 100%;
  }

  .ai-player {
    flex: 0 0 auto;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    gap: 0.1rem;
    max-width: 3.75rem;
  }

  .ai-player-name {
    font-size: 0.52rem;
    font-weight: 800;
    text-align: center;
    white-space: normal;
    line-height: 1.15;
    overflow: visible;
    text-overflow: unset;
  }

  .side-think-monitor {
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    gap: 0.2rem 0.35rem;
    padding: 0.2rem 0.35rem;
    min-height: 2.6rem;
  }

  .side-think-title {
    font-size: 0.58rem;
  }

  .side-think-label {
    font-size: 0.68rem;
  }

  .side-think-time {
    font-size: 0.78rem;
  }

  .ai-icon-link {
    touch-action: manipulation;
  }

  .ai-icon,
  .hands-panel.ai-vs-ai .gote-ai-icon,
  .hands-panel.ai-vs-ai .sente-ai-icon {
    width: 2.15rem;
    height: 2.15rem;
  }

  .gote-hand,
  .sente-hand {
    flex: 1;
    min-width: 0;
  }

  .hand {
    padding: 0.25rem 0.35rem;
    min-height: 2.5rem;
    display: flex;
    flex-direction: column;
    justify-content: center;
  }

  .hand-label {
    font-size: 0.58rem;
    font-weight: 700;
    opacity: 0.9;
    text-align: left;
    margin-bottom: 0.12rem;
  }

  .hand-pieces {
    flex-direction: row;
    flex-wrap: nowrap;
    align-items: center;
    gap: 0.3rem;
    min-height: 2.35rem;
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    padding-bottom: 0.05rem;
  }

  .hand-pieces::-webkit-scrollbar {
    display: none;
  }

  .hand-piece {
    flex-shrink: 0;
    width: auto;
    min-width: 2.5rem;
    min-height: 2.5rem;
    font-size: 0.82rem;
    font-weight: 800;
    padding: 0.3rem 0.5rem;
    touch-action: manipulation;
  }

  .hand-empty {
    font-size: 0.68rem;
    font-weight: 600;
  }

  .file-labels span,
  .rank-labels span {
    font-weight: 700;
    opacity: 0.85;
  }

  .piece {
    font-weight: 900;
    pointer-events: none;
  }
    width: min(100%, calc(var(--cell-size) * 9 + 1rem));
    margin: 0 auto;
  }

  .board {
    touch-action: manipulation;
  }

  .cell {
    touch-action: manipulation;
    -webkit-tap-highlight-color: transparent;
  }

  .board:not(.disabled) .cell:active {
    background: rgba(255, 255, 255, 0.14);
  }

  .board:not(.disabled) .cell.legal::after {
    width: 34%;
    height: 34%;
  }

  .commentary-panel {
    padding: 0.25rem 0.4rem;
    min-height: 2rem;
    max-height: 3.2rem;
    overflow: hidden;
  }

  .commentary-label {
    font-size: 0.68rem;
    font-weight: 800;
  }

  .commentary-text {
    font-size: 0.78rem;
    font-weight: 700;
    line-height: 1.45;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
    overflow: hidden;
  }

  .controls {
    gap: 0.2rem;
    padding: 0.25rem 0.3rem;
    border-radius: 5px;
  }

  .level-select select,
  .mode-select select,
  .time-select select,
  .think-select select,
  .control-select select {
    font-size: 0.72rem;
    max-width: 5.5rem;
  }

  .btn-primary,
  .btn-secondary {
    font-size: 0.72rem;
    min-height: 2rem;
    padding: 0.22rem 0.35rem;
    touch-action: manipulation;
  }

  #new-game {
    font-size: 0.78rem;
    font-weight: 800;
    min-height: 2rem;
    padding: 0.25rem 0.45rem;
  }

  .modal-content {
    width: min(92vw, 20rem);
    padding: 1.5rem 1.25rem;
  }

  .modal-content p {
    font-size: 1.05rem;
    font-weight: 700;
    margin-bottom: 1rem;
  }

  .modal-buttons {
    gap: 0.75rem;
  }

  .modal-buttons .btn-primary,
  .modal-buttons .btn-secondary {
    flex: 1;
    min-height: 3rem;
    font-size: 1rem;
    font-weight: 800;
  }
}

@media (min-width: 640px) {
  body {
    height: 100dvh;
    max-height: 100dvh;
  }

  .app {
    max-height: calc(100dvh - var(--safe-top));
    gap: 0.15rem;
    align-items: stretch;
    padding: 0.2rem 0.35rem;
  }

  .top-bar-title h1 {
    font-size: 1rem;
    white-space: nowrap;
    display: inline;
  }

  .app.ai-vs-ai-mode .subtitle {
    display: inline;
    margin-left: 0.35rem;
  }

  .game-area {
    flex: 1;
    min-height: 0;
    justify-content: center;
  }

  .board-row {
    width: fit-content;
    max-width: 100%;
    margin-inline: auto;
  }

  .hand {
    background: var(--surface-raised);
    border-color: #6a5438;
  }

  .hand-piece {
    font-weight: 800;
    min-height: 1.75rem;
  }
}

.boot-error {
  position: fixed;
  top: 0;
  left: 0;
  right: 0;
  z-index: 200;
  background: #922b21;
  color: #fff;
  padding: 0.5rem 0.75rem;
  font-size: 0.75rem;
  text-align: center;
}

.debug-panel {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  background: rgba(10, 8, 6, 0.95);
  border-top: 1px solid #5a4632;
  font-family: Consolas, 'Courier New', monospace;
  font-size: 0.58rem;
  color: #d7ccb8;
}

.debug-panel.collapsed .debug-panel-body {
  display: none;
}

.debug-panel-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0.12rem 0.35rem;
  background: #2c2218;
}

.debug-toggle {
  font-family: inherit;
  font-size: 0.55rem;
  padding: 0.06rem 0.3rem;
  border-radius: 3px;
  border: 1px solid #5a4632;
  background: #3d2e1f;
  color: var(--text);
  cursor: pointer;
}

.debug-panel-body {
  display: grid;
  grid-template-columns: 4rem 1fr;
  gap: 0.06rem 0.25rem;
  padding: 0.2rem 0.35rem;
  max-height: min(16vh, 72px);
  overflow: auto;
}

.debug-panel-body dt {
  opacity: 0.7;
}

.debug-panel-body dd {
  margin: 0;
  word-break: break-all;
}
