:root {
  color-scheme: light;
  --bg: #f6f7f9;
  --panel: #ffffff;
  --line: #d8dde5;
  --line-strong: #c6ccd6;
  --text: #1d2430;
  --muted: #657184;
  --accent: #1f6feb;
  --accent-dark: #174ea6;
  --danger: #b42318;
  --user: #e9f2ff;
  --assistant: #ffffff;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background: var(--bg);
  color: var(--text);
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

button,
input,
textarea {
  font: inherit;
}

button {
  border: 0;
  border-radius: 7px;
  background: var(--accent);
  color: white;
  cursor: pointer;
  font-weight: 650;
}

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

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

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

.sidebar {
  position: fixed;
  z-index: 30;
  inset: 0 auto 0 0;
  width: min(340px, calc(100vw - 48px));
  transform: translateX(-100%);
  transition: transform 160ms ease;
  border-right: 1px solid var(--line);
  background: #eef1f5;
  padding: 16px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  gap: 14px;
  box-shadow: 18px 0 36px rgba(20, 29, 42, 0.16);
}

.sidebar-open .sidebar {
  transform: translateX(0);
}

.sidebar-backdrop {
  position: fixed;
  z-index: 20;
  inset: 0;
  background: rgba(23, 31, 44, 0.22);
  opacity: 0;
  pointer-events: none;
  transition: opacity 160ms ease;
}

.sidebar-open .sidebar-backdrop {
  opacity: 1;
  pointer-events: auto;
}

.brand {
  display: flex;
  align-items: center;
  justify-content: space-between;
  min-height: 40px;
}

.brand strong {
  display: block;
  font-size: 18px;
}

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

.icon-button {
  width: 34px;
  height: 34px;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--line);
  display: inline-grid;
  place-items: center;
  padding: 0;
  font-size: 18px;
  line-height: 1;
}

.icon-button:hover {
  background: #f2f4f8;
  color: var(--text);
}

.new-session {
  width: 100%;
  height: 40px;
}

.sessions {
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
  min-height: 0;
}

.session-link {
  display: block;
  min-width: 0;
  padding: 10px 6px 10px 11px;
  text-decoration: none;
  color: var(--text);
}

.session-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 34px;
  align-items: center;
  position: relative;
  border: 1px solid transparent;
  border-radius: 7px;
}

.session-item:hover,
.session-item.active {
  background: var(--panel);
  border-color: var(--line);
}

.session-menu {
  position: relative;
}

.session-menu-button {
  width: 28px;
  height: 28px;
  border-radius: 6px;
  background: transparent;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
  display: grid;
  place-items: center;
  opacity: 0;
}

.session-item:hover .session-menu-button,
.session-item.active .session-menu-button,
.session-menu.open .session-menu-button {
  opacity: 1;
}

.session-menu-button:hover {
  background: #e5edf8;
  color: var(--text);
}

.session-rename,
.session-delete {
  width: 100%;
  height: 30px;
  padding: 0 8px;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
}

.session-menu-popover {
  display: none;
}

.session-menu-portal {
  position: fixed;
  z-index: 100;
  min-width: 124px;
  padding: 5px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fff;
  box-shadow: 0 10px 24px rgba(25, 32, 45, 0.18);
}

.session-menu-portal[hidden] {
  display: none;
}

.session-menu-portal button {
  width: 100%;
  height: 30px;
  padding: 0 8px;
  border-radius: 6px;
  background: transparent;
  color: var(--text);
  font-size: 13px;
  font-weight: 500;
  text-align: left;
}

.session-menu-portal button:hover {
  background: #f2f4f8;
}

#portalRename:hover {
  background: #e5edf8;
  color: var(--accent);
}

#portalDelete:hover {
  background: #fee2e2;
  color: var(--danger);
}

.session-rename:hover {
  background: #e5edf8;
  color: var(--accent);
}

.session-delete:hover {
  background: #fee2e2;
  color: var(--danger);
}

.session-title-input {
  width: 100%;
  height: 22px;
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 0 5px;
  font-size: 14px;
  font-weight: 650;
  outline: none;
}

.session-link strong,
.session-link span {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.session-link strong {
  font-size: 14px;
}

.session-item.running .session-title::after {
  content: ".";
  display: inline-block;
  width: 1.2em;
  margin-left: 5px;
  color: var(--accent);
  font-weight: 800;
  animation: loadingDots 1.1s steps(1, end) infinite;
}

.session-link span {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.chat {
  display: grid;
  grid-template-rows: minmax(0, 1fr);
  min-width: 0;
  height: 100vh;
  position: relative;
}

.sidebar-actions {
  display: grid;
  gap: 8px;
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--line);
}

.sidebar-actions form,
.sidebar-actions button {
  width: 100%;
}

.sidebar-action-link {
  min-height: 38px;
  display: grid;
  grid-template-columns: 24px minmax(0, 1fr);
  align-items: center;
  gap: 8px;
  padding: 7px 10px;
  border: 1px solid transparent;
  border-radius: 7px;
  color: var(--text);
  background: transparent;
  text-align: left;
  text-decoration: none;
  font-size: 16px;
  font-weight: 650;
}

.sidebar-action-link:hover,
.sidebar-action-link.active {
  background: var(--panel);
  border-color: var(--line);
}

.sidebar-action-icon {
  width: 24px;
  height: 24px;
  display: inline-grid;
  place-items: center;
  border: 1px solid var(--line);
  border-radius: 6px;
  background: #fff;
  color: var(--muted);
  font-size: 13px;
  line-height: 1;
}

.secondary {
  height: 36px;
  padding: 0 13px;
  border: 1px solid var(--line);
  background: white;
  color: var(--text);
}

.secondary.sidebar-action-link {
  height: auto;
  padding: 7px 10px;
  background: transparent;
}

.secondary:hover {
  background: #f2f4f8;
}

.messages {
  overflow-y: auto;
  padding: 18px 16px 108px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  background: #fafbfc;
}

.mobile-menu-button {
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 9;
  width: 36px;
  height: 36px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.9);
  color: var(--muted);
  font-size: 18px;
}

.message {
  display: flex;
  align-items: flex-end;
  width: min(100%, 980px);
}

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

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

.message-body {
  display: grid;
  min-width: 0;
  width: 100%;
  max-width: min(720px, calc(100vw - 96px));
}

.message.user .message-body {
  justify-items: end;
}

.bubble {
  min-width: 0;
  width: fit-content;
  max-width: 100%;
  white-space: normal;
  overflow-wrap: anywhere;
  word-break: break-word;
  border: 1px solid var(--line);
  border-radius: 16px;
  border-bottom-left-radius: 5px;
  padding: 9px 12px;
  line-height: 1.6rem;
  background: var(--assistant);
  box-shadow: 0 1px 3px rgba(28, 35, 45, 0.08);
}

.bubble,
.bubble * {
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.bubble br + br {
  display: none;
}

.message.user .bubble {
  margin-left: auto;
  border-color: #1f6feb;
  border-bottom-left-radius: 16px;
  border-bottom-right-radius: 5px;
  background: var(--accent);
  color: #fff;
}

.message.assistant .bubble {
  color: var(--text);
}

.bubble p {
  margin: 0;
  line-height: inherit;
}

.bubble p + p {
  margin-top: 0.36em;
}

.bubble ul,
.bubble ol {
  margin: 0.35em 0;
  padding-left: 1.2em;
}

.bubble > :first-child {
  margin-top: 0;
}

.bubble > :last-child {
  margin-bottom: 0;
}

.bubble pre {
  max-width: 100%;
  overflow-x: auto;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  margin: 0.5em 0;
  padding: 10px;
  border-radius: 8px;
  background: #111827;
  color: #f8fafc;
}

.bubble code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, monospace;
  font-size: 0.92em;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
}

.bubble table {
  display: block;
  max-width: 100%;
  overflow-x: auto;
  border-collapse: collapse;
}

.bubble img {
  max-width: 100%;
  height: auto;
}

.bubble :not(pre) > code {
  padding: 0.12em 0.34em;
  border-radius: 5px;
  background: rgba(100, 116, 139, 0.16);
}

.message.user .bubble :not(pre) > code {
  background: rgba(255, 255, 255, 0.2);
}

.bubble a {
  color: inherit;
  text-decoration: underline;
  text-underline-offset: 2px;
}

.bubble.loading::after {
  content: ".";
  display: inline-block;
  width: 1.2em;
  min-width: 1.2em;
  margin-left: 8px;
  color: var(--muted);
  font-weight: 700;
  text-align: left;
  animation: loadingDots 1.1s steps(1, end) infinite;
  vertical-align: baseline;
}

.bubble.status-bubble {
  white-space: normal;
}

.bubble.status-bubble p {
  display: inline;
  margin: 0;
}

.spinner {
  width: 14px;
  height: 14px;
  border: 2px solid #9cc6ff;
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin 800ms linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}

@keyframes loadingDots {
  0%,
  24% {
    content: ".";
  }
  25%,
  64% {
    content: "..";
  }
  65%,
  100% {
    content: "...";
  }
}

.composer {
  position: fixed;
  z-index: 10;
  left: 50%;
  bottom: 22px;
  width: min(820px, calc(100vw - 32px));
  transform: translateX(-50%);
  display: grid;
  gap: 8px;
  padding: 0;
}

.is-running .composer {
  border-color: var(--line-strong);
}

.composer-box {
  display: grid;
  grid-template-columns: 38px minmax(0, 1fr) 38px;
  align-items: end;
  gap: 8px;
  min-height: 58px;
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 22px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 14px 38px rgba(26, 34, 46, 0.16);
}

.composer textarea {
  resize: none;
  max-height: 180px;
  min-height: 38px;
  height: 38px;
  border: 0;
  padding: 8px 4px;
  outline: none;
  background: transparent;
  line-height: 1.45;
}

.composer textarea:disabled {
  background: transparent;
}

.composer textarea:focus,
.login-form input:focus {
  box-shadow: none;
}

.attach-button,
.send-button {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  display: grid;
  place-items: center;
  padding: 0;
  font-size: 20px;
  line-height: 1;
  user-select: none;
}

.attach-button {
  border: 1px solid var(--line);
  background: #fff;
  color: var(--muted);
  cursor: pointer;
}

.attach-button:hover {
  background: #f2f4f8;
  color: var(--text);
}

.attach-button input {
  display: none;
}

.send-button {
  background: var(--text);
  color: #fff;
  font-weight: 700;
}

.send-button:hover {
  background: #000;
}

.send-button.stop {
  background: var(--danger);
  font-size: 13px;
}

.send-button[hidden] {
  display: none;
}

.attachment-preview {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  padding: 0 10px;
}

.attachment-preview[hidden] {
  display: none;
}

.attachment-chip {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  min-height: 26px;
  max-width: min(100%, 420px);
  padding: 6px 9px;
  border: 1px solid #c9d8eb;
  border-radius: 7px;
  background: #f3f8ff;
  color: var(--text);
  font-size: 12px;
  line-height: 1.35;
  overflow: visible;
  text-align: left;
  white-space: normal;
}

.file-icon {
  position: relative;
  flex: 0 0 auto;
  width: 14px;
  height: 17px;
  border: 1.5px solid #5179a8;
  border-radius: 2px;
  background: #fff;
}

.file-icon::after {
  content: "";
  position: absolute;
  top: -1.5px;
  right: -1.5px;
  width: 6px;
  height: 6px;
  border-left: 1.5px solid #5179a8;
  border-bottom: 1.5px solid #5179a8;
  background: #e4effb;
}

.file-name {
  min-width: 0;
  overflow-wrap: anywhere;
  word-break: break-word;
}

.attachment-remove {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 14px;
  line-height: 1;
}

.attachment-preview .attachment-chip {
  cursor: pointer;
  max-width: 100%;
}

.attachments {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 7px;
}

.files-view {
  overflow-y: auto;
  padding: 28px 18px 48px;
  background: #fafbfc;
}

.files-header {
  width: min(920px, 100%);
  margin: 0 auto 16px;
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 12px;
}

.files-header h1 {
  margin: 0;
  font-size: 22px;
}

.files-header span {
  color: var(--muted);
  font-size: 13px;
}

.file-list {
  width: min(920px, 100%);
  margin: 0 auto;
  display: grid;
  gap: 8px;
}

.file-row {
  display: grid;
  grid-template-columns: 34px minmax(0, 1fr) auto;
  gap: 10px;
  align-items: start;
  padding: 12px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #fff;
}

.file-row-icon {
  width: 28px;
  height: 28px;
  display: grid;
  place-items: center;
}

.file-row-main {
  min-width: 0;
  display: grid;
  gap: 5px;
}

.file-row-name {
  color: var(--text);
  font-size: 14px;
  font-weight: 700;
  line-height: 1.35;
  overflow-wrap: anywhere;
  text-decoration: none;
}

.file-row-name:hover,
.file-row-meta a:hover {
  text-decoration: underline;
}

.file-row-meta {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  color: var(--muted);
  font-size: 12px;
}

.file-row-meta a {
  color: var(--accent);
  text-decoration: none;
}

.file-row p {
  margin: 0;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
  overflow-wrap: anywhere;
}

.file-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: flex-end;
}

.file-download,
.file-delete {
  min-height: 30px;
  padding: 6px 10px;
  border: 1px solid var(--line);
  border-radius: 7px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
}

.file-download {
  color: var(--text);
  background: #f7f9fc;
  text-decoration: none;
}

.file-download:hover {
  background: #eef4ff;
}

.file-delete {
  color: var(--danger);
  background: #fff;
}

.file-delete:hover {
  background: #fee2e2;
}

.inline-empty {
  min-height: 260px;
}

.empty {
  grid-row: 1 / -1;
  display: grid;
  place-content: center;
  gap: 16px;
  text-align: center;
}

.empty h1 {
  margin: 0;
  font-size: 22px;
}

.empty button {
  height: 42px;
  padding: 0 18px;
}

body.login-body {
  min-height: 100vh;
  min-height: 100dvh;
  overflow: hidden;
}

.login-shell {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  height: 100dvh;
  display: grid;
  place-items: center;
  padding: 24px;
  overflow: auto;
}

.login-panel {
  width: min(380px, 100%);
  padding: 28px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--panel);
}

.login-panel h1 {
  margin: 0 0 22px;
  font-size: 24px;
}

.login-form {
  display: grid;
  gap: 14px;
}

.login-form label {
  display: grid;
  gap: 6px;
}

.login-form span {
  color: var(--muted);
  font-size: 13px;
}

.login-form input {
  height: 42px;
  border: 1px solid var(--line);
  border-radius: 7px;
  padding: 0 11px;
  outline: none;
}

.login-form button {
  height: 42px;
}

.error {
  margin: 0;
  color: var(--danger);
  font-size: 14px;
}

@media (max-width: 760px) {
  .message {
    width: 100%;
  }

  .message-body {
    max-width: calc(100vw - 48px);
  }

  .composer {
    grid-template-columns: 1fr;
  }

  .file-row {
    grid-template-columns: 28px minmax(0, 1fr);
  }

  .file-actions {
    grid-column: 2;
    justify-self: start;
  }
}

@media (min-width: 920px) {
  .shell {
    grid-template-columns: 320px minmax(0, 1fr);
  }

  .sidebar {
    position: relative;
    inset: auto;
    width: 320px;
    transform: none;
    box-shadow: none;
  }

  .sidebar-backdrop,
  .mobile-menu-button,
  #closeSidebar {
    display: none;
  }

  .message {
    width: min(100%, 820px);
  }

  .message-body {
    max-width: 640px;
  }

  .composer {
    left: calc(320px + (100vw - 320px) / 2);
    width: min(820px, calc(100vw - 320px - 64px));
  }
}
