html[data-social-game="mental-math"] {
  scroll-behavior: auto;
  --ui-game-light: #6845a7; --ui-game-dark: #c4abff;
  --teal: #168a7b;
}
html[data-social-game="mental-math"][data-theme="dark"] {
  --teal: #68d7be;
}
.math-create-form { margin-top: 20px; }
.math-settings-fields { display: grid; gap: 16px; min-width: 0; }
.math-settings-fields .select-input {
  appearance: none; -webkit-appearance: none; height: 48px; min-height: 48px; padding-right: 36px;
  background-image: linear-gradient(45deg, transparent 50%, currentColor 50%), linear-gradient(135deg, currentColor 50%, transparent 50%);
  background-position: right 18px center, right 13px center;
  background-size: 5px 5px; background-repeat: no-repeat;
}
.math-settings-fields .form-grid { grid-template-columns: repeat(2,minmax(0,1fr)); }
.math-operations { min-width: 0; margin: 0; padding: 0; border: 0; }
.math-operations legend { margin-bottom: 9px; font-size: 13px; font-weight: 850; }
.math-operations { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 8px; }
.math-operations label { display: flex; align-items: center; gap: 8px; min-height: 44px; border: 1px solid var(--line); border-radius: 12px; padding: 8px; cursor: pointer; }
.math-operations input,.math-check input { width: 18px; height: 18px; accent-color: var(--accent-strong); flex: 0 0 auto; }
.math-operations label:has(:checked) { border-color: var(--accent); background: var(--accent-soft); }
.math-operations label > span { font-weight: 850; }
.math-operations label > span > span { font-size: 12px; }
.math-advanced { border-top: 1px solid var(--line); padding-top: 8px; }
.math-advanced > summary { min-height: 44px; display: list-item; padding: 10px 0; color: var(--muted); font-weight: 800; cursor: pointer; }
.math-advanced-body { display: grid; gap: 14px; padding-top: 8px; }
.math-setting-help,.math-range > p { color: var(--muted); font-size: 12px; line-height: 1.5; margin: 0; }
.math-check { min-height: 36px; display: flex; gap: 9px; align-items: center; font-size: 13px; }
.math-range { min-width: 0; border: 1px solid var(--line); border-radius: 12px; padding: 12px; }
.math-range legend { font-weight: 850; padding: 0 5px; }
.math-range-fields { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 10px; margin-top: 12px; }
.math-range-fields .field > span { font-size: 11px; }
.math-range-fields .text-input { min-width: 0; padding-inline: 9px; }
.math-settings-summary { display: grid; gap: 12px; font-size: 13px; }
.math-settings-summary dd { margin: 4px 0 0; color: var(--muted); }
.math-join { width: min(100%,480px); margin: 30px auto; padding: 28px; display: grid; gap: 18px; }
.math-join h1 { margin: 0; font-size: 32px; }
.math-lobby-players { display: grid; gap: 10px; margin: 18px 0; }
.math-lobby-player { display: flex; align-items: center; gap: 8px; min-height: 64px; padding: 12px; border: 1px solid var(--line); border-radius: 14px; }
.math-lobby-player > strong { min-width: 0; overflow-wrap: anywhere; flex: 1; }
.math-lobby-player > span:last-child { font-size: 12px; color: var(--muted); }
.math-lobby-player.is-empty { border-style: dashed; color: var(--muted); }
.math-presence { display: inline-block; width: 8px; height: 8px; flex: 0 0 8px; border: 2px solid var(--muted); border-radius: 50%; }
.math-presence.is-online { background: var(--success); border-color: var(--success); }
.math-lobby-controls,.math-result-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.math-room-settings { margin-top: 18px; }
body.is-math-playing .site-header { position: relative; }
body.is-math-playing .app-shell { width: min(100% - 24px,920px); height: calc(100svh - 53px); min-height: 0; padding: 8px 0 max(8px,env(safe-area-inset-bottom)); }
body.is-math-playing #game-learning,body.is-math-playing #logic-privacy-footer { display: none !important; }
.math-play { height: 100%; min-height: 420px; display: grid; grid-template-rows: 44px 62px minmax(0,1fr) 44px; gap: 8px; }
.math-match-head { display: grid; grid-template-columns: 1fr auto 1fr; gap: 12px; align-items: center; }
.math-match-head > strong { color: var(--muted); font-size: 14px; }
.math-match-head > .button { justify-self: end; min-height: 44px; }
.math-timer { font-size: 30px; font-weight: 900; font-variant-numeric: tabular-nums; }
.math-timer.is-low { color: var(--danger); }
.math-scoreboard { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 10px; }
.math-player { display: flex; align-items: center; justify-content: space-between; gap: 8px; min-width: 0; background: var(--panel); border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px; }
.math-player-name { display: flex; gap: 6px; align-items: center; min-width: 0; }
.math-player-name > strong { overflow: hidden; white-space: nowrap; text-overflow: ellipsis; font-size: 15px; }
.math-player-name > small { color: var(--muted); font-size: 10px; }
.math-player output { color: var(--math-player-color); font-size: 28px; font-weight: 900; font-variant-numeric: tabular-nums; }
.math-stage { position: relative; min-height: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 18px; padding: 16px 0; }
.math-question { max-width: 100%; font-size: clamp(32px,7vw,68px); line-height: 1.2; font-weight: 800; text-align: center; white-space: nowrap; font-variant-numeric: tabular-nums; letter-spacing: -.04em; }
.math-question.is-long { font-size: clamp(24px,5.3vw,52px); }
.math-countdown { position: absolute; top: 20%; font-size: clamp(32px,7vw,56px); font-weight: 900; color: var(--accent-strong); }
#mental-math-answer-form { width: min(100%,340px); display: grid; justify-items: center; gap: 8px; }
.math-answer { width: 100%; min-width: 0; height: 66px; background: var(--panel-solid); color: var(--ink); border: 2px solid var(--accent); border-radius: 16px; text-align: center; font: inherit; font-size: 38px; font-weight: 750; font-variant-numeric: tabular-nums; outline-offset: 3px; caret-color: var(--accent-strong); }
.math-answer[readonly] { border-color: var(--line); }
.math-answer-help { font-size: 12px; min-height: 18px; text-align: center; color: var(--muted); }
.math-keypad { display: none; width: min(100%,340px); grid-template-columns: repeat(3,minmax(0,1fr)); gap: 7px; }
.math-keypad .button { min-height: 44px; height: clamp(44px,6.4svh,58px); padding: 4px; font-size: 23px; user-select: none; touch-action: manipulation; box-shadow: none; }
.math-play-actions { display: flex; align-items: center; justify-content: center; gap: 8px; }
.math-play-actions .button { min-height: 44px; }
.math-play-actions #room-chat-toolbar-launcher { font: inherit; color: var(--ink); border: 1px solid var(--line); background: var(--panel); border-radius: 12px; padding: 8px 14px; display: inline-flex; gap: 7px; align-items: center; }
.math-play-actions .control-icon { width: 18px; height: 18px; }
.math-notice { position: fixed; z-index: 220; left: 50%; bottom: max(65px,env(safe-area-inset-bottom)); transform: translateX(-50%); width: max-content; max-width: calc(100vw - 24px); border: 1px solid var(--line); border-radius: 14px; background: var(--panel-solid); color: var(--ink); padding: 10px 14px; font-size: 13px; box-shadow: var(--shadow); pointer-events: none; }
.math-result-cards { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 16px; }
.math-result-card { padding: 22px; }
.math-result-card h2 { margin: 0; font-size: 18px; overflow-wrap: anywhere; }
.math-result-score { font-size: 54px; line-height: 1.15; color: var(--math-player-color); font-weight: 900; margin: 16px 0; }
.math-result-score small { margin-left: 8px; font-size: 13px; color: var(--muted); }
.math-result-card dl { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 16px; margin: 0; }
.math-result-card dt { font-size: 12px; color: var(--muted); }
.math-result-card dd { margin: 3px 0 0; font-size: 20px; font-weight: 850; }
.math-results-detail { margin: 20px 0; padding: 24px; }
.math-score-graph { margin: 0 0 26px; }
.math-score-graph figcaption { font-size: 18px; font-weight: 850; margin-bottom: 12px; }
.math-score-graph svg { display: block; width: 100%; max-height: 280px; }
.math-score-graph svg text { fill: var(--muted); font-size: 12px; }
.math-graph-axis { stroke: var(--line); stroke-width: 1; }
.math-graph-legend { display: flex; flex-wrap: wrap; gap: 16px; font-size: 12px; }
.math-graph-legend span::before { content: ''; display: inline-block; width: 16px; height: 3px; background: var(--math-player-color); margin: 0 6px 3px 0; }
.math-operation-results { display: grid; grid-template-columns: repeat(2,minmax(0,1fr)); gap: 24px; margin-bottom: 18px; }
.math-operation-results h3 { font-size: 15px; overflow-wrap: anywhere; }
.math-operation-results table { width: 100%; border-collapse: collapse; font-size: 13px; }
.math-operation-results th,.math-operation-results td { padding: 10px 5px; text-align: right; border-bottom: 1px solid var(--line); }
.math-operation-results th:first-child { text-align: left; }
.math-operation-results thead { color: var(--muted); font-size: 11px; }
@media (pointer: coarse), (max-width: 600px) {
  .math-keypad:not([hidden]) { display: grid; }
  .math-stage { gap: 10px; padding: 4px 0; }
  .math-question { font-size: clamp(30px,7.2vw,46px); }
  .math-question.is-long { font-size: clamp(23px,5.5vw,35px); }
  .math-answer { height: 54px; font-size: 32px; }
  .math-play { grid-template-rows: 44px 56px minmax(0,1fr) 44px; gap: 6px; }
  .math-player { padding: 8px; border-radius: 12px; }
  .math-player-name { gap: 4px; }
  .math-player-name > strong { font-size: 13px; }
  .math-player-name > small { display: none; }
  .math-player output { font-size: 24px; }
  .math-scoreboard { gap: 6px; }
  .math-countdown { top: 6%; }
  .math-result-cards { gap: 10px; }
  .math-result-card { padding: 15px; }
  .math-result-card h2 { font-size: 15px; }
  .math-result-score { font-size: 42px; }
  .math-result-score small { display: block; margin-left: 0; }
  .math-operation-results { grid-template-columns: 1fr; gap: 12px; }
  .math-results-detail { padding: 16px; }
}
@media (max-width: 360px) {
  body.is-math-playing .app-shell { width: calc(100% - 16px); }
  .math-settings-fields .form-grid { gap: 8px; }
  .math-operations label { gap: 5px; padding-inline: 6px; }
  .math-operations label > span > span { font-size: 11px; }
  .math-player-name > strong { font-size: 12px; }
}
@media (max-width: 420px) {
  .math-settings-fields > .form-grid:first-child { grid-template-columns: minmax(0,1fr); }
}
@media (max-height: 520px) and (orientation: landscape) {
  .math-play { min-height: 0; grid-template-rows: 44px minmax(0,1fr) 44px; grid-template-columns: minmax(0,1fr) 1fr; }
  .math-match-head { grid-column: 1/-1; }
  .math-scoreboard { align-self: center; grid-template-columns: 1fr; }
  .math-stage { grid-column: 2; grid-row: 2/4; gap: 8px; }
  .math-keypad { grid-template-columns: repeat(6,minmax(0,1fr)); gap: 4px; }
  .math-keypad .button { height: 44px; min-height: 44px; font-size: 18px; }
  .math-question { font-size: 32px; }
  .math-answer { height: 48px; font-size: 28px; }
  .math-answer-help { display: none; }
  .math-play-actions { grid-column: 1; grid-row: 3; }
}
.math-play .sr-only { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip: rect(0,0,0,0); white-space: nowrap; border: 0; }
.math-history-list { display: grid; gap: 12px; margin-top: 16px; }
.math-history-item { border: 1px solid var(--line); border-radius: 14px; overflow: hidden; }
.math-history-item summary { min-height: 64px; cursor: pointer; padding: 16px; }
.math-history-item summary > span { display: block; margin-top: 5px; color: var(--muted); font-size: 12px; }
.math-history-detail { padding: 18px; border-top: 1px solid var(--line); }
.math-history-detail p { font-size: 12px; color: var(--muted); }

html[data-social-game="mental-math"] .app-shell { overflow-anchor: none; }
.math-practice-setup { width: min(100%, 640px); margin-inline: auto; }
.math-practice-setup .page-head { margin-bottom: 20px; }
.math-practice-setup .create-form { margin-top: 0; }
.math-practice-return:empty { display: none; }
.math-practice-live-return:empty { display: none; }
.math-practice-return { margin-bottom: 16px; }
.math-practice-footnote { text-align: center; font-size: 14px; color: var(--muted); margin: 18px 0; }
.math-practice-footnote a { color: var(--accent-strong); text-underline-offset: 3px; }
.math-practice-error { color: var(--danger); font-size: 14px; margin: 0; }
.math-play.is-practice .math-scoreboard { grid-template-columns: 1fr; width: min(100%, 340px); justify-self: center; }
.math-results.is-practice { width: min(100%, 760px); margin-inline: auto; }
.math-results.is-practice .math-result-cards,
.math-results.is-practice .math-operation-results { grid-template-columns: 1fr; }
.math-results.is-practice .math-result-card dl { grid-template-columns: repeat(4, minmax(0,1fr)); }
@media (max-width: 600px) {
  .math-practice-setup .panel-body { padding: 18px; }
  .math-results.is-practice .math-result-score small { display: inline; margin-left: 8px; }
  .math-results.is-practice .math-result-card dl { gap: 10px; }
  .math-result-actions > .button { flex: 1 1 auto; }
}
