:root {
  /* 固定棋盤寬度／牌面高度為主（少用 vh／vw），避免視窗縮放時左右區塊比例差太多 */
  --cg-max-width: 560px;
  --cg-gap-x: 14px;
  --cg-gap-y: 14px;
  --cg-card-pad: 12px;
  --cg-card-font: clamp(14px, 0.875rem + 0.35vw, 17px);
  --cg-tile-h: 72px;
  --cg-rows: 4;
  /* 聊天室高度有上限；矮視窗仍以 viewport 為限 */
  --chat-fixed-h: min(620px, calc(100vh - 148px));
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  height: 100%;
}

body { font-family: system-ui, -apple-system, "Segoe UI", Arial; margin: 24px; }
h1 { margin: 0 0 12px; }
[hidden] { display: none !important; }
.row { display: flex; gap: 8px; align-items: center; margin: 12px 0; }
input { padding: 10px; border: 1px solid #ddd; border-radius: 8px; width: 260px; }
button { padding: 10px 14px; border: 1px solid #ddd; border-radius: 10px; background: #fff; cursor: pointer; }
button:hover { background: #f6f6f6; }
button:disabled { background: #f0f0f0; color: #999; cursor: not-allowed; }
small { color: #666; }

.hai-experiment-notice {
  margin: 0 0 12px;
  width: 100%;
  max-width: 520px;
  padding: 12px 14px;
  border: 1px solid #e8e8e8;
  border-radius: 10px;
  background: #fafafa;
  font-size: 14px;
  line-height: 1.55;
  color: #444;
}

.hai-experiment-notice p {
  margin: 0 0 8px;
}

.hai-experiment-notice p:last-child {
  margin-bottom: 0;
}

.hai-experiment-notice-title {
  font-weight: 700;
  color: #222;
}

.hai-start-row {
  flex-wrap: wrap;
  gap: 8px 12px;
  width: 100%;
  max-width: 520px;
}

.hai-start-row #participantId {
  flex: 1 1 auto;
  width: auto;
  min-width: 120px;
}

.hai-start-row #btnStart {
  flex-shrink: 0;
  margin-left: auto;
}

.hai-start-row #status {
  flex: 1 1 100%;
  min-width: 0;
}

.end-action-panel {
  margin-top: 20px;
  width: 100%;
  max-width: 800px;
  padding: 16px;
  border: 1px solid #ddd;
  border-radius: 12px;
  background: #fafafa;
}

.end-action-message {
  margin: 0 0 12px;
  font-size: 16px;
  font-weight: 600;
}

.end-action-button {
  min-width: 120px;
}

.end-answer-key-panel {
  margin-top: 20px;
  padding-top: 16px;
  border-top: 1px solid #e0e0e0;
}

.end-answer-key-title {
  margin: 0 0 8px;
  font-size: 15px;
  font-weight: 700;
}

.end-answer-key-hint {
  margin: 0 0 12px;
  font-size: 13px;
  color: #555;
  line-height: 1.4;
}

.end-answer-key-table-wrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
}

.end-answer-key-table {
  width: 100%;
  min-width: 720px;
  border-collapse: collapse;
  font-size: 12px;
  line-height: 1.35;
  background: #fff;
}

.end-answer-key-table th,
.end-answer-key-table td {
  border: 1px solid #ddd;
  padding: 8px 10px;
  vertical-align: top;
  text-align: left;
}

.end-answer-key-table th {
  background: #f0f4f8;
  font-weight: 600;
  white-space: nowrap;
}

.end-answer-key-item-id {
  font-weight: 700;
  white-space: nowrap;
  background: #fafafa;
}

.end-answer-key-words {
  min-width: 120px;
}

.end-answer-key-reason {
  min-width: 100px;
  color: #333;
}

/* Chat status line styles live near chat section below */

.game-status-banner {
  width: 100%;
  max-width: var(--cg-max-width);
  margin: 0 0 10px;
  font-size: 14px;
  line-height: 1.45;
}

.game-status-banner.is-error {
  color: #c62828;
}

.game-status-banner.is-success {
  color: #2e7d32;
}

.game-status-banner.is-neutral {
  color: #555;
}

.grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  row-gap: var(--cg-gap-y);
  column-gap: var(--cg-gap-x);
  width: 100%;
  max-width: var(--cg-max-width);
  /* board 最小高度 = 4 列牌面 + 3 個 row gap */
  min-height: calc(var(--cg-rows) * var(--cg-tile-h) + (var(--cg-rows) - 1) * var(--cg-gap-y));
  align-content: start;
}

/* M3: AI turn visual lock */
.grid.is-ai-turn {
  opacity: 0.5;
  filter: grayscale(0.1);
  pointer-events: none;
}

/* HAI: human turn + remote chat/adapter busy — same lock as AI turn (chat modal not inside .grid) */
.grid.is-hai-ai-request-busy {
  opacity: 0.5;
  filter: grayscale(0.1);
  pointer-events: none;
}
.card {
  padding: var(--cg-card-pad);
  border: 1px solid #ddd;
  border-radius: 14px;
  background: white;
  text-align: center;
  cursor: pointer;
  height: var(--cg-tile-h);
  font-size: var(--cg-card-font);
  display: flex;
  align-items: center;
  justify-content: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}
.card:hover {
  background: #f9f9f9;
}
.card.is-selected {
  background: #e0e0e0;
  border-color: #bbb;
  color: #999;
  cursor: not-allowed;
  pointer-events: none;
}

.card.is-solved {
  background: #E8F5E9;
  border-color: #A5D6A7;
  color: #689F38;
  cursor: not-allowed;
  pointer-events: none;
}

.card.is-solved:hover {
  background: #E8F5E9;
}

.answer-panel {
  margin: 20px 0;
  width: 100%;
  max-width: var(--cg-max-width);
}

.correct-panel {
  margin-top: 12px;
  width: 100%;
  max-width: var(--cg-max-width);
  display: none; /* Hide correct panel (replaced by solved rows) */
}

/* Solved rows (displayed above grid) */
.solved-rows {
  margin-bottom: 16px;
  display: flex;
  flex-direction: column;
  gap: var(--cg-gap-y);
  width: 100%;
  max-width: var(--cg-max-width);
}

.solved-row {
  display: grid;
  grid-template-columns: auto repeat(4, 1fr);
  column-gap: var(--cg-gap-x);
  row-gap: var(--cg-gap-y);
  position: relative;
}

.solved-word {
  display: flex;
  align-items: center;
  justify-content: center;
  padding: var(--cg-card-pad);
  background: #E8F5E9;
  color: #689F38;
  border: 1px solid #A5D6A7;
  border-radius: 14px;
  font-size: var(--cg-card-font);
  text-align: center;
  pointer-events: none;
  user-select: none;
  height: var(--cg-tile-h);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  min-width: 0;
}

/* Info button for showing reason */
.solved-row-info {
  padding: 0;
  background: transparent;
  border: none;
  color: #689F38;
  font-size: 18px;
  cursor: pointer;
  align-self: center;
  line-height: 1;
  user-select: none;
  min-width: 32px;
  height: var(--cg-tile-h);
  display: grid;
  place-items: center;
  border-radius: 4px;
  transition: background-color 0.2s, opacity 0.2s;
}

.solved-row-info:hover,
.solved-row-info:focus {
  background-color: rgba(104, 159, 56, 0.1);
  outline: 2px solid #689F38;
  outline-offset: 2px;
}

.solved-row-info:active {
  opacity: 0.7;
}

/* Tooltip for reason */
.solved-row-tooltip {
  position: absolute;
  bottom: calc(100% + 8px);
  left: 0;
  right: 0;
  background: #333;
  color: white;
  padding: 12px;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1.5;
  z-index: 100;
  display: none;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.2);
  word-wrap: break-word;
}

/* Desktop: hover/focus 顯示 tooltip */
@media (hover: hover) {
  .solved-row-info:hover + .solved-row-tooltip,
  .solved-row-info:focus + .solved-row-tooltip {
    display: block;
  }
}

/* Mobile: class 控制顯示 */
@media (hover: none) {
  .solved-row.has-tooltip .solved-row-tooltip {
    display: block;
  }
}

/* 答對或還原進度後短暫提示：hover ✓ 可看分組原因 */
.solved-row.show-tooltip-brief .solved-row-tooltip {
  display: block;
}

/* ============================================
   Layout Container (統一排版系統)
   ============================================ */

.layout-container {
  max-width: 1440px;
  margin: 0 auto;
  padding: 8px;
  min-height: 100vh;
}

.layout-container > h1 {
  margin: 0 0 12px;
}

.layout-container > .row {
  margin: 0 0 12px;
}

.panels {
  display: grid;
  grid-template-columns: minmax(0, var(--cg-max-width)) minmax(280px, 1fr);
  gap: 20px;
  align-items: start;
}

.panel {
  min-width: 0; /* 允許內容收縮 */
}

.panel.is-hidden {
  display: none;
}


.chat-area {
  height: 100%;
  min-width: 0;
}

/* 響應式：小螢幕時改為垂直堆疊 */
@media (max-width: 960px) {
  .layout-container {
    padding: 16px;
  }
  
  .panels {
    grid-template-columns: 1fr;
    gap: 20px;
  }
  
  .chat-area {
    min-height: 600px;
  }
}

.correct-title {
  font-size: 16px;
  font-weight: 600;
  margin: 0 0 12px;
}

.correct-groups {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.correct-group {
  border: 1px solid #ddd;
  border-radius: 12px;
  padding: 12px;
  background: #fafafa;
}

.correct-row {
  display: flex;
  align-items: center;
  gap: 12px;
}

.correct-words {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.correct-word {
  display: inline-block;
  padding: 6px 12px;
  background: #e0e0e0;
  color: #666;
  border-radius: 16px;
  font-size: 14px;
}

.correct-reason {
  flex: 0 0 auto;
  max-width: 40%;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  color: #555;
  font-size: 14px;
  line-height: 1.5;
}

.answer-row {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.answer-actions {
  display: flex;
  gap: 8px;
}

.answer-actions .btn-block {
  flex: 1;
  width: auto;
}

.answer-bar {
  min-height: 50px;
  padding: 10px 14px;
  border: 2px solid #ddd;
  border-radius: 14px;
  background: #fafafa;
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
}

.answer-bar.is-ai-turn {
  opacity: 0.7;
  pointer-events: none;
}

.answer-bar.is-hai-ai-request-busy {
  opacity: 0.7;
  pointer-events: none;
}

.answer-bar.is-empty {
  border: 2px dashed #ddd;
  color: #999;
}

.answer-bar.is-empty::before {
  content: "請選擇 4 個詞語";
}

.btn-block {
  width: 100%;
}

.answer-chip {
  display: inline-flex;
  align-items: center;
  padding: 8px 14px;
  background: #4CAF50;
  color: white;
  border-radius: 20px;
  cursor: pointer;
  font-size: 14px;
  transition: background 0.2s;
}

.answer-chip:hover {
  background: #45a049;
}

.modal {
  display: none;
  position: fixed;
  z-index: 1000;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.5);
}
.modal-content {
  background-color: #fff;
  margin: 15% auto;
  padding: 20px;
  border: 1px solid #ddd;
  border-radius: 14px;
  width: 80%;
  max-width: 500px;
  position: relative;
}
.modal-close {
  color: #aaa;
  float: right;
  font-size: 28px;
  font-weight: bold;
  cursor: pointer;
  line-height: 20px;
}
.modal-close:hover {
  color: #000;
}

/* Hide close button for game modals (confidence/difficulty) - users must use action buttons */
#confidenceModal .modal-close,
#difficultyModal .modal-close {
  display: none;
}
.modal-body {
  margin-top: 20px;
}

.modal-title {
  font-size: 18px;
  font-weight: 600;
  margin: 0 0 20px;
  line-height: 1.5;
}

.confidence-fieldset {
  border: none;
  padding: 0;
  margin: 0 0 24px;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border-width: 0;
}

.confidence-scale {
  display: flex;
  gap: 12px;
  justify-content: center;
  align-items: center;
}

.confidence-option {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border: 2px solid #ddd;
  border-radius: 8px;
  cursor: pointer;
  transition: all 0.2s;
}

.confidence-option:hover {
  border-color: #4CAF50;
  background: #f9f9f9;
}

.confidence-option input[type="radio"] {
  position: absolute;
  opacity: 0;
  width: 0;
  height: 0;
}

.confidence-option.is-selected {
  border-color: #4CAF50;
  background: #e8f5e9;
}

.confidence-option.is-selected span {
  font-weight: bold;
  color: #4CAF50;
}

.confidence-option span {
  font-size: 18px;
  user-select: none;
}

.modal-actions {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
  margin-top: 24px;
}

.table-wrap { overflow: auto; border: 1px solid #eee; border-radius: 12px; }

.admin-tabs {
  display: flex;
  gap: 4px;
  margin: 12px 0 0;
  border-bottom: 1px solid #ddd;
}
.admin-tabs button {
  padding: 10px 16px;
  border: 1px solid #ddd;
  border-bottom: none;
  border-radius: 10px 10px 0 0;
  margin-bottom: -1px;
  background: #f6f6f6;
}
.admin-tabs button:hover { background: #eee; }
.admin-tabs button[aria-selected="true"] {
  background: #fff;
  font-weight: 600;
  border-bottom: 1px solid #fff;
}

table { border-collapse: collapse; width: 100%; min-width: 900px; }
th, td { border-bottom: 1px solid #eee; padding: 10px; vertical-align: top; }
th { text-align: left; background: #fafafa; position: sticky; top: 0; }
code { background: #f4f4f4; padding: 2px 6px; border-radius: 6px; }

/* Landing Page Styles */
.landing {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

.landing-container {
  text-align: center;
  max-width: 400px;
  padding: 2rem;
}

.landing-container h1 {
  margin-bottom: 1rem;
}

.landing-links {
  display: flex;
  flex-direction: column;
  gap: 1rem;
  margin-top: 2rem;
}

.landing-btn {
  display: inline-block;
  padding: 12px 24px;
  background: #4CAF50;
  color: white;
  text-decoration: none;
  border-radius: 8px;
  font-size: 16px;
  transition: background 0.2s;
  border: none;
  cursor: pointer;
}

.landing-btn:hover {
  background: #45a049;
}

.landing-btn--admin {
  background: #2196F3;
}

.landing-btn--admin:hover {
  background: #1976D2;
}

.landing-links--inline {
  flex-direction: row;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 1rem;
}

.landing-admin-bar {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  flex-wrap: wrap;
  margin-bottom: 1rem;
}

.landing-btn--secondary {
  background: #607d8b;
}

.landing-btn--secondary:hover {
  background: #455a64;
}

.landing-btn.is-disabled,
.landing-btn[aria-disabled="true"] {
  background: #e0e0e0;
  color: #999;
  cursor: not-allowed;
  pointer-events: none;
}

.landing-btn.is-disabled:hover,
.landing-btn[aria-disabled="true"]:hover {
  background: #e0e0e0;
}

/* ============================================
   Chat UI Styles (現代化聊天室設計)
   ============================================ */

.chat-ui {
  display: flex;
  flex-direction: column;
  height: var(--chat-fixed-h);
  min-height: 0;
  border: 1px solid #e0e0e0;
  border-radius: 12px;
  background: #ffffff;
  box-shadow: none;
  overflow: hidden;
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid #e0e0e0;
  background: #f8f9fa;
  gap: 12px;
}

.chat-title {
  margin: 0;
  font-size: 16px;
  font-weight: 600;
  color: #333;
  flex: 1;
}

.chat-strategy-toggle {
  display: flex;
  gap: 4px;
  background: #ffffff;
  border: 1px solid #e0e0e0;
  border-radius: 8px;
  padding: 2px;
}

.strategy-btn {
  padding: 6px 12px;
  border: none;
  border-radius: 6px;
  background: transparent;
  color: #666;
  font-size: 12px;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.2s ease;
  white-space: nowrap;
}

.strategy-btn:hover {
  background: #f0f0f0;
  color: #333;
}

.strategy-btn-active {
  background: #4CAF50;
  color: #ffffff;
}

.strategy-btn-active:hover {
  background: #45a049;
  color: #ffffff;
}

.chat-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 16px 20px;
  background: #ffffff;
  display: flex;
  flex-direction: column;
  gap: 12px;
  scroll-behavior: smooth;
}

/* 訊息 bubble 設計 */
.chat-message {
  display: flex;
  flex-direction: column;
  max-width: 75%;
  animation: fadeIn 0.2s ease-in;
}

@keyframes fadeIn {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

/* User 訊息（右對齊，綠色 bubble） */
.chat-message-user {
  align-self: flex-end;
}

.chat-message-user .chat-message-content {
  background: #4CAF50;
  color: white;
  border-radius: 18px 18px 4px 18px;
  padding: 10px 16px;
  word-wrap: break-word;
}

/* AI 訊息（左對齊，灰色 bubble） */
.chat-message-ai {
  align-self: flex-start;
}

.chat-message-ai .chat-message-content {
  background: #f1f3f5;
  color: #333;
  border-radius: 18px 18px 18px 4px;
  padding: 10px 16px;
  word-wrap: break-word;
}

/* System 訊息（置中，淺藍色） */
.chat-message-system {
  align-self: center;
  max-width: 90%;
}

.chat-message-system .chat-message-role {
  font-size: 12px;
  color: #666;
  text-align: center;
  margin-bottom: 4px;
  font-weight: 500;
}

.chat-message-system .chat-message-content {
  background: #e3f2fd;
  color: #1976d2;
  border-radius: 12px;
  padding: 8px 12px;
  font-size: 14px;
  text-align: center;
}

/* User/AI 訊息不顯示 role label */
.chat-message-user .chat-message-role,
.chat-message-ai .chat-message-role {
  display: none;
}

/* 輸入區域 */
.chat-input-area {
  padding: 16px 20px;
  border-top: 1px solid #e0e0e0;
  background: #f8f9fa;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Chat status line (pinned below the last message) */
.chat-messages .chat-status {
  margin: 10px 0 0;
  padding: 6px 10px;
  color: #888;
  font-size: 13px;
  line-height: 1.4;
}

.chat-quick-btn {
  padding: 8px 16px;
  border: 1px solid #4CAF50;
  border-radius: 20px;
  background: white;
  color: #4CAF50;
  font-size: 14px;
  cursor: pointer;
  transition: all 0.2s;
  align-self: flex-start;
}

.chat-quick-btn:hover {
  background: #4CAF50;
  color: white;
}

.chat-input-row {
  display: flex;
  gap: 8px;
  align-items: center;
}

.chat-input {
  flex: 1;
  padding: 12px 16px;
  border: 1px solid #ddd;
  border-radius: 24px;
  font-size: 14px;
  outline: none;
  transition: border-color 0.2s;
}

.chat-input:focus {
  border-color: #4CAF50;
}

.chat-send-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border: none;
  border-radius: 50%;
  background: #4CAF50;
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.2s;
  flex-shrink: 0;
}

.chat-send-btn:hover {
  background: #45a049;
}

.chat-send-btn:disabled {
  background: #ccc;
  cursor: not-allowed;
}

.chat-send-icon {
  font-size: 18px;
  line-height: 1;
}

/* 2AFC experiment */
.twoafc-layout {
  max-width: 900px;
}

.twoafc-instructions {
  margin: 8px 0 18px;
  padding: 14px 16px;
  border: 1px solid #e2e2e2;
  border-radius: 10px;
  background: #fafafa;
}

.twoafc-instructions h2 {
  margin: 0 0 8px;
  font-size: 20px;
}

.twoafc-instructions p {
  margin: 8px 0;
}

.twoafc-instructions ol {
  margin: 8px 0 8px 20px;
}

.twoafc-preview-actions {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 10px;
  flex-wrap: wrap;
}

.twoafc-stage {
  margin-top: 24px;
  min-height: 320px;
  border: 1px solid #ddd;
  border-radius: 12px;
  padding: 20px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 16px;
}

.twoafc-fixation {
  font-size: 52px;
  font-weight: 700;
}

.twoafc-stimulus-panel {
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.twoafc-canvas {
  border: 1px solid #ddd;
  border-radius: 8px;
  background: #fff;
}

.twoafc-response {
  font-size: 18px;
}

.twoafc-confidence {
  width: 100%;
  text-align: center;
}

.twoafc-confidence-buttons {
  display: flex;
  justify-content: center;
  gap: 10px;
  margin: 12px 0;
}

.twoafc-confidence-buttons button {
  min-width: 40px;
}
