:root {
  --screen-bg: #ffffff;
  --sheet-bg: #e9e9eb;
  --chip-bg: #6440ef;
  --chip-text: #ffffff;
  --input-border: #d9d9dd;
  --accent-secondary: #ee9c57;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

button {
  cursor: pointer;
  transition: transform 0.14s ease, filter 0.14s ease, box-shadow 0.14s ease;
}

button:hover {
  filter: brightness(0.97);
}

button:active {
  transform: translateY(1px) scale(0.99);
}

body {
  margin: 0;
  font-family: 'SF Pro Display', 'Segoe UI', system-ui, sans-serif;
  background: #ffffff;
  color: #15151a;
}

/* ===== Loading State ===== */

.loading-state {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  z-index: 100;
}

.loading-spinner {
  width: 40px;
  height: 40px;
  border: 3px solid #e8e8ed;
  border-top-color: var(--chip-bg);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

/* ===== Error State ===== */

.error-state {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  z-index: 100;
  padding: 24px;
}

.error-state-content {
  text-align: center;
  max-width: 320px;
}

.error-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #fee;
  color: #c00;
  font-size: 24px;
  font-weight: 700;
  margin-bottom: 16px;
}

.error-state-content p {
  font-size: 16px;
  color: #555;
  line-height: 1.5;
}

/* ===== Stage & Phone ===== */

.stage {
  height: 100dvh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow: hidden;
  padding: 0;
  background: #111217;
}

.phone {
  position: relative;
  width: 375px;
  height: 847px;
  background: #fff;
  border-radius: 0;
  overflow: hidden;
  transform-origin: top center;
}

/* Iframe mode: no phone frame, fill entire viewport */
.phone.iframe-mode {
  width: 100vw;
  height: 100dvh;
  transform: none !important;
}

.phone.iframe-mode .stage {
  background: #fff;
}

.screen {
  position: absolute;
  inset: 0;
}

.screen.is-active {
  background: #fff;
}

/* ===== Top Header ===== */

.top {
  position: relative;
  height: 80px;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 0;
}

.top h1 {
  margin: 0;
  font-size: 38px;
  font-weight: 700;
  letter-spacing: -0.02em;
  color: #7b7b81;
  text-align: center;
  transform: translate(12px, -6px);
  /* Prevent overflow of long names */
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.top-chat {
  height: 70px;
  background: rgba(255, 255, 255, 0.92);
  border-bottom: 1px solid #e8e8ed;
}

.top-chat h2 {
  margin: 0;
  font-size: 24px;
  font-weight: 700;
  color: #111118;
  max-width: 260px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.back {
  position: absolute;
  left: 16px;
  top: 50%;
  z-index: 2;
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  padding: 0;
  transform: translateY(-50%);
}

.back span {
  display: block;
  width: 14px;
  height: 14px;
  border-left: 3px solid #43235f;
  border-bottom: 3px solid #43235f;
  transform: rotate(45deg);
  margin: 7px 0 0 8px;
}

/* ===== Hero / Avatar Video ===== */

.hero {
  position: absolute;
  left: 36px;
  top: 115px;
  width: 307px;
  height: 388.15px;
  margin: 0;
  background: #fff;
}

.avatar-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center center;
  position: absolute;
  inset: 0;
  background: #fff;
}

.avatar-video.hidden {
  opacity: 0;
  pointer-events: none;
}

/* ===== Switcher Chips ===== */

.switcher {
  position: absolute;
  left: 20px;
  top: 447px;
  width: 337px;
  height: 44px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 5px;
}

.chip {
  border-radius: 12px;
  font-size: 16px;
  border: 2px solid #5f34ff;
  display: grid;
  place-items: center;
  padding: 0;
}

.chip-voice {
  background: transparent;
  color: #1c1b21;
}

.chip-chat {
  background: var(--chip-bg);
  color: var(--chip-text);
}

.chip-live {
  background: #ef4f4f !important;
  border-color: #ef4f4f !important;
  color: #fff !important;
}

.chip-disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.chip-soon {
  font-size: 10px;
  margin-left: 4px;
  opacity: 0.7;
}

/* ===== Composer ===== */

.composer {
  position: absolute;
  left: 16px;
  width: 343px;
  height: 44px;
  border-radius: 12px;
  background: #f1f1f3;
  border: 1px solid var(--input-border);
  display: flex;
  align-items: center;
  padding: 4px;
  gap: 4px;
}

.composer-home { top: 516px; }
.composer-chat { bottom: 20px; }

/* Iframe mode: full width composers */
.phone.iframe-mode .composer {
  width: calc(100% - 32px);
}

.composer input {
  border: 0;
  outline: none;
  flex: 1;
  padding: 0 14px;
  font-size: 17px;
  background: transparent;
  color: #6f6f75;
}

.mini {
  width: 28px;
  height: 28px;
  border: 0;
  background: transparent;
  font-size: 15px;
}

.send {
  width: 47px;
  height: 37px;
  border: 0;
  border-radius: 10px;
  background: linear-gradient(135deg, #7029f3 0%, #a43fd6 48%, #ee9c57 100%);
  color: #fff;
  font-size: 18px;
  display: grid;
  place-items: center;
}

.send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ===== Bottom Sheet / Suggestions ===== */

.bottom-sheet {
  position: absolute;
  left: 0;
  top: 577px;
  width: 375px;
  height: 226px;
  background: var(--sheet-bg);
  border-top-left-radius: 12px;
  border-top-right-radius: 12px;
  padding: 25px 0 18px;
  overflow-y: auto;
}

/* Iframe mode: full width */
.phone.iframe-mode .bottom-sheet {
  width: 100%;
}

.grabber {
  width: 36px;
  height: 3.4px;
  border-radius: 999px;
  background: #c4c4c8;
  margin: 0 auto 22px;
}

.suggestion {
  display: block;
  width: 243px;
  height: 39px;
  margin: 0 auto 12px;
  border: 0;
  border-radius: 18px;
  background: #d7d8dc;
  color: #202126;
  font-size: 17px;
}

/* ===== Chat Canvas ===== */

.chat-canvas {
  position: absolute;
  inset: 86px 16px 84px;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

/* ===== Message Wrapper ===== */

.message-wrapper {
  margin-bottom: 12px;
}

.message-user {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
}

.message-assistant {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
}

/* ===== Bubbles ===== */

.bubble {
  border-radius: 14px;
  padding: 10px 12px;
  font-size: 16px;
  line-height: 1.4;
  max-width: 80%;
  word-wrap: break-word;
  overflow-wrap: break-word;
}

.bubble-out {
  background: var(--chip-bg);
  color: #fff;
}

.bubble-in {
  background: #e8e8ed;
  color: #1f1f25;
}

/* ===== Source Citation ===== */

.source-citation {
  display: block;
  margin-top: 4px;
  font-size: 12px;
  color: #8e8e93;
  line-height: 1.3;
  padding-left: 4px;
}

/* ===== Typing Indicator ===== */

.typing-indicator {
  position: absolute;
  left: 16px;
  bottom: 72px;
  display: flex;
  gap: 5px;
  padding: 10px 14px;
  background: #e8e8ed;
  border-radius: 14px;
  width: fit-content;
}

.typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #999;
  animation: typing-bounce 1.2s ease-in-out infinite;
}

.typing-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.typing-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes typing-bounce {
  0%, 60%, 100% {
    opacity: 0.3;
    transform: translateY(0);
  }
  30% {
    opacity: 1;
    transform: translateY(-4px);
  }
}

/* ===== Feedback Buttons ===== */

.feedback-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-top: 4px;
  padding-left: 4px;
}

.feedback-btn {
  background: none;
  border: 1px solid #ddd;
  border-radius: 16px;
  padding: 3px 8px;
  font-size: 14px;
  cursor: pointer;
  transition: background 0.15s ease, border-color 0.15s ease;
  line-height: 1;
}

.feedback-btn:hover:not(:disabled) {
  background: #f0f0f0;
  border-color: #bbb;
}

.feedback-btn.feedback-active {
  border-color: var(--chip-bg);
  background: rgba(100, 64, 239, 0.1);
}

.feedback-btn.feedback-disabled {
  opacity: 0.5;
  cursor: default;
  pointer-events: none;
}

/* ===== Feedback Comment Form ===== */

.feedback-comment-form {
  width: 100%;
  margin-top: 6px;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.feedback-textarea {
  width: 100%;
  max-width: 260px;
  padding: 8px 10px;
  border: 1px solid #ddd;
  border-radius: 8px;
  font-size: 14px;
  font-family: inherit;
  resize: vertical;
  outline: none;
}

.feedback-textarea:focus {
  border-color: var(--chip-bg);
}

.feedback-error {
  font-size: 12px;
  color: #c00;
  min-height: 14px;
}

.feedback-submit-btn {
  align-self: flex-start;
  padding: 5px 16px;
  border: 0;
  border-radius: 8px;
  background: var(--chip-bg);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.feedback-submit-btn:hover {
  filter: brightness(0.92);
}

.feedback-confirm {
  font-size: 12px;
  color: #4a9;
  margin-left: 4px;
}

/* ===== Debug Panel ===== */

.debug-panel {
  position: fixed;
  right: 12px;
  bottom: 12px;
  z-index: 4;
  width: min(220px, calc(100vw - 24px));
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0;
  padding: 6px 8px;
  border-radius: 8px;
  background: rgba(20, 20, 24, 0.68);
  color: #fff;
}

.debug-panel.active {
  background: rgba(55, 114, 255, 0.84);
}

.debug-panel p {
  margin: 0;
  font-size: 10px;
  line-height: 1.25;
  text-align: center;
}

/* ===== Iframe Responsive Mode ===== */

/* When in iframe, the stage background matches the widget */
.phone.iframe-mode ~ .debug-panel,
.phone.iframe-mode ~ .stage {
  background: #fff;
}

/* In iframe mode, hero and switcher use relative positioning */
.phone.iframe-mode .hero {
  position: relative;
  left: auto;
  top: auto;
  width: 100%;
  max-width: 307px;
  margin: 16px auto 0;
}

.phone.iframe-mode .switcher {
  position: relative;
  left: auto;
  top: auto;
  width: calc(100% - 40px);
  margin: 16px auto 0;
}

.phone.iframe-mode .composer-home {
  position: relative;
  left: auto;
  top: auto;
  width: calc(100% - 32px);
  margin: 16px auto 0;
}

.phone.iframe-mode .bottom-sheet {
  position: relative;
  left: auto;
  top: auto;
  width: 100%;
  height: auto;
  min-height: 100px;
  margin-top: 16px;
}

.phone.iframe-mode .chat-canvas {
  position: absolute;
  inset: 70px 16px 64px;
}

.phone.iframe-mode .composer-chat {
  bottom: 10px;
  width: calc(100% - 32px);
}
