:root {
  --paper: #f3ecdc;
  --sheet: #fffdf6;
  --ink: #221c15;
  --sub: #6b5f50;
  --rule: #d8cbb4;
  --shu: #d8402c;
  --g: #3f9d4a;
  --y: #d9a92b;
  --r: #e5792b; /* 遠のいた手。赤だと失敗に見えるので橙（黄の --y と見分けられる濃さ） */
  --serif: "Shippori Mincho", "Hiragino Mincho ProN", "Yu Mincho", YuMincho, "Noto Serif JP", "Noto Serif CJK JP", serif;
  --sans: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", "Yu Gothic", Meiryo, sans-serif;
}
* { box-sizing: border-box; }
/* ページ（html・body）はスクロールさせない。縦に長い画面（タイトル・リザルト）は .screen の中で流す。
   スマホのキーボードが出てもブラウザにページごと動かされないようにするため。 */
html, body { height: 100%; margin: 0; background: var(--paper); color: var(--ink); font-family: var(--sans); -webkit-text-size-adjust: 100%; overflow: hidden; overscroll-behavior: none; }
/* .screen の height:100% を効かせる。あいだの #app をはさまないと、%の高さが auto になる。 */
#app { height: 100%; }
button { font: inherit; color: inherit; }
.hidden { display: none !important; }

.screen {
  max-width: 520px; margin: 0 auto; height: 100%; overflow-y: auto; -webkit-overflow-scrolling: touch;
  padding: max(14px, env(safe-area-inset-top)) 16px calc(28px + env(safe-area-inset-bottom));
}

.btn {
  appearance: none; border: 2px solid var(--ink); background: var(--sheet); border-radius: 12px;
  padding: 12px 20px; min-height: 46px; font-weight: 700; font-size: 17px; cursor: pointer; touch-action: manipulation;
}
.btn.primary { background: var(--ink); color: var(--sheet); }
.btn.plain { background: transparent; border-color: var(--rule); }
.btn:disabled { opacity: .45; cursor: default; }
.btn:active:not(:disabled) { transform: translateY(1px); }
.link { appearance: none; border: 0; background: none; color: var(--sub); text-decoration: underline; font-size: 14px; padding: 10px 8px; min-height: 44px; cursor: pointer; }

/* --- title --- */
.title { text-align: center; padding-top: max(48px, env(safe-area-inset-top)); display: flex; flex-direction: column; align-items: center; }
/* 縦に余った高さは、ロゴから「問題を作る」までの上下に等しく分け、クレジットは下端に置く（高さが足りなければ詰めて流れる）。 */
.title > :first-child { margin-top: auto; }
/* 高さが足りないとき（X のアプリ内ブラウザ・共有のお題）に、ボタンが縮んで字が下へ押し出されないよう、縮めずに流す。 */
.title > * { flex-shrink: 0; }
.title .titlelinks { margin-bottom: auto; }
.title > :only-child { margin-bottom: auto; } /* 読み込みに失敗したときの1文は真ん中に */
.logo { font-family: var(--serif); font-weight: 700; font-size: 62px; letter-spacing: .08em; margin: 8px 0 20px; }
.lead { font-family: var(--serif); font-size: 20px; line-height: 1.7; margin: 0 0 22px; }
.badlink { margin: -6px 0 18px; padding: 8px 14px; border: 1px solid var(--shu); border-radius: 10px; color: var(--shu); font-size: 14px; line-height: 1.5; background: #fffaf0; }
.today { margin: 0 0 18px; color: var(--sub); font-size: 15px; }
.today b { font-family: var(--serif); font-size: 22px; color: var(--ink); margin-right: 8px; }
.title .btn.primary { min-width: 220px; font-size: 20px; padding: 15px 28px; }
.title .btn.plain { display: block; margin: 10px auto 0; min-width: 220px; } /* 2つ縦に並ぶので、はじめると同じ幅にそろえる */
.sub { color: var(--sub); font-size: 13px; margin: 14px 0 10px; }
.titlelinks { display: flex; flex-direction: column; align-items: center; gap: 2px; margin-bottom: 18px; }
.credit { margin-top: 26px; font-size: 11px; color: #9a8d7c; text-align: center; }
.credit a { color: inherit; }

/* --- play --- */
/* 外枠は position: fixed。高さは visualViewport.height、位置は visualViewport.offsetTop に合わせる
   （--app-h と --app-top を JS が更新する）。iOS がキーボードでページを上へずらしても、
   外枠だけが画面いっぱいに残り、はしご・ゴール・入力欄が見え続ける。 */
.play {
  position: fixed; top: 0; left: 0; right: 0; margin: 0 auto; width: 100%; max-width: 520px;
  height: var(--app-h, 100dvh); transform: translateY(var(--app-top, 0px));
  padding: 0; overflow: hidden; display: flex; flex-direction: column;
}
.playtop {
  display: flex; align-items: center; gap: 10px; padding: max(10px, env(safe-area-inset-top)) 16px 6px;
}
.top { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.top .no { font-family: var(--serif); font-weight: 700; font-size: 17px; }
.top .date { color: var(--sub); font-size: 13px; margin-left: auto; white-space: nowrap; }
.top .par { color: var(--sub); font-size: 12px; white-space: nowrap; font-variant-numeric: tabular-nums; }
.giveup { appearance: none; border: 0; background: none; color: var(--sub); text-decoration: underline; font-size: 14px; padding: 10px 4px; min-height: 44px; cursor: pointer; display: inline-flex; align-items: center; }

.board { flex: 1; overflow-y: auto; padding: 6px 16px 10px; -webkit-overflow-scrolling: touch; overscroll-behavior: contain; display: flex; flex-direction: column; }
/* スタートとゴールも、はしごの行と同じ3列（左の印・中央のタイル・右端）。中央のタイルは
   はしごの列と同じ中心線にそろう。見出しの行は作らず、左の印の列に小さく「始」「終」を出す。 */
.anchor {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  padding: 3px 4px; margin: 4px 0; border-radius: 10px;
}
.ladder { flex: 1 0 auto; }
.anchor.goal { margin-top: auto; }

/* キーボードが出て高さが縮んだとき（.play.compact）: スタートとゴールの大タイルをはしごの行と
   同じ大きさにして、ゴールは入力欄の帯のすぐ上に1行で出し続ける。スタートははしごと一緒に
   スクロールで上に流れてよい。キーボードが閉じれば元の配置に戻る。 */
.goalbar { display: none; }
.play.compact .goalbar {
  animation: goalin .24s ease;
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  padding: 2px 4px 6px;
}
.play.compact .anchor { margin: 2px 0; }
@keyframes goalin { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.play.compact .anchor.goal { display: none; }
/* はしごの左端の印の列に出す、スタート（始）とゴール（終）の小さな印。明朝・薄い墨色。 */
.endlabel { font-family: var(--serif); font-size: 13px; color: var(--sub); opacity: .6; justify-self: start; }
.tiles { display: inline-flex; gap: 6px; }
.tile {
  width: 44px; height: 44px; border: 2px solid var(--ink); border-radius: 9px; background: var(--sheet);
  display: inline-flex; align-items: center; justify-content: center;
  font-family: var(--serif); font-weight: 600; font-size: 27px; line-height: 1;
  backface-visibility: hidden;
}
.anchor .tile { width: 66px; height: 66px; font-size: 40px; border-radius: 12px; }
/* iOS（WebKit）はキーボードを出すとき、ページごと滑らかに上へずらして入力欄を見せる。それを打ち消さずに
   任せ、かわりにスタートとはしごをゴールのすぐ上まで押し下げる（JS が margin-top を入れる）。
   押し下げはキーボードと同じくらいの速さで動かし、上の方が跳ねずに続けて動いて見えるようにする。 */
.anchor.start { transition: margin-top .32s cubic-bezier(.2, .8, .2, 1); }
.tile.goal { border-color: var(--shu); color: var(--shu); }
.tile.new .ch { animation: flash .7s ease; }
.tile.goal.new .ch { animation: none; } /* ゴールの字は朱のまま（墨へ戻る点滅のあと朱に跳ねないように） */
.tile.hit .ch { color: var(--shu); } /* ゴールと同じ位置に同じ字（枠は墨のまま。枠まで朱なのはゴールだけ） */
.tile.hit.new .ch { animation: none; }
.tile.flip { animation: flip .4s ease; }
@keyframes flip { 0% { transform: rotateY(0); } 45% { transform: rotateY(88deg); } 100% { transform: rotateY(0); } }
@keyframes flash { 0%, 55% { color: var(--shu); } 100% { color: var(--ink); } }
/* 足したタイルは幅0から割り込む。消すタイルは下に落ちて消える。 */
.tile.insert { overflow: hidden; animation: insert .42s cubic-bezier(.2, .8, .3, 1); }
@keyframes insert { 0% { width: 0; opacity: .2; border-width: 0; } 100% { width: 44px; opacity: 1; border-width: 2px; } }
.tile.drop { animation: drop .38s ease forwards; }
@keyframes drop { 0% { transform: translateY(0); opacity: 1; } 100% { transform: translateY(30px); opacity: 0; } }
.tile.bad { border-color: var(--shu); }
.tile.bad .ch { color: var(--shu); }
.tile.bad.hi { animation: badflash .5s 2; }
@keyframes badflash { 50% { background: #ffe3dc; border-color: var(--shu); } }

.ladder { list-style: none; margin: 2px 0; padding: 0; }
.row {
  display: grid; grid-template-columns: 1fr auto 1fr; align-items: center; gap: 8px;
  padding: 3px 4px; border-radius: 10px; cursor: pointer;
}
.row:hover { background: rgba(0,0,0,.03); }
.mark { width: 10px; height: 10px; border-radius: 50%; background: var(--rule); display: inline-block; justify-self: start; }
.mark.g { background: var(--g); }
.mark.y { background: var(--y); }
.mark.r { background: var(--r); }
.rem { justify-self: end; color: var(--sub); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.remwrap { justify-self: end; display: inline-flex; align-items: center; justify-content: flex-end; min-width: 0; }
/* 左の印・中央のタイル・右端のアイコンの列を明示して、子の数が変わってもタイルを中央に保つ。 */
.row .mark, .anchor .endlabel, .goalbar .endlabel { grid-column: 1; }
.row .tiles, .anchor .tiles, .goalbar .tiles { grid-column: 2; }
.row .rem, .row .remwrap, .anchor .remwrap, .goalbar .remwrap { grid-column: 3; }

/* 選んだ行にだけ出す「ここまで戻す」のアイコン。行の右端（前の「あとN」の位置）に出す。 */
.undobtn {
  display: none; appearance: none; border: 0; background: none; color: var(--ink);
  position: relative; width: 44px; height: 44px; padding: 0; flex: none;
  align-items: center; justify-content: center; border-radius: 50%;
  cursor: pointer; touch-action: manipulation;
}
.undobtn::before {
  content: ''; position: absolute; width: 30px; height: 30px; border-radius: 50%;
  background: rgba(34,28,21,.10);
}
.undobtn svg { position: relative; width: 24px; height: 24px; display: block; }
.undobtn:active { transform: scale(.94); }
.row.selected, .anchor.start.selected { background: #e6dcc8; } /* 薄い墨色。朱は使わない */
.row.selected .undobtn, .anchor.start.selected .undobtn { display: inline-flex; }
.row.selected .rem, .anchor.start.selected .rem { display: none; }

.foot { flex: none; padding: 6px 16px calc(10px + env(safe-area-inset-bottom)); border-top: 1px solid var(--rule); background: var(--paper); }
.inputbar { display: flex; gap: 8px; }
.inputbar input {
  flex: 1; min-width: 0; font-size: 18px; padding: 11px 12px; min-height: 46px;
  border: 2px solid var(--ink); border-radius: 12px; background: var(--sheet); color: var(--ink);
}
/* placeholder「一字替える・足す・消す」は、入力欄の内側の幅（360px幅で約158px）に収めるため、
   入力の文字（18px）より小さくする。 */
.inputbar input::placeholder { font-size: 14px; }
.inputbar input:focus { outline: none; box-shadow: inset 0 0 0 1px var(--ink); } /* 枠を太く見せる。border-width を変えると高さが2px動いてちらつく */
.btn.put { background: var(--ink); color: var(--sheet); min-width: 74px; }
.inputbar.shake { animation: shake .35s; }
@keyframes shake { 20%, 60% { transform: translateX(-7px); } 40%, 80% { transform: translateX(7px); } }
.hint { text-align: center; color: var(--shu); font-size: 13px; margin: 0 0 6px; }
.hint.taphint { color: var(--sub); } /* 初回の案内は弾かれた赤い字と見分けられるよう、墨の薄い色で出す */
.msg { color: var(--shu); font-size: 13px; opacity: 0; transition: opacity .2s; }
.msg.show { opacity: 1; }
.echo { display: flex; justify-content: center; gap: 6px; opacity: 0; transition: opacity .2s; }
.echo.show { opacity: 1; }

/* 入力欄の上の帯。何も出ていないときも同じ高さを空けておき、中身だけ差し替える。
   「元に戻す」が入る高さにそろえ、中身は1行に収める。 */
.notices { position: relative; height: 46px; }
.notices > * {
  position: absolute; left: 0; right: 0; top: 50%; transform: translateY(-50%);
  display: flex; align-items: center; justify-content: center; margin: 0;
  pointer-events: none; white-space: nowrap;
}
/* 弾かれた赤い字とメッセージは、帯のほかの文に重ならないよう上に重ねて地の色で覆う。 */
.notices .echo, .notices .msg { z-index: 3; background: var(--paper); }
/* 元に戻す（消した直後だけ、入力欄の上に出す） */
.notices .undobar { z-index: 2; pointer-events: auto; }
.notices .undobar[hidden] { display: none; }
.undorestore { position: relative; min-height: 40px; padding: 7px 20px; font-size: 14px; border-color: var(--shu); color: var(--shu); background: var(--sheet); }
.undorestore::after { content: ''; position: absolute; inset: -4px -6px; } /* 見た目は40pxのまま、押せる範囲を44px以上に広げる */

/* ヒント（電球ボタンと、1段目の朱の印） */
.hintbtn {
  display: inline-flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 1px; padding: 4px 6px; min-width: 48px; min-height: 46px;
}
.hintbtn svg { width: 20px; height: 20px; color: var(--shu); transition: transform .15s; }
/* キーボード中の1回目: 電球が光って、もう一度押すのを待つ */
.hintbtn.armed { background: var(--shu); border-color: var(--shu); }
.hintbtn.armed svg { color: var(--sheet); transform: scale(1.12); }
.hintbtn.armed .hintstep { color: var(--sheet); }
.hintbtn .hintstep { font-size: 9px; line-height: 1; color: var(--sub); font-variant-numeric: tabular-nums; }
.hintmsg { text-align: center; font-size: 13px; color: var(--shu); }
.hintmsg:empty { display: none; }
.hintline { margin-right: 8px; }
.hintread { font-family: var(--serif); color: var(--ink); }
.tile.hintReplace, .tile.hintRemove { border-color: var(--shu); border-style: dashed; }
.tile.hintRemove .ch { position: relative; display: inline-block; }
.tile.hintRemove .ch::after {
  content: ''; position: absolute; left: -3px; right: -3px; top: 52%;
  border-top: 2px solid var(--shu); transform: rotate(-18deg);
}
.tile.hintAdd { border: 2px dashed var(--shu); background: transparent; animation: hintpulse 1.2s ease infinite; }
@keyframes hintpulse { 0%, 100% { opacity: .5; } 50% { opacity: 1; } }
.hintback { box-shadow: 0 0 0 3px rgba(216,64,44,.4); animation: glow .9s ease infinite alternate; }
@keyframes glow { from { box-shadow: 0 0 0 2px rgba(216,64,44,.2); } to { box-shadow: 0 0 0 5px rgba(216,64,44,.55); } }
.hintcount { margin-left: 10px; }

/* --- result --- */
/* ヘッダーはプレイ中と同じ高さ・同じ上端にして、クリアした瞬間に #No と日付が跳ねないようにする。 */
.screen.result { padding-top: max(10px, env(safe-area-inset-top)); }
.result > .top { min-height: 44px; }
.verdict { text-align: center; margin: 10px 0 12px; }
.verdict .big { font-family: var(--serif); font-size: 24px; font-weight: 700; margin: 0; }
.verdict .big b { font-size: 64px; line-height: 1; margin-right: 2px; }
.verdict .big b.ng { font-size: 46px; }
.verdict .meta { margin: 4px 0 0; color: var(--sub); font-size: 14px; }
.verdict .cheer { margin: 8px 0 0; font-family: var(--serif); font-size: 18px; font-weight: 600; }
.final { list-style: none; margin: 6px 0 14px; padding: 0; }
/* リザルトのはしごもプレイ中と同じ3列（1fr auto 1fr）にして、タイルを中央の軸にそろえる。
   色の丸は左端、あとN は右端のまま。 */
.final .row { cursor: default; }
.final .row.goal { background: rgba(216,64,44,.06); }
.final .rem { font-size: 13px; }
.box { background: var(--sheet); border: 1px solid var(--rule); border-radius: 8px; padding: 10px 14px; margin: 0 0 14px; }
.box h2 { font-family: var(--serif); font-size: 15px; margin: 0 0 6px; }
/* みんなの手数（毎日の問題のリザルト） */
.dist-top { margin: 0 0 8px; font-size: 13px; color: var(--sub); }
.dist-top b { color: var(--shu); font-size: 18px; font-family: var(--serif); }
.dist-top span { font-size: 12px; }
.dist-empty { margin: 0; color: var(--sub); font-size: 13px; }
.dist { list-style: none; margin: 0; padding: 0; display: grid; gap: 4px; }
.dist li { display: grid; grid-template-columns: 64px 1fr 32px; align-items: center; gap: 8px; font-size: 12px; color: var(--sub); }
.dist li i { display: block; height: 14px; width: var(--w); background: #cdbfa8; border-radius: 2px; }
.dist li em { font-style: normal; text-align: right; font-variant-numeric: tabular-nums; }
.dist li.me { color: var(--shu); font-weight: 700; }
.dist li.me i { background: var(--shu); }
.example { margin: 0; font-family: var(--serif); font-size: 18px; line-height: 2.1; }
.example .step { white-space: nowrap; } /* 「→熟語」をひとまとまりにして、熟語の途中や矢印の後ろで折り返さない */
.example ruby { ruby-align: start; } /* ルビが長くても字の間を空けない */
.pw { margin: 0 2px; }
.example rt { font-size: 10px; color: var(--sub); font-family: var(--sans); }
.arrow { color: var(--sub); margin: 0 2px; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); margin: 0 0 14px; text-align: center; }
.stats dt { font-size: 11px; color: var(--sub); }
.stats dd { margin: 2px 0 0; font-family: var(--serif); font-size: 24px; font-weight: 700; }
.stats dd small { font-size: 12px; font-weight: 400; }
.next { text-align: center; color: var(--sub); font-size: 14px; margin: 4px 0 12px; }
.next b { font-family: var(--serif); font-size: 20px; color: var(--ink); font-variant-numeric: tabular-nums; margin-left: 6px; }
.actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; margin: 0 0 12px; }
/* 並んだボタンは同じ幅にそろえる（1つだけの行も同じ幅）。320px 幅でも2つ並ぶ。 */
.actions .btn { flex: 1 1 130px; max-width: 180px; padding-left: 12px; padding-right: 12px; }
.btn.x { display: inline-flex; align-items: center; justify-content: center; gap: 4px; }
.btn.x svg { width: 20px; height: 20px; }
.card { display: block; width: 100%; height: auto; margin: 4px 0 14px; border-radius: 6px; border: 1px solid var(--rule); }
.sn-after { margin-top: 6px; }

/* --- achievements --- */

.sn-after, .sn-guide-link { color: inherit; }

@media (prefers-reduced-motion: reduce) {
  .anchor.start { transition: none; }
  .play.compact .goalbar { animation: none; }
}

/* 共有URLのタイトルと「問題を作る」: 始め → 終わりのタイル */
.sharedpz { display: flex; align-items: center; justify-content: center; gap: 10px; margin: 6px 0 4px; }
.sharedpz .arrow { font-family: var(--serif); font-size: 22px; color: var(--sub); }
.sharedpz + .sub { margin: 4px 0 18px; }
/* 3字→3字でも 320px 幅に収める */
@media (max-width: 374px) {
  .sharedpz { gap: 6px; }
  .sharedpz .tile { width: 38px; height: 38px; font-size: 23px; }
}

/* 問題を作る */
.make .top { margin: 6px 0 14px; }
.makelead { color: var(--sub); font-size: 13px; text-align: center; margin: 0 0 14px; }
.makelead span { display: inline-block; } /* 折り返すなら「、」のあとで */
.makeform { display: flex; flex-direction: column; align-items: center; gap: 10px; margin: 0 0 16px; }
/* 入力欄を画面の中心線に置き、始・終の印は左にぶら下げる（右は同じ幅の空き）。確かめるも入力欄と同じ幅。 */
.makerow { display: grid; grid-template-columns: 24px 180px 24px; align-items: center; gap: 8px; }
.makerow input {
  font-family: var(--serif); font-size: 26px; text-align: center; letter-spacing: .12em;
  padding: 8px 10px; min-height: 52px; width: 100%;
  border: 2px solid var(--ink); border-radius: 12px; background: var(--sheet); color: var(--ink);
}
.makerow input:focus { outline: none; box-shadow: inset 0 0 0 1px var(--ink); }
.makerow input::placeholder { color: #c9bda8; }
.makeform .btn.primary { width: 180px; margin-top: 4px; }
.makeres { text-align: center; min-height: 40px; margin-bottom: 12px; }
/* できた問題のボタンは、入力欄・確かめると同じ幅で縦に並べる */
.makeacts { flex-direction: column; align-items: center; }
.makeacts .btn, .make > .actions .btn { flex: none; width: 180px; max-width: none; }
.makeerr { color: var(--shu); font-size: 14px; margin: 6px 0; }

/* 一瞬だけ出る知らせ（URLをコピーした など） */
.toast {
  position: fixed; left: 50%; bottom: calc(28px + env(safe-area-inset-bottom)); transform: translate(-50%, 8px);
  background: var(--ink); color: var(--sheet); font-size: 14px; padding: 9px 16px; border-radius: 999px;
  opacity: 0; pointer-events: none; transition: opacity .18s, transform .18s; z-index: 50; white-space: nowrap;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
