/* ─── AK Chat Platform — Chat Styles ────────────────────────────────────────── */

/* ── Messages container ─────────────────────────────────────────────────────── */
#messages-container {
  flex: 1;
  overflow-y: auto;
  padding: 24px 16px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  scroll-behavior: smooth;
}

#messages-container::-webkit-scrollbar       { width: 5px; }
#messages-container::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }

/* Welcome / empty state inside chat */
#chat-welcome {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  gap: 16px;
  color: var(--text-muted);
  text-align: center;
  padding: 40px;
  animation: fadeIn 0.4s ease;
}

.welcome-icon {
  width: 64px;
  height: 64px;
  border-radius: 16px;
  background: var(--secondary);
  border: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 28px;
  font-weight: 700;
  color: var(--primary);
  margin-bottom: 4px;
}

#chat-welcome h2 { font-size: 18px; font-weight: 700; color: var(--text); }
#chat-welcome p  { font-size: 13px; color: var(--text-muted); line-height: 1.6; max-width: 340px; }

/* ── Message rows ───────────────────────────────────────────────────────────── */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  max-width: 75%;
  animation: msgIn 0.25s ease;
}

@keyframes msgIn {
  from { opacity: 0; transform: translateY(8px); }
  to   { opacity: 1; transform: translateY(0); }
}

.msg-row.user     { align-self: flex-end; flex-direction: row-reverse; }
.msg-row.assistant { align-self: flex-start; }

/* Avatar */
.msg-avatar {
  width: 28px;
  height: 28px;
  border-radius: 8px;
  background: var(--tertiary);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 700;
  color: var(--primary);
  flex-shrink: 0;
  overflow: hidden;
  margin-bottom: 2px;
}
.msg-avatar img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; }
.msg-row.user .msg-avatar { background: var(--primary); color: #fff; }

/* Bubble */
.msg-bubble-wrap {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 0;
}

.msg-row.user .msg-bubble-wrap { align-items: flex-end; }

.msg-bubble {
  padding: 10px 14px;
  border-radius: 14px;
  font-size: 14px;
  line-height: 1.6;
  word-wrap: break-word;
  max-width: 100%;
  position: relative;
}

/* User bubble */
.msg-row.user .msg-bubble {
  background: var(--primary);
  color: #fff;
  border-bottom-right-radius: 4px;
}

/* Assistant bubble */
.msg-row.assistant .msg-bubble {
  background: var(--tertiary);
  color: var(--text);
  border-bottom-left-radius: 4px;
}

/* ── Attachment thumbnails & file chips ──────────────────────────────────────── */
.msg-images {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 6px;
}
.msg-img-thumb {
  max-width: 220px;
  max-height: 180px;
  border-radius: 8px;
  object-fit: cover;
  cursor: pointer;
  border: 2px solid rgba(255,255,255,0.2);
  transition: opacity 0.15s;
}
.msg-img-thumb:hover { opacity: 0.85; }

.msg-files {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 6px;
}
.msg-file-chip {
  background: rgba(255,255,255,0.15);
  border-radius: 6px;
  padding: 3px 8px;
  font-size: 12px;
  white-space: nowrap;
}

/* Timestamp + controls row */
.msg-meta {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 0 2px;
}

.msg-time {
  font-size: 10px;
  color: var(--text-dim);
  white-space: nowrap;
}

.btn-voice, .btn-copy, .btn-edit {
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: var(--text-muted);
  transition: background var(--transition), color var(--transition);
  flex-shrink: 0;
}
.btn-voice:hover, .btn-copy:hover, .btn-edit:hover { background: var(--border); color: var(--text); }
.btn-voice.playing       { color: var(--primary); animation: pulse 1s infinite; }
.btn-voice.loading       { color: var(--text-muted); animation: pulse 0.6s infinite; pointer-events: none; }
.btn-copy                { opacity: 0; transition: opacity var(--transition), background var(--transition), color var(--transition); }
.msg-row:hover .btn-copy { opacity: 1; }
.btn-edit                { opacity: 0; transition: opacity var(--transition), background var(--transition), color var(--transition); }
.msg-row:hover .btn-edit { opacity: 1; }

@keyframes pulse { 0%,100% { opacity: 1; } 50% { opacity: 0.5; } }

/* ── Markdown rendering ─────────────────────────────────────────────────────── */
.msg-bubble p   { margin: 0 0 8px; }
.msg-bubble p:last-child { margin-bottom: 0; }
.msg-bubble strong { font-weight: 700; }
.msg-bubble em     { font-style: italic; }
.msg-bubble a      { color: var(--primary); }

.msg-row.user .msg-bubble a { color: rgba(255,255,255,0.85); }

.msg-bubble code {
  font-family: 'Fira Code', 'Courier New', monospace;
  font-size: 12.5px;
  background: rgba(0,0,0,0.3);
  padding: 1px 5px;
  border-radius: 4px;
}

.msg-bubble pre {
  background: #0d1117;
  border-radius: 8px;
  padding: 12px 12px 12px 12px;
  overflow-x: auto;
  margin: 8px 0;
  position: relative;
}
.msg-bubble pre code { background: none; padding: 0; font-size: 12px; }

/* Code block copy button */
.code-copy-btn {
  position: absolute;
  top: 7px;
  right: 8px;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.12);
  border-radius: 5px;
  color: rgba(255,255,255,0.5);
  font-size: 11px;
  padding: 2px 8px;
  cursor: pointer;
  transition: background .15s, color .15s;
  font-family: inherit;
  line-height: 1.6;
}
.code-copy-btn:hover { background: rgba(255,255,255,0.15); color: #fff; }

/* Streaming text */
.stream-content {
  white-space: pre-wrap;
  word-break: break-word;
  font-size: 14px;
  line-height: 1.65;
  color: var(--text);
}

.msg-bubble ul, .msg-bubble ol {
  padding-left: 18px;
  margin: 6px 0;
}
.msg-bubble li { margin-bottom: 2px; }

.msg-bubble h1,.msg-bubble h2,.msg-bubble h3 {
  font-size: 15px;
  font-weight: 700;
  margin: 10px 0 4px;
}

.msg-bubble blockquote {
  border-left: 3px solid var(--primary);
  padding-left: 10px;
  color: var(--text-muted);
  margin: 6px 0;
  font-style: italic;
}

.msg-bubble table { border-collapse: collapse; margin: 8px 0; width: 100%; }
.msg-bubble th, .msg-bubble td { border: 1px solid var(--border); padding: 6px 10px; font-size: 13px; }
.msg-bubble th { background: rgba(255,107,0,0.15); }
.msg-bubble tr:nth-child(even) td { background: rgba(255,255,255,0.03); }

/* ── Typing indicator ───────────────────────────────────────────────────────── */
#typing-indicator {
  display: none;
  align-self: flex-start;
  align-items: flex-end;
  gap: 8px;
  margin-top: 4px;
}
#typing-indicator.visible { display: flex; }

.typing-bubble {
  background: var(--tertiary);
  border-radius: 14px;
  border-bottom-left-radius: 4px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 4px;
}

.typing-dot {
  width: 6px;
  height: 6px;
  background: var(--text-muted);
  border-radius: 50%;
  animation: typingBounce 1.2s infinite;
}
.typing-dot:nth-child(2) { animation-delay: 0.2s; }
.typing-dot:nth-child(3) { animation-delay: 0.4s; }

@keyframes typingBounce {
  0%, 80%, 100% { transform: translateY(0); opacity: 0.4; }
  40%           { transform: translateY(-5px); opacity: 1; }
}

/* ── Day separator ──────────────────────────────────────────────────────────── */
.day-separator {
  display: flex;
  align-items: center;
  gap: 12px;
  margin: 12px 0;
  color: var(--text-dim);
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.8px;
}
.day-separator::before, .day-separator::after {
  content: '';
  flex: 1;
  height: 1px;
  background: var(--border);
}

/* ── Input area ─────────────────────────────────────────────────────────────── */
#input-area {
  flex-shrink: 0;
  border-top: 1px solid var(--border);
  background: var(--secondary);
  padding: 12px 16px;
  padding-bottom: calc(12px + env(safe-area-inset-bottom));
  padding-left: calc(16px + env(safe-area-inset-left));
  padding-right: calc(16px + env(safe-area-inset-right));
}

#input-form {
  display: flex;
  align-items: flex-end;
  gap: 10px;
  background: var(--bg-dark);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 8px 8px 8px 14px;
  transition: border-color var(--transition), box-shadow var(--transition);
}
#input-form:focus-within {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px var(--primary-glow);
}

#message-input {
  flex: 1;
  background: none;
  border: none;
  color: var(--text);
  font-size: 16px; /* 16px minimum prevents iOS Safari auto-zoom */
  resize: none;
  line-height: 1.5;
  max-height: 160px;
  min-height: 24px;
  overflow-y: auto;
  padding: 0;
  outline: none;
  box-shadow: none;
}
#message-input::placeholder { color: var(--text-muted); }
#message-input:focus        { border: none; box-shadow: none; }

#send-btn {
  width: 36px;
  height: 36px;
  border-radius: 8px;
  background: var(--primary);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), transform 0.1s;
}
#send-btn:hover   { background: var(--primary-dark); }
#send-btn:active  { transform: scale(0.92); }
#send-btn:disabled { background: var(--tertiary); color: var(--text-dim); cursor: not-allowed; }

#mic-btn {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: transparent;
  color: var(--text-muted);
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  transition: background var(--transition), color var(--transition);
}
#mic-btn:hover     { background: var(--border); color: var(--text); }
#mic-btn.recording { color: #e53e3e; animation: pulse 0.8s infinite; }

.input-hint {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 6px;
  text-align: right;
}

#model-select {
  appearance: none;
  -webkit-appearance: none;
  background-color: rgba(255,107,0,0.08);
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='11' height='11' viewBox='0 0 24 24' fill='%23ff6b00'%3E%3Cpath d='M7 10l5 5 5-5z'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 7px center;
  border: 1px solid rgba(255,107,0,0.35);
  border-radius: 20px;
  color: var(--primary);
  font-size: 11px;
  font-weight: 600;
  font-family: inherit;
  padding: 5px 24px 5px 10px;
  height: auto;
  align-self: center;
  min-width: 80px;
  max-width: 180px;
  cursor: pointer;
  outline: none;
  flex-shrink: 0;
  transition: border-color var(--transition), background-color var(--transition), box-shadow var(--transition);
}
#model-select:hover { background-color: rgba(255,107,0,0.15); border-color: var(--primary); }
#model-select:focus { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-glow); color: var(--primary); }

/* ── Custom model dropdown ──────────────────────────────────────────────────── */
#model-dd-wrap {
  position: relative;
  flex-shrink: 0;
  align-self: center;
}

#model-dd-trigger {
  display: flex;
  align-items: center;
  gap: 5px;
  appearance: none;
  background-color: rgba(255,107,0,0.08);
  border: 1px solid rgba(255,107,0,0.35);
  border-radius: 20px;
  color: var(--primary);
  font-size: 11px;
  font-weight: 600;
  font-family: inherit;
  padding: 5px 10px;
  min-width: 80px;
  max-width: 180px;
  cursor: pointer;
  outline: none;
  transition: border-color var(--transition), background-color var(--transition), box-shadow var(--transition);
  white-space: nowrap;
  overflow: hidden;
}
#model-dd-trigger:hover { background-color: rgba(255,107,0,0.15); border-color: var(--primary); }
#model-dd-trigger.open  { border-color: var(--primary); box-shadow: 0 0 0 2px var(--primary-glow); }
#model-dd-trigger svg   { flex-shrink: 0; opacity: 0.7; transition: transform 0.15s; }
#model-dd-trigger.open svg { transform: rotate(180deg); }
.model-dd-trigger-name  { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; }

#model-dropdown-list {
  display: none;
  position: absolute;
  bottom: calc(100% + 6px);
  left: 0;
  z-index: 200;
  background: var(--secondary);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.35);
  min-width: 260px;
  width: max-content;
  max-width: 360px;
  overflow: hidden;
  animation: ddFadeIn 0.12s ease;
}
#model-dropdown-list.open { display: block; }

@keyframes ddFadeIn {
  from { opacity: 0; transform: translateY(4px); }
  to   { opacity: 1; transform: translateY(0); }
}

.model-dd-item {
  display: flex;
  flex-direction: column;
  gap: 2px;
  padding: 9px 14px;
  cursor: pointer;
  transition: background var(--transition);
  border-bottom: 1px solid var(--border);
}
.model-dd-item:last-child { border-bottom: none; }
.model-dd-item:hover      { background: var(--tertiary); }
.model-dd-item.selected   { background: rgba(255,107,0,0.1); }

.model-dd-name {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  white-space: nowrap;
}
.model-dd-desc {
  font-size: 11px;
  color: var(--text-muted);
  line-height: 1.4;
  white-space: nowrap;
}

#btn-attach {
  opacity: 0.5;
  transition: opacity var(--transition);
}
#btn-attach:hover { opacity: 1; }

/* ── Utility ────────────────────────────────────────────────────────────────── */
@keyframes fadeIn {
  from { opacity: 0; }
  to   { opacity: 1; }
}

@media (max-width: 768px) {
  .msg-row { max-width: 88%; }
  #messages-container { padding: 16px 10px; }
}

/* ── Mobile input: two-row layout ───────────────────────────────────────────── */
@media (max-width: 600px) {
  #input-area {
    padding: 8px 10px;
    padding-bottom: calc(8px + env(safe-area-inset-bottom));
  }

  #input-form {
    flex-wrap: wrap;
    padding: 8px 10px 6px;
    gap: 0;
    align-items: center;
  }

  /* Row 1: textarea, full width */
  #message-input {
    order: 1;
    flex-basis: 100%;
    width: 100%;
    min-height: 28px;
    max-height: 100px;
    color: var(--text);
    padding-bottom: 6px;
    border-bottom: 1px solid var(--border);
    margin-bottom: 4px;
  }

  /* Row 2: [model][attach] ···· [mic][send] */
  #model-dd-wrap { order: 2; flex-shrink: 0; }
  #model-dd-trigger { font-size: 10px; padding: 4px 8px; min-width: 70px; }
  #btn-attach   { order: 3; margin-left: 4px; }
  #mic-btn      { order: 4; margin-left: auto; width: 28px; height: 28px; }
  #send-btn     { order: 5; margin-left: 6px; width: 32px; height: 32px; }

  .input-hint { display: none; }
}
