/*
 * シミュレーション結果ビューアー（リポジトリ直下の simulation_viewer.html。S3 の htmls/ にそのまま置く静的ページ）。
 * naga.css の上に重ねる。sv- はこのページだけの部品。
 *  - 2件以上: 比較の表。JS が画面の下まで高さを決める枠（.sv-board）の中で縦横にスクロールし、
 *    手牌の行（どの列がどの打牌か）と左の見出し列が貼り付く。
 *  - 1件: 条件を 1 枚のカード（.sv-cond）にして、結果の表と並べる（992px 未満は縦に積み、ページごとスクロール）。
 *    スマホ幅（JS の bs_size が 0・1）は 1 件だけ読み込むので、いつもこの形になる。
 */

.sv-page {
  /* 表の面。貼り付くセルは下が透けないよう不透明の色で塗る */
  --sv-bg: #0a1233;
  --sv-bg-head: #0e1842;
  --sv-bg-label: #0c1539;
  --sv-bg-label-head: #101b48;
  --sv-bg-hover: #141b45;
  --sv-bg-label-hover: #182051;
  --sv-label-w: 196px;
  --sv-col-w: 368px;
  --sv-hai: 22px;
  --sv-i-doc: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 48 48' fill='none' stroke='%23000' stroke-width='3.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M13 6h15l8 8v28H13z'/%3E%3Cpath d='M28 6v8h8M19 23h11M19 29h11M19 35h7'/%3E%3C/svg%3E");
  --sv-i-ext: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24' fill='none' stroke='%23000' stroke-width='2.4' stroke-linecap='round' stroke-linejoin='round'%3E%3Cpath d='M14 4h6v6M20 4l-8.5 8.5M18 14v4.5a1.5 1.5 0 0 1-1.5 1.5h-11A1.5 1.5 0 0 1 4 18.5v-11A1.5 1.5 0 0 1 5.5 6H10'/%3E%3C/svg%3E");
}

/* ---- ヘッダー・ページの枠 ---------------------------------------------------------------- */
.sv-back {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  min-height: 44px;
  padding: 0 14px 0 9px;
  border-radius: var(--radius-s);
  color: var(--muted);
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 0.04em;
  text-decoration: none;
  white-space: nowrap;
  transition: color 0.15s, background-color 0.15s;
}
.sv-back:hover, .sv-back:focus-visible { color: #fff; background: rgba(255, 255, 255, 0.06); }
.sv-back svg { flex: none; transition: transform 0.2s var(--ease-out); }
.sv-back:hover svg { transform: translateX(-2px); }

.sv-main.n-main { padding-top: 22px; padding-bottom: 24px; }
.sv-wrap { padding-left: clamp(16px, 3vw, 32px); padding-right: clamp(16px, 3vw, 32px); }
.sv-head.n-page-head { margin-bottom: 18px; }
/* 題は「シミュレーション / 結果」の所だけで折る */
.sv-title { word-break: keep-all; overflow-wrap: anywhere; }
#app { container-type: inline-size; }

/* ---- 表の枠（縦横にスクロールする面） ---------------------------------------------------- */
.sv-board {
  position: relative;
  width: fit-content;
  max-width: 100%;
  min-height: 360px;
  overflow: auto;
  overscroll-behavior: contain;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-s);
  background: var(--sv-bg);
  box-shadow: var(--highlight), var(--shadow);
}
/* 2・3件の比較は列を広げて枠いっぱいに（4件以上は横にスクロール） */
@supports (width: 1cqw) {
  .sv-layout.is-n2 { --sv-col-w: max(336px, calc((100cqw - var(--sv-label-w) - 16px) / 2)); }
  .sv-layout.is-n3 { --sv-col-w: max(336px, calc((100cqw - var(--sv-label-w) - 16px) / 3)); }
  .sv-layout.is-n2 .sv-board, .sv-layout.is-n3 .sv-board { width: 100%; }
}

.sv-table {
  width: max-content;
  min-width: 100%;
  border-collapse: separate;
  border-spacing: 0;
  color: var(--text);
  font-size: 14px;
}
.sv-table th, .sv-table td {
  padding: 10px 12px;
  border-bottom: 1px solid var(--line);
  background-color: var(--sv-bg);
  text-align: center;
  vertical-align: middle;
  transition: background-color 0.12s;
}
.sv-table td, .sv-table th.sv-head { width: var(--sv-col-w); min-width: var(--sv-col-w); }
.sv-table td + td, .sv-table .sv-head + .sv-head { border-left: 1px solid rgba(160, 185, 255, 0.08); }

/* 左の見出し列（横にスクロールしても残る） */
.sv-table .sv-label {
  position: sticky;
  left: 0;
  z-index: 2;
  width: var(--sv-label-w);
  min-width: var(--sv-label-w);
  max-width: var(--sv-label-w);
  padding-left: 18px;
  padding-right: 14px;
  border-right: 1px solid var(--line-strong);
  background-color: var(--sv-bg-label);
  box-shadow: 10px 0 14px -12px rgba(0, 0, 0, 0.75);
  color: var(--muted);
  font-size: 13.5px;
  font-weight: 700;
  line-height: 1.45;
  letter-spacing: 0.03em;
  text-align: left;
  /* 語の途中で折り返さない（折り返す位置はテンプレートの <wbr>。入り切らない語だけ途中で折る） */
  word-break: keep-all;
  overflow-wrap: anywhere;
  text-wrap: balance;
}

/* 条件の行（手牌・打牌・局情報・点数・タイプ・シミュレーション数） */
.sv-table thead th, .sv-table thead td { background-color: var(--sv-bg-head); }
.sv-table thead .sv-label { background-color: var(--sv-bg-label-head); }
.sv-table thead tr.sv-row-last > * { border-bottom: 1px solid rgba(181, 140, 255, 0.45); }

/* 手牌の行は上に貼り付く（どの列がどの打牌かを見失わない） */
.sv-table .sv-row-hand > * {
  position: sticky;
  top: 0;
  z-index: 3;
  padding-top: 30px;
  padding-bottom: 14px;
  border-bottom-color: var(--line-strong);
  box-shadow: 0 12px 18px -14px rgba(0, 0, 0, 0.85);
}
.sv-table .sv-row-hand > .sv-label { z-index: 4; padding-bottom: 18px; vertical-align: bottom; box-shadow: 10px 0 14px -12px rgba(0, 0, 0, 0.75), 0 12px 18px -14px rgba(0, 0, 0, 0.85); }

/* 結果の行 */
.sv-table tbody tr.sv-sep > * { border-bottom-color: rgba(181, 140, 255, 0.3); }
@media (hover: hover) { .sv-table tbody tr:hover > * { background-color: var(--sv-bg-hover); } }
@media (hover: hover) { .sv-table tbody tr:hover > .sv-label { background-color: var(--sv-bg-label-hover); color: #fff; } }
.sv-table tbody tr:last-child > * { border-bottom: 0; }

/* ---- 値 ------------------------------------------------------------------------------------ */
.sv-v {
  display: inline-flex;
  align-items: baseline;
  justify-content: center;
  color: #fff;
  font-size: 16.5px;
  font-weight: 600;
  line-height: 1.55;
  white-space: nowrap;
}
.sv-v small { margin-left: 2px; color: var(--muted); font-family: var(--font-ui); font-size: 11.5px; font-weight: 700; letter-spacing: 0.02em; }
.sv-ci { margin-top: 1px; color: var(--faint); font-size: 12px; font-weight: 500; line-height: 1.45; white-space: nowrap; }
.sv-rate { display: inline-flex; align-items: center; justify-content: center; gap: 10px; }

/* 比較でいちばん良い値（表示している桁で比べる）: 角 2px の札と、左上の小さなひし形 */
.is-best .sv-v {
  position: relative;
  padding: 0 10px;
  border-radius: var(--radius-xs);
  background: rgba(181, 140, 255, 0.16);
  box-shadow: inset 0 0 0 1px rgba(214, 190, 255, 0.55);
}
.is-best .sv-v::before { content: ""; position: absolute; left: -3px; top: -3px; width: 6px; height: 6px; background: var(--violet-hi); transform: rotate(45deg); }
.is-best .sv-v small { color: var(--violet-text); }

/* まだ結果がない列（読み込み中・結果ファイルがない）は値の代わりに線を引く。出なかった役は控えめに */
.sv-table td.is-empty > * { display: none; }
.sv-table td.is-empty::before { content: "—"; color: var(--faint); font-family: var(--font-latin); font-size: 15px; }
.is-zero .sv-v { color: var(--faint); font-weight: 500; }
.sv-none { margin: 0; color: var(--faint); font-family: var(--font-latin); font-weight: 600; }

/* 牌譜一覧を開く件数 */
.sv-count {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-width: 34px;
  height: 26px;
  padding: 0 9px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: transparent;
  color: var(--faint);
  font-size: 12.5px;
  font-weight: 600;
  line-height: 1;
}
button.sv-count {
  padding-left: 7px;
  border-color: rgba(181, 140, 255, 0.4);
  background: rgba(181, 140, 255, 0.08);
  color: var(--violet-text);
  cursor: pointer;
  transition: color 0.15s, background-color 0.15s, border-color 0.15s, transform 0.15s var(--ease-out);
}
button.sv-count::before { content: ""; flex: none; width: 12px; height: 12px; background: currentColor; -webkit-mask: var(--sv-i-doc) center / contain no-repeat; mask: var(--sv-i-doc) center / contain no-repeat; opacity: 0.85; }
button.sv-count::after { content: ""; position: absolute; inset: -9px -5px; }
@media (hover: hover) { button.sv-count:hover { color: #fff; border-color: var(--violet-hi); background: rgba(181, 140, 255, 0.22); } }
@media (hover: none) { button.sv-count:active { color: #fff; border-color: var(--violet-hi); background: rgba(181, 140, 255, 0.22); } }
button.sv-count:active { transform: translateY(1px); }

/* 順位の割合の帯（0〜100%。数値はすぐ上に出している） */
.sv-bar { width: min(168px, 76%); height: 4px; margin: 7px auto 1px; overflow: hidden; background: rgba(255, 255, 255, 0.07); }
.sv-bar > i { display: block; height: 100%; background: var(--violet-lo); }
.is-best .sv-bar > i { background: var(--violet-hi); }

/* ---- 条件 --------------------------------------------------------------------------------- */
.sv-hand { display: flex; justify-content: center; gap: 2px; }
.sv-hai { position: relative; flex: none; width: var(--sv-hai); }
/* 比較の列が広いとき（2件など）は牌も大きく */
.sv-table .sv-hai { width: clamp(20px, calc((var(--sv-col-w) - 64px) / 14), 30px); }
.sv-hai img, .sv-tile {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 3px;
  filter: drop-shadow(0 2px 0 #1b2a6b) drop-shadow(0 3px 5px rgba(0, 0, 0, 0.45));
}
/* 打牌に選んだ牌を持ち上げて、下に紫の小さなひし形 */
.sv-hai.is-cut { transform: translateY(-7px); }
.sv-hai.is-cut img { outline: 1px solid var(--violet-hi); outline-offset: 1px; }
.sv-hai.is-cut::after { content: ""; position: absolute; left: 50%; bottom: -10px; width: 5px; height: 5px; margin-left: -2.5px; background: var(--violet-hi); transform: rotate(45deg); }
/* 手牌がまだ分からないとき（読み込み中など）は伏せた牌 */
.sv-hai.is-back, .sv-tile.is-back { aspect-ratio: 66 / 90; border-radius: 3px; background: linear-gradient(180deg, #2a3f9a, #13235e 72%); box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.22), inset 0 0 0 1px rgba(160, 185, 255, 0.18), 0 2px 0 #0a1442; opacity: 0.55; }
.sv-hai__reach {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 4px);
  transform: translateX(-50%);
  padding: 0 5px;
  border-radius: var(--radius-xs);
  background: #7c4ee4;
  box-shadow: inset 0 0 0 1px rgba(214, 196, 255, 0.35);
  color: #fff;
  font-size: 10px;
  font-weight: 900;
  line-height: 1.6;
  letter-spacing: 0.04em;
  white-space: nowrap;
}
.sv-tile { width: 24px; }
.sv-tile--lg { width: 30px; }

.sv-dahai { display: inline-flex; align-items: center; gap: 10px; }
.sv-reach {
  display: inline-flex;
  align-items: center;
  height: 24px;
  padding: 0 9px;
  border: 1px solid rgba(181, 140, 255, 0.5);
  border-radius: var(--radius-xs);
  background: rgba(181, 140, 255, 0.1);
  color: #fff;
  font-size: 12.5px;
  font-weight: 900;
  letter-spacing: 0.1em;
}

.sv-kyoku-row { display: flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px 20px; }
.sv-kyoku { display: inline-flex; flex-wrap: wrap; align-items: baseline; gap: 2px 8px; margin: 0; color: var(--muted); font-size: 13px; font-weight: 700; }
.sv-kyoku b { color: #fff; font-size: 16px; font-weight: 900; letter-spacing: 0.04em; }
.sv-kyoku .n-num { margin-right: 1px; color: var(--text); font-size: 14px; font-weight: 600; }
.sv-dora { display: inline-flex; align-items: center; gap: 8px; margin: 0; }
.sv-k { color: var(--faint); font-size: 12px; font-weight: 700; letter-spacing: 0.06em; white-space: nowrap; }

.sv-scores { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 6px; max-width: 336px; margin: 0 auto; }
.sv-seat { display: grid; justify-items: center; padding: 3px 0 4px; border-radius: var(--radius-xs); background: rgba(255, 255, 255, 0.035); line-height: 1.35; }
.sv-seat small { color: var(--faint); font-size: 11px; font-weight: 700; }
.sv-seat .n-num { color: var(--muted); font-size: 14.5px; font-weight: 600; }
.sv-seat.is-self { background: rgba(181, 140, 255, 0.08); box-shadow: inset 0 0 0 1px rgba(181, 140, 255, 0.38), inset 0 2px 0 var(--violet-hi); }
.sv-seat.is-self small { color: var(--violet-text); }
.sv-seat.is-self .n-num { color: #fff; }

.sv-vs { display: inline-flex; flex-wrap: wrap; align-items: center; justify-content: center; gap: 6px 18px; }
.sv-vs__item { display: inline-flex; align-items: center; gap: 7px; }

/* シミュレーション数（押すと全部の牌譜）。押せるときはボタンと同じく右上と左下の角を落とす */
.sv-total {
  --cut: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  min-width: 96px;
  min-height: 40px;
  padding: 0 16px 0 13px;
  border: 1px solid var(--line);
  border-radius: var(--radius-s);
  background: transparent;
  color: var(--faint);
  font-size: 17px;
  font-weight: 700;
  line-height: 1;
}
@supports (corner-shape: bevel) { button.sv-total { border-radius: 0 var(--cut) 0 var(--cut); corner-shape: bevel; } }
button.sv-total {
  border-color: rgba(181, 140, 255, 0.5);
  background: rgba(181, 140, 255, 0.1);
  box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.08);
  color: #fff;
  cursor: pointer;
  transition: background-color 0.15s, border-color 0.15s, box-shadow 0.2s, transform 0.15s var(--ease-out);
}
button.sv-total::before { content: ""; flex: none; width: 16px; height: 16px; background: var(--violet-text); -webkit-mask: var(--sv-i-doc) center / contain no-repeat; mask: var(--sv-i-doc) center / contain no-repeat; }
@media (hover: hover) { button.sv-total:hover { border-color: var(--violet-hi); background: rgba(181, 140, 255, 0.2); } }
@media (hover: none) { button.sv-total:active { border-color: var(--violet-hi); background: rgba(181, 140, 255, 0.2); } }
button.sv-total:active { transform: translateY(1px); }

/* ---- 1件: 条件のカード ------------------------------------------------------------------ */
.sv-cond {
  padding: 18px 18px 18px;
  border: 1px solid var(--line-strong);
  border-radius: var(--radius-s);
  background: var(--ticks), var(--surface);
  box-shadow: var(--highlight), var(--shadow);
}
@supports (corner-shape: bevel) { .sv-cond { border-radius: 0 var(--cut) 0 var(--cut); corner-shape: bevel; } }
.sv-cond__top { display: flex; flex-wrap: wrap; align-items: center; justify-content: space-between; gap: 6px 12px; }
.sv-hand--wide { gap: 1.5px; margin: 32px 0 20px; }
.sv-hand--wide .sv-hai { width: calc((100% - 19.5px) / 14); }
.sv-cond__grid { display: grid; grid-template-columns: minmax(0, 1fr) minmax(0, 1fr); gap: 14px 12px; margin: 0; padding-top: 14px; border-top: 1px solid var(--line); }
.sv-cond__item { display: grid; align-content: start; justify-items: start; gap: 6px; }
.sv-cond__item--wide { grid-column: 1 / -1; justify-items: stretch; }
.sv-cond__item dt { color: var(--faint); font-size: 12px; font-weight: 700; letter-spacing: 0.04em; }
.sv-cond__item dd { margin: 0; }
.sv-cond .sv-scores { max-width: none; margin: 0; }
.sv-cond .sv-vs { justify-content: flex-start; }
.sv-cond .sv-total { min-height: 44px; }

/* 1件の結果の表は見出しと値の 2 列 */
/* 1 件の表示は広い画面でも伸ばしすぎない（値が項目名から遠く離れないように）。題も含めて 1,200px の中央に置く */
.sv-wrap:has(.sv-layout.is-single) { max-width: calc(1200px + 2 * clamp(16px, 3vw, 32px)); margin-left: auto; margin-right: auto; }
.sv-layout.is-single .sv-board { width: 100%; }
.sv-layout.is-single .sv-table { width: 100%; }
.sv-layout.is-single .sv-table td { width: auto; min-width: 0; }

/* 1 件の表は短いので、表の中ではなくページごとスクロールする（JS の高さ指定を外す）。表が広すぎるときだけ横にスクロール */
.sv-layout.is-single .sv-board { height: auto !important; min-height: 0; overscroll-behavior: auto; }
@media (min-width: 992px) {
  .sv-layout.is-single { display: grid; grid-template-columns: minmax(340px, 400px) minmax(0, 1fr); gap: 20px; align-items: start; }
  .sv-layout.is-single .sv-hand--wide { gap: 2px; }
  .sv-layout.is-single .sv-hand--wide .sv-hai { width: calc((100% - 26px) / 14); }
  /* 左の条件のカードは、表を読み進める間も見えるように留める */
  .sv-layout.is-single .sv-cond { position: sticky; top: calc(var(--header-stick, 0px) + 16px); }
}
@media (max-width: 991.98px) {
  /* 縦に積む */
  .sv-layout.is-single .sv-cond { margin-bottom: 14px; }
}

/* ---- 牌譜一覧（天鳳の牌譜ビューアーを別タブで開く） ----------------------------------------- */
.sv-modal .modal-content { max-height: min(84vh, 760px); }
.sv-modal__count { display: inline-block; margin-left: 10px; padding: 1px 9px; border-radius: var(--radius-xs); border: 1px solid rgba(181, 140, 255, 0.38); background: rgba(181, 140, 255, 0.08); color: var(--violet-text); font-size: 14px; font-weight: 700; vertical-align: 2px; }
.sv-haihu { display: grid; grid-template-columns: repeat(auto-fill, minmax(108px, 1fr)); gap: 8px; margin: 0; padding: 0; list-style: none; }
.sv-haihu__link {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 5px;
  min-height: 40px;
  padding: 0 8px;
  border: 1px solid var(--line);
  border-radius: var(--radius-xs);
  background: rgba(255, 255, 255, 0.03);
  color: var(--text);
  font-size: 13.5px;
  font-weight: 700;
  text-decoration: none;
  white-space: nowrap;
  transition: color 0.15s, background-color 0.15s, border-color 0.15s;
}
.sv-haihu__link .n-num { font-size: 14px; }
.sv-haihu__link::after { content: ""; flex: none; width: 11px; height: 11px; margin-left: 1px; background: currentColor; -webkit-mask: var(--sv-i-ext) center / contain no-repeat; mask: var(--sv-i-ext) center / contain no-repeat; opacity: 0.55; }
.sv-haihu__link:hover { color: #fff; border-color: rgba(181, 140, 255, 0.6); background: rgba(181, 140, 255, 0.13); }
.sv-haihu__link:visited { color: var(--faint); }

/* ---- スマホ ------------------------------------------------------------------------------ */
.sv-layout.is-compact { --sv-label-w: 140px; }
.is-compact .sv-cond { padding: 16px; }
.is-compact .sv-table td { padding: 10px; }
.is-compact .sv-table .sv-label { padding-left: 14px; padding-right: 10px; font-size: 13px; }
.is-compact .sv-v { font-size: 16px; }
.is-compact .sv-ci { font-size: 11.5px; white-space: normal; }
.is-compact .sv-rate { gap: 8px; }
.is-compact .sv-count { min-width: 44px; height: 32px; font-size: 13px; }
.is-compact button.sv-count::after { inset: -6px -3px; }

@media (max-width: 575.98px) {
  .sv-main.n-main { padding-top: 16px; }
  .sv-back { padding-right: 10px; font-size: 13.5px; }
  .sv-haihu { grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
  .sv-haihu__link { min-height: 44px; padding: 0 4px; font-size: 13px; }
  .sv-modal .modal-body { padding: 1rem; }
}
