:root {
  --bg: #f6f3ec;
  --sidebar: #ece7dc;
  --surface: #fffdf9;
  --ink: #2e2923;
  --muted: #746c60;
  --line: #d8d0c2;
  --assistant: #f3eee4;
  --user: #faf7f1;
  --primary: #26231f;
  --primary-2: #171512;
  --danger: #9f4d17;
}

body[data-theme="dark"] {
  --bg: #15130f;
  --sidebar: #1e1a15;
  --surface: #201b15;
  --ink: #ebe2d3;
  --muted: #b8ab96;
  --line: #3b3328;
  --assistant: #262017;
  --user: #1f1a13;
  --primary: #e8dbc6;
  --primary-2: #f3e8d7;
  --danger: #f08a55;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body[data-theme="dark"] {
  background:
    radial-gradient(circle at 18% 0%, rgba(69, 56, 41, 0.72) 0, rgba(69, 56, 41, 0) 33%),
    radial-gradient(circle at 90% 8%, rgba(60, 46, 33, 0.7) 0, rgba(60, 46, 33, 0) 27%),
    linear-gradient(180deg, #1a1611 0%, var(--bg) 100%);
}

body {
  margin: 0;
  overflow: hidden;
  color: var(--ink);
  background:
    radial-gradient(circle at 18% 0%, rgba(255, 247, 227, 0.9) 0, rgba(255, 247, 227, 0) 33%),
    radial-gradient(circle at 90% 8%, rgba(247, 234, 207, 0.8) 0, rgba(247, 234, 207, 0) 27%),
    linear-gradient(180deg, #faf7f0 0%, var(--bg) 100%);
  font-family: "IBM Plex Sans", sans-serif;
}

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

.layout {
  height: 100vh;
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
}

.sidebar {
  min-height: 0;
  overflow: hidden;
  border-right: 1px solid var(--line);
  background: linear-gradient(180deg, rgba(244, 239, 229, 0.98) 0%, var(--sidebar) 100%);
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.9rem;
}

body[data-theme="dark"] .sidebar {
  background: linear-gradient(180deg, rgba(35, 29, 22, 0.98) 0%, #1d1813 100%);
}

.sidebar-top {
  display: grid;
  gap: 0.75rem;
}

.brand {
  font-family: "Source Serif 4", serif;
  font-weight: 600;
  font-size: 1.4rem;
}

.history-list {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: grid;
  gap: 0.55rem;
  padding-right: 0.2rem;
}

.history-item {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.48);
  padding: 0.72rem 0.78rem;
  display: block;
  text-align: left;
  cursor: pointer;
}

body[data-theme="dark"] .history-item {
  background: rgba(45, 37, 28, 0.55);
}

.history-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.45rem;
}

.history-item:hover {
  background: rgba(255, 255, 255, 0.76);
}

body[data-theme="dark"] .history-item:hover {
  background: rgba(58, 48, 36, 0.72);
}

.history-item.active {
  border-color: #c8baa6;
  background: #fffdf8;
}

body[data-theme="dark"] .history-item.active {
  border-color: #72624d;
  background: #332a20;
}

.history-title {
  font-size: 0.94rem;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.history-actions {
  display: flex;
  gap: 0.34rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity 120ms ease;
}

.history-item:hover .history-actions,
.history-item:focus-within .history-actions,
.history-item.active .history-actions {
  opacity: 1;
  pointer-events: auto;
}

.history-actions button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--line);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.9);
  padding: 0.34rem 0.42rem;
}

body[data-theme="dark"] .history-actions button {
  border-color: #594b3a;
  background: rgba(43, 35, 27, 0.96);
}

.chat-shell {
  min-height: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: rgba(255, 253, 249, 0.9);
}

body[data-theme="dark"] .chat-shell {
  background: rgba(24, 20, 16, 0.9);
}

.topbar {
  border-bottom: 1px solid var(--line);
  padding: 0.9rem 1rem;
  background: rgba(255, 253, 249, 0.86);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 0.85rem;
}

body[data-theme="dark"] .topbar {
  background: rgba(26, 22, 17, 0.88);
}

body[data-theme="dark"] .topbar,
body[data-theme="dark"] .sidebar {
  color: var(--ink);
}

.title-wrap {
  min-width: 0;
}

h1 {
  margin: 0;
  font-size: 1.07rem;
  font-weight: 600;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

#status {
  display: inline-block;
  margin-top: 0.2rem;
  color: var(--muted);
  font-size: 0.78rem;
}

#status.busy {
  color: #c87014;
}

#status.error {
  color: var(--danger);
}

.controls-wrap {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: flex-end;
  gap: 0.5rem;
}

#signin-btn {
  height: 2.7rem;
  border-radius: 999px;
  padding: 0 1rem;
}

.profile-wrap {
  position: relative;
}

.profile-menu {
  position: absolute;
  top: calc(100% + 0.4rem);
  right: 0;
  min-width: 180px;
  border: 1px solid var(--line);
  border-radius: 14px;
  background: #fffdfa;
  box-shadow: 0 10px 24px rgba(42, 33, 23, 0.12);
  padding: 0.45rem;
  display: grid;
  gap: 0.28rem;
  z-index: 20;
}

body[data-theme="dark"] .profile-menu {
  background: #211b14;
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.45);
}

.profile-status {
  color: var(--muted);
  font-size: 0.8rem;
  padding: 0.34rem 0.45rem;
}

.profile-action {
  border: 1px solid transparent;
  border-radius: 10px;
  background: transparent;
  padding: 0.44rem 0.5rem;
  text-align: left;
  cursor: pointer;
}

.profile-action:hover {
  border-color: var(--line);
  background: #fff;
}

body[data-theme="dark"] .profile-action:hover {
  background: #2c241b;
}

.messages {
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 1.15rem 1.2rem 1.3rem;
  display: grid;
  gap: 0.9rem;
  align-content: start;
}

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

.message {
  max-width: 860px;
  width: min(100%, 860px);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 0.9rem 1rem;
  line-height: 1.58;
}

.message.user {
  justify-self: end;
  background: var(--user);
}

.message.assistant {
  justify-self: start;
  background: var(--assistant);
}

.message-role {
  display: block;
  margin-bottom: 0.35rem;
  font-size: 0.72rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--muted);
}

.message-content {
  white-space: pre-wrap;
  word-break: break-word;
}

.message.assistant .message-content :is(p, ul, ol, pre, blockquote, table) {
  margin: 0.55rem 0;
}

.message.assistant .message-content pre {
  overflow: auto;
  background: #1e1b17;
  color: #f9f5ed;
  border-radius: 12px;
  padding: 0.8rem;
}

.message.assistant .message-content code {
  font-family: ui-monospace, SFMono-Regular, Consolas, monospace;
}

.message.assistant .message-content a {
  color: #8f4f0f;
}

.attachment-list {
  display: flex;
  flex-wrap: wrap;
  gap: 0.45rem;
  margin-bottom: 0.65rem;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 0.48rem;
  border-radius: 999px;
  border: 1px solid #dccfbf;
  background: rgba(255, 255, 255, 0.92);
  padding: 0.4rem 0.68rem;
  font-size: 0.8rem;
}

.attachment-chip button {
  border: none;
  background: transparent;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.attachment-chip button:hover {
  color: var(--danger);
}

.attachment-meta {
  margin-top: 0.5rem;
  color: var(--muted);
  font-size: 0.76rem;
}

.composer {
  border-top: 1px solid var(--line);
  background: rgba(252, 249, 243, 0.95);
  padding: 0.9rem 1rem;
}

body[data-theme="dark"] .composer {
  background: rgba(29, 24, 19, 0.96);
}

.composer-main {
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto auto;
  align-items: center;
  gap: 0.55rem;
}

.model-wrap {
  display: grid;
  gap: 0.2rem;
  min-width: 180px;
}

.model-label {
  color: var(--muted);
  font-size: 0.72rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-weight: 600;
}

#prompt {
  width: 100%;
  min-height: 56px;
  max-height: 160px;
  height: 56px;
  resize: none;
  overflow-y: hidden;
  border-radius: 16px;
  border: 1px solid #d8d0c1;
  background: rgba(255, 255, 255, 0.96);
  padding: 0.78rem 0.9rem;
  color: var(--ink);
}

#prompt:focus,
select:focus {
  outline: none;
  border-color: #ba9f7b;
  box-shadow: 0 0 0 3px rgba(203, 111, 18, 0.12);
}

.composer-main #model {
  min-width: 180px;
  border: 1px solid #d8d0c1;
  border-radius: 14px;
  background: linear-gradient(180deg, #fff 0%, #f8f4ec 100%);
  height: 2.55rem;
  padding: 0 2rem 0 0.85rem;
  appearance: none;
  background-image:
    linear-gradient(45deg, transparent 50%, #7a6e60 50%),
    linear-gradient(135deg, #7a6e60 50%, transparent 50%),
    linear-gradient(180deg, #fff 0%, #f8f4ec 100%);
  background-position:
    calc(100% - 16px) calc(50% - 2px),
    calc(100% - 11px) calc(50% - 2px),
    0 0;
  background-size: 5px 5px, 5px 5px, 100% 100%;
  background-repeat: no-repeat;
}

body[data-theme="dark"] .composer-main #model {
  border-color: #4a3f31;
  background-image:
    linear-gradient(45deg, transparent 50%, #c9baa2 50%),
    linear-gradient(135deg, #c9baa2 50%, transparent 50%),
    linear-gradient(180deg, #2b241c 0%, #201a13 100%);
}

.icon-button,
.controls-wrap button,
.controls-wrap select,
.profile-action {
  border: 1px solid var(--line);
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.94);
  padding: 0.58rem 0.8rem;
}

body[data-theme="dark"] .icon-button,
body[data-theme="dark"] .controls-wrap button,
body[data-theme="dark"] .controls-wrap select,
body[data-theme="dark"] .profile-action {
  border-color: #5a4c3b;
  background: linear-gradient(180deg, #312820 0%, #281f18 100%);
  color: #ecdcc5;
}

.icon-button,
.profile-action,
.controls-wrap button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.45rem;
  cursor: pointer;
  transition: transform 120ms ease, background-color 120ms ease, border-color 120ms ease;
}

.icon-button:hover,
.profile-action:hover,
.controls-wrap button:hover {
  background: #fff;
  border-color: #cdbfa9;
}

body[data-theme="dark"] .icon-button:hover,
body[data-theme="dark"] .profile-action:hover,
body[data-theme="dark"] .controls-wrap button:hover {
  background: #3a3025;
  border-color: #7a6750;
}

.icon-button:active,
.profile-action:active,
.controls-wrap button:active {
  transform: translateY(1px);
}

.icon {
  width: 1.1rem;
  height: 1.1rem;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-only {
  width: 2.35rem;
  height: 2.35rem;
  padding: 0;
}

.top-icon {
  width: 2.7rem;
  height: 2.7rem;
}

.top-icon .icon {
  width: 1.3rem;
  height: 1.3rem;
}

.profile-btn {
  width: 2.95rem;
  height: 2.95rem;
  border-radius: 999px;
}

.profile-btn .icon {
  width: 1.45rem;
  height: 1.45rem;
}

.icon-only span:not(.icon) {
  display: none;
}

.icon svg {
  width: 100%;
  height: 100%;
}

body[data-theme="dark"] .top-icon .icon,
body[data-theme="dark"] .profile-btn .icon,
body[data-theme="dark"] .history-actions .icon {
  color: #f2e5cf;
}

body[data-theme="dark"] #new-chat-btn {
  background: linear-gradient(180deg, #e9dbbf 0%, #d8c4a4 100%);
  border-color: #d8c4a4;
  color: #221910;
}

body[data-theme="dark"] #new-chat-btn .icon {
  color: #221910;
}

body[data-theme="dark"] #new-chat-btn:hover {
  background: linear-gradient(180deg, #f2e5cd 0%, #e0ccad 100%);
  border-color: #e0ccad;
}

.primary-button,
#send-btn {
  background: var(--primary);
  color: #fff;
  border-color: var(--primary);
  min-width: 108px;
}

body[data-theme="dark"] .primary-button,
body[data-theme="dark"] #send-btn {
  color: #19150f;
}

body[data-theme="dark"] #signin-btn {
  background: linear-gradient(180deg, #e8d8bd 0%, #d5c19f 100%);
  border-color: #d5c19f;
  color: #231b12;
}

#send-btn.icon-only {
  min-width: 0;
}

#attach-btn,
#send-btn {
  border: none;
  background: transparent;
  box-shadow: none;
}

#attach-btn:hover,
#send-btn:hover {
  border: none;
  background: rgba(255, 255, 255, 0.9);
}

#send-btn {
  background: var(--primary);
  border-radius: 50%;
}

#send-btn .icon {
  color: #fff;
}

#send-btn:hover {
  background: var(--primary-2);
}

.primary-button:hover,
#send-btn:hover {
  background: var(--primary-2);
  border-color: var(--primary-2);
}

.danger-button {
  background: #fff4ec;
  color: var(--danger);
  border-color: #ebc7ab;
}

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

@media (max-width: 980px) {
  body {
    overflow: auto;
  }

  .layout {
    height: auto;
    min-height: 100vh;
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    max-height: 260px;
    border-right: none;
    border-bottom: 1px solid var(--line);
  }

  .history-list {
    grid-template-columns: repeat(auto-fill, minmax(180px, 1fr));
  }

  .topbar {
    flex-direction: column;
    align-items: flex-start;
  }

  .controls-wrap {
    width: 100%;
    justify-content: flex-start;
  }

  .composer-main {
    grid-template-columns: auto minmax(0, 1fr);
  }

  .composer-main #model,
  #send-btn {
    justify-self: end;
  }

  .composer-main #model {
    min-width: 180px;
    flex: 1 1 210px;
  }
}

@media (max-width: 640px) {
  .sidebar,
  .topbar,
  .composer {
    padding-left: 0.75rem;
    padding-right: 0.75rem;
  }

  .controls-wrap,
  .composer-main {
    width: 100%;
  }

  .controls-wrap button,
  .composer-main button {
    width: 100%;
    justify-content: center;
  }

  .composer-main #model {
    width: 100%;
  }

  #send-btn {
    min-width: 0;
  }
}
