* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

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

:root {
  --background: #8c43bd;
  --chat-background: #f6e4fc;

  --primary: #4f46e5;
  --primary-hover: #4338ca;

  --text: #1f2937;
  --secondary-text: #6b7280;

  --border: #e5e7eb;

  --user-message: #4f46e5;
  --bot-message: #f1f3f5;
}

button,
input,
textarea,
select {
  font-family: inherit;
}

body {
  font-family: "Yekan Bakh", "Segoe UI", Tahoma, Arial, sans-serif;

  background: var(--background);

  min-height: 100vh;
  min-height: 100dvh;

  display: flex;
  justify-content: center;
  align-items: center;
}

/* =========================
   Chat Container
========================= */

.chat-container {
  width: 100%;
  max-width: 900px;

  height: 90vh;
  height: 90dvh;
  max-height: 800px;

  background: var(--chat-background);

  border-radius: 18px;

  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.08);

  display: flex;
  flex-direction: column;

  overflow: hidden;
}

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

.chat-header {
  height: 75px;
  flex: 0 0 75px;

  padding: 0 24px;

  border-bottom: 1px solid var(--border);

  display: flex;
  align-items: center;
  justify-content: space-between;
}

.bot-info {
  display: flex;
  align-items: center;
  gap: 12px;
}

.bot-avatar {
  width: 45px;
  height: 45px;

  border-radius: 50%;

  background-color: #eef2ff;
  background-image: url("assets/bot-avatar.png");
  background-position: center;
  background-size: cover;

  overflow: hidden;

  display: flex;
  align-items: center;
  justify-content: center;

  font-size: 0;
}

.bot-info h1 {
  font-size: 17px;
  color: var(--text);

  margin-bottom: 3px;
}

.status {
  font-size: 12px;
  color: #16a34a;

  display: flex;
  align-items: center;
  gap: 5px;
}

.status-dot {
  width: 7px;
  height: 7px;

  border-radius: 50%;

  background: #22c55e;
}

/* =========================
   Clear Button
========================= */

.clear-button {
  border: none;

  background: transparent;

  color: var(--secondary-text);

  cursor: pointer;

  padding: 8px 12px;

  border-radius: 8px;

  transition: 0.2s;
}

.clear-button:hover {
  background: #f3f4f6;
  color: var(--text);
}

/* =========================
   Messages
========================= */

.chat-messages {
  flex: 1;
  min-height: 0;

  padding: 25px;

  overflow-y: auto;

  display: flex;
  flex-direction: column;

  gap: 20px;

  scroll-behavior: smooth;
}

/*
    مهم:
    چون صفحه RTL است، برای اینکه
    جای پیام‌ها همیشه قابل پیش‌بینی باشد،
    از margin استفاده می‌کنیم.
*/

.message {
  display: flex;
  gap: 10px;
  max-width: 80%;
  direction: rtl;
}

/* Chatbot → LEFT */
.bot-message {
  align-self: flex-start;
  margin-right: auto;
  text-align: right;
  flex-direction: row;
  direction: rtl;
}

/* User → RIGHT */
.user-message {
  align-self: flex-end;
  margin-left: auto;
  flex-direction: row;
  direction: rtl;
  text-align: right;
}

/* =========================
   Avatar
========================= */

.message-avatar {
  width: 35px;
  height: 35px;

  flex-shrink: 0;

  border-radius: 50%;

  display: flex;
  align-items: center;
  justify-content: center;

  background: #eef2ff;

  font-size: 17px;
}

.bot-message .message-avatar {
  background-color: #eef2ff;
  background-image: url("assets/bot-avatar.png");
  background-position: center;
  background-size: cover;
  font-size: 0;
  overflow: hidden;
}

/* =========================
   Content
========================= */

.message-content {
  display: flex;

  flex-direction: column;

  gap: 5px;

  min-width: 0;
}

.bot-message .message-content {
  align-items: flex-start;
}

.user-message .message-content {
  align-items: flex-start;
}

.message-sender {
  font-size: 11px;

  color: var(--secondary-text);
}

/* =========================
   Bubble
========================= */

.message-bubble {
  padding: 11px 15px;

  border-radius: 14px;

  font-size: 14px;

  line-height: 1.7;

  word-break: break-word;

  overflow: hidden;
}

.bot-message .message-bubble {
  background: var(--bot-message);

  color: var(--text);

  border-top-left-radius: 4px;
}

.user-message .message-bubble {
  background: var(--user-message);

  color: white;

  border-top-right-radius: 4px;
}

/* =========================
   Images
========================= */

.message-image {
  display: block;

  max-width: 320px;
  max-height: 350px;

  border-radius: 10px;

  object-fit: contain;
}

/* =========================
   Videos
========================= */

.message-video {
  display: block;

  width: 320px;
  max-width: 100%;

  max-height: 350px;

  border-radius: 10px;

  background: #000;
}

/* =========================
   File Card
========================= */

.file-card {
  display: flex;

  align-items: center;

  gap: 10px;

  min-width: 220px;

  padding: 10px;

  border-radius: 10px;

  background: rgba(255, 255, 255, 0.15);
}

.bot-message .file-card {
  background: #ffffff;
}

.file-icon {
  font-size: 28px;
}

.file-info {
  display: flex;

  flex-direction: column;

  min-width: 0;
}

.file-name {
  font-size: 13px;

  font-weight: 600;

  overflow: hidden;

  text-overflow: ellipsis;

  white-space: nowrap;
}

.file-size {
  font-size: 10px;

  opacity: 0.7;

  margin-top: 2px;
}

/* =========================
   Input Area
========================= */

.chat-input-area {
  padding: 15px 20px 12px;

  border-top: 1px solid var(--border);

  background: var(--chat-background);

  flex-shrink: 0;
}

.input-container {
  display: flex;
  width: 100%;
  min-width: 0;
  direction: rtl;

  align-items: flex-end;

  gap: 8px;

  background: #f8f9fa;

  border: 1px solid var(--border);

  border-radius: 14px;

  padding: 8px;

  transition: 0.2s;
}

.input-container:focus-within {
  border-color: var(--primary);
}

/* =========================
   Textarea
========================= */

textarea {
  flex: 1;
  width: 100%;
  min-width: 0;
  order: 2;

  border: none;

  outline: none;

  resize: none;

  background: transparent;

  font-family: inherit;

  font-size: 14px;

  line-height: 1.5;

  max-height: 120px;

  padding: 8px 4px;
}

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

/* =========================
   Attach Button
========================= */

.attach-button {
  order: 3;

  width: 42px;
  height: 42px;

  border: none;

  border-radius: 10px;

  background: transparent;

  cursor: pointer;

  font-size: 20px;

  flex-shrink: 0;

  transition: 0.2s;
}

.attach-button:hover {
  background: #e5e7eb;
}

/* =========================
   Send Button
========================= */

.send-button {
  order: 1;

  width: 42px;
  height: 42px;

  border: none;

  border-radius: 10px;

  background: var(--primary);

  color: white;

  cursor: pointer;

  display: flex;

  align-items: center;
  justify-content: center;

  font-size: 17px;

  transition: 0.2s;

  flex-shrink: 0;
}

.send-button:hover {
  background: var(--primary-hover);

  transform: translateY(-1px);
}

.send-button:disabled {
  background: #c7c9d9;

  cursor: not-allowed;

  transform: none;
}

/* =========================
   Selected Files
========================= */

.file-preview {
  display: flex;

  flex-wrap: wrap;

  gap: 8px;

  margin-top: 8px;
}

.file-preview:empty {
  display: none;
}

.preview-item {
  display: flex;

  align-items: center;

  gap: 7px;

  padding: 6px 9px;

  background: #f1f3f5;

  border: 1px solid var(--border);

  border-radius: 8px;

  max-width: 250px;

  font-size: 12px;
}

.preview-name {
  overflow: hidden;

  text-overflow: ellipsis;

  white-space: nowrap;
}

.remove-file {
  border: none;

  background: transparent;

  color: #6b7280;

  cursor: pointer;

  font-size: 14px;

  padding: 0 3px;
}

.remove-file:hover {
  color: #ef4444;
}

/* =========================
   Disclaimer
========================= */

.disclaimer {
  text-align: center;

  font-size: 10px;

  color: #9ca3af;

  margin-top: 8px;
}

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

.typing-indicator {
  display: flex;

  gap: 4px;

  padding: 5px 2px;
}

.typing-indicator span {
  width: 6px;
  height: 6px;

  background: #9ca3af;

  border-radius: 50%;

  animation: typing 1.2s infinite;
}

.typing-indicator span:nth-child(2) {
  animation-delay: 0.15s;
}

.typing-indicator span:nth-child(3) {
  animation-delay: 0.3s;
}

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

  30% {
    opacity: 1;
    transform: translateY(-3px);
  }
}

/* =========================
   Mobile
========================= */

@media (max-width: 768px) {
  html,
  body {
    position: fixed;
    inset: 0;
    max-width: 100%;
    overscroll-behavior: none;
  }

  body {
    background: white;
    height: var(--app-height, 100dvh);
    min-height: 0;
    align-items: stretch;
  }

  .chat-container {
    position: fixed;
    top: var(--app-top, 0px);
    right: 0;
    left: 0;

    width: auto;
    max-width: 100%;
    height: var(--app-height, 100dvh);
    min-height: 0;
    max-height: none;

    border-radius: 0;
  }

  .chat-header {
    padding-right: max(15px, env(safe-area-inset-right));
    padding-left: max(15px, env(safe-area-inset-left));
  }

  .chat-messages {
    padding-right: max(14px, env(safe-area-inset-right));
    padding-left: max(14px, env(safe-area-inset-left));
  }

  .chat-input-area {
    padding-top: 10px;
    padding-right: max(10px, env(safe-area-inset-right));
    padding-bottom: max(10px, env(safe-area-inset-bottom));
    padding-left: max(10px, env(safe-area-inset-left));
    max-height: 45%;
    overflow-y: auto;
    overscroll-behavior: contain;
  }

  .input-container {
    gap: 4px;
    padding: 6px;
    max-width: 100%;
  }

  textarea {
    font-size: 16px;
  }

  .attach-button,
  .record-button,
  .send-button {
    width: 40px;
    height: 40px;
  }

  .message {
    max-width: 90%;
    min-width: 0;
  }

  .message-content,
  .message-bubble {
    max-width: 100%;
  }

  .message-bubble {
    overflow-wrap: anywhere;
  }

  .file-card,
  .audio-message {
    width: 100%;
    min-width: 0;
  }

  .message-image {
    max-width: 240px;
  }

  .message-video {
    width: 240px;
  }

  .clear-button {
    font-size: 12px;
  }
}

/* =========================
   Voice Record Button
========================= */

.record-button {
  display: flex;
  order: 2;

  width: 42px;
  height: 42px;

  border: none;

  border-radius: 10px;

  background: transparent;

  cursor: pointer;

  font-size: 19px;

  align-items: center;
  justify-content: center;

  flex-shrink: 0;

  transition: 0.2s;
}

.record-button:hover {
  background: #e5e7eb;
}

/* Recording */

.record-button.recording {
  background: #fee2e2;

  color: #dc2626;

  animation: recordingPulse 1.2s infinite;
}

@keyframes recordingPulse {
  0% {
    transform: scale(1);
  }

  50% {
    transform: scale(1.08);
  }

  100% {
    transform: scale(1);
  }
}

/* Recording status */

.recording-status {
  display: none;

  align-items: center;

  justify-content: center;

  gap: 7px;

  margin-top: 8px;

  font-size: 12px;

  color: #dc2626;
}

.recording-status.active {
  display: flex;
}

.recording-dot {
  width: 8px;
  height: 8px;

  border-radius: 50%;

  background: #ef4444;

  animation: recordingDot 1s infinite;
}

@keyframes recordingDot {
  0% {
    opacity: 1;
  }

  50% {
    opacity: 0.3;
  }

  100% {
    opacity: 1;
  }
}

/* =========================
   Audio Message
========================= */

.audio-message {
  display: flex;

  align-items: center;

  gap: 8px;

  min-width: 230px;
}

.audio-message audio {
  width: 220px;

  max-width: 100%;
}

.voice-transcript {
  margin-top: 8px;

  font-size: 13px;

  line-height: 1.6;

  white-space: pre-line;
}

.voice-transcript.pending,
.voice-transcript.error {
  opacity: 0.8;

  font-size: 12px;
}

.grammar-corrections {
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0, 0, 0, 0.08);
}

.grammar-corrections-title {
  font-size: 12px;
  font-weight: 600;
  margin-bottom: 6px;
  opacity: 0.85;
}

.grammar-correction-item + .grammar-correction-item {
  margin-top: 8px;
}

.grammar-correction-change {
  direction: ltr;
  text-align: left;
  unicode-bidi: isolate;
  font-size: 13px;
  font-weight: 600;
}

.grammar-correction-feedback {
  margin-top: 2px;
  font-size: 12px;
  opacity: 0.85;
  direction: rtl;
  text-align: right;
}

.learning-response {
  display: flex;
  flex-direction: column;
  gap: 8px;
  white-space: normal;
}

.learning-response .persian-text {
  direction: rtl;
  text-align: right;
  white-space: pre-line;
}

.learning-response .english-text {
  direction: ltr;
  text-align: left;
  unicode-bidi: isolate;
  white-space: pre-line;
}

/* =========================
   Voice Preview
========================= */

.voice-preview {
  display: none;

  align-items: center;

  gap: 8px;

  margin-top: 10px;

  padding: 8px 10px;

  background: #f8f9fa;

  border: 1px solid var(--border);

  border-radius: 12px;
}

.voice-preview.active {
  display: flex;
}

.voice-preview audio {
  flex: 1;

  min-width: 0;

  height: 38px;
}

/* =========================
   Voice Delete
========================= */

.voice-delete-button {
  width: 38px;
  height: 38px;

  border: none;

  border-radius: 9px;

  background: #fee2e2;

  color: #dc2626;

  cursor: pointer;

  font-size: 17px;

  flex-shrink: 0;
}

.voice-delete-button:hover {
  background: #fecaca;
}

@media (max-width: 768px) {
  .record-button {
    width: 40px;
    height: 40px;
  }
}
