:root {
  color-scheme: light; --forest: #18251d; --sage: #71806f; --cream: #f3efe4; --paper: #fffdf6;
  --wood: #e5bc73; --wood-light: #f0cf8e; --ink: #171914; --muted: #6c7067; --gold: #c7983d; --red: #a74837;
  --safe-top: env(safe-area-inset-top, 0px); --safe-bottom: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; min-height: 100%; background: var(--cream); }
body { color: var(--ink); font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", system-ui, sans-serif; -webkit-tap-highlight-color: transparent; overscroll-behavior: none; }
button, input { font: inherit; }
button { color: inherit; touch-action: manipulation; }
h1, h2, p { margin: 0; }
.app, .screen { width: 100%; min-height: 100dvh; }

.setupScreen {
  display: flex; flex-direction: column; padding: calc(24px + var(--safe-top)) 20px calc(18px + var(--safe-bottom));
  background: radial-gradient(circle at 90% 2%, rgba(199, 152, 61, .14), transparent 30%), var(--cream);
}
.brand { display: flex; align-items: center; gap: 14px; max-width: 520px; width: 100%; margin: 0 auto; }
.brandMark {
  display: grid; place-items: center; width: 58px; height: 64px; background: var(--forest); color: #f6e4b7;
  font-family: serif; font-size: 28px; font-weight: 700; clip-path: polygon(16% 8%, 84% 8%, 100% 100%, 0 100%);
  filter: drop-shadow(0 6px 12px rgba(24, 37, 29, .18));
}
.eyebrow { color: var(--sage); font-size: 10px; font-weight: 800; letter-spacing: .18em; }
.brand h1 { margin-top: 3px; font-family: serif; font-size: 29px; letter-spacing: .08em; }
.setupBody { display: grid; gap: 19px; width: 100%; max-width: 520px; margin: 32px auto 24px; }
.settingGroup { min-width: 0; margin: 0; padding: 0; border: 0; }
.settingGroup legend { display: flex; align-items: center; gap: 8px; margin-bottom: 9px; font-size: 14px; font-weight: 800; }
.settingGroup legend span { display: grid; place-items: center; width: 25px; height: 25px; border-radius: 50%; background: var(--forest); color: white; font-size: 11px; }
.segmented, .timeChoices { display: grid; gap: 8px; }
.segmented { grid-template-columns: 1fr 1fr; }
.timeChoices { grid-template-columns: repeat(3, 1fr); }
.segmented label, .timeChoices label { position: relative; }
.segmented input, .timeChoices input { position: absolute; opacity: 0; pointer-events: none; }
.segmented label > span, .timeChoices label > span {
  display: grid; place-content: center; min-height: 74px; border: 1.5px solid #d5d0c3; border-radius: 13px;
  background: rgba(255, 253, 246, .72); text-align: center; font-weight: 800; transition: .15s ease;
}
.segmented small, .timeChoices small { display: block; margin-top: 4px; color: var(--muted); font-size: 10px; font-weight: 500; }
.segmented input:focus-visible + span, .timeChoices input:focus-visible + span { outline: 3px solid rgba(199, 152, 61, .5); }
.segmented input:checked + span, .timeChoices input:checked + span {
  border-color: var(--forest); background: var(--paper); color: var(--forest); box-shadow: 0 4px 14px rgba(24, 37, 29, .10), inset 0 0 0 1px var(--forest);
}
.timeChoices label > span { min-height: 78px; font-size: 13px; }
.timeChoices b { margin-right: 2px; font-family: Georgia, serif; font-size: 25px; }
.setupFooter { width: 100%; max-width: 520px; margin: auto auto 0; }
.setupFooter p { margin-bottom: 10px; color: var(--sage); font-size: 11px; text-align: center; }
.engineDot { display: inline-block; width: 7px; height: 7px; margin-right: 4px; border-radius: 50%; background: #4d8a5d; }
.primaryButton { width: 100%; min-height: 57px; border: 0; border-radius: 14px; background: var(--forest); color: white; box-shadow: 0 9px 20px rgba(24, 37, 29, .2); font-weight: 800; letter-spacing: .04em; }
.primaryButton span { float: right; margin-right: 16px; font-size: 20px; line-height: 20px; }
.installButton { display: block; margin: 12px auto 0; border: 0; background: transparent; color: var(--forest); font-size: 12px; text-decoration: underline; }

.gameScreen { position: relative; display: flex; flex-direction: column; padding: calc(8px + var(--safe-top)) 10px calc(7px + var(--safe-bottom)); background: var(--cream); overflow: hidden; }
.gameHeader { position: relative; display: grid; grid-template-columns: 42px 1fr 42px; align-items: center; min-height: 48px; }
.gameHeading { text-align: center; }
.gameHeading h1 { margin-top: 1px; font-family: serif; font-size: 20px; letter-spacing: .05em; }
.iconButton { width: 40px; height: 40px; border: 0; border-radius: 50%; background: transparent; font-size: 28px; line-height: 1; }
.settingsButton { font-size: 22px; }
#menuButton { font-size: 16px; letter-spacing: 2px; }
.gameMenu { position: absolute; z-index: 20; top: 44px; right: 0; width: 136px; padding: 6px; border: 1px solid #d5d0c3; border-radius: 12px; background: var(--paper); box-shadow: 0 9px 24px rgba(0,0,0,.16); }
.gameMenu button { width: 100%; min-height: 42px; border: 0; border-radius: 8px; background: transparent; text-align: left; padding: 0 12px; }
.gameMenu button:active { background: #eee9dd; }
.gameMenu .menuDanger { color: var(--red); }
.turnCard { display: flex; align-items: center; gap: 10px; min-height: 51px; margin: 4px 2px 7px; padding: 7px 10px; border-radius: 12px; background: var(--forest); color: white; }
.turnBadge { display: grid; place-items: center; flex: 0 0 auto; width: 34px; height: 36px; background: #f2d598; color: var(--forest); font-family: serif; font-weight: 800; clip-path: polygon(14% 6%, 86% 6%, 100% 100%, 0 100%); }
.turnBadge.gote { transform: rotate(180deg); }
.turnCopy { display: grid; gap: 1px; min-width: 0; }
.turnCopy b { font-size: 14px; }
.turnCopy span { color: #c9d2ca; font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.thinking { display: flex; gap: 3px; margin-left: auto; padding-right: 5px; }
.thinking i { width: 5px; height: 5px; border-radius: 50%; background: #f2d598; animation: pulse 1s infinite ease-in-out; }
.thinking i:nth-child(2) { animation-delay: .15s; }.thinking i:nth-child(3) { animation-delay: .3s; }
@keyframes pulse { 0%, 70%, 100% { opacity: .25; transform: translateY(0); } 35% { opacity: 1; transform: translateY(-3px); } }

.gameTable { display: flex; flex: 1; min-height: 0; flex-direction: column; justify-content: center; }
.connectionError { margin: auto 12px; padding: 28px 20px; border: 1px solid #d5d0c3; border-radius: 18px; background: var(--paper); text-align: center; box-shadow: 0 10px 30px rgba(24,37,29,.1); }
.connectionIcon { display: grid; place-items: center; width: 48px; height: 48px; margin: 0 auto 14px; border-radius: 50%; background: #f0dfd8; color: var(--red); font-family: Georgia, serif; font-size: 28px; font-weight: 700; }
.connectionError h2 { font-family: serif; font-size: 18px; }
.connectionError p { margin: 7px auto 18px; max-width: 280px; color: var(--muted); font-size: 12px; line-height: 1.6; }
.connectionError .primaryButton { min-height: 48px; box-shadow: none; }
.gameScreen.connectionFailed .handArea,
.gameScreen.connectionFailed .boardShell { display: none; }
.handArea { min-height: 56px; padding: 4px 2px; }
.playerLine { display: flex; align-items: center; gap: 6px; height: 16px; font-size: 10px; }
.playerLine span { padding: 1px 5px; border-radius: 8px; background: #dde3da; color: var(--forest); font-size: 8px; font-weight: 800; }
.opponentHand .playerLine { flex-direction: row-reverse; }
.hand { display: flex; align-items: center; gap: 4px; min-height: 38px; overflow-x: auto; scrollbar-width: none; }
.opponentHand .hand { justify-content: flex-start; flex-direction: row-reverse; }
.hand::-webkit-scrollbar { display: none; }
.handPiece { position: relative; flex: 0 0 38px; width: 38px; height: 40px; padding: 0; border: 0; background: transparent; }
.handPiece.selected { background: #f9dc72; filter: drop-shadow(0 0 3px var(--gold)); }
.handPiece:disabled { color: inherit; opacity: 1; }
.handPiece .piece { width: 100%; height: 100%; }
.handPiece b { position: absolute; right: 1px; bottom: 0; padding: 0 2px; border-radius: 5px; background: rgba(255,255,255,.78); font-size: 9px; }
.emptyHand { color: var(--muted); font-size: 10px; }
.boardShell { width: 100%; max-width: min(100%, calc(100dvh - 250px)); margin: 0 auto; }
.fileLabels { display: grid; grid-template-columns: repeat(9, 1fr); padding: 0 2px 3px; color: var(--muted); font-size: 9px; text-align: center; }
.board { display: grid; grid-template-columns: repeat(9, 1fr); grid-template-rows: repeat(9, 1fr); width: 100%; aspect-ratio: 878 / 960; padding: 1.3%; border: 0; background: url("/assets/shogi-images/board/light.png") center / 100% 100% no-repeat; gap: 0; box-shadow: 0 6px 18px rgba(53, 38, 20, .16); }
.square { position: relative; display: grid; place-items: center; min-width: 0; min-height: 0; padding: 0; border: 0; background: transparent; }
.square:disabled { color: inherit; opacity: 1; }
.piece { display: block; width: 92%; height: 92%; object-fit: contain; pointer-events: none; user-select: none; -webkit-user-drag: none; }
.square.selected { background: rgba(250, 217, 92, .66); }
.square.enemySelected { background: rgba(168, 67, 48, .24); box-shadow: inset 0 0 0 3px rgba(151, 55, 38, .78); }
.square.effect { background: rgba(168, 67, 48, .17); }
.square.effect::after { content: ""; position: absolute; width: 22%; aspect-ratio: 1; border-radius: 50%; background: rgba(151, 55, 38, .76); pointer-events: none; }
.square.lastMove { box-shadow: inset 0 0 0 3px rgba(72, 112, 76, .58); }
.square.target::after { content: ""; position: absolute; width: 25%; aspect-ratio: 1; border-radius: 50%; background: rgba(31, 84, 58, .72); }
.square.capture::after { content: ""; position: absolute; inset: 8%; border: 3px solid rgba(151, 55, 38, .8); border-radius: 50%; }
.gameFooter { display: flex; justify-content: space-between; align-items: center; min-height: 25px; color: var(--muted); font-size: 10px; }
.gameFooter #selection { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.gameFooter #moveCount { flex: 0 0 auto; margin-left: 8px; }

dialog { width: calc(100% - 40px); max-width: 390px; border: 0; border-radius: 18px; background: var(--paper); padding: 22px; color: var(--ink); box-shadow: 0 18px 60px rgba(0,0,0,.25); }
dialog::backdrop { background: rgba(15, 22, 17, .58); backdrop-filter: blur(2px); }
dialog h2 { font-family: serif; font-size: 21px; }
dialog p { margin-top: 7px; color: var(--muted); font-size: 12px; }
.promoteChoices, .dialogActions { display: flex; gap: 9px; margin-top: 20px; }
.promoteChoices button, .dialogActions button { flex: 1; min-height: 48px; border: 1px solid #bbb5a8; border-radius: 11px; background: white; font-weight: 800; }
.promoteChoices .primaryButton { background: var(--forest); color: white; }
.dangerButton { border-color: var(--red) !important; background: var(--red) !important; color: white; }
.resultDialog { text-align: center; }
.resultEmblem { display: grid; place-items: center; width: 58px; height: 64px; margin: 0 auto 13px; background: var(--forest); color: #f6e4b7; font-family: serif; font-size: 27px; font-weight: 700; clip-path: polygon(16% 8%, 84% 8%, 100% 100%, 0 100%); }
.resultDialog h2 { margin-top: 5px; font-size: 27px; }
.resultSummary { display: grid; grid-template-columns: 1fr 1fr; margin: 20px 0; border-block: 1px solid #ddd7ca; }
.resultSummary div { padding: 12px 8px; }
.resultSummary div + div { border-left: 1px solid #ddd7ca; }
.resultSummary dt { color: var(--muted); font-size: 10px; }
.resultSummary dd { margin: 4px 0 0; font-size: 14px; font-weight: 800; }
.resultActions { display: grid; grid-template-columns: 1fr 1.25fr; gap: 9px; }
.resultActions button { min-height: 50px; border: 1px solid #bbb5a8; border-radius: 11px; background: white; font-weight: 800; }
.resultActions .primaryButton { border-color: var(--forest); background: var(--forest); color: white; box-shadow: none; }
.resultActions .resultKifu { grid-column: 1 / -1; border-color: var(--forest); background: var(--forest); color: white; }

.kifuDialog { height: min(94dvh, 780px); padding: 14px; overflow: hidden; }
.kifuDialog[open] { display: grid; grid-template-rows: auto auto minmax(90px, 1fr); gap: 9px; }
.kifuHeader { display: flex; align-items: center; justify-content: space-between; padding-inline: 2px; }
.kifuHeader h2 { margin-top: 1px; font-size: 23px; }
.kifuHeader .iconButton { font-size: 26px; }
.kifuViewer { display: grid; justify-items: center; min-height: 0; }
.kifuBoardShell { width: min(100%, 38dvh, 340px); }
.kifuBoard { box-shadow: 0 4px 14px rgba(53, 38, 20, .14); }
.kifuHand { display: flex; align-items: center; gap: 5px; width: min(100%, 38dvh, 340px); min-height: 22px; overflow-x: auto; color: var(--muted); font-size: 10px; white-space: nowrap; }
.kifuHand b { color: var(--ink); }
.kifuHand span { padding: 2px 5px; border-radius: 8px; background: #eee9dd; color: var(--ink); }
.kifuControls { display: grid; grid-template-columns: 48px 1fr 48px; align-items: center; gap: 8px; width: min(100%, 340px); margin-top: 5px; }
.kifuControls button { min-height: 36px; border: 1px solid #cec7b8; border-radius: 9px; background: white; font-weight: 800; }
.kifuControls button:disabled { opacity: .35; }
.kifuControls b { text-align: center; font-size: 12px; }
.kifuList { display: grid; grid-template-columns: 1fr 1fr; align-content: start; gap: 5px; min-height: 0; overflow-y: auto; padding: 2px; border-top: 1px solid #ddd7ca; }
.kifuList button { display: grid; grid-template-columns: 26px 1fr; align-items: center; min-height: 38px; padding: 4px 7px; border: 1px solid transparent; border-radius: 8px; background: transparent; text-align: left; }
.kifuList button b { color: var(--muted); font-size: 10px; }
.kifuList button span { overflow: hidden; font-size: 12px; font-weight: 700; white-space: nowrap; text-overflow: ellipsis; }
.kifuList button.current { border-color: var(--forest); background: #e7ebe5; }

@media (min-width: 600px) {
  .gameScreen { max-width: 560px; margin: 0 auto; border-inline: 1px solid rgba(0,0,0,.06); }
  .boardShell { max-width: min(520px, calc(100dvh - 250px)); }
}
@media (max-height: 650px) {
  .turnCard { min-height: 44px; margin-block: 1px 3px; }
  .handArea { min-height: 45px; padding-block: 1px; }
  .hand { min-height: 28px; }
  .handPiece { width: 29px; height: 30px; flex-basis: 29px; }
  .boardShell { max-width: min(100%, calc(100dvh - 195px)); }
  .gameFooter { min-height: 16px; }
  .kifuBoardShell, .kifuHand { width: min(100%, 33dvh, 280px); }
  .kifuControls { margin-top: 2px; }
}
