:root {
  --ink: #2b2d4a; --ink2: #4a4d73; --muted: #6f7399; --bg: #f1f0fb; --card: #ffffff;
  --lav: #c9b8f0; --lavL: #ece5ff; --blue: #a9c8f5; --blueL: #e0ecff; --pink: #f7b6c8; --pinkL: #ffe5ee;
  --mint: #a8e6cf; --mintL: #dcf7ee; --amber: #ffc23d; --orange: #ff8a50; --red: #ff5f6d; --green: #3fc98a;
  --fs: 15px;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; overscroll-behavior: none; }
body {
  position: fixed; inset: 0; touch-action: manipulation; -webkit-user-select: none; user-select: none; color: var(--ink);
  font: 600 var(--fs)/1.42 ui-rounded, "SF Pro Rounded", "Arial Rounded MT Bold", "Nunito", "Segoe UI", system-ui, -apple-system, Roboto, sans-serif;
  background: radial-gradient(circle at 15% 10%, #e4ddff 0, transparent 40%), radial-gradient(circle at 90% 85%, #d6f4ea 0, transparent 40%), radial-gradient(circle at 85% 15%, #ffe2ec 0, transparent 35%), var(--bg);
  -webkit-text-size-adjust: 100%;
}
.hidden { display: none !important; }
b { color: #d4572a; font-weight: 800; }
i { color: #7a5cc8; font-style: normal; font-weight: 800; }

#app { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; padding-top: 8px; }
body.landscape #app { justify-content: center; padding-top: 0; }
#stage { position: relative; flex: none; border-radius: 18px; overflow: hidden; border: 3px solid var(--ink); box-shadow: 0 6px 0 rgba(43, 45, 74, .18); background: var(--bg); }
#screen { display: block; width: 100%; height: 100%; touch-action: none; }

/* ---------- panel ---------- */
#panel { width: 100%; padding: 8px 10px; overflow-y: auto; -webkit-overflow-scrolling: touch; }
body.portrait #panel { flex: none; padding-bottom: 6px; }
body.landscape #panel { position: absolute; z-index: 5; padding: 0 10px 10px; overflow: visible; pointer-events: none; }
body.landscape #panel > * { pointer-events: auto; }
body.landscape #panel #hint { display: none; }

#dlg {
  display: flex; gap: 10px; align-items: flex-start; position: relative; cursor: pointer;
  background: var(--card); border: 3px solid var(--ink); border-radius: 18px; box-shadow: 0 5px 0 rgba(43, 45, 74, .16);
  padding: 10px 12px;
}
#face { width: 3.8em; height: 3.8em; flex: none; border-radius: 14px; border: 2.5px solid var(--ink); background: var(--lavL); }
#face.none { display: none; }
#dlg .body { flex: 1; min-width: 0; }
#who {
  display: inline-block; font-size: .74em; font-weight: 800; letter-spacing: .5px; text-transform: uppercase;
  background: var(--lav); border: 2px solid var(--ink); border-radius: 99px; padding: 1px 9px; margin-bottom: 4px;
}
#who:empty { display: none; }
#txt { min-height: 2.8em; white-space: pre-line; font-weight: 600; }
#more { position: absolute; right: 12px; bottom: 6px; color: var(--orange); font-size: .8em; animation: bob .8s ease-in-out infinite; }
#more.off { visibility: hidden; }
@keyframes bob { 50% { transform: translateY(3px); } }

#choices { margin-top: 6px; }
#choices.grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 8px; }
#choices.bar { display: grid; grid-auto-flow: column; grid-auto-columns: 1fr; gap: 0 8px; margin-top: 0; }
.opt {
  display: block; width: 100%; text-align: left; font: inherit; font-weight: 700; color: var(--ink); cursor: pointer;
  background: var(--card); border: 2.5px solid var(--ink); border-radius: 14px; box-shadow: 0 4px 0 rgba(43, 45, 74, .18);
  padding: 9px 12px; margin: 7px 0; line-height: 1.3; transition: transform .05s;
}
#choices.bar .opt { text-align: center; padding: 11px 4px; }
.opt .k {
  display: inline-grid; place-items: center; width: 1.45em; height: 1.45em; margin-right: 8px; font-size: .78em; font-weight: 800;
  background: var(--lavL); border: 2px solid var(--ink); border-radius: 50%; vertical-align: 1px;
}
.opt small { display: block; color: var(--muted); font-size: .78em; margin-top: 2px; font-weight: 600; }
.opt.sel { background: var(--lavL); box-shadow: 0 4px 0 var(--ink); }
.opt.sel .k { background: var(--amber); }
.opt:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--ink); }
.opt.right { background: var(--mint); }
.opt.wrong { background: var(--pink); }
.opt.on { background: var(--mint); box-shadow: 0 4px 0 var(--ink); }
.opt.on .k { background: var(--green); color: #fff; }
.opt.glow { animation: glow 1s ease-in-out infinite; }
.opt[disabled] { opacity: .5; cursor: default; }
@keyframes glow { 50% { background: #fff2cc; box-shadow: 0 4px 0 var(--orange), 0 0 0 4px rgba(255, 194, 61, .45); } }

#hint { color: var(--muted); font-size: .8em; text-align: center; padding: 6px 4px; font-weight: 600; }

/* ---------- toast ---------- */
#toast {
  position: absolute; left: 50%; top: 13%; transform: translateX(-50%); z-index: 6;
  background: #fff; border: 2.5px solid var(--ink); border-radius: 14px; box-shadow: 0 4px 0 rgba(43, 45, 74, .18);
  padding: 6px 12px; font-size: .8em; max-width: 92%; width: max-content; text-align: center; font-weight: 700;
  opacity: 0; transition: opacity .2s; pointer-events: none;
}
#toast.show { opacity: 1; }
#toast.good { background: var(--mintL); }
#toast.bad { background: var(--pinkL); }

#mute {
  position: absolute; top: 7px; right: 7px; z-index: 7; font: inherit; font-size: .8em; width: 2.2em; height: 2.2em; font-weight: 800;
  background: #fff; color: var(--ink); border: 2.5px solid var(--ink); border-radius: 50%; cursor: pointer; padding: 0;
}
#mute.off { color: var(--red); text-decoration: line-through; }

/* ---------- touch pad ---------- */
#pad { display: none; width: 100%; flex: none; position: relative; touch-action: none; }
body.touch #pad { display: block; }
#dpad {
  position: absolute; width: 128px; height: 128px; border-radius: 50%; touch-action: none;
  background: radial-gradient(circle, #fff 0 28%, var(--lavL) 29% 100%); border: 3px solid var(--ink); box-shadow: 0 5px 0 rgba(43, 45, 74, .2);
}
#dpad span { position: absolute; color: var(--ink2); font-size: 17px; line-height: 1; pointer-events: none; }
#dpad .u { top: 11px; left: 50%; transform: translateX(-50%); }
#dpad .d { bottom: 11px; left: 50%; transform: translateX(-50%); }
#dpad .l { left: 13px; top: 50%; transform: translateY(-50%); }
#dpad .r { right: 13px; top: 50%; transform: translateY(-50%); }
#dpad.u .u, #dpad.d .d, #dpad.l .l, #dpad.r .r { color: var(--orange); }
#ab { position: absolute; display: flex; gap: 14px; align-items: center; }
#ab button {
  width: 62px; height: 62px; border-radius: 50%; font: inherit; font-size: 21px; font-weight: 800; color: var(--ink);
  border: 3px solid var(--ink); box-shadow: 0 5px 0 rgba(43, 45, 74, .25); touch-action: none;
}
#btnA { background: var(--amber); margin-top: -26px; }
#btnB { background: var(--blue); margin-top: 26px; }
#ab button.down { transform: translateY(4px); box-shadow: 0 1px 0 rgba(43, 45, 74, .25); }
body.portrait #dpad { left: 18px; top: 50%; transform: translateY(-50%); }
body.portrait #ab { right: 18px; top: 50%; transform: translateY(-50%); }
body.landscape #pad { position: absolute; inset: 0; pointer-events: none; }
body.landscape #dpad { left: 14px; bottom: 18px; pointer-events: auto; }
body.landscape #ab { right: 14px; bottom: 30px; pointer-events: auto; }

/* ---------- cards ---------- */
#cardwrap { position: fixed; inset: 0; z-index: 20; background: rgba(43, 45, 74, .35); display: flex; align-items: center; justify-content: center; padding: 14px; }
#card {
  width: 100%; max-width: 600px; max-height: 92%; overflow-y: auto; -webkit-overflow-scrolling: touch;
  background: var(--card); border: 3px solid var(--ink); border-radius: 22px; box-shadow: 0 8px 0 rgba(43, 45, 74, .22); padding: 16px 18px; font-size: .95em;
}
#card .tag {
  display: inline-block; font-size: .7em; font-weight: 800; letter-spacing: 1px; background: var(--lav); border: 2px solid var(--ink);
  border-radius: 99px; padding: 2px 10px;
}
#card h3 { margin: 10px 0 8px; font-size: 1.2em; line-height: 1.25; font-weight: 900; }
#card ul { margin: 6px 0 10px; padding-left: 1.2em; }
#card li { margin: 7px 0; font-weight: 500; }
#card p { font-weight: 500; margin: 8px 0; }
#card .src { color: var(--muted); font-size: .74em; font-weight: 500; border-top: 2px dashed #d8d6f0; padding-top: 8px; margin-top: 10px; }
#card .row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
#card .row .opt { flex: 1; text-align: center; margin: 0; min-width: 140px; }
#card .stars { color: #f0a020; letter-spacing: 3px; font-weight: 800; }
#card .lesson { background: #f7f6ff; border: 2px solid #d8d6f0; border-radius: 14px; padding: 8px 12px; margin: 8px 0; }
#card .lesson .lt { font-weight: 800; }
#card .lesson .ls { color: var(--muted); font-size: .76em; font-weight: 500; }
#card .muted { color: var(--muted); font-weight: 500; }
#card canvas.px { border-radius: 16px; }
#card table td { padding: 4px 2px; border-bottom: 1px dashed #e2e0f4; }

#menuBtn {
  position: absolute; top: 7px; right: calc(7px + 2.6em); z-index: 7; font: inherit; font-size: .8em; width: 2.2em; height: 2.2em; font-weight: 800;
  background: #fff; color: var(--ink); border: 2.5px solid var(--ink); border-radius: 50%; cursor: pointer; padding: 0;
}
#askJ {
  position: absolute; left: 8px; bottom: 8px; z-index: 6; width: 3.6em; height: 3.6em; padding: 0; cursor: pointer;
  border: 2.5px solid var(--ink); border-radius: 16px; background: var(--lav); box-shadow: 0 4px 0 rgba(43, 45, 74, .2);
}
#askJ canvas { width: 100%; height: 100%; border-radius: 13px; display: block; }
#askJ span {
  position: absolute; right: -8px; top: -8px; width: 1.5em; height: 1.5em; display: grid; place-items: center; font-weight: 900; font-size: .8em;
  background: var(--amber); border: 2.5px solid var(--ink); border-radius: 50%; animation: bob 1s ease-in-out infinite;
}
#askJ:active { transform: translateY(3px); }

#card input[type=text] {
  width: 100%; font: inherit; font-size: 1.15em; font-weight: 800; color: var(--ink); padding: 10px 14px; margin: 6px 0 4px;
  border: 3px solid var(--ink); border-radius: 14px; background: var(--lavL); outline: none; -webkit-user-select: text; user-select: text;
}
#card input[type=text]:focus { background: #fff; box-shadow: 0 0 0 4px rgba(255, 194, 61, .5); }
#screen { cursor: pointer; }
