:root {
  color-scheme: light;
  --ink: #1f3b4d;
  --muted: #6b8a9c;
  --line: #cfe7f1;
  --panel: rgba(255, 255, 255, .78);
  --accent: #3ba9ff;
  --accent-soft: #e2f3ff;
  --mint: #2fc4a0;
  --danger: #ff6b81;
  --sky: #dff3fb;
  --elementary-a: #ffd36e;
  --elementary-b: #ff9f68;
  --middle-a: #8fd3ff;
  --middle-b: #a58bff;
  --shadow: 0 8px 24px rgba(59, 169, 255, .14);
  font-family: Pretendard, "Noto Sans KR", system-ui, sans-serif;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  min-width: 320px;
  color: var(--ink);
  background: #f2f8fb;
  overflow: hidden;
}
button, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* 상단 바 */
.topbar {
  position: fixed;
  inset: 0 0 auto;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  height: 52px;
  padding: 0 20px;
  border-bottom: 1px solid var(--line);
  background: #fff;
}
.brand { display: flex; align-items: center; gap: 8px; font-size: 18px; }
.brand__mark { display: grid; place-items: center; width: 28px; height: 28px; border-radius: 8px; background: var(--accent); color: #fff; font-weight: 800; }
.topbar__right { display: flex; align-items: center; gap: 8px; }
.system-status { display: inline-flex; align-items: center; gap: 6px; padding: 6px 12px; border-radius: 999px; background: #fff; color: var(--muted); font-size: 12px; }
.system-status i { width: 8px; height: 8px; border-radius: 50%; background: #c3d4dd; }
.system-status.is-ready { color: var(--mint); }
.system-status.is-ready i { background: var(--mint); }
.system-status.is-error { color: var(--danger); }
.system-status.is-error i { background: var(--danger); }
.character-tabs { display: flex; gap: 4px; padding: 4px; border-radius: 999px; background: #fff; }
.character-tab { padding: 4px 12px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font-size: 13px; }
.character-tab.is-active { background: var(--accent); color: #fff; font-weight: 700; }

/* 무대 + 대화창 */
/* 캐릭터 무대는 화면 전체, 대화창은 그 위 오른쪽에 투명하게 떠 있다 */
.layout { position: relative; height: 100vh; }
.stage { position: absolute; inset: 52px 0 0; outline: none; }
.character-canvas { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; transition: opacity .4s ease-out;  }
.character-canvas.is-ready { opacity: 1; }
.model-loading { position: absolute; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px; color: var(--muted); font: 700 11px/1 monospace; }
.model-loading.is-hidden { display: none; }
.model-loading span { width: 36px; height: 36px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin 1s linear infinite; }
.model-loading p { margin: 0; }
@keyframes spin { to { transform: rotate(360deg); } }
.speech-caption {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 2;
  max-width: min(520px, 80%);
  padding: 12px 16px;
  border: 2px solid #fff;
  border-radius: 18px;
  background: rgba(255, 255, 255, .92);
  box-shadow: var(--shadow);
  font-size: 15px;
  font-weight: 600;
  text-align: center;
  transform: translateX(-50%);
}
.character-meta { position: absolute; left: 4px; bottom: 4px; display: grid; gap: 2px; color: var(--muted); font-size: 11px; }
.character-meta strong { color: var(--ink); font-size: 16px; }
.character-meta p { margin: 0; }
.character-meta a { color: inherit; }

/* 카드 선택 팝업 */
.card-overlay {
  position: absolute;
  inset: 0;
  z-index: 4;
  display: grid;
  place-items: center;
  padding: 16px calc(min(420px, 34vw) + 36px) 16px 16px;
  pointer-events: none;
}
.card-hand { pointer-events: none; }
.card-hand__cards > * { pointer-events: auto; }
.card-controls { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin: 8px 12px 0; }
.deck-picker { display: flex; gap: 6px; }
.deck-button { padding: 6px 14px; border: 2px solid var(--line); border-radius: 999px; background: #fff; font-size: 14px; font-weight: 700; }
.deck-button:hover { border-color: var(--accent); }
.deck-button.is-active { border-color: var(--accent); background: var(--accent); color: #fff; }
.card-hand { width: 100%; }
.card-hand__cards { display: flex; justify-content: center; gap: 20px; perspective: 1200px; }
@media (max-width: 1400px) { .playing-card { width: 200px; height: 300px; } .playing-card__emoji { width: 90px; height: 90px; font-size: 48px; } .playing-card__title { font-size: 20px; } }

.playing-card {
  display: block;
  width: 240px;
  height: 340px;
  padding: 0;
  border: 0;
  background: none;
  transition: transform .2s ease-out;
  animation: deal .5s ease-out both;
}
.playing-card:hover { transform: translateY(-12px); }
.playing-card__inner { position: relative; display: block; width: 100%; height: 100%; transform-style: preserve-3d; animation: flip .6s ease-out both; }
.playing-card__face {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  padding: 20px 16px;
  border: 6px solid var(--card-color);
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 12px 32px rgba(31, 59, 77, .25);
  backface-visibility: hidden;
}
.playing-card--elementary { --card-color: #ffb547; --card-soft: #fff3dd; }
.playing-card--middle { --card-color: #3ba9ff; --card-soft: #e2f3ff; }
.playing-card__back { justify-content: center; background: var(--card-color); color: #fff; font-size: 64px; font-weight: 800; transform: rotateY(180deg); }
.playing-card__badge { align-self: flex-start; padding: 4px 10px; border-radius: 999px; background: var(--card-soft); font-size: 12px; font-weight: 700; }
.playing-card__emoji { display: grid; place-items: center; width: 110px; height: 110px; border-radius: 50%; background: var(--card-soft); font-size: 60px; }
.playing-card__title { color: var(--ink); font-size: 24px; font-weight: 800; }
.playing-card__situation { color: var(--ink); font-size: 15px; line-height: 1.5; text-align: center; }
.playing-card.is-picked { transform: scale(1.06); }
.playing-card.is-discarded { opacity: .25; }
@keyframes deal { from { opacity: 0; transform: translateY(40px); } }
@keyframes flip { from { transform: rotateY(180deg); } 30% { transform: rotateY(180deg); } to { transform: rotateY(0deg); } }

.pill-button { padding: 8px 16px; border: 2px solid var(--line); border-radius: 999px; background: #fff; font-size: 14px; font-weight: 600; }
.pill-button:hover { border-color: var(--accent); }
.pill-button--small { padding: 4px 10px; font-size: 12px; }

/* 대화창 위 현재 카드 */
.active-card { display: grid; grid-template-columns: auto 1fr auto auto; align-items: center; gap: 8px; margin: 8px 12px 0; padding: 8px 12px; border: 2px solid #3ba9ff; border-radius: 14px; background: #e2f3ff; }
.active-card.level-elementary { border-color: #ffb547; background: #fff3dd; }
.active-card__emoji { font-size: 28px; }
.active-card__text { display: grid; min-width: 0; }
.active-card__text strong { font-size: 15px; }
.active-card__text small { color: var(--muted); font-size: 11px; }
.turn-counter { padding: 2px 8px; border-radius: 999px; background: #fff; color: var(--mint); font-size: 12px; font-weight: 800; }

/* 대화창 */
.chat-panel { position: absolute; top: 64px; right: 20px; bottom: 16px; z-index: 5; display: flex; flex-direction: column; width: min(420px, 34vw); min-height: 0; border: 1px solid rgba(255, 255, 255, .7); border-radius: 20px; background: rgba(255, 255, 255, .25); overflow: hidden; }
.chat-header, .active-card { background: transparent; }
.chat-header { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 12px 16px; border-bottom: 1px solid var(--line); }
.chat-title { display: flex; align-items: center; gap: 8px; }
.chat-title h2 { margin: 0; font-size: 15px; }
.chat-header__actions { display: flex; align-items: center; gap: 8px; }
.mode-tabs { display: flex; gap: 2px; padding: 2px; border-radius: 999px; background: var(--accent-soft); }
.mode-tab { padding: 4px 10px; border: 0; border-radius: 999px; background: transparent; color: var(--muted); font-size: 12px; }
.mode-tab.is-active { background: #fff; color: var(--accent); font-weight: 700; }
.icon-button { display: grid; place-items: center; width: 32px; height: 32px; border: 0; border-radius: 50%; background: transparent; font-size: 16px; }
.icon-button[aria-pressed="false"] { opacity: .4; }
.messages { flex: 1; min-height: 0; padding: 16px; overflow-y: auto; scroll-behavior: smooth; }
.message { display: flex; gap: 8px; margin-bottom: 12px; }
.message--user { justify-content: flex-end; }
.message__avatar { display: grid; flex: none; place-items: center; width: 32px; height: 32px; border-radius: 50%; background: var(--accent-soft); color: var(--accent); font-size: 13px; font-weight: 800; }
.message--user .message__avatar { background: #ffe9d6; color: #ff8a3d; }
.message__body { max-width: 80%; }
.message__name { margin: 0 0 4px; color: var(--muted); font-size: 11px; }
.message__bubble { margin: 0; padding: 10px 12px; border-radius: 4px 16px 16px 16px; background: #fff; box-shadow: 0 2px 8px rgba(31, 59, 77, .06); font-size: 14px; line-height: 1.55; white-space: pre-wrap; }
.message--user .message__bubble { border-radius: 16px 4px 16px 16px; background: var(--accent); color: #fff; }
.message--error .message__bubble { background: #ffe8ec; color: #b33a52; }
.message__actions { display: flex; gap: 4px; margin-top: 4px; }
.text-button { padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; background: #fff; font-size: 12px; }
.text-button:hover { border-color: var(--accent); color: var(--accent); }
.text-button--small { padding: 2px 8px; font-size: 11px; }
.summary-box { margin-top: 8px; padding: 12px; border-radius: 12px; background: #e8fbf4; }
.summary-box ul { display: grid; gap: 4px; margin: 0 0 8px; padding: 0; list-style: none; font-size: 12px; }
.summary-box li { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.activity { display: flex; align-items: center; gap: 8px; padding: 0 16px 8px; color: var(--muted); font-size: 12px; }
.activity__dots { display: flex; gap: 4px; }
.activity__dots i { width: 6px; height: 6px; border-radius: 50%; background: var(--accent); animation: bounce .7s ease-out infinite alternate; }
.activity__dots i:nth-child(2) { animation-delay: .15s; }
.activity__dots i:nth-child(3) { animation-delay: .3s; }
@keyframes bounce { to { transform: translateY(-4px); opacity: .5; } }
.hint-list { display: grid; gap: 4px; max-height: 200px; margin: 0; padding: 8px 12px; overflow-y: auto; border-top: 1px solid var(--line); list-style: none; background: #fffbea; }
.hint-list li { display: flex; align-items: center; gap: 6px; padding: 6px 8px; border-radius: 10px; background: #fff; }
.hint-list li div { display: grid; flex: 1; min-width: 0; }
.hint-list strong { font-size: 13px; }
.hint-list small { color: var(--muted); font-size: 11px; }
.composer { margin: 8px; padding: 12px; border-radius: 16px; background: #d9f3fb; }
.composer textarea { width: 100%; min-height: 48px; max-height: 140px; border: 0; background: transparent; color: var(--ink); font-size: 15px; resize: none; outline: none; }
.composer textarea::placeholder { color: #4f9fc0; }
.composer__actions { display: grid; grid-template-columns: auto auto 1fr auto; align-items: center; gap: 6px; }
.composer__actions p { margin: 0; color: var(--muted); font-size: 11px; }
.round-button { display: grid; place-items: center; width: 36px; height: 36px; border: 0; border-radius: 50%; background: rgba(255, 255, 255, .7); font-size: 16px; transition: transform .15s ease-out; }
.round-button:hover { transform: scale(1.08); }
.round-button[aria-expanded="true"] { background: #fff1b8; }
#micButton.is-recording { background: var(--danger); animation: pulse 1s ease-out infinite; }
@keyframes pulse { 50% { box-shadow: 0 0 0 8px rgba(255, 107, 129, .2); } }
.send-button { display: grid; place-items: center; width: 40px; height: 40px; border: 0; border-radius: 50%; background: var(--accent); color: #fff; font-size: 18px; font-weight: 800; }
.round-button:disabled, .send-button:disabled { opacity: .45; cursor: not-allowed; transform: none; }

/* 설정 독 */
.settings-dock { position: fixed; left: 20px; top: 64px; z-index: 6; }
.settings-dock summary { display: grid; place-items: center; width: 40px; height: 40px; border-radius: 12px; background: #fff; box-shadow: var(--shadow); list-style: none; cursor: pointer; }
.settings-dock summary::-webkit-details-marker { display: none; }
.settings-panel { display: grid; gap: 12px; width: 300px; margin-top: 8px; padding: 12px; border-radius: 16px; background: #fff; box-shadow: var(--shadow); font-size: 12px; }
.motion-group strong { display: block; margin-bottom: 4px; color: var(--muted); }
.motion-group > div { display: flex; flex-wrap: wrap; gap: 4px; }
.motion-button { padding: 4px 10px; border: 1px solid var(--line); border-radius: 999px; background: #fff; font-size: 12px; }
.motion-button.is-active { border-color: var(--accent); background: var(--accent); color: #fff; }
.motion-button:disabled { opacity: .4; }
.rig-inspector summary { cursor: pointer; color: var(--muted); }
.rig-inspector__fields { display: grid; gap: 6px; padding-top: 8px; }
.rig-inspector label { display: grid; gap: 2px; }
.rig-inspector input { accent-color: var(--accent); }

.composer textarea.is-live { color: var(--accent); }
.composer textarea.is-live::placeholder { color: var(--accent); }
.hint-list .hint-heading { padding: 4px 2px 0; background: transparent; color: var(--muted); font-size: 11px; font-weight: 700; }

/* ── 모바일: 캐릭터는 화면 전체, 대화는 하단 플로팅 바 위에 떠 있게 ── */
@media (max-width: 900px) {
  body { height: 100dvh; overflow: hidden; }
  .topbar { height: 48px; padding: 0 12px; }
  .brand strong { font-size: 15px; }
  .brand__mark { width: 24px; height: 24px; }
  .system-status { padding: 4px; font-size: 0; }
  .system-status i { width: 10px; height: 10px; }
  .character-tab { padding: 4px 10px; font-size: 12px; }
  .layout { display: block; height: 100dvh; padding: 48px 0 0; }
  .stage { position: absolute; inset: 48px 0 0; }
  .speech-caption, .character-meta { display: none; }
  .settings-panel { width: min(300px, calc(100vw - 16px)); max-height: 60dvh; overflow-y: auto; }

  /* 대화창은 배경 없이 화면 아래쪽에 떠 있는 층 */
  .chat-panel {
    position: fixed;
    top: auto;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 6;
    width: auto;
    max-height: 62dvh;
    padding: 0 8px calc(8px + env(safe-area-inset-bottom));
    border: 0;
    border-radius: 0;
    background: none;
    box-shadow: none;
    overflow: visible;
    pointer-events: none;
  }
  .chat-panel > * { pointer-events: auto; }
  /* 모바일: 카드 정보·학년 선택·🔊는 상단 바 안에 한 줄로 넣고 캐릭터 위에는 아무것도 띄우지 않는다 */
  /* AIRI 모바일 구조: 상단 바 = 왼쪽 🔊 · 가운데 카드 이름 ⌄ · 오른쪽 ⚙️ */
  .brand, .system-status, .mode-tabs { display: none; }
  /* 가운데: 캐릭터 선택 (AIRI의 모델 선택 자리) */
  .topbar { justify-content: center; }
  .character-tabs { background: #f0f7fb; }
  .character-tab { padding: 4px 14px; font-size: 13px; white-space: nowrap; }
  .chat-header { position: fixed; top: 8px; left: 8px; z-index: 7; padding: 0; border: 0; }
  .icon-button { width: 32px; height: 32px; box-shadow: none; background: transparent; }
  .settings-dock { top: 6px; right: 8px; left: auto; z-index: 8; }
  .settings-dock summary { width: 36px; height: 36px; background: transparent; box-shadow: none; }
  .settings-panel { position: absolute; right: 0; }
  .chat-title { display: none; }
  .mode-tabs, .icon-button { background: rgba(255, 255, 255, .9); box-shadow: var(--shadow); }
  .active-card { margin: 0 0 6px; }
  .active-card { display: flex; align-items: center; gap: 6px; width: fit-content; max-width: 100%; padding: 4px 4px 4px 12px; border: 0; border-radius: 999px; background: rgba(255, 255, 255, .92); box-shadow: var(--shadow); }
  .active-card .pill-button { padding: 4px 10px; font-size: 12px; white-space: nowrap; box-shadow: none; }
  .active-card__emoji { font-size: 18px; }
  .active-card__text { flex: 1; }
  .active-card__text strong { overflow: hidden; font-size: 14px; white-space: nowrap; text-overflow: ellipsis; }
  .turn-counter { padding: 0 6px; font-size: 11px; }

  .active-card__text small { display: none; }

  /* 말풍선: 캐릭터 위에 반투명하게, 위로 갈수록 흐려지며 스크롤 */
  .messages {
    flex: 0 1 auto;
    max-height: 26dvh;
    padding: 24px 4px 4px;
    background: none;
  }
  /* AIRI처럼 이름 + 내용이 한 장의 반투명 카드로 깔린다 */
  .message { display: block; margin-bottom: 6px; }
  .message__avatar { display: none; }
  .message__body { max-width: 100%; padding: 8px 12px; border-radius: 12px; background: rgba(226, 243, 255, .9); }
  .message__name { margin: 0 0 2px; color: var(--muted); font-size: 11px; }
  .message__bubble { padding: 0; border-radius: 0; background: none; box-shadow: none; color: #1d6fb8; font-size: 15px; }
  .message--user .message__body { width: fit-content; max-width: 85%; margin-left: auto; background: rgba(255, 255, 255, .92); }
  .message--user .message__bubble { background: none; color: var(--ink); }
  .message--error .message__body { background: rgba(255, 232, 236, .92); }
  .message__actions .text-button { background: rgba(255, 255, 255, .6); }
  .activity { padding: 0 4px 6px; }
  .hint-list { max-height: 30dvh; margin-bottom: 6px; border: 0; border-radius: 16px; background: rgba(255, 251, 234, .8); }
  .hint-list li { background: rgba(255, 255, 255, .7); }

  /* 하단 플로팅 입력 바: 🎙️ · 입력칸 · 💡 · 보내기 한 줄 */
  .composer {
    display: flex;
    align-items: flex-end;
    gap: 6px;
    margin: 0;
    padding: 6px;
    border-radius: 28px;
    background: #fff;
    box-shadow: 0 6px 24px rgba(31, 59, 77, .18);
  }
  .composer__actions { display: contents; }
  .composer__actions p { display: none; }
  #micButton { order: -1; }
  .composer textarea { order: 0; flex: 1; min-height: 40px; max-height: 96px; padding: 9px 4px; font-size: 16px; }
  #hintButton { order: 1; }
  .send-button { order: 2; width: 40px; height: 40px; }
  .round-button { width: 40px; height: 40px; flex: none; background: #f0f7fb; }

  /* 카드: 가로로 긴 카드 3장을 아래쪽에 쌓아 고른다 (얼굴은 가리지 않음) */
  body.is-picking .messages, body.is-picking .activity { display: none; }
  .card-overlay { align-items: end; padding: 0 0 calc(130px + env(safe-area-inset-bottom)); }
  .card-hand__cards { flex-direction: column; gap: 10px; padding: 0 12px; perspective: none; }
  .playing-card { width: 100%; height: 88px; animation-name: deal-up; }
  .playing-card:hover { transform: none; }
  .playing-card__inner { animation: none; }
  .playing-card__back { display: none; }
  .playing-card__face {
    display: grid;
    grid-template-columns: 64px 1fr;
    grid-template-rows: auto auto;
    align-items: center;
    column-gap: 12px;
    row-gap: 2px;
    padding: 10px 14px;
    border-width: 4px;
    text-align: left;
  }
  .playing-card__emoji { grid-row: 1 / span 2; width: 64px; height: 64px; margin: 0; font-size: 34px; }
  .playing-card__badge { display: none; }
  .playing-card__title { align-self: end; margin: 0; font-size: 18px; }
  .playing-card__situation { align-self: start; margin: 0; font-size: 13px; text-align: left; }
  .playing-card.is-picked { transform: scale(1.03); }
}
@keyframes deal-up { from { opacity: 0; transform: translateY(24px); } }
@media (max-width: 420px) {
  .brand strong { display: none; }
}
@media (max-width: 900px) { .card-controls .deck-button.is-active { background: var(--accent); color: #fff; border-color: var(--accent); } }

/* 카드 선택 2단계 */
.card-step, .card-hand { display: grid; justify-items: center; gap: 16px; width: 100%; pointer-events: none; }
.card-step > *, .card-hand > * { pointer-events: auto; }
.card-hand__actions { display: flex; gap: 8px; }
.card-hand__actions .pill-button { white-space: nowrap; box-shadow: var(--shadow); }
.level-card .playing-card__title { font-size: 28px; }
@media (max-width: 900px) {
  .card-overlay { padding-bottom: calc(84px + env(safe-area-inset-bottom)); }
  .level-card { height: 96px; }
  .level-card .playing-card__title { font-size: 22px; }
}
.card-step .card-hand__cards, .card-hand .card-hand__cards { width: 100%; }
@media (max-width: 900px) {
  .card-overlay { display: block; position: absolute; inset: auto 0 0; }
  .card-step, .card-hand { justify-items: stretch; }
  .card-hand__actions { justify-content: center; }
}
@media (max-width: 900px) {
  .card-step, .card-hand { width: auto; }
  .card-step .card-hand__cards, .card-hand .card-hand__cards { width: auto; padding: 0 12px; }
}
/* 모바일 카드 목록: 그리드 대신 단순 블록으로 폭을 화면에 맞춘다 */
@media (max-width: 900px) {
  .card-step, .card-hand { display: block; width: 100%; }
  .card-step .card-hand__cards, .card-hand .card-hand__cards { display: flex; flex-direction: column; width: 100%; padding: 0 12px; }
  .card-hand__actions { display: flex; justify-content: center; margin-top: 12px; }
}
/* 모바일 카드 목록(학년 선택·주제 선택 공통): 그리드 대신 단순 블록으로 화면 폭에 맞춘다 */
@media (max-width: 900px) {
  .card-step, .card-hand { display: block; width: 100%; }
  .card-step .card-hand__cards, .card-hand .card-hand__cards { display: flex; flex-direction: column; align-items: stretch; width: 100%; padding: 0 12px; }
  .card-step .playing-card, .card-hand .playing-card { width: 100%; }
  .card-hand__actions { display: flex; justify-content: center; margin-top: 12px; }
}
