:root {
  --brand: #2563eb;
  --brand-ink: #1e3a8a;
  --brand-soft: #eff4ff;
  --ink: #0f172a;
  --sub: #64748b;
  --line: #e2e8f0;
  --bg: #f6f8fb;
  --card: #ffffff;
  --warm: #f59e0b;
  --warm-soft: #fff7e6;
  --ok: #16a34a;
  --radius: 16px;
  --shadow: 0 1px 2px rgba(15, 23, 42, .04), 0 8px 24px rgba(15, 23, 42, .06);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Hiragino Sans", "Hiragino Kaku Gothic ProN", "Noto Sans JP", system-ui, sans-serif;
  font-size: 15px;
  line-height: 1.7;
}
button, select, input, textarea { font: inherit; color: inherit; }
h1, h2, h3, h4 { margin: 0; line-height: 1.4; }
[hidden] { display: none !important; }

/* top bar */
.topbar {
  position: sticky; top: 0; z-index: 5;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 20px;
  background: rgba(255, 255, 255, .92);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--line);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand b { display: block; font-size: 15px; }
.brand small { display: block; color: var(--sub); font-size: 11px; line-height: 1.3; }
.logo {
  width: 34px; height: 34px; border-radius: 10px;
  display: grid; place-items: center;
  background: var(--brand); color: #fff; font-weight: 800;
}
.top-actions { display: flex; align-items: center; gap: 10px; }
.mode-chip {
  font-size: 12px; font-weight: 700; color: #92400e;
  background: var(--warm-soft); border: 1px solid #fcd9a0;
  padding: 2px 10px; border-radius: 999px;
}

/* buttons */
.primary, .ghost, .link-btn { cursor: pointer; border-radius: 12px; }
.primary {
  border: 0; background: var(--brand); color: #fff; font-weight: 700;
  padding: 12px 20px; box-shadow: 0 6px 16px rgba(37, 99, 235, .25);
}
.primary:hover { background: #1d4ed8; }
.ghost { border: 1px solid var(--line); background: #fff; padding: 8px 14px; font-size: 13px; }
.ghost:hover { border-color: #cbd5e1; }
.link-btn { border: 0; background: none; color: var(--brand); padding: 0; font-weight: 700; font-size: 13px; text-decoration: underline; text-underline-offset: 3px; }
:focus-visible { outline: 3px solid rgba(37, 99, 235, .45); outline-offset: 2px; }

/* layout */
.layout {
  display: grid;
  grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr);
  gap: 16px;
  max-width: 1440px;
  margin: 0 auto;
  padding: 16px 20px 24px;
  align-items: start;
}
body.with-debug .layout { grid-template-columns: minmax(0, 1.1fr) minmax(0, 1fr) 300px; }
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.side { display: grid; gap: 16px; min-width: 0; }
.layout > * { min-width: 0; }
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.count { color: var(--sub); font-size: 12px; }
.fine { color: var(--sub); font-size: 12px; margin: 6px 0; }

/* stage */
.stage { padding: 18px; }
.stage-head { display: grid; grid-template-columns: 168px 1fr; gap: 18px; align-items: center; }
.avatar { margin: 0; position: relative; }
.avatar-frame {
  aspect-ratio: 4 / 5; border-radius: 14px; overflow: hidden;
  background: linear-gradient(180deg, #eef3fb, #f8fafc);
  border: 2px solid var(--line);
  transition: border-color .25s, box-shadow .25s;
}
.avatar-frame img {
  width: 100%; height: 100%; object-fit: cover; object-position: 50% 0;
  transform: scale(2.05); transform-origin: 50% 9%;
}
.avatar[data-state="speaking"] .avatar-frame { border-color: var(--brand); box-shadow: 0 0 0 5px rgba(37, 99, 235, .12); }
.avatar[data-state="listening"] .avatar-frame { border-color: var(--ok); box-shadow: 0 0 0 5px rgba(22, 163, 74, .12); }
.avatar[data-state="thinking"] .avatar-frame { border-color: var(--warm); }
.avatar figcaption {
  position: absolute; left: 8px; bottom: 8px;
  display: flex; align-items: center; gap: 6px;
  background: rgba(255, 255, 255, .94); border-radius: 999px;
  padding: 2px 10px; font-size: 12px; font-weight: 700;
  box-shadow: 0 2px 6px rgba(15, 23, 42, .1);
}
.dot { width: 8px; height: 8px; border-radius: 50%; background: #94a3b8; }
.avatar[data-state="speaking"] .dot { background: var(--brand); }
.avatar[data-state="listening"] .dot { background: var(--ok); }
.avatar[data-state="thinking"] .dot { background: var(--warm); animation: pulse 1s infinite; }
.wave { position: absolute; right: 10px; bottom: 12px; display: flex; gap: 3px; align-items: flex-end; height: 18px; opacity: 0; transition: opacity .2s; }
.wave i { width: 3px; height: 6px; background: var(--brand); border-radius: 2px; }
.avatar[data-state="speaking"] .wave { opacity: 1; }
.avatar[data-state="speaking"] .wave i { animation: wave .9s ease-in-out infinite; }
.wave i:nth-child(2) { animation-delay: .1s !important; }
.wave i:nth-child(3) { animation-delay: .2s !important; }
.wave i:nth-child(4) { animation-delay: .3s !important; }
.wave i:nth-child(5) { animation-delay: .4s !important; }
.nod { animation: nod .55s ease-in-out; }
@keyframes wave { 0%, 100% { height: 5px; } 50% { height: 18px; } }
@keyframes pulse { 50% { opacity: .35; } }
@keyframes nod { 0%, 100% { transform: scale(2.05) translateY(0); } 40% { transform: scale(2.05) translateY(2.2%); } }

.eyebrow { margin: 0; color: var(--brand); font-size: 11px; font-weight: 800; letter-spacing: .14em; }
.intro h1 { font-size: 24px; margin: 4px 0 6px; }
.lead { margin: 0; color: var(--sub); font-size: 14px; }

.start-panel {
  margin-top: 16px; padding: 16px; border-radius: 14px;
  background: var(--brand-soft); border: 1px solid #d6e2ff;
  display: grid; gap: 10px; justify-items: start;
}
.consent { margin: 0; font-size: 13px; }
.scenario { display: grid; gap: 4px; font-size: 12px; color: var(--sub); }
.scenario select { padding: 6px 10px; border-radius: 10px; border: 1px solid var(--line); background: #fff; color: var(--ink); max-width: 100%; }

.captions {
  margin-top: 16px; height: 360px; overflow-y: auto;
  display: flex; flex-direction: column; gap: 10px;
  padding: 4px 2px; scroll-behavior: smooth;
}
body:not(.talking) .captions:empty { display: none; }
.bubble { max-width: 88%; }
.bubble .who { display: block; font-size: 11px; color: var(--sub); margin: 0 4px 2px; }
.bubble p { margin: 0; padding: 10px 14px; border-radius: 14px; font-size: 14px; }
.bubble.moka p { background: var(--brand-soft); border: 1px solid #d6e2ff; border-top-left-radius: 4px; }
.bubble.user { align-self: flex-end; }
.bubble.user .who { text-align: right; }
.bubble.user p { background: #fff; border: 1px solid var(--line); border-top-right-radius: 4px; }

/* hearing sheet */
.sheet { padding: 16px 18px; }
.progress { height: 6px; background: #eef2f7; border-radius: 99px; margin: 8px 0 10px; overflow: hidden; }
.progress i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--brand), #60a5fa); transition: width .4s; }
.sheet-list { margin: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.sheet-row { border: 1px dashed #d4dbe5; border-radius: 10px; padding: 6px 10px; min-width: 0; transition: background .3s, border-color .3s; }
.sheet-row dt { font-size: 11px; color: var(--sub); }
.sheet-row dd { margin: 0; font-size: 13px; color: #94a3b8; overflow-wrap: anywhere; }
.sheet-row.filled { border-style: solid; border-color: #d6e2ff; background: #fbfcff; }
.sheet-row.filled dd { color: var(--ink); font-weight: 700; }
.sheet-row.flash { animation: flash 1.2s ease-out; }
@keyframes flash { 0% { background: #fff3c4; border-color: var(--warm); } 100% { background: #fbfcff; } }

/* offers */
.offers { padding: 16px 18px; }
.offers h2 { font-size: 15px; }
.offer-cards { display: grid; gap: 10px; margin-top: 10px; }
.offer-cards.swap > * { animation: rise .45s ease-out both; }
.offer-cards.swap > *:nth-child(2) { animation-delay: .08s; }
@keyframes rise { from { opacity: 0; transform: translateY(8px); } to { opacity: 1; transform: none; } }
.offer-card { position: relative; border: 1px solid var(--line); border-radius: 14px; padding: 14px 16px; background: #fff; }
.offer-card:first-child { border-color: var(--brand); box-shadow: 0 0 0 3px rgba(37, 99, 235, .08); }
.offer-card.muted { opacity: .62; border-color: var(--line); box-shadow: none; }
.offer-card h4 { font-size: 16px; margin-top: 2px; }
.badge { display: inline-block; font-size: 11px; font-weight: 800; color: #fff; background: var(--brand); padding: 1px 10px; border-radius: 999px; }
.offer-card.muted .badge { background: #94a3b8; }
.price { margin: 4px 0 0; font-size: 18px; font-weight: 800; color: var(--brand-ink); }
.period, .summary { margin: 2px 0; font-size: 13px; color: var(--sub); }
.summary { color: var(--ink); }
.offer-note { margin: 0; color: var(--sub); font-size: 13px; }

/* draft */
.draft { padding: 16px 18px; border-color: var(--brand); }
.draft textarea { width: 100%; border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; font-size: 13px; line-height: 1.75; resize: vertical; background: #fbfcff; }
.row { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }

/* debug panel */
.debug { background: #111827; color: #e5e7eb; border-radius: var(--radius); padding: 16px; font-size: 12px; position: sticky; top: 72px; max-height: calc(100vh - 88px); overflow-y: auto; }
.debug h2 { font-size: 14px; color: #fff; }
.debug h3 { font-size: 12px; margin: 14px 0 6px; color: #9ca3af; }
.debug .fine { color: #9ca3af; }
.debug-calls { margin: 8px 0; font-weight: 700; color: #fff; }
.debug-groups { display: grid; gap: 4px; }
.dq { display: grid; grid-template-columns: 88px 1fr; grid-template-rows: auto auto; column-gap: 8px; padding: 4px 0; border-bottom: 1px solid #1f2937; opacity: .5; }
.dq.on { opacity: 1; }
.dq-label { grid-row: 1 / 3; color: #9ca3af; }
.dq-value { font-weight: 700; display: flex; justify-content: space-between; }
.dq-bar { height: 4px; background: #1f2937; border-radius: 4px; overflow: hidden; margin-top: 3px; }
.dq-bar i { display: block; height: 100%; background: #60a5fa; transition: width .3s; }
.dq-pct { display: none; }
.debug-log { list-style: none; margin: 0; padding: 0; display: grid; gap: 6px; }
.debug-log li { background: #1f2937; border-radius: 8px; padding: 6px 8px; }
.debug-log b { display: block; color: #93c5fd; font-size: 11px; }

/* sample form */
.sample-form { max-width: 720px; margin: 0 auto 40px; padding: 20px; display: grid; gap: 10px; }
.sample-form label { display: grid; gap: 4px; font-size: 13px; font-weight: 700; }
.sample-form input, .sample-form textarea { border: 1px solid var(--line); border-radius: 10px; padding: 9px 12px; font-weight: 400; font-size: 14px; }
.sample-form .primary { justify-self: start; }

/* dialog */
dialog { border: 0; border-radius: 18px; padding: 24px; max-width: min(520px, calc(100vw - 32px)); box-shadow: 0 24px 60px rgba(15, 23, 42, .25); }
dialog::backdrop { background: rgba(15, 23, 42, .4); }
dialog .close { position: absolute; right: 12px; top: 10px; border: 0; background: none; font-size: 22px; cursor: pointer; color: var(--sub); }
.dialog-kind { margin: 0; font-size: 12px; color: var(--brand); font-weight: 700; }
dialog h3 { font-size: 20px; margin: 2px 0; }
dialog ul { padding-left: 1.2em; margin: 8px 0; }
.dialog-pay { font-size: 12px; color: var(--sub); border-top: 1px solid var(--line); padding-top: 8px; }

/* responsive */
@media (max-width: 1180px) {
  body.with-debug .layout { grid-template-columns: minmax(0, 1.15fr) minmax(0, 1fr); }
  body.with-debug .debug { grid-column: 1 / -1; position: static; max-height: none; }
}
@media (max-width: 820px) {
  .layout, body.with-debug .layout { grid-template-columns: minmax(0, 1fr); padding: 12px 16px 20px; }
  .topbar { padding: 8px 16px; }
  .brand small { display: none; }
  .stage { padding: 14px; }
  .stage-head { grid-template-columns: 112px 1fr; gap: 12px; }
  .intro h1 { font-size: 18px; }
  .lead { font-size: 12px; }
  .captions { height: 42vh; min-height: 260px; }
  .sheet-list { grid-template-columns: 1fr 1fr; }
}
@media (max-width: 380px) {
  .mode-chip { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* live mode */
.budget-line { margin: 0; font-size: 13px; font-weight: 700; color: var(--brand-ink); }
.budget-line:empty { display: none; }
.live-line { display: none; margin: 12px 0 0; font-size: 12px; color: var(--sub); justify-content: space-between; gap: 8px; }
body.live .live-line { display: flex; }
#countdown { font-weight: 700; color: var(--brand-ink); white-space: nowrap; }
body.live .mode-chip { color: #991b1b; background: #fef2f2; border-color: #fecaca; }
.primary:disabled { background: #94a3b8; box-shadow: none; cursor: not-allowed; }

/* hearing pipeline (Archify, isolated in an iframe) */
.flow-card { padding: 16px 18px; }
.flow-frame-wrap { margin-top: 8px; }
#pipeline-frame { display: block; width: 100%; height: 0; border: 0; border-radius: 12px; background: #fff; transition: height .3s; }
.flow-card.updated #pipeline-frame { animation: flowIn .6s ease-out; }
@keyframes flowIn { from { opacity: .3; } to { opacity: 1; } }

/* 2D avatar (Mesh Avatar Studio, WebGL2). The still image stays as a fallback. */
#avatar-canvas { display: block; width: 100%; height: 100%; }
.avatar.mesh #avatar-img { display: none; }
.avatar.mesh .wave { display: none; }
