:root {
  color-scheme: light;
  font-family: Inter, ui-rounded, "SF Pro Rounded", system-ui, sans-serif;
  background: #e8e8fa;
  color: #29253d;
}

* { box-sizing: border-box; }

body {
  min-height: 100dvh;
  margin: 0;
  padding: 104px 16px 40px;
  display: grid;
  place-items: start center;
  background:
    radial-gradient(circle at 68% 23%, rgb(255 255 255 / 90%) 0 12%, transparent 34%),
    linear-gradient(145deg, #f9f5ff 0 45%, #dcecf5 100%);
}

.app-header {
  position: fixed;
  inset: 0 0 auto;
  z-index: 10;
  min-height: 72px;
  padding: 14px clamp(16px, 4vw, 64px);
  display: flex;
  align-items: center;
  justify-content: space-between;
  border-bottom: 1px solid rgb(99 85 151 / 12%);
  background: rgb(249 247 255 / 82%);
  backdrop-filter: blur(14px);
}

.brand {
  display: flex;
  align-items: center;
  gap: 9px;
  color: #544b78;
  font-size: 17px;
  font-weight: 750;
  letter-spacing: 0;
}

.brand span {
  width: 12px;
  height: 12px;
  border: 3px solid #fff;
  border-radius: 50%;
  background: #7b6fd0;
  box-shadow: 0 0 0 2px #7b6fd0;
}

.header-controls {
  display: flex;
  align-items: center;
  gap: 14px;
}

#language {
  padding: 7px 28px 7px 10px;
  border: 1px solid #c8c2dd;
  border-radius: 8px;
  background: #fff;
  color: #29253d;
  font: inherit;
  font-size: 14px;
}

#language:focus-visible { outline: 3px solid rgb(123 111 208 / 35%); outline-offset: 2px; }

.mode-toggle {
  display: flex;
  align-items: center;
  gap: 9px;
  padding: 6px;
  border: 0;
  background: transparent;
  color: #29253d;
  font: inherit;
  font-size: 14px;
  cursor: pointer;
}

.mode-toggle-track {
  position: relative;
  width: 46px;
  height: 26px;
  border-radius: 999px;
  background: #c8c2dd;
  box-shadow: inset 0 0 0 1px rgb(68 56 111 / 14%);
  transition: background .18s ease;
}

.mode-toggle-track span {
  position: absolute;
  top: 3px;
  left: 3px;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 2px 6px rgb(45 36 73 / 25%);
  transition: transform .18s ease;
}

.mode-toggle[aria-checked="true"] .mode-toggle-track { background: #7b6fd0; }
.mode-toggle[aria-checked="true"] .mode-toggle-track span { transform: translateX(20px); }
.mode-toggle:focus-visible { outline: 3px solid rgb(123 111 208 / 35%); outline-offset: 3px; border-radius: 8px; }

[dir="rtl"] #language { padding: 7px 10px 7px 28px; }
[dir="rtl"] .mode-toggle-track span { right: 3px; left: auto; }
[dir="rtl"] .mode-toggle[aria-checked="true"] .mode-toggle-track span { transform: translateX(-20px); }

.scene {
  width: 100%;
  max-width: 520px;
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 12px;
}

#avatar,
.speech-bubble { display: none; }

.assistant-panel {
  width: 100%;
  padding: 28px 32px;
  display: grid;
  gap: 18px;
  border: 1px solid rgb(99 85 151 / 18%);
  border-radius: 24px;
  background: rgb(255 255 255 / 88%);
  box-shadow: 0 18px 50px rgb(57 46 92 / 14%);
  backdrop-filter: blur(14px);
}

.form-view { display: contents; }

.notice {
  flex-basis: 100%;
  margin: 0;
  padding: 10px 12px;
  border-radius: 8px;
  background: #eceeec;
  color: #444;
  font-size: 14px;
  line-height: 1.4;
}

.notice.frozen {
  background: #f0ecfa;
  color: #544b78;
}

.notice.warning {
  background: #fbecec;
  color: #8a2b2b;
}

.notice[hidden] { display: none; }

form {
  flex-basis: 100%;
  display: grid;
  grid-template-columns: 1fr;
  gap: 4px;
}

.form-view h2 {
  margin: 0;
  color: #4d466d;
  font-size: 17px;
}

label {
  color: #544b78;
  font-size: 13px;
}

#patient input,
#patient select {
  margin-bottom: 8px;
  padding: 10px 12px;
  border: 1px solid #c8c2dd;
  border-radius: 8px;
  background: #fff;
  color: #111;
  font: inherit;
}

#patient input:read-only,
#patient select:disabled {
  background: #f3f1f7;
  color: #555;
}

#patient input.changed {
  border-color: #319343;
  box-shadow: 0 0 0 2px rgb(49 147 67 / 20%);
}

.controls {
  flex-basis: 100%;
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  width: 100%;
}

.call-direction {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  margin: 0;
  padding: 4px;
  border: 0;
  border-radius: 10px;
  background: #efecf6;
}

.call-direction label {
  position: relative;
  color: #716b87;
  font-size: 14px;
  font-weight: 650;
  text-align: center;
  cursor: pointer;
}

.call-direction input {
  position: absolute;
  opacity: 0;
}

.call-direction span {
  display: block;
  padding: 10px 8px;
  border-radius: 8px;
}

.call-direction input:checked + span {
  background: #fff;
  color: #332e4e;
  box-shadow: 0 2px 7px rgb(45 36 73 / 12%);
}

.call-direction input:focus-visible + span {
  outline: 3px solid rgb(123 111 208 / 28%);
  outline-offset: 2px;
}

.call-direction input:disabled + span { opacity: .5; cursor: not-allowed; }

.call-controls { display: grid; gap: 8px; }

.controls button {
  padding: 12px 18px;
  border: 1px solid #c8c2dd;
  border-radius: 8px;
  background: #fff;
  color: #544b78;
  font: inherit;
  cursor: pointer;
}

#call {
  width: 100%;
  min-height: 54px;
  font-size: 17px;
  font-weight: 700;
}

#call[data-state="idle"]::before { content: "☎"; margin-right: 9px; }

#patient input:focus-visible,
.controls button:focus-visible {
  outline: 3px solid rgb(123 111 208 / 28%);
  outline-offset: 2px;
}

#call[data-state="idle"] {
  border-color: #287a36;
  background: #319343;
  color: #fff;
}

#call[data-state="connecting"] {
  border-color: #777;
  background: #888;
  color: #fff;
}

#call[data-state="active"] {
  border-color: #c33;
  background: #d44;
  color: #fff;
}

button:disabled { cursor: wait; }

#enable-audio { flex-basis: 100%; }
#enable-audio[hidden] { display: none; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

body[data-mode="avatar"] {
  display: block;
  padding: 0;
  background: #e8e8fa;
}

body[data-mode="avatar"] .scene {
  position: relative;
  max-width: none;
  min-height: 100dvh;
  display: block;
  overflow: hidden;
  isolation: isolate;
  background:
    radial-gradient(circle at 68% 23%, rgb(255 255 255 / 90%) 0 12%, transparent 34%),
    linear-gradient(145deg, #f9f5ff 0 45%, #dcecf5 100%);
}

body[data-mode="avatar"] .scene::before {
  position: absolute;
  inset: 0;
  z-index: -2;
  content: "";
  background:
    linear-gradient(90deg, transparent 0 65%, rgb(255 255 255 / 55%) 65% 66%, transparent 66% 84%, rgb(255 255 255 / 45%) 84% 85%, transparent 85%),
    linear-gradient(0deg, transparent 0 69%, rgb(255 255 255 / 60%) 69% 70%, transparent 70%);
  opacity: .8;
}

body[data-mode="avatar"] .scene::after {
  position: absolute;
  inset: 72% -20% -20%;
  z-index: -1;
  content: "";
  background: repeating-linear-gradient(100deg, #c9c9e3 0 1px, transparent 1px 14vw), #eeeef8;
  border-top: 2px solid rgb(119 112 154 / 18%);
  transform: perspective(360px) rotateX(56deg);
  transform-origin: top;
}

body[data-mode="avatar"] #avatar {
  position: absolute;
  bottom: 0;
  left: calc(50% - clamp(180px, 15vw, 240px));
  z-index: 2;
  display: block;
  width: auto;
  height: min(100dvh, 1120px);
  max-width: 70vw;
  object-fit: contain;
  object-position: center bottom;
  filter: drop-shadow(0 24px 25px rgb(45 36 73 / 20%));
  transform: translateX(-50%);
  transform-origin: center bottom;
  animation: arrive .55s cubic-bezier(.2, .8, .2, 1) both;
}

body[data-mode="avatar"] .assistant-panel {
  position: absolute;
  top: 34%;
  left: calc(50% + clamp(45px, 5vw, 85px));
  z-index: 3;
  display: grid;
  gap: 22px;
  width: min(470px, 38vw);
  padding: 32px 40px 27px;
  border: 1px solid rgb(99 85 151 / 18%);
  border-radius: 24px;
  background: rgb(255 255 255 / 88%);
  box-shadow: 0 18px 50px rgb(57 46 92 / 14%);
  backdrop-filter: blur(14px);
  text-align: center;
}

body[data-mode="avatar"] .form-view { display: none; }

body[data-mode="avatar"] .speech-bubble { display: block; }
body[data-mode="avatar"] .speech-bubble span { display: none; }

body[data-mode="avatar"] .speech-bubble strong {
  display: block;
  font-size: clamp(24px, 2.2vw, 34px);
  font-weight: 700;
  line-height: 1.25;
  text-wrap: balance;
}

body[data-mode="avatar"] .call-controls {
  display: grid;
  justify-items: center;
  gap: 9px;
}

body[data-mode="avatar"] .controls button {
  min-width: 190px;
  padding: 13px 20px;
  border: 1px solid transparent;
  border-radius: 999px;
  color: #fff;
  font-weight: 720;
  transition: background .18s ease, box-shadow .18s ease, transform .18s ease;
}

body[data-mode="avatar"] .controls button:hover:not(:disabled) { transform: translateY(-1px); }
body[data-mode="avatar"] .controls button:focus-visible { outline: 3px solid rgb(123 111 208 / 35%); outline-offset: 3px; }

body[data-mode="avatar"] #call {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  width: 100%;
  min-height: 64px;
  font-size: 19px;
}

body[data-mode="avatar"] #call::before {
  width: 27px;
  height: 27px;
  flex: 0 0 auto;
  content: "";
  background: currentColor;
  -webkit-mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='none' stroke='black' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Zm-7 9v1a7 7 0 0 0 14 0v-1m-7 8v3m-4 0h8'/%3E%3C/svg%3E") center / contain no-repeat;
  mask: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='none' stroke='black' stroke-linecap='round' stroke-linejoin='round' stroke-width='2' d='M12 2a3 3 0 0 0-3 3v7a3 3 0 0 0 6 0V5a3 3 0 0 0-3-3Zm-7 9v1a7 7 0 0 0 14 0v-1m-7 8v3m-4 0h8'/%3E%3C/svg%3E") center / contain no-repeat;
}

body[data-mode="avatar"] #call:not([data-state="idle"])::before { display: none; }
body[data-mode="avatar"] #call[data-state="idle"] { background: #35a55a; box-shadow: 0 8px 20px rgb(53 165 90 / 25%); }
body[data-mode="avatar"] #call[data-state="connecting"] { background: #8b8994; }
body[data-mode="avatar"] #call[data-state="active"] { background: #dd5963; box-shadow: 0 8px 20px rgb(221 89 99 / 24%); }

body[data-mode="avatar"] #enable-audio {
  min-width: 0;
  padding: 7px 12px;
  background: #7b6fd0;
  font-size: 12px;
}

@keyframes arrive {
  from { opacity: 0; transform: translateX(-50%) translateY(18px); }
}

@media (max-width: 760px) {
  body { padding: 88px 12px 24px; }
  .app-header { min-height: 64px; padding-block: 10px; }
  .header-controls { gap: 6px; }
  #language { max-width: 96px; font-size: 12px; }
  .mode-toggle { gap: 6px; font-size: 12px; }
  .assistant-panel { padding: 20px 18px; border-radius: 20px; }

  body[data-mode="avatar"] #avatar {
    bottom: 58px;
    left: 50%;
    height: 77dvh;
    max-width: 100vw;
  }

  body[data-mode="avatar"] .assistant-panel { display: contents; }

  body[data-mode="avatar"] .speech-bubble {
    position: absolute;
    top: 80px;
    left: 50%;
    z-index: 3;
    width: calc(100% - 32px);
    min-height: 82px;
    padding: 14px 17px 16px;
    border: 1px solid rgb(99 85 151 / 18%);
    border-radius: 22px;
    background: rgb(255 255 255 / 90%);
    box-shadow: 0 18px 50px rgb(57 46 92 / 14%);
    backdrop-filter: blur(14px);
    text-align: left;
    transform: translateX(-50%);
  }

  body[data-mode="avatar"] .speech-bubble span {
    display: block;
    margin-bottom: 6px;
    color: #7b6fd0;
    font-size: 11px;
    font-weight: 800;
    letter-spacing: 0;
    text-transform: uppercase;
  }

  body[data-mode="avatar"] .speech-bubble strong {
    font-size: clamp(18px, 5vw, 25px);
    font-weight: 650;
  }

  body[data-mode="avatar"] .call-controls {
    position: absolute;
    right: 50%;
    bottom: 14px;
    z-index: 4;
    min-width: min(310px, calc(100% - 28px));
    transform: translateX(50%);
  }

  body[data-mode="avatar"] #call { min-height: 0; font-size: inherit; }
  body[data-mode="avatar"] .controls button { width: 100%; }
}

@media (max-width: 340px) {
  .brand { font-size: 0; }
}

@media (prefers-reduced-motion: reduce) {
  #avatar { animation: none; }
  .mode-toggle-track,
  .mode-toggle-track span,
  body[data-mode="avatar"] .controls button { transition: none; }
}
