/* 王城のひとやすみ — 外部フォント・外部画像なし */
:root {
  --parchment: #e6d9bb;
  --parchment-2: #d8c69d;
  --ink: #2b2118;
  --ink-soft: #5a4a38;
  --green: #1e3b2e;
  --green-2: #27503d;
  --gold: #c9a54c;
  --gold-bright: #f0cf6e;
  --card: #fbf8f0;
  --card-edge: #3a3226;
  --red: #b0242a;
  --black: #1a1a1a;
  --focus: #1565c0;
  --radius: 6px;
  --toolbar-h: 60px;
  color-scheme: light;
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; min-height: 100%; background: var(--parchment); }

body {
  margin: 0;
  min-width: 320px;
  min-height: 100vh;
  color: var(--ink);
  background:
    radial-gradient(ellipse at 50% 0%, rgba(255, 250, 235, 0.7), transparent 60%),
    radial-gradient(ellipse at 50% 120%, rgba(120, 90, 50, 0.25), transparent 60%),
    var(--parchment);
  font-family: system-ui, -apple-system, "Segoe UI", "Hiragino Sans", "Yu Gothic UI", "Meiryo", sans-serif;
  line-height: 1.6;
}
body.playing { padding-bottom: calc(var(--toolbar-h) + env(safe-area-inset-bottom, 0px) + 8px); }

.page { max-width: 720px; margin: 0 auto; padding: 0 8px; }

button, input, label { touch-action: manipulation; }
button { font: inherit; color: inherit; }

:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ---------- ヘッダー ---------- */
.app-header {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px; flex-wrap: wrap;
  padding: 10px 2px 6px;
}
.title {
  margin: 0; font-size: 1.15rem; letter-spacing: 0.08em;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", "BIZ UDMincho", serif;
  display: flex; align-items: center; gap: 6px;
}
.game-info { margin: 0; font-size: 0.9rem; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.game-time { display: inline-block; min-width: 2.6em; text-align: left; font-weight: 700; color: var(--ink); }

/* ---------- 通知 ---------- */
.banners { display: grid; gap: 6px; }
.banner { margin: 0 0 6px; padding: 8px 10px; border-radius: var(--radius); font-size: 0.9rem; }
.banner[hidden] { display: none; }
.banner.warn { background: #fff3cd; border: 1px solid #b38b1d; color: #4a3600; }
.banner.ok { background: #e7f2e4; border: 1px solid #5c8a4f; color: #1f3b17; }
#conflict-banner { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
#conflict-banner[hidden] { display: none; }

/* ---------- ボタン ---------- */
.btn {
  min-height: 44px; padding: 8px 18px; border-radius: 8px;
  border: 1px solid #8a6d2f; background: #f7efd9; color: var(--ink);
  font-weight: 600; cursor: pointer;
}
.btn:hover { background: #fff7e3; }
.btn.primary { background: var(--green); color: #fbf3dc; border-color: var(--gold); }
.btn.primary:hover { background: var(--green-2); }
.btn.ghost { background: transparent; border-color: transparent; text-decoration: underline; }
.btn.small { min-height: 36px; padding: 4px 12px; }

/* ---------- 開始画面 ---------- */
.screen[hidden] { display: none; }
.start { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 12px; padding: 16px 8px 32px; }
.crest { width: 96px; height: auto; border-radius: 8px; box-shadow: 0 4px 14px rgba(40, 25, 5, 0.35); }
.start-title { margin: 4px 0 0; font-family: "Hiragino Mincho ProN", "Yu Mincho", "YuMincho", serif; font-size: 1.3rem; letter-spacing: 0.06em; }
.lead { margin: 0; max-width: 32em; color: var(--ink-soft); }
.notice { margin: 0; max-width: 32em; padding: 8px 12px; border-radius: var(--radius); background: #fde8e4; border: 1px solid #b0412f; color: #5a160b; }
.notice[hidden] { display: none; }
.note { margin: -4px 0 0; font-size: 0.85rem; color: var(--ink-soft); }
.note[hidden] { display: none; }
.draw-choice { border: 1px solid #b59a5e; border-radius: 8px; padding: 6px 14px 10px; display: grid; gap: 2px; text-align: left; }
.draw-choice legend { padding: 0 6px; font-weight: 600; }
.draw-choice label { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.draw-choice input { width: 20px; height: 20px; accent-color: var(--green); }

/* ---------- 盤面 ---------- */
.board {
  --cw: 40px; --ch: 56px; --gap: 4px; --corner-fs: 12px;
  position: relative;
  padding: 8px 0 12px;
  border-radius: 10px;
  /* 7列の幅は JS が board の内幅から計算する。左右の余白は外枠の擬似要素で作る */
}
.play { position: relative; isolation: isolate; }
.play::before {
  content: ""; position: absolute; inset: 0 -6px; z-index: -1; border-radius: 12px;
  background:
    radial-gradient(ellipse at 50% 30%, rgba(255, 255, 255, 0.06), transparent 70%),
    repeating-linear-gradient(45deg, rgba(0, 0, 0, 0.03) 0 2px, transparent 2px 6px),
    var(--green);
  border: 1px solid var(--gold);
  box-shadow: inset 0 0 0 3px rgba(201, 165, 76, 0.18), 0 3px 10px rgba(40, 25, 5, 0.25);
}

.top-row {
  display: grid;
  grid-template-columns: repeat(7, var(--cw));
  column-gap: var(--gap);
  justify-content: space-between;
  margin-bottom: 12px;
}
/* 上段は全画面幅で1列：左から 山札・捨て札（2列分）・組札4つ。高さをそろえて段差を作らない */
.top-row .stock { grid-column: 1; grid-row: 1; align-self: start; }
.top-row .waste { grid-column: 2 / span 2; grid-row: 1; align-self: start; position: relative; height: var(--ch); }
.top-row .foundations { grid-column: 4 / span 4; grid-row: 1; align-self: start; display: grid; grid-template-columns: repeat(4, var(--cw)); column-gap: var(--gap); justify-content: space-between; }

.tableau {
  display: grid;
  grid-template-columns: repeat(7, var(--cw));
  column-gap: var(--gap);
  justify-content: space-between;
  align-items: start;
}
.column { position: relative; min-height: var(--ch); }

/* ボタン共通：配置は外側（.card / .slot / .card-pos）、揺れは内側（.card-face） */
.card, .slot {
  display: block; width: var(--cw); height: var(--ch);
  padding: 0; margin: 0; border: 0; background: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  border-radius: var(--radius);
}
.card { position: absolute; left: 0; top: 0; }
.slot { position: relative; }
.card-pos { position: absolute; inset: 0; display: block; }

.card-face {
  position: absolute; inset: 0; display: block;
  border-radius: var(--radius);
  user-select: none; -webkit-user-select: none;
}
.card-face.up {
  background: var(--card);
  border: 1px solid var(--card-edge);
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35);
}
.card-face.red { color: var(--red); }
.card-face.black { color: var(--black); }
.card-face.down {
  background: #5b1f24 url("assets/card-back.svg") center / cover no-repeat;
  border: 1px solid #2a0e10;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}
.card-face.placeholder {
  border: 1.5px dashed rgba(240, 207, 110, 0.45);
  background: rgba(0, 0, 0, 0.12);
  color: rgba(240, 207, 110, 0.55);
  display: flex; align-items: center; justify-content: center;
  font-size: calc(var(--cw) * 0.5);
}
.waste > .card-face.placeholder { right: auto; width: var(--cw); }
.card-face.placeholder.red { color: rgba(240, 150, 140, 0.55); }

.corner {
  position: absolute; left: 3px; top: 1px;
  display: flex; align-items: baseline; gap: 1px;
  font-size: var(--corner-fs); font-weight: 800; line-height: 1.15;
  letter-spacing: -0.04em;
  font-family: "Segoe UI", "Hiragino Sans", Arial, sans-serif;
}
.corner .suit { font-size: 1em; }
.pip {
  position: absolute; left: 0; right: 0; bottom: 6%;
  text-align: center; font-size: calc(var(--cw) * 0.62); line-height: 1;
}
.pip.court {
  font-size: calc(var(--cw) * 0.4); font-weight: 700;
  font-family: "Hiragino Mincho ProN", "Yu Mincho", Georgia, serif;
  bottom: 10%;
}
.pip.court::before {
  content: ""; position: absolute; left: 18%; right: 18%; top: -18%; height: 2px; background: var(--gold); opacity: 0.8;
}

.stock .count {
  position: absolute; right: 2px; bottom: 2px;
  min-width: 18px; padding: 0 3px; border-radius: 9px;
  background: rgba(0, 0, 0, 0.55); color: #fbefcf;
  font-size: 11px; line-height: 18px; text-align: center; font-variant-numeric: tabular-nums;
}
.stock.recycle .card-face { font-size: calc(var(--cw) * 0.55); color: var(--gold-bright); }

/* 選択中・移動先 */
.is-selected > .card-face, .foundation.is-selected .card-pos:last-child .card-face {
  box-shadow: 0 0 0 3px var(--gold-bright), 0 0 10px rgba(240, 207, 110, 0.7);
}
.is-target > .card-face, .foundation.is-target > .card-face:first-child {
  outline: 3px dashed var(--gold-bright); outline-offset: 1px;
}
.foundation.is-target::after {
  content: ""; position: absolute; inset: -3px; border-radius: 8px;
  border: 3px dashed var(--gold-bright); pointer-events: none;
}

/* フォーカス表示：盤面上でも見えるように明るい色 */
.board :focus-visible { outline: 3px solid #7fd3ff; outline-offset: 1px; z-index: 100; }

/* 移動不可の小さな揺れ（合計180ms） */
@keyframes shake-x {
  0% { transform: translateX(0); }
  20% { transform: translateX(-3px); }
  40% { transform: translateX(3px); }
  60% { transform: translateX(-2px); }
  80% { transform: translateX(2px); }
  100% { transform: translateX(0); }
}
@keyframes flash-outline {
  0%, 100% { box-shadow: 0 0 0 3px #e2483d; }
}
.card-face.shake { animation: shake-x 180ms linear 1; }
@media (prefers-reduced-motion: reduce) {
  .card-face.shake { animation: flash-outline 180ms steps(1, end) 1; }
}

/* ---------- 下部の操作欄 ---------- */
.toolbar {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 50;
  display: grid; grid-template-columns: repeat(4, 1fr); gap: 4px;
  padding: 6px max(6px, env(safe-area-inset-left, 0px)) calc(6px + env(safe-area-inset-bottom, 0px));
  background: rgba(43, 33, 24, 0.94);
  border-top: 1px solid var(--gold);
}
.toolbar[hidden] { display: none; }
@media (min-width: 740px) {
  .toolbar { left: 50%; right: auto; width: 720px; transform: translateX(-50%); border: 1px solid var(--gold); border-bottom: 0; border-radius: 10px 10px 0 0; }
}
.tool {
  min-height: 48px; padding: 4px 2px; border-radius: 8px;
  border: 1px solid rgba(201, 165, 76, 0.55); background: rgba(255, 255, 255, 0.06);
  color: #f6ecd2; font-size: 0.85rem; font-weight: 600; line-height: 1.2;
  display: flex; align-items: center; justify-content: center; gap: 4px; text-align: center;
  cursor: pointer;
}
.tool:disabled { opacity: 0.4; cursor: default; }
.tool.toggle input { width: 18px; height: 18px; margin: 0; accent-color: var(--gold); flex: none; }
.tool.toggle:has(input:checked) { background: rgba(201, 165, 76, 0.3); border-color: var(--gold-bright); }
.toolbar :focus-visible, .tool:focus-within { outline-color: #7fd3ff; }

/* ---------- 状態文 ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(var(--toolbar-h) + env(safe-area-inset-bottom, 0px) + 14px);
  transform: translateX(-50%);
  max-width: min(92vw, 520px); z-index: 60;
  padding: 8px 14px; border-radius: 20px;
  background: rgba(30, 22, 14, 0.92); color: #fbefcf; border: 1px solid var(--gold);
  font-size: 0.92rem; text-align: center;
  opacity: 0; pointer-events: none; transition: opacity 120ms;
}
.toast.visible { opacity: 1; }
body:not(.playing) .toast { bottom: 20px; }

/* ---------- ダイアログ ---------- */
dialog {
  width: min(92vw, 560px); max-height: 86vh;
  border: 1px solid var(--gold); border-radius: 12px; padding: 18px 18px 14px;
  background: #f8f1de; color: var(--ink);
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.45);
}
dialog::backdrop { background: rgba(20, 14, 6, 0.55); }
dialog h2 { margin: 0 0 8px; font-family: "Hiragino Mincho ProN", "Yu Mincho", serif; letter-spacing: 0.05em; }
dialog h3 { margin: 14px 0 4px; font-size: 1rem; }
dialog ul, dialog ol { padding-left: 1.3em; margin: 4px 0; }
.dialog-body { overflow-y: auto; max-height: calc(86vh - 140px); padding-right: 4px; }
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; margin-top: 14px; flex-wrap: wrap; }
.check { display: flex; align-items: center; gap: 8px; min-height: 44px; cursor: pointer; }
.check input { width: 20px; height: 20px; accent-color: var(--green); }
.result { display: grid; grid-template-columns: auto 1fr; gap: 4px 16px; margin: 8px 0; }
.result dt { color: var(--ink-soft); }
.result dd { margin: 0; font-weight: 700; font-variant-numeric: tabular-nums; }

/* ---------- フッター ---------- */
.site-footer { margin: 24px 0 12px; text-align: center; font-size: 0.85rem; color: var(--ink-soft); }
.site-footer p { margin: 2px 0; }
.small { font-size: 0.82rem; }
.noscript { padding: 16px; text-align: center; }
a { color: #6b3f12; }

/* ---------- クリアのお祝い ---------- */
.celebration-layer { position: fixed; inset: 0; width: 100vw; height: 100vh; pointer-events: none; z-index: 40; }

@keyframes king-glow {
  0% { transform: translateY(0); box-shadow: 0 1px 2px rgba(0, 0, 0, 0.35); }
  35% { transform: translateY(-10px) scale(1.06); box-shadow: 0 0 0 3px var(--gold-bright), 0 0 22px 6px rgba(240, 207, 110, 0.85); }
  100% { transform: translateY(0); box-shadow: 0 0 0 2px var(--gold), 0 0 12px 2px rgba(240, 207, 110, 0.5); }
}
.foundation.celebrate .card-pos:last-child .card-face {
  animation: king-glow 900ms ease-out var(--celebrate-delay, 0ms) both;
}
/* 光が横切るきらめき */
.foundation.celebrate .card-pos:last-child .card-face::after {
  content: ""; position: absolute; inset: 0; border-radius: inherit; pointer-events: none;
  background: linear-gradient(115deg, transparent 30%, rgba(255, 245, 200, 0.85) 48%, transparent 66%);
  background-size: 250% 100%; background-position: 120% 0;
  animation: shimmer 900ms ease-in-out calc(var(--celebrate-delay, 0ms) + 200ms) both;
}
@keyframes shimmer { to { background-position: -40% 0; } }

@keyframes felt-glow {
  0% { box-shadow: inset 0 0 0 3px rgba(201, 165, 76, 0.18), 0 3px 10px rgba(40, 25, 5, 0.25); }
  40% { box-shadow: inset 0 0 0 3px rgba(240, 207, 110, 0.6), 0 0 30px 6px rgba(240, 207, 110, 0.55); }
  100% { box-shadow: inset 0 0 0 3px rgba(201, 165, 76, 0.35), 0 0 16px 2px rgba(240, 207, 110, 0.3); }
}
.play.celebrate::before { animation: felt-glow 1600ms ease-out both; }

/* クリア画面 */
#win-dialog { text-align: center; }
#win-dialog::backdrop { background: rgba(20, 14, 6, 0.3); }
#win-dialog .result { display: inline-grid; text-align: left; }
#win-dialog .dialog-actions { justify-content: center; }
.win-crest { display: block; width: 96px; height: 96px; margin: 0 auto 4px; filter: drop-shadow(0 3px 6px rgba(60, 40, 5, 0.35)); }
.win-ornament { display: flex; align-items: center; justify-content: center; gap: 8px; margin: 2px 0 6px; color: var(--gold); font-size: 0.7rem; }
.win-ornament span { display: block; width: 60px; height: 1px; background: linear-gradient(90deg, transparent, var(--gold), transparent); }
.win-text { margin: 4px 0 8px; }
#win-message { color: var(--ink-soft); }

#win-dialog.festive[open] { animation: dialog-rise 480ms cubic-bezier(0.2, 0.8, 0.2, 1) both; }
#win-dialog.festive[open] .win-crest { animation: crest-in 900ms cubic-bezier(0.2, 0.9, 0.25, 1.2) 120ms both; }
#win-dialog.festive[open] h2 { animation: fade-up 500ms ease-out 450ms both; }
#win-dialog.festive[open] .win-ornament { animation: fade-up 500ms ease-out 600ms both; }
#win-dialog.festive[open] .win-text { animation: fade-up 500ms ease-out 750ms both; }
#win-dialog.festive[open] .result { animation: fade-up 500ms ease-out 900ms both; }
#win-dialog.festive[open] .dialog-actions { animation: fade-up 500ms ease-out 1150ms both; }
@keyframes dialog-rise { from { opacity: 0; transform: translateY(16px) scale(0.97); } to { opacity: 1; transform: none; } }
@keyframes crest-in {
  0% { opacity: 0; transform: scale(0.4) rotate(-12deg); }
  60% { opacity: 1; transform: scale(1.08) rotate(3deg); }
  100% { opacity: 1; transform: none; }
}
@keyframes fade-up { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }

@media (prefers-reduced-motion: reduce) {
  .foundation.celebrate .card-pos:last-child .card-face,
  .foundation.celebrate .card-pos:last-child .card-face::after,
  .play.celebrate::before,
  #win-dialog.festive[open], #win-dialog.festive[open] * { animation: none !important; }
}

/* ---------- スコア ---------- */
.score-box { margin: 6px auto 0; padding: 8px 12px 6px; max-width: 300px; border: 1px solid var(--gold); border-radius: 10px; background: linear-gradient(180deg, #fffaf0, #f3e7c6); }
.score-label { margin: 0; font-size: 0.85rem; color: var(--ink-soft); letter-spacing: 0.2em; }
.score-value { margin: 0; font-size: 2rem; font-weight: 800; line-height: 1.2; color: #6b4a12; font-variant-numeric: tabular-nums; font-family: "Hiragino Mincho ProN", "Yu Mincho", Georgia, serif; }
.score-detail { margin-top: 2px; font-size: 0.85rem; text-align: left; }
.score-detail summary { cursor: pointer; text-align: center; color: var(--ink-soft); min-height: 32px; line-height: 32px; }
.score-detail dl { display: grid; grid-template-columns: 1fr auto; gap: 2px 12px; margin: 4px 0; }
.score-detail dl > div { display: contents; }
.score-detail dl > div[hidden] { display: none; }
.score-detail dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
#win-dialog.festive[open] .score-box { animation: fade-up 500ms ease-out 950ms both; }
#win-dialog { overflow-y: auto; }
@media (max-height: 640px) {
  .win-crest { width: 64px; height: 64px; }
  .score-value { font-size: 1.6rem; }
  #win-dialog .result { gap: 0 16px; }
}
@media (max-width: 350px) {
  .tool { font-size: 0.78rem; gap: 3px; }
  .tool.toggle input { width: 16px; height: 16px; }
}

/* ---------- 音の設定ボタン（ヘッダー右上） ---------- */
.app-header { display: grid; grid-template-columns: 1fr auto; grid-template-areas: "title sound" "info info"; align-items: center; column-gap: 8px; row-gap: 2px; }
.app-header .title { grid-area: title; }
.app-header .sound-btn { grid-area: sound; }
.app-header .game-info { grid-area: info; }
@media (min-width: 600px) {
  .app-header { grid-template-columns: 1fr auto auto; grid-template-areas: "title info sound"; }
}
.sound-btn {
  min-height: 44px; min-width: 44px; padding: 4px 12px; border-radius: 22px;
  border: 1px solid #8a6d2f; background: #f7efd9; color: var(--ink-soft);
  font-size: 0.85rem; font-weight: 700; cursor: pointer; display: inline-flex; align-items: center; gap: 4px;
}
.sound-btn span[aria-hidden] { font-size: 1.1rem; line-height: 1; }
.sound-btn.on { background: var(--green); color: #fbf3dc; border-color: var(--gold); }
@media (max-width: 350px) {
  .toolbar { grid-template-columns: 0.85fr 1.2fr 1.15fr 0.8fr; }
}

/* ---------- 手詰まりのお知らせ ---------- */
.banner.dead { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; background: #efe6f3; border: 1px solid #6a4a7e; color: #2e1d3a; }
.banner.dead[hidden] { display: none; }
.banner-actions { display: flex; gap: 6px; margin-left: auto; }

.banner.dead-below { margin: 12px 0 0; }
.page { position: relative; }
/* 大きな画面：盤面を広げてカードを大きく（高さが足りなければ render.js が縮める） */
@media (min-width: 1100px) {
  .page { max-width: 940px; }
  .toolbar { width: 940px; }
}
/* スマホ幅：左右の余白を詰めて盤面（カード）を広く使う */
@media (max-width: 520px) {
  .page { padding: 0 6px; }
  .play::before { inset: 0 -4px; }
}
.banner.dead-below { scroll-margin-bottom: calc(var(--toolbar-h) + 24px); }
.banner.dead.overlay { position: absolute; margin: 0; z-index: 30; box-shadow: 0 4px 16px rgba(0, 0, 0, 0.35); }
