:root {
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: #18212f;
  background: #f4f6fa;
  --primary: #5b5ce2;
  --primary-dark: #4647bd;
  --line: #dfe4ec;
  --muted: #697487;
  --panel: #ffffff;
  --danger: #a62b2b;
  --success: #187446;
}
* { box-sizing: border-box; }
html, body, #root { min-height: 100%; margin: 0; }
body { margin: 0; background: #f4f6fa; }
button, input, textarea, select { font: inherit; }
button { cursor: pointer; }
button:disabled { cursor: not-allowed; opacity: .5; }
input, textarea, select {
  width: 100%; border: 1px solid #cfd7e3; border-radius: 11px; padding: 11px 12px;
  background: white; color: #18212f; outline: none;
}
input:focus, textarea:focus, select:focus { border-color: var(--primary); box-shadow: 0 0 0 3px rgba(91,92,226,.12); }
label { display: grid; gap: 7px; font-size: 13px; font-weight: 750; color: #354054; }
h1, h2, h3, p { margin-top: 0; }
.primary, .ghost-button, .icon-text-button, .text-button { border: 0; border-radius: 11px; font-weight: 800; }
.primary { background: var(--primary); color: white; padding: 12px 17px; }
.primary:hover { background: var(--primary-dark); }
.ghost-button { background: white; color: #39465a; border: 1px solid var(--line); padding: 10px 13px; }
.icon-text-button { background: transparent; color: #354054; padding: 8px 0; }
.text-button { background: transparent; color: var(--primary); padding: 6px; }
.page-center { min-height: 100vh; display: grid; place-items: center; color: var(--muted); }
.stack { display: grid; gap: 15px; }
.panel { background: white; border: 1px solid var(--line); border-radius: 18px; padding: 20px; }
.panel-title, .section-header, .topbar { display: flex; justify-content: space-between; align-items: flex-start; gap: 18px; }
.panel-title { margin-bottom: 18px; }
.panel-title h2 { margin-bottom: 4px; font-size: 18px; }
.panel-title p { color: var(--muted); margin: 0; font-size: 13px; line-height: 1.5; }
.eyebrow { margin: 0 0 7px; color: var(--primary); text-transform: uppercase; letter-spacing: .12em; font-size: 11px; font-weight: 900; }
.muted { color: var(--muted); line-height: 1.6; }
.pill, .status, .quota-badge, .live-status {
  display: inline-flex; width: max-content; align-items: center; gap: 5px; border-radius: 999px;
  font-size: 11px; font-weight: 900; padding: 6px 9px;
}
.pill { background: #eeefff; color: var(--primary); }
.status { text-transform: capitalize; background: #eef1f5; color: #566174; }
.status.published { background: #e6f7ee; color: #187446; }
.status.disabled { background: #fff0f0; color: #a62b2b; }
.quota-badge { background: #f0f2f8; color: #566174; white-space: nowrap; }
.quota-badge.low { background: #fff0e6; color: #a44f15; }
.live-status { background: #e7f8ef; color: #13723f; }
.error-box, .success-box, .note-box, .interface-help { border-radius: 12px; padding: 12px 14px; line-height: 1.5; }
.error-box { background: #fff0f0; color: var(--danger); border: 1px solid #ffd3d3; }
.success-box { background: #e9f8ef; color: var(--success); border: 1px solid #c9ead7; }
.note-box, .interface-help { background: #f7f8fc; color: #586273; border: 1px solid #e6e9f0; }

/* Login */
.login-shell { min-height: 100vh; display: grid; place-items: center; padding: 24px; background: radial-gradient(circle at 20% 20%, #e7e8ff, transparent 35%), #f5f7fb; }
.login-card { width: min(440px, 100%); background: white; padding: 34px; border-radius: 24px; box-shadow: 0 22px 70px rgba(31,42,59,.14); }
.brand-mark { width: 52px; height: 52px; display: grid; place-items: center; border-radius: 16px; background: var(--primary); color: white; font-weight: 900; margin-bottom: 20px; }

/* Home */
.home-shell { width: min(1240px, calc(100% - 40px)); margin: auto; padding: 34px 0 60px; }
.topbar { margin-bottom: 24px; }
.topbar h1 { margin-bottom: 0; }
.top-actions { display: flex; flex-wrap: wrap; align-items: center; justify-content: flex-end; gap: 9px; }
.hero-panel { min-height: 230px; display: grid; align-items: center; padding: 32px; border-radius: 25px; margin-bottom: 28px; color: white; background: linear-gradient(135deg, #4e50ce, #7777ec); box-shadow: 0 20px 50px rgba(66,68,190,.23); }
.hero-panel div { max-width: 720px; }
.hero-panel h2 { font-size: clamp(28px, 4vw, 46px); margin-bottom: 12px; }
.hero-panel p { margin: 0; line-height: 1.65; color: rgba(255,255,255,.86); }
.hero-icon { display: grid; place-items: center; width: 42px; height: 42px; border-radius: 13px; background: rgba(255,255,255,.16); margin-bottom: 18px; }
.character-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 20px; }
.character-card { padding: 0; width: 100%; text-align: left; background: white; border: 1px solid var(--line); border-radius: 21px; overflow: hidden; transition: .22s transform, .22s box-shadow; }
.character-card:hover { transform: translateY(-4px); box-shadow: 0 18px 50px rgba(32,44,65,.12); }
.character-preview { background: linear-gradient(#eef0ff, #f7f8fb); height: 300px; display: grid; align-items: end; overflow: hidden; }
.character-preview.framing-full_body .sprite-stage { max-height: 98%; }
.character-preview.framing-waist_up .sprite-stage { max-height: 118%; transform: translateY(13%); }
.character-copy { padding: 20px; }
.character-copy h3 { margin: 10px 0 7px; }
.character-copy p { color: var(--muted); line-height: 1.5; min-height: 46px; }
.card-meta { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.card-meta strong { color: var(--primary); font-size: 14px; }
.card-meta small { color: var(--muted); text-transform: capitalize; }
.empty-panel { padding: 40px; text-align: center; background: white; border: 1px dashed #cbd3df; border-radius: 18px; color: var(--muted); }

/* Sprite */
.sprite-stage { width: 100%; max-height: 70vh; background-repeat: no-repeat; image-rendering: auto; background-position: center; align-self: end; }
.sprite-stage img { width: 100%; height: 100%; object-fit: contain; display: block; }
.sprite-empty { display: grid; place-items: center; min-height: 260px; color: var(--muted); padding: 30px; text-align: center; }

/* User experience */
.tutor-shell { min-height: 100vh; background: white; position: relative; overflow: hidden; }
.tutor-shell.tutor-split { display: grid; grid-template-columns: minmax(340px, 42%) 1fr; }
.character-pane { background: radial-gradient(circle at 40% 18%, #eeeeff, transparent 38%), #e9ebf5; padding: 24px; position: relative; display: flex; flex-direction: column; min-height: 100vh; overflow: hidden; }
.back-button { width: max-content; position: relative; z-index: 5; }
.character-stage { flex: 1; display: grid; place-items: end center; position: relative; min-height: 350px; overflow: hidden; }
.character-stage .sprite-stage { max-width: 680px; }
.framing-full_body .character-stage .sprite-stage { max-height: 78vh; }
.framing-waist_up .character-stage .sprite-stage { max-height: 86vh; transform: translateY(16%); }
.state-chip { position: absolute; bottom: 16px; left: 50%; transform: translateX(-50%); background: rgba(28,36,51,.82); color: white; padding: 7px 12px; border-radius: 999px; font-size: 11px; font-weight: 900; text-transform: capitalize; z-index: 4; }
.character-meta { position: relative; z-index: 4; display: flex; justify-content: space-between; gap: 16px; align-items: flex-end; background: rgba(255,255,255,.8); backdrop-filter: blur(16px); border: 1px solid rgba(255,255,255,.85); border-radius: 19px; padding: 17px; }
.character-meta h1 { margin: 8px 0 5px; font-size: 24px; }
.character-meta p { margin: 0; color: var(--muted); line-height: 1.45; max-width: 480px; }
.character-controls { display: grid; justify-items: end; gap: 8px; }
.floating-talk { position: absolute; z-index: 9; left: 50%; bottom: 128px; transform: translateX(-50%); border: 0; border-radius: 999px; padding: 14px 20px; background: var(--primary); color: white; box-shadow: 0 16px 40px rgba(73,74,200,.34); font-weight: 900; display: flex; align-items: center; gap: 9px; }
.floating-talk.active { background: #c93b3b; animation: pulse 1s infinite; }
.live-caption { position: absolute; z-index: 8; left: 50%; bottom: 188px; transform: translateX(-50%); width: min(620px, calc(100% - 40px)); padding: 11px 14px; border-radius: 13px; text-align: center; background: rgba(28,36,51,.84); color: white; line-height: 1.45; backdrop-filter: blur(8px); }

.chat-pane { min-width: 0; display: grid; grid-template-rows: auto 1fr auto auto; height: 100vh; background: #fbfcfe; }
.chat-header { display: flex; justify-content: space-between; align-items: center; gap: 15px; padding: 17px 22px; border-bottom: 1px solid var(--line); background: white; }
.chat-header > div:first-child { display: grid; gap: 3px; }
.chat-header span { color: var(--muted); font-size: 12px; }
.chat-header-actions { display: flex; align-items: center; justify-content: flex-end; gap: 8px; flex-wrap: wrap; }
.messages { overflow: auto; padding: 24px; display: flex; flex-direction: column; gap: 15px; }
.message { max-width: min(760px, 88%); padding: 14px 16px; border-radius: 16px; }
.message span { display: block; font-size: 10px; font-weight: 900; margin-bottom: 6px; opacity: .65; text-transform: uppercase; letter-spacing: .06em; }
.message p { margin: 0; white-space: pre-wrap; line-height: 1.65; }
.message.user { align-self: flex-end; background: var(--primary); color: white; border-bottom-right-radius: 5px; }
.message.assistant { align-self: flex-start; background: white; border: 1px solid var(--line); box-shadow: 0 5px 16px rgba(32,44,65,.05); border-bottom-left-radius: 5px; }
.thinking-message { color: var(--muted); }
.welcome-message { margin: auto; text-align: center; max-width: 660px; padding: 30px; }
.welcome-message p { color: var(--muted); line-height: 1.6; }
.suggestions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 18px; }
.suggestions button { border: 1px solid var(--line); background: white; padding: 10px 12px; border-radius: 11px; color: #465164; }
.chat-error { margin: 0 20px 8px; }
.composer { display: grid; grid-template-columns: auto 1fr auto; align-items: end; gap: 10px; padding: 14px 20px calc(14px + env(safe-area-inset-bottom)); background: white; border-top: 1px solid var(--line); }
.composer textarea { min-height: 48px; max-height: 150px; resize: none; line-height: 1.45; }
.mic-button, .send-button { width: 48px; height: 48px; border-radius: 14px; border: 1px solid var(--line); display: grid; place-items: center; }
.mic-button { background: white; color: #586273; }
.mic-button.active { background: #ffecec; color: #bd3333; animation: pulse 1s infinite; }
.send-button { background: var(--primary); color: white; border-color: var(--primary); }
.chat-minimized, .chat-open-fab { position: fixed; z-index: 20; right: 22px; bottom: 22px; border: 1px solid rgba(255,255,255,.7); background: rgba(255,255,255,.92); box-shadow: 0 16px 50px rgba(28,39,55,.18); backdrop-filter: blur(14px); color: #263246; }
.chat-minimized { width: min(330px, calc(100% - 28px)); border-radius: 18px; padding: 14px 16px; display: flex; align-items: center; gap: 12px; text-align: left; }
.chat-minimized > span { font-size: 24px; }
.chat-minimized div { display: grid; gap: 3px; }
.chat-minimized small { color: var(--muted); }
.chat-open-fab { border-radius: 999px; padding: 13px 17px; font-weight: 900; }
.floating-error { position: fixed; z-index: 30; right: 22px; top: 22px; width: min(430px, calc(100% - 44px)); }

/* Character focus preset */
.tutor-shell.character-focus { display: block; }
.character-focus .character-pane { min-height: 100vh; width: 100%; padding: 22px; }
.character-focus .character-stage { position: absolute; inset: 0; min-height: 100vh; }
.character-focus .character-stage .sprite-stage { width: min(760px, 72vw); max-height: 94vh; }
.character-focus.framing-full_body .character-stage .sprite-stage { transform: none; }
.character-focus.framing-waist_up .character-stage .sprite-stage { transform: translateY(18%); width: min(860px, 78vw); }
.character-focus .character-meta { position: absolute; left: 22px; bottom: 22px; max-width: min(550px, calc(100% - 44px)); }
.character-focus .floating-talk { bottom: 42px; left: auto; right: 30px; transform: none; }
.character-focus .live-caption { bottom: 108px; }
.character-focus .chat-pane { position: fixed; z-index: 15; right: 18px; top: 18px; bottom: 18px; width: min(620px, calc(100% - 36px)); height: auto; border: 1px solid var(--line); border-radius: 20px; overflow: hidden; box-shadow: 0 22px 80px rgba(26,35,50,.22); }
.character-focus.chat-open .character-meta { max-width: min(430px, calc(100% - 680px)); }

@keyframes pulse { 50% { transform: scale(1.04); } }
.character-focus .floating-talk.active { animation: pulse-focus 1s infinite; }
@keyframes pulse-focus { 50% { transform: scale(1.04); } }

/* Admin */
.admin-shell { min-height: 100vh; display: grid; grid-template-columns: 240px 1fr; }
.admin-sidebar { background: #181a3e; color: white; padding: 22px 14px; position: sticky; top: 0; height: 100vh; }
.admin-brand { width: 100%; border: 0; background: transparent; color: white; text-align: left; font-weight: 900; padding: 10px; margin-bottom: 25px; }
.admin-sidebar nav { display: grid; gap: 5px; }
.admin-sidebar nav button { display: flex; align-items: center; padding: 11px 12px; border-radius: 10px; color: #c8cae9; font-weight: 700; font-size: 14px; border: 0; background: transparent; text-align: left; width: 100%; }
.admin-sidebar nav button.active, .admin-sidebar nav button:hover { background: rgba(255,255,255,.1); color: white; }
.admin-content { min-width: 0; padding: 34px; }
.section-header { margin-bottom: 22px; }
.section-header p { color: var(--muted); line-height: 1.5; margin-bottom: 0; }
.section-header.compact { align-items: center; margin-top: 16px; }
.admin-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 17px; }
.admin-card { width: 100%; background: white; border: 1px solid var(--line); border-radius: 17px; padding: 18px; display: grid; gap: 16px; text-align: left; transition: .2s; }
.admin-card:hover { transform: translateY(-2px); box-shadow: 0 12px 30px rgba(32,44,65,.08); }
.admin-card-icon { width: 44px; height: 44px; border-radius: 13px; background: #efefff; color: var(--primary); display: grid; place-items: center; }
.admin-card h3 { margin: 8px 0 5px; }
.admin-card p, .admin-card small { color: var(--muted); margin: 0; }
.editor-layout { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 20px; align-items: start; }
.editor-main { display: grid; gap: 20px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.form-grid .full { grid-column: 1 / -1; }
.check-label { display: flex; align-items: center; gap: 8px; align-self: end; padding-bottom: 12px; }
.check-label input { width: auto; margin: 0; }
.check-card { display: flex; align-items: flex-start; gap: 10px; border: 1px solid var(--line); padding: 12px; border-radius: 12px; background: #fbfcfe; }
.check-card input { width: auto; margin-top: 3px; }
.check-card span { display: grid; gap: 4px; }
.check-card small { color: var(--muted); line-height: 1.45; font-weight: 500; }
.editor-preview { position: sticky; top: 24px; }
.preview-stage { min-height: 300px; background: #eef0f8; border-radius: 14px; overflow: hidden; display: grid; align-items: end; }
.preview-stage.framing-waist_up .sprite-stage { transform: translateY(16%); max-height: 360px; }
.preview-stage.framing-full_body .sprite-stage { max-height: 360px; }
.editor-preview h3 { margin: 18px 0 5px; }
.editor-preview > p:not(.eyebrow) { color: var(--muted); }
.preview-settings { display: flex; flex-wrap: wrap; gap: 6px; margin: 12px 0; }
.preview-settings span { background: #f1f3f8; color: #596477; border-radius: 999px; padding: 5px 8px; font-size: 10px; font-weight: 850; text-transform: capitalize; }
.animation-list { display: grid; gap: 6px; margin-top: 18px; }
.animation-list button { border: 1px solid var(--line); background: white; border-radius: 9px; padding: 9px 10px; display: flex; justify-content: space-between; text-transform: capitalize; font-weight: 750; }
.animation-list button span { color: var(--muted); font-size: 11px; }
.animation-list button.active { border-color: var(--primary); background: #f4f3ff; color: var(--primary); }
.credential-status { display: flex; gap: 12px; align-items: center; padding: 13px; background: #f7f8fb; border-radius: 12px; }
.credential-status div { display: grid; gap: 3px; }
.credential-status span { color: var(--muted); font-size: 12px; }
.users-layout { display: grid; grid-template-columns: 350px 1fr; gap: 20px; align-items: start; }
.table-wrap { overflow: auto; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 12px; border-bottom: 1px solid var(--line); font-size: 13px; white-space: nowrap; }
th { color: var(--muted); font-size: 11px; text-transform: uppercase; }

@media (max-width: 1100px) {
  .character-grid, .admin-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .editor-layout { grid-template-columns: 1fr; }
  .editor-preview { position: static; }
  .tutor-shell.tutor-split { grid-template-columns: 360px 1fr; }
  .character-focus.chat-open .character-meta { display: none; }
}

@media (max-width: 780px) {
  .home-shell { width: min(100% - 24px, 1240px); padding-top: 18px; }
  .topbar, .section-header { flex-direction: column; }
  .top-actions { width: 100%; justify-content: flex-start; }
  .character-grid, .admin-grid { grid-template-columns: 1fr; }

  .tutor-shell.tutor-split { display: block; min-height: 100vh; overflow: auto; }
  .tutor-split .character-pane { min-height: auto; height: 43vh; padding: 12px; display: block; position: relative; }
  .tutor-split .character-stage { position: absolute; inset: 0; min-height: 0; }
  .tutor-split .character-stage .sprite-stage { height: 100%; width: auto; max-width: 100%; }
  .tutor-split.framing-waist_up .character-stage .sprite-stage { transform: translateY(18%); }
  .tutor-split .character-meta { position: absolute; left: 12px; right: 12px; bottom: 10px; padding: 10px 12px; z-index: 6; align-items: center; }
  .tutor-split .character-meta h1 { font-size: 17px; margin: 0; }
  .tutor-split .character-meta p, .tutor-split .character-meta .pill, .tutor-split .character-controls .quota-badge { display: none; }
  .tutor-split .character-controls { margin-left: auto; }
  .tutor-split .state-chip { bottom: 72px; }
  .tutor-split .floating-talk { bottom: 75px; padding: 10px 14px; }
  .tutor-split .live-caption { bottom: 122px; font-size: 13px; }
  .tutor-split .chat-pane { height: 57vh; }

  .character-focus .character-pane { padding: 12px; }
  .character-focus .character-stage .sprite-stage { width: 95vw; max-height: 90vh; }
  .character-focus .character-meta { left: 12px; right: 12px; bottom: 12px; max-width: none; padding: 11px; }
  .character-focus .character-meta p { display: none; }
  .character-focus .character-meta h1 { font-size: 18px; margin: 5px 0 0; }
  .character-focus .character-controls .quota-badge { display: none; }
  .character-focus .floating-talk { right: 14px; bottom: 100px; padding: 12px 15px; }
  .character-focus .live-caption { bottom: 155px; }
  .character-focus .chat-pane { inset: 8px; width: auto; height: auto; border-radius: 15px; }

  .chat-header { padding: 10px 12px; }
  .chat-header-actions .quota-badge { display: none; }
  .messages { padding: 13px; }
  .composer { padding: 9px 10px calc(9px + env(safe-area-inset-bottom)); }
  .message { max-width: 92%; }
  .chat-minimized, .chat-open-fab { right: 14px; bottom: 14px; }

  .admin-shell { display: block; }
  .admin-sidebar { height: auto; position: static; padding: 10px; }
  .admin-sidebar nav { display: flex; overflow: auto; }
  .admin-sidebar nav button { white-space: nowrap; width: auto; }
  .admin-brand { margin-bottom: 5px; }
  .admin-content { padding: 20px 14px; }
  .form-grid, .users-layout { grid-template-columns: 1fr; }
  .form-grid .full { grid-column: 1; }
}
