:root {
  --background: #8a5735;
  --foreground: #2f190d;
  --panel: rgba(214, 168, 116, 0.97);
  --panel-2: rgba(239, 207, 168, 0.98);
  --border: rgba(76, 40, 19, 0.42);
  --soft: #5f3922;
  --green: #16a34a;
  --wood-dark: #4b2917;
  --wood-mid: #875331;
  --wood-light: #d6a874;
  --skin: #eac57e;
  --skin-alt: #edc881;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background-color: var(--background);
  background-image:
    repeating-linear-gradient(92deg, rgba(255, 255, 255, 0.025) 0 2px, rgba(65, 31, 13, 0.045) 2px 6px),
    linear-gradient(135deg, #a16c43 0%, #875331 48%, #714126 100%);
  color: var(--foreground);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  touch-action: manipulation;
}

button,
select {
  font: inherit;
}

button,
.board,
.hand-control {
  user-select: none;
}

.shogi-app {
  min-height: 100vh;
  min-height: 100svh;
  display: grid;
  place-items: center;
  padding: 10px 292px 10px 76px;
}

.game-shell {
  --board-size: min(calc(100vw - 388px), calc(100svh - 20px), 900px);
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
}

.board-wrap {
  position: relative;
  width: var(--board-size);
  height: var(--board-size);
}

.board {
  position: relative;
  display: grid;
  grid-template-columns: repeat(9, 1fr);
  grid-template-rows: repeat(9, 1fr);
  width: 100%;
  height: 100%;
  gap: 0;
  overflow: hidden;
  border: clamp(2px, 0.42vmin, 5px) solid var(--wood-dark);
  border-radius: clamp(4px, 0.8vmin, 10px);
  background: var(--wood-dark);
  box-shadow: 0 20px 48px rgba(54, 27, 12, 0.34);
  transition: box-shadow 150ms ease;
}

.board.turn-sente {
  box-shadow:
    0 20px 48px rgba(54, 27, 12, 0.34),
    0 0 0 clamp(2px, 0.36vmin, 4px) rgba(255, 232, 202, 0.98);
}

.board.turn-gote {
  box-shadow:
    0 20px 48px rgba(54, 27, 12, 0.34),
    0 0 0 clamp(2px, 0.36vmin, 4px) rgba(177, 103, 53, 0.98);
}

.cell {
  position: relative;
  display: grid;
  place-items: center;
  min-width: 0;
  min-height: 0;
  padding: 0;
  overflow: hidden;
  border: 0;
  border-right: 1px solid rgba(75, 41, 23, 0.72);
  border-bottom: 1px solid rgba(75, 41, 23, 0.72);
  border-radius: 0;
  background-color: var(--skin);
  background-image: linear-gradient(
    90deg,
    rgba(255, 238, 190, 0.1) 0%,
    rgba(255, 255, 255, 0) 36%,
    rgba(205, 151, 69, 0.05) 64%,
    rgba(255, 237, 185, 0.12) 84%,
    rgba(255, 255, 255, 0) 100%
  );
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}

.cell:nth-child(9n) {
  border-right: 0;
}

.cell:nth-child(n + 73) {
  border-bottom: 0;
}

.cell.tone {
  background-color: var(--skin-alt);
  background-image: linear-gradient(
    90deg,
    rgba(255, 238, 190, 0.08) 0%,
    rgba(255, 255, 255, 0) 38%,
    rgba(205, 151, 69, 0.045) 66%,
    rgba(255, 237, 185, 0.1) 86%,
    rgba(255, 255, 255, 0) 100%
  );
}

.cell.selected {
  background-color: #f4d19e;
}

.cell.in-check {
  background-color: #d9977f;
}

.cell.last-origin {
  box-shadow: inset 0 0 0 clamp(2px, 0.35vmin, 4px) rgba(148, 82, 38, 0.62);
}

.cell.last-target {
  box-shadow: inset 0 0 0 clamp(2px, 0.42vmin, 5px) rgba(93, 49, 25, 0.72);
}

.cell:focus-visible,
.hand-piece:focus-visible,
.tray-toggle:focus-visible,
.promotion-choice button:focus-visible,
.icon-btn:focus-visible,
#ai-toggle:focus-visible,
select:focus-visible {
  outline: 3px solid #ffd09a;
  outline-offset: -3px;
  z-index: 9;
}

.board-piece {
  display: block;
  width: 91%;
  height: 91%;
  object-fit: contain;
  pointer-events: none;
  filter: drop-shadow(0 2px 2px rgba(0, 0, 0, 0.18));
  transition: transform 80ms ease;
}

.rotated {
  transform: rotate(180deg);
}

.cell:active .board-piece:not(.rotated) {
  transform: scale(0.96);
}

.cell:active .board-piece.rotated {
  transform: rotate(180deg) scale(0.96);
}

.move-dot {
  position: absolute;
  width: 25%;
  aspect-ratio: 1;
  border-radius: 50%;
  background: rgba(82, 45, 24, 0.52);
  pointer-events: none;
}

.capture-ring {
  position: absolute;
  width: 87%;
  aspect-ratio: 1;
  border: clamp(2px, 0.5vmin, 5px) solid rgba(145, 58, 39, 0.66);
  border-radius: 50%;
  pointer-events: none;
}

.last-move-arrow {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: visible;
  pointer-events: none;
  z-index: 7;
}

.last-move-arrow-line,
.last-move-arrow-origin {
  animation: arrow-pop 180ms ease-out;
}

@keyframes arrow-pop {
  from { opacity: 0; }
  to { opacity: 0.88; }
}

.hand-controls {
  display: contents;
}

.hand-control {
  position: absolute;
  z-index: 20;
  left: calc(100% + 8px);
  width: 38px;
  height: 38px;
}

.hand-gote {
  top: 0;
}

.hand-sente {
  bottom: 0;
}

.tray-toggle {
  position: relative;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: linear-gradient(145deg, #e4bd8d, #c68f59);
  color: #4b2917;
  box-shadow: 0 4px 12px rgba(54, 27, 12, 0.2);
  cursor: pointer;
  font-size: 23px;
  line-height: 1;
}

.tray-toggle.active {
  color: #7a3e1f;
}

.tray-toggle.open {
  background: linear-gradient(145deg, #c88e57, #aa6c3d);
}

.tray-toggle:active {
  transform: scale(0.96);
}

.tray-label {
  display: none;
}

.tray-count {
  position: absolute;
  top: -5px;
  right: -5px;
  min-width: 17px;
  height: 17px;
  display: grid;
  place-items: center;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--wood-dark);
  color: #fff3e2;
  font-size: 10px;
  font-weight: 800;
}

.hand-tray {
  position: absolute;
  right: 0;
  width: 46px;
  min-height: 46px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 3px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background-color: rgba(220, 174, 120, 0.98);
  background-image: linear-gradient(92deg, rgba(255, 240, 217, 0.14), rgba(104, 54, 27, 0.08));
  box-shadow: 0 10px 25px rgba(54, 27, 12, 0.28);
}

.hand-tray.active {
  border-color: rgba(104, 53, 26, 0.74);
}

.hand-gote .hand-tray {
  top: calc(100% + 6px);
}

.hand-sente .hand-tray {
  bottom: calc(100% + 6px);
}

.empty-tray {
  width: 38px;
  height: 38px;
  display: block;
}

.hand-piece {
  position: relative;
  width: 38px;
  height: 38px;
  display: grid;
  place-items: center;
  padding: 1px;
  border: 0;
  border-radius: 6px;
  background: transparent;
  cursor: pointer;
}

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

.hand-piece.selected {
  background: #f4d19e;
}

.hand-piece img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
}

.piece-count {
  position: absolute;
  right: -1px;
  bottom: 1px;
  min-width: 17px;
  height: 17px;
  display: grid;
  place-items: center;
  padding: 0 4px;
  border-radius: 999px;
  background: var(--wood-dark);
  color: #fff3e2;
  font-size: 10px;
  font-weight: 800;
  line-height: 1;
}

.promotion-choice {
  position: absolute;
  z-index: 30;
  left: 50%;
  bottom: 12px;
  display: flex;
  gap: 6px;
  transform: translateX(-50%);
  padding: 6px;
  border: 1px solid var(--border);
  border-radius: 12px;
  background: linear-gradient(145deg, rgba(226, 183, 133, 0.98), rgba(190, 132, 79, 0.98));
  box-shadow: 0 14px 35px rgba(54, 27, 12, 0.32);
  backdrop-filter: blur(8px);
}

.promotion-choice button {
  min-width: 94px;
  display: flex;
  align-items: center;
  gap: 5px;
  padding: 5px 8px;
  border: 0;
  border-radius: 8px;
  background: #efd0a9;
  color: #4b2917;
  cursor: pointer;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.promotion-choice button.promote {
  background: #c98951;
  color: #fff5e7;
}

.promotion-choice img {
  width: 34px;
  height: 34px;
  object-fit: contain;
}

.icon-stack,
.side-panel {
  background: var(--panel);
  background-image: linear-gradient(92deg, rgba(255, 239, 217, 0.12), rgba(92, 46, 22, 0.07));
  border: 1px solid var(--border);
  box-shadow: 0 18px 42px rgba(54, 27, 12, 0.28);
  backdrop-filter: blur(8px);
}

.icon-stack {
  position: absolute;
  top: 50%;
  right: calc(100% + clamp(10px, 1.5vw, 20px));
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 9px;
  border-radius: 14px;
  z-index: 20;
}

.icon-btn,
#ai-toggle {
  border: 0;
  cursor: pointer;
  color: var(--foreground);
  transition: transform 120ms ease, filter 120ms ease, background 120ms ease;
}

.icon-btn {
  width: 46px;
  height: 46px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  background: linear-gradient(145deg, #754126, #4b2917);
  color: #fff3e2;
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.08);
}

.icon-btn:disabled {
  cursor: default;
  opacity: 0.38;
}

.icon-btn:not(:disabled):hover,
#ai-toggle:hover {
  filter: brightness(1.08);
}

.icon-btn:not(:disabled):active,
#ai-toggle:active {
  transform: scale(0.96);
}

.side-panel {
  position: absolute;
  top: 50%;
  left: calc(100% + 58px);
  transform: translateY(-50%);
  width: 218px;
  padding: 12px;
  border-radius: 16px;
  z-index: 20;
}

.panel-title {
  margin-bottom: 10px;
  color: #3f2414;
  text-align: center;
  font-size: 13px;
  font-weight: 900;
  letter-spacing: 0.12em;
}

#ai-toggle {
  width: 100%;
  min-height: 40px;
  margin-bottom: 10px;
  border-radius: 12px;
  background: #374151;
  color: #ffffff;
  font-weight: 900;
}

#ai-toggle.enabled {
  background: var(--green);
}

.field-label {
  display: block;
  margin: 8px 0 4px;
  color: var(--soft);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

select {
  width: 100%;
  min-height: 36px;
  padding: 0 8px;
  border: 1px solid rgba(76, 40, 19, 0.34);
  border-radius: 10px;
  outline: none;
  background: var(--panel-2);
  color: var(--foreground);
  cursor: pointer;
}

#ai-status {
  min-height: 48px;
  margin: 10px 0;
  overflow-wrap: anywhere;
  color: var(--soft);
  text-align: center;
  font-size: 12px;
  line-height: 1.32;
}

.mini-grid {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-top: 10px;
}

.metric {
  min-width: 0;
  padding: 8px 6px;
  border: 1px solid rgba(76, 40, 19, 0.25);
  border-radius: 12px;
  background: var(--panel-2);
  text-align: center;
}

.metric-value {
  overflow: hidden;
  color: #3f2414;
  font-size: 15px;
  font-weight: 900;
  line-height: 18px;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.metric-label {
  margin-top: 2px;
  color: var(--soft);
  font-size: 10px;
  line-height: 12px;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

#stats-status {
  margin-top: 8px;
  color: rgba(81, 45, 25, 0.76);
  text-align: center;
  font-size: 10px;
  line-height: 1.25;
}

.hidden {
  display: none !important;
}

@media (max-width: 1000px) {
  html,
  body {
    overflow-x: hidden;
    overflow-y: auto;
  }

  .shogi-app {
    display: block;
    min-height: 100svh;
    padding:
      max(8px, env(safe-area-inset-top, 0px))
      max(8px, env(safe-area-inset-right, 0px))
      max(16px, env(safe-area-inset-bottom, 0px))
      max(8px, env(safe-area-inset-left, 0px));
  }

  .game-shell {
    --board-size: min(
      calc(100vw - 16px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      720px
    );
    height: auto;
    display: flex;
    flex-direction: column;
    margin: 0 auto;
  }

  .board-wrap {
    order: 2;
  }

  .side-panel {
    position: relative;
    top: auto;
    left: auto;
    order: 1;
    width: 100%;
    min-height: 0;
    transform: none;
    display: grid;
    grid-template-columns: repeat(3, minmax(0, 1fr));
    grid-template-areas:
      "toggle side level"
      "status status status"
      "stats stats stats";
    gap: 7px;
    align-items: center;
    margin: 0 0 10px;
    padding: 8px;
    border-radius: 12px;
  }

  .panel-title,
  .field-label,
  #stats-status {
    display: none;
  }

  #ai-toggle {
    grid-area: toggle;
    min-height: 40px;
    margin: 0;
    font-size: 13px;
  }

  #ai-side-wrap { grid-area: side; }
  #ai-level-wrap { grid-area: level; }

  select {
    min-height: 40px;
    padding: 0 6px;
    font-size: 13px;
  }

  #ai-status {
    grid-area: status;
    min-height: 28px;
    max-height: 42px;
    margin: 0;
    overflow: auto;
    font-size: 10px;
  }

  #stats-metrics {
    grid-area: stats;
    margin: 0;
    gap: 5px;
  }

  .metric {
    padding: 4px;
    border-radius: 8px;
  }

  .metric-value {
    font-size: 12px;
    line-height: 13px;
  }

  .metric-label {
    font-size: 8px;
    line-height: 9px;
  }

  .hand-controls {
    order: 3;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: start;
    gap: 8px;
    width: 100%;
    margin-top: 10px;
  }

  .hand-control {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    left: auto;
    width: 100%;
    height: auto;
    min-height: 44px;
  }

  .tray-toggle {
    width: 100%;
    height: 44px;
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 7px;
    font-size: 22px;
  }

  .tray-label {
    display: inline;
    font-size: 12px;
    font-weight: 900;
    letter-spacing: 0.05em;
    text-transform: uppercase;
  }

  .tray-count {
    top: 4px;
    right: 6px;
  }

  .hand-tray {
    position: relative;
    top: auto;
    right: auto;
    bottom: auto;
    width: 100%;
    min-height: 48px;
    flex-direction: row;
    flex-wrap: wrap;
    justify-content: center;
    margin-top: 6px;
  }

  .hand-gote .hand-tray,
  .hand-sente .hand-tray {
    top: auto;
    bottom: auto;
  }

  .empty-tray,
  .hand-piece {
    width: 40px;
    height: 40px;
  }

  .icon-stack {
    position: relative;
    top: auto;
    right: auto;
    left: auto;
    order: 4;
    transform: none;
    width: max-content;
    flex-direction: row;
    gap: 7px;
    margin: 10px auto 0;
    padding: 5px;
    border-radius: 10px;
  }

  .icon-btn {
    width: 44px;
    height: 44px;
    border-radius: 9px;
  }
}

@media (max-width: 1000px) and (orientation: landscape) {
  .game-shell {
    --board-size: min(
      calc(100vw - 16px - env(safe-area-inset-left, 0px) - env(safe-area-inset-right, 0px)),
      calc(100svh - 12px - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px)),
      720px
    );
  }
}

@media (max-height: 520px) and (min-width: 1001px) {
  .game-shell {
    --board-size: min(calc(100vw - 388px), calc(100svh - 12px));
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    scroll-behavior: auto !important;
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
  }
}
