:root {
  color-scheme: light;
  --bg: #f7f7fb;
  --panel: #ffffff;
  --panel-soft: #fbfbfd;
  --line: #e5e7eb;
  --line-strong: #d5d9e2;
  --text: #111827;
  --muted: #6b7280;
  --accent: #111827;
  --accent-contrast: #ffffff;
  --accent-soft: #f3f4f6;
  --sidebar-bg: #f8fafc;
  --shadow: 0 18px 44px rgba(15, 23, 42, 0.08);
  --radius-xl: 28px;
  --radius-lg: 22px;
  --radius-md: 16px;
}

* { box-sizing: border-box; }
html, body { min-height: 100%; }
body {
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}
button, textarea, input { font: inherit; }
button { cursor: pointer; }
img { max-width: 100%; display: block; }
.hidden { display: none !important; }
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.app-layout {
  min-height: 100vh;
  display: grid;
  grid-template-columns: 280px minmax(0, 1fr);
  transition: grid-template-columns .2s ease;
}
.app-layout.is-sidebar-hidden {
  grid-template-columns: 0 minmax(0, 1fr);
}
.app-layout.is-sidebar-hidden .sidebar {
  transform: translateX(-100%);
  opacity: 0;
  pointer-events: none;
}

.sidebar {
  background: var(--sidebar-bg);
  border-right: 1px solid var(--line);
  padding: 18px 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  position: sticky;
  top: 0;
  height: 100vh;
  overflow: hidden;
  transition: transform .2s ease, opacity .2s ease;
}
.sidebar-header {
  display: flex;
  flex-direction: column;
  gap: 14px;
}
.sidebar-brand-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
}
.sidebar-collapse-btn { flex: 0 0 auto; }
.brand-wrap {
  display: flex;
  align-items: center;
  gap: 12px;
}
.brand-mark {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: linear-gradient(135deg, #2563eb, #4f46e5);
  color: #fff;
  font-weight: 800;
  font-size: 1.05rem;
}
.brand-name {
  font-size: 1.22rem;
  font-weight: 800;
  letter-spacing: -0.02em;
}
.brand-subtitle {
  margin-top: 2px;
  color: var(--muted);
  font-size: 0.92rem;
}
.new-chat-btn,
.primary-btn,
.send-btn,
.menu-btn,
.tool-menu-item,
.tool-chip,
.upload-action-btn,
.history-item,
.icon-btn {
  border: 1px solid transparent;
  transition: background-color .16s ease, border-color .16s ease, transform .12s ease;
}
.new-chat-btn {
  width: 100%;
  padding: 11px 14px;
  border-radius: 14px;
  background: #fff;
  border-color: var(--line);
  font-weight: 700;
  color: var(--text);
  text-align: left;
}
.new-chat-btn:hover,
.history-item:hover,
.icon-btn:hover,
.menu-btn:hover,
.tool-chip:hover,
.upload-action-btn:hover,
.tool-menu-item:hover {
  background: #fff;
}
.sidebar-action-row {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}
.sidebar-action-btn {
  min-height: 38px;
  border-radius: 12px;
  border: 1px solid var(--line);
  background: #fff;
  color: var(--text);
  font-weight: 700;
}
.sidebar-action-danger {
  color: #b91c1c;
  border-color: #fecaca;
  background: #fff5f5;
}
.sidebar-action-btn:disabled {
  cursor: not-allowed;
  opacity: .55;
}
.sidebar-section-title {
  font-size: 0.88rem;
  color: var(--muted);
  font-weight: 700;
  padding: 4px 4px 0;
}
.history-empty {
  color: var(--muted);
  font-size: 0.92rem;
  padding: 6px 8px 0;
}
.history-list {
  flex: 1;
  overflow: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding-right: 2px;
}
.history-item {
  width: 100%;
  border-radius: 16px;
  background: transparent;
  border-color: transparent;
  padding: 12px 12px;
  text-align: left;
}
.history-item.is-active {
  background: #fff;
  border-color: var(--line);
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.06);
}
.history-title {
  font-size: 0.95rem;
  font-weight: 700;
  line-height: 1.35;
  display: -webkit-box;
  -webkit-line-clamp: 2;
  -webkit-box-orient: vertical;
  overflow: hidden;
}
.history-meta {
  margin-top: 6px;
  font-size: 0.82rem;
  color: var(--muted);
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(15, 23, 42, 0.35);
  z-index: 30;
}

.main-shell {
  min-width: 0;
  padding: clamp(16px, 2vw, 28px);
  display: grid;
  grid-template-rows: auto auto 1fr auto auto;
  gap: clamp(10px, 1vw, 18px);
}
.desktop-sidebar-show {
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 35;
  box-shadow: 0 10px 28px rgba(15, 23, 42, 0.1);
}
.mobile-topbar {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.mobile-brand-name {
  font-size: 1rem;
  font-weight: 800;
}
.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: #fff;
  border-color: var(--line);
  color: var(--text);
}
.icon-btn svg { width: 18px; height: 18px; }

.consent-banner {
  background: #fff7ed;
  border: 1px solid #fed7aa;
  border-radius: 18px;
  padding: 14px 16px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
}
.consent-text { margin-top: 4px; color: #9a3412; font-size: .92rem; }
.primary-btn {
  border-radius: 999px;
  padding: 10px 16px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 700;
}

.empty-state {
  display: grid;
  place-items: center;
  min-height: clamp(180px, 28vh, 300px);
  padding-top: 4vh;
}
.hero-title {
  margin: 0;
  text-align: center;
  font-size: clamp(1.9rem, 3.2vw, 3.2rem);
  line-height: 1.16;
  font-weight: 800;
  letter-spacing: -0.03em;
}

.chat-list {
  min-width: 0;
  width: min(100%, 1100px);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: clamp(16px, 1.7vw, 24px);
}
.msg {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-width: 100%;
}
.msg[data-role="user"] { align-items: flex-end; }
.msg-role {
  font-size: 0.84rem;
  color: var(--muted);
  font-weight: 700;
}
.msg-body {
  display: flex;
  flex-direction: column;
  gap: 12px;
}
.msg-content {
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.8;
  font-size: clamp(0.98rem, 0.92vw, 1.05rem);
}
.msg[data-role="assistant"] .msg-content,
.msg[data-role="system"] .msg-content {
  max-width: min(100%, 920px);
}
.msg[data-role="user"] .msg-content {
  max-width: min(100%, 760px);
  background: #ffffff;
  border: 1px solid var(--line);
  border-radius: 24px;
  padding: 16px 18px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}
.msg[data-role="system"] .msg-content {
  background: #fff;
  border: 1px dashed var(--line-strong);
  border-radius: 18px;
  padding: 12px 14px;
  color: var(--muted);
}
.msg-thinking {
  max-width: min(100%, 920px);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 16px;
  padding: 10px 12px;
}
.msg-thinking summary {
  cursor: pointer;
  font-size: .9rem;
  font-weight: 700;
  color: var(--muted);
}
.msg-thinking-body {
  margin-top: 10px;
  white-space: pre-wrap;
  word-break: break-word;
  line-height: 1.75;
  font-size: .95rem;
}
.msg-image {
  width: min(100%, 600px);
  border-radius: 18px;
  border: 1px solid var(--line);
  box-shadow: var(--shadow);
}
.msg-attachment {
  max-width: min(100%, 760px);
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 12px;
  box-shadow: 0 8px 20px rgba(15, 23, 42, 0.04);
}
.msg[data-role="assistant"] .msg-attachment,
.msg[data-role="system"] .msg-attachment {
  max-width: min(100%, 920px);
}
.attachment-preview {
  border-radius: 16px;
  overflow: hidden;
  border: 1px solid var(--line);
  background: var(--panel-soft);
  margin-bottom: 10px;
}
.attachment-preview img {
  width: 100%;
  max-height: 320px;
  object-fit: contain;
  background: #fff;
}
.attachment-file-row {
  display: flex;
  align-items: center;
  gap: 12px;
}
.attachment-file-icon {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: var(--accent-soft);
  border: 1px solid var(--line);
  color: var(--text);
  flex: 0 0 auto;
}
.attachment-file-icon svg { width: 18px; height: 18px; }
.attachment-file-main { min-width: 0; }
.attachment-file-name {
  font-weight: 700;
  line-height: 1.4;
  word-break: break-word;
}
.attachment-file-meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: .84rem;
}
.msg-meta {
  font-size: 0.82rem;
  color: var(--muted);
}

.upload-tray {
  width: min(100%, 1100px);
  margin: 0 auto;
  background: rgba(255,255,255,.92);
  border: 1px solid var(--line);
  border-radius: 22px;
  padding: 12px 14px;
  box-shadow: 0 10px 24px rgba(15, 23, 42, 0.06);
  backdrop-filter: blur(8px);
}
.tray-header {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 10px;
}
.tray-header span:first-child { font-weight: 700; }
.tray-hint { color: var(--muted); font-size: .86rem; }
.upload-list {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}
.upload-item {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: min(100%, 320px);
  max-width: 100%;
  padding: 10px 12px;
  border-radius: 18px;
  background: var(--panel-soft);
  border: 1px solid var(--line);
}
.upload-item.is-failed { border-color: #fca5a5; background: #fef2f2; }
.upload-item-main { min-width: 0; }
.upload-name {
  font-size: .94rem;
  font-weight: 700;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.upload-state { font-size: .82rem; color: var(--muted); }
.upload-actions {
  margin-left: auto;
  display: flex;
  align-items: center;
  gap: 8px;
}
.upload-action-btn {
  background: #fff;
  border-color: var(--line);
  border-radius: 999px;
  padding: 8px 10px;
  font-size: .88rem;
  color: var(--text);
}

.composer-shell {
  position: sticky;
  bottom: 0;
  z-index: 15;
  width: min(100%, 1100px);
  margin: 0 auto;
  padding-bottom: max(6px, env(safe-area-inset-bottom));
}
.composer-surface {
  background: rgba(255,255,255,.96);
  border: 1px solid var(--line);
  border-radius: 30px;
  box-shadow: var(--shadow);
  backdrop-filter: blur(12px);
  padding: 12px;
}
.composer-topline {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  min-height: 34px;
  margin-bottom: 8px;
}
.tool-chip-wrap { display: flex; align-items: center; gap: 10px; }
.tool-chip {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  padding: 8px 12px;
  border-radius: 999px;
  background: var(--accent-soft);
  border-color: var(--line);
  color: var(--text);
}
.tool-chip-close { font-size: 1rem; line-height: 1; }
.chat-status {
  margin-left: auto;
  color: var(--muted);
  font-size: 0.9rem;
}
#chatInput {
  width: 100%;
  resize: none;
  min-height: clamp(88px, 10vw, 124px);
  max-height: 260px;
  border: 0;
  outline: 0;
  background: transparent;
  color: var(--text);
  font-size: clamp(1rem, 0.96vw, 1.08rem);
  line-height: 1.7;
  padding: 4px 2px 12px;
}
.composer-bottom {
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 14px;
}
.composer-hint {
  color: var(--muted);
  font-size: 0.9rem;
  min-height: 22px;
}
.composer-actions-right {
  display: flex;
  align-items: center;
  gap: 10px;
}
.menu-wrap { position: relative; }
.menu-btn {
  width: 46px;
  height: 46px;
  border-radius: 999px;
  display: grid;
  place-items: center;
  background: #fff;
  border-color: var(--line);
  color: var(--text);
}
.menu-plus { font-size: 1.45rem; line-height: 1; }
.tool-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 10px);
  min-width: 240px;
  background: #fff;
  border: 1px solid var(--line);
  border-radius: 18px;
  box-shadow: 0 18px 44px rgba(17,24,39,.14);
  padding: 8px;
}
.tool-menu-item {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px 12px;
  background: #fff;
  border-radius: 14px;
  border-color: transparent;
  color: var(--text);
  text-align: left;
}
.tool-menu-item[aria-checked="true"] {
  background: var(--accent-soft);
  border-color: var(--line);
}
.tool-icon { width: 18px; height: 18px; }
.tool-icon svg { width: 18px; height: 18px; }
.send-btn {
  min-width: 84px;
  height: 46px;
  border-radius: 999px;
  padding: 0 16px;
  background: var(--accent);
  color: var(--accent-contrast);
  font-weight: 800;
}
.send-btn:disabled,
.menu-btn:disabled,
.new-chat-btn:disabled { opacity: 0.55; cursor: not-allowed; }

.hidden-file-input { position: absolute; left: -9999px; width: 1px; height: 1px; }

@media (max-width: 980px) {
  .app-layout,
  .app-layout.is-sidebar-hidden { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    z-index: 40;
    left: 0;
    top: 0;
    width: min(82vw, 320px);
    transform: translateX(-100%);
    transition: transform .2s ease;
    opacity: 1;
    pointer-events: auto;
  }
  .sidebar.is-open { transform: translateX(0); }
  .main-shell { padding-top: 12px; }
  .mobile-topbar { display: flex; }
  .desktop-sidebar-show,
  .sidebar-collapse-btn { display: none !important; }
}

@media (min-width: 981px) {
  .sidebar-backdrop,
  .mobile-topbar { display: none !important; }
}

@media (max-width: 720px) {
  .main-shell { padding: 12px; }
  .hero-title { font-size: 2rem; }
  .composer-surface { border-radius: 24px; }
  .composer-bottom { align-items: center; }
  .composer-actions-right { width: auto; }
  .send-btn { min-width: 72px; }
  .consent-banner {
    flex-direction: column;
    align-items: stretch;
  }
  .upload-item { min-width: 100%; }
}

@media (min-width: 1600px) {
  .app-layout { grid-template-columns: 320px minmax(0, 1fr); }
  .main-shell { padding: 28px 34px; }
  .chat-list,
  .upload-tray,
  .composer-shell { width: min(100%, 1280px); }
  #chatInput { min-height: 134px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
}
