:root {
  --sidebar-bg: #111111;
  --main-bg: #1e1e1e;
  --top-bg: #1e1e1e;
  --surface: #2a2a2a;
  --surface-soft: #1f1f1f;
  --surface-active: #1c1c1c;
  --border: #3a3a3a;
  --text: #ffffff;
  --text-soft: #d0d0d0;
  --text-muted: #b0b0b0;
  --placeholder: #9a9a9a;
  --heading: #ededed;
  --danger: #ff6b6b;
}

* {
  box-sizing: border-box;
}

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

body {
  font-family: Inter, "Segoe UI", sans-serif;
  background: var(--main-bg);
  color: var(--text);
  min-height: 100dvh;
}

body.theme-light {
  --sidebar-bg: #e7e7ea;
  --main-bg: #f5f5f7;
  --top-bg: #f5f5f7;
  --surface: #ffffff;
  --surface-soft: #f0f0f3;
  --surface-active: #ececf2;
  --border: #d5d5dd;
  --text: #141417;
  --text-soft: #2e2e33;
  --text-muted: #555560;
  --placeholder: #777784;
  --heading: #18181e;
  --danger: #b73c3c;
}

body.theme-graphite {
  --sidebar-bg: #10161c;
  --main-bg: #1a2028;
  --top-bg: #1a2028;
  --surface: #252d38;
  --surface-soft: #1d242d;
  --surface-active: #202833;
  --border: #3f4855;
  --text: #edf3fa;
  --text-soft: #d2dde9;
  --text-muted: #a7b4c3;
  --placeholder: #90a0b2;
  --heading: #f4f8fd;
  --danger: #e57a7a;
}

.hidden {
  display: none !important;
}

.toast {
  position: fixed;
  right: 16px;
  top: 14px;
  z-index: 60;
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 12px;
  background: var(--surface-soft);
  color: var(--text-soft);
  font-size: 13px;
}

.toast.error {
  border-color: var(--danger);
  color: var(--danger);
}

.auth-screen {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 24px;
  background: var(--main-bg);
}

.auth-card {
  width: min(420px, 100%);
  border: 1px solid var(--border);
  border-radius: 16px;
  background: var(--surface-soft);
  padding: 24px;
}

.auth-brand {
  font-family: "Space Grotesk", Inter, sans-serif;
  font-size: 24px;
  font-weight: 600;
  color: var(--text);
}

.auth-subtitle {
  margin: 8px 0 16px;
  color: var(--text-muted);
  font-size: 14px;
}

.auth-tabs {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  margin-bottom: 12px;
}

.tab {
  border: 1px solid var(--border);
  background: transparent;
  color: var(--text-muted);
  border-radius: 10px;
  padding: 10px;
  cursor: pointer;
}

.tab.active {
  background: var(--surface-active);
  color: var(--text);
}

.auth-form,
.settings-form {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.field {
  display: flex;
  flex-direction: column;
  gap: 6px;
  color: var(--text-muted);
  font-size: 13px;
}

.field input,
.field textarea,
.field select {
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  font-size: 14px;
  font-family: Inter, sans-serif;
  padding: 10px 12px;
}

.field textarea {
  resize: vertical;
}

.readonly-field input {
  color: var(--text-muted);
}

.primary-btn {
  border: none;
  border-radius: 10px;
  background: var(--surface-active);
  color: var(--text);
  padding: 10px 12px;
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
}

.primary-btn:hover {
  filter: brightness(1.08);
}

.app-screen {
  height: 100dvh;
  display: grid;
  grid-template-columns: 280px 1fr;
  overflow: hidden;
}

.sidebar {
  background: var(--sidebar-bg);
  padding: 24px 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 24px;
}

.sidebar-backdrop {
  position: fixed;
  inset: 0;
  z-index: 64;
  background: rgba(0, 0, 0, 0.46);
}

.sidebar-top {
  display: flex;
  flex-direction: column;
  gap: 24px;
}

.app-title {
  margin: 0;
  color: var(--text);
  font-family: "Space Grotesk", Inter, sans-serif;
  font-size: 18px;
  font-weight: 600;
  line-height: 1.2;
}

.nav-stack {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.nav-item {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text-soft);
  border-radius: 10px;
  padding: 8px 12px;
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  font-weight: 400;
  text-align: left;
  cursor: pointer;
}

.nav-item:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.nav-item-active {
  background: var(--surface-active);
  color: var(--text);
  font-weight: 500;
}

.nav-icon {
  width: 16px;
  height: 16px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  color: inherit;
}

.nav-icon i {
  font-size: 13px;
}

.recent-block {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.section-label {
  color: var(--text-muted);
  font-family: Inter, sans-serif;
  font-size: 12px;
  font-weight: 500;
  letter-spacing: 0.02em;
}

.recent-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.recent-list li {
  width: 100%;
}

.recent-chat-row {
  width: 100%;
  display: grid;
  grid-template-columns: 1fr 28px;
  gap: 6px;
  align-items: center;
}

.recent-chat-btn {
  width: 100%;
  border: 1px solid transparent;
  background: transparent;
  color: var(--text-soft);
  font-size: 13px;
  line-height: 1.3;
  text-align: left;
  border-radius: 8px;
  padding: 7px 8px;
  cursor: pointer;
}

.recent-chat-btn:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.recent-chat-btn:hover {
  background: var(--surface-soft);
}

.recent-chat-btn.active {
  border-color: var(--border);
  background: var(--surface-active);
  color: var(--text);
}

.recent-chat-delete {
  width: 28px;
  height: 28px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
}

.recent-chat-delete:hover {
  border-color: #7f2323;
  background: #341414;
  color: #ff8a8a;
}

.recent-chat-delete:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.sidebar-actions {
  display: flex;
  flex-direction: column;
  gap: 8px;
  width: 100%;
}

.logout-btn {
  width: 100%;
  border: 1px solid #7f2323;
  border-radius: 10px;
  background: #341414;
  color: #ff8a8a;
  font-size: 14px;
  font-weight: 600;
  padding: 10px 12px;
  cursor: pointer;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}

.logout-btn:hover {
  border-color: #a62f2f;
  background: #461919;
  color: #ff9f9f;
}

.icon-btn:hover,
.send-btn:hover {
  filter: brightness(1.1);
}

.main-shell {
  background: var(--main-bg);
  display: grid;
  grid-template-rows: 64px minmax(0, 1fr) auto;
  height: 100%;
  min-height: 0;
}

.top-bar {
  height: 64px;
  background: var(--top-bg);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 24px;
}

.top-left {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.sidebar-toggle {
  display: none;
}

.top-model {
  display: inline-flex;
  align-items: center;
  gap: 10px;
  color: var(--text);
  font-family: "Space Grotesk", Inter, sans-serif;
  font-size: 14px;
  font-weight: 500;
  line-height: 1;
}

.model-select {
  -webkit-appearance: none;
  -moz-appearance: none;
  appearance: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface-soft);
  color: var(--text-soft);
  font-size: 12px;
  line-height: 1.2;
  font-family: Inter, sans-serif;
  padding: 6px 32px 6px 10px;
  cursor: pointer;
  min-width: 168px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'%3E%3Cpath d='M1 1l4 4 4-4' stroke='%23b0b0b0' stroke-width='1.5' stroke-linecap='round' stroke-linejoin='round' fill='none'/%3E%3C/svg%3E");
  background-position: right 11px center;
  background-size: 10px 6px;
  background-repeat: no-repeat;
}

.model-select:disabled {
  opacity: 0.6;
  cursor: not-allowed;
}

.icon-btn {
  width: 32px;
  height: 32px;
  border: none;
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  cursor: pointer;
}

.icon-btn i {
  font-size: 15px;
}

.chat-stage {
  overflow-y: auto;
  padding: 12px 24px;
  min-height: 0;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.welcome-state {
  margin-top: clamp(100px, 28vh, 260px);
  text-align: center;
}

.welcome-state h2 {
  margin: 0;
  color: var(--heading);
  font-size: clamp(30px, 3vw, 32px);
  font-weight: 500;
}

.messages {
  width: min(760px, 100%);
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  gap: 12px;
  padding-bottom: 18px;
}

.message {
  border-radius: 14px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: var(--text-soft);
  padding: 11px 13px;
  font-size: 14px;
  line-height: 1.45;
  word-break: break-word;
}

.message.user {
  align-self: flex-end;
  max-width: 86%;
  background: #2f2f2f;
  color: var(--text);
  white-space: pre-wrap;
}

.message.assistant {
  max-width: 95%;
}

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

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

.message.assistant .message-body p,
.message.assistant .message-body ul,
.message.assistant .message-body ol,
.message.assistant .message-body pre,
.message.assistant .message-body blockquote {
  margin: 0.52rem 0;
}

.message.assistant .message-body ul,
.message.assistant .message-body ol {
  padding-left: 1.2rem;
}

.message.assistant .message-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, Monaco, Consolas, "Liberation Mono", "Courier New", monospace;
  font-size: 12.5px;
  border-radius: 6px;
  padding: 1px 6px;
  background: rgba(255, 255, 255, 0.07);
  color: var(--text);
}

.message.assistant .message-body pre {
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(0, 0, 0, 0.28);
  overflow-x: auto;
  padding: 10px;
}

.message.assistant .message-body pre code {
  padding: 0;
  border-radius: 0;
  background: transparent;
  color: inherit;
}

.message.assistant .message-body a {
  color: #8ec1ff;
}

.message.assistant .message-body .katex-display {
  margin: 0.62rem 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding: 2px 0;
}

.message.assistant .message-body .katex {
  max-width: 100%;
}

.message-toolbar {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 8px;
}

.message-copy-btn {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: transparent;
  color: var(--text-muted);
  font-size: 12px;
  line-height: 1;
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 5px 8px;
  cursor: pointer;
}

.message-copy-btn i {
  font-size: 12px;
}

.message-copy-btn:hover {
  border-color: var(--text-muted);
  color: var(--text-soft);
}

.message-copy-btn.copied {
  border-color: #2f8859;
  color: #8bd4ae;
}

.message-copy-btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
}

.message.search-hit {
  border-color: #5c8cff;
  box-shadow: 0 0 0 1px rgba(92, 140, 255, 0.4);
  animation: searchHitPulse 1.6s ease-out 1;
}

@keyframes searchHitPulse {
  0% {
    box-shadow: 0 0 0 0 rgba(92, 140, 255, 0.54);
  }

  100% {
    box-shadow: 0 0 0 8px rgba(92, 140, 255, 0);
  }
}

.message-meta {
  margin-top: 6px;
  color: var(--text-muted);
  font-size: 11px;
}

.message.streaming .streaming-cursor {
  width: 7px;
  height: 1em;
  display: inline-block;
  vertical-align: text-bottom;
  margin-left: 3px;
  background: var(--text-muted);
  animation: cursorBlink 0.95s steps(1, end) infinite;
}

@keyframes cursorBlink {
  0%,
  45% {
    opacity: 1;
  }

  46%,
  100% {
    opacity: 0;
  }
}

.composer-form {
  width: min(680px, calc(100% - 40px));
  min-height: 90px;
  margin: 0 auto 24px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--surface);
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 1fr 36px;
  align-items: end;
  gap: 12px;
}

.send-btn {
  width: 36px;
  height: 36px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--surface-soft);
  color: #cfcfcf;
  cursor: pointer;
}

.send-btn i {
  font-size: 14px;
}

.send-btn:disabled {
  cursor: not-allowed;
  opacity: 0.6;
}

.composer-form textarea {
  width: 100%;
  height: 68px;
  max-height: 68px;
  border: none;
  outline: none;
  resize: none;
  overflow-y: auto;
  overflow-x: hidden;
  background: transparent;
  color: var(--text-soft);
  font-size: 14px;
  line-height: 1.45;
  font-family: Inter, sans-serif;
}

.composer-form textarea::placeholder {
  color: var(--placeholder);
}

.overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  background: rgba(0, 0, 0, 0.52);
  display: flex;
  justify-content: flex-end;
}

.search-overlay {
  z-index: 65;
  justify-content: center;
  align-items: flex-start;
  padding: 72px 16px 20px;
}

.search-panel {
  width: min(760px, 100%);
  max-height: min(72vh, 700px);
  border: 1px solid var(--border);
  border-radius: 14px;
  background: var(--surface-soft);
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.4);
}

.search-header {
  display: grid;
  grid-template-columns: 20px 1fr 32px;
  gap: 10px;
  align-items: center;
  border-bottom: 1px solid var(--border);
  padding: 10px 12px;
}

.search-header i {
  color: var(--text-muted);
  font-size: 13px;
}

.search-header input {
  width: 100%;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-family: Inter, sans-serif;
}

.search-header input::placeholder {
  color: var(--placeholder);
}

.search-hint {
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 12px;
}

.search-hint.error {
  color: var(--danger);
}

.search-results {
  list-style: none;
  margin: 0;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  overflow-y: auto;
}

.search-results li {
  width: 100%;
}

.search-result-btn {
  width: 100%;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: var(--surface);
  color: var(--text-soft);
  text-align: left;
  cursor: pointer;
  padding: 10px 12px;
}

.search-result-btn:hover {
  border-color: var(--text-muted);
  background: var(--surface-active);
}

.search-result-title {
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.search-result-snippet {
  margin-top: 5px;
  color: var(--text-soft);
  font-size: 12px;
  line-height: 1.4;
}

.search-result-meta {
  margin-top: 7px;
  color: var(--text-muted);
  font-size: 11px;
}

.settings-panel {
  width: min(400px, 100%);
  height: 100%;
  background: var(--surface-soft);
  border-left: 1px solid var(--border);
  padding: 18px;
  overflow-y: auto;
}

.settings-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
}

.settings-header h3 {
  margin: 0;
  color: var(--text);
  font-family: "Space Grotesk", Inter, sans-serif;
  font-size: 22px;
}

@media (max-width: 960px) {
  .app-screen {
    grid-template-columns: 1fr;
    grid-template-rows: auto minmax(0, 1fr);
  }

  .sidebar {
    padding: 12px 10px;
    gap: 10px;
    max-height: 44vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }

  .sidebar-top {
    gap: 10px;
  }

  .nav-stack {
    flex-direction: row;
  }

  .nav-item {
    flex: 1;
  }

  .recent-list {
    flex-direction: row;
    overflow-x: auto;
    gap: 10px;
    -webkit-overflow-scrolling: touch;
  }

  .recent-list li {
    min-width: 180px;
  }

  .recent-chat-btn {
    background: var(--surface-active);
    min-width: 180px;
    padding: 8px 10px;
  }

  .recent-chat-delete {
    width: 32px;
    height: 32px;
  }

  .main-shell {
    grid-template-rows: 56px minmax(0, 1fr) auto;
  }

  .top-bar {
    height: 56px;
    padding: calc(8px + env(safe-area-inset-top)) 12px 8px;
  }

  .chat-stage {
    padding: 8px 10px 12px;
  }

  .welcome-state {
    margin-top: clamp(56px, 18vh, 150px);
  }

  .composer-form {
    width: calc(100% - 16px);
    margin-bottom: calc(8px + env(safe-area-inset-bottom));
    min-height: 82px;
    padding: 6px 10px;
  }

  .composer-form textarea {
    height: 62px;
    max-height: 62px;
  }

  .send-btn {
    width: 34px;
    height: 34px;
  }

  .sidebar-actions {
    width: 100%;
  }

  .model-select {
    min-width: 124px;
    max-width: 58vw;
    font-size: 12px;
  }

  .messages {
    width: 100%;
  }

  .field input,
  .field textarea,
  .field select,
  .composer-form textarea,
  .search-header input {
    font-size: 16px;
  }

  .search-overlay {
    padding:
      calc(10px + env(safe-area-inset-top))
      8px
      calc(10px + env(safe-area-inset-bottom));
  }

  .search-panel {
    max-height: calc(100dvh - 20px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  }
}

@media (max-width: 720px) {
  body.sidebar-open {
    overflow: hidden;
  }

  .app-screen {
    grid-template-rows: 1fr;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(86vw, 320px);
    max-height: 100dvh;
    border-right: 1px solid var(--border);
    z-index: 70;
    padding-top: calc(12px + env(safe-area-inset-top));
    padding-bottom: calc(12px + env(safe-area-inset-bottom));
    transform: translateX(-105%);
    transition: transform 0.2s ease;
  }

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

  .sidebar-toggle {
    display: inline-flex;
    align-items: center;
    justify-content: center;
  }

  .top-bar {
    padding-left: 8px;
    padding-right: 8px;
  }

  .top-model {
    min-width: 0;
  }

  .model-select {
    max-width: 62vw;
  }
}