:root {
  color-scheme: light;
  --bg: #f4efe7;
  --panel: #fffaf2;
  --ink: #23211e;
  --muted: #756f66;
  --line: #ded4c4;
  --accent: #166b68;
  --accent-dark: #104b4a;
  --assistant: #ffffff;
  --user: #e4f0ed;
  --button-bg: #ffffff;
  --composer-bg: rgba(255, 255, 255, 0.86);
  --fade-rgb: 244, 239, 231;
  --shadow: rgba(60, 45, 24, 0.04);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #151716;
  --panel: #1d211f;
  --ink: #f0eee8;
  --muted: #aaa59a;
  --line: #3a403c;
  --accent: #6dc7be;
  --accent-dark: #55ada5;
  --assistant: #242926;
  --user: #193735;
  --button-bg: #282e2b;
  --composer-bg: rgba(38, 42, 39, 0.86);
  --fade-rgb: 21, 23, 22;
  --shadow: rgba(0, 0, 0, 0.22);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100dvh;
  overflow-y: auto;
  overflow-x: hidden;
  background: var(--bg);
  color: var(--ink);
}

html {
  min-height: 100%;
  overflow-x: hidden;
}

button,
textarea {
  font: inherit;
}

.app {
  width: 100%;
  min-height: 100dvh;
  margin: 0 auto;
  background: var(--bg);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  min-height: 48px;
  padding: 8px clamp(16px, 4vw, 32px);
  border-bottom: 1px solid var(--line);
  background: color-mix(in srgb, var(--bg) 88%, transparent);
  backdrop-filter: blur(18px);
}

.topbar h1 {
  margin: 0;
  font-size: clamp(17px, 2.4vw, 21px);
  font-weight: 750;
  letter-spacing: 0;
}

.topbar-actions {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.icon-button {
  display: inline-grid;
  place-items: center;
  width: 42px;
  height: 32px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--button-bg);
  color: var(--muted);
  cursor: pointer;
  font-size: 25px;
  line-height: 1;
}

.icon-button:hover {
  color: var(--ink);
  border-color: var(--accent);
}

.icon-button svg {
  width: 19px;
  height: 19px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

:root[data-theme="dark"] .theme-moon,
:root:not([data-theme="dark"]) .theme-sun {
  display: none;
}

.chat-scroll {
  position: relative;
  min-height: calc(100dvh - 48px);
}

.chat-scroll::after {
  content: "";
  position: fixed;
  left: 0;
  right: 0;
  z-index: 10;
  pointer-events: none;
}

.chat-scroll::after {
  bottom: 0;
  height: 118px;
  background: linear-gradient(
    to top,
    rgba(var(--fade-rgb), 1) 0,
    rgba(var(--fade-rgb), 0.94) 28%,
    rgba(var(--fade-rgb), 0.56) 62%,
    rgba(var(--fade-rgb), 0)
  );
}

.messages {
  width: min(860px, calc(100% - 40px));
  min-height: calc(100dvh - 48px);
  margin: 0 auto;
  padding: 24px 0 172px;
  display: flex;
  flex-direction: column;
  gap: 16px;
  scroll-behavior: smooth;
}

.message {
  position: relative;
  max-width: 860px;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 14px 52px 14px 14px;
  background: var(--assistant);
  box-shadow: 0 1px 0 var(--shadow);
}

.message.user {
  align-self: flex-end;
  max-width: min(78%, 520px);
  background: var(--user);
}

.message.assistant {
  align-self: flex-start;
  width: 100%;
}

.message-role {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.message-content {
  white-space: pre-wrap;
  line-height: 1.5;
  overflow-wrap: anywhere;
}

.message.streaming .message-content::after {
  content: "";
  display: inline-block;
  width: 7px;
  height: 1.1em;
  margin-left: 3px;
  vertical-align: -0.18em;
  background: var(--accent);
  animation: cursor-blink 0.9s steps(1) infinite;
}

@keyframes cursor-blink {
  50% {
    opacity: 0;
  }
}

.copy-button {
  position: absolute;
  top: 8px;
  right: 8px;
  display: inline-grid;
  place-items: center;
  width: 34px;
  height: 34px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--button-bg);
  color: var(--accent);
  cursor: pointer;
  padding: 0;
}

.message.user .copy-button {
  display: none;
}

.copy-button:hover {
  border-color: var(--accent);
}

.copy-button.copied {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--panel);
}

.copy-button svg,
.composer button svg {
  width: 18px;
  height: 18px;
  fill: none;
  stroke: currentColor;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-width: 2;
}

.composer {
  position: fixed;
  left: 50%;
  transform: translateX(-50%);
  bottom: 0;
  z-index: 30;
  width: min(770px, calc(100% - 40px));
  margin: 0;
  padding: 0 0 max(20px, env(safe-area-inset-bottom));
  background: transparent;
}

.composer textarea {
  width: 100%;
  min-height: 46px;
  max-height: 180px;
  resize: none;
  overflow-y: auto;
  border: 1px solid var(--line);
  border-radius: 20px;
  padding: 12px 58px 12px 12px;
  background: var(--composer-bg);
  color: var(--ink);
  line-height: 1.4;
  font-size: 16px;
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.18);
  backdrop-filter: blur(18px);
}

.composer textarea:focus {
  outline: 2px solid rgba(22, 107, 104, 0.22);
  border-color: var(--accent);
}

.composer button {
  position: absolute;
  right: 10px;
  bottom: calc(max(20px, env(safe-area-inset-bottom)) + 8px);
  display: inline-grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 14px;
  background: var(--accent);
  color: #fff;
  cursor: pointer;
  padding: 0;
}

.composer button:hover {
  background: var(--accent-dark);
}

.composer button:disabled {
  cursor: wait;
  opacity: 0.7;
}

.empty {
  margin: auto;
  max-width: 620px;
  color: var(--muted);
  text-align: center;
  line-height: 1.5;
}

@media (max-width: 640px) {
  .app {
    width: 100%;
  }

  .topbar {
    min-height: 44px;
    padding: 6px 12px;
  }

  .topbar h1 {
    font-size: 17px;
  }

  .icon-button {
    width: 36px;
    height: 32px;
  }

  .chat-scroll {
    min-height: calc(100dvh - 44px);
  }

  .chat-scroll::after {
    height: 104px;
  }

  .messages {
    width: calc(100% - 24px);
    min-height: calc(100dvh - 44px);
    padding: 18px 0 148px;
    gap: 12px;
  }

  .message {
    max-width: 100%;
    padding: 12px 48px 12px 12px;
  }

  .message.user {
    max-width: 88%;
  }

  .composer {
    width: calc(100% - 24px);
    padding: 0 0 max(12px, env(safe-area-inset-bottom));
  }

  .composer textarea {
    min-height: 46px;
    max-height: 150px;
  }

  .composer button {
    right: 8px;
    bottom: calc(max(12px, env(safe-area-inset-bottom)) + 8px);
    width: 40px;
    height: 40px;
  }
}

@media (max-width: 420px) {
  .topbar h1 {
    font-size: 19px;
  }

  .message,
  .message.user {
    max-width: 100%;
  }
}
