:root {
  --shu: #c8102e;        /* 朱 */
  --shu-d: #9c0c23;
  --navy: #22305a;
  --navy-d: #17213f;
  --paper: #fbf8f0;
  --paper-2: #f2ecdc;
  --ink: #2a2620;
  --muted: #7a7062;
  --line: #d9cfb8;
  --wood: #7a4e2c;
  --wood-d: #5c3a20;
  --gold: #c9a23a;
  --r: 14px;
  --safe-b: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  font-family: "Zen Maru Gothic", "Hiragino Maru Gothic ProN", sans-serif;
  color: var(--ink);
  background: var(--wood-d);
  -webkit-user-select: none; user-select: none;
}
input { -webkit-user-select: text; user-select: text; }
button { font-family: inherit; cursor: pointer; }
#app { position: fixed; inset: 0; }

/* ---------- 画面 ---------- */
.screen {
  position: absolute; inset: 0; display: none; flex-direction: column;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(255,255,255,.12), transparent 60%),
    repeating-linear-gradient(92deg, rgba(0,0,0,.05) 0 2px, transparent 2px 9px),
    linear-gradient(160deg, #8a5a34, #6a4225);
}
.screen.active { display: flex; }
.scroll { flex: 1; overflow-y: auto; padding: 14px 14px calc(30px + var(--safe-b)); -webkit-overflow-scrolling: touch; }
.scroll > * { max-width: 640px; margin-left: auto; margin-right: auto; }

.bar {
  display: grid; grid-template-columns: 90px 1fr 90px; align-items: center;
  padding: calc(8px + env(safe-area-inset-top, 0px)) 10px 8px;
  background: rgba(30,18,8,.35); color: #fff;
}
.bar h2 { margin: 0; text-align: center; font-family: "Shippori Mincho B1", serif; font-size: 20px; letter-spacing: .1em; }
.back { background: none; border: 0; color: #fff; font-size: 15px; text-align: left; padding: 6px; }

/* ---------- タイトル ---------- */
.title-wrap {
  margin: auto; width: min(420px, 92vw);
  background: var(--paper);
  border-radius: 6px;
  padding: 30px 22px 18px;
  box-shadow: 0 2px 0 var(--paper-2), 0 4px 0 #e2d8c2, 0 6px 0 #d8ccb1, 0 18px 40px rgba(0,0,0,.45);
  position: relative;
  max-height: calc(100% - 24px); overflow-y: auto;
}
.logo { text-align: center; position: relative; padding-bottom: 10px; border-bottom: 1px dashed var(--line); margin-bottom: 14px; }
.logo-sub { font-size: 12px; color: var(--muted); letter-spacing: .2em; }
.logo h1 { margin: 6px 0 0; font-family: "Shippori Mincho B1", serif; font-weight: 800; font-size: 46px; letter-spacing: .06em; line-height: 1.1; }
.logo h1 span { display: block; font-size: 22px; color: var(--shu); letter-spacing: .3em; }
.logo-seal {
  position: absolute; right: 2px; top: 6px; width: 56px; height: 56px; border-radius: 50%;
  border: 3px solid var(--shu); color: var(--shu); display: grid; place-items: center;
  font-family: "Shippori Mincho B1", serif; font-weight: 800; font-size: 28px;
  transform: rotate(12deg); opacity: .85; animation: pon 2.6s ease-in-out infinite;
}
@keyframes pon { 0%, 70%, 100% { transform: rotate(12deg) scale(1); } 76% { transform: rotate(12deg) scale(1.25); opacity: .4; } 82% { transform: rotate(12deg) scale(.95); opacity: .9; } }
.user-chip { text-align: center; font-size: 13px; color: var(--muted); margin-bottom: 12px; min-height: 18px; }
.user-chip b { color: var(--ink); }
.user-chip .rank { display: inline-block; background: var(--navy); color: #fff; border-radius: 99px; padding: 1px 9px; margin-left: 6px; font-size: 12px; }
.menu { display: grid; gap: 10px; }
.auth-row { display: flex; gap: 8px; justify-content: center; margin-top: 14px; flex-wrap: wrap; }
.foot { text-align: center; font-size: 11px; color: #b5aa95; margin: 14px 0 0; }

/* ---------- ボタン ---------- */
.btn {
  border: 2px solid var(--ink); background: #fff; color: var(--ink);
  border-radius: 10px; padding: 11px 16px; font-size: 16px; font-weight: 700;
  box-shadow: 0 3px 0 var(--ink); transition: transform .06s, box-shadow .06s;
  display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.25;
}
.btn small { font-size: 11px; font-weight: 500; opacity: .75; }
.btn:active { transform: translateY(3px); box-shadow: 0 0 0 var(--ink); }
.btn.primary { background: var(--shu); color: #fff; border-color: var(--shu-d); box-shadow: 0 3px 0 var(--shu-d); }
.btn.navy { background: var(--navy); color: #fff; border-color: var(--navy-d); box-shadow: 0 3px 0 var(--navy-d); }
.btn.ghost { background: transparent; border-color: var(--line); color: var(--muted); box-shadow: none; }
.btn.big { font-size: 21px; padding: 14px; }
.btn.small-btn { font-size: 13px; padding: 6px 12px; }
.btn:disabled { opacity: .45; pointer-events: none; }
.link { background: none; border: 0; color: var(--navy); text-decoration: underline; font-size: 14px; padding: 4px; }

/* ---------- カード ---------- */
.card {
  background: var(--paper); border-radius: var(--r); padding: 14px 16px; margin-bottom: 12px;
  box-shadow: 0 6px 18px rgba(0,0,0,.25);
}
.card h3 { margin: 0 0 6px; font-family: "Shippori Mincho B1", serif; font-size: 18px; }
.card p { margin: 0; line-height: 1.75; font-size: 14px; }
.howto b { color: var(--shu); }

/* ---------- ランク・レベル ---------- */
.rank-card { background: var(--paper); border-radius: var(--r); padding: 14px; margin-bottom: 14px; display: flex; gap: 14px; align-items: center; box-shadow: 0 6px 18px rgba(0,0,0,.25); }
.rank-badge {
  width: 64px; height: 64px; flex: none; border-radius: 50%; border: 3px double var(--shu); color: var(--shu);
  display: grid; place-items: center; font-family: "Shippori Mincho B1", serif; font-weight: 800; font-size: 15px; text-align: center; line-height: 1.1;
  transform: rotate(-6deg);
}
.rank-info { flex: 1; font-size: 13px; color: var(--muted); }
.rank-info b { display: block; font-size: 20px; color: var(--ink); font-family: "Shippori Mincho B1", serif; }
.prog { height: 8px; background: var(--paper-2); border-radius: 9px; overflow: hidden; margin-top: 6px; }
.prog i { display: block; height: 100%; background: var(--shu); border-radius: 9px; }
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(84px, 1fr)); gap: 10px; }
.lv {
  position: relative; aspect-ratio: 1 / 1.25; border: 0; border-radius: 4px;
  background: var(--paper); box-shadow: 2px 2px 0 #e5dcc6, 4px 4px 0 #d3c6a8, 0 8px 14px rgba(0,0,0,.3);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; padding: 4px;
  font-family: "Shippori Mincho B1", serif;
}
.lv .n { font-size: 26px; font-weight: 800; }
.lv .t { font-size: 10px; color: var(--muted); font-family: "Zen Maru Gothic", sans-serif; }
.lv .s { font-size: 13px; color: var(--gold); letter-spacing: 1px; min-height: 16px; }
.lv .s .off { color: #d8cfbd; }
.lv.locked { background: #d9cfba; color: #9c907b; box-shadow: 0 6px 10px rgba(0,0,0,.25); }
.lv.locked .n::after { content: "🔒"; font-size: 13px; margin-left: 2px; }
.lv.next { outline: 3px solid var(--shu); outline-offset: 2px; animation: wob 1.6s infinite; }
@keyframes wob { 50% { transform: translateY(-3px); } }
.lv .newmark { position: absolute; top: -6px; right: -6px; background: var(--shu); color: #fff; font-size: 10px; padding: 2px 6px; border-radius: 9px; font-family: "Zen Maru Gothic", sans-serif; }
.rank-sep { grid-column: 1 / -1; color: #fff; font-family: "Shippori Mincho B1", serif; font-weight: 800; margin-top: 8px; display: flex; align-items: center; gap: 8px; text-shadow: 0 1px 3px rgba(0,0,0,.4); }
.rank-sep::after { content: ""; flex: 1; height: 1px; background: rgba(255,255,255,.35); }

/* ---------- 検定 ---------- */
.exam-intro { color: #fff; font-size: 14px; line-height: 1.7; text-shadow: 0 1px 2px rgba(0,0,0,.4); margin-bottom: 10px; }
.exam-intro b { color: #ffd166; }
.gate { background: var(--paper); border-radius: var(--r); padding: 16px; text-align: center; margin-bottom: 14px; border: 2px dashed var(--shu); }
.gate p { margin: 0 0 10px; line-height: 1.7; font-size: 14px; }
.exam-item { background: var(--paper); border-radius: var(--r); padding: 12px 14px; margin-bottom: 10px; display: flex; align-items: center; gap: 12px; box-shadow: 0 6px 16px rgba(0,0,0,.25); }
.exam-item .medal { flex: none; }
.exam-item .info { flex: 1; min-width: 0; }
.exam-item .info b { font-family: "Shippori Mincho B1", serif; font-size: 18px; }
.exam-item .info p { margin: 2px 0 0; font-size: 12px; color: var(--muted); line-height: 1.5; }
.exam-item .acts { display: flex; flex-direction: column; gap: 6px; }
.exam-item.locked { opacity: .6; }
.tag-pass { display: inline-block; background: var(--shu); color: #fff; font-size: 11px; border-radius: 4px; padding: 1px 6px; margin-left: 6px; vertical-align: 2px; }

/* ---------- マイページ ---------- */
.profile { display: flex; align-items: center; gap: 14px; }
.profile .name { font-size: 22px; font-weight: 900; }
.stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin-top: 12px; }
.stats div { background: var(--paper-2); border-radius: 10px; padding: 8px; text-align: center; font-size: 11px; color: var(--muted); }
.stats b { display: block; font-size: 18px; color: var(--ink); }
.badges { display: grid; grid-template-columns: repeat(auto-fill, minmax(86px, 1fr)); gap: 10px; margin-top: 8px; }
.badge { text-align: center; font-size: 11px; line-height: 1.35; }
.badge svg { width: 64px; height: 64px; display: block; margin: 0 auto 4px; }
.badge.off { opacity: .4; filter: grayscale(1); }
.cert-list { display: grid; gap: 8px; margin-top: 8px; }
.cert-row { display: flex; align-items: center; gap: 10px; background: var(--paper-2); border-radius: 10px; padding: 8px 10px; }
.cert-row .g { font-family: "Shippori Mincho B1", serif; font-weight: 800; font-size: 17px; flex: 1; }
.cert-row small { color: var(--muted); display: block; font-family: "Zen Maru Gothic"; font-weight: 500; font-size: 11px; }
.empty { color: var(--muted); font-size: 13px; text-align: center; padding: 10px; }

/* ---------- ゲーム ---------- */
.game { background: #5c3a20; }
.hud {
  display: flex; align-items: center; gap: 10px;
  padding: calc(6px + env(safe-area-inset-top, 0px)) 10px 6px;
  background: linear-gradient(#2a1a0d, #3b2513); color: #fff;
  z-index: 2;
}
.hud-btn { width: 40px; height: 40px; border-radius: 10px; border: 0; background: rgba(255,255,255,.12); color: #fff; font-size: 14px; flex: none; }
.hud-main { flex: 1; min-width: 0; }
.hud-top { display: flex; justify-content: space-between; font-size: 12px; opacity: .9; margin-bottom: 4px; white-space: nowrap; overflow: hidden; gap: 6px; }
.timebar { height: 10px; background: rgba(255,255,255,.15); border-radius: 9px; overflow: hidden; }
.timebar i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #3ec28f, #a3e36b); border-radius: 9px; transition: width .1s linear; transform-origin: left; }
.timebar.warn i { background: linear-gradient(90deg, #ff6b35, #ffb347); }
.timebar.danger i { background: linear-gradient(90deg, #e63946, #ff5d73); animation: blink .5s infinite; }
@keyframes blink { 50% { opacity: .55; } }
.hud-num, .hud-score { text-align: center; line-height: 1; flex: none; }
.hud-num small, .hud-score small { display: block; font-size: 10px; opacity: .7; }
.hud-num b { font-size: 24px; }
.hud-score b { font-size: 18px; font-variant-numeric: tabular-nums; min-width: 56px; display: inline-block; }
#stage { position: relative; flex: 1; min-height: 0; overflow: hidden; }
#cv { position: absolute; inset: 0; touch-action: none; display: block; }
#combo {
  position: absolute; top: 10px; left: 12px; color: #fff; font-weight: 900; font-size: 15px; pointer-events: none;
  text-shadow: 0 2px 0 var(--shu-d), 0 0 8px rgba(0,0,0,.5); opacity: 0; transition: opacity .2s;
}
#combo.on { opacity: 1; }
#combo b { font-size: 30px; display: block; line-height: 1; color: #ffd166; }
.tip {
  position: absolute; left: 50%; bottom: 12px; transform: translateX(-50%); width: min(92%, 440px);
  background: rgba(255,255,255,.94); border-radius: 12px; padding: 9px 14px; font-size: 13px; line-height: 1.55;
  box-shadow: 0 6px 18px rgba(0,0,0,.35); pointer-events: none; opacity: 0; transition: opacity .4s;
  border-left: 5px solid var(--shu);
}
.tip.on { opacity: 1; }
.tray {
  display: flex; gap: 6px; justify-content: center; align-items: stretch;
  padding: 8px 8px calc(8px + var(--safe-b));
  background: linear-gradient(#3b2513, #2a1a0d);
  z-index: 2;
}
.stamp-btn {
  flex: 1 1 0; min-width: 40px; max-width: 68px; border: 0; border-radius: 12px; background: rgba(255,255,255,.08); color: #e9dcc6;
  padding: 5px 2px 4px; font-size: 11px; display: flex; flex-direction: column; align-items: center; gap: 2px;
  transition: transform .1s, background .1s; position: relative;
}
.stamp-btn img { width: min(40px, 100%); aspect-ratio: 1; height: auto; }
.stamp-btn span:last-child { white-space: nowrap; font-size: clamp(9px, 2.6vw, 11px); }
.stamp-btn .key { position: absolute; top: 2px; left: 5px; font-size: 9px; opacity: .5; }
.stamp-btn.sel { background: var(--paper); color: var(--ink); transform: translateY(-4px); box-shadow: 0 0 0 3px var(--shu), 0 6px 12px rgba(0,0,0,.4); font-weight: 900; }
@media (hover: none) { .stamp-btn .key { display: none; } }

/* ---------- モーダル ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(15,10,5,.6); display: none; align-items: center; justify-content: center;
  z-index: 50; padding: 16px; backdrop-filter: blur(2px);
}
.modal.open { display: flex; animation: fade .2s; }
@keyframes fade { from { opacity: 0; } }
.sheet {
  background: var(--paper); border-radius: 8px; width: min(440px, 100%); max-height: 100%; overflow-y: auto;
  padding: 20px 18px 16px; box-shadow: 0 20px 50px rgba(0,0,0,.5); animation: up .25s cubic-bezier(.2,1.4,.5,1);
}
.sheet.small { width: min(360px, 100%); }
@keyframes up { from { transform: translateY(30px) scale(.96); } }
.sheet h3 { margin: 0 0 10px; font-family: "Shippori Mincho B1", serif; font-size: 22px; text-align: center; }
.sheet-head { text-align: center; }
.pill { display: inline-block; background: var(--navy); color: #fff; font-size: 12px; padding: 2px 10px; border-radius: 99px; }
.row { display: flex; gap: 10px; margin-top: 14px; }
.row > * { flex: 1; }
.row.wrap { flex-wrap: wrap; }
.col { display: grid; gap: 10px; }
.brief-list { list-style: none; padding: 0; margin: 6px 0 10px; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.brief-list li { background: var(--paper-2); border-radius: 10px; padding: 8px; text-align: center; font-size: 11px; color: var(--muted); }
.brief-list li b { display: block; font-size: 18px; color: var(--ink); }
.brief-stamps { display: flex; flex-wrap: wrap; gap: 6px; justify-content: center; }
.brief-stamps span { display: flex; flex-direction: column; align-items: center; font-size: 10px; color: var(--muted); position: relative; }
.brief-stamps img { width: 40px; height: 40px; }
.brief-stamps .new::after { content: "NEW"; position: absolute; top: -4px; right: -8px; background: var(--shu); color: #fff; font-size: 8px; padding: 1px 4px; border-radius: 6px; }
.brief-tip { background: #fff8d9; border-radius: 10px; padding: 10px 12px; font-size: 13px; line-height: 1.6; margin: 12px 0 0; border-left: 4px solid #e9b949; }

.result { text-align: center; position: relative; }
.res-stamp {
  width: 110px; height: 110px; margin: -2px auto 6px; border-radius: 50%; border: 5px double var(--shu); color: var(--shu);
  display: grid; place-items: center; font-family: "Shippori Mincho B1", serif; font-weight: 800; font-size: 26px; line-height: 1.05;
  transform: rotate(-10deg); animation: slam .45s cubic-bezier(.3,1.6,.5,1);
}
.res-stamp.fail { border-color: #777; color: #777; }
@keyframes slam { from { transform: rotate(-10deg) scale(2.4); opacity: 0; } }
.stars { font-size: 34px; letter-spacing: 6px; color: var(--gold); min-height: 10px; }
.stars .off { color: #dfd6c4; }
.promo { display: none; background: linear-gradient(90deg, #ffd166, #ffb703); border-radius: 10px; padding: 8px; font-weight: 900; margin: 8px 0; animation: up .4s; }
.promo.on { display: block; }
.res-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 10px 0 0; }
.res-grid div { background: var(--paper-2); border-radius: 10px; padding: 7px 4px; }
.res-grid dt { font-size: 10px; color: var(--muted); }
.res-grid dd { margin: 0; font-size: 18px; font-weight: 900; }
.res-extra { font-size: 13px; color: var(--muted); margin-top: 10px; line-height: 1.6; }
.res-extra .pass { color: var(--shu); font-size: 22px; font-weight: 900; }

.tabs { display: flex; margin-bottom: 14px; border-bottom: 2px solid var(--line); }
.tab { flex: 1; background: none; border: 0; padding: 10px; font-size: 16px; font-weight: 700; color: var(--muted); border-bottom: 3px solid transparent; margin-bottom: -2px; }
.tab.active { color: var(--shu); border-color: var(--shu); }
form label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 10px; }
form input { display: block; width: 100%; margin-top: 4px; font: inherit; font-size: 16px; padding: 10px 12px; border: 2px solid var(--line); border-radius: 10px; background: #fff; color: var(--ink); }
form input:focus { outline: none; border-color: var(--navy); }
form .btn { width: 100%; }
.form-err { color: var(--shu); font-size: 13px; min-height: 18px; margin: 0 0 6px; }
.form-note { font-size: 11px; color: var(--muted); line-height: 1.6; margin: 10px 0; }
#mdAuth .btn.ghost { width: 100%; }

.sheet.cert { width: min(760px, 100%); }
#certCanvas { width: 100%; height: auto; display: block; border-radius: 4px; box-shadow: 0 4px 12px rgba(0,0,0,.2); }

.ranking { padding-left: 0; list-style: none; margin: 0 0 10px; counter-reset: r; }
.ranking li { display: flex; justify-content: space-between; padding: 8px 10px; border-bottom: 1px dashed var(--line); counter-increment: r; font-size: 14px; }
.ranking li::before { content: counter(r); width: 26px; font-weight: 900; color: var(--shu); }
.ranking li span:first-of-type { flex: 1; }
.ranking li.me { background: #fff3d1; }

/* ---------- トースト ---------- */
#toasts { position: fixed; top: calc(10px + env(safe-area-inset-top, 0px)); left: 50%; transform: translateX(-50%); z-index: 80; display: grid; gap: 8px; width: min(360px, 92vw); pointer-events: none; }
.toast {
  background: var(--paper); border-radius: 12px; padding: 10px 14px; box-shadow: 0 8px 24px rgba(0,0,0,.4);
  display: flex; align-items: center; gap: 10px; font-size: 13px; animation: tin .35s cubic-bezier(.2,1.4,.5,1);
}
.toast svg { width: 40px; height: 40px; flex: none; }
.toast b { display: block; font-size: 14px; }
.toast.out { animation: tout .3s forwards; }
@keyframes tin { from { transform: translateY(-30px); opacity: 0; } }
@keyframes tout { to { transform: translateY(-20px); opacity: 0; } }

.sound-btn {
  position: fixed; right: 10px; bottom: calc(10px + var(--safe-b)); z-index: 40; width: 42px; height: 42px; border-radius: 50%;
  border: 0; background: rgba(0,0,0,.4); color: #fff; font-size: 18px;
}
body.in-game .sound-btn { top: calc(8px + env(safe-area-inset-top, 0px)); bottom: auto; right: auto; left: 56px; width: 40px; height: 40px; border-radius: 10px; background: rgba(255,255,255,.12); font-size: 15px; }
body.in-game .hud-main { margin-left: 46px; }
