body {
  margin: 0;
  font-family: "Segoe UI", sans-serif;
  background: url("https://64.media.tumblr.com/fc6a0e49c8d330035210f4e01af403c5/4515e378235d2f32-08/s1280x1920/f2ec3522b3f8cad1ade5682a84eb885386dc4636.jpg")
    no-repeat center center fixed;
  background-size: cover;
  /* background: linear-gradient(135deg, #2b1544, #4a1b6a, #5a2c89); */
  color: #fff;
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100vh;
  overflow: hidden;
}

.app-container {
  display: flex;
  width: 95%;
  max-width: 1200px;
  height: 90vh;
  background: rgba(30, 10, 50, 0.85);
  border: 2px solid #9b5de5;
  border-radius: 20px;
  box-shadow: 0 0 20px #c77dff;
  overflow: hidden;
}

.sidebar {
  width: 250px; /* Fixed width for desktop */
  flex-shrink: 0; /* Prevent sidebar from shrinking on desktop */
  background: rgba(20, 5, 35, 0.9);
  border-right: 1px solid #9b5de5;
  padding: 1rem;
  display: flex;
  flex-direction: column;
  overflow-y: auto;
}

.sidebar-header {
  text-align: center;
  margin-bottom: 1rem;
  padding-bottom: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
}

.sidebar-header h2 {
  margin: 0;
  font-size: 1.3rem;
  color: #fff;
}

.session-list {
  list-style: none;
  padding: 0;
  margin: 0;
  flex-grow: 1;
}

.session-item {
  padding: 10px 15px;
  margin-bottom: 8px;
  background-color: rgba(255, 255, 255, 0.08);
  border-radius: 8px;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s ease;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 0.9rem;
  word-break: break-word;
}

.session-item:hover {
  background-color: rgba(255, 255, 255, 0.15);
  transform: translateY(-2px);
}

.session-item.active {
  background-color: #9d4edd;
  box-shadow: 0 0 10px rgba(157, 78, 221, 0.5);
  font-weight: bold;
}

.session-item .delete-session-button {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.6);
  font-size: 1.1rem;
  cursor: pointer;
  margin-left: 10px;
  padding: 5px;
  border-radius: 50%;
  transition: color 0.2s ease, background-color 0.2s ease;
}

.session-item .delete-session-button:hover {
  color: #ff6b6b;
  background-color: rgba(255, 255, 255, 0.1);
}

.new-chat-button {
  width: 100%;
  padding: 12px;
  background: #7b2cbf;
  color: white;
  border: none;
  border-radius: 10px;
  cursor: pointer;
  font-size: 1rem;
  margin-top: 1rem;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.new-chat-button:hover {
  background: #9d4edd;
  transform: translateY(-2px);
}

.chat-container {
  flex-grow: 1; /* Allow chat container to take remaining width on desktop */
  display: flex;
  flex-direction: column;
  padding: 1rem;
}

.chat-header {
  text-align: center;
  margin-bottom: 1rem;
  flex-shrink: 0; /* Prevent header from shrinking */
}

.chat-header img {
  width: 80px;
  height: 80px;
  border-radius: 50%;
  border: 3px solid #ffffff44;
}

.chat-header h1 {
  margin: 0.5rem 0 0;
  font-size: 1.5rem;
  color: #fff;
}

.chat-header p {
  font-size: 0.9rem;
  color: #ddd;
}

.session-info {
  margin-top: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  font-size: 0.85rem;
  color: #bbb;
}

#current-session-display {
  background-color: rgba(255, 255, 255, 0.1);
  padding: 5px 10px;
  border-radius: 5px;
  font-family: monospace;
  color: #fff;
}

.copy-button {
  background: #7b2cbf;
  color: white;
  border: none;
  border-radius: 5px;
  padding: 5px 8px;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s ease;
  display: flex;
  align-items: center;
  gap: 5px;
  position: relative;
}

.copy-button:hover {
  background: #9d4edd;
  transform: translateY(-1px);
}

.copy-button svg {
  fill: currentColor;
}

.copy-feedback {
  position: absolute;
  top: -25px;
  left: 50%;
  transform: translateX(-50%);
  background-color: rgba(0, 0, 0, 0.8);
  color: #fff;
  padding: 4px 8px;
  border-radius: 4px;
  font-size: 0.75rem;
  white-space: nowrap;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
  pointer-events: none;
}

.copy-feedback.show {
  opacity: 1;
}

.chat-box {
  background: rgba(255, 255, 255, 0.05);
  border-radius: 10px;
  flex-grow: 1; /* Allows chat box to take available vertical space */
  overflow-y: auto;
  padding: 10px;
  font-size: 0.95rem;
  margin-bottom: 10px;
  display: flex;
  flex-direction: column;
  min-height: 0; /* Crucial for flex item inside column flex container */
}

.chat-box .user,
.chat-box .bot {
  margin-bottom: 8px;
  padding: 8px 10px;
  border-radius: 10px;
  max-width: 80%;
  line-height: 1.5;
  position: relative; /* เพิ่มเพื่อให้ปุ่ม regenerate วางตำแหน่งได้ */
}

.chat-box .user {
  background-color: #9d4edd;
  margin-left: auto;
  text-align: right;
}

.chat-box .bot {
  background-color: #5a189a;
  margin-right: auto;
  text-align: left;
  display: flex; /* ใช้ flexbox เพื่อจัดวางข้อความและปุ่ม */
  flex-direction: column; /* วางข้อความและปุ่มในแนวตั้ง */
}

.chat-box .bot .message-text {
  margin-bottom: 5px; /* ระยะห่างระหว่างข้อความกับปุ่ม */
}

.bot-message-controls {
  display: flex;
  justify-content: flex-end; /* จัดปุ่มไปทางขวา */
  width: 100%;
}

.regenerate-msg-btn {
  background: none;
  border: none;
  color: rgba(255, 255, 255, 0.7);
  font-size: 0.9rem; /* ขนาดไอคอน */
  cursor: pointer;
  padding: 3px 5px;
  border-radius: 5px;
  transition: color 0.2s ease, background-color 0.2s ease;
  display: flex;
  align-items: center;
  gap: 5px;
}

.regenerate-msg-btn:hover {
  color: #fff;
  background-color: rgba(255, 255, 255, 0.15);
}

.chat-input {
  display: flex;
  gap: 10px;
  padding-top: 10px;
  flex-shrink: 0; /* Prevent input from shrinking */
}

.chat-input input {
  flex-grow: 1;
  padding: 8px 12px;
  border-radius: 10px;
  border: none;
  font-size: 1rem;
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
}

.chat-input input::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.chat-input button {
  background: #7b2cbf;
  color: white;
  border: none;
  border-radius: 10px;
  padding: 8px 16px;
  cursor: pointer;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.chat-input button:hover {
  background: #9d4edd;
  transform: translateY(-2px);
}

/* Modal Styles */
.modal-overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.7);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 1000;
}

.modal-content {
  background: rgba(30, 10, 50, 0.95);
  padding: 30px;
  border-radius: 15px;
  box-shadow: 0 0 25px #c77dff;
  text-align: center;
  width: 90%;
  max-width: 400px;
  position: relative;
  border: 2px solid #9b5de5;
}

.modal-content h3 {
  margin-top: 0;
  color: #fff;
  font-size: 1.4rem;
  margin-bottom: 20px;
}

.modal-button {
  width: 100%;
  padding: 12px;
  margin-bottom: 10px;
  background: #7b2cbf;
  color: white;
  border: none;
  border-radius: 8px;
  cursor: pointer;
  font-size: 1rem;
  transition: background-color 0.2s ease, transform 0.1s ease;
}

.modal-button:hover {
  background: #9d4edd;
  transform: translateY(-2px);
}

.modal-button.confirm-yes {
  background-color: #28a745;
}

.modal-button.confirm-yes:hover {
  background-color: #218838;
}

.modal-button.confirm-no {
  background-color: #dc3545;
}

.modal-button.confirm-no:hover {
  background-color: #c82333;
}

#enterSessionContainer {
  margin-top: 20px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

#sessionIdInput {
  padding: 10px;
  border-radius: 8px;
  border: 1px solid #9b5de5;
  background-color: rgba(255, 255, 255, 0.1);
  color: #fff;
  width: calc(100% - 22px);
  font-size: 1rem;
}

#sessionIdInput::placeholder {
  color: rgba(255, 255, 255, 0.6);
}

.error-message {
  color: #ff6b6b;
  font-size: 0.9rem;
  margin-top: 5px;
}

.modal-close-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background: none;
  border: none;
  font-size: 1.8rem;
  color: rgba(255, 255, 255, 0.7);
  cursor: pointer;
  transition: color 0.2s ease;
}

.modal-close-button:hover {
  color: #fff;
}

.hidden {
  display: none !important;
}

.modal-buttons {
  display: flex;
  gap: 10px;
  margin-top: 20px;
}

.modal-buttons .modal-button {
  width: 50%;
  margin-bottom: 0;
}

/* --- Mobile Responsiveness --- */
@media (max-width: 768px) {
  .app-container {
    flex-direction: column; /* Stack sidebar and chat vertically */
    height: 100vh; /* Take full viewport height on mobile */
    width: 100vw; /* Take full viewport width on mobile */
    border-radius: 0; /* Remove border radius for full screen effect */
    padding: 0;
  }

  .sidebar {
    width: 100%; /* Sidebar takes full width */
    height: auto; /* Allow height to adjust to content */
    max-height: 120px; /* Cap sidebar height */
    border-right: none;
    border-bottom: 1px solid #9b5de5; /* Add border to bottom */
    flex-direction: row; /* Layout sidebar content horizontally */
    flex-wrap: nowrap;
    justify-content: space-between;
    align-items: center;
    padding: 0.5rem 1rem;
    overflow-x: auto; /* Enable horizontal scrolling for sessions */
    -webkit-overflow-scrolling: touch;
    flex-shrink: 0; /* Ensure sidebar doesn't shrink vertically */
  }

  .sidebar-header {
    width: auto;
    margin-bottom: 0;
    padding-bottom: 0;
    border-bottom: none;
    display: none; /* Hide header on mobile for more space */
  }

  .session-list {
    display: flex;
    flex-wrap: nowrap;
    overflow-x: auto;
    padding: 0 5px;
    flex-grow: 1;
    white-space: nowrap;
    margin-right: 10px;
  }

  .session-item {
    flex-shrink: 0;
    margin: 5px;
    padding: 8px 12px;
    font-size: 0.85rem;
  }

  .session-item .delete-session-button {
    font-size: 1rem;
    padding: 3px;
  }

  .new-chat-button {
    flex-shrink: 0;
    width: auto;
    margin: 5px 0;
    padding: 8px 12px;
    font-size: 0.9rem;
  }

  .chat-container {
    flex-grow: 1; /* Allow chat container to take all remaining vertical space */
    padding: 0.8rem;
    min-height: 0; /* Crucial: Allows flex item to shrink if needed */
  }

  .chat-header {
    margin-bottom: 0.8rem;
    flex-shrink: 0; /* Prevent header from shrinking */
  }

  .chat-header img {
    width: 60px;
    height: 60px;
  }

  .chat-header h1 {
    font-size: 1.3rem;
  }

  .chat-header p {
    font-size: 0.8rem;
  }

  .session-info {
    flex-direction: column;
    gap: 5px;
    font-size: 0.8rem;
  }

  .copy-button {
    padding: 4px 6px;
    font-size: 0.8rem;
  }

  .chat-box {
    padding: 8px;
    font-size: 0.9rem;
    flex-grow: 1; /* Ensures chat box expands within chat container */
    overflow-y: auto;
    min-height: 0; /* Allows chat box to scroll independently */
  }

  .chat-box .user,
  .chat-box .bot {
    max-width: 90%;
    padding: 7px 9px;
  }

  .chat-input {
    flex-direction: row;
    gap: 8px;
    flex-shrink: 0; /* Prevent chat input from shrinking */
    padding-top: 0.5rem;
  }

  .chat-input input {
    padding: 10px;
    font-size: 0.95rem;
  }

  .chat-input button {
    padding: 10px 14px;
    font-size: 0.95rem;
  }

  .modal-content {
    padding: 20px;
    border-radius: 10px;
  }

  .modal-content h3 {
    font-size: 1.2rem;
    margin-bottom: 15px;
  }

  .modal-button {
    padding: 10px;
    font-size: 0.9rem;
  }

  #sessionIdInput {
    padding: 8px;
    font-size: 0.9rem;
  }

  .modal-close-button {
    font-size: 1.5rem;
  }

  .modal-buttons {
    flex-direction: column;
    gap: 8px;
  }

  .modal-buttons .modal-button {
    width: 100%;
    margin-bottom: 0;
  }
}

/* Extra small screens (e.g., iPhone 5/SE) */
@media (max-width: 480px) {
  .app-container {
    height: 100vh;
    width: 100vw;
    border-radius: 0;
    padding: 0;
  }

  .sidebar {
    max-height: 90px; /* Adjusted slightly more */
    padding: 0.4rem;
  }

  .chat-container {
    padding: 0.5rem; /* Slightly less padding */
  }

  .session-item {
    font-size: 0.75rem; /* Even smaller font */
    padding: 5px 8px;
  }

  .new-chat-button {
    font-size: 0.75rem; /* Even smaller font */
    padding: 5px 8px;
  }

  .chat-header img {
    width: 45px;
    height: 45px;
  }

  .chat-header h1 {
    font-size: 1.1rem;
  }

  .chat-header p {
    font-size: 0.7rem;
  }

  .copy-button {
    font-size: 0.7rem;
    padding: 2px 4px;
  }

  .chat-input input,
  .chat-input button {
    padding: 7px 10px;
    font-size: 0.85rem;
  }

  .modal-content {
    width: 98%; /* Slightly wider for XS */
    padding: 12px;
  }
  .modal-content h3 {
    font-size: 1.1rem;
  }
  .modal-button {
    font-size: 0.85rem;
    padding: 8px;
  }
  #sessionIdInput {
    padding: 7px;
    font-size: 0.85rem;
  }
}

/* Define custom properties for easier calculation */
:root {
  --sidebar-height-mobile: 120px;
  --sidebar-height-mobile-xs: 90px;
}

/* Fallback for browsers that don't support custom properties */
@supports not (--custom-property: value) {
  @media (max-width: 768px) {
    .chat-container {
      height: calc(100% - 120px);
    }
  }
  @media (max-width: 480px) {
    .chat-container {
      height: calc(100% - 90px);
    }
  }
}

#user-input {
  width: 100%;
  resize: none;
  padding: 8px;
  font-size: 16px;
  border-radius: 6px;
  border: 1px solid #ccc;
  box-sizing: border-box;
  overflow-y: hidden;
}

textarea#user-input {
  resize: none;
  overflow: hidden;
  min-height: 2.5rem;
  max-height: 7.5rem; /* ประมาณ 3 บรรทัด */
  line-height: 1.5rem;
}
