* { box-sizing: border-box; }
body {
  margin: 0;
  font-family: "Hiragino Kaku Gothic ProN", "Yu Gothic", Meiryo, system-ui, sans-serif;
  background: #14161a;
  color: #e8eaf0;
  /* アプリ全体をビューポートに固定し、body はスクロールさせない。
     長くなるのは結果パネル内部だけ (そちらは overflow-y: auto)。 */
  display: flex; flex-direction: column;
  height: 100dvh; overflow: hidden;
}
header {
  display: flex; align-items: baseline; gap: 16px;
  padding: 10px 16px; background: #1d2026; border-bottom: 1px solid #333743;
}
header h1 { font-size: 18px; margin: 0; }
#status { font-size: 13px; color: #9fb0c3; }
#help-btn {
  margin-left: auto; padding: 6px 14px; border-radius: 6px; cursor: pointer;
  border: 1px solid #454b5c; background: #262a33; color: #e8eaf0; font-size: 13.5px;
}
#help-dialog {
  width: min(680px, calc(100vw - 32px)); max-height: 85dvh; overflow-y: auto;
  background: #1d2026; color: #e8eaf0;
  border: 1px solid #333743; border-radius: 10px; padding: 20px 22px;
}
#help-dialog::backdrop { background: rgba(0, 0, 0, .65); }
#help-dialog h2 { margin: 0; font-size: 17px; }
#help-dialog h3 { font-size: 14.5px; margin: 18px 0 6px; color: #cdd6e4; }
#help-dialog p { font-size: 13.5px; line-height: 1.7; margin: 4px 0; color: #c3cad6; }
.help-head { display: flex; align-items: center; justify-content: space-between; }
#help-close {
  padding: 6px 12px; border-radius: 6px; cursor: pointer;
  border: 1px solid #454b5c; background: #262a33; color: #e8eaf0;
}
.hidden { display: none !important; }

#join-panel {
  width: 480px;max-width: 480px; margin: 48px auto; padding: 24px;
  background: #1d2026; border: 1px solid #333743; border-radius: 10px;
}
#join-form { display: flex; flex-direction: column; gap: 14px; }
#join-form label { display: flex; flex-direction: column; gap: 6px; font-size: 14px; }
#join-form input {
  padding: 9px 10px; border-radius: 6px; border: 1px solid #454b5c;
  background: #101216; color: #e8eaf0; font-size: 15px;
}
#join-form button {
  padding: 10px; border: 0; border-radius: 6px; cursor: pointer;
  background: #3d6fe0; color: #fff; font-size: 15px;
}
#join-form button:disabled { background: #556; cursor: default; }
.note { font-size: 12.5px; color: #9fb0c3; line-height: 1.6; }
.error { color: #ff8080; font-size: 13.5px; min-height: 1em; }

#room {
  flex: 1; min-height: 0;
  display: grid; gap: 14px; padding: 14px;
  grid-template-rows: auto minmax(0, 1fr);
  grid-template-columns: minmax(0, 1fr) 300px;
  grid-template-areas: "toolbar toolbar" "grid panel";
}
#toolbar { grid-area: toolbar; display: flex; gap: 12px; align-items: center; }
#toolbar button {
  padding: 8px 14px; border: 0; border-radius: 6px; cursor: pointer;
  background: #3d6fe0; color: #fff; font-size: 14px;
}
#toolbar button:hover { background: #3563c9; }
#order-status { font-size: 13.5px; color: #c4ccd9; }
#grid {
  grid-area: grid; display: grid; gap: 12px;
  /* 割り当てられた高さに収めて、参加人数に応じて 1x1 / 2x1 / 2x2 に割り付ける */
  min-height: 0; min-width: 0;
  grid-template-columns: 1fr 1fr;
}
#grid[data-count="1"] { grid-template-columns: 1fr; grid-template-rows: 1fr; }
#grid[data-count="2"] { grid-template-columns: 1fr 1fr; grid-template-rows: 1fr; }
#grid[data-count="3"], #grid[data-count="4"] {
  grid-template-columns: 1fr 1fr; grid-template-rows: 1fr 1fr;
}
.tile {
  position: relative; background: #000; border: 1px solid #333743;
  border-radius: 8px; overflow: hidden; min-height: 0;
}
.tile video { width: 100%; height: 100%; object-fit: contain; display: block; cursor: crosshair; background: #000; touch-action: none; }
.tile .sel-box {
  position: absolute; z-index: 3; pointer-events: none;
  border: 2px solid #ffd94d; background: rgba(255, 217, 77, .18);
}
.tile .label {
  position: absolute; left: 8px; bottom: 8px; padding: 2px 9px;
  background: rgba(0,0,0,.65); border-radius: 10px; font-size: 12.5px; pointer-events: none;
}
.tile .focus-btn {
  position: absolute; top: 8px; right: 8px; z-index: 2;
  padding: 4px 9px; border: 0; border-radius: 6px; cursor: pointer;
  background: rgba(0,0,0,.65); color: #e8eaf0; font-size: 12.5px;
}
.tile .focus-btn:hover { background: rgba(61,111,224,.85); }
.tile .flip-btn {
  position: absolute; top: 46px; right: 8px; z-index: 2;
  padding: 4px 9px; border: 0; border-radius: 6px; cursor: pointer;
  background: rgba(0,0,0,.65); color: #e8eaf0; font-size: 12.5px;
}
.tile .flip-btn:hover { background: rgba(61,111,224,.85); }
/* 最大化中: 対象タイルだけをグリッド全面に表示する */
#grid[data-focus] { grid-template-columns: 1fr !important; grid-template-rows: 1fr !important; }
#grid[data-focus] .tile { display: none; }
#grid[data-focus] .tile.focused { display: block; }
.tile.local { border-color: #3d6fe0; }

#result-panel {
  grid-area: panel; background: #1d2026; border: 1px solid #333743;
  border-radius: 10px; padding: 14px;
  /* 割り当てられた高さに収まり、長い内容はパネル内部でスクロールする */
  min-height: 0; overflow-y: auto;
}
.pager {
  display: flex; gap: 10px; align-items: center; justify-content: center;
  margin-top: 8px; font-size: 13px; color: #c4ccd9;
}
.pager button {
  padding: 5px 12px; border: 1px solid #454b5c; border-radius: 6px;
  background: #101216; color: #e8eaf0; cursor: pointer;
}
.pager button:hover:not(:disabled) { border-color: #3d6fe0; }
.pager button:disabled { opacity: 0.4; cursor: default; }
#result-panel h2 { font-size: 15px; margin: 0 0 10px; }
#search-form { display: flex; gap: 6px; margin-bottom: 8px; }
#search-form input {
  flex: 1; min-width: 0; padding: 7px 9px; border-radius: 6px;
  border: 1px solid #454b5c; background: #101216; color: #e8eaf0;
}
#search-form button {
  padding: 7px 12px; border: none; border-radius: 6px;
  background: #274b9f; color: #fff; cursor: pointer;
}
#search-form button:hover { background: #3563c9; }
#card-detail {
  border-bottom: 1px solid #333743; padding-bottom: 12px; margin-bottom: 12px;
  min-height: 60px;
}

/* パートナー表示チップ (タイル左上) */
.partner-chip {
  position: absolute; left: 6px; top: 6px; display: flex; align-items: center;
  gap: 6px; background: rgba(8, 10, 14, .8); border: 1px solid #d8a93b;
  border-radius: 8px; padding: 3px 9px 3px 3px; cursor: pointer;
  max-width: calc(100% - 110px);
}
.partner-chip:hover { border-color: #ffd968; background: rgba(24, 20, 8, .9); }
.partner-chip img { height: 40px; border-radius: 3px; display: block; }
.partner-chip span {
  font-size: 12px; color: #ffe9a8; white-space: nowrap;
  overflow: hidden; text-overflow: ellipsis;
}
.partner-actions { margin: 8px 0 2px; }
.partner-actions button {
  background: #2c3242; color: #ffe9a8; border: 1px solid #d8a93b;
  border-radius: 6px; padding: 6px 12px; cursor: pointer; font-size: 13px;
}
.partner-actions button:hover { background: #3a4157; }
#cand-area .meta, #card-detail .meta { font-size: 13px; color: #c4ccd9; line-height: 1.7; }
#card-detail img.card { width: 100%; border-radius: 6px; display: block; margin-bottom: 10px; cursor: zoom-in; }

/* カード画像の拡大表示 (ライトボックス) */
#lightbox {
  position: fixed; inset: 0; z-index: 50;
  background: rgba(0, 0, 0, .88);
  display: flex; align-items: center; justify-content: center;
  cursor: zoom-out;
}
#lightbox img {
  max-width: 94vw; max-height: 94dvh; border-radius: 8px;
  box-shadow: 0 8px 40px rgba(0, 0, 0, .6);
}
#card-detail .card-name { font-size: 16px; font-weight: bold; margin: 4px 0; }
#cand-area .verdict-match { color: #7ee2a0; font-weight: bold; }
#cand-area .verdict-none { color: #ffb37e; font-weight: bold; }
#result-panel .cand-list { margin-top: 12px; display: flex; flex-direction: column; gap: 6px; }
#result-panel .cand-head { font-size: 12.5px; color: #9fb0c3; }
#result-panel .cand-row {
  display: flex; gap: 10px; align-items: center; text-align: left;
  padding: 6px; border: 1px solid #454b5c; border-radius: 8px;
  background: #101216; color: #e8eaf0; cursor: pointer; width: 100%;
}
#result-panel .cand-row:hover { border-color: #3d6fe0; }
#result-panel .cand-row.selected { border-color: #7ee2a0; background: #16241b; }
#result-panel .cand-row img { width: 42px; border-radius: 4px; display: block; }
#result-panel .cand-main { display: flex; flex-direction: column; gap: 2px; min-width: 0; }
#result-panel .cand-name { font-size: 13.5px; font-weight: bold; }
#result-panel .cand-meta { font-size: 12px; color: #9fb0c3; }
#result-panel .badge-ok {
  color: #7ee2a0; border: 1px solid #7ee2a0; border-radius: 8px;
  padding: 0 6px; font-size: 11px;
}
#card-detail a { color: #8db1ff; }
.spinner { color: #c4ccd9; font-size: 14px; }
#fan-note {
  flex-shrink: 0; padding: 6px 12px; border-top: 1px solid #333743;
  font-size: 11px; line-height: 1.55; color: #8b98a9; text-align: center;
}
.tile .cam-off {
  position: absolute; inset: 0; z-index: 1;
  display: flex; align-items: center; justify-content: center;
  background: #101216; color: #9fb0c3; font-size: 15px;
}
#fan-note p { margin: 0; }
#fan-note .doc-links { margin-bottom: 2px; }
#fan-note a { color: #8db1ff; }
#fan-note .en { font-size: 10px; color: #6d7889; }
@media (max-width: 800px) {
  /* スマホでは通常の縦スクロールに戻す */
  body { display: block; height: auto; overflow: visible; }
  #room { display: flex; flex-direction: column; }
  #result-panel { width: 100%; overflow: visible; }
  #grid { height: auto; width: 100%; }
  .tile { aspect-ratio: 4 / 3; }
}
