:root {
  color-scheme: light;
  --bg: #f7f5f0;
  --surface: #fffdf8;
  --panel: #fbfaf6;
  --line: #ded8cc;
  --text: #1f1f1c;
  --muted: #77736a;
  --accent: #2f6f64;
  --accent-soft: #e5f0eb;
  --danger: #b84d4d;
  --bubble-user: #e8f0ea;
  --bubble-assistant: #ffffff;
  --shadow: 0 12px 34px rgba(45, 41, 32, 0.08);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  font-size: 16px;
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #171717;
  --surface: #232321;
  --panel: #1d1d1b;
  --line: #3a3935;
  --text: #f4f0e8;
  --muted: #aaa397;
  --accent: #8bc5b5;
  --accent-soft: #263933;
  --danger: #ff8f8f;
  --bubble-user: #263933;
  --bubble-assistant: #232321;
  --shadow: 0 12px 34px rgba(0, 0, 0, 0.24);
}

:root[data-text-size="small"] {
  font-size: 14px;
}

:root[data-text-size="medium"] {
  font-size: 16px;
}

:root[data-text-size="large"] {
  font-size: 18px;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  background: var(--bg);
  color: var(--text);
}

body {
  min-width: 320px;
}

button,
input,
textarea,
select {
  font: inherit;
}

button,
.file-button {
  align-items: center;
  border: 1px solid transparent;
  border-radius: 8px;
  background: var(--text);
  color: #fffdf8;
  cursor: pointer;
  display: inline-flex;
  font-weight: 700;
  justify-content: center;
  min-height: 40px;
  padding: 0 14px;
  text-decoration: none;
}

button.secondary,
.file-button {
  background: var(--surface);
  color: var(--text);
  border-color: var(--line);
}

button.danger {
  background: transparent;
  color: var(--danger);
  border-color: rgba(184, 77, 77, 0.35);
}

button:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.app-shell {
  display: grid;
  grid-template-columns: minmax(320px, 388px) minmax(0, 1fr);
  height: 100dvh;
  overflow: hidden;
}

.side-panel {
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow-y: auto;
  padding: max(18px, env(safe-area-inset-top)) 18px 18px;
  z-index: 5;
}

.panel-header,
.chat-header,
.toolbar,
.prompt-top,
.setting-row,
.profile-row,
.profile-actions {
  align-items: center;
  display: flex;
  gap: 10px;
}

.panel-header {
  justify-content: space-between;
  margin-bottom: 16px;
}

.eyebrow {
  color: var(--muted);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0;
  margin: 0 0 4px;
}

h1 {
  font-size: 24px;
  line-height: 1.1;
  margin: 0;
}

.tabs {
  border-bottom: 1px solid var(--line);
  display: grid;
  grid-template-columns: repeat(6, 1fr);
  gap: 2px;
  margin-bottom: 16px;
  padding-bottom: 8px;
}

.tab {
  background: transparent;
  color: var(--muted);
  min-height: 34px;
  padding: 0 4px;
}

.tab.is-active {
  background: var(--accent-soft);
  color: var(--accent);
}

.tab-panel {
  display: none;
}

.tab-panel.is-active {
  display: grid;
  gap: 14px;
}

label {
  color: var(--muted);
  display: grid;
  font-size: 13px;
  font-weight: 700;
  gap: 7px;
}

input,
textarea,
select {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
  color: var(--text);
  min-height: 42px;
  padding: 10px 12px;
  width: 100%;
}

textarea {
  line-height: 1.45;
  resize: vertical;
}

input[type="range"] {
  padding: 0;
}

.range-value {
  color: var(--accent);
  font-size: 13px;
}

.hint {
  color: var(--muted);
  font-size: 13px;
  line-height: 1.5;
  margin: 0;
}

.toolbar {
  flex-wrap: wrap;
}

.toolbar.compact {
  align-items: stretch;
}

.toolbar.compact select {
  flex: 1;
}

.profile-row {
  align-items: center;
}

.profile-actions {
  align-items: stretch;
  flex-wrap: wrap;
}

.switch {
  display: inline-flex;
  flex: 0 0 auto;
}

.switch input {
  height: 1px;
  opacity: 0;
  position: absolute;
  width: 1px;
}

.switch span {
  background: #d8d2c7;
  border-radius: 999px;
  display: block;
  height: 26px;
  position: relative;
  width: 48px;
}

.switch span::after {
  background: #fffdf8;
  border: 1px solid var(--line);
  border-radius: 999px;
  content: "";
  height: 22px;
  left: 1px;
  position: absolute;
  top: 1px;
  transition: transform 160ms ease;
  width: 22px;
}

.switch input:checked + span {
  background: var(--accent);
}

.switch input:checked + span::after {
  transform: translateX(22px);
}

.setting-row {
  justify-content: space-between;
}

.conversation-list,
.prompt-list,
.archive-list {
  display: grid;
  gap: 12px;
}

.conversation-item,
.prompt-card,
.archive-item,
.stat-grid div {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.conversation-item {
  display: grid;
  gap: 10px;
  padding: 12px;
}

.conversation-item.is-active {
  background: var(--accent-soft);
  border-color: rgba(47, 111, 100, 0.55);
}

.conversation-switch {
  align-items: start;
  background: transparent;
  border: 0;
  color: var(--text);
  display: grid;
  justify-content: stretch;
  min-height: 0;
  padding: 0;
  text-align: left;
}

.conversation-switch span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 500;
  margin-top: 4px;
}

.prompt-card,
.archive-item {
  display: grid;
  gap: 10px;
  padding: 12px;
}

.prompt-title {
  font-weight: 800;
}

.stat-grid {
  display: grid;
  gap: 10px;
  grid-template-columns: 1fr 1fr;
}

.stat-grid div {
  padding: 12px;
}

.stat-grid span,
.archive-item span {
  color: var(--muted);
  display: block;
  font-size: 12px;
}

.stat-grid strong {
  display: block;
  font-size: 28px;
  line-height: 1.1;
  margin-top: 4px;
}

.chat-view {
  background: var(--bg);
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  height: 100dvh;
  min-width: 0;
}

.chat-header {
  background: rgba(247, 245, 240, 0.92);
  border-bottom: 1px solid var(--line);
  min-height: 72px;
  padding: max(12px, env(safe-area-inset-top)) 18px 12px;
}

:root[data-theme="dark"] .chat-header {
  background: rgba(23, 23, 23, 0.92);
}

.avatar,
.message-avatar,
.avatar-preview {
  align-items: center;
  background: var(--accent-soft);
  background-position: center;
  background-size: cover;
  border: 1px solid var(--line);
  border-radius: 50%;
  color: var(--accent);
  display: grid;
  flex: 0 0 auto;
  font-weight: 900;
  height: 42px;
  place-items: center;
  width: 42px;
}

.avatar-preview {
  height: 72px;
  width: 72px;
}

.has-image {
  color: transparent;
}

.chat-title {
  display: grid;
  flex: 1;
  min-width: 0;
}

.chat-title strong {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-title span {
  color: var(--muted);
  font-size: 12px;
}

.icon-button {
  background: var(--surface);
  border-color: var(--line);
  color: var(--text);
  font-size: 22px;
  height: 42px;
  min-height: 42px;
  padding: 0;
  width: 42px;
}

.mobile-only {
  display: none;
}

.message-list {
  display: flex;
  flex-direction: column;
  gap: 14px;
  min-height: 0;
  overflow-y: auto;
  padding: 22px clamp(14px, 4vw, 48px);
}

.empty-state {
  align-self: center;
  color: var(--muted);
  line-height: 1.55;
  max-width: 560px;
  padding: 40px 16px;
  text-align: center;
}

.message {
  align-items: flex-end;
  display: flex;
  gap: 10px;
  max-width: min(760px, 92%);
}

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

.message.assistant,
.message.system {
  align-self: flex-start;
}

.message.user .message-avatar {
  background-color: #ece9e1;
}

:root[data-theme="dark"] .message.user .message-avatar {
  background-color: #302f2b;
}

.bubble {
  background: var(--bubble-assistant);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  min-width: 0;
  padding: 10px 12px;
}

.message.user .bubble {
  background: var(--bubble-user);
}

.message.system .bubble {
  background: #f0ede5;
}

:root[data-theme="dark"] .message.system .bubble {
  background: #2e2d29;
}

.message-meta {
  color: var(--muted);
  font-size: 11px;
  font-weight: 800;
  margin-bottom: 5px;
}

.message-body {
  line-height: 1.65;
  overflow-wrap: anywhere;
  white-space: pre-wrap;
}

.typing-indicator {
  align-items: center;
  display: inline-flex;
  gap: 5px;
  min-height: 20px;
}

.typing-indicator span {
  animation: typingPulse 1s infinite ease-in-out;
  background: var(--muted);
  border-radius: 999px;
  height: 6px;
  opacity: 0.45;
  width: 6px;
}

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

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

@keyframes typingPulse {
  0%,
  80%,
  100% {
    transform: translateY(0);
    opacity: 0.35;
  }
  40% {
    transform: translateY(-3px);
    opacity: 0.9;
  }
}

.message-actions {
  display: flex;
  gap: 6px;
  justify-content: flex-end;
  margin-top: 8px;
  opacity: 0;
  transition: opacity 160ms ease;
}

.message:hover .message-actions,
.message:focus-within .message-actions {
  opacity: 1;
}

.message-actions button {
  background: transparent;
  border-color: var(--line);
  color: var(--muted);
  font-size: 12px;
  min-height: 28px;
  padding: 0 8px;
}

.composer {
  align-items: end;
  background: rgba(247, 245, 240, 0.94);
  border-top: 1px solid var(--line);
  display: grid;
  gap: 10px;
  grid-template-columns: minmax(0, 1fr) auto;
  padding: 12px clamp(14px, 4vw, 48px) max(12px, env(safe-area-inset-bottom));
}

:root[data-theme="dark"] .composer {
  background: rgba(23, 23, 23, 0.94);
}

.composer textarea {
  max-height: 180px;
  min-height: 46px;
  resize: none;
}

.toast {
  background: var(--text);
  border-radius: 999px;
  bottom: max(18px, env(safe-area-inset-bottom));
  box-shadow: var(--shadow);
  color: var(--surface);
  font-size: 13px;
  font-weight: 800;
  left: 50%;
  opacity: 0;
  padding: 10px 14px;
  pointer-events: none;
  position: fixed;
  transform: translate(-50%, 12px);
  transition: opacity 160ms ease, transform 160ms ease;
  z-index: 20;
}

.toast.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

@media (max-width: 820px) {
  .app-shell {
    display: block;
  }

  .side-panel {
    bottom: 0;
    box-shadow: 20px 0 54px rgba(45, 41, 32, 0.18);
    max-width: 420px;
    position: fixed;
    top: 0;
    transform: translateX(-105%);
    transition: transform 180ms ease;
    width: min(94vw, 420px);
  }

  .side-panel.is-open {
    transform: translateX(0);
  }

  .mobile-only {
    display: inline-flex;
  }

  .chat-header,
  .message-list,
  .composer {
    padding-left: 12px;
    padding-right: 12px;
  }

  .message {
    max-width: 96%;
  }
}
