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

:root {
  --bg: #ffffff;
  --surface: #f4f4f5;
  --surface-hover: #e9e9eb;
  --border: #e4e4e7;
  --border-strong: #d4d4d8;
  --text: #0a0d12;
  --muted: #71717a;
  --accent: #0a0d12;
  --accent-hover: #27272a;
  --on-accent: #ffffff;
  --error-bg: #fdecec;
  --error-text: #b42318;
  --drop-backdrop: rgba(255, 255, 255, 0.82);
  --drop-shadow: rgba(10, 13, 18, 0.08);
  --shimmer-mid: #ececee;
  --overlay-btn-bg: rgba(10, 13, 18, 0.78);
  --overlay-btn-text: #ffffff;
  --pending-overlay: rgba(255, 255, 255, 0.35);
  --hero-shadow: rgba(10, 13, 18, 0.18);
  --composer-shadow: rgba(10, 13, 18, 0.05);
  --radius-lg: 22px;
  --radius-md: 14px;
}

[data-theme="dark"] {
  --bg: #0a0d12;
  --surface: #171a21;
  --surface-hover: #22262f;
  --border: #2a303c;
  --border-strong: #363d4c;
  --text: #f4f4f5;
  --muted: #a1a1aa;
  --accent: #f4f4f5;
  --accent-hover: #e4e4e7;
  --on-accent: #0a0d12;
  --error-bg: #3a1f1f;
  --error-text: #fca5a5;
  --drop-backdrop: rgba(10, 13, 18, 0.88);
  --drop-shadow: rgba(0, 0, 0, 0.35);
  --shimmer-mid: #2a303c;
  --overlay-btn-bg: rgba(244, 244, 245, 0.88);
  --overlay-btn-text: #0a0d12;
  --pending-overlay: rgba(0, 0, 0, 0.35);
  --hero-shadow: rgba(0, 0, 0, 0.45);
  --composer-shadow: rgba(0, 0, 0, 0.25);
}

html, body { height: 100%; }

body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  transition: background 0.18s ease, color 0.18s ease;
  overflow-x: hidden;
}

.chat {
  position: relative;
  display: flex;
  flex-direction: column;
  height: 100dvh;
}

/* --- Header --- */

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  flex-shrink: 0;
  padding: 10px 16px;
  border-bottom: 1px solid var(--border);
  background: var(--bg);
}

.chat-header-brand {
  display: flex;
  align-items: center;
  gap: 9px;
}

.brand-logo { display: flex; }
.brand-logo svg { border-radius: 6px; display: block; }

.chat-header-title { font-size: 14.5px; font-weight: 600; letter-spacing: -0.01em; }

.theme-toggle {
  position: relative;
  flex-shrink: 0;
}

.theme-toggle .theme-icon {
  display: block;
}

.theme-toggle .theme-icon-sun {
  display: none;
}

[data-theme="dark"] .theme-toggle .theme-icon-moon {
  display: none;
}

[data-theme="dark"] .theme-toggle .theme-icon-sun {
  display: block;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 34px;
  height: 34px;
  border: none;
  border-radius: 10px;
  background: transparent;
  color: var(--muted);
  cursor: pointer;
  transition: background 0.12s, color 0.12s;
}

.icon-btn:hover { background: var(--surface); color: var(--text); }

/* --- Chat body (messages + composer) --- */

.chat-body {
  position: relative;
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.chat--empty .chat-body {
  justify-content: center;
  align-items: center;
  gap: 24px;
  padding: 16px;
}

.chat-empty-state {
  flex: 0 1 auto;
  width: 100%;
}

.chat--empty .chat-empty-state {
  display: block;
}

.chat--empty .chat-messages {
  display: none !important;
}

.chat:not(.chat--empty) .chat-empty-state {
  display: none !important;
}

.chat:not(.chat--empty) .chat-messages {
  display: block;
}

.chat--empty .chat-composer {
  flex: 0 0 auto;
  width: 100%;
  padding: 0 16px calc(14px + env(safe-area-inset-bottom));
}

.drop-overlay {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--drop-backdrop);
  backdrop-filter: blur(2px);
  pointer-events: none;
}

.drop-overlay-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 28px 36px;
  border: 2px dashed var(--border-strong);
  border-radius: var(--radius-lg);
  background: var(--bg);
  box-shadow: 0 8px 28px var(--drop-shadow);
  text-align: center;
}

.drop-overlay-icon {
  font-size: 28px;
  line-height: 1;
}

.drop-overlay-title {
  font-size: 16px;
  font-weight: 600;
  color: var(--text);
}

.drop-overlay-sub {
  font-size: 13px;
  color: var(--muted);
}

.chat.drag-active .drop-overlay {
  pointer-events: auto;
}

.chat.drag-active .drop-overlay:not(.hidden) {
  display: flex;
}

/* --- Messages --- */

.chat-messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 24px 16px 8px;
}

.msg-row {
  display: flex;
  max-width: 740px;
  margin-left: auto;
  margin-right: auto;
  margin-bottom: 18px;
}

.msg-row.user { justify-content: flex-end; }

.msg-bubble {
  max-width: 78%;
  padding: 10px 16px;
  border-radius: var(--radius-lg);
  border-bottom-right-radius: 8px;
  background: var(--surface);
  font-size: 15px;
  line-height: 1.5;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-row.support { gap: 10px; }

.msg-avatar {
  flex: 0 0 28px;
  width: 28px;
  height: 28px;
  border-radius: 8px;
  overflow: hidden;
  margin-top: 2px;
}

.msg-avatar svg { display: block; }

.msg-body {
  max-width: 85%;
  font-size: 15px;
  line-height: 1.55;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  padding-top: 3px;
}

.msg-bubble.has-media,
.msg-body.has-media {
  max-width: min(300px, 85%);
}

.msg-bubble.has-media {
  padding: 3px;
}

.msg-body.has-media {
  padding-top: 0;
}

.msg-caption {
  padding: 8px 10px 2px;
  font-size: 15px;
  line-height: 1.45;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.msg-bubble.has-media .msg-meta,
.msg-body.has-media .msg-meta {
  padding: 0 10px 2px;
  margin-top: 4px;
}

.msg-media {
  display: block;
}

.msg-media-frame {
  position: relative;
  overflow: hidden;
  border-radius: calc(var(--radius-lg) - 3px);
  background: var(--surface-hover);
  cursor: zoom-in;
  max-width: 100%;
}

.msg-media-img {
  display: block;
  width: 100%;
  max-height: 320px;
  object-fit: cover;
  opacity: 0;
  transition: opacity 0.18s ease;
}

.msg-media-frame.is-loaded .msg-media-img {
  opacity: 1;
}

.msg-media-shimmer {
  position: absolute;
  inset: 0;
  min-height: 160px;
  background: linear-gradient(
    110deg,
    var(--surface-hover) 8%,
    var(--shimmer-mid) 18%,
    var(--surface-hover) 33%
  );
  background-size: 200% 100%;
  animation: media-shimmer 1.2s linear infinite;
}

.msg-media-frame.is-loaded .msg-media-shimmer,
.msg-media-frame.is-error .msg-media-shimmer {
  display: none;
}

.msg-media-retry {
  display: none;
  position: absolute;
  inset: 0;
  margin: auto;
  height: 34px;
  padding: 0 14px;
  border: none;
  border-radius: 999px;
  background: var(--overlay-btn-bg);
  color: var(--overlay-btn-text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

.msg-media-frame.is-error .msg-media-retry {
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.msg-media-frame.is-error .msg-media-img {
  opacity: 0.15;
}

.msg-row--pending .msg-media-frame {
  cursor: default;
}

.msg-row--pending .msg-media-frame::after {
  content: "";
  position: absolute;
  inset: 0;
  background: var(--pending-overlay);
}

.msg-row--failed .msg-bubble {
  opacity: 0.75;
}

.msg-retry {
  display: block;
  margin-top: 8px;
  padding: 6px 12px;
  border: none;
  border-radius: 999px;
  background: var(--overlay-btn-bg);
  color: var(--overlay-btn-text);
  font: inherit;
  font-size: 13px;
  cursor: pointer;
}

@keyframes media-shimmer {
  to { background-position-x: -200%; }
}

.msg-doc-link {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 6px;
  padding: 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
  text-decoration: none;
  word-break: break-all;
}

.msg-doc-link:hover { background: var(--surface-hover); }

.msg-meta {
  display: block;
  font-size: 11px;
  color: var(--muted);
  margin-top: 5px;
}

.msg-row.user .msg-meta { text-align: right; }

.msg-row.system {
  justify-content: center;
}

.msg-system {
  max-width: min(520px, 92%);
  padding: 10px 14px;
  border-radius: var(--radius-md);
  background: var(--surface);
  font-size: 13px;
  line-height: 1.5;
  color: var(--muted);
  text-align: center;
}

.msg-system a {
  color: var(--text);
  word-break: break-all;
}

/* --- Hero (empty state) --- */

.hero {
  max-width: 480px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  text-align: center;
  padding: 0 8px;
}

.hero-logo { margin-bottom: 22px; }
.hero-logo svg { border-radius: 12px; box-shadow: 0 6px 18px var(--hero-shadow); display: block; }

.hero-hi { font-size: 19px; font-weight: 500; color: var(--muted); letter-spacing: -0.01em; }
.hero-title { font-size: 17px; font-weight: 600; margin-top: 2px; letter-spacing: -0.006em; }

.hero-sub {
  font-size: 13.5px;
  color: var(--muted);
  line-height: 1.55;
  margin-top: 10px;
}

.primary-btn {
  margin-top: 18px;
  height: 38px;
  padding: 0 18px;
  border: none;
  border-radius: 10px;
  background: var(--accent);
  color: var(--on-accent);
  font-size: 14px;
  font-weight: 500;
  cursor: pointer;
  transition: background 0.12s;
}

.primary-btn:hover { background: var(--accent-hover); }

/* --- Composer --- */

.chat-composer {
  flex-shrink: 0;
  padding: 8px 16px calc(14px + env(safe-area-inset-bottom));
  background: var(--bg);
}

.composer-box {
  max-width: 740px;
  margin: 0 auto;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 6px 8px 6px 14px;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.composer-box:focus-within {
  border-color: var(--border-strong);
  box-shadow: 0 1px 8px var(--composer-shadow);
}

.composer-box textarea {
  display: block;
  width: 100%;
  border: none;
  background: transparent;
  resize: none;
  font: inherit;
  font-size: 15px;
  line-height: 1.5;
  color: var(--text);
  padding: 8px 2px 2px;
  max-height: 45dvh;
  outline: none;
}

.composer-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding-top: 2px;
}

.send-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
  transition: background 0.12s, opacity 0.12s;
}

.send-btn:hover { background: var(--accent-hover); }
.send-btn:disabled { background: var(--border-strong); cursor: default; }

.attach-strip {
  display: flex;
  gap: 10px;
  padding: 8px 2px 2px;
}

.attach-thumb {
  position: relative;
  width: 80px;
  height: 64px;
  border-radius: 10px;
  overflow: visible;
}

.attach-thumb img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  display: block;
}

.attach-thumb button,
.attach-doc-preview button {
  position: absolute;
  top: -7px;
  right: -7px;
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  border: none;
  border-radius: 50%;
  background: var(--accent);
  color: var(--on-accent);
  cursor: pointer;
}

.attach-doc-preview {
  position: relative;
  display: inline-flex;
  align-items: center;
  gap: 8px;
  max-width: 100%;
  padding: 8px 28px 8px 12px;
  border-radius: 10px;
  border: 1px solid var(--border-strong);
  background: var(--bg);
}

.attach-doc-name {
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  max-width: 220px;
}

.composer-error {
  max-width: 740px;
  margin: 0 auto 8px;
  padding: 9px 14px;
  border-radius: var(--radius-md);
  background: var(--error-bg);
  color: var(--error-text);
  font-size: 13.5px;
  line-height: 1.45;
}

/* --- Lightbox --- */

body.lightbox-open {
  overflow: hidden;
}

.lightbox {
  position: fixed;
  inset: 0;
  z-index: 30;
  background: rgba(0, 0, 0, 0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: zoom-out;
  padding: 16px;
  animation: lightbox-in 0.15s ease;
}

.lightbox img {
  max-width: min(960px, 96vw);
  max-height: 92vh;
  border-radius: 10px;
  object-fit: contain;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
}

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

.hidden { display: none !important; }
