:root {
  color-scheme: light;
  --bg: #f7efe5;
  --paper: #fffaf3;
  --ink: #243335;
  --muted: #756b64;
  --line: #e9ded1;
  --teal: #29666b;
  --teal-soft: #d9ece8;
  --rose: #bd6f68;
  --rose-soft: #f4d8ce;
  --gold: #b98b42;
  --shadow: 0 18px 50px rgba(57, 42, 35, 0.14);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", sans-serif;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  background: linear-gradient(180deg, #f7efe5 0%, #fff7ec 46%, #e8f1ee 100%);
  color: var(--ink);
}

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

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  min-height: 100dvh;
  max-width: 780px;
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto 1fr auto;
  background: rgba(255, 250, 243, 0.72);
  border-left: 1px solid rgba(233, 222, 209, 0.8);
  border-right: 1px solid rgba(233, 222, 209, 0.8);
}

.topbar {
  position: sticky;
  top: 0;
  z-index: 4;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: max(14px, env(safe-area-inset-top)) 16px 12px;
  background: rgba(255, 250, 243, 0.92);
  border-bottom: 1px solid var(--line);
  backdrop-filter: blur(16px);
}

.identity {
  display: flex;
  align-items: center;
  min-width: 0;
  gap: 12px;
}

.avatar {
  width: 48px;
  height: 48px;
  flex: 0 0 48px;
  border-radius: 16px;
  box-shadow: 0 8px 20px rgba(41, 102, 107, 0.14);
  object-fit: cover;
}

.large-avatar {
  width: 72px;
  height: 72px;
  flex-basis: 72px;
  border-radius: 18px;
}

h1,
h2,
h3,
p {
  margin: 0;
}

h1 {
  font-size: 21px;
  line-height: 1.1;
  letter-spacing: 0;
}

#statusLine,
#modelLine {
  margin-top: 4px;
  color: var(--muted);
  font-size: 13px;
}

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

.icon-btn {
  width: 40px;
  height: 40px;
  display: inline-grid;
  place-items: center;
  border-radius: 8px;
  background: var(--paper);
  color: var(--teal);
  border: 1px solid var(--line);
  font-size: 20px;
}

.icon-btn:active {
  transform: translateY(1px);
}

.messages {
  overflow-y: auto;
  padding: 18px 14px 18px;
  scroll-behavior: smooth;
}

.message {
  display: flex;
  gap: 9px;
  margin-bottom: 14px;
}

.message.assistant {
  align-items: flex-start;
  justify-content: flex-start;
}

.message.user {
  justify-content: flex-end;
}

.message-main {
  display: grid;
  gap: 6px;
  max-width: min(84%, 620px);
}

.message-avatar {
  width: 32px;
  height: 32px;
  flex: 0 0 32px;
  margin-top: 4px;
  border-radius: 10px;
  object-fit: cover;
  box-shadow: 0 6px 16px rgba(41, 102, 107, 0.12);
}

.bubble {
  padding: 12px 14px;
  border-radius: 8px;
  line-height: 1.62;
  font-size: 16px;
  white-space: pre-wrap;
  word-break: break-word;
  box-shadow: 0 8px 22px rgba(57, 42, 35, 0.08);
}

.assistant .bubble {
  background: var(--paper);
  border: 1px solid var(--line);
}

.user .bubble {
  color: #fff;
  background: var(--teal);
}

.meta {
  color: var(--muted);
  font-size: 12px;
  padding: 0 4px;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  align-items: end;
  padding: 12px 14px max(12px, env(safe-area-inset-bottom));
  background: rgba(255, 250, 243, 0.94);
  border-top: 1px solid var(--line);
}

textarea {
  min-height: 44px;
  max-height: 132px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 11px 12px;
  background: #fffdf8;
  color: var(--ink);
  outline: none;
  line-height: 1.45;
}

.persona-textarea {
  min-height: 220px;
  max-height: 420px;
  width: 100%;
  resize: vertical;
  line-height: 1.58;
}

textarea:focus,
input:focus,
select:focus {
  border-color: var(--teal);
  box-shadow: 0 0 0 3px rgba(41, 102, 107, 0.12);
}

.composer button,
.settings-section button {
  min-height: 44px;
  border-radius: 8px;
  background: var(--teal);
  color: #fff;
  padding: 0 16px;
  font-weight: 650;
}

.composer button:disabled {
  opacity: 0.54;
}

.sheet {
  position: fixed;
  inset: 0;
  z-index: 10;
  display: none;
  background: rgba(36, 51, 53, 0.28);
}

.sheet.open {
  display: grid;
  place-items: end center;
}

.sheet-panel {
  width: min(780px, 100%);
  max-height: 86dvh;
  overflow: auto;
  background: var(--paper);
  border-radius: 18px 18px 0 0;
  box-shadow: var(--shadow);
  padding: 16px 16px max(18px, env(safe-area-inset-bottom));
}

.sheet-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--line);
}

h2 {
  font-size: 20px;
  letter-spacing: 0;
}

.settings-section {
  padding: 16px 0;
  border-bottom: 1px solid var(--line);
}

.settings-section:last-child {
  border-bottom: 0;
}

h3 {
  margin-bottom: 12px;
  font-size: 15px;
  color: var(--teal);
  letter-spacing: 0;
}

.switch-row,
.field {
  display: grid;
  gap: 8px;
  margin-bottom: 12px;
  color: var(--ink);
}

.switch-row {
  grid-template-columns: 1fr auto;
  align-items: center;
}

input[type="checkbox"] {
  width: 48px;
  height: 28px;
  accent-color: var(--teal);
}

input,
select {
  min-height: 42px;
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fffdf8;
  color: var(--ink);
  padding: 0 11px;
  outline: none;
}

.time-grid,
.button-row,
.memory-form {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.button-row button:last-child {
  background: var(--rose);
}

.fine-print {
  margin-top: 10px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}

.avatar-setting {
  display: flex;
  gap: 14px;
  align-items: center;
}

.avatar-controls {
  min-width: 0;
  flex: 1;
}

.avatar-controls .button-row {
  margin-bottom: 0;
}

.avatar-controls .button-row button:last-child {
  background: var(--rose);
}

.memory-list {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-bottom: 12px;
}

.memory-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  max-width: 100%;
  padding: 8px 10px;
  border-radius: 8px;
  background: var(--teal-soft);
  color: #163f43;
  font-size: 13px;
}

.memory-chip button {
  min-height: auto;
  width: 22px;
  height: 22px;
  padding: 0;
  border-radius: 7px;
  background: rgba(255, 255, 255, 0.7);
  color: var(--teal);
}

.memory-form {
  grid-template-columns: 1fr auto;
}

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(82px + env(safe-area-inset-bottom));
  z-index: 20;
  transform: translateX(-50%);
  width: min(90vw, 440px);
  padding: 11px 13px;
  border-radius: 8px;
  background: rgba(36, 51, 53, 0.94);
  color: #fff;
  box-shadow: var(--shadow);
  text-align: center;
  font-size: 14px;
}

@media (min-width: 760px) {
  .app-shell {
    min-height: calc(100dvh - 32px);
    margin-top: 16px;
    margin-bottom: 16px;
    border-radius: 18px;
    overflow: hidden;
    box-shadow: var(--shadow);
  }

  .sheet.open {
    place-items: center;
  }

  .sheet-panel {
    border-radius: 18px;
  }
}

@media (max-width: 420px) {
  .topbar {
    padding-left: 12px;
    padding-right: 12px;
  }

  .top-actions {
    gap: 6px;
  }

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

  .bubble {
    font-size: 15px;
  }

  .message-main {
    max-width: calc(100% - 46px);
  }

  .time-grid,
  .button-row {
    grid-template-columns: 1fr;
  }
}
