/* 数学速查表 - 样式（移动优先） */
:root {
  --bg: #eef2f9;
  --card: #ffffff;
  --ink: #0f172a;
  --muted: #64748b;
  --line: #dbe3ee;
  --brand: #1d4ed8;
  --brand-soft: #e3ecff;
  --head-bg: #dbe7ff;
  --ok: #0f9d58;
  --bad: #d93025;
  --radius: 10px;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei",
               "Helvetica Neue", Arial, sans-serif;
  font-size: 16px;
  line-height: 1.5;
  touch-action: manipulation;
  -webkit-text-size-adjust: 100%;
}

.app {
  max-width: 760px;
  margin: 0 auto;
  min-height: 100vh;
  min-height: 100dvh;
  background: var(--bg);
  display: flex;
  flex-direction: column;
}

/* ---------- 头部 ---------- */
.hd {
  padding: 14px 12px 10px;
  background: linear-gradient(160deg, #1d4ed8, #3b82f6);
  color: #fff;
  position: sticky;
  top: 0;
  z-index: 20;
  box-shadow: 0 2px 10px rgba(15, 23, 42, .12);
}
.hd h1 {
  margin: 0;
  font-size: 19px;
  font-weight: 700;
  letter-spacing: .5px;
}
.hd-sub {
  margin: 3px 0 0;
  font-size: 12px;
  color: #dbeafe;
}
.searchbar {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}
.searchbar input {
  flex: 1;
  min-width: 0;
  height: 38px;
  padding: 0 12px;
  border: none;
  border-radius: 8px;
  font-size: 15px;
  background: rgba(255, 255, 255, .96);
  color: var(--ink);
  outline: none;
}
.searchbar button {
  height: 38px;
  padding: 0 16px;
  border: none;
  border-radius: 8px;
  background: #0f172a;
  color: #fff;
  font-size: 15px;
  font-weight: 600;
  cursor: pointer;
}
.searchbar button:active { opacity: .8; }

.qres {
  margin-top: 10px;
  padding: 10px 12px;
  border-radius: 8px;
  background: rgba(255, 255, 255, .16);
  font-size: 14px;
}
.qres b { font-size: 17px; }
.qres .qnote { display: block; color: #dbeafe; font-size: 12px; margin-top: 2px; }
.qres.bad { background: rgba(255, 255, 255, .12); }

/* ---------- 标签栏 ---------- */
.tabs {
  display: flex;
  gap: 0;
  background: var(--card);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 96px;
  z-index: 15;
}
.tabs button {
  flex: 1;
  padding: 11px 0;
  border: none;
  background: transparent;
  font-size: 15px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
  border-bottom: 3px solid transparent;
}
.tabs button.on {
  color: var(--brand);
  border-bottom-color: var(--brand);
  background: var(--brand-soft);
}

/* ---------- 内容区 ---------- */
.body { padding: 10px 10px 40px; flex: 1; }
.pane { animation: fade .15s ease-in; }
@keyframes fade { from { opacity: .4 } to { opacity: 1 } }

.tip {
  margin: 4px 2px 10px;
  font-size: 13px;
  color: var(--muted);
  line-height: 1.55;
}
.foot {
  margin: 12px 2px 0;
  font-size: 12px;
  color: var(--muted);
  line-height: 1.6;
}

.toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
}
.tbtn {
  padding: 7px 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--card);
  color: var(--brand);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}
.tbtn:active { background: var(--brand-soft); }
.count { font-size: 12px; color: var(--muted); margin-left: auto; }

/* ---------- 通用格子 ---------- */
.grid { display: grid; gap: 4px; }

.cell {
  position: relative;
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  transition: background .12s;
}
.cell:active { background: var(--brand-soft); }
.cell.masked { background: #f1f5f9; }
.cell.masked .val { opacity: 0; }
.cell .val { font-weight: 700; }

/* ---------- 乘法表 9×9 ----------
 * 列数必须是 10（1 个角格 + 9 个列头）：每行元素 = 行头 + 9 个格子，
 * 若写 repeat(9) 每行第 10 个元素会折到下一行，整表错位。 */
.grid-mul { grid-template-columns: repeat(10, 1fr); }
.grid-mul .cell {
  aspect-ratio: 1 / 1;
  padding: 0;
}
.grid-mul .head {
  background: var(--head-bg);
  color: var(--brand);
  font-weight: 700;
  cursor: default;
  border-color: #c7d7f7;
}
.grid-mul .head:active { background: var(--head-bg); }
.grid-mul .corner { background: #c7d7f7; }
/* 三角表对侧留白格：保留占位以维持网格对齐，但不可见、不可点 */
.grid-mul .cell.blank {
  visibility: hidden;
  pointer-events: none;
  border-color: transparent;
  background: transparent;
}
.grid-mul .val { font-size: clamp(11px, 3.1vw, 17px); }
.grid-mul .head { font-size: clamp(11px, 3.1vw, 17px); }

/* ---------- 平方表 ---------- */
.grid-sq { grid-template-columns: repeat(auto-fill, minmax(78px, 1fr)); }
.grid-sq .cell {
  padding: 7px 4px 8px;
  min-height: 62px;
  gap: 1px;
}
.grid-sq .lab {
  font-size: 12px;
  color: var(--muted);
  font-weight: 600;
}
.grid-sq .val { font-size: 19px; }

/* ---------- 乘法口诀：9 列网格（手机竖屏也排满，不折行）----------
 * 一行正好 9 格（1×1=1 … 1×9=9），每往下一行少一格，排成课本那种三角。
 * 手机竖屏宽度不够时**不折行**，而是靠 clamp 把字号缩到极限（约 9px），
 * 格子几乎贴在一起——这样手机上也能一眼看全整个三角（孩子横过来字会更大）。 */
.mul-list { display: block; max-width: 920px; margin: 0 auto; }
.mg {
  display: grid;
  grid-template-columns: repeat(9, minmax(0, 1fr));
  gap: 3px;
  padding: 3px 4px;
  margin-bottom: 3px;
  background: #f5f8fe;
  border: 1px solid var(--line);
  border-radius: 8px;
}
.mg:last-child { margin-bottom: 0; }
.mcell {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 0;
  padding: 7px 0;
  font: inherit;
  font-size: clamp(9px, 0.8vw + 6px, 16px);
  font-weight: 700;
  line-height: 1.15;
  white-space: nowrap;
  overflow: hidden;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.mcell .meq { margin: 0 1px; color: var(--muted); font-weight: 400; }
.mcell .mv { color: var(--brand); }
.mcell:active { background: var(--head-bg); }
/* 遮罩：得数文字透明 + 淡蓝底块。宽度不变，遮罩前后不会重排跳动 */
.mcell.masked .mv {
  color: transparent;
  background: #e8eefb;
  border-radius: 3px;
}

/* ---------- π 表 ---------- */
/* ===== 自绘数字键盘（考一考专用）=====
 * 为什么要自己画：手机上系统输入法每答一题就弹起/收起一次，页面跟着跳动，
 * 孩子连续答题很难受。答案框是自绘 div（不是原生 input），根本不唤起系统键盘，
 * 输入全部靠这里的按键（电脑上物理键盘依旧可用，见 app.js wireQuiz）。 */
.keypad {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 8px;
  margin-top: 12px;
}
.key {
  min-height: 50px;
  padding: 12px 0;
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  color: var(--ink);
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;   /* 连点不触发双击缩放 */
}
.key:active { background: var(--head-bg, #dbe7ff); }   /* 按下不做位移：位移会让手机 touchend 命中判定偏移，快速连点时偶发丢键 */
.key.fn { background: #f1f5f9; color: var(--muted); font-size: 18px; }
.key.fn:active { background: #e2e8f0; }
.key.wide { grid-column: span 2; }
.key.ok {
  background: linear-gradient(135deg, #2563eb, var(--brand));
  color: #fff;
  border-color: transparent;
  font-size: 19px;
}
.key.ok:active { background: linear-gradient(135deg, var(--brand), #1e40af); }

.pi-head {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 9px 12px;
  font-size: 13px;
  color: var(--muted);
  margin-bottom: 8px;
}
.grid-pi { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }
.grid-pi .cell {
  flex-direction: row;
  justify-content: space-between;
  padding: 12px 14px;
  gap: 10px;
}
.grid-pi .left { display: flex; flex-direction: column; align-items: flex-start; }
.grid-pi .lab { font-size: 18px; font-weight: 700; color: var(--brand); }
.grid-pi .src { font-size: 11px; color: var(--muted); margin-top: 2px; }
.grid-pi .right { display: flex; flex-direction: column; align-items: flex-end; }
/* π 表遮罩：数值与精确值一起藏起来（否则精确值会泄露答案） */
.grid-pi .cell.masked .right { opacity: 0; }
.grid-pi .val { font-size: 21px; font-weight: 700; }
.grid-pi .exact { font-size: 11px; color: var(--muted); margin-top: 2px; }

/* ---------- 考一考 ---------- */
.quiz {
  background: var(--card);
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 14px 12px;
}
.quiz-cfg { display: flex; gap: 8px; flex-wrap: wrap; }
.chip {
  padding: 6px 12px;
  border: 1px solid var(--line);
  border-radius: 999px;
  background: #fff;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  cursor: pointer;
}
.chip.on {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.quiz-q {
  margin: 18px 0 2px;
  text-align: center;
  font-size: clamp(30px, 10vw, 46px);
  font-weight: 800;
  letter-spacing: 1px;
  color: var(--ink);
}
.quiz-in { display: flex; gap: 8px; }
.quiz-in .ansbox {
  flex: 1;
  min-width: 0;
  height: 46px;
  padding: 0 10px;
  border: 1px solid var(--line);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1px;
  font-size: 22px;
  font-weight: 700;
  text-align: center;
  color: var(--ink);
  outline: none;
  overflow: hidden;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}
.quiz-in .ansbox:focus { border-color: var(--brand); }
/* 一位一格：点数字中间选中（变蓝）替换这一位；点两个数字中间则出现光标，接着插字符 */
.quiz-in .ansbox .d { padding: 2px 3px; border-radius: 4px; }
.quiz-in .ansbox .d.sel { background: var(--brand); color: #fff; }
.quiz-in .ansbox .caret {
  width: 2px;
  height: 1.15em;
  margin: 0 -1px;
  background: var(--brand);
  border-radius: 1px;
  animation: quizCaret 1.05s step-end infinite;
}
@keyframes quizCaret { 0%, 55% { opacity: 1; } 56%, 100% { opacity: 0; } }
.quiz-in .ansbox .ph { color: var(--muted); font-size: 14px; font-weight: 400; }
.quiz-in button {
  height: 46px;
  padding: 0 20px;
  border: none;
  border-radius: 10px;
  background: var(--brand);
  color: #fff;
  font-size: 16px;
  font-weight: 700;
  cursor: pointer;
}
.quiz-in button:active { opacity: .85; }
.quiz-hint {
  display: none;            /* 宽屏用上面那段 .tip 就够了，不重复 */
  margin: 8px 0 0;
  text-align: center;
  font-size: 12px;
  line-height: 1.5;
  color: var(--muted);
}
.quiz-fb {
  min-height: 24px;
  margin: 0 0 10px;
  text-align: center;
  font-size: 15px;
  font-weight: 700;
  line-height: 1.35;
}
.quiz-fb.ok { color: var(--ok); }
.quiz-fb.bad { color: var(--bad); }
.quiz-score {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px dashed var(--line);
  font-size: 13px;
  color: var(--muted);
}

/* 9 列网格已经是默认样式（.mg 见上），任何宽度都排满 9 格，不再按屏宽分叉 */

/* ?mul=rowr：行优先 + 右对齐（每行从右往左填，右端排成一列 a×a：1×1=1／2×2=4／…9×9=81），
 * 即“第一行 9 格、越往下越靠右”的阶梯样式。 */
.mul-list.rowr .mg { direction: rtl; }
.mul-list.rowr .mcell { direction: ltr; }

/* 横屏手机（高度小）：头部压成一行、行高收紧，让整个 9 行三角一屏放完 */
@media (orientation: landscape) and (max-height: 520px) {
  .hd {
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 8px;
    padding: 6px 10px;
  }
  .hd h1 { flex: 0 0 auto; font-size: 15px; letter-spacing: 0; }
  .hd-sub { display: none; }
  .searchbar { flex: 1 1 220px; margin-top: 0; gap: 6px; }
  .searchbar input { height: 30px; padding: 0 10px; font-size: 13px; }
  .searchbar button { height: 30px; padding: 0 12px; font-size: 13px; }
  .qres { flex: 1 1 100%; margin-top: 4px; padding: 6px 10px; font-size: 13px; }
  .tabs { top: 42px; }
  .tabs button { padding: 5px 0; font-size: 13px; }
  .body { padding: 5px 10px 10px; }
  .toolbar { margin-bottom: 4px; gap: 6px; }
  .tbtn { padding: 3px 10px; font-size: 12px; }
  .mg { gap: 3px; padding: 2px 4px; margin-bottom: 2px; }
  .mg .mcell { padding: 2px 2px; font-size: clamp(11px, 1.45vw, 14px); }
  .foot { display: none; }        /* 横屏看表时不需要打印提示，省下高度让 9 行不滚动 */
}

/* 竖屏窄屏：一行确实放不下 9 句，提示把手机横过来（横屏/宽屏不显示这条） */
.only-portrait { display: none; }
@media (orientation: portrait) and (max-width: 679px) {
  .only-portrait { display: block; }
  .body { padding: 8px 8px 16px; }
  /* 头部压扁：竖屏下把高度让给 9 行三角，尽量一屏看完 */
  .hd { padding: 8px 10px 10px; }
  .hd-sub { display: none; }
  .searchbar { margin-top: 6px; }
  .searchbar input, .searchbar button { height: 32px; font-size: 13px; }
  .tabs button { padding: 8px 0; font-size: 13px; }
  .toolbar { margin-bottom: 4px; }
  .tbtn { padding: 4px 10px; font-size: 12px; }
  /* 一行要排满 9 格，格子必然很窄 —— 把算式**折成两行**（1×9 ／ =9），
   * 数字依旧横着写、字号不用缩（缩到 9px 会被手机浏览器的最小字号撑破）。
   * 宽度够的横屏/电脑端保持一行横排，不折行。 */
  .mg {
    display: grid;
    grid-template-columns: repeat(9, minmax(0, 1fr));
    gap: 2px;
    padding: 2px;
    margin-bottom: 1px;
  }
  .mg .mcell {
    flex-wrap: wrap;
    justify-content: center;
    align-content: center;
    padding: 3px 0;
    font-size: clamp(12px, 1.2vw + 10px, 15px);
    line-height: 1.15;
    letter-spacing: 0;
  }
  .mg .mcell .ma { flex: 0 0 100%; }   /* “1×9” 独占一行，剩余 “=9” 落到第二行 */
  .mcell .meq { margin: 0; }
  .tip { display: none; }   /* 竖屏窄屏省下高度，让 9 行三角尽量一屏装下 */
  .quiz-hint { display: block; }   /* 说明收起了，改数字的提示留在答案框下面 */
  .foot { display: none; }
}
/* 横屏手机高度小：把说明收起来，9 行三角尽量一屏看完 */
@media (orientation: landscape) and (max-height: 520px) {
  .tip { display: none; }
}

/* ---------- 打印：三张表全部展开，去掉交互控件 ---------- */
@media print {
  .hd, .tabs, .toolbar, .tip, #pane-test, .foot { display: none !important; }
  .body { padding: 0; }
  .pane[hidden] { display: block !important; }
  .pane { break-inside: avoid; margin-bottom: 14px; }
  .cell.masked .val, .grid-pi .cell.masked .right { opacity: 1 !important; }
  .cell.masked { background: var(--card) !important; }
  .grid-mul .val, .grid-sq .val, .grid-pi .val { color: #000; }
  /* 口诀列表打印：被遮住的得数也要印出来，去掉遮罩底块 */
  .mcell.masked .mv { color: #000 !important; background: transparent !important; }
  .mg { background: transparent; border-color: #ddd; }
  .grid { gap: 3px; }
  .grid-mul { grid-template-columns: repeat(10, 1fr); }
  .grid {
    border-collapse: collapse;
  }
}
