/* Layout: 1画面に収まる「帳面」。上に罫線の得点表（2列）、下に親指で届くダイス皿と振るボタン */
:root {
  --paper: #e9edf5;      /* 藍がかった紙 */
  --sheet: #f8f9fc;      /* 帳面のページ */
  --ink: #1b2442;        /* 藍墨 */
  --ink-soft: #565f7e;
  --rule: #d3d9e7;       /* 罫線 */
  --shu: #c43b2c;        /* 朱（1の目・押印） */
  --hold: #2d4cb0;       /* キープ中 */
  --preview: #2d4cb0;    /* 記入候補の点 */
  --die: #fffdf7;        /* 象牙のダイス */
  --die-edge: #cfd4e2;
  --on-accent: #ffffff;
  --shadow: rgba(27, 36, 66, 0.16);
  --used: #e3e8f2;       /* 記入済みの欄（帳面に墨が入った所） */
  --bank: #ff9583;       /* 振るボタン（藍地）の上の貯金◆ */
  --pip: #1b2442;        /* ダイスは両テーマとも象牙色なので目の色は固定 */
  --pip-one: #c43b2c;    /* 和ダイスの赤い1 */

  --font-display: "Dela Gothic One", "Hiragino Sans", "Noto Sans JP", sans-serif;
  --font-body: "Zen Kaku Gothic New", "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", system-ui, sans-serif;

  --radius: 14px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0e1326; --sheet: #161d36; --ink: #e5e9f4; --ink-soft: #98a2c2; --rule: #29335a;
    --shu: #ff6b57; --hold: #7f9cff; --preview: #91aaff; --die: #f6f2e8; --die-edge: #8d94ab;
    --on-accent: #0e1326; --shadow: rgba(0, 0, 0, 0.45); --used: #0f152b; --bank: #a92e21;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #0e1326; --sheet: #161d36; --ink: #e5e9f4; --ink-soft: #98a2c2; --rule: #29335a;
  --shu: #ff6b57; --hold: #7f9cff; --preview: #91aaff; --die: #f6f2e8; --die-edge: #8d94ab;
  --on-accent: #0e1326; --shadow: rgba(0, 0, 0, 0.45); --used: #0f152b; --bank: #a92e21;
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }
html, body { height: 100%; }
body {
  margin: 0;
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font-body);
  -webkit-tap-highlight-color: transparent;
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}
button { font: inherit; color: inherit; }
[hidden] { display: none !important; }

.app {
  height: 100%;
  max-width: 520px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding-top: max(8px, env(safe-area-inset-top, 0px));
  padding-bottom: max(12px, env(safe-area-inset-bottom, 0px));
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
}

/* ---- ヘッダー ---- */
.bar {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 44px;
}
.logo {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  font-size: clamp(18px, 5.6vw, 22px);
  letter-spacing: 0.04em;
  white-space: nowrap;
  flex: 1;
}
.turn, .total { display: flex; align-items: baseline; gap: 4px; font-variant-numeric: tabular-nums; }
.turn span { font-size: 20px; font-weight: 700; }
.turn small, .total small { color: var(--ink-soft); font-size: 12px; }
.total b { font-weight: 700; font-size: clamp(22px, 6.6vw, 28px); min-width: 2.2em; text-align: right; font-variant-numeric: tabular-nums; }
.bar { gap: clamp(6px, 2.4vw, 12px); }
.icon-btn {
  width: 44px; height: 44px; border: 0; background: transparent; border-radius: 12px;
  display: grid; place-items: center; cursor: pointer;
}
.icon-btn svg { width: 24px; height: 24px; stroke: currentColor; stroke-width: 2; fill: none; stroke-linecap: round; }
.icon-btn:active { background: var(--rule); }

/* ---- 得点表 ---- */
.sheet {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}
.col {
  background: var(--sheet);
  border-radius: var(--radius);
  box-shadow: 0 1px 0 var(--rule), 0 6px 18px -12px var(--shadow);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  min-width: 0;
}
.row {
  flex: 1;
  min-height: 40px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 0 12px;
  border: 0;
  border-bottom: 1px solid var(--rule);
  background: transparent;
  text-align: left;
  cursor: pointer;
}
.row:last-child { border-bottom: 0; }
.row .name { font-size: clamp(13px, 3.9vw, 15px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; flex: 1; min-width: 0; }
.row .par { font-size: 11px; font-weight: 700; font-variant-numeric: tabular-nums; color: var(--ink-soft); }
.row .par.plus { color: var(--hold); }
.row .par.minus { color: var(--shu); }
.row .par.jackpot { color: var(--on-accent); background: var(--shu); border-radius: 6px; padding: 1px 5px; }
.row.info .name.stack { display: flex; flex-direction: column; line-height: 1.2; }
.row.info .name small { font-size: 11px; font-weight: 700; }
.row.info.lost .val { text-decoration: line-through; opacity: 0.6; }
.row.info.hard .val { font-size: 13px; }
.row .val {
  font-size: 19px;
  font-variant-numeric: tabular-nums;
  min-width: 2ch;
  text-align: right;
}
.row.used { background: var(--used); }
.row.used .name { color: var(--ink-soft); }
.row.used .val { font-weight: 700; }
.row.open .val { color: var(--ink-soft); opacity: 0.5; }
.row.preview .val { color: var(--preview); opacity: 1; font-weight: 700; }
.row.preview.zero .val { color: var(--ink-soft); opacity: 0.7; font-weight: 400; }
.row:disabled { cursor: default; }
.row.preview:active { background: var(--rule); }
.row:focus-visible, .die:focus-visible, .roll:focus-visible, .icon-btn:focus-visible, .btn:focus-visible {
  outline: 3px solid var(--hold);
  outline-offset: 2px;
}
.row.info { cursor: default; }
.row.info .name { color: var(--ink-soft); font-size: 13px; }
.row.info .val { font-size: 15px; color: var(--ink-soft); }
.row.info.got .val { color: var(--shu); font-weight: 700; }

/* ---- ダイス皿 ---- */
.tray { display: flex; flex-direction: column; gap: 8px; position: relative; }
.tip {
  position: absolute; left: 0; right: 0; bottom: calc(100% + 6px); z-index: 5;
  background: var(--ink); color: var(--paper);
  border-radius: 12px; padding: 10px 12px; font-size: 13px; line-height: 1.6;
  display: flex; gap: 10px; align-items: center;
  box-shadow: 0 8px 20px -8px var(--shadow);
}
.tip::after {
  content: ""; position: absolute; left: 50%; bottom: -6px; width: 12px; height: 12px;
  background: var(--ink); transform: translateX(-50%) rotate(45deg);
}
.tip button {
  flex: none; border: 0; border-radius: 8px; padding: 8px 12px; font-weight: 700; cursor: pointer;
  background: var(--paper); color: var(--ink);
}
.opt { display: flex; align-items: center; gap: 10px; font-size: 14px; padding: 6px 0; }
.opt input { width: 22px; height: 22px; accent-color: var(--hold); }
.review h3 { margin: 0 0 6px; font-size: 15px; }
.review ol { margin: 0; padding: 0; list-style: none; display: grid; gap: 8px; }
.review li { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; font-size: 13px; line-height: 1.5; padding: 8px 10px; border-radius: 10px; background: var(--used); }
.review li b { grid-column: 1; font-variant-numeric: tabular-nums; }
.review li span { grid-column: 1; color: var(--ink-soft); }
.review li small { grid-column: 1; font-size: 12px; color: var(--ink-soft); }
.review > .next { font-size: 12px; margin-bottom: 8px; }
.review li em { grid-column: 2; grid-row: 1 / span 3; align-self: center; font-style: normal; font-weight: 700; color: var(--shu); white-space: nowrap; }
/* ---- 記録 ---- */
.tiles { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tiles div { background: var(--used); border-radius: 12px; padding: 10px; display: grid; gap: 2px; min-width: 0; }
.tiles small { font-size: 11px; color: var(--ink-soft); }
.tiles b { font-size: 22px; font-variant-numeric: tabular-nums; }
.chart { display: grid; gap: 2px; }
.chart-title { font-size: 13px; font-weight: 700; }
.readout { font-size: 13px; color: var(--ink-soft); font-variant-numeric: tabular-nums; min-height: 1.6em; }
.spark { width: 100%; height: auto; touch-action: none; }
.spark .ln { fill: none; stroke: var(--ink); stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.spark .g { stroke: var(--rule); stroke-width: 1; stroke-dasharray: 3 3; }
.spark .gl { fill: var(--ink-soft); font-size: 10px; font-family: var(--font-body); }
.spark .best { fill: var(--shu); stroke: var(--sheet); stroke-width: 2; }
.spark .last { fill: var(--sheet); stroke: var(--ink); stroke-width: 2; }
.spark .cur { stroke: var(--ink-soft); stroke-width: 1; }
.chip {
  font-family: var(--font-body); font-size: 11px; font-weight: 700; letter-spacing: 0;
  vertical-align: middle; padding: 2px 7px; border-radius: 999px;
  border: 1.5px solid var(--shu); color: var(--shu);
}
.btn.warn { box-shadow: inset 0 0 0 2px var(--shu); color: var(--shu); font-size: 14px; }
.rules summary { cursor: pointer; font-weight: 700; padding: 8px 0; }
.rules { display: grid; gap: 10px; }
.rules[open] { gap: 12px; }
.share { width: 100%; min-height: 84px; font: inherit; font-size: 13px; padding: 8px; border-radius: 10px; border: 1px solid var(--rule); background: var(--paper); color: var(--ink); }
.howto { margin: 0; padding-left: 1.2em; display: grid; gap: 8px; line-height: 1.7; }
/* 小さい画面でも「はじめる」が常に見えるように、パネルの下端に貼り付ける */
.sticky-foot { position: sticky; bottom: -22px; background: var(--sheet); padding: 10px 0 0; box-shadow: 0 -14px 14px -8px var(--sheet); }
@media (max-height: 640px) {
  .howto { line-height: 1.55; gap: 6px; font-size: 14px; }
  .panel { padding: 18px 18px 22px; gap: 10px; }
}
.dice {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 10px;
  padding-top: 8px;
}
.die {
  aspect-ratio: 1;
  max-width: 100%;
  border-radius: 22%;
  border: 0;
  background: var(--die);
  box-shadow: inset 0 -4px 0 var(--die-edge), 0 4px 10px -4px var(--shadow);
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(3, 1fr);
  padding: 16%;
  cursor: pointer;
  position: relative;
  transition: transform 120ms ease;
}
.die .pip { width: 72%; aspect-ratio: 1; border-radius: 50%; background: var(--pip); place-self: center; }
.die .pip.big { width: 150%; background: var(--pip-one); }
.die.blank .pip { visibility: hidden; }
.die.blank { opacity: 0.45; cursor: default; }
.die.held { transform: translateY(-6px); box-shadow: 0 0 0 3px var(--hold), 0 8px 14px -6px var(--shadow); }
.die.held::after {
  content: "キープ";
  position: absolute; left: 50%; bottom: -20px; transform: translateX(-50%);
  font-size: 11px; font-weight: 700; color: var(--hold); white-space: nowrap;
}
.msg {
  margin: 10px 0 0;
  height: 32px;
  text-align: center;
  font-size: 14px;
  color: var(--ink-soft);
}
.roll {
  height: 58px;
  border: 0;
  border-radius: 16px;
  background: var(--ink);
  color: var(--paper);
  font-size: 19px;
  font-weight: 700;
  display: flex; align-items: center; justify-content: center; gap: 12px;
  cursor: pointer;
}
.roll:disabled { opacity: 0.35; cursor: default; }
.roll .pips { display: inline-flex; gap: 5px; }
.roll .pips i { width: 9px; height: 9px; border-radius: 50%; border: 2px solid currentColor; }
.roll .pips i.on { background: currentColor; }
.roll .bank { display: inline-flex; gap: 4px; padding-left: 10px; border-left: 1px solid currentColor; }
.roll .bank b { width: 10px; height: 10px; transform: rotate(45deg); border: 2px solid var(--bank); }
.roll .bank b.on { background: var(--bank); }
.roll.bank-mode { background: var(--shu); color: var(--on-accent); }
.roll.bank-mode .bank b { border-color: currentColor; }
.roll.bank-mode .bank b.on { background: currentColor; }

/* ---- 記入の押印 ---- */
@keyframes stamp {
  0% { transform: scale(1.9) rotate(-10deg); color: var(--shu); opacity: 0; }
  55% { transform: scale(0.94) rotate(2deg); color: var(--shu); opacity: 1; }
  100% { transform: scale(1) rotate(0); }
}
.row.stamped .val { animation: stamp 420ms cubic-bezier(.2,.8,.3,1.2); display: inline-block; }
.row.stamped.quiet .val { animation: fade-in 300ms ease; }
@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }
/* 大きな手: 押印を大きく、朱の輪が広がる */
@keyframes stamp-big {
  0% { transform: scale(2.8) rotate(-14deg); color: var(--shu); opacity: 0; }
  50% { transform: scale(0.9) rotate(3deg); color: var(--shu); opacity: 1; }
  75% { transform: scale(1.08); color: var(--shu); }
  100% { transform: scale(1) rotate(0); }
}
@keyframes ring {
  from { transform: scale(0.3); opacity: 0.9; }
  to { transform: scale(2.4); opacity: 0; }
}
.row { position: relative; }
.row.stamped.grand .val { animation: stamp-big 640ms cubic-bezier(.2,.8,.3,1.2); }
.row.stamped.grand::after {
  content: ""; position: absolute; right: 8px; top: 50%; width: 44px; height: 44px; margin-top: -22px;
  border-radius: 50%; border: 3px solid var(--shu); pointer-events: none;
  animation: ring 620ms ease-out forwards;
}
@keyframes flash { 0%, 100% { background: transparent; } 30% { background: var(--shu); } }
.row.info.flash { animation: flash 900ms ease; }
@keyframes flash-text { 0%, 100% { color: inherit; } 30% { color: var(--on-accent); } }
.row.info.flash .val, .row.info.flash .name { animation: flash-text 900ms ease; }
@keyframes jump {
  0% { transform: translateY(0); } 35% { transform: translateY(-16px) rotate(-6deg); }
  65% { transform: translateY(0) rotate(3deg); } 80% { transform: translateY(-5px); } 100% { transform: translateY(0); }
}
.die.jump { animation: jump 620ms ease both; }
/* 振り直したダイス: 下に小さな印が一瞬出る */
@keyframes fresh-mark { 0% { opacity: 0; transform: translateX(-50%) scale(0.4); } 20% { opacity: 1; transform: translateX(-50%) scale(1); } 80% { opacity: 1; } 100% { opacity: 0; } }
.die.fresh::before {
  content: ""; position: absolute; left: 50%; bottom: -12px; width: 6px; height: 6px; border-radius: 50%;
  background: var(--ink-soft); transform: translateX(-50%); animation: fresh-mark 900ms ease forwards;
}
.undo {
  border: 0; background: transparent; color: var(--hold); font-weight: 700;
  text-decoration: underline; text-underline-offset: 3px; padding: 6px 8px; cursor: pointer;
}
.msg { display: flex; align-items: center; justify-content: center; gap: 2px; }

/* ---- 振るアニメーション ---- */
@keyframes tumble {
  0% { transform: rotate(0) scale(1); }
  30% { transform: rotate(-14deg) scale(0.92); }
  60% { transform: rotate(10deg) scale(1.04); }
  100% { transform: rotate(0) scale(1); }
}
.die.rolling { animation: tumble 380ms ease; }
@media (prefers-reduced-motion: reduce) {
  .die.rolling, .die.jump, .row.stamped .val, .row.stamped.grand .val, .row.info.flash, .row.info.flash .val, .row.info.flash .name { animation: none; }
  .row.stamped.grand::after, .die.fresh::before { display: none; }
  .die { transition: none; }
}

/* ---- オーバーレイ ---- */
.overlay {
  position: fixed; inset: 0;
  background: rgba(10, 14, 30, 0.55);
  display: grid; place-items: end center;
  padding: 16px max(16px, env(safe-area-inset-right, 0px)) max(16px, env(safe-area-inset-bottom, 0px)) max(16px, env(safe-area-inset-left, 0px));
  z-index: 20;
}
.panel {
  width: 100%;
  max-width: 480px;
  max-height: 88%;
  overflow: auto;
  background: var(--sheet);
  border-radius: 20px;
  padding: 22px 20px;
  display: flex; flex-direction: column; gap: 14px;
}
.panel h2 { margin: 0; font-family: var(--font-display); font-weight: 400; font-size: 22px; }
.panel p { margin: 0; line-height: 1.7; }
.panel .big { font-family: var(--font-display); font-size: 56px; line-height: 1; text-align: center; }
.panel table { width: 100%; border-collapse: collapse; font-size: 14px; }
.panel td { padding: 6px 4px; border-bottom: 1px solid var(--rule); }
.panel td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
/* ---- 結果 ---- */
.result-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; }
.eyebrow { font-size: 12px; color: var(--ink-soft); letter-spacing: 0.08em; }
.panel .result-head .big { text-align: left; }
.panel .big small { font-family: var(--font-body); font-size: 16px; margin-left: 4px; }
.seal {
  flex: none;
  width: 92px; height: 92px; border-radius: 50%;
  border: 3px solid var(--shu); color: var(--shu);
  display: grid; place-items: center;
  transform: rotate(-8deg);
  font-family: var(--font-display); font-size: 21px; line-height: 1.1; text-align: center;
  box-shadow: inset 0 0 0 4px var(--sheet), inset 0 0 0 5px var(--shu);
  animation: stamp 520ms 200ms cubic-bezier(.2,.8,.3,1.2) both;
}
.seal span { max-width: 4em; }
.next { color: var(--ink-soft); font-size: 14px; }
.notes { margin: 0; padding: 0; list-style: none; display: flex; flex-wrap: wrap; gap: 6px; }
.notes li { font-size: 13px; padding: 4px 10px; border-radius: 999px; background: var(--used); }
@media (prefers-reduced-motion: reduce) { .seal { animation: none; } }

.btn {
  height: 52px; border-radius: 14px; border: 0; cursor: pointer;
  background: var(--ink); color: var(--paper); font-weight: 700; font-size: 17px;
}
.btn.sub { background: transparent; color: var(--ink); box-shadow: inset 0 0 0 2px var(--rule); }
.btns { display: grid; gap: 10px; }

@media (max-height: 640px) {
  .row { min-height: 34px; }
  .row .name { font-size: 14px; }
  .row .val { font-size: 17px; }
  .dice { gap: 8px; padding-top: 4px; }
  .die { padding: 18%; }
  .roll { height: 52px; }
  .app { gap: 8px; }
}
