/* ===== 设计令牌：黑白灰，主色白 ===== */
: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;
  --frame-width: min(620px, 92vw);
}

* { 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;
  height: 100dvh;
  background: var(--white);
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

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

.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(15px, 5.4vw, 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;
  gap: 6px;
}

/* 骰子画面区：撑满投掷方式下面剩余的全部空间（不用 justify-content:center
   去猜居中，直接让这块区域吃掉所有多余空间），再用容器查询量出这块区域
   实际的宽高，取较小的一边当正方形边长——避免用 vh 之类跟视口挂钩的公式去
   估算「大概有多少高度」，因为视口高度和这块区域实际剩余的高度是两回事，
   猜错了就会出现「明明还有空间，圆盘却没长满」的留白。 */
.dice-stage-area {
  flex: 1;
  min-height: 0;
  width: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
  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.is-inactive {
  visibility: hidden;
}

.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）要一起改，
     只改一边不会有效果 */
  width: clamp(160px, min(100cqw, 100cqh), 680px);
  aspect-ratio: 1 / 1;
}

.dice-plate {
  position: absolute;
  inset: 8%;
  border-radius: 50%;
  background: var(--gray-1);
}

.dice-stage {
  position: absolute;
  inset: 0;
  cursor: pointer;
}
.dice-stage canvas { width: 100% !important; height: 100% !important; display: block; }

/* 立即投掷模式：不会真的跑物理动画，骰子模型不显示；但圆盘桌面本身要留着，
   不然点击区域看起来就是一片空白，找不到该点哪里 */
.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;
  /* 跟上面的属性/高级面板留出比跟下面骰子圆盘更大的距离——视觉上归到
     骰子这一组里，不是紧贴在上面那块面板下面 */
  margin-top: 10px;
  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;
  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;
}

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

@media (max-height: 750px) {
  .stage {
    gap: 6px;
    padding-top: 4px;
    padding-bottom: 12px;
  }
  .stage__middle {
    gap: 8px;
  }
}

@media (max-height: 620px) {
  .frame__header {
    padding: 14px 20px 8px;
  }
  .stage {
    gap: 4px;
    padding-top: 2px;
    padding-bottom: 8px;
  }
  .stage__middle {
    gap: 6px;
  }
}

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