/* ============================================================
   花札 こいこい - Hanafuda Koikoi
   ============================================================ */

:root {
  --felt-1: #2e7d4f;
  --felt-2: #1b5e35;
  --felt-3: #123f24;
  --ink: #241d18;
  --gold: #e0a72e;
  --gold-lt: #f3d488;
  --red: #c8392f;
  --paper: #f7efd8;
  --ui-font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, "Hiragino Kaku Gothic ProN", "Hiragino Sans", Meiryo, sans-serif;
  --cw: 46px;
  --ad-h: 0px;
  --ad-space: 0px;
}

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

html, body {
  height: 100%;
  overscroll-behavior: none;
  touch-action: manipulation;
}

body {
  font-family: var(--ui-font);
  background: var(--felt-3);
  color: #f4f2e8;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
}

#app {
  display: flex;
  flex-direction: column;
  height: 100%;
  height: calc(100dvh - var(--sgh-top, 0px) - var(--ad-space, 0px));
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.hidden { display: none !important; }

/* ============ ヘッダー ============ */

#topbar {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  /* 上のセーフエリアは共通ヘッダー（--sgh-top）が持つので、ここでは足さない */
  padding: 6px 12px;
  background: rgba(0, 0, 0, 0.26);
}

#logo-wrap {
  display: flex;
  align-items: baseline;
  gap: 9px;
  min-width: 0;
}

#logo {
  font-size: 17px;
  font-weight: 800;
  letter-spacing: 0.04em;
  color: #fff;
  white-space: nowrap;
}

#logo::before { content: "🎴"; margin-right: 5px; }

#round-info {
  font-size: 12px;
  color: rgba(255, 255, 255, 0.75);
  white-space: nowrap;
}

#topbar-buttons { display: flex; gap: 5px; flex: none; }

.icon-btn {
  width: 34px;
  height: 31px;
  border: none;
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.12);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
  color: #fff;
  font-family: var(--ui-font);
  transition: background 0.15s ease;
}

.icon-btn:hover { background: rgba(255, 255, 255, 0.22); }
.icon-btn:active { transform: translateY(1px); }
.icon-btn.off { opacity: 0.45; }

/* ============ 盤面 ============ */

#table {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 6px 6px 8px;
  overflow: hidden;
  background: radial-gradient(ellipse 95% 78% at 50% 45%, var(--felt-1) 0%, var(--felt-2) 60%, var(--felt-3) 100%);
}

#table::before {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(45deg, transparent 0 3px, rgba(0, 0, 0, 0.025) 3px 4px);
  pointer-events: none;
}

/* ---- プレイヤー情報 ---- */

.plate {
  flex: none;
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 100%;
  padding: 3px 10px 3px 4px;
  border-radius: 18px;
  background: rgba(0, 0, 0, 0.34);
  border: 2px solid transparent;
  transition: border-color 0.25s ease, box-shadow 0.25s ease;
  white-space: nowrap;
  z-index: 4;
}

.plate.turn {
  border-color: var(--gold);
  box-shadow: 0 0 12px rgba(224, 167, 46, 0.5);
}

.p-avatar {
  font-size: 15px;
  line-height: 1;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.14);
  display: flex;
  align-items: center;
  justify-content: center;
  flex: none;
}

.p-name { font-size: 11px; font-weight: 700; flex: none; }
.p-score { font-size: 11px; color: var(--gold-lt); font-weight: 700; flex: none; }

.plate.oya .p-name::after {
  content: "Dealer";
  margin-left: 4px;
  font-size: 9px;
  background: var(--gold);
  color: #3d2c07;
  border-radius: 4px;
  padding: 1px 3px;
  vertical-align: 1px;
}

.plate.koikoi::after {
  content: "Koi-Koi";
  font-size: 9px;
  font-weight: 700;
  color: #ffe6a8;
  background: rgba(200, 57, 47, 0.85);
  border-radius: 8px;
  padding: 2px 6px;
  flex: none;
}

.p-left {
  display: none;
  font-size: 10px;
  color: rgba(255, 255, 255, 0.7);
  flex: none;
}

.p-yaku {
  font-size: 10px;
  color: rgba(255, 255, 255, 0.6);
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

.p-yaku.on { color: #ffe6a8; font-weight: 700; }
.p-yaku:empty { display: none; }

/* ---- 取り札 ---- */

.capt {
  flex: none;
  width: 100%;
  min-height: calc(var(--cw) * 0.74);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 0 4px;
  overflow-x: auto;
  overflow-y: hidden;
  scrollbar-width: none;
  z-index: 3;
}

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

.capt-group {
  position: relative;
  display: flex;
  align-items: center;
  flex: none;
  padding: 2px 4px 2px 2px;
  border-radius: 6px;
  background: rgba(0, 0, 0, 0.18);
}

.capt-group .card.mini {
  width: calc(var(--cw) * 0.44);
  height: calc(var(--cw) * 0.704);
  margin-right: calc(var(--cw) * -0.27);
  flex: none;
}

.capt-group .card.mini:last-of-type { margin-right: 0; }

.capt-badge {
  font-size: 9px;
  font-weight: 700;
  color: #fff;
  background: rgba(0, 0, 0, 0.5);
  border-radius: 7px;
  padding: 1px 5px;
  margin-left: 5px;
  flex: none;
}

.capt-group.k-hikari { background: rgba(224, 167, 46, 0.3); }
.capt-group.k-tane { background: rgba(200, 57, 47, 0.24); }
.capt-group.k-tan { background: rgba(63, 111, 181, 0.28); }

/* ---- 手札 ---- */

.hand {
  flex: none;
  display: flex;
  align-items: flex-end;
  justify-content: center;
  gap: 3px;
  height: calc(var(--cw) * 1.6 + 10px);
  padding-top: 10px;
  z-index: 2;
}

.hand-cpu { height: calc(var(--cw) * 1.6 * 0.62); align-items: flex-start; padding-top: 0; }

.hand-cpu .card {
  width: calc(var(--cw) * 0.62);
  height: calc(var(--cw) * 0.992);
}

/* 幅が足りないときは手札を少しだけ重ねる（--hand-ov は 0 以下の値） */
.hand-me .card { margin-right: var(--hand-ov, 0px); }
.hand-me .card:last-child { margin-right: 0; }

/* 選んでいる札・カーソルのある札は手前に出して全部見えるようにする */
.hand-me .card.selected,
.hand-me .card.kbd { position: relative; z-index: 3; }

/* ---- 場・山札 ---- */

#center {
  position: relative;
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  /* 下は #msg のぶんを空けておく（場札と重ならないように） */
  padding: 2px 0 20px;
}

#deck-wrap { flex: none; }

#deck {
  position: relative;
  width: var(--cw);
  height: calc(var(--cw) * 1.6);
  border-radius: 7%;
  background:
    repeating-linear-gradient(120deg, rgba(255, 255, 255, 0.06) 0 3px, transparent 3px 6px),
    linear-gradient(150deg, #96302a, #7d1f1a 60%, #5c1512);
  border: 2px solid #3a1210;
  box-shadow: 0 2px 0 #5c1512, 0 4px 0 #4a1210, 0 6px 10px rgba(0, 0, 0, 0.4);
  display: flex;
  align-items: flex-end;
  justify-content: center;
  padding-bottom: 4px;
}

#deck.empty { opacity: 0.28; box-shadow: none; }

#deck-count {
  font-size: 10px;
  font-weight: 700;
  color: #ffe6a8;
  background: rgba(0, 0, 0, 0.45);
  border-radius: 8px;
  padding: 1px 6px;
}

#field {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 4px;
  max-height: 100%;
  /* 場札が想定より増えても盤面を壊さないための逃げ道 */
  overflow-y: auto;
  scrollbar-width: none;
}

#field::-webkit-scrollbar { display: none; }

/* ---- 札 ---- */

.card {
  width: var(--cw);
  height: calc(var(--cw) * 1.6);
  flex: none;
  border-radius: 7%;
  line-height: 0;
  transition: transform 0.16s ease, filter 0.16s ease, box-shadow 0.16s ease;
}

.card img {
  width: 100%;
  height: 100%;
  display: block;
  border-radius: 7%;
  pointer-events: none;
}

.card.in-hand.playable { cursor: pointer; }
.card.in-hand.has-match img { box-shadow: 0 0 0 2px rgba(224, 167, 46, 0.9); }
.card.in-hand.selected { transform: translateY(-10px); }
.card.in-hand.selected img { box-shadow: 0 0 0 3px var(--gold), 0 6px 12px rgba(0, 0, 0, 0.45); }
.card.in-hand.kbd img { box-shadow: 0 0 0 3px #7ec8ff; }

.card.on-field.pickable {
  cursor: pointer;
  animation: pickPulse 1.1s ease-in-out infinite;
}
.card.on-field.pickable img { box-shadow: 0 0 0 3px var(--gold), 0 0 14px rgba(224, 167, 46, 0.85); }
.card.on-field.dim img { filter: brightness(0.55) saturate(0.65); }
.card.on-field.hint img { box-shadow: 0 0 0 2px rgba(255, 255, 255, 0.8); }

@keyframes pickPulse {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.card.mini img { box-shadow: 0 1px 2px rgba(0, 0, 0, 0.4); }

/* 飛んでいく札 */

#fly-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 60;
}

.card.flying {
  position: absolute;
  left: 0;
  top: 0;
  will-change: transform;
  filter: drop-shadow(0 6px 10px rgba(0, 0, 0, 0.45));
}

.card.flying.revealed img { box-shadow: 0 0 0 2px var(--gold-lt); }
.card.flying.pulse { animation: pickPulse 1.1s ease-in-out infinite; }

/* ---- メッセージ・トースト ---- */

/* 場の下端に貼り付ける（#center が基準） */
#msg {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translateX(-50%);
  max-width: 96%;
  text-align: center;
  font-size: 12px;
  font-weight: 600;
  background: rgba(0, 0, 0, 0.55);
  padding: 5px 14px;
  border-radius: 14px;
  z-index: 50;
  pointer-events: none;
}

#msg:empty { display: none; }

#toast {
  position: absolute;
  top: 34%;
  left: 50%;
  transform: translateX(-50%);
  max-width: 92%;
  text-align: center;
  background: rgba(0, 0, 0, 0.78);
  color: #ffe6a8;
  font-size: 14px;
  font-weight: 700;
  padding: 9px 20px;
  border-radius: 20px;
  z-index: 70;
  pointer-events: none;
  transition: opacity 0.25s ease;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

/* display:none にせず opacity で消す */
#toast.hidden { opacity: 0; display: block !important; }

/* ============ モーダル ============ */

.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.58);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  padding: 16px;
  animation: fadeIn 0.18s ease;
}

@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }

.modal-box {
  background: linear-gradient(170deg, #fdfaf0 0%, #f0ead8 100%);
  color: var(--ink);
  border-radius: 16px;
  padding: 20px 22px;
  width: min(430px, 100%);
  max-height: min(82dvh, 680px);
  overflow-y: auto;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.5);
  animation: popIn 0.24s cubic-bezier(0.3, 1.3, 0.5, 1);
}

@keyframes popIn {
  from { transform: scale(0.93); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-box h2 {
  font-size: 18px;
  margin-bottom: 12px;
  text-align: center;
}

.yaku-box { margin-bottom: 4px; }

.yaku-row {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  gap: 10px;
  padding: 5px 4px;
  border-bottom: 1px solid #e2dcc8;
  font-size: 14px;
}

.yaku-row .yn { font-weight: 700; }
.yaku-row .yp { color: #9c2a22; font-weight: 700; white-space: nowrap; }

.yaku-row.total {
  border-bottom: none;
  border-top: 2px solid #d8d0b6;
  margin-top: 4px;
  font-size: 15px;
}

.round-banner {
  text-align: center;
  font-size: 16px;
  font-weight: 800;
  border-radius: 10px;
  padding: 9px;
  margin-bottom: 12px;
}

.round-banner.win { color: #6d4c00; background: linear-gradient(160deg, #fff3cd, #ffe08a); }
.round-banner.lose { color: #5a2a26; background: linear-gradient(160deg, #f6dcd8, #eebfb8); }
.round-banner.draw { color: #444; background: #e6e2d4; }

.round-total {
  text-align: center;
  font-size: 14px;
  font-weight: 700;
  margin-top: 12px;
}

.round-note {
  text-align: center;
  font-size: 12px;
  line-height: 1.7;
  color: #6f6a5c;
  margin-top: 8px;
}

.round-note.center { color: #444; font-size: 13px; }

#over-title.win { color: #b8860b; }
#over-title.lose { color: #8a3a32; }

.modal-actions {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin-top: 16px;
}

.primary-btn, .ghost-btn {
  border: none;
  border-radius: 10px;
  padding: 10px 20px;
  font-size: 14px;
  font-weight: 700;
  font-family: var(--ui-font);
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.15s ease, background 0.15s ease;
}

.primary-btn {
  background: linear-gradient(160deg, var(--gold-lt) 0%, var(--gold) 55%, #cfa64e 100%);
  color: #3d2c07;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.28);
}

.ghost-btn {
  background: #e4dec9;
  color: #4a4232;
}

.primary-btn:disabled, .ghost-btn:disabled { opacity: 0.4; cursor: default; }
.primary-btn:not(:disabled):hover, .ghost-btn:not(:disabled):hover { transform: translateY(-1px); }
.primary-btn:not(:disabled):active, .ghost-btn:not(:disabled):active { transform: translateY(1px); }

/* ---- 遊び方 ---- */

.help-box { text-align: left; }

.help-box h3 {
  font-size: 14px;
  margin: 16px 0 6px;
  color: #1b5e35;
  border-left: 4px solid var(--gold);
  padding-left: 7px;
}

.help-box p, .help-box li {
  font-size: 13px;
  line-height: 1.75;
  color: #3b352b;
}

.help-box ul, .help-box ol { padding-left: 20px; margin: 4px 0; }

.yaku-table {
  width: 100%;
  border-collapse: collapse;
  margin-top: 6px;
}

.yaku-table th, .yaku-table td {
  border-bottom: 1px solid #e2dcc8;
  padding: 5px 4px;
  font-size: 12px;
  text-align: left;
  vertical-align: top;
  line-height: 1.5;
}

.yaku-table th { white-space: nowrap; color: #1b5e35; }
.yaku-table td:first-of-type { white-space: nowrap; color: #9c2a22; font-weight: 700; }
.yaku-table td:last-child { color: #6f6a5c; }
.yaku-table th small { font-weight: 400; color: #6f6a5c; font-size: .85em; }

.card-chart { display: grid; gap: 4px; margin: 8px 0 4px; }
.card-chart .cc-row { display: grid; grid-template-columns: 74px repeat(4, minmax(0, 1fr)); gap: 4px; align-items: center; }
.card-chart .cc-month { font-size: 11px; line-height: 1.25; font-weight: 700; color: #1b5e35; }
.card-chart .cc-month small { display: block; font-weight: 400; color: #6f6a5c; }
.card-chart img { width: 100%; max-width: 46px; height: auto; display: block; justify-self: center; }

/* ---- 設定 ---- */

.conf-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 9px 2px;
  border-bottom: 1px solid #e2dcc8;
  font-size: 14px;
}

.conf-row label { font-weight: 700; }

.conf-row select {
  font-family: var(--ui-font);
  font-size: 14px;
  padding: 6px 8px;
  border-radius: 8px;
  border: 1px solid #cdc5ad;
  background: #fff;
  color: var(--ink);
}

.conf-row input[type="checkbox"] {
  width: 22px;
  height: 22px;
  accent-color: #1b5e35;
}

/* ============ フッター ============ */

#footer {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 12px calc(5px + env(safe-area-inset-bottom));
  background: rgba(0, 0, 0, 0.3);
  font-size: 10px;
  color: rgba(255, 255, 255, 0.55);
}

/* 下部広告があるときは、下のセーフエリアを広告枠（--ad-space）が持つので足さない */
html.has-ad-banner #footer { padding-bottom: 5px; }

/* 右の著作表記（もう一方のサイトへのリンクつき）が 1 行に収まらない幅では、右側だけを 2 行に折る（左のタイトルは折らない） */
#footer > div:first-child { flex: none; white-space: nowrap; }
#footer > div:last-child { text-align: right; }

#footer a { color: rgba(255, 255, 255, 0.75); text-decoration: none; }
#footer a:hover { text-decoration: underline; }

/* ============ レスポンシブ・その他 ============ */

@media (hover: hover) {
  .card.in-hand.playable:hover { transform: translateY(-8px); position: relative; z-index: 4; }
  .card.on-field.pickable:hover { transform: translateY(-6px); }
}

@media (prefers-reduced-motion: reduce) {
  .card, .modal, .modal-box, .card.flying { transition: none !important; animation: none !important; }
}

/* 横向きで背が低いとき（スマホ横持ち）は、名前プレートと取り札を同じ段に並べて
   縦の余裕を作る。段組みが変わるだけで DOM は同じ */
@media (orientation: landscape) and (max-height: 560px) {
  #table {
    display: grid;
    grid-template-columns: auto minmax(0, 1fr);
    grid-template-rows: auto auto minmax(0, 1fr) auto auto;
    align-items: center;
    justify-items: center;
    gap: 2px 6px;
    padding: 4px 6px 6px;
  }
  #plate-1 { grid-column: 1; grid-row: 1; }
  #capt-1 { grid-column: 2; grid-row: 1; justify-content: flex-start; }
  #hand-1 { grid-column: 1 / 3; grid-row: 2; }
  #center { grid-column: 1 / 3; grid-row: 3; }
  #hand-0 { grid-column: 1 / 3; grid-row: 4; }
  #plate-0 { grid-column: 1; grid-row: 5; }
  #capt-0 { grid-column: 2; grid-row: 5; justify-content: flex-start; }

  .capt { min-height: 0; }
  .hand { padding-top: 5px; height: calc(var(--cw) * 1.6 + 5px); }
  #msg { font-size: 11px; padding: 3px 10px; }

  /* 相手の手札は裏向きで情報が無いので、枚数だけにして縦を空ける */
  #hand-1 { display: none; }
  .p-left { display: inline; }

  #topbar { padding: 3px 10px; }
  .icon-btn { width: 30px; height: 26px; font-size: 13px; }
  #logo { font-size: 15px; }
  #footer { font-size: 9px; padding: 2px 10px calc(2px + env(safe-area-inset-bottom)); }
  html.has-ad-banner #footer { padding-bottom: 2px; }

  /* 背が低いので、モーダルは共通ヘッダーと下部広告のあいだに収めて中をスクロールさせる
     （画面全体の中央に置くと、こいこい／あがり・次の回戦へのボタンが広告の下に隠れて押せない） */
  .modal { top: var(--sgh-top, 0px); bottom: var(--ad-space, 0px); padding: 8px 16px; }
  .modal-box { max-height: 100%; }
}

@media (min-width: 700px) {
  #logo { font-size: 20px; }
  #round-info { font-size: 13px; }
  .p-name, .p-score { font-size: 12px; }
  .p-yaku { font-size: 11px; }
  #msg { font-size: 13px; }
  #table { gap: 6px; padding: 10px 10px 12px; }
}

/* ==========================================================================
   フッター固定バナー広告（スマホ表示時のみ / js/ad-banner.js が挿入する）
   --ad-h    : 広告そのものの高さ。広告なしのときは 0px
   --ad-space: レイアウトが空けるべき高さ（広告 + セーフエリア）
   ========================================================================== */
html.has-ad-banner {
  --ad-space: calc(var(--ad-h, 0px) + env(safe-area-inset-bottom, 0px));
}

.ad-banner {
  /* height は広告そのものの高さ。セーフエリアぶんの padding は外側に足す */
  box-sizing: content-box;
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  /* リザルト等の全画面オーバーレイ（2147483000）よりは下に置く */
  z-index: 2147482000;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  height: var(--ad-h, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.86);
  /* border ではなく inset shadow にして、枠線ぶん高さがずれないようにする */
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.14);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
}

.ad-banner__frame {
  display: block;
  width: 100%;
  height: var(--ad-h, 0px);
  border: 0;
  color-scheme: light;
}

@media print {
  .ad-banner { display: none; }
}
