@import url('https://fonts.googleapis.com/css2?family=DM+Sans:ital,opsz,wght@0,9..40,300;0,9..40,500;0,9..40,700;1,9..40,400&display=swap');

:root {
  --primary: #0055ff;
  --primary-dark: #003ecb;
  --bg: #efeae2;
  --panel: #ffffff;
  --panel-soft: #f7f7f7;
  --text: #111827;
  --text-muted: #6b7280;
  --border: #e5e7eb;
  --user-bubble: #dcf8c6;
  --ai-bubble: #ffffff;
  --green: #25d366;
  --whatsapp-dark: #111b21;
  --whatsapp-icon: #54656f;
  --tick-blue: #53bdeb;
  --danger: #dc3545;
  --shadow-sm: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-md: 0 8px 24px rgba(0,0,0,0.12);
  --radius: 18px;
  --transition: 0.2s ease;
}

* {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
  overflow: hidden;
}

body {
  font-family: 'DM Sans', -apple-system, BlinkMacSystemFont, sans-serif;
  color: var(--text);
  background: var(--bg);
}

#ai2-chat-app {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: flex;
  background: var(--bg);
  z-index: 9999;
}

/* ===== REVISED SIDEBAR LOGIC ===== */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  height: 100%;
  height: 100dvh;
  width: 300px;
  min-width: 300px;
  background: #f7f8fa;
  border-right: 1px solid var(--border);
  display: flex;
  flex-direction: column;
  z-index: 60;
  transform: translateX(-100%);
  transition: transform 0.28s ease;
  box-shadow: var(--shadow-md);
}

.sidebar.open {
  transform: translateX(0);
}

.sidebar.desktop {
  position: relative;
  top: auto;
  left: auto;
  bottom: auto;
  height: auto;
  width: 300px;
  min-width: 300px;
  transform: none;
  box-shadow: none;
  z-index: 30;
}

.sidebar.desktop.open {
  transform: none;
}

.sidebar-header {
  padding: 14px;
  border-bottom: 1px solid var(--border);
  background: #fff;
  flex-shrink: 0;
}

.new-chat-btn {
  width: 100%;
  border: none;
  border-radius: 12px;
  padding: 12px 14px;
  font: inherit;
  font-weight: 700;
  font-size: 14px;
  background: var(--primary);
  color: #fff;
  cursor: pointer;
  transition: background var(--transition);
}

.new-chat-btn:hover {
  background: var(--primary-dark);
}

.sidebar-conversations {
  flex: 1;
  overflow-y: auto;
  padding: 10px;
  -webkit-overflow-scrolling: touch;
}

.conversation-group {
  margin-bottom: 14px;
}

.group-title {
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 4px 10px 8px;
}

.conversation-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 11px 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: background var(--transition);
  margin-bottom: 4px;
  background: transparent;
}

.conversation-item:hover {
  background: rgba(0,0,0,0.04);
}

.conversation-item.active {
  background: #e9f1ff;
}

.conversation-title {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.conv-delete-btn {
  background: none;
  border: none;
  color: #999;
  cursor: pointer;
  font-size: 16px;
  opacity: 0;
  transition: opacity var(--transition), color var(--transition);
}

.conversation-item:hover .conv-delete-btn,
.conversation-item.active .conv-delete-btn {
  opacity: 1;
}

.conv-delete-btn:hover {
  color: var(--danger);
}

.sidebar-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.34);
  z-index: 59;
  display: none;
}

.sidebar-overlay.show {
  display: block;
}

.menu-overlay,
.sheet-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.34);
  display: none;
}

.menu-overlay {
  z-index: 39;
}

.sheet-overlay {
  z-index: 99998;
}

.menu-overlay.show,
.sheet-overlay.show {
  display: block;
}

.main-content {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  position: relative;
  overflow: hidden;
  background:
    radial-gradient(circle at 25px 25px, rgba(0,0,0,0.02) 2px, transparent 2px) 0 0 / 26px 26px,
    var(--bg);
}

.wa-topbar {
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 10px 14px;
  background: #f7f7f7;
  border-bottom: 1px solid rgba(0,0,0,0.05);
  position: relative;
}

.wa-topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.icon-btn {
  width: 38px;
  height: 38px;
  border: none;
  background: transparent;
  border-radius: 999px;
  cursor: pointer;
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: background var(--transition);
  flex-shrink: 0;
  font-size: 22px;
}

.icon-btn:hover {
  background: rgba(0,0,0,0.06);
}

.app-avatar {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  object-fit: cover;
  flex-shrink: 0;
  border: 1px solid rgba(0,0,0,0.06);
}

.wa-title-wrap {
  min-width: 0;
}

.wa-title {
  font-size: 18px;
  font-weight: 700;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.wa-subline {
  margin-top: 3px;
  display: flex;
  align-items: center;
  gap: 8px;
  min-height: 20px;
}

.status-badge {
  display: inline-flex;
  align-items: center;
  padding: 3px 9px;
  border-radius: 999px;
  background: #d7f0cf;
  color: #3d7f4a;
  font-size: 11px;
  font-weight: 700;
  line-height: 1;
}

.sub-model {
  font-size: 12px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 220px;
}

.wa-topbar-right {
  display: flex;
  align-items: center;
  gap: 2px;
  flex-shrink: 0;
}

.overflow-menu {
  position: absolute;
  top: 58px;
  right: 10px;
  width: min(320px, calc(100vw - 20px));
  background: #fff;
  border-radius: 18px;
  box-shadow: var(--shadow-md);
  z-index: 40;
  display: none;
  overflow: hidden;
}

.overflow-menu.open {
  display: block;
}

.menu-section {
  padding: 14px 14px 8px;
  border-bottom: 1px solid var(--border);
}

.menu-section:last-child {
  border-bottom: none;
}

.menu-title {
  font-size: 13px;
  font-weight: 700;
  color: var(--text-muted);
  margin-bottom: 10px;
}

.menu-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.menu-item {
  width: 100%;
  border: none;
  background: #fff;
  border-radius: 12px;
  padding: 12px 12px;
  font: inherit;
  color: var(--text);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  transition: background var(--transition);
  text-align: left;
}

.menu-item:hover {
  background: #f5f7fa;
}

.menu-item.active {
  background: #edf7ea;
}

.menu-item.disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.menu-item-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.menu-item-icon {
  width: 24px;
  height: 24px;
  border-radius: 8px;
  background: #eef2f7;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 13px;
  flex-shrink: 0;
}

.usage-card {
  background: #fafafa;
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 12px;
  line-height: 1.5;
  font-size: 14px;
}

.usage-card strong {
  font-weight: 700;
}

.chat-messages {
  flex: 1;
  overflow-y: auto;
  padding: 14px 12px 18px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  -webkit-overflow-scrolling: touch;
}

.date-divider {
  align-self: center;
  padding: 6px 12px;
  border-radius: 999px;
  background: rgba(255,255,255,0.92);
  box-shadow: var(--shadow-sm);
  font-size: 12px;
  color: #555;
  margin: 4px 0 6px;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: min(82%, 760px);
  position: relative;
  animation: fadeInMsg 0.22s ease;
}

@keyframes fadeInMsg {
  from {
    opacity: 0;
    transform: translateY(5px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.message-user {
  align-self: flex-end;
  align-items: flex-end;
}

.message-ai {
  align-self: flex-start;
  align-items: flex-start;
}

.message-bubble {
  position: relative;
  padding: 12px 14px 10px;
  border-radius: 14px;
  box-shadow: var(--shadow-sm);
  line-height: 1.55;
  font-size: 15px;
  word-wrap: break-word;
  white-space: pre-wrap;
}

.message-user .message-bubble {
  background: var(--user-bubble);
  border-top-right-radius: 4px;
}

.message-ai .message-bubble {
  background: var(--ai-bubble);
  border-top-left-radius: 4px;
}

.message-bubble p {
  margin: 0 0 10px 0;
}

.message-bubble p:last-child {
  margin-bottom: 0;
}

.message-bubble table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
  font-size: 14px;
}

.message-bubble th,
.message-bubble td {
  border: 1px solid #ddd;
  padding: 8px 10px;
  text-align: left;
}

.message-bubble th {
  background: #f4f4f4;
  font-weight: 700;
}

.message-bubble tr:nth-child(even) {
  background: #fafafa;
}

.message-bubble code {
  background: #f0f2f5;
  padding: 2px 6px;
  border-radius: 4px;
  font-size: 13px;
}

.message-bubble pre {
  background: #1f2937;
  color: #f3f4f6;
  padding: 14px;
  border-radius: 10px;
  overflow-x: auto;
  margin: 10px 0;
}

.message-bubble pre code {
  background: transparent;
  padding: 0;
  color: inherit;
}

.message-bubble ul,
.message-bubble ol {
  padding-left: 20px;
  margin: 8px 0;
}

.message-bubble a {
  color: var(--primary);
  text-decoration: underline;
}

.message-bubble .table-wrapper {
  position: relative;
  margin-top: 28px;
}

.copy-table-btn {
  position: absolute;
  top: -32px;
  right: 0;
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 4px 10px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.msg-copy-btn {
  position: absolute;
  top: 8px;
  right: 8px;
  border: none;
  background: transparent;
  border-radius: 6px;
  padding: 5px;
  cursor: pointer;
  color: #8b8b8b;
  opacity: 0;
  transition: opacity var(--transition), background var(--transition);
}

.message:hover .msg-copy-btn {
  opacity: 0.65;
}

.msg-copy-btn:hover {
  opacity: 1 !important;
  background: rgba(0,0,0,0.05);
}

.message-footer {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 5px;
  padding: 0 4px;
  font-size: 12px;
  color: var(--text-muted);
  line-height: 1;
}

.message-model,
.message-time {
  white-space: nowrap;
}

.message-status {
  display: inline-flex;
  align-items: center;
  gap: 0;
  color: var(--tick-blue);
  margin-left: -2px;
}

.message-status svg {
  display: block;
}

.message-attachment {
  margin-top: 8px;
  max-width: 290px;
}

.message-attachment img {
  max-width: 100%;
  border-radius: 10px;
  cursor: pointer;
  border: 1px solid rgba(0,0,0,0.06);
}

.message-attachment .file-info {
  display: flex;
  align-items: center;
  gap: 8px;
  background: rgba(0,0,0,0.04);
  padding: 9px 10px;
  border-radius: 10px;
  font-size: 13px;
}

.generated-image-container {
  margin-top: 10px;
  max-width: 400px;
}

.generated-image-container img {
  max-width: 100%;
  border-radius: 12px;
  border: 1px solid var(--border);
  cursor: pointer;
}

.image-actions {
  display: flex;
  gap: 6px;
  margin-top: 8px;
}

.image-action-btn {
  border: 1px solid var(--border);
  background: #fff;
  border-radius: 8px;
  padding: 6px 10px;
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-muted);
  padding: 40px 20px;
  font-size: 15px;
}

.empty-state-icon {
  font-size: 42px;
  margin-bottom: 12px;
}

.chat-input-area {
  flex-shrink: 0;
  padding: 10px 10px 12px;
  background: #f7f7f7;
  border-top: 1px solid rgba(0,0,0,0.05);
}

.file-preview {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 8px;
}

.file-preview-item {
  position: relative;
}

.file-preview-item img {
  width: 56px;
  height: 56px;
  object-fit: cover;
  border-radius: 10px;
  border: 2px solid var(--border);
}

.file-preview-item .file-icon {
  width: 56px;
  height: 56px;
  border-radius: 10px;
  border: 2px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  background: #fff;
  font-size: 13px;
  font-weight: 700;
}

.remove-file {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  border: none;
  border-radius: 50%;
  background: var(--danger);
  color: #fff;
  cursor: pointer;
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.input-shell {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}

.input-pill {
  flex: 1;
  display: flex;
  align-items: flex-end;
  gap: 4px;
  background: #fff;
  border-radius: 28px;
  padding: 6px 8px 6px 8px;
  border: 1px solid rgba(0,0,0,0.08);
  box-shadow: var(--shadow-sm);
  min-width: 0;
}

.wa-mini-btn {
  width: 34px;
  height: 34px;
  border: none;
  background: transparent;
  border-radius: 999px;
  cursor: pointer;
  color: var(--whatsapp-icon);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
  padding: 0;
}

.wa-mini-btn:hover {
  background: rgba(0,0,0,0.05);
}

.wa-mini-btn svg {
  width: 24px;
  height: 24px;
  stroke: currentColor;
  fill: none;
  stroke-width: 1.9;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.chat-input {
  flex: 1;
  border: none;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: 15px;
  line-height: 1.45;
  padding: 8px 2px 8px 2px;
  max-height: 120px;
  min-height: 40px;
  outline: none;
  color: var(--text);
}

.send-btn,
.voice-btn {
  width: 56px;
  height: 56px;
  border: none;
  border-radius: 50%;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  flex-shrink: 0;
  color: #fff;
  background: var(--whatsapp-dark);
  box-shadow: var(--shadow-sm);
  transition: transform var(--transition), opacity var(--transition), background var(--transition);
  padding: 0;
}

.send-btn:hover,
.voice-btn:hover {
  transform: scale(1.03);
}

.send-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none;
}

.send-btn svg,
.voice-btn svg {
  width: 26px;
  height: 26px;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.voice-btn.recording {
  background: #d9485f;
  animation: pulseMic 1.1s infinite;
}

@keyframes pulseMic {
  0% {
    box-shadow: 0 0 0 0 rgba(217,72,95,0.35);
  }
  70% {
    box-shadow: 0 0 0 12px rgba(217,72,95,0);
  }
  100% {
    box-shadow: 0 0 0 0 rgba(217,72,95,0);
  }
}

.loading-dot {
  display: inline-block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: #8a8a8a;
  animation: dotPulse 1.4s ease-in-out infinite;
  margin-right: 4px;
}

.loading-dot:nth-child(2) {
  animation-delay: 0.2s;
}

.loading-dot:nth-child(3) {
  animation-delay: 0.4s;
}

@keyframes dotPulse {
  0%, 80%, 100% {
    opacity: 0.25;
    transform: scale(0.8);
  }
  40% {
    opacity: 1;
    transform: scale(1);
  }
}

.model-sheet {
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(680px, calc(100vw - 32px));
  max-height: 82vh;
  background: #fff;
  border-radius: 24px;
  box-shadow: 0 24px 70px rgba(0,0,0,0.22);
  z-index: 99999;
  display: none;
  flex-direction: column;
  overflow: hidden;
}

.model-sheet.open {
  display: flex;
}

.sheet-handle {
  width: 54px;
  height: 5px;
  border-radius: 999px;
  background: #d1d5db;
  margin: 10px auto 6px;
  flex-shrink: 0;
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 18px 14px;
  border-bottom: 1px solid #eceff3;
  flex-shrink: 0;
}

.sheet-title {
  font-size: 18px;
  font-weight: 700;
  color: #111827;
}

.sheet-close {
  border: none;
  background: transparent;
  font-size: 30px;
  line-height: 1;
  color: #6b7280;
  cursor: pointer;
}

.sheet-body {
  flex: 1;
  overflow-y: auto;
  padding: 10px 18px 18px;
  -webkit-overflow-scrolling: touch;
}

.model-group {
  margin-bottom: 18px;
}

.model-group-title {
  font-size: 14px;
  font-weight: 700;
  color: #6b7280;
  margin: 0 0 8px;
}

.model-option {
  width: 100%;
  border: none;
  background: #fff;
  border-radius: 14px;
  padding: 12px 14px;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 14px;
  cursor: pointer;
  text-align: left;
  transition: background 0.18s ease;
  margin-bottom: 4px;
}

.model-option:hover {
  background: #f7f8fa;
}

.model-option.active {
  background: #edf7ea;
}

.model-option.locked {
  opacity: 0.55;
  cursor: not-allowed;
}

.model-option-left {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
  flex: 1;
}

.model-option-label {
  font-size: 16px;
  font-weight: 600;
  color: #111827;
  line-height: 1.25;
}

.model-option-desc {
  font-size: 13px;
  color: #6b7280;
  line-height: 1.45;
}

.checkmark {
  color: #0055ff;
  font-size: 22px;
  font-weight: 700;
  flex-shrink: 0;
  padding-top: 2px;
}

.sheet-actions {
  border-top: 1px solid #eceff3;
  padding: 14px 18px calc(14px + env(safe-area-inset-bottom));
  display: flex;
  gap: 10px;
  background: #fff;
  flex-shrink: 0;
}

.sheet-btn {
  flex: 1;
  border: none;
  border-radius: 14px;
  padding: 13px 16px;
  font: inherit;
  font-size: 15px;
  font-weight: 700;
  cursor: pointer;
}

.sheet-btn.secondary {
  background: #f3f4f6;
  color: #111827;
}

.sheet-btn.primary {
  background: #1677ff;
  color: #fff;
}

@media (max-width: 900px) {
  .sidebar.desktop {
    display: none !important;
  }
}

@media (max-width: 768px) {
  .model-sheet {
    top: auto;
    left: 50%;
    bottom: 0;
    transform: translateX(-50%);
    width: min(720px, 100vw);
    max-height: 85vh;
    border-radius: 24px 24px 0 0;
  }

  .message {
    max-width: 90%;
  }

  .wa-title {
    font-size: 16px;
  }

  .sub-model {
    max-width: 160px;
  }
}