/*
 * Chat widget (Home / Chat / Messages / Help layout).
 * Brand tokens (--aisk-primary, --aisk-primary-dark, --aisk-primary-soft,
 * --aisk-primary-ring, --aisk-on-primary) are set inline from the admin color.
 */

$aisk-radius: 22px;
$aisk-surface: #f6f7f9;
$aisk-border: #e7e8ec;
$aisk-text: #111827;
$aisk-muted: #6b7280;
$aisk-footer-height: 28px;

.support-buddy-widget {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
  font-size: 14px;
  line-height: 1.5;
  color: $aisk-text;

  *,
  *::before,
  *::after {
    box-sizing: border-box;
  }

  button {
    font-family: inherit;
    text-transform: none;
    letter-spacing: normal;
  }
}

/* ---------- Panel ---------- */

.support-buddy-container {
  position: relative;
  display: flex;
  flex-direction: column;
  width: 400px;
  height: min(680px, calc(100vh - 120px));
  margin-bottom: 16px;
  background: $aisk-surface;
  border-radius: $aisk-radius;
  box-shadow: 0 24px 60px rgba(17, 24, 39, 0.18), 0 4px 14px rgba(17, 24, 39, 0.08);
  overflow: hidden;
  transition: width 0.25s ease, height 0.25s ease;

  &.is-expanded {
    width: min(620px, calc(100vw - 48px));
    height: min(860px, calc(100vh - 110px));
  }
}

.aisk-view {
  position: relative;
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

/* ---------- Shared controls ---------- */

.aisk-header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}

.aisk-icon-button {
  width: 34px;
  height: 34px;
  padding: 0;
  margin: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: transparent;
  color: inherit;
  cursor: pointer;
  opacity: 0.85;
  transition: background-color 0.2s ease, opacity 0.2s ease;

  &:hover,
  &:focus-visible {
    background: rgba(255, 255, 255, 0.16);
    opacity: 1;
  }
}

.aisk-primary-button {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 10px 18px;
  border: none;
  border-radius: 999px;
  background: var(--aisk-primary);
  color: var(--aisk-on-primary);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  box-shadow: 0 6px 16px var(--aisk-primary-ring);
  transition: transform 0.15s ease, opacity 0.2s ease;

  &:hover {
    transform: translateY(-1px);
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
    transform: none;
  }
}

.aisk-empty-state {
  padding: 32px 16px;
  text-align: center;
  color: $aisk-muted;
}

/* ---------- Composer ---------- */

.aisk-composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0;
  padding: 8px 8px 8px 16px;
  background: #fff;
  border: 1px solid $aisk-border;
  border-radius: 18px;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;

  &:focus-within {
    border-color: var(--aisk-primary);
    box-shadow: 0 0 0 3px var(--aisk-primary-ring);
  }

  textarea {
    flex: 1;
    min-height: 24px;
    max-height: 120px;
    margin: 0 0 6px;
    padding: 0;
    border: none !important;
    background: transparent !important;
    box-shadow: none !important;
    outline: none !important;
    resize: none;
    font: inherit;
    font-size: 14px;
    line-height: 1.5;
    color: $aisk-text;

    &::placeholder {
      color: #9ca3af;
    }
  }

  &.is-large {
    flex-direction: column;
    align-items: stretch;
    padding: 16px 12px 12px 18px;
    border: none;
    border-radius: 20px;

    textarea {
      min-height: 44px;
      margin-bottom: 4px;
    }

    .aisk-composer-send {
      align-self: flex-end;
    }
  }
}

.aisk-composer-send {
  width: 36px;
  height: 36px;
  padding: 0 !important;
  margin: 0 !important;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border: none;
  border-radius: 50%;
  background: var(--aisk-primary);
  color: var(--aisk-on-primary);
  cursor: pointer;
  transition: transform 0.15s ease, opacity 0.2s ease;

  &:hover:not(:disabled) {
    transform: scale(1.06);
  }

  &:disabled {
    opacity: 0.35;
    cursor: default;
  }
}

/* ---------- Home ---------- */

.aisk-home {
  overflow-y: auto;
  background:
    linear-gradient(180deg,
      var(--aisk-primary-dark) 0px,
      var(--aisk-primary) 190px,
      $aisk-surface 330px);
  scrollbar-width: thin;
}

.aisk-hero {
  padding: 22px 20px 64px 24px;
  color: var(--aisk-on-primary);
}

.aisk-hero-top {
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 34px;
}

.aisk-hero-brand {
  display: flex;
  align-items: center;
  min-width: 0;
}

.aisk-hero-logo {
  max-width: 140px;
  max-height: 36px;
  object-fit: contain;
}

.aisk-hero-name {
  font-size: 16px;
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.aisk-hero-text {
  margin-top: 44px;
}

.aisk-hero-title {
  margin: 0;
  font-size: 22px;
  font-weight: 400;
  line-height: 1.3;
  opacity: 0.85;
  color: inherit;
}

.aisk-hero-subtitle {
  margin: 2px 0 0;
  padding: 0;
  font-size: 24px;
  font-weight: 700;
  line-height: 1.3;
  letter-spacing: -0.01em;
  color: inherit;
}

.aisk-home-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
  margin-top: -40px;
  padding: 0 16px 104px;
}

.aisk-home-composer-card {
  border-radius: 20px;
  background: #fff;
  box-shadow: 0 10px 30px rgba(17, 24, 39, 0.12);
}

/* ---------- Chips ---------- */

.aisk-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;

  &.is-stacked {
    flex-direction: column;
    align-items: flex-start;
    margin: 4px 0 12px;
  }
}

.aisk-chip {
  margin: 0;
  padding: 7px 14px;
  border: 1px solid $aisk-border;
  border-radius: 999px;
  background: #fff;
  color: $aisk-text;
  font-size: 13px;
  font-weight: 500;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  transition: border-color 0.2s ease, background-color 0.2s ease, color 0.2s ease;

  &:hover:not(:disabled) {
    border-color: var(--aisk-primary);
    background: var(--aisk-primary-soft);
    color: var(--aisk-primary);
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

/* ---------- Cards ---------- */

.aisk-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  margin: 0;
  padding: 14px 16px;
  border: 1px solid $aisk-border;
  border-radius: 16px;
  background: #fff;
  color: $aisk-text;
  text-align: left;
  box-shadow: 0 1px 2px rgba(17, 24, 39, 0.04);
}

.aisk-card-link {
  cursor: pointer;
  transition: border-color 0.2s ease, box-shadow 0.2s ease, transform 0.15s ease;

  &:hover:not(:disabled) {
    border-color: var(--aisk-primary-ring);
    box-shadow: 0 6px 18px rgba(17, 24, 39, 0.08);
    transform: translateY(-1px);
  }

  &:disabled {
    opacity: 0.5;
    cursor: not-allowed;
  }
}

.aisk-card-icon {
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 12px;
  background: var(--aisk-primary-soft);
  color: var(--aisk-primary);

  &.is-plain {
    background: #f3f4f6;
  }

  img {
    width: 22px;
    height: 22px;
  }
}

.aisk-card-content {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
}

.aisk-card-title {
  font-size: 14px;
  font-weight: 600;
  line-height: 1.4;
}

.aisk-card-text {
  font-size: 13px;
  color: $aisk-muted;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.aisk-card-chevron {
  flex-shrink: 0;
  color: #9ca3af;
}

/* ---------- Search for help (Home + Help) ---------- */

.aisk-help-search {
  display: flex;
  flex-direction: column;
  padding: 8px;
  border: 1px solid $aisk-border;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 1px 2px rgba(17, 24, 39, 0.04);
}

.aisk-help-search-field {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 0 0 4px;
  padding: 0 6px 0 14px;
  border-radius: 10px;
  background: #f3f4f6;
  transition: box-shadow 0.2s ease, background-color 0.2s ease;

  &:focus-within {
    background: #fff;
    box-shadow: 0 0 0 2px var(--aisk-primary-ring);
  }

  input {
    flex: 1;
    min-width: 0;
    height: 40px;
    margin: 0;
    padding: 0;
    border: none !important;
    background: transparent !important;
    box-shadow: none !important;
    outline: none !important;
    font: inherit;
    font-size: 14px;
    font-weight: 600;
    color: $aisk-text;

    &::placeholder {
      color: $aisk-text;
      opacity: 1;
    }

    &:focus::placeholder {
      color: #9ca3af;
      font-weight: 400;
    }
  }

  button {
    width: 32px;
    height: 32px;
    padding: 0;
    margin: 0;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    border: none;
    border-radius: 8px;
    background: transparent;
    color: $aisk-text;
    cursor: pointer;

    &:hover {
      color: var(--aisk-primary);
    }
  }
}

.aisk-question-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  width: 100%;
  margin: 0;
  padding: 10px 8px 10px 14px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: $aisk-text;
  font-size: 14px;
  line-height: 1.4;
  text-align: left;
  cursor: pointer;
  transition: background-color 0.2s ease, color 0.2s ease;

  &:hover {
    background: var(--aisk-primary-soft);
    color: var(--aisk-primary);

    .aisk-card-chevron {
      color: var(--aisk-primary);
    }
  }
}

/* ---------- Compact view header ---------- */

.aisk-view-header {
  display: flex;
  align-items: center;
  gap: 10px;
  min-height: 64px;
  padding: 12px 14px 12px 16px;
  background: linear-gradient(135deg, var(--aisk-primary), var(--aisk-primary-dark));
  color: var(--aisk-on-primary);
  flex-shrink: 0;

  > .aisk-icon-button:first-child {
    margin-left: -6px;
  }
}

.aisk-view-header-text {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.aisk-view-header-title {
  font-size: 16px;
  font-weight: 600;
  line-height: 1.3;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.aisk-view-header-subtitle {
  font-size: 12px;
  opacity: 0.8;
}

.aisk-avatar {
  position: relative;
  width: 38px;
  height: 38px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  border-radius: 50%;
  background: rgba(255, 255, 255, 0.95);
  color: var(--aisk-primary);
  font-size: 16px;
  font-weight: 700;

  img {
    width: 100%;
    height: 100%;
    padding: 5px;
    border-radius: 50%;
    object-fit: contain;
  }
}

.aisk-avatar-status {
  position: absolute;
  right: 0;
  bottom: 0;
  width: 10px;
  height: 10px;
  border: 2px solid #fff;
  border-radius: 50%;
  background: #22c55e;
}

.aisk-view-body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 16px 16px 104px;
}

.aisk-floating-action {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 84px;
  display: flex;
  justify-content: center;
  pointer-events: none;

  > * {
    pointer-events: auto;
  }
}

/* ---------- Chat view ---------- */

.support-buddy-messages {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 18px 16px 8px;
  background: $aisk-surface;
}

.support-buddy-message {
  display: block;
  margin-bottom: 14px;

  &.user {
    text-align: right;
  }

  .support-buddy-message-content {
    display: inline-block;
    max-width: 82%;
    padding: 10px 14px;
    border-radius: 18px;
    font-size: 14px;
    line-height: 1.5;
    word-wrap: break-word;
    text-align: left;

    p {
      margin-top: 0;
      margin-bottom: 10px;

      &:last-child {
        margin-bottom: 0;
      }
    }

    a {
      color: inherit;
      text-decoration: underline;
    }
  }

  &.user .support-buddy-message-content {
    border-bottom-right-radius: 6px;
    background: var(--aisk-primary);
    color: var(--aisk-on-primary);
  }

  &.bot .support-buddy-message-content {
    border: 1px solid $aisk-border;
    border-bottom-left-radius: 6px;
    background: #fff;
    color: $aisk-text;
  }

  .support-buddy-message-timestamp {
    display: block;
    margin: 4px 6px 0;
    font-size: 11px;
    color: #9ca3af;
  }

  &.user .support-buddy-message-timestamp {
    text-align: right;
  }

  &.bot .support-buddy-message-timestamp {
    text-align: left;
  }
}

.aisk-chat-composer {
  flex-shrink: 0;
  padding: 10px 14px 6px;
  background: $aisk-surface;
}

/* ---------- Bottom navigation ---------- */

.aisk-bottom-nav {
  position: absolute;
  left: 12px;
  right: 12px;
  bottom: $aisk-footer-height + 4px;
  z-index: 2;
  display: flex;
  justify-content: space-around;
  padding: 6px;
  border: 1px solid rgba(231, 232, 236, 0.9);
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.86);
  -webkit-backdrop-filter: blur(14px);
  backdrop-filter: blur(14px);
  box-shadow: 0 8px 24px rgba(17, 24, 39, 0.1);
}

.aisk-bottom-nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  margin: 0;
  padding: 6px 4px;
  border: none;
  border-radius: 12px;
  background: transparent;
  color: $aisk-muted;
  font-size: 12px;
  font-weight: 500;
  line-height: 1.2;
  cursor: pointer;
  transition: color 0.2s ease, background-color 0.2s ease;

  &:hover {
    color: $aisk-text;
  }

  &.is-active {
    color: var(--aisk-primary);
    font-weight: 600;
  }
}

/* ---------- Footer ---------- */

.support-buddy-footer {
  flex-shrink: 0;
  height: $aisk-footer-height;
  display: flex;
  align-items: center;
  justify-content: center;
  background: $aisk-surface;
}

.support-buddy-footer-text {
  font-size: 11px;
  color: #9ca3af;

  a {
    color: inherit;
    text-decoration: underline;
  }
}

/* ---------- Launcher (open state) ---------- */

.aisk-launcher-close {
  width: 56px;
  height: 56px;
  padding: 0;
  margin: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: var(--aisk-primary);
  color: var(--aisk-on-primary);
  cursor: pointer;
  box-shadow: 0 8px 24px var(--aisk-primary-ring), 0 2px 6px rgba(17, 24, 39, 0.15);
  animation: aiskPop 0.25s ease;
  transition: transform 0.2s ease;

  &:hover {
    transform: scale(1.06);
  }
}

@keyframes aiskPop {
  from {
    transform: scale(0.6) rotate(-90deg);
    opacity: 0;
  }

  to {
    transform: scale(1) rotate(0);
    opacity: 1;
  }
}

/* ---------- Animation ---------- */

.support-buddy-fade-in {
  animation: aiskSlideUp 0.28s cubic-bezier(0.2, 0.8, 0.2, 1);
  transform-origin: bottom right;
}

@keyframes aiskSlideUp {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.98);
  }

  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

/* ---------- Typing indicators ---------- */

.support-buddy-typing {
  display: flex;
  gap: 4px;
  width: fit-content;
  padding: 12px 16px;
  border-radius: 16px;
  border-bottom-left-radius: 4px;
  background: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.support-buddy-typing-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #d1d5db;
  animation: typing 1.4s infinite both;

  &:nth-child(2) {
    animation-delay: 0.2s;
  }

  &:nth-child(3) {
    animation-delay: 0.4s;
  }
}

@keyframes typing {
  0%,
  100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(-4px);
  }
}

.typing-indicator {
  display: flex;
  align-items: center;
  gap: 4px;
  padding: 6px 2px;

  span {
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #9ca3af;
    animation: typingBounce 1.2s infinite ease-in-out both;

    &:nth-child(2) {
      animation-delay: 0.15s;
    }

    &:nth-child(3) {
      animation-delay: 0.3s;
    }
  }
}

@keyframes typingBounce {
  0%,
  80%,
  100% {
    opacity: 0.4;
    transform: translateY(0);
  }

  40% {
    opacity: 1;
    transform: translateY(-4px);
  }
}

/* ---------- Products carousel ---------- */

.support-buddy-products {
  margin-top: 8px;
  padding: 8px;
  background: white;
  border-radius: 12px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.support-buddy-products-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.support-buddy-product-card {
  border: 1px solid #e5e7eb;
  border-radius: 8px;
  overflow: hidden;
}

.support-buddy-product-image {
  height: 128px;
  width: 100%;
  object-fit: cover;
}

.support-buddy-product-info {
  padding: 8px;
}

/* ---------- Email link styling ---------- */

.chat-email-link {
  color: #1976d2;
  text-decoration: none;
  border-bottom: 1px solid transparent;
  transition: all 0.2s ease;

  &:hover {
    color: #1565c0;
    border-bottom-color: #1565c0;
    text-decoration: none;
  }

  &:focus {
    outline: 2px solid #1976d2;
    outline-offset: 2px;
    border-radius: 2px;
  }
}

/* ---------- Mobile ---------- */

@media screen and (max-width: 450px) {
  .support-buddy-container,
  .support-buddy-container.is-expanded {
    position: fixed;
    inset: 0;
    width: 100%;
    height: 100%;
    height: 100dvh;
    margin: 0;
    border-radius: 0;
  }

  .aisk-expand-button {
    display: none;
  }

  .support-buddy-widget.is-open .aisk-launcher-close {
    display: none;
  }

  .aisk-composer textarea {
    font-size: 16px; // prevents iOS zoom on focus
  }
}

/* Orders tab: order-number lookup card */
.aisk-order-lookup {
  flex-direction: column;
  align-items: stretch;
  gap: 4px;

  .aisk-order-lookup-field {
    margin: 8px 0 0;
    color: $aisk-muted;
  }
}

.aisk-order-lookup-submit {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  margin: 0;
  padding: 0;
  border: none;
  border-radius: 8px;
  background: var(--aisk-primary);
  color: var(--aisk-on-primary);
  cursor: pointer;
  transition: opacity 0.2s ease;

  &:disabled {
    opacity: 0.4;
    cursor: not-allowed;
  }
}
