:root {
  color-scheme: light;
  --bg: #eef1ec;
  --panel: #f8f8f5;
  --ink: #24231f;
  --muted: #7c8179;
  --line: #deded8;
  --accent: #6d8f68;
  --accent-dark: #557650;
  --assistant: #ffffff;
  --user: #d9efcf;
  --error: #a33a35;
  --shadow: 0 18px 48px rgba(37, 42, 34, 0.1);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: linear-gradient(135deg, #eef1ec 0%, #f7f4ee 100%);
  color: var(--ink);
}

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

.app-shell {
  display: block;
  width: min(720px, calc(100vw - 32px));
  height: calc(100vh - 32px);
  margin: 16px auto;
}

.chat-panel,
.side-panel {
  border: 1px solid var(--line);
  background: rgba(255, 250, 243, 0.86);
  box-shadow: var(--shadow);
}

.chat-panel {
  display: grid;
  grid-template-rows: auto 1fr auto;
  min-height: 0;
  border-radius: 8px;
  overflow: hidden;
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  padding: 12px 16px;
  border-bottom: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.62);
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 20px;
  line-height: 1.2;
  letter-spacing: 0;
}

h2 {
  font-size: 15px;
  line-height: 1.3;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.chat-identity {
  display: flex;
  align-items: center;
  min-width: 0;
}

.profile-avatar {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  object-fit: cover;
  object-position: 58% 32%;
  border: 1px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 2px 8px rgba(37, 42, 34, 0.14);
}

.topbar-actions {
  display: flex;
  gap: 8px;
}

.icon-button,
.composer button,
.message-actions button {
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}

.icon-button {
  min-width: 44px;
  height: 36px;
  color: var(--muted);
}

.icon-button.subtle {
  font-weight: 700;
  letter-spacing: 0;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  padding: 18px 16px;
}

.message-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0 0 14px;
}

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

.message-avatar {
  width: 34px;
  height: 34px;
  flex: 0 0 34px;
  border-radius: 50%;
  object-fit: cover;
  object-position: 58% 32%;
  border: 1px solid rgba(255, 255, 255, 0.9);
}

.message {
  width: fit-content;
  max-width: min(62ch, 78%);
  padding: 10px 13px;
  border: 0;
  border-radius: 15px;
  line-height: 1.58;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  box-shadow: 0 1px 2px rgba(37, 42, 34, 0.06);
}

.message.assistant {
  background: var(--assistant);
  border-bottom-left-radius: 5px;
}

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

.message.error {
  background: #fff2f1;
  color: var(--error);
  border-color: #edc5c1;
}

.message.pending {
  color: var(--muted);
}

.message-actions {
  display: flex;
  justify-content: flex-end;
  margin-top: 8px;
}

.message-actions button {
  min-height: 30px;
  padding: 0 10px;
  color: var(--accent-dark);
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
  padding: 12px;
  border-top: 1px solid var(--line);
  background: rgba(255, 255, 255, 0.72);
}

.composer textarea {
  min-height: 46px;
  max-height: 160px;
  resize: none;
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 12px 13px;
  outline: none;
  background: #fff;
}

.composer textarea:focus,
.side-panel input:focus,
.side-panel select:focus,
.side-panel textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(47, 139, 123, 0.13);
}

.composer button {
  min-width: 76px;
  height: 46px;
  border-color: var(--accent);
  background: var(--accent);
  color: #fff;
  border-radius: 18px;
}

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

.side-panel {
  display: none;
  position: fixed;
  top: 16px;
  right: 16px;
  z-index: 20;
  width: min(360px, calc(100vw - 32px));
  max-height: calc(100vh - 32px);
  min-height: 0;
  overflow-y: auto;
  border-radius: 8px;
  padding: 16px;
}

.side-panel.open {
  display: block;
}

.panel-backdrop {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 15;
  border: 0;
  background: rgba(26, 28, 24, 0.18);
  cursor: default;
}

.panel-backdrop.open {
  display: block;
}

.panel-close {
  position: sticky;
  top: 0;
  float: right;
  width: 34px;
  height: 34px;
  margin: -4px -4px 8px 8px;
  border: 1px solid var(--line);
  border-radius: 50%;
  background: #fff;
  color: var(--muted);
  cursor: pointer;
  font-size: 20px;
  line-height: 1;
}

.side-section + .side-section {
  margin-top: 18px;
  padding-top: 18px;
  border-top: 1px solid var(--line);
}

.persona-input {
  width: 100%;
  height: 42vh;
  min-height: 260px;
  margin-top: 10px;
  resize: vertical;
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px;
  background: #fff;
  line-height: 1.5;
}

.settings-grid {
  display: grid;
  gap: 12px;
}

.settings-grid label {
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 13px;
}

.settings-grid input,
.settings-grid select {
  width: 100%;
  height: 38px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 10px;
  background: #fff;
  color: var(--ink);
}

.local-key {
  display: none;
}

.show-local .local-key {
  display: grid;
}

@media (max-width: 860px) {
  .app-shell {
    height: auto;
    min-height: calc(100vh - 24px);
    width: min(100vw - 20px, 720px);
    margin: 10px auto;
  }

  .chat-panel {
    height: calc(100vh - 20px);
  }

  .side-panel.open {
    top: 10px;
    right: 10px;
    min-height: 70vh;
  }
}
