:root {
  color-scheme: light;
  --bg: #f6f7f8;
  --panel: #ffffff;
  --panel-soft: #eef2f1;
  --border: #d8dedc;
  --text: #1f2926;
  --muted: #66736e;
  --primary: #12665a;
  --primary-dark: #0d4f46;
  --danger: #a43d32;
  --assistant: #ffffff;
  --user: #dcefe9;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}

html,
body {
  width: 100%;
}

button,
input,
textarea {
  font: inherit;
}

button {
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: 300px minmax(0, 1fr);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.sidebar {
  display: flex;
  min-height: 0;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: #fafafa;
  overflow: hidden;
}

.sidebar-backdrop {
  display: none;
}

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

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: 18px;
  line-height: 1.2;
}

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

.sidebar-header p,
.chat-header p,
.chat-time,
.empty-state {
  color: var(--muted);
  font-size: 13px;
}

.chat-header p {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.icon-button {
  display: grid;
  width: 38px;
  height: 38px;
  place-items: center;
  border: 1px solid var(--primary);
  border-radius: 8px;
  background: var(--primary);
  color: #ffffff;
  font-size: 24px;
  line-height: 1;
}

.chat-list {
  display: flex;
  flex: 1;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  padding: 12px;
}

.chat-item {
  width: 100%;
  border: 1px solid transparent;
  border-radius: 8px;
  background: transparent;
  padding: 12px;
  text-align: left;
}

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

.chat-item-title {
  overflow: hidden;
  color: var(--text);
  font-weight: 650;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-meta {
  margin-top: 4px;
  color: var(--muted);
  font-size: 12px;
}

.chat-panel {
  display: grid;
  min-width: 0;
  min-height: 0;
  grid-template-rows: auto 1fr auto;
  background: var(--panel);
  overflow: hidden;
}

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

.chat-header > div {
  min-width: 0;
}

.mobile-title-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.history-button {
  display: none;
}

.chat-actions {
  display: flex;
  align-items: center;
  gap: 12px;
}

.model-picker {
  display: grid;
  gap: 5px;
  min-width: 190px;
  color: var(--muted);
  font-size: 12px;
}

.model-picker select {
  min-width: 0;
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  color: var(--text);
  padding: 0 10px;
  outline: none;
}

.model-picker select:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(18, 102, 90, 0.12);
}

.ghost-button {
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #ffffff;
  color: var(--danger);
  padding: 9px 13px;
}

.login-page {
  display: grid;
  min-height: 100vh;
  min-height: 100dvh;
  place-items: center;
  overflow: auto;
  padding: 24px;
}

.login-card {
  display: grid;
  width: min(420px, 100%);
  gap: 24px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  padding: 28px;
  box-shadow: 0 18px 50px rgba(31, 41, 38, 0.1);
}

.login-card h1 {
  margin-bottom: 6px;
}

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

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

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

.login-form input {
  min-height: 42px;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 0 12px;
  outline: none;
}

.login-form input:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(18, 102, 90, 0.12);
}

.login-form button {
  min-height: 42px;
  border: 1px solid var(--primary);
  border-radius: 8px;
  background: var(--primary);
  color: #ffffff;
}

.login-form button:disabled {
  cursor: not-allowed;
  opacity: 0.65;
}

.login-error {
  min-height: 18px;
  color: var(--danger) !important;
}

.ghost-button:disabled,
.composer button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

.messages {
  display: flex;
  min-height: 0;
  flex-direction: column;
  gap: 16px;
  overflow-y: auto;
  padding: 24px;
}

.empty-state {
  margin: auto;
  max-width: 420px;
  text-align: center;
}

.message {
  display: flex;
  flex-direction: column;
  max-width: min(760px, 84%);
  gap: 6px;
}

.message.user {
  align-self: flex-end;
}

.message.assistant {
  align-self: flex-start;
}

.bubble {
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 12px 14px;
  word-break: break-word;
  line-height: 1.55;
}

.user .bubble {
  white-space: pre-wrap;
}

.user .bubble {
  border-color: #c3dfd6;
  background: var(--user);
}

.assistant .bubble {
  background: var(--assistant);
}

.message.streaming .bubble {
  border-color: #aacdc5;
}

.message.streaming .bubble::after {
  content: '';
  display: inline-block;
  width: 7px;
  height: 1.1em;
  margin-left: 3px;
  vertical-align: -0.18em;
  background: var(--primary);
  animation: cursor-blink 0.9s steps(1) infinite;
}

@keyframes cursor-blink {
  50% {
    opacity: 0;
  }
}

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

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

.markdown-body h1,
.markdown-body h2,
.markdown-body h3,
.markdown-body h4 {
  margin: 18px 0 8px;
  line-height: 1.25;
}

.markdown-body h1 {
  font-size: 22px;
}

.markdown-body h2 {
  font-size: 19px;
}

.markdown-body h3 {
  font-size: 16px;
}

.markdown-body p,
.markdown-body ul,
.markdown-body ol,
.markdown-body pre,
.markdown-body blockquote,
.markdown-body table {
  margin: 0 0 12px;
}

.markdown-body ul,
.markdown-body ol {
  padding-left: 22px;
}

.markdown-body li + li {
  margin-top: 4px;
}

.markdown-body code {
  border: 1px solid #d9e1df;
  border-radius: 5px;
  background: #f4f7f6;
  padding: 1px 5px;
  font-family: "Cascadia Code", Consolas, monospace;
  font-size: 0.92em;
}

.markdown-body pre {
  overflow-x: auto;
  border: 1px solid #d9e1df;
  border-radius: 8px;
  background: #f4f7f6;
  padding: 12px;
}

.markdown-body pre code {
  border: 0;
  background: transparent;
  padding: 0;
}

.markdown-body blockquote {
  border-left: 3px solid var(--primary);
  color: var(--muted);
  padding-left: 12px;
}

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

.markdown-body th,
.markdown-body td {
  border: 1px solid var(--border);
  padding: 7px 9px;
  text-align: left;
}

.markdown-body th {
  background: var(--panel-soft);
}

.markdown-body a {
  color: var(--primary);
}

.chat-time {
  padding: 0 2px;
}

.composer {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 12px;
  border-top: 1px solid var(--border);
  background: #ffffff;
  padding: 16px 24px;
}

.composer textarea {
  min-height: 44px;
  max-height: 180px;
  resize: none;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 11px 12px;
  outline: none;
  line-height: 1.45;
}

.composer textarea:focus {
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(18, 102, 90, 0.12);
}

.composer button {
  min-width: 74px;
  border: 1px solid var(--primary);
  border-radius: 8px;
  background: var(--primary);
  color: #ffffff;
  padding: 0 18px;
}

.composer button:hover:not(:disabled),
.icon-button:hover {
  background: var(--primary-dark);
}

.toast {
  position: fixed;
  right: 18px;
  bottom: 18px;
  max-width: min(420px, calc(100vw - 36px));
  border: 1px solid #e4beb8;
  border-radius: 8px;
  background: #fff5f3;
  color: #7a281f;
  padding: 12px 14px;
  box-shadow: 0 10px 26px rgba(31, 41, 38, 0.12);
}

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

  .sidebar {
    position: fixed;
    inset: 0 auto 0 0;
    z-index: 20;
    width: min(86vw, 340px);
    min-height: 0;
    transform: translateX(-100%);
    border-right: 0;
    border-bottom: 0;
    box-shadow: 12px 0 36px rgba(31, 41, 38, 0.16);
    transition: transform 160ms ease;
  }

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

  .sidebar-backdrop {
    position: fixed;
    inset: 0;
    z-index: 15;
    display: block;
    pointer-events: none;
    background: rgba(31, 41, 38, 0);
    transition: background 160ms ease;
  }

  .sidebar-backdrop.open {
    pointer-events: auto;
    background: rgba(31, 41, 38, 0.32);
  }

  body.sidebar-open .chat-panel {
    pointer-events: none;
  }

  .chat-list {
    flex-direction: column;
    overflow-x: hidden;
    overflow-y: auto;
  }

  .chat-item {
    min-width: 0;
  }

  .chat-panel {
    min-height: 0;
  }

  .chat-header {
    align-items: stretch;
    flex-direction: column;
    gap: 8px;
    min-width: 0;
    padding: 10px 12px;
  }

  .chat-actions {
    align-items: stretch;
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    min-width: 0;
  }

  .chat-actions .model-picker {
    grid-column: 1 / -1;
  }

  .history-button {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    flex: 0 0 auto;
    min-height: 36px;
    padding: 7px 10px;
  }

  .mobile-title-row {
    width: 100%;
  }

  .mobile-title-row h2 {
    min-width: 0;
    flex: 1;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }

  .model-picker {
    flex: 1;
    min-width: 0;
  }

  .model-picker select {
    width: 100%;
  }

  .ghost-button {
    min-width: 0;
    padding: 8px 10px;
  }

  .chat-header p,
  .chat-time,
  .chat-item-meta {
    font-size: 12px;
  }

  .messages {
    padding: 12px;
  }

  .message {
    max-width: 100%;
  }

  .bubble {
    max-width: 100%;
    padding: 10px 12px;
  }

  .markdown-body pre,
  .markdown-body table {
    max-width: calc(100vw - 48px);
  }

  .composer {
    grid-template-columns: 1fr;
    gap: 8px;
    padding: 10px 12px;
  }

  .composer textarea {
    min-height: 42px;
    max-height: 120px;
  }

  .composer button {
    min-height: 40px;
  }
}

@media (max-width: 420px) {
  .chat-header {
    padding: 8px 10px;
  }

  .history-button,
  .ghost-button {
    font-size: 14px;
  }

  .model-picker {
    gap: 3px;
  }

  .model-picker select {
    min-height: 36px;
  }

  .messages {
    gap: 12px;
    padding: 10px;
  }

  .composer {
    padding: 8px 10px;
  }
}
