/* ===== 设计令牌：黑白灰，主色白 ===== */
:root {
  --page-bg: #e4e4e4;
  --white: #ffffff;
  --ink: #111111;
  --gray-1: #f4f4f4;
  --gray-2: #e2e2e2;
  --gray-3: #c7c7c7;
  --gray-4: #8a8a8a;
  --gray-5: #4a4a4a;
  --line: #dcdcdc;

  --font-body: -apple-system, "Segoe UI", "PingFang SC", "Microsoft YaHei", "Noto Sans CJK SC", "Helvetica Neue", sans-serif;

  --radius-sm: 3px;
  --radius-md: 8px;
  /* 桌面鼠标端封顶在窄宽度，两边留灰边（这是个「手机应用」形态的小工具，
     不是网站，宽屏下拉满铺开反而难用）。平板会在下面 pointer:coarse 里
     覆盖成「比桌面宽、但仍然封顶，两侧留一点边」——平板和桌面对宽屏的
     期望不一样，不能用同一个数字兼顾。
     先写一份 vw 兜底，dvw 不支持的浏览器会整条丢弃这行、留着上一行生效；
     支持 dvw 的浏览器两行都认，后面这行覆盖前面。 */
  --frame-width: min(460px, 92vw);
  --frame-width: min(460px, 92dvw);
  /* 圆盘的高度上限要减掉「投掷方式」按钮 + 两者间距占用的空间，矮屏下按钮
     和间距会一起缩小，这个值也要跟着变小，否则矮屏下圆盘会被顶出容器 */
  --mode-row-reserve: 78px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
}

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--ink);
  background: var(--page-bg);
  display: flex;
  align-items: stretch;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

input {
  font-family: inherit;
  user-select: text;
  -webkit-user-select: text;
}

button {
  font-family: inherit;
  cursor: pointer;
}

svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 1.5;
  stroke-linecap: round;
  stroke-linejoin: round;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* ===== 竖屏画面：宽度固定，上下顶格贴满整个视口高度，不滚动 ===== */

.frame {
  width: var(--frame-width);
  max-width: 100vw;
  max-width: 100dvw;
  height: 100vh;
  height: 100dvh;
  background: var(--white);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  /* viewport-fit=cover 会让页面延伸到刘海/挖孔屏和手势条底下，不留这圈安全区
     内容会被系统状态栏、Home 指示条实际盖住看不见（不是布局往下推了，是真的
     被盖住），加了 box-sizing:border-box 之后这圈内边距只吃内容空间，不会把
     .frame 撑出 100dvh */
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
}

@media (min-width: 640px) and (pointer: fine) {
  .frame {
    box-shadow: 0 0 0 1px var(--line), 0 0 60px rgba(0,0,0,0.08);
  }
}

/* 平板（触屏、pointer:coarse）比手机封顶宽度更大，但不能整个撑到 100%——
   按钮顶到屏幕最左最右会很难点、也不好看，两侧还是要留一点灰边；只是
   不能像桌面那样封顶在 460px 那么窄，那样平板上画面会显得很小。 */
@media (pointer: coarse) and (min-width: 640px), (any-pointer: coarse) and (min-width: 640px) {
  :root {
    --frame-width: min(700px, 96vw);
    --frame-width: min(700px, 96dvw);
  }
}

.frame__header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 20px 10px;
}

.frame__title {
  flex: 1;
  min-width: 0;
  margin: 0;
  font-weight: 700;
  /* 下限压得很低——宽度不够时应该是字变小，不是把标题掐头去尾变成
     「…骰子」这种看不出名字的省略号，省略号只留给理论上极端到算不出来的
     兜底情况，正常宽度范围内不应该真的触发 */
  font-size: clamp(9px, 5.4vw, 21px);
  font-size: clamp(9px, 5.4dvw, 21px);
  letter-spacing: 0.01em;
  color: var(--ink);
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.info-btn,
.reset-btn {
  flex: none;
  width: 30px;
  height: 30px;
  border-radius: 50%;
  border: 1px solid var(--gray-3);
  background: var(--white);
  color: var(--gray-5);
  display: flex;
  align-items: center;
  justify-content: center;
  transition: border-color 0.15s ease, color 0.15s ease;
}
.info-btn svg,
.reset-btn svg { width: 16px; height: 16px; }
.info-btn:hover,
.reset-btn:hover { border-color: var(--ink); color: var(--ink); }
.info-btn:focus-visible,
.reset-btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.reset-btn:active { transform: scale(0.94); }

/* ===== 主舞台：从上到下依次是属性输入、高级算式、骰子画面、骰子类型选择条 ===== */

/* 属性数值贴着头部、骰子类型贴着页脚，中间这块（高级/投掷方式/骰子画面）
   撑满剩下的所有空间，并在这块空间里自己居中——这样上下两头不会跟着屏幕
   变高而被越推越远，多出来的空间都会去到中间。 */

.stage {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 20px 16px;
  gap: 8px;
}

.stage__middle {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* 骰子画面区：撑满投掷方式下面剩余的全部空间——不是为了让圆盘自己居中，
   是为了给容器查询一个真实、完整的可用高度（cqh），避免用 vh 之类跟视口
   挂钩的公式去估算「大概有多少高度」，猜错了就会出现「明明还有空间，圆盘
   却没长满」的留白。「投掷方式」按钮挪进了这个容器内部，跟圆盘一起排布：
   用 justify-content: space-evenly 把多出来的空间平均分成三份——上面的
   面板到按钮、按钮到圆盘、圆盘到下面的分隔线——三段间距基本相等，不是
   按钮紧贴圆盘、上面空一大截的割裂感，也不是按钮紧贴上面面板。 */
.dice-stage-area {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  container-type: size;
}

/* 骰子类型：标签单独一行，按钮另起一行尽量摆成一排；真的放不下就横向滚动，
   不换行 */

.picker-row {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.picker-row__label {
  font-size: 12px;
  color: var(--gray-4);
  text-align: center;
}

.picker-row__list {
  width: 100%;
  display: flex;
  flex-wrap: nowrap;
  justify-content: center;
  gap: 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.picker-row__list::-webkit-scrollbar { display: none; }

/* 按钮先用 flex 挤压/撑开去适应这一排能用的宽度，实在小到连最小尺寸都
   放不下才出现横向滚动——不会出现「明明有地方却被裁切掉一块」的情况 */
.picker-row__btn {
  flex: 1 1 44px;
  min-width: 32px;
  max-width: 44px;
  aspect-ratio: 1 / 1;
  height: auto;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: var(--gray-4);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 3px;
  padding: 0;
  transition: background 0.15s ease, color 0.15s ease;
}
.picker-row__icon { width: 20px; height: 20px; }
.picker-row__btn-name { font-size: 10px; font-weight: 600; letter-spacing: 0.01em; }
.picker-row__btn:hover { color: var(--ink); }
.picker-row__btn[aria-pressed="true"] {
  background: var(--ink);
  color: var(--white);
}
.picker-row__btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }
.picker-row__btn:active { transform: scale(0.96); }

/* 属性数值：始终占着这行位置，非 d100 时只是视觉上隐藏，不摘出布局，
   避免切换骰子类型时下面的内容跟着挪位置 */

/* 属性数值和高级算式是同一块面板，不用线隔开 */
.config-block {
  width: 100%;
  max-width: 400px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  padding: 12px 16px;
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  background: var(--white);
}

.skill-row {
  width: 100%;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 10px;
}

.skill-row label {
  font-size: 13px;
  color: var(--gray-5);
  white-space: nowrap;
}

.skill-row input {
  width: 48px;
  text-align: center;
  font-size: 18px;
  font-weight: 700;
  color: var(--ink);
  background: transparent;
  border: none;
  border-bottom: 2px solid var(--gray-3);
  padding: 0 0 2px;
}
.skill-row input:focus-visible {
  outline: none;
  border-color: var(--ink);
}
.skill-row input::-webkit-outer-spin-button,
.skill-row input::-webkit-inner-spin-button {
  -webkit-appearance: none;
  margin: 0;
}
.skill-row input[type="number"] { appearance: textfield; }

/* 骰子画面：圆盘当桌面，骰子滚动时可以超出圆盘边缘，不整体套方框 */

.dice-canvas-wrap {
  position: relative;
  /* 边长取「容器实际可用宽度」和「容器实际可用高度」里较小的一个（cqw/cqh
     由 .dice-stage-area 的 container-type: size 提供），封顶 680px、
     兜底不小于 160px；封顶值和画面宽度上限（--frame-width）要一起改，
     只改一边不会有效果。高度那一项减掉约 50px，是留给同一容器里「投掷方式」
     按钮 + 两者间距占用的空间，不减这一块圆盘会比容器实际能给的高度更高，
     顶到按钮往外挤。 */
  width: clamp(var(--dice-floor, 160px), min(100cqw, calc(100cqh - var(--mode-row-reserve))), 680px);
  aspect-ratio: 1 / 1;
  /* 圆盘到下面分隔线的间距同样靠 space-evenly 自动分配，圆盘占满高度时
     这段会被压得很小，这里也加一个固定的最小值兜底。 */
  margin-bottom: 14px;
}

.dice-plate {
  position: absolute;
  /* 之前是 8%，圆盘可视边缘比容器盒子小了一整圈看不出原因的留白，导致
     「按钮到圆盘」的视觉间距比数值算出来的更远；缩到 3% 又变成骰子紧贴着
     圆盘边缘、桌子比骰子本身大不了多少。5% 是两头都要顾到之后的折中值。 */
  inset: 5%;
  border-radius: 50%;
  background: var(--gray-1);
}

.dice-stage {
  position: absolute;
  inset: 0;
  cursor: pointer;
}
/* 引擎自己给 canvas 加了 tabindex 用来接键盘事件，点击后会被聚焦，
   触发浏览器默认的蓝色/黑色焦点框——这个骰子画面本来就不需要键盘操作，
   去掉这个默认框 */
.dice-stage canvas { width: 100% !important; height: 100% !important; display: block; outline: none; }

/* 立即投掷模式：不会真的跑物理动画，骰子模型不显示；但圆盘桌面本身要留着，
   不然点击区域看起来就是一片空白，找不到该点哪里 */
.dice-canvas-wrap.is-instant .dice-stage canvas {
  display: none;
}

/* ===== 结果：浮在骰子正中间，一个整体的框，不是每行单独套 ===== */

.center-readout {
  position: absolute;
  inset: 0;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  pointer-events: none;
  padding: 12px;
}

.center-readout__box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  text-align: center;
  background: rgba(255, 255, 255, 0.92);
  border: 1px solid var(--line);
  border-radius: var(--radius-md);
  padding: 10px 18px;
  min-width: 92px;
}

.center-readout__label {
  font-weight: 700;
  font-size: 18px;
  letter-spacing: 0.02em;
  color: var(--gray-5);
}

.center-readout__number {
  font-weight: 900;
  font-size: 34px;
  line-height: 1.3;
  color: var(--ink);
}

.center-readout__detail {
  font-size: 13px;
  color: var(--gray-4);
}

.center-readout__label:empty,
.center-readout__detail:empty {
  display: none;
}

.center-readout__hint {
  margin: 0;
  font-size: 14px;
  color: var(--gray-4);
}

/* 空闲时只显示「点击投掷」提示，不显示结果卡片；有结果之后反过来 */
.center-readout[data-state="hint"] .center-readout__box {
  display: none;
}
.center-readout:not([data-state="hint"]) .center-readout__hint {
  display: none;
}

/* 失败一侧：反相为黑底白字，靠对比而不是颜色来区分成败 */
.center-readout[data-state="fail"] .center-readout__box,
.center-readout[data-state="fumble"] .center-readout__box {
  background: var(--ink);
  border-color: var(--ink);
}
.center-readout[data-state="fail"] .center-readout__label,
.center-readout[data-state="fumble"] .center-readout__label,
.center-readout[data-state="fail"] .center-readout__number,
.center-readout[data-state="fumble"] .center-readout__number {
  color: var(--white);
}
.center-readout[data-state="fail"] .center-readout__detail,
.center-readout[data-state="fumble"] .center-readout__detail {
  color: var(--gray-3);
}

.center-readout[data-state]:not([data-state="hint"]) .center-readout__number {
  animation: readout-in 0.2s ease-out;
}
@keyframes readout-in {
  from { opacity: 0; transform: scale(1.08); }
  to { opacity: 1; transform: scale(1); }
}

/* ===== 投掷方式：实际投掷 / 立即投掷，常驻的两段式切换 ===== */

.mode-row {
  display: flex;
  flex: none;
  /* 面板到按钮的间距靠 .dice-stage-area 的 space-evenly 自动分配，圆盘
     占满高度、留白很少时这段会被压得很小甚至看不出来，这里加一个固定的
     最小值兜底，保证按钮跟上面的面板之间始终有能看出来的间隔。 */
  margin-top: 14px;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 3px;
  gap: 3px;
}

.mode-row__btn {
  border: none;
  background: transparent;
  color: var(--gray-4);
  font-size: 12.5px;
  font-weight: 600;
  padding: 6px 14px;
  border-radius: 999px;
  min-height: 30px;
  transition: background 0.15s ease, color 0.15s ease;
}
.mode-row__btn:hover { color: var(--ink); }
.mode-row__btn[aria-pressed="true"] {
  background: var(--ink);
  color: var(--white);
}
.mode-row__btn:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

/* ===== 高级：自定义骰子算式——常驻显示的独立区域，不做成可收起的面板 ===== */

.advanced {
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}

.advanced__label {
  font-size: 12px;
  color: var(--gray-4);
}

.advanced input {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: var(--radius-sm);
  padding: 8px 10px;
  font-size: 14px;
  text-align: center;
  color: var(--ink);
  background: var(--gray-1);
}
.advanced input:focus-visible {
  outline: none;
  border-color: var(--ink);
}

.advanced__error {
  margin: 0;
  font-size: 12px;
  color: var(--gray-5);
}

/* ===== 分隔线：把骰子类型选择区单独框出来 ===== */

.section-divider {
  width: 100%;
  border-top: 1px solid var(--line);
}

/* ===== 页脚：开源来源标注（纯文字，不做外链） ===== */

.frame__footer {
  flex: none;
  padding: 10px 20px;
  border-top: 1px solid var(--line);
  font-size: 11.5px;
  color: var(--gray-4);
  text-align: center;
}

/* ===== 判定说明弹窗 ===== */

.modal {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 24px;
}
.modal[hidden] { display: none; }

.modal__backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.4);
}

.modal__panel {
  position: relative;
  width: min(380px, 100%);
  max-height: 82vh;
  max-height: 82dvh;
  overflow-y: auto;
  background: var(--white);
  color: var(--ink);
  border-radius: var(--radius-md);
  padding: 30px 24px 22px;
  box-shadow: 0 24px 60px rgba(0,0,0,0.25);
  animation: modal-in 0.18s ease-out;
  user-select: text;
  -webkit-user-select: text;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(6px) scale(0.98); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

.modal__panel h2 {
  margin: 0 0 16px;
  font-size: 18px;
}

.modal__close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  border: none;
  background: transparent;
  color: var(--ink);
  display: flex;
  align-items: center;
  justify-content: center;
}
.modal__close svg { width: 17px; height: 17px; }
.modal__close:hover { background: var(--gray-1); }
.modal__close:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.rule-list {
  margin: 0;
  padding: 0;
  list-style: none;
  display: flex;
  flex-direction: column;
  gap: 11px;
}
.rule-list li {
  padding-left: 14px;
  border-left: 2px solid var(--gray-3);
  line-height: 1.6;
  font-size: 14px;
}
.rule-list strong {
  margin-right: 2px;
}

.modal__divider {
  margin: 20px 0 16px;
  border-top: 1px solid var(--line);
}

/* ===== 下载入口 ===== */

.download-block__btn {
  display: block;
  width: 100%;
  box-sizing: border-box;
  text-align: center;
  padding: 12px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--white);
  font-size: 14px;
  font-weight: 600;
}
.download-block__btn:hover { background: var(--gray-5); }

/* ===== 工作室标注：头像在左，文字在右，整体左对齐、垂直居中 ===== */

.kk-credit {
  display: flex;
  align-items: center;
  gap: 10px;
  text-align: left;
}

.kk-credit__avatar {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: var(--radius-sm);
  object-fit: cover;
}

.kk-credit__text {
  min-width: 0;
  font-size: 12.5px;
  line-height: 1.6;
  color: var(--gray-5);
}

.kk-credit__text p {
  margin: 0;
}

.kk-credit__link {
  color: var(--ink);
  text-decoration: underline;
}

/* ===== 矮屏/窄屏兜底：骰子画面本身已经用容器查询跟着实际剩余高度走，
   会自己缩小，这里只需要再压缩一下间距，给它多留一点可用高度 ===== */

@media (max-height: 750px) {
  :root {
    --mode-row-reserve: 66px;
  }
  .stage {
    gap: 6px;
    padding-top: 4px;
    padding-bottom: 12px;
  }
  .dice-stage-area {
    gap: 8px;
  }
  .mode-row {
    margin-top: 10px;
  }
  .dice-canvas-wrap {
    margin-bottom: 10px;
  }
}

@media (max-height: 620px) {
  :root {
    --mode-row-reserve: 54px;
  }
  .frame__header {
    padding: 14px 20px 8px;
  }
  .stage {
    gap: 4px;
    padding-top: 2px;
    padding-bottom: 8px;
  }
  .dice-stage-area {
    gap: 6px;
  }
  .mode-row {
    margin-top: 6px;
  }
  .dice-canvas-wrap {
    margin-bottom: 6px;
  }
}

/* 横屏手机这类极矮的窗口：连按钮本身也要一起缩小，不然「按钮 + 间距 +
   圆盘最小尺寸」三者加起来还是会比这块区域实际可用的高度大，被裁掉一块 */
@media (max-height: 460px) {
  :root {
    --mode-row-reserve: 38px;
    --dice-floor: 110px;
  }
  .frame__header {
    padding: 8px 16px 4px;
  }
  .frame__title {
    font-size: 15px;
  }
  .config-block {
    padding: 8px 12px;
    gap: 6px;
  }
  .stage {
    gap: 2px;
    padding-top: 0;
    padding-bottom: 4px;
  }
  .dice-stage-area {
    gap: 4px;
  }
  .mode-row {
    margin-top: 4px;
  }
  .dice-canvas-wrap {
    margin-bottom: 4px;
  }
  .mode-row__btn {
    min-height: 22px;
    padding: 4px 10px;
    font-size: 11px;
  }
}

@media (max-width: 360px) {
  .picker-row__list {
    gap: 6px;
  }
}
