@import url('https://fonts.googleapis.com/css2?family=Noto+Sans+SC:wght@400;500;600&display=swap');

:root {
  --bg:        oklch(14% 0.012 250);
  --bg-deep:   oklch(11% 0.014 250);
  --bg-raise:  oklch(18% 0.013 250);
  --surface:   oklch(20% 0.014 250);
  --border:    oklch(26% 0.014 250);
  --text:      oklch(92% 0.008 250);
  --text-muted:oklch(58% 0.012 250);
  --bubble-me: oklch(34% 0.055 240);
  --bubble-ai: oklch(22% 0.016 250);
  --send-hover:oklch(40% 0.08 240);
}

[data-theme="light"] {
  --bg:        #f7f8fa;
  --bg-deep:   #eef1f4;
  --bg-raise:  #ffffff;
  --surface:   #ffffff;
  --border:    #d7dde5;
  --text:      #111827;
  --text-muted:#667085;
  --bubble-me: #2563eb;
  --bubble-ai: #ffffff;
  --send-hover:#1d4ed8;
}

:root {
  --r-bubble: 20px;
  --r-tail-me: 4px;
  --r-tail-ai: 4px;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
}

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

body {
  font-family: "Noto Sans SC", "PingFang SC", "Helvetica Neue", sans-serif;
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  height: 100dvh;
  overflow: hidden;
}

/* ── Layout ── */
.layout {
  display: flex;
  height: 100dvh;
}

/* ── Sidebar ── */
.sidebar {
  width: 240px;
  flex-shrink: 0;
  background: var(--bg-deep);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.22s var(--ease-out);
  border-right: 1px solid var(--border);
}
.sidebar.hidden { width: 0; }

.sidebar-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 16px 14px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  white-space: nowrap;
}

.sidebar-header button {
  display: flex;
  align-items: center;
  gap: 4px;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s;
  font-family: inherit;
}
.sidebar-header button:hover { color: var(--text); border-color: var(--text-muted); }

.sidebar-footer {
  flex-shrink: 0;
  padding: 8px;
  border-top: 1px solid var(--border);
}
.sidebar-footer-btn {
  display: flex;
  align-items: center;
  width: 100%;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 13.5px;
  font-family: inherit;
  padding: 9px 12px;
  border-radius: 10px;
  cursor: pointer;
  text-align: left;
  transition: background 0.14s, color 0.14s;
  white-space: nowrap;
}
.sidebar-footer-btn:hover { background: var(--surface); color: var(--text); }
.sidebar-footer-btn.hidden { display: none; }

#convList {
  list-style: none;
  overflow-y: auto;
  flex: 1;
  padding: 6px 8px;
}
#convList::-webkit-scrollbar { width: 4px; }
#convList::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Conv list item ── */
#convList li {
  padding: 10px 12px;
  font-size: 13.5px;
  cursor: pointer;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-radius: 10px;
  margin-bottom: 2px;
  transition: background 0.14s, color 0.14s;
  display: flex;
  align-items: center;
  gap: 4px;
}
#convList li:hover { background: var(--surface); color: var(--text); }
#convList li.active { background: var(--surface); color: var(--text); font-weight: 500; }
#convList li.pinned .conv-title::before { content: "📌 "; font-size: 11px; }

.conv-title-row {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 1px;
  overflow: hidden;
}
.conv-title { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.conv-time { font-size: 10px; color: var(--text-muted); opacity: 0.6; white-space: nowrap; }

.conv-menu-btn {
  flex-shrink: 0;
  opacity: 0;
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 15px;
  cursor: pointer;
  padding: 0 2px;
  line-height: 1;
  border-radius: 4px;
  transition: opacity 0.12s, background 0.12s;
}
#convList li:hover .conv-menu-btn,
#convList li.active .conv-menu-btn { opacity: 1; }
.conv-menu-btn:hover { background: var(--border); color: var(--text); }

/* ── Conv context menu ── */
.conv-menu {
  position: fixed;
  z-index: 700;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  min-width: 110px;
  box-shadow: 0 8px 24px oklch(0% 0 0 / 0.3);
  animation: fade-in 0.12s ease both;
}
.conv-menu button {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  text-align: left;
  padding: 8px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.1s;
}
.conv-menu button:hover { background: var(--surface); }
.conv-menu button.danger { color: oklch(62% 0.2 25); }

/* ── Dialog ── */
.dialog-overlay {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.45);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 300;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade-in 0.15s ease both;
}
.dialog {
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 24px 24px 18px;
  width: 320px;
  max-width: 90vw;
  animation: modal-in 0.18s var(--ease-out) both;
}
.dialog h3 { font-size: 16px; font-weight: 600; margin-bottom: 8px; }
.dialog p  { font-size: 14px; color: var(--text-muted); margin-bottom: 20px; }
.dialog input[type="text"],
.dialog input[type="password"],
.dialog textarea,
.dialog select {
  width: 100%;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  margin-bottom: 16px;
}
.dialog textarea {
  min-height: 110px;
  resize: vertical;
}
.dialog input[type="text"]:focus,
.dialog input[type="password"]:focus,
.dialog textarea:focus,
.dialog select:focus { border-color: oklch(52% 0.16 248 / 0.6); }
.dialog input[type="checkbox"] {
  width: auto;
  flex: 0 0 auto;
  margin: 3px 0 0 0;
  padding: 0;
}
.dialog-actions { display: flex; justify-content: flex-end; gap: 8px; }
.dialog-actions button {
  border: none;
  border-radius: 10px;
  padding: 8px 18px;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.15s;
}
.dialog-actions .btn-cancel { background: var(--surface); color: var(--text); }
.dialog-actions .btn-cancel:hover { background: var(--border); }
.dialog-actions .btn-confirm { background: var(--bubble-me); color: #fff; }
.dialog-actions .btn-confirm:hover { background: var(--send-hover); }
.dialog-actions .btn-danger { background: oklch(50% 0.2 25); color: #fff; }
.dialog-actions .btn-danger:hover { background: oklch(44% 0.2 25); }
.memory-edit-dialog { width: 420px; }

/* ── Chat shell ── */
.chat-shell {
  display: flex;
  flex-direction: column;
  flex: 1;
  min-width: 0;
  background: var(--bg);
}

/* ── Top bar ── */
.top-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 13px 18px;
  background: var(--bg-deep);
  border-bottom: 1px solid var(--border);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
}

.top-bar h1 {
  font-size: 16px;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.chat-status {
  font-size: 12px;
  color: var(--text-muted);
  flex: 1;
  white-space: nowrap;
}

.top-actions { display: flex; gap: 6px; }

.icon-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 8px;
  padding: 5px 12px;
  font-size: 13px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  font-family: inherit;
  white-space: nowrap;
}
.icon-btn:hover { color: var(--text); border-color: var(--text-muted); background: var(--surface); }

/* ── Message list ── */
.message-list {
  flex: 1;
  padding: 20px 16px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 8px;
  background: var(--bg);
}
.message-list::-webkit-scrollbar { width: 4px; }
.message-list::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

/* ── Message row (for avatar layout) ── */
.msg-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
}
.msg-row.user { justify-content: flex-end; }
.msg-row.assistant { justify-content: flex-start; }

.avatar {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: oklch(22% 0.02 250);
  border: 1px solid var(--border);
  flex-shrink: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  letter-spacing: 0;
}
.avatar::after { content: "G"; }

/* ── Msg stack ── */
.msg-stack {
  display: flex;
  flex-direction: column;
  max-width: min(68%, 480px);
  min-width: 0;
  align-items: flex-start;
}
.msg-row.user .msg-stack { align-items: flex-end; }
.msg-row.assistant .msg-stack { align-items: flex-start; }

/* ── Bubbles ── */
.message {
  max-width: 100%;
  padding: 10px 14px;
  font-size: 15px;
  line-height: 1.55;
  word-break: break-word;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
  border-radius: 18px;
  animation: bubble-in 0.22s var(--ease-out) both;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(6px) scale(0.97); }
  to   { opacity: 1; transform: none; }
}

.message.user {
  background: var(--bubble-me);
  color: var(--text);
}

[data-theme="light"] .message.user {
  color: #ffffff;
}

.message.assistant {
  background: var(--bubble-ai);
  color: var(--text);
  border: 1px solid var(--border);
}

/* ── Message actions ── */
.msg-actions {
  display: flex;
  gap: 2px;
  margin-top: 2px;
  opacity: 0;
  transition: opacity 0.18s ease;
}
.msg-row:hover .msg-actions {
  opacity: 1;
}
.msg-actions button {
  background: none;
  border: none;
  color: var(--text-muted);
  padding: 2px 6px;
  font-size: 11px;
  font-family: inherit;
  cursor: pointer;
  border-radius: 4px;
  transition: color 0.12s, background 0.12s;
  opacity: 0.6;
}
.msg-actions button:hover { color: var(--text); background: var(--surface); opacity: 1; }

.message-action-menu {
  position: fixed;
  z-index: 2000;
  display: flex;
  flex-direction: column;
  min-width: 132px;
  padding: 6px;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.message-action-menu button {
  border: 0;
  background: transparent;
  color: var(--text);
  padding: 10px 12px;
  font: inherit;
  font-size: 14px;
  text-align: left;
  border-radius: 6px;
  cursor: pointer;
}

.message-action-menu button:hover,
.message-action-menu button:focus {
  background: var(--surface);
  outline: none;
}

/* ── Time separator ── */
.time-separator {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  opacity: 0.55;
  margin: 10px 0 4px;
  user-select: none;
}

/* ── Date divider ── */
.date-divider {
  text-align: center;
  font-size: 11px;
  color: var(--text-muted);
  opacity: 0.65;
  margin: 8px 0 4px;
  user-select: none;
}

/* ── Typing indicator ── */
.typing-indicator {
  background: var(--bubble-ai);
  border-radius: 18px;
  padding: 12px 16px;
  display: flex;
  align-items: center;
  gap: 5px;
  animation: bubble-in 0.22s var(--ease-out) both;
}

.typing-indicator span {
  display: block;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--text-muted);
  animation: typing-dot 1.2s ease-in-out infinite;
}
.typing-indicator span:nth-child(2) { animation-delay: 0.18s; }
.typing-indicator span:nth-child(3) { animation-delay: 0.36s; }

@keyframes typing-dot {
  0%, 60%, 100% { opacity: 0.25; transform: translateY(0); }
  30%           { opacity: 1;    transform: translateY(-3px); }
}

/* ── Tier bar ── */
.tier-bar {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  background: var(--bg-deep);
}

.tier-btn {
  padding: 3px 12px;
  font-size: 12px;
  font-family: inherit;
  font-weight: 500;
  color: var(--text-muted);
  background: none;
  border: 1px solid var(--border);
  border-radius: 20px;
  cursor: pointer;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  line-height: 1.6;
  letter-spacing: 0.02em;
}
.tier-btn:hover {
  color: var(--text);
  border-color: var(--text-muted);
}
.tier-btn.active {
  color: oklch(62% 0.18 248);
  border-color: oklch(52% 0.16 248 / 0.7);
  background: oklch(52% 0.16 248 / 0.08);
}

/* ── Tier dropdown (mobile only) ── */
.tier-dropdown-btn { display: none; }

/* ── Mobile tier bar: collapse to single dropdown ── */
@media (max-width: 820px) {
  .tier-bar {
    padding: 6px 12px;
    gap: 0;
    justify-content: flex-start;
  }
  .tier-bar .tier-btn { display: none; }
  .tier-dropdown-btn {
    display: inline-flex;
    align-items: center;
    gap: 2px;
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 20px;
    color: var(--text-muted);
    font-size: 12px;
    font-family: inherit;
    font-weight: 500;
    padding: 3px 10px 3px 12px;
    cursor: pointer;
    white-space: nowrap;
    transition: color 0.15s, border-color 0.15s, background 0.15s;
  }
  .tier-dropdown-btn:hover { color: var(--text); border-color: var(--text-muted); }
  .tier-dropdown-btn.active {
    color: oklch(62% 0.18 248);
    border-color: oklch(52% 0.16 248 / 0.6);
    background: oklch(52% 0.16 248 / 0.08);
  }
}

/* ── Input bar ── */
.input-bar {
  display: flex;
  align-items: center;
  padding: 12px 16px;
  background: var(--bg-deep);
  border-top: 1px solid var(--border);
}

.input-bar input {
  flex: 1;
  min-width: 0;
  padding: 12px 18px;
  font-size: 15px;
  font-family: inherit;
  color: var(--text);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 24px;
  outline: none;
  transition: border-color 0.15s;
  line-height: 1.4;
}
.input-bar input::placeholder { color: var(--text-muted); }
.input-bar input:focus { border-color: oklch(52% 0.16 248 / 0.6); }

.ghost-icon-btn {
  position: relative;
  flex-shrink: 0;
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 15px;
  font-family: inherit;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  margin-left: 6px;
}
.ghost-icon-btn:hover { color: var(--text); border-color: var(--text-muted); background: var(--surface); }
.ghost-icon-btn:disabled { opacity: 0.35; cursor: default; }
.ghost-icon-btn.active { color: oklch(62% 0.18 248); border-color: oklch(52% 0.16 248 / 0.6); background: oklch(52% 0.16 248 / 0.1); }

.ghost-icon-btn.auto-badge::after {
  content: "";
  position: absolute;
  top: 4px;
  right: 4px;
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: oklch(62% 0.18 248);
  border: 1.5px solid var(--bg-deep);
}

/* Desktop-only / mobile-only composer buttons */
.mobile-composer-btn { display: none; }

/* ── Composer popup menu (mobile) ── */
.composer-menu {
  position: fixed;
  z-index: 600;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 6px;
  min-width: 150px;
  box-shadow: 0 8px 24px oklch(0% 0 0 / 0.3);
  animation: fade-in 0.12s ease both;
}
.composer-menu button {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  text-align: left;
  padding: 10px 12px;
  border-radius: 8px;
  cursor: pointer;
  transition: background 0.1s;
}
.composer-menu button:hover { background: var(--surface); }
.composer-menu button .menu-icon { font-size: 16px; width: 20px; text-align: center; }
.composer-menu button.active-item { color: oklch(62% 0.18 248); }

.input-bar button[type="submit"] {
  flex-shrink: 0;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: oklch(38% 0.06 240);
  color: #fff;
  font-size: 18px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.15s, transform 0.12s;
}
.input-bar button[type="submit"]:hover  { background: oklch(44% 0.08 240); }
.input-bar button[type="submit"]:active { transform: scale(0.92); }
.input-bar button[type="submit"]:disabled { opacity: 0.4; cursor: default; transform: none; }

/* ── Memory modal ── */
.overlay {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  animation: fade-in 0.18s ease both;
}
.overlay.hidden { display: none; }

@keyframes fade-in {
  from { opacity: 0; }
  to   { opacity: 1; }
}

.modal {
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 16px;
  width: 460px;
  max-width: 92vw;
  max-height: 68vh;
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modal-in 0.22s var(--ease-out) both;
}

@keyframes modal-in {
  from { opacity: 0; transform: translateY(10px) scale(0.97); }
  to   { opacity: 1; transform: none; }
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 18px 14px;
  font-size: 15px;
  font-weight: 600;
}

.modal-header button {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 18px;
  cursor: pointer;
  width: 28px;
  height: 28px;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 50%;
  transition: background 0.14s, color 0.14s;
}
.modal-header button:hover { background: var(--surface); color: var(--text); }

#memoryList {
  overflow-y: auto;
  flex: 1;
  padding: 4px 0;
}
#memoryList::-webkit-scrollbar { width: 4px; }
#memoryList::-webkit-scrollbar-thumb { background: var(--border); border-radius: 2px; }

.memory-item {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 10px 18px;
  font-size: 14px;
  transition: background 0.12s;
}
.memory-item:hover { background: var(--surface); }
.memory-item span { flex: 1; color: var(--text); }
.memory-item.disabled span { color: var(--text-muted); text-decoration: line-through; }

.memory-domain {
  width: 58px;
  color: var(--text-muted);
  font-size: 11px;
  line-height: 1;
  text-transform: uppercase;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.memory-actions {
  display: flex;
  flex: 0 0 auto;
  gap: 6px;
}

.memory-item button {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 3px 10px;
  font-size: 12px;
  cursor: pointer;
  font-family: inherit;
  transition: color 0.14s, border-color 0.14s;
  white-space: nowrap;
}
.memory-item button:hover { color: var(--text); border-color: var(--text-muted); }
.memory-item button.danger { color: oklch(62% 0.2 25); }
.memory-item button.danger:hover { border-color: oklch(62% 0.2 25); }

.memory-add {
  display: flex;
  gap: 8px;
  padding: 12px 18px;
  border-top: 1px solid var(--border);
}

.memory-add select,
.memory-add input {
  flex: 1;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
}
.memory-add select {
  flex: 0 0 104px;
  cursor: pointer;
}
.memory-add input::placeholder { color: var(--text-muted); }
.memory-add select:focus,
.memory-add input:focus { border-color: oklch(52% 0.16 248 / 0.6); }

.memory-add button {
  background: var(--bubble-me);
  border: none;
  color: #fff;
  border-radius: 10px;
  padding: 0 16px;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  transition: background 0.15s;
}
.memory-add button:hover { background: var(--send-hover); }

/* ── Reduced motion ── */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 0.01ms !important; transition-duration: 0.01ms !important; }
}

.user-email-label { display: none; }
.hidden { display: none; }

/* ── More menu ── */
.mobile-only { display: none; }

.more-menu {
  position: fixed;
  z-index: 600;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px;
  min-width: 120px;
  box-shadow: 0 8px 24px oklch(0% 0 0 / 0.3);
  animation: fade-in 0.12s ease both;
}
.more-menu button {
  display: block;
  width: 100%;
  background: none;
  border: none;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  text-align: left;
  padding: 8px 12px;
  border-radius: 7px;
  cursor: pointer;
  transition: background 0.1s;
}
.more-menu button:hover { background: var(--surface); }

/* ── Login overlay ── */
#loginOverlay .modal input[type="email"],
#loginOverlay .modal input[type="password"] {
  display: block;
  width: 100%;
  padding: 9px 12px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  color: var(--text);
  font-size: 14px;
  font-family: inherit;
  outline: none;
  transition: border-color 0.15s;
  margin-bottom: 10px;
}
#loginOverlay .modal input[type="email"]::placeholder,
#loginOverlay .modal input[type="password"]::placeholder { color: var(--text-muted); }
#loginOverlay .modal input[type="email"]:focus,
#loginOverlay .modal input[type="password"]:focus { border-color: oklch(52% 0.16 248 / 0.6); }

#loginBtn {
  display: block;
  width: 100%;
  background: var(--bubble-me);
  border: none;
  color: #fff;
  border-radius: 10px;
  padding: 10px 0;
  cursor: pointer;
  font-size: 14px;
  font-family: inherit;
  font-weight: 500;
  transition: background 0.15s;
}
#loginBtn:hover { background: var(--send-hover); }
#loginBtn:disabled { opacity: 0.5; cursor: default; }

/* ── Sidebar backdrop ── */
.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.35);
  z-index: 450;
}
.sidebar-backdrop.hidden { display: none; }

/* ── Mobile ── */
@media (hover: none), (pointer: coarse) {
  .msg-actions { display: none !important; }
  .conv-menu-btn { opacity: 1; min-width: 32px; min-height: 32px; padding: 6px; }
}

@media (max-width: 820px) {
  .layout { display: block; overflow: hidden; }
  .chat-shell { width: 100vw; height: 100dvh; }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(82vw, 340px);
    max-width: 340px;
    z-index: 500;
    transform: translateX(-100%);
    transition: transform 0.22s var(--ease-out);
  }
  .sidebar.hidden { width: min(82vw, 340px); }
  .sidebar.mobile-open { transform: translateX(0); }

  .top-bar h1 { font-size: 14px; white-space: nowrap; flex-shrink: 0; }
  .top-bar {
    gap: 6px;
    padding: 10px 12px;
    padding-top: calc(10px + env(safe-area-inset-top));
  }
  .chat-status { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; min-width: 0; }
  .top-actions { flex-shrink: 0; }
  .icon-btn { padding: 5px 8px; font-size: 12px; }

  .mobile-only { display: inline-flex; }

  .message-list { padding: 12px 10px 8px; }
  .msg-stack { max-width: 82vw; }
  .message {
    max-width: 100%;
    -webkit-touch-callout: none;
    -webkit-user-select: none;
    user-select: none;
    touch-action: pan-y;
  }
  /* .msg-actions intentionally shown on pointer devices; hidden via capability query below */

  .input-bar {
    padding: 8px 12px;
    padding-bottom: calc(env(safe-area-inset-bottom) + 8px);
    gap: 6px;
  }
  .input-bar input { font-size: 16px; /* prevent iOS zoom */ }

  /* Reduce weight of secondary composer buttons on mobile */
  .ghost-icon-btn {
    width: 40px;
    height: 40px;
    font-size: 15px;
    margin-left: 0;
    opacity: 0.55;
    border-color: transparent;
  }
  .ghost-icon-btn:active,
  .ghost-icon-btn.active { opacity: 1; border-color: oklch(52% 0.16 248 / 0.6); }

  /* Desktop-only secondary buttons hidden on mobile */
  .desktop-only-btn { display: none !important; }
  /* Mobile composer menu button shown only on mobile */
  .mobile-composer-btn { display: inline-flex; }

  /* Keep send button full weight */
  .input-bar button[type="submit"] { width: 44px; height: 44px; }

  .modal { max-width: 94vw !important; max-height: 80dvh; overflow-y: auto; }
  .dialog { max-width: 94vw !important; }
}

/* ── Image preview bar ── */
.image-preview-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
  background: var(--bg-deep);
  border-top: 1px solid var(--border);
}
.image-preview-bar.hidden { display: none !important; }
.img-preview-wrap {
  position: relative;
  display: inline-flex;
}
.img-preview-thumb {
  width: 64px;
  height: 64px;
  object-fit: cover;
  border-radius: 8px;
  border: 1px solid var(--border);
}
.img-preview-remove {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 18px;
  height: 18px;
  background: var(--bg-raise);
  border: 1px solid var(--border);
  border-radius: 50%;
  color: var(--text-muted);
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
  padding: 0;
  font-family: inherit;
}
.img-preview-remove:hover { color: var(--text); }

/* ── Message image ── */
img.msg-image {
  display: block;
  max-width: 100%;
  max-height: 300px;
  border-radius: 12px;
  margin-bottom: 4px;
  object-fit: contain;
}

@media (max-width: 820px) {
  .image-preview-bar { padding: 6px 12px; }
}

/* ── Image attachment card states ── */
@keyframes skeleton-sweep {
  0%   { background-position: -200px 0; }
  100% { background-position: calc(200px + 100%) 0; }
}
.img-preview-skeleton {
  display: none;
  width: 64px;
  height: 64px;
  border-radius: 8px;
  background: linear-gradient(90deg, var(--border) 25%, var(--surface) 50%, var(--border) 75%);
  background-size: 400px 100%;
  animation: skeleton-sweep 1.2s ease-in-out infinite;
}
.image-preview-bar.loading .img-preview-skeleton { display: block; }
.image-preview-bar.loading .img-preview-thumb,
.image-preview-bar.error .img-preview-thumb { display: none; }
.image-preview-bar.loading .img-preview-remove,
.image-preview-bar.error .img-preview-remove { display: none; }
.img-preview-error-row { display: flex; align-items: center; gap: 8px; flex: 1; }
.img-preview-error-row.hidden { display: none; }
.img-preview-error-msg {
  font-size: 12px;
  color: oklch(62% 0.2 25);
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.img-preview-retry-btn {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 6px;
  padding: 3px 10px;
  font-size: 12px;
  font-family: inherit;
  cursor: pointer;
  flex-shrink: 0;
  transition: color 0.14s, border-color 0.14s;
}
.img-preview-retry-btn:hover { color: var(--text); border-color: var(--text-muted); }

/* ── Drag-over indicator ── */
.chat-shell.drag-over .message-list {
  outline: 2px dashed oklch(52% 0.16 248 / 0.6);
  outline-offset: -8px;
}

/* ── Lightbox ── */
.lightbox-overlay {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.85);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 3000;
  animation: fade-in 0.15s ease both;
}
.lightbox-img {
  max-width: min(90vw, 960px);
  max-height: 85dvh;
  border-radius: 8px;
  object-fit: contain;
}
.lightbox-close {
  position: fixed;
  top: 16px;
  right: 16px;
  background: oklch(20% 0.01 250 / 0.8);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 50%;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  backdrop-filter: blur(4px);
  -webkit-backdrop-filter: blur(4px);
  transition: background 0.14s;
}
.lightbox-close:hover { background: oklch(28% 0.01 250); }

/* ── Mobile image bottom sheet ── */
.img-bottom-sheet-overlay {
  position: fixed;
  inset: 0;
  background: oklch(0% 0 0 / 0.4);
  z-index: 2000;
  display: flex;
  align-items: flex-end;
}
.img-bottom-sheet {
  width: 100%;
  background: var(--bg-raise);
  border-top-left-radius: 16px;
  border-top-right-radius: 16px;
  padding: 8px 16px calc(env(safe-area-inset-bottom) + 16px);
  display: flex;
  flex-direction: column;
  animation: sheet-in 0.22s var(--ease-out) both;
}
@keyframes sheet-in {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
.img-bottom-sheet button {
  background: none;
  border: none;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  font-size: 17px;
  font-family: inherit;
  padding: 16px 8px;
  text-align: left;
  cursor: pointer;
  transition: background 0.12s;
}
.img-bottom-sheet button:last-child { border-bottom: none; }
.img-bottom-sheet button:hover { background: var(--surface); }
.img-bottom-sheet-cancel { color: oklch(62% 0.18 248) !important; font-weight: 600; margin-top: 8px; }

img.msg-image { cursor: pointer; }
