:root {
  color-scheme: light;
  --app-width: 58rem;
  --side: clamp(.75rem, 3.5vw, 1.6rem);
  --ink: #262938;
  --muted: #5d6172;
  --paper: #fffefa;
  --thread: #f1f0f4;
  --line: #dfdde7;
  --accent: #475cc0;
  --accent-hover: #37499f;
  --focus: #5269ce;
  --avatar-size: 2.3rem;
  --view-height: 100vh;
}

@supports (height: 100dvh) { :root { --view-height: 100dvh; } }

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
}

body {
  background: #e9e7ef;
  color: var(--ink);
  font: 1rem/1.55 system-ui, "Microsoft YaHei", "PingFang SC", sans-serif;
}

button, textarea, input, select { font: inherit; }
button, select { cursor: pointer; }
button:disabled { cursor: default; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 3px; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.chat {
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  width: 100%;
  max-width: var(--app-width);
  height: var(--visible-height, var(--view-height));
  position: relative;
  top: var(--viewport-top, 0px);
  margin-inline: auto;
  background: var(--thread);
}

.chat-header {
  display: flex;
  align-items: center;
  gap: .75rem;
  min-width: 0;
  padding: calc(.65rem + env(safe-area-inset-top, 0px))
    max(var(--side), env(safe-area-inset-right, 0px)) .65rem
    max(var(--side), env(safe-area-inset-left, 0px));
  background: var(--paper);
  border-bottom: 1px solid var(--line);
}

.avatar {
  flex: 0 0 var(--avatar-size);
  inline-size: var(--avatar-size);
  block-size: var(--avatar-size);
  display: grid;
  place-items: center;
  border-radius: 50%;
  user-select: none;
  font-size: .93rem;
  line-height: 1;
}

.avatar-yuko {
  color: #533f54;
  background: #eadbe8;
  font-family: "Yu Mincho", "Songti SC", serif;
  font-weight: 700;
}

.avatar-mine { color: #394a8a; background: #dbe2fc; }
.header-avatar { --avatar-size: 2.6rem; font-size: 1.12rem; }
.chat-heading { flex: 1; min-width: 0; }
.chat-heading h1 { margin: 0; font-size: 1.09rem; line-height: 1.25; font-weight: 690; }
.chat-heading h1.is-typing { color: var(--accent); }
.chat-heading p { margin: .18rem 0 0; color: var(--muted); font-size: .72rem; line-height: 1.25; }

button {
  padding: .45rem .8rem;
  border: 1px solid #c8c7d6;
  border-radius: .7rem;
  background: var(--paper);
  color: var(--ink);
}

.icon-button {
  flex-shrink: 0;
  display: grid;
  place-items: center;
  inline-size: 2.75rem;
  block-size: 2.75rem;
  padding: 0;
  border: 0;
  border-radius: .72rem;
  background: #f0edf6;
  color: #404d89;
}

@media (hover: hover) {
  .icon-button:hover { background: #e6e2f3; }
}

.menu-icon, .menu-icon::before, .menu-icon::after {
  inline-size: 1.18rem;
  block-size: 2px;
  border-radius: 1rem;
  background: currentColor;
}

.menu-icon { position: relative; }
.menu-icon::before, .menu-icon::after { content: ""; position: absolute; inset-inline-start: 0; }
.menu-icon::before { inset-block-start: -.33rem; }
.menu-icon::after { inset-block-start: .33rem; }

#thread, .avatar-stage { grid-area: 2 / 1; }

.avatar-stage {
  position: relative;
  overflow: hidden;
  pointer-events: none;
  background: radial-gradient(ellipse at 66% 38%, #faf7f0, #ece8f3 58%, #e5e3ef);
}

.avatar-stage canvas {
  position: absolute;
  inset-block-end: 0;
  inset-inline-start: 62%;
  translate: -50% 0;
  height: min(96%, 43rem);
  width: auto;
  max-width: 87%;
  object-fit: contain;
  object-position: bottom;
  opacity: .79;
  transform-origin: 54% 92%;
  animation: avatar-breathe 5.4s ease-in-out infinite;
}

.avatar-stage.natural-motion canvas { animation: none; }
#avatar-compare { white-space: nowrap; font-size: .8rem; }

@keyframes avatar-breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: translateY(-2px) scale(1.004, 1.008); }
}

@media (max-width: 38rem) {
  .avatar-stage canvas { inset-inline-start: 54%; height: min(92%, 35rem); opacity: .62; }
}

@media (prefers-reduced-motion: reduce) {
  .avatar-stage canvas { animation: none; }
}

#thread {
  position: relative;
  min-inline-size: 0;
  min-block-size: 0;
  overflow-y: auto;
  overscroll-behavior-y: contain;
  padding: .35rem max(var(--side), env(safe-area-inset-right, 0px)) 1.1rem
    max(var(--side), env(safe-area-inset-left, 0px));
  scrollbar-color: #c4c3d2 transparent;
}

#thread:focus-visible { outline-offset: -3px; }

.row {
  display: flex;
  align-items: flex-start;
  gap: .58rem;
  margin-block: .9rem;
  min-inline-size: 0;
}

.row.mine { justify-content: flex-end; }

.bubble {
  min-inline-size: 0;
  max-inline-size: min(calc(100% - var(--avatar-size) - .9rem), 36rem);
  padding: .64rem .88rem;
  border-radius: .42rem 1.06rem 1.06rem;
  background: #fff;
  box-shadow: 0 1px 2px #25253d0e;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.mine .bubble {
  border-radius: 1.06rem .42rem 1.06rem 1.06rem;
  background: var(--accent);
  color: #fff;
}

.bubble button {
  display: block;
  max-inline-size: 100%;
  min-block-size: 2.1rem;
  margin-block-start: .45rem;
  padding: .2rem .56rem;
  font-size: .81rem;
  font-weight: 550;
  white-space: normal;
  color: #414d8e;
  background: #f3f4fe;
  border-color: #dce1f7;
}

.message-stack {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: .45rem;
  min-inline-size: 0;
  max-inline-size: min(calc(100% - var(--avatar-size) - .9rem), 36rem);
}

.mine .message-stack { align-items: flex-end; }
.message-stack .bubble { max-inline-size: 100%; }

.voice-bubble {
  display: flex;
  align-items: center;
  gap: .65rem;
  inline-size: 12rem;
  max-inline-size: 100%;
  min-block-size: 2.75rem;
  padding: .55rem .75rem;
  border: 1px solid #dce1f7;
  border-radius: .38rem .95rem .95rem;
  background: #f6f7ff;
  color: #394a9f;
  box-shadow: 0 1px 2px #25253d0e;
}

@media (hover: hover) {
  .voice-bubble:hover:not(:disabled) { background: #ecefff; }
}

.voice-icon {
  inline-size: .75rem;
  block-size: .78rem;
  flex: 0 0 auto;
  background: currentColor;
  clip-path: polygon(10% 0, 100% 50%, 10% 100%);
}

.voice-bubble.is-playing .voice-icon {
  clip-path: polygon(0 0, 32% 0, 32% 100%, 0 100%, 0 0, 68% 0, 100% 0, 100% 100%, 68% 100%, 68% 0);
}

.voice-progress {
  flex: 1 1 auto;
  min-inline-size: 1.35rem;
  inline-size: 5.15rem;
  block-size: .28rem;
  overflow: hidden;
  appearance: none;
  border: 0;
  border-radius: .3rem;
  background: #dce2f8;
}

.voice-progress::-webkit-progress-bar { background: #dce2f8; }
.voice-progress::-webkit-progress-value { background: var(--accent); border-radius: .3rem; }
.voice-progress::-moz-progress-bar { background: var(--accent); border-radius: .3rem; }
.voice-length { font-size: .83rem; font-variant-numeric: tabular-nums; white-space: nowrap; }

.message-image {
  display: block;
  overflow: hidden;
  padding: 0;
  border: 1px solid #dfdde7;
  border-radius: .86rem;
  cursor: zoom-in;
  background: #fff;
  max-width: 100%;
}

.message-image img { display: block; width: 100%; height: 100%; object-fit: contain; }
.photo-image { width: min(18rem, 62vw); aspect-ratio: 4 / 3; }
.sticker-image { width: clamp(5rem, 28vw, 7.3rem); aspect-ratio: 1; }

.empty {
  max-width: 21rem;
  margin: clamp(1.8rem, 12vh, 7rem) auto 1.1rem;
  text-align: center;
  color: var(--muted);
  font-size: .92rem;
}

.empty button { margin-top: .6rem; }
.older-chat { display: block; margin: .6rem auto .8rem; font-size: .85rem; color: #414d8e; }

.composer-area {
  min-width: 0;
  background: var(--paper);
  border-top: 1px solid var(--line);
  padding: .63rem max(var(--side), env(safe-area-inset-right, 0px))
    calc(.63rem + env(safe-area-inset-bottom, 0px))
    max(var(--side), env(safe-area-inset-left, 0px));
}

#notice { margin: 0 0 .38rem; color: #654b64; font-size: .83rem; overflow-wrap: anywhere; }
#notice:empty { margin: 0; }

.attachment-preview {
  display: flex;
  align-items: center;
  gap: .6rem;
  width: fit-content;
  max-width: 100%;
  padding: .28rem .35rem;
  margin: 0 0 .45rem;
  border: 1px solid var(--line);
  border-radius: .66rem;
  background: #f5f3fa;
  color: var(--muted);
  font-size: .82rem;
}

.attachment-preview img { width: 2.9rem; height: 2.9rem; object-fit: cover; border-radius: .4rem; }
.attachment-preview span { min-width: 0; max-width: 13rem; text-overflow: ellipsis; overflow: hidden; white-space: nowrap; }
.attachment-preview button { font-size: 1.2rem; }

#compose { display: flex; align-items: flex-end; gap: .6rem; }
.attach-button { block-size: 2.95rem; font-size: 1.35rem; line-height: 1; }
.attach-button:disabled, .attachment-preview button:disabled { opacity: .52; }

#message {
  flex: 1;
  min-inline-size: 0;
  min-block-size: 2.95rem;
  max-block-size: 7rem;
  padding: .7rem .85rem;
  border: 1px solid #d0cedc;
  border-radius: 1.05rem;
  background: #fff;
  color: var(--ink);
  resize: none;
  field-sizing: content;
}

#message::placeholder { color: #686b7b; }

.accent { background: var(--accent); color: white; border-color: var(--accent); font-weight: 650; }
#send { flex-shrink: 0; min-inline-size: 4.2rem; min-block-size: 2.95rem; border-radius: 1.05rem; }
.accent:disabled { color: #454e78; border-color: #dce1f2; background: #dce1f2; }

@media (hover: hover) {
  .accent:not(:disabled):hover { background: var(--accent-hover); border-color: var(--accent-hover); }
}

dialog {
  position: fixed;
  inset: calc(.75rem + env(safe-area-inset-top, 0px))
    max(.6rem, calc((100% - var(--app-width)) / 2 + .65rem), env(safe-area-inset-right, 0px))
    auto auto;
  margin: 0;
  width: min(24rem, calc(100% - max(.6rem, env(safe-area-inset-right, 0px)) - max(.6rem, env(safe-area-inset-left, 0px))));
  max-width: none;
  max-height: calc(var(--visible-height, var(--view-height)) - 1.5rem - env(safe-area-inset-top, 0px) - env(safe-area-inset-bottom, 0px));
  overflow: auto;
  padding: 1rem 1.05rem 1.05rem;
  border: 1px solid var(--line);
  border-radius: 1rem;
  background: var(--paper);
  color: var(--ink);
  box-shadow: 0 12px 38px #272b4430;
}

dialog::backdrop { background: #28274465; }
.photo-viewer {
  inset: 0;
  margin: auto;
  width: min(46rem, 96vw);
  max-height: calc(var(--visible-height, var(--view-height)) - 1rem);
  padding: .55rem;
}

.photo-viewer button { margin-inline-start: auto; margin-block-end: .4rem; }
.photo-viewer img { display: block; max-width: 100%; max-height: calc(var(--visible-height, var(--view-height)) - 5rem); margin-inline: auto; object-fit: contain; }
.panel-heading { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.panel-heading h2 { margin: 0; font-size: 1.13rem; }
.close-button { font-size: 1.45rem; line-height: 1; }
.settings-group { margin: .9rem 0 0; padding: .8rem 0 0; min-width: 0; border: 0; border-top: 1px solid var(--line); }
.settings-group legend, .section-label, .settings-group h3 { padding: 0; margin: 0; font-size: .93rem; font-weight: 700; }
.settings-group legend { padding-inline-end: .65rem; }
.settings-row { display: flex; gap: .55rem; align-items: center; justify-content: space-between; min-height: 2.65rem; }
.settings-row select { min-width: 6.25rem; padding: .36rem .55rem; color: var(--ink); background: #f4f3f9; border: 1px solid #cdccda; border-radius: .52rem; }
.settings-row select:disabled { color: #676a79; }
.settings-row input { inline-size: 1.1rem; block-size: 1.1rem; accent-color: var(--accent); }
.avatar-picker { margin-block: 0 .65rem; }
.avatar-picker summary { padding: .45rem 0; cursor: pointer; }
.avatar-picker summary span { float: inline-end; color: var(--muted); font-size: .87rem; }
.avatar-options { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: .42rem; }
.avatar-option { position: relative; cursor: pointer; }
.avatar-option > input { position: absolute; inset: 0 auto auto 0; opacity: 0; }
.avatar-option-content {
  display: grid;
  grid-template-rows: auto 1fr;
  min-height: 100%;
  overflow: hidden;
  border: 1px solid #d4d1df;
  border-radius: .65rem;
  background: #f4f3f8;
  color: var(--ink);
}
.avatar-option-content img {
  display: block;
  width: 100%;
  height: 4.5rem;
  object-fit: contain;
  object-position: bottom;
  background: linear-gradient(145deg, #f8f3ec, #e8e5f2);
}
.avatar-option-content img[hidden] { display: none; }
.avatar-option-content span { align-self: center; padding: .3rem .2rem; font-size: .75rem; line-height: 1.2; text-align: center; }
.avatar-option > input:checked + .avatar-option-content {
  border-color: var(--accent);
  box-shadow: inset 0 0 0 1px var(--accent);
  background: #e9edff;
}
.avatar-option > input:focus-visible + .avatar-option-content { outline: 2px solid var(--focus); outline-offset: 2px; }
.avatar-off { display: flex; align-items: center; gap: .4rem; margin-top: .45rem; min-height: 2rem; font-size: .87rem; cursor: pointer; }
.avatar-off[hidden] { display: none; }
.avatar-off input { accent-color: var(--accent); }
.settings-group #avatar-preview:disabled { opacity: .6; }
.avatar-preview-actions { display: flex; flex-wrap: wrap; gap: .45rem; }
.fine-print { margin: .35rem 0 0; font-size: .83rem; color: var(--muted); overflow-wrap: anywhere; }
.fine-print:empty { display: none; }
.connection input { display: block; width: 100%; padding: .6rem .68rem; margin-top: .4rem; border: 1px solid #cdccda; background: #fff; color: var(--ink); border-radius: .62rem; }
.connection button, .clear-button { margin-top: .72rem; }
.clear-button { color: #872d47; border-color: #e4c4cf; }

@media (min-width: 50rem) {
  body { padding: 1.25rem 1rem; }
  .chat {
    height: calc(var(--visible-height, var(--view-height)) - 2.5rem);
    border: 1px solid #d8d6e2;
    border-radius: 1rem;
    overflow: hidden;
    box-shadow: 0 10px 34px #26254014;
  }
  dialog { inset-block-start: 1.9rem; }
}

@media (max-width: 22rem) {
  :root { --avatar-size: 2rem; }
  .row { gap: .45rem; }
  .bubble { padding-inline: .7rem; }
  dialog { padding-inline: .8rem; }
}
