:root {
  --bg: #f6f4ef;
  --surface: #ffffff;
  --surface-2: #efece5;
  --text: #1d2320;
  --muted: #5f6b66;
  --line: #dcd8cf;
  --accent: #2f6f5e;
  --accent-ink: #ffffff;
  --accent-soft: #d9ebe4;
  --danger: #b4443a;
  --ake-bubble: #e6f1ec;
  --you-bubble: #2f6f5e;
  --you-ink: #ffffff;
  --error: #b4443a;
  --level: 0;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121614;
    --surface: #1b201e;
    --surface-2: #232a27;
    --text: #e8ece9;
    --muted: #9aa7a1;
    --line: #2e3733;
    --accent: #5fb49b;
    --accent-ink: #0d1411;
    --accent-soft: #1f3a32;
    --danger: #e0766b;
    --ake-bubble: #21302b;
    --you-bubble: #3d8a75;
    --you-ink: #ffffff;
    --error: #f08b80;
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #121614;
  --surface: #1b201e;
  --surface-2: #232a27;
  --text: #e8ece9;
  --muted: #9aa7a1;
  --line: #2e3733;
  --accent: #5fb49b;
  --accent-ink: #0d1411;
  --accent-soft: #1f3a32;
  --danger: #e0766b;
  --ake-bubble: #21302b;
  --you-bubble: #3d8a75;
  --you-ink: #ffffff;
  --error: #f08b80;
  color-scheme: dark;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Hiragino Sans", "Noto Sans JP", "Yu Gothic UI", system-ui, sans-serif;
  line-height: 1.6;
}

button,
input {
  font: inherit;
}

[hidden] {
  display: none !important;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 20px 16px 32px;
  display: flex;
  flex-direction: column;
  gap: 20px;
  min-height: 100dvh;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.brand-mark {
  font-weight: 800;
  letter-spacing: 0.02em;
}

.brand-sub {
  color: var(--muted);
  font-size: 0.9rem;
}

.account {
  margin-left: auto;
  display: flex;
  align-items: baseline;
  gap: 10px;
  min-width: 0;
  font-size: 0.85rem;
}

.account-email {
  color: var(--muted);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.link-btn {
  flex-shrink: 0;
  padding: 0;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 700;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 3px;
}

/* Login */
.card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 22px 20px;
}

.form {
  display: flex;
  flex-direction: column;
  gap: 14px;
}

.form h2 {
  margin: 0;
  font-size: 1.1rem;
}

.form .btn {
  flex: none;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
  color: var(--muted);
}

.field input {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 12px;
  background: var(--bg);
  color: var(--text);
  font-size: 1rem;
}

.hint {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
}

.form-error {
  margin: 0;
  color: var(--error);
  font-size: 0.9rem;
}

.form-error:empty {
  display: none;
}

.lesson {
  flex: 1;
  display: flex;
  flex-direction: column;
  gap: 20px;
}

/* Teacher card */
.teacher {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 28px 20px 22px;
  text-align: center;
}

.teacher h1 {
  margin: 14px 0 0;
  font-size: 1.6rem;
  letter-spacing: 0.02em;
}

.teacher-tagline {
  margin: 2px 0 0;
  color: var(--muted);
  font-size: 0.95rem;
}

.status {
  margin: 14px 0 0;
  min-height: 1.6em;
  font-size: 0.95rem;
}

.status.error {
  color: var(--error);
}

.avatar {
  position: relative;
  width: 112px;
  height: 112px;
  margin: 0 auto;
}

.avatar-face {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-ink);
  font-size: 3rem;
  font-weight: 700;
  font-family: Georgia, "Times New Roman", serif;
}

.avatar-ring {
  position: absolute;
  inset: -10px;
  border-radius: 50%;
  background: var(--accent-soft);
  transform: scale(calc(0.9 + var(--level) * 0.35));
  opacity: calc(0.35 + var(--level) * 0.65);
  transition: transform 120ms ease-out, opacity 120ms ease-out;
}

.avatar.connecting .avatar-ring {
  animation: pulse 1.4s ease-in-out infinite;
}

@keyframes pulse {
  0%,
  100% {
    transform: scale(0.92);
    opacity: 0.4;
  }
  50% {
    transform: scale(1.08);
    opacity: 0.9;
  }
}

/* Controls */
.controls {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.buttons {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.btn {
  flex: 1 1 160px;
  min-height: 52px;
  padding: 12px 18px;
  border-radius: 14px;
  border: 1px solid transparent;
  font-weight: 700;
  cursor: pointer;
  transition: filter 120ms, background 120ms;
}

.btn:disabled {
  opacity: 0.55;
  cursor: default;
}

.btn:focus-visible,
.link-btn:focus-visible,
.field input:focus-visible,
.switch input:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.btn.primary {
  background: var(--accent);
  color: var(--accent-ink);
}

.btn.danger {
  background: var(--danger);
  color: #fff;
}

.btn.ghost {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
}

.btn.ghost.is-muted {
  background: var(--surface-2);
  color: var(--danger);
}

.btn:not(:disabled):hover {
  filter: brightness(1.06);
}

.btn.small {
  min-height: 44px;
  padding: 8px 14px;
  font-size: 0.95rem;
}

/* Level choice (shown before a lesson) */
.levels {
  min-width: 0;
  margin: 0;
  padding: 0;
  border: 0;
}

.levels legend {
  padding: 0;
  margin-bottom: 6px;
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 700;
}

.level-list {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.level {
  position: relative;
  display: block;
  cursor: pointer;
}

/* Visually hidden but still focusable, so the keyboard can change it. */
.level input {
  position: absolute;
  inset: 0;
  margin: 0;
  opacity: 0;
  cursor: pointer;
}

.level-body {
  display: flex;
  flex-direction: column;
  gap: 2px;
  height: 100%;
  padding: 12px 14px;
  border: 2px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
}

.level input:checked + .level-body {
  border-color: var(--accent);
  background: var(--accent-soft);
}

.level input:focus-visible + .level-body {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
}

.level-name {
  font-weight: 700;
}

.level-desc {
  color: var(--muted);
  font-size: 0.82rem;
  line-height: 1.45;
}

/* Passed lessons at this level, e.g. "合格 2 / 3 回" or "クリア". */
.level-progress {
  margin-top: auto;
  padding-top: 6px;
  font-size: 0.8rem;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

.level-progress.cleared {
  color: var(--accent);
}

.progress-note {
  margin-top: 8px;
}

/* Ake's verdict on the lesson that just ended (shown until the next one starts). */
.lesson-result {
  padding: 12px 14px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: var(--surface);
  font-size: 0.95rem;
}

.lesson-result p {
  margin: 0;
}

.lesson-result-title {
  font-weight: 700;
}

.lesson-result .lesson-result-comment {
  margin-top: 6px;
}

.lesson-result.passed {
  border-color: var(--accent);
}

.lesson-result.level-up {
  background: var(--accent-soft);
}

@media (max-width: 520px) {
  .level-list {
    grid-template-columns: 1fr;
  }
}

/* Help buttons (shown during a lesson) */
.helpers {
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
}

.meta {
  display: flex;
  align-items: center;
  gap: 12px;
  min-height: 28px;
}

.meta:not(:has(> :not([hidden]))) {
  display: none;
}

.chip {
  padding: 3px 12px;
  border-radius: 999px;
  background: var(--surface-2);
  color: var(--muted);
  font-size: 0.85rem;
  font-variant-numeric: tabular-nums;
}

.mic-meter {
  flex: 1;
  height: 6px;
  border-radius: 999px;
  background: var(--surface-2);
  overflow: hidden;
}

.mic-meter span {
  display: block;
  height: 100%;
  width: calc(var(--mic, 0) * 100%);
  background: var(--accent);
  transition: width 100ms linear;
}

/* Transcript */
.transcript-panel {
  flex: 1;
  display: flex;
  flex-direction: column;
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 16px;
  min-height: 260px;
}

.transcript-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin-bottom: 10px;
}

.transcript-head h2 {
  margin: 0;
  font-size: 1rem;
}

.switch {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font-size: 0.85rem;
  color: var(--muted);
  cursor: pointer;
}

.transcript {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  max-height: 46dvh;
}

.transcript .empty {
  color: var(--muted);
  font-size: 0.9rem;
  text-align: center;
  padding: 24px 0;
}

.bubble {
  max-width: 85%;
  padding: 10px 14px;
  border-radius: 16px;
  overflow-wrap: anywhere;
  transition: filter 150ms;
}

.bubble .who {
  display: block;
  font-size: 0.72rem;
  font-weight: 700;
  opacity: 0.75;
  margin-bottom: 2px;
}

.bubble.ake {
  align-self: flex-start;
  background: var(--ake-bubble);
  border-bottom-left-radius: 4px;
  font-size: 1.05rem;
}

.bubble.user {
  align-self: flex-end;
  background: var(--you-bubble);
  color: var(--you-ink);
  border-bottom-right-radius: 4px;
}

.captions-hidden .bubble:not(.revealed) .text {
  filter: blur(6px);
  cursor: pointer;
  user-select: none;
}

.tip {
  margin: 0;
  color: var(--muted);
  font-size: 0.85rem;
  text-align: center;
}

@media (prefers-reduced-motion: reduce) {
  .avatar-ring,
  .mic-meter span,
  .bubble {
    transition: none;
  }
  .avatar.connecting .avatar-ring {
    animation: none;
  }
}
