:root {
  --bg: #F5F4EE;
  --bg-elevated: #FAF9F5;
  --card: #FFFEFA;
  --text: #1F1E1D;
  --muted: #6B6860;
  --border: #E6E2D8;
  --border-soft: #EFEBE3;
  --accent: #D97757;
  --accent-deep: #C96442;
  --accent-soft: rgba(217, 119, 87, 0.12);
  --user-bg: #EDE9E0;
  --err: #B54A3A;
  --radius: 18px;
  --radius-sm: 12px;
  --safe-b: env(safe-area-inset-bottom, 0px);
  --serif: Georgia, "Songti SC", "Noto Serif SC", "Source Han Serif SC", "SimSun", serif;
  --sans: -apple-system, BlinkMacSystemFont, "PingFang SC", "Noto Sans SC", "Segoe UI", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, "PingFang SC", monospace;
  --shadow: 0 1px 2px rgba(31, 30, 29, 0.04), 0 8px 24px rgba(31, 30, 29, 0.04);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #262624;
    --bg-elevated: #2C2C2A;
    --card: #30302E;
    --text: #F5F4EE;
    --muted: #A29E94;
    --border: #3D3C38;
    --border-soft: #353430;
    --accent: #D97757;
    --accent-deep: #E08A6C;
    --accent-soft: rgba(217, 119, 87, 0.18);
    --user-bg: #3A3935;
    --err: #E07A6A;
    --shadow: 0 1px 2px rgba(0,0,0,0.2), 0 8px 24px rgba(0,0,0,0.25);
  }
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  padding: 0;
  height: 100%;
  width: 100%;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: var(--sans);
  font-size: 18px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}
#app {
  max-width: 560px;
  margin: 0 auto;
  min-height: 100%;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  width: 100%;
  overflow-x: hidden;
  box-sizing: border-box;
}
.mono { font-family: var(--mono); font-size: 0.82rem; letter-spacing: 0.01em; }

.top {
  padding: 16px 16px 12px 20px;
  background: var(--bg);
  border-bottom: 1px solid var(--border-soft);
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; flex: 1; }
.new-chat-btn {
  flex-shrink: 0;
  border: 1px solid var(--border);
  background: var(--card);
  color: var(--muted);
  font-family: var(--sans);
  font-size: 0.88rem;
  font-weight: 600;
  padding: 8px 12px;
  border-radius: 999px;
  cursor: pointer;
}
.new-chat-btn:active { border-color: var(--accent); color: var(--accent-deep); }
.brand-mark {
  color: var(--accent);
  font-size: 1.35rem;
  line-height: 1;
  animation: pulse-mark 2.4s ease-in-out infinite;
}
.top h1 {
  margin: 0;
  font-family: var(--serif);
  font-size: 1.35rem;
  font-weight: 600;
  letter-spacing: 0.01em;
}
.sub {
  margin: 2px 0 0;
  color: var(--muted);
}

.panel { padding: 28px 20px; flex: 1; display: flex; align-items: flex-start; }
.login-card {
  width: 100%;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 28px 22px;
  box-shadow: var(--shadow);
  animation: fade-up 0.45s ease both;
}
.login-title {
  font-family: var(--serif);
  font-size: 1.55rem;
  margin: 0 0 8px;
  font-weight: 600;
}
.hint { color: var(--muted); margin: 0 0 18px; line-height: 1.5; }
.big-input {
  width: 100%;
  font-size: 1.12rem;
  padding: 14px 16px;
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
  margin: 0 0 14px;
  background: var(--bg-elevated);
  color: var(--text);
  font-family: var(--sans);
}
.big-input:focus {
  outline: none;
  border-color: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.btn {
  border: none;
  border-radius: 999px;
  font-size: 1.08rem;
  padding: 14px 20px;
  cursor: pointer;
  width: 100%;
  font-weight: 600;
  font-family: var(--sans);
}
.btn.primary {
  background: var(--accent);
  color: #fff;
}
.btn.primary:active { background: var(--accent-deep); }
.btn:disabled, .send-fab:disabled { opacity: 0.5; cursor: wait; }
.err { color: var(--err); font-size: 0.95rem; margin: 12px 0 0; }
.hidden { display: none !important; }

.chat {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.messages {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 18px 18px 8px;
  display: flex;
  flex-direction: column;
  gap: 18px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
}

/* Messages */
.msg {
  animation: fade-up 0.4s ease both;
  max-width: 100%;
  min-width: 0;
  width: 100%;
  box-sizing: border-box;
}
.msg.user {
  align-self: flex-end;
  width: auto;
  max-width: min(88%, 100%);
  min-width: 0;
  background: var(--user-bg);
  color: var(--text);
  padding: 12px 16px;
  border-radius: 18px 18px 6px 18px;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  box-sizing: border-box;
  box-shadow: 0 1px 0 rgba(31,30,29,0.03);
}
.msg.bot {
  align-self: stretch;
  display: grid;
  grid-template-columns: 14px minmax(0, 1fr);
  gap: 10px;
  align-items: start;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
}
.msg.bot .accent {
  width: 8px;
  height: 8px;
  margin-top: 0.55em;
  border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 3px var(--accent-soft);
}
.msg.bot .body {
  font-family: var(--serif);
  font-size: 1.08rem;
  line-height: 1.65;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  word-break: break-word;
  color: var(--text);
  min-width: 0;
  max-width: 100%;
  box-sizing: border-box;
}
.thumb {
  display: block;
  max-width: 180px;
  border-radius: 12px;
  margin-bottom: 8px;
  border: 1px solid var(--border);
}

/* File card */
.file-card {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-top: 14px;
  padding: 12px 14px;
  width: 100%;
  max-width: 100%;
  min-width: 0;
  box-sizing: border-box;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 14px;
  text-decoration: none;
  color: var(--text);
  box-shadow: var(--shadow);
  transition: border-color 0.15s ease, transform 0.15s ease;
  overflow: hidden;
}
.file-card:active { transform: scale(0.99); border-color: var(--accent); }
.file-ico {
  width: 40px;
  height: 40px;
  border-radius: 10px;
  background: var(--accent-soft);
  color: var(--accent-deep);
  display: grid;
  place-items: center;
  font-size: 1.1rem;
  flex-shrink: 0;
}
.file-meta { min-width: 0; flex: 1 1 auto; overflow: hidden; max-width: 100%; }
.file-name {
  display: block;
  font-family: var(--sans);
  font-weight: 600;
  font-size: 0.98rem;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
  min-width: 0;
}
.file-ico, .file-arrow { flex-shrink: 0; }
.file-sub { color: var(--muted); font-size: 0.82rem; margin-top: 2px; font-family: var(--mono); }
.file-arrow { color: var(--accent); font-size: 1.1rem; }

/* Thinking / spinner */
.thinking {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 4px 22px 12px;
  color: var(--muted);
  min-height: 36px;
}
.spin-glyph {
  color: var(--accent);
  font-size: 1.15rem;
  width: 1.2em;
  text-align: center;
  display: inline-block;
}
.shimmer-word {
  font-family: var(--serif);
  font-size: 1.02rem;
  background: linear-gradient(
    90deg,
    var(--muted) 0%,
    var(--accent) 35%,
    var(--text) 50%,
    var(--accent) 65%,
    var(--muted) 100%
  );
  background-size: 220% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  animation: shimmer 2.2s linear infinite;
}
.elapsed { color: var(--muted); opacity: 0.85; }

/* Composer */
.composer-wrap {
  padding: 8px 14px calc(12px + var(--safe-b));
  background: linear-gradient(to top, var(--bg) 70%, transparent);
}
.composer.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 10px 12px 12px;
  box-shadow: var(--shadow);
}
.tools {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
  padding: 0 4px;
}
.tool-btn {
  font-size: 0.9rem;
  background: var(--bg-elevated);
  border: 1px solid var(--border-soft);
  padding: 7px 12px;
  border-radius: 999px;
  cursor: pointer;
  color: var(--muted);
  font-weight: 550;
}
.tool-btn:active { border-color: var(--accent); color: var(--accent-deep); }
.attach-name {
  font-size: 0.82rem;
  color: var(--muted);
  max-width: 42%;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-family: var(--mono);
}
.composer-row {
  display: flex;
  align-items: flex-end;
  gap: 10px;
}
.composer-input {
  flex: 1;
  border: none;
  resize: none;
  min-height: 52px;
  max-height: 140px;
  padding: 10px 8px;
  font-size: 1.08rem;
  font-family: var(--sans);
  background: transparent;
  color: var(--text);
  line-height: 1.45;
}
.composer-input:focus { outline: none; }
.composer-input::placeholder { color: var(--muted); opacity: 0.75; }
.send-fab {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: #fff;
  display: grid;
  place-items: center;
  cursor: pointer;
  flex-shrink: 0;
  margin-bottom: 2px;
  transition: background 0.15s ease, transform 0.15s ease;
}
.send-fab:active { background: var(--accent-deep); transform: scale(0.96); }

@keyframes fade-up {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: translateY(0); }
}
@keyframes shimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}
@keyframes pulse-mark {
  0%, 100% { opacity: 0.7; transform: scale(1); }
  50% { opacity: 1; transform: scale(1.08); }
}

@media (prefers-reduced-motion: reduce) {
  .msg, .login-card { animation: none; }
  .shimmer-word {
    animation: none;
    color: var(--muted);
    background: none;
    -webkit-background-clip: unset;
    background-clip: unset;
  }
  .brand-mark { animation: none; }
  .spin-glyph { animation: none; }
}
