:root {
  --paper: #ffffff;
  --surface: #ffffff;
  --surface-2: #f4f8fd;
  --ink: #1b2533;
  --ink-2: #475569;
  --ink-3: #7b8796;
  --line: #d9e2ee;
  --navy: #1d5bb5;
  --navy-2: #164a95;
  --navy-bg: #e8f1fc;
  --green: #1d5bb5;
  --green-bg: #e8f1fc;
  --red: #c0392b;
  --red-bg: #fdeeec;
  --marker: #ffd23f;
  --yellow-bg: #fff8dc;
  --stage-bg: #e8f1fc;
  --radius: 16px;
  --font: "BIZ UDPGothic", "Hiragino Kaku Gothic ProN", "Hiragino Sans", "Meiryo", "Yu Gothic", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #0f1622; --surface: #172131; --surface-2: #1c2839; --ink: #e8eef6; --ink-2: #b4c1d1; --ink-3: #8795a7;
    --line: #2a3a50; --navy: #7fb0f0; --navy-2: #b3d0f6; --navy-bg: #1a2c47; --green: #7fb0f0; --green-bg: #1a2c47;
    --red: #ef8f82; --red-bg: #2c1d1b; --marker: #c9a21c; --yellow-bg: #2a2410; --stage-bg: #1a2c47;
  }
}
:root[data-theme="dark"] {
  --paper: #0f1622; --surface: #172131; --surface-2: #1c2839; --ink: #e8eef6; --ink-2: #b4c1d1; --ink-3: #8795a7;
    --line: #2a3a50; --navy: #7fb0f0; --navy-2: #b3d0f6; --navy-bg: #1a2c47; --green: #7fb0f0; --green-bg: #1a2c47;
    --red: #ef8f82; --red-bg: #2c1d1b; --marker: #c9a21c; --yellow-bg: #2a2410; --stage-bg: #1a2c47;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--paper);
  color: var(--ink);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.7;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; touch-action: manipulation; }
.hidden { display: none !important; }

/* ---------- 共通部品 ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 56px; padding: 0 22px; border-radius: 14px;
  border: 2px solid var(--navy); background: var(--navy); color: #fff; font-weight: 700; font-size: 18px;
}
.btn:active { transform: translateY(1px); }
.btn:disabled { opacity: .45; }
.btn.ghost { background: var(--surface); color: var(--navy); }
.btn.small { min-height: 44px; padding: 0 14px; font-size: 16px; border-radius: 12px; }
.btn.green { background: var(--green); border-color: var(--green); }
.icon { width: 26px; height: 26px; flex: none; }
.field { display: grid; gap: 6px; margin-bottom: 16px; }
.field label { font-weight: 700; font-size: 16px; color: var(--ink-2); }
.field input, .field select {
  width: 100%; min-width: 0;
  min-height: 56px; padding: 0 16px; border-radius: 12px; border: 2px solid var(--line);
  background: var(--surface); font-size: 20px;
}
.field input:focus, .field select:focus, .composer textarea:focus { outline: 3px solid var(--navy-bg); border-color: var(--navy); }
.error { color: var(--red); background: var(--red-bg); border-radius: 12px; padding: 10px 14px; font-size: 16px; margin: 0 0 12px; }

/* ---------- ログイン ---------- */
.login-wrap { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-card {
  width: min(520px, 100%); background: var(--surface); border: 1px solid var(--line); border-radius: 24px; padding: 28px;
  display: grid; gap: 8px;
}
.login-head { display: flex; align-items: center; gap: 16px; margin-bottom: 12px; }
.login-head img { width: 96px; height: 106px; object-fit: cover; border-radius: 18px; border: 1px solid var(--line); }
.login-head h1 { font-size: 24px; margin: 0; line-height: 1.4; }
.login-head p { margin: 4px 0 0; color: var(--ink-2); font-size: 16px; }

/* ---------- メイン画面 ---------- */
.app { height: 100%; display: grid; grid-template-rows: auto 1fr; }
.topbar {
  border-top: 4px solid var(--navy);
  display: flex; align-items: center; gap: 10px; padding: 10px 16px; border-bottom: 1px solid var(--line); background: var(--surface);
}
.topbar .title { font-weight: 700; font-size: 18px; flex: 1; min-width: 0; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar .who { color: var(--ink-2); font-size: 16px; white-space: nowrap; }

.main { display: grid; grid-template-columns: minmax(280px, 36%) 1fr; min-height: 0; }
.teacher {
  border-right: 1px solid var(--line); padding: 16px; display: flex; flex-direction: column; gap: 12px; min-height: 0; overflow: auto;
}
.stage { position: relative; width: 100%; aspect-ratio: 912 / 1008; border-radius: 20px; overflow: hidden; background: var(--stage-bg); border: 1px solid var(--line); }
.stage video, .stage img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; transition: opacity .18s; }
.stage .talk { opacity: 0; }
.stage.talking .talk { opacity: 1; }
.status {
  display: flex; align-items: center; gap: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px; padding: 10px 14px; font-weight: 700;
}
.status .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--ink-3); flex: none; }
.status.listen .dot { background: var(--red); animation: pulse 1s infinite; }
.status.think .dot { background: var(--navy); animation: pulse 1s infinite; }
.status.speak .dot { background: var(--green); }
@keyframes pulse { 50% { opacity: .3; } }
@media (prefers-reduced-motion: reduce) { .status .dot { animation: none !important; } }

.subjects { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.subjects button {
  min-height: 52px; border-radius: 12px; border: 2px solid var(--line); background: var(--surface); font-weight: 700; font-size: 18px;
}
.subjects button[aria-pressed="true"] { border-color: var(--navy); background: var(--navy-bg); color: var(--navy-2); }
.hint { color: var(--ink-3); font-size: 15px; margin: 0; }

.chat { display: grid; grid-template-rows: 1fr auto; min-height: 0; }
.messages { background: var(--surface-2); overflow-y: auto; padding: 20px 20px 8px; display: flex; flex-direction: column; gap: 16px; overscroll-behavior: contain; }
.welcome { background: var(--surface); border: 1px dashed var(--line); border-radius: var(--radius); padding: 18px 20px; color: var(--ink-2); }
.welcome h2 { margin: 0 0 6px; font-size: 20px; color: var(--ink); }
.welcome ol { margin: 6px 0 0; padding-left: 1.3em; }

.bubble-me {
  align-self: flex-end; max-width: 80%; background: var(--navy); color: #fff; border-radius: 18px 18px 6px 18px; padding: 12px 16px; white-space: pre-wrap; word-break: break-word;
}
.bubble-me img { display: block; max-width: 240px; border-radius: 10px; margin-top: 8px; }
.answer { align-self: stretch; display: grid; gap: 12px; }
.answer .unit { justify-self: start; background: var(--navy-bg); color: var(--navy-2); border-radius: 999px; padding: 2px 14px; font-size: 15px; font-weight: 700; }
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.card .head { display: flex; align-items: center; gap: 10px; margin-bottom: 6px; }
.card .num {
  width: 34px; height: 34px; border-radius: 50%; background: var(--navy); color: #fff; display: grid; place-items: center; font-weight: 700; font-size: 17px; flex: none;
}
.card .ttl { font-weight: 700; font-size: 19px; }
.card .body { white-space: normal; word-break: break-word; font-size: 20px; line-height: 1.9; }
.card .actions { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 12px; }
.card.point { background: var(--yellow-bg); border: 2px solid var(--marker); }
.card.point .ttl { background: var(--marker); color: #1b2533; border-radius: 6px; padding: 0 10px; }
.card.try { border-style: dashed; }
.refs { color: var(--ink-3); font-size: 14px; }

/* 分数・囲み数字 */
.frac { display: inline-flex; flex-direction: column; align-items: center; vertical-align: middle; line-height: 1.15; margin: 0 2px; font-size: .9em; }
.frac > span:first-child { border-bottom: 2px solid currentColor; padding: 0 3px; }
.frac > span:last-child { padding: 0 3px; }
.enc { display: inline-grid; place-items: center; min-width: 1.9em; height: 1.9em; padding: 0 .3em; margin: 0 2px; vertical-align: middle; border: 2px solid currentColor; font-weight: 700; line-height: 1; }
.enc.maru { border-radius: 999px; }
.enc.shikaku { border-radius: 3px; }
.enc.sankaku { border: 0; position: relative; min-width: 2.6em; height: 2.3em; padding-top: .6em; }
.enc.sankaku svg { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.enc .frac { font-size: .75em; }

.thinking { align-self: flex-start; background: var(--surface); border: 1px solid var(--line); border-radius: 18px; padding: 12px 18px; color: var(--ink-2); }
.thinking.ack { color: var(--ink, inherit); font-size: 19px; line-height: 1.6; max-width: 92%; }
.thinking.ack .wait { display: block; margin-top: 4px; font-size: 15px; color: var(--ink-2); }

/* 入力欄 */
.composer { border-top: 1px solid var(--line); background: var(--surface); padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); display: grid; gap: 10px; }
.preview { display: flex; align-items: center; gap: 10px; }
.preview img { height: 64px; border-radius: 8px; border: 1px solid var(--line); }
.composer-row { display: flex; align-items: flex-end; gap: 10px; }
.composer textarea {
  flex: 1; min-height: 56px; max-height: 160px; resize: none; border-radius: 14px; border: 2px solid var(--line); background: var(--surface-2); padding: 12px 14px; font-size: 19px; line-height: 1.5;
}
.round {
  width: 64px; height: 64px; border-radius: 50%; border: 2px solid var(--navy); background: var(--surface); color: var(--navy); display: grid; place-items: center; flex: none;
}
.round.mic { background: var(--navy); color: #fff; width: 72px; height: 72px; }
.round.mic.rec { background: var(--red); border-color: var(--red); }
.round .icon { width: 30px; height: 30px; }
.composer .labels { display: flex; gap: 10px; font-size: 13px; color: var(--ink-3); }

/* 履歴 */
.drawer-back { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); z-index: 20; }
.drawer {
  position: fixed; top: 0; right: 0; bottom: 0; width: min(420px, 92vw); background: var(--surface); z-index: 21; padding: 16px; overflow: auto; border-left: 1px solid var(--line);
}
.drawer h2 { margin: 4px 0 12px; font-size: 20px; }
.drawer .item { display: block; width: 100%; text-align: left; border: 1px solid var(--line); background: var(--surface-2); border-radius: 12px; padding: 12px 14px; margin-bottom: 8px; }
.drawer .item small { display: block; color: var(--ink-3); font-size: 14px; }

/* タブレット縦・スマホ */
@media (max-width: 900px) {
  .main { grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .teacher { border-right: 0; border-bottom: 1px solid var(--line); flex-direction: row; flex-wrap: wrap; align-items: center; padding: 10px 16px; overflow: visible; }
  .stage { width: 170px; flex: none; border-radius: 14px; }
  .teacher .side { flex: 1; min-width: 200px; display: grid; gap: 8px; }
  .teacher .hint { display: none; }
  .subjects button { min-height: 46px; font-size: 16px; }
  .messages { padding: 14px 12px 8px; }
  .bubble-me { max-width: 90%; }
  .card .body { font-size: 19px; }
}
@media (max-width: 480px) {
  .topbar .who { display: none; }
  .round { width: 56px; height: 56px; }
  .round.mic { width: 64px; height: 64px; }
}

/* ---------- 管理画面 ---------- */
.admin { max-width: 1100px; margin: 0 auto; padding: 16px; }
.admin h1 { font-size: 22px; margin: 8px 0 16px; }
.admin h2 { font-size: 19px; margin: 0 0 12px; }
.panel { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin-bottom: 16px; }
.grid-form { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; align-items: end; }
.grid-form .field { margin: 0; }
.table-wrap { overflow-x: auto; }
table { border-collapse: collapse; width: 100%; font-size: 16px; }
th, td { text-align: left; padding: 10px 8px; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { color: var(--ink-2); font-size: 14px; }
.tag { display: inline-block; border-radius: 999px; padding: 0 10px; font-size: 13px; background: var(--navy-bg); color: var(--navy-2); }
.tag.off { background: var(--red-bg); color: var(--red); }
.log { display: grid; gap: 10px; }
.log .q { background: var(--navy-bg); border-radius: 12px; padding: 10px 12px; white-space: pre-wrap; }
.log .a { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 16px; }
.muted { color: var(--ink-3); font-size: 14px; }

/* 声で話した文のたしかめ */
.composer textarea.heard { border-color: var(--marker); background: var(--yellow-bg); }
.heard-note { margin: 0; background: var(--yellow-bg); border: 2px solid var(--marker); border-radius: 12px; padding: 8px 14px; font-size: 16px; font-weight: 700; }

.subject-fixed { display:inline-block; justify-self:start; align-self:start; width:auto; padding:8px 18px; border:2px solid var(--navy); background:var(--navy-bg); color:var(--navy-2); border-radius:10px; font-weight:700; font-size:16px; }
.stop-voice { width:100%; margin:0 0 8px; background:var(--surface); color:var(--ink); border:2px solid var(--ink); font-size:18px; min-height:52px; font-weight:700; }
.fig { margin: 12px 0 4px; padding: 10px 8px 6px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface); color: var(--ink); --fig-shade: #e3e7ec; }
.fig svg { display: block; width: 100%; height: auto; max-height: 340px; }
.fig svg text { fill: currentColor; font-family: inherit; }
.fig figcaption { margin-top: 4px; font-size: 15px; color: var(--ink-2); text-align: center; }
.card.reading { outline: 3px solid var(--ink-3); outline-offset: 2px; }
.voice-retry { width:100%; margin:0 0 8px; background:#ffd23f; color:#1a1a1a; border:none; font-size:18px; min-height:52px; font-weight:700; }

/* ---------- 登録フォーム ---------- */
.login-wrap { align-items: start; }
.lead { margin: 0 0 14px; font-size: 18px; }
.muted { color: var(--ink-2); }
.note { margin: 2px 0 0; color: var(--ink-3); font-size: 14px; line-height: 1.6; }
.group { border: 0; padding: 0; margin: 0 0 6px; }
.group legend { font-weight: 700; font-size: 16px; color: var(--ink-2); padding: 0; margin-bottom: 6px; }
.two { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.two .field { margin-bottom: 10px; }
.field .label { font-weight: 700; font-size: 16px; color: var(--ink-2); }
.choice { display: flex; flex-wrap: wrap; gap: 10px; }
.choice label { position: relative; }
.choice input { position: absolute; opacity: 0; inset: 0; }
.choice span {
  display: inline-flex; align-items: center; justify-content: center; min-width: 110px; min-height: 52px; padding: 0 18px;
  border: 2px solid var(--line); border-radius: 12px; background: var(--surface); font-weight: 700; font-size: 18px;
}
.choice input:checked + span { border-color: var(--navy); background: var(--navy-bg); color: var(--navy-2); }
.choice input:focus-visible + span { outline: 3px solid var(--navy-bg); }
.agree { display: flex; gap: 10px; align-items: flex-start; margin: 4px 0 16px; font-size: 16px; }
.agree input { width: 24px; height: 24px; flex: none; margin-top: 3px; }
#registerForm .btn, #loginForm .btn { width: 100%; }
.link { display: block; margin: 14px auto 0; background: none; border: 0; color: var(--navy); font-weight: 700; text-decoration: underline; font-size: 16px; padding: 8px; }

/* ---------- はじめる ---------- */
.start { position: fixed; inset: 0; background: var(--paper); display: grid; place-items: center; padding: 24px 16px; z-index: 30; }
.start-card { width: min(460px, 100%); text-align: center; display: grid; gap: 14px; justify-items: center; }
.start-card img { width: 220px; height: 243px; object-fit: cover; border-radius: 24px; border: 1px solid var(--line); }
.start-name { font-size: 22px; font-weight: 700; margin: 0; }
.btn.big { min-height: 72px; font-size: 24px; padding: 0 40px; border-radius: 18px; width: 100%; }

/* ---------- のこり回数・プラン ---------- */
.quota { border-color: var(--marker); background: var(--yellow-bg); color: var(--ink); }
.modal {
  position: fixed; z-index: 25; left: 50%; top: 50%; transform: translate(-50%, -50%);
  width: min(720px, calc(100% - 32px)); max-height: calc(100% - 32px); overflow: auto;
  background: var(--surface); border-radius: 20px; border: 1px solid var(--line); padding: 20px 22px;
}
.modal-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; }
.modal-head h2 { margin: 0; font-size: 20px; }
.plans { display: grid; grid-template-columns: repeat(3, 1fr); gap: 12px; margin: 14px 0; }
.plan { border: 2px solid var(--line); border-radius: 16px; padding: 16px; display: grid; gap: 6px; align-content: start; }
.plan.current { border-color: var(--navy); }
.plan h3 { margin: 0; font-size: 18px; }
.plan .price { font-size: 26px; font-weight: 700; }
.plan .price small { font-size: 14px; color: var(--ink-2); font-weight: 400; }
.plan .per { color: var(--ink-2); font-size: 14px; }
.plan .btn { margin-top: 6px; }
.small-text { font-size: 14px; }
#portalBtn { width: 100%; margin-top: 8px; }
@media (max-width: 640px) {
  .plans { grid-template-columns: 1fr; }
  .two { grid-template-columns: 1fr; gap: 0; }
}
.stage .talk { transition: opacity .12s; }
.plans > p { grid-column: 1 / -1; margin: 0; }
.plan .price small { white-space: nowrap; }
.plan .price { line-height: 1.3; }
@media (max-width: 1180px) { .topbar .title { display: none; } }

#voiceToggle.off { border-color: var(--marker); background: var(--yellow-bg); color: var(--ink); }
.voice-off-note { margin: 0 0 8px; padding: 10px 14px; border-radius: 12px; background: var(--yellow-bg); font-size: 16px; }

/* ---------- 問題をえらんで質問 ---------- */
.pick-btn { width: 100%; min-height: 52px; font-size: 18px; font-weight: 700; background: var(--surface); color: var(--ink); border: 2px solid var(--line); }
.picked { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 2px solid var(--ink); border-radius: 12px; background: var(--surface); }
.picked-label { flex: 1; font-size: 17px; font-weight: 700; }
.pick-drawer { width: min(520px, 100vw); }
.pick-head { display: flex; align-items: center; gap: 8px; margin-bottom: 10px; }
.pick-head h2 { flex: 1; margin: 0; font-size: 20px; }
.pick-tabs { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.pick-tabs button { min-height: 44px; padding: 0 14px; border-radius: 22px; border: 2px solid var(--line); background: var(--surface); font-size: 17px; }
.pick-tabs button.on { border-color: var(--ink); background: var(--ink); color: var(--surface); }
.pick-drawer .item { font-size: 17px; line-height: 1.5; }
.pick-drawer .item.off { opacity: .5; }
.pick-sec { margin: 14px 0 6px; font-size: 16px; color: var(--ink-2); font-weight: 700; }
