* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

:root {
  --bg: #f5f5f7;
  --surface: #ffffff;
  --surface-2: #eeeef2;
  --text: #17171b;
  --text-muted: #6c6c77;
  --text-faint: #98989f;
  --border: #e3e3e9;
  --border-strong: #d4d4dc;

  --accent: #4338ca;
  --accent-hover: #372ea8;
  --accent-soft: #edecfb;
  --accent-contrast: #ffffff;

  --user-bg: #17171b;
  --user-text: #f6f6f7;
  --ai-bg: #ffffff;
  --ai-text: #17171b;

  --danger: #b3261e;
  --danger-soft: #fbeceb;

  --code-bg: #f1f1f5;
  --code-text: #2a2a33;
  --code-border: #e3e3e9;

  --shadow-sm: 0 1px 2px rgba(20, 20, 30, 0.05);
  --shadow-md: 0 6px 20px rgba(20, 20, 30, 0.08);
  --shadow-lg: 0 12px 36px rgba(20, 20, 30, 0.12);

  --radius-sm: 8px;
  --radius-md: 14px;
  --radius-lg: 20px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", "Inter", Roboto, Helvetica, Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  color-scheme: light;
}

body.dark {
  --bg: #0e0f13;
  --surface: #17181f;
  --surface-2: #1d1f28;
  --text: #eeeef1;
  --text-muted: #9c9ca8;
  --text-faint: #6c6c78;
  --border: #2a2c36;
  --border-strong: #383a46;

  --accent: #8b7ef6;
  --accent-hover: #a094fa;
  --accent-soft: #23223a;
  --accent-contrast: #101018;

  --user-bg: #8b7ef6;
  --user-text: #101018;
  --ai-bg: #17181f;
  --ai-text: #eeeef1;

  --danger: #ff8f87;
  --danger-soft: #351b1c;

  --code-bg: #1b1c24;
  --code-text: #dcdce2;
  --code-border: #2c2e39;

  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.3);
  --shadow-md: 0 8px 24px rgba(0, 0, 0, 0.4);
  --shadow-lg: 0 16px 44px rgba(0, 0, 0, 0.5);
  --shadow-inset: inset 0 1px 1px rgba(255, 255, 255, 0.045), inset 0 -3px 10px rgba(0, 0, 0, 0.45);
  --shadow-inset-sm: inset 0 1px 1px rgba(255, 255, 255, 0.04), inset 0 -2px 6px rgba(0, 0, 0, 0.4);

  color-scheme: dark;
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
  }
}

html, body { height: 100%; }

body {
  font-family: var(--font-sans);
  background: var(--bg);
  color: var(--text);
  transition: background 0.25s ease, color 0.25s ease;
  -webkit-font-smoothing: antialiased;
  overflow: hidden;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  height: calc(var(--vh, 1vh) * 100);
  max-width: 760px;
  width: 100%;
  margin: 0 auto;
  background: var(--bg);
}

@supports (height: 100dvh) {
  .app {
    height: 100dvh;
  }
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 14px 16px;
  padding-top: max(14px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  z-index: 5;
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
}

.brand-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
}

.brand-clock {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  font-size: 0.68rem;
  font-weight: 500;
  letter-spacing: 0.02em;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.live-dot {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #34c759;
  box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.6);
  animation: live-pulse 1.8s ease-out infinite;
}

.clock-zone {
  color: var(--text-faint);
  opacity: 0.8;
}

@keyframes live-pulse {
  0% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0.5); }
  70% { box-shadow: 0 0 0 5px rgba(52, 199, 89, 0); }
  100% { box-shadow: 0 0 0 0 rgba(52, 199, 89, 0); }
}

.brand-name {
  font-size: 1rem;
  font-weight: 600;
  letter-spacing: -0.01em;
  color: var(--text);
}

.brand-mark {
  width: 30px;
  height: 30px;
  border-radius: 8px;
  object-fit: cover;
  flex-shrink: 0;
  box-shadow: var(--shadow-sm);
}

.header-actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

.icon-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 36px;
  height: 36px;
  border: none;
  background: transparent;
  color: var(--text-muted);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background 0.15s ease, color 0.15s ease, transform 0.1s ease;
}

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

.icon-btn:active {
  transform: scale(0.92);
}

.icon-btn.subtle {
  color: var(--text-faint);
}

.icon-btn .icon {
  width: 19px;
  height: 19px;
}

.icon-btn .icon-moon { display: none; }
body.dark .icon-btn .icon-sun { display: none; }
body.dark .icon-btn .icon-moon { display: block; }

.chat-area {
  flex: 1;
  overflow-y: auto;
  overflow-x: hidden;
  -webkit-overflow-scrolling: touch;
  padding: 20px 16px 8px;
  display: flex;
  flex-direction: column;
}

.chat-area::-webkit-scrollbar { width: 6px; }
.chat-area::-webkit-scrollbar-thumb {
  background: var(--border-strong);
  border-radius: 10px;
}

.empty-state {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 24px 12px 40px;
  gap: 6px;
  animation: fade-up 0.4s ease;
}

.empty-badge {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.5rem;
  background: radial-gradient(circle at 30% 30%, var(--accent-soft), var(--surface-2));
  border: 1px solid var(--border);
  box-shadow: var(--shadow-sm);
  margin-bottom: 14px;
  overflow: hidden;
}

.empty-badge-img {
  width: 62%;
  height: 62%;
  object-fit: contain;
}

.empty-title {
  font-size: 1.3rem;
  font-weight: 650;
  letter-spacing: -0.015em;
  color: var(--text);
}

.empty-subtitle {
  font-size: 0.92rem;
  color: var(--text-muted);
  margin-top: 4px;
  margin-bottom: 22px;
}

.suggestions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  width: 100%;
  max-width: 420px;
}

.suggestion-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px 13px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  color: var(--text);
  font-size: 0.83rem;
  font-family: inherit;
  cursor: pointer;
  text-align: left;
  transition: border-color 0.15s ease, background 0.15s ease, transform 0.1s ease;
}

body.dark .suggestion-chip {
  border-color: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow-inset-sm);
}

body.dark .empty-badge {
  border-color: rgba(255, 255, 255, 0.07);
  box-shadow: var(--shadow-inset-sm);
}

.suggestion-chip svg {
  width: 16px;
  height: 16px;
  flex-shrink: 0;
  color: var(--accent);
}

.suggestion-chip:hover {
  border-color: var(--border-strong);
  background: var(--surface-2);
}

.suggestion-chip:active {
  transform: scale(0.97);
}

.messages {
  display: flex;
  flex-direction: column;
  gap: 18px;
  padding-bottom: 12px;
}

.message-row {
  display: flex;
  flex-direction: column;
  max-width: 84%;
  animation: fade-up 0.22s ease;
}

.message-row.user {
  align-self: flex-end;
  align-items: flex-end;
}

.message-row.ai,
.message-row.error {
  align-self: flex-start;
  align-items: flex-start;
}

.message {
  padding: 10px 14px;
  border-radius: var(--radius-md);
  line-height: 1.55;
  font-size: 0.94rem;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}

.message.user {
  background: var(--user-bg);
  color: var(--user-text);
  border-bottom-right-radius: 5px;
}

.message.ai {
  background: var(--ai-bg);
  color: var(--ai-text);
  border: 1px solid var(--border);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
}

body.dark .message.ai {
  border-color: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow-inset);
}

.message.error {
  background: var(--danger-soft);
  color: var(--danger);
  border: 1px solid var(--danger);
  border-color: color-mix(in srgb, var(--danger) 35%, transparent);
  border-bottom-left-radius: 5px;
}

.message p { margin: 0 0 8px; }
.message p:last-child { margin-bottom: 0; }

.message strong { font-weight: 650; }
.message em { font-style: italic; }

.message code.inline-code {
  font-family: var(--font-mono);
  font-size: 0.85em;
  background: var(--code-bg);
  border: 1px solid var(--code-border);
  padding: 1px 5px;
  border-radius: 5px;
}

.message.user code.inline-code {
  background: rgba(255, 255, 255, 0.14);
  border-color: rgba(255, 255, 255, 0.18);
  color: inherit;
}

.code-block {
  margin: 10px 0;
  border-radius: var(--radius-sm);
  overflow: hidden;
  border: 1px solid var(--code-border);
  background: var(--code-bg);
}

body.dark .code-block {
  border-color: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow-inset-sm);
}

.code-block:first-child { margin-top: 0; }
.code-block:last-child { margin-bottom: 0; }

.code-block-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 10px;
  font-size: 0.72rem;
  font-family: var(--font-mono);
  color: var(--text-muted);
  background: var(--surface-2);
  border-bottom: 1px solid var(--code-border);
}

.code-copy-btn {
  background: none;
  border: none;
  color: var(--text-muted);
  font-size: 0.72rem;
  font-family: var(--font-sans);
  cursor: pointer;
  padding: 2px 6px;
  border-radius: 4px;
  transition: color 0.15s ease, background 0.15s ease;
}

.code-copy-btn:hover {
  color: var(--text);
  background: var(--border);
}

.code-block pre {
  margin: 0;
  padding: 10px 12px;
  overflow-x: auto;
  font-family: var(--font-mono);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--code-text);
  -webkit-overflow-scrolling: touch;
}

.message-meta {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 5px;
  padding: 0 3px;
}

.msg-time {
  font-size: 0.68rem;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.message-row.user .msg-time {
  align-self: flex-end;
  margin-top: 4px;
  padding: 0 3px;
}

.copy-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  font-size: 0.72rem;
  color: var(--text-faint);
  background: none;
  border: none;
  cursor: pointer;
  padding: 2px 0;
  transition: color 0.15s ease;
}

.copy-btn:hover { color: var(--text-muted); }
.copy-btn svg { width: 12px; height: 12px; }

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

.typing-row {
  align-self: flex-start;
  animation: fade-up 0.2s ease;
}

.typing {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 13px 16px;
  background: var(--ai-bg);
  border: 1px solid var(--border);
  border-radius: var(--radius-md);
  border-bottom-left-radius: 5px;
  box-shadow: var(--shadow-sm);
}

body.dark .typing {
  border-color: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow-inset);
}

.typing span {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--text-faint);
  animation: blink 1.1s infinite ease-in-out;
}

.typing span:nth-child(2) { animation-delay: 0.15s; }
.typing span:nth-child(3) { animation-delay: 0.3s; }

@keyframes blink {
  0%, 80%, 100% { opacity: 0.25; transform: scale(0.85); }
  40% { opacity: 1; transform: scale(1); }
}

.input-area {
  flex-shrink: 0;
  padding: 10px 14px;
  padding-bottom: max(10px, env(safe-area-inset-bottom));
  background: var(--bg);
}

.input-shell {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  padding: 8px 8px 8px 16px;
  box-shadow: var(--shadow-sm);
  transition: border-color 0.15s ease, box-shadow 0.15s ease;
}

body.dark .input-shell {
  border-color: rgba(255, 255, 255, 0.06);
  box-shadow: var(--shadow-inset);
}

body.dark .input-shell:focus-within {
  box-shadow: var(--shadow-inset), 0 0 0 3px color-mix(in srgb, var(--accent) 22%, transparent);
}

.input-shell:focus-within {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px color-mix(in srgb, var(--accent) 16%, transparent);
}

.input-shell textarea {
  flex: 1;
  resize: none;
  border: none;
  background: transparent;
  color: var(--text);
  font-size: 0.95rem;
  font-family: inherit;
  line-height: 1.4;
  max-height: 140px;
  padding: 6px 0;
}

.input-shell textarea:focus {
  outline: none;
}

.input-shell textarea::placeholder {
  color: var(--text-faint);
}

.input-shell button#sendBtn {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: none;
  background: var(--accent);
  color: var(--accent-contrast);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.1s ease, opacity 0.15s ease;
}

.input-shell button#sendBtn:hover:not(:disabled) {
  background: var(--accent-hover);
}

.input-shell button#sendBtn:active:not(:disabled) {
  transform: scale(0.92);
}

.input-shell button#sendBtn:disabled {
  background: var(--surface-2);
  color: var(--text-faint);
  cursor: not-allowed;
}

.send-icon, .spinner-icon {
  width: 17px;
  height: 17px;
}

.spinner-icon { display: none; animation: spin 0.8s linear infinite; }

#sendBtn.loading .send-icon { display: none; }
#sendBtn.loading .spinner-icon { display: block; }

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

.input-hint {
  text-align: center;
  font-size: 0.7rem;
  color: var(--text-faint);
  margin-top: 8px;
}

@media (max-width: 360px) {
  .brand-name { font-size: 0.88rem; }
  .empty-badge { width: 48px; height: 48px; font-size: 1.25rem; }
  .empty-title { font-size: 1.05rem; }
  .empty-subtitle { font-size: 0.85rem; margin-bottom: 16px; }
  .suggestion-chip { padding: 10px; font-size: 0.75rem; }
  .suggestion-chip svg { width: 14px; height: 14px; }
  .input-shell { padding: 6px 6px 6px 12px; }
}

@media (max-width: 400px) {
  .brand-name { font-size: 0.94rem; }
  .empty-title { font-size: 1.15rem; }
  .suggestions { grid-template-columns: 1fr 1fr; gap: 7px; }
  .suggestion-chip { padding: 11px; font-size: 0.79rem; }
  .message-row { max-width: 90%; }
  .input-hint { display: none; }
}

@media (max-height: 480px) and (orientation: landscape) {
  .empty-state { padding: 12px 12px 20px; gap: 4px; }
  .empty-badge { width: 40px; height: 40px; font-size: 1.1rem; margin-bottom: 8px; }
  .empty-title { font-size: 1.05rem; }
  .empty-subtitle { margin-bottom: 12px; }
  .suggestions { max-width: 520px; }
  .input-hint { display: none; }
}

@media (min-width: 601px) and (max-width: 900px) {
  .app { max-width: 640px; }
  .suggestions { grid-template-columns: 1fr 1fr; max-width: 460px; }
}

@media (min-width: 901px) {
  body {
    background: var(--surface-2);
  }
  .app {
    max-width: 820px;
    margin: 0 auto;
    box-shadow: var(--shadow-lg);
  }
}

@media (min-width: 1200px) {
  .app {
    max-width: 880px;
    min-height: 100vh;
  }
  .header { padding: 16px 24px; }
  .chat-area { padding: 28px 24px 8px; }
  .input-area { padding: 14px 24px; }
  .messages { gap: 20px; }
  .empty-badge { width: 64px; height: 64px; font-size: 1.7rem; }
  .empty-title { font-size: 1.5rem; }
  .suggestions { max-width: 480px; }
  .suggestion-chip:hover { border-color: var(--accent); }
}

@media (min-width: 760px) {
  .app {
    border-left: 1px solid var(--border);
    border-right: 1px solid var(--border);
  }
}

@media (hover: hover) and (pointer: fine) {
  .icon-btn:hover { background: var(--surface-2); }
  .code-copy-btn:hover { background: var(--border); }
}

.boot-loader {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 22px;
  background: var(--bg);
  transition: opacity 0.45s ease, visibility 0.45s ease;
}

.boot-loader.hide {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.boot-ring {
  position: relative;
  width: 84px;
  height: 84px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.boot-ring::before {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  padding: 3px;
  background: conic-gradient(from 0deg, var(--accent), transparent 65%, var(--accent));
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask-composite: exclude;
  animation: boot-spin 1.1s linear infinite;
}

.boot-ring::after {
  content: '';
  position: absolute;
  inset: 0;
  border-radius: 50%;
  box-shadow: 0 0 24px 2px color-mix(in srgb, var(--accent) 35%, transparent);
  animation: boot-pulse 1.8s ease-in-out infinite;
}

.boot-logo {
  width: 46px;
  height: 46px;
  object-fit: contain;
  border-radius: 10px;
  position: relative;
  z-index: 1;
  animation: boot-breathe 1.8s ease-in-out infinite;
}

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

@keyframes boot-pulse {
  0%, 100% { opacity: 0.35; transform: scale(0.96); }
  50% { opacity: 0.75; transform: scale(1.04); }
}

@keyframes boot-breathe {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.07); }
}

.boot-text {
  font-size: 0.85rem;
  font-weight: 500;
  color: var(--text-muted);
  letter-spacing: 0.01em;
  display: flex;
  align-items: center;
}

.boot-dots span {
  animation: boot-dot 1.4s infinite;
  opacity: 0;
}

.boot-dots span:nth-child(1) { animation-delay: 0s; }
.boot-dots span:nth-child(2) { animation-delay: 0.2s; }
.boot-dots span:nth-child(3) { animation-delay: 0.4s; }

@keyframes boot-dot {
  0%, 80%, 100% { opacity: 0; }
  40% { opacity: 1; }
}

.pull-refresh {
  position: absolute;
  top: -46px;
  left: 50%;
  transform: translate(-50%, 0px);
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--surface);
  border: 1px solid var(--border);
  box-shadow: var(--shadow-md);
  color: var(--text-faint);
  opacity: 0;
  z-index: 4;
  transition: opacity 0.2s ease, color 0.2s ease, background 0.2s ease;
  pointer-events: none;
}

.pull-refresh.visible {
  opacity: 1;
}

.pull-refresh.ready {
  color: var(--accent);
}

.pull-refresh svg {
  width: 16px;
  height: 16px;
  transition: transform 0.15s ease;
}

.pull-refresh.spinning svg {
  animation: boot-spin 0.7s linear infinite;
}

.chat-area {
  position: relative;
}
