:root {
  color-scheme: dark;
  --bg: #212121;
  --sidebar: #171717;
  --panel: #2f2f2f;
  --panel-soft: #262626;
  --panel-hover: #343434;
  --border: rgba(255, 255, 255, 0.08);
  --text: #ececec;
  --muted: #a3a3a3;
  --shadow: 0 12px 30px rgba(0, 0, 0, 0.22);
}

body.theme-light {
  color-scheme: light;
  --bg: #f7f7f8;
  --sidebar: #efeff1;
  --panel: #ffffff;
  --panel-soft: #f4f4f5;
  --panel-hover: #ececef;
  --border: rgba(15, 23, 42, 0.08);
  --text: #1f2937;
  --muted: #6b7280;
  --shadow: 0 10px 30px rgba(15, 23, 42, 0.08);
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  min-height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: Inter, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
button, input, textarea, label { font: inherit; }
body { overflow: hidden; transition: background .2s ease, color .2s ease; }
.hidden { display: none !important; }

.login-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: 20px;
  background: radial-gradient(circle at top, rgba(255,255,255,.05), transparent 35%), var(--bg);
}
.login-card {
  width: min(460px, 100%);
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 28px;
  padding: 28px;
  box-shadow: var(--shadow);
  text-align: center;
}
.login-logo {
  width: 52px;
  height: 52px;
  border-radius: 18px;
  display: grid;
  place-items: center;
  margin: 0 auto 16px;
  background: #fff;
  color: #111;
  font-weight: 700;
  font-size: 20px;
}
body.theme-light .login-logo,
body.theme-light .send-btn {
  background: #111;
  color: #fff;
}
.login-card h1 { margin: 0 0 8px; font-size: 28px; }
.login-card p { margin: 0 0 18px; color: var(--muted); }
.login-form { display: flex; flex-direction: column; gap: 12px; }
.login-form input {
  width: 100%;
  padding: 14px 16px;
  border-radius: 18px;
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
}
.login-btn { width: 100%; justify-content: center; }
.login-error { min-height: 20px; color: #ff8a8a; margin-top: 10px; font-size: 13px; }

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

.sidebar {
  background: var(--sidebar);
  border-right: 1px solid var(--border);
  padding: 14px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  overflow-y: auto;
  overflow-x: hidden;
}
.sidebar-top { display: flex; flex-direction: column; gap: 14px; }
.brand-wrap { display: flex; align-items: center; gap: 12px; }
.brand-logo {
  width: 36px;
  height: 36px;
  border-radius: 12px;
  display: grid;
  place-items: center;
  background: #fff;
  color: #111;
  font-weight: 700;
}
.brand { font-size: 16px; font-weight: 700; line-height: 1.2; }
.brand-subtitle { font-size: 12px; color: var(--muted); }

.new-chat-btn,
.ghost-btn,
.send-btn,
.conversation-main,
.conversation-delete,
.attach-btn,
.model-dropdown-button,
.mobile-sidebar-toggle {
  border: 1px solid var(--border);
  background: var(--panel-soft);
  color: var(--text);
  border-radius: 14px;
  transition: .18s ease;
}
.new-chat-btn,
.ghost-btn,
.send-btn,
.conversation-main,
.conversation-delete,
.sidebar-text-btn,
.attach-btn,
.model-dropdown-button,
.mobile-sidebar-toggle {
  cursor: pointer;
}
.new-chat-btn { width: 100%; padding: 12px 14px; text-align: left; font-weight: 600; }
.new-chat-btn:hover,
.ghost-btn:hover,
.send-btn:hover,
.conversation-main:hover,
.conversation-delete:hover,
.attach-btn:hover,
.model-dropdown-button:hover,
.mobile-sidebar-toggle:hover { background: var(--panel-hover); }

.sidebar-section-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 4px 4px 0;
}
.sidebar-section-label { color: var(--muted); font-size: 12px; }
.sidebar-text-btn {
  border: none;
  background: transparent;
  color: var(--muted);
  font-size: 12px;
  padding: 4px 6px;
  border-radius: 8px;
}
.sidebar-text-btn:hover { background: var(--panel-hover); color: var(--text); }

.conversation-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: auto;
  overflow-x: hidden;
  padding-right: 2px;
}
.conversation-item {
  display: flex;
  align-items: stretch;
  gap: 8px;
  min-width: 0;
}
.conversation-main {
  width: 100%;
  min-width: 0;
  flex: 1 1 auto;
  padding: 12px;
  text-align: left;
}
.conversation-item.active .conversation-main {
  background: var(--panel-hover);
  border-color: rgba(255,255,255,.12);
}
.conversation-delete {
  width: 0;
  min-width: 0;
  overflow: hidden;
  display: grid;
  place-items: center;
  padding: 0;
  border-color: transparent;
  font-size: 18px;
  opacity: 0;
  transform: translateX(8px) scale(.92);
  pointer-events: none;
}
.conversation-item:hover .conversation-delete,
.conversation-item:focus-within .conversation-delete {
  width: 34px;
  min-width: 34px;
  border-color: var(--border);
  opacity: .78;
  transform: translateX(0) scale(1);
  pointer-events: auto;
}
.conversation-title {
  font-size: 14px;
  font-weight: 500;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.conversation-time { display: block; margin-top: 6px; color: var(--muted); font-size: 12px; }

.main {
  display: grid;
  grid-template-rows: auto minmax(0,1fr) auto;
  min-width: 0;
  min-height: 0;
  overflow: hidden;
  background: var(--bg);
}
.topbar {
  display: flex;
  justify-content: space-between;
  gap: 20px;
  align-items: center;
  padding: 12px 20px 10px;
  background: var(--bg);
}
.topbar-mobile-row { display: flex; align-items: center; gap: 12px; min-width: 0; }
.compact-title { flex: 0 0 auto; min-width: 0; }
.topbar-title h1,
.compact-title h1 { margin: 0; font-size: 24px; font-weight: 700; line-height: 1.1; }
.topbar-title p { display: none; }
.mobile-sidebar-toggle {
  display: none;
  width: 40px;
  height: 40px;
  place-items: center;
  flex: 0 0 auto;
}
.mobile-sidebar-backdrop { display: none; }

.control-panel { display: flex; gap: 10px; justify-content: flex-end; }
.compact-control-panel { flex: 1 1 auto; min-width: 0; }
.control-group {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 4px;
}
.model-group { min-width: 160px; }
.model-group-plain {
  background: transparent;
  border: none;
  padding: 0;
}
.model-dropdown { position: relative; min-width: 0; width: 100%; }
.model-dropdown-button {
  width: 100%;
  min-height: 38px;
  padding: 0 34px 0 12px;
  border-radius: 14px;
  display: flex;
  align-items: center;
  text-align: left;
  background: linear-gradient(180deg, rgba(255,255,255,.03), rgba(255,255,255,.015)), var(--panel);
  position: relative;
  font-size: 13px;
}
#modelDropdownLabel {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.model-dropdown-caret {
  position: absolute;
  right: 12px;
  top: 50%;
  width: 7px;
  height: 7px;
  border-right: 1.5px solid var(--muted);
  border-bottom: 1.5px solid var(--muted);
  transform: translateY(-65%) rotate(45deg);
  transition: transform .18s ease;
}
.model-dropdown.open .model-dropdown-caret { transform: translateY(-35%) rotate(225deg); }
.model-dropdown-menu {
  position: absolute;
  top: calc(100% + 8px);
  left: 0;
  right: 0;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 18px;
  box-shadow: var(--shadow);
  padding: 8px;
  max-height: 280px;
  overflow: auto;
  z-index: 40;
  display: none;
}
.model-dropdown.open .model-dropdown-menu { display: block; }
.model-option {
  width: 100%;
  border: none;
  background: transparent;
  color: var(--text);
  text-align: left;
  padding: 11px 12px;
  border-radius: 12px;
  cursor: pointer;
}
.model-option:hover,
.model-option.active { background: var(--panel-hover); }
.model-option.empty { color: var(--muted); cursor: default; }

.messages {
  min-height: 0;
  overflow-y: auto;
  overflow-x: hidden;
  padding: 8px 0 18px;
  overscroll-behavior: contain;
}
.empty-state,
.message-row,
.composer-card { width: min(860px, calc(100% - 24px)); margin: 0 auto; }
.empty-state {
  min-height: calc(100vh - 240px);
  display: grid;
  place-items: center;
  text-align: center;
  color: var(--muted);
  padding: 32px 0;
}
.empty-state-box { max-width: 520px; }
.empty-state h2 { margin: 0 0 10px; font-size: 32px; color: var(--text); }
.empty-state p { margin: 0; line-height: 1.7; }

.message-row { display: flex; gap: 14px; padding: 18px 0; }
.user-row { justify-content: flex-end; }
.user-row .message-bubble { text-align: right; max-width: min(78%, 680px); }
.assistant-row .message-bubble { max-width: min(78%, 680px); }
.avatar {
  width: 30px;
  height: 30px;
  flex: 0 0 30px;
  border-radius: 10px;
  display: grid;
  place-items: center;
  font-size: 13px;
  font-weight: 700;
}
.avatar.assistant { background: #fff; color: #111; }
.avatar.user { background: #3a3a3a; color: #fff; }
body.theme-light .avatar.assistant { background: #111; color: #fff; }
body.theme-light .avatar.user { background: #dfe3ea; color: #111; }
.message-bubble { flex: 0 1 auto; min-width: 0; }
.message-role { font-size: 13px; font-weight: 600; margin-bottom: 6px; }
.message-content { color: var(--text); line-height: 1.75; word-break: break-word; }
.message-content p { margin: 0 0 12px; }
.message-content p:last-child { margin-bottom: 0; }
.message-think {
  margin: 0 0 12px;
  border: 1px solid var(--border);
  background: var(--panel-soft);
  border-radius: 16px;
  overflow: hidden;
}
.message-think summary {
  list-style: none;
  cursor: pointer;
  padding: 12px 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  font-size: 13px;
  color: var(--muted);
}
.message-think summary::-webkit-details-marker { display: none; }
.message-think summary::after { content: "+"; font-size: 18px; line-height: 1; color: var(--muted); }
.message-think[open] summary::after { content: "−"; }
.message-think.is-live summary { color: var(--text); }
.message-think.is-live summary span:last-child { color: #7fb3ff; font-variant-numeric: tabular-nums; }
.message-think-body { padding: 0 14px 14px; color: var(--text); border-top: 1px solid var(--border); }
.message-think-body p:first-child { margin-top: 12px; }
.message-content h1, .message-content h2, .message-content h3 { margin: 0 0 12px; line-height: 1.35; }
.message-content ul, .message-content ol { margin: 0 0 12px 20px; padding: 0; }
.message-content li + li { margin-top: 4px; }
.message-content a { color: #7fb3ff; text-decoration: none; }
.message-content a:hover { text-decoration: underline; }
.md-inline-code {
  padding: 2px 6px;
  border-radius: 8px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  font-size: .92em;
}
.md-pre {
  margin: 0 0 12px;
  padding: 14px 16px;
  border-radius: 16px;
  overflow: auto;
  background: #161616;
  border: 1px solid rgba(255,255,255,.08);
}
body.theme-light .md-pre { background: #f3f4f6; border-color: rgba(15,23,42,.08); }
.md-pre code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; }
.message-image-gallery { display: flex; flex-direction: column; gap: 10px; margin-top: 10px; }
.message-inline-image { max-width: min(100%, 420px); border-radius: 18px; display: block; border: 1px solid var(--border); }
.message-attachments { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 10px; }
.message-attachment-chip {
  padding: 8px 10px;
  border-radius: 12px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  font-size: 12px;
  color: var(--muted);
}
.assistant-row { background: rgba(255,255,255,.02); }
body.theme-light .assistant-row { background: rgba(15,23,42,.025); }

.composer-shell { padding: 0 16px 14px; background: var(--bg); }
.composer-card {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 24px;
  box-shadow: var(--shadow);
  padding: 14px 16px 12px;
}
.attachment-list { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 10px; width: 100%; min-width: 0; max-width: 100%; }
.attachment-list:empty { display: none; }
.attachment-chip {
  display: flex;
  align-items: center;
  gap: 8px;
  background: var(--panel-soft);
  border: 1px solid var(--border);
  border-radius: 14px;
  padding: 8px 8px 8px 12px;
  max-width: 100%;
  min-width: 0;
  overflow: hidden;
}
.attachment-chip-main { min-width: 0; flex: 1 1 auto; overflow: hidden; }
.attachment-name { font-size: 13px; font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 100%; }
.attachment-meta { font-size: 12px; color: var(--muted); margin-top: 2px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.attachment-remove { border: none; background: transparent; color: var(--muted); font-size: 18px; cursor: pointer; flex: 0 0 auto; }
#messageInput {
  width: 100%;
  resize: none;
  min-height: 28px;
  max-height: 220px;
  line-height: 1.65;
  padding: 2px 0;
  border: none;
  outline: none;
  background: transparent;
  color: var(--text);
  overflow-y: hidden;
  overscroll-behavior: contain;
}
#messageInput::placeholder, input::placeholder { color: #8f8f8f; }
.composer-bottom {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 40px minmax(0, 1fr) auto;
  align-items: end;
  column-gap: 12px;
}
.attach-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  border-radius: 14px;
  font-size: 22px;
  line-height: 1;
  display: inline-grid;
  place-items: center;
  text-decoration: none;
  user-select: none;
  flex: 0 0 auto;
  align-self: end;
}
.composer-hint { display: none; }
.single-line-actions {
  display: flex;
  justify-content: flex-end;
  flex: 0 0 auto;
  align-self: end;
}
.send-btn {
  padding: 10px 16px;
  background: #fff;
  color: #111;
  border: none;
  font-weight: 600;
  border-radius: 14px;
  min-width: 72px;
  font-size: 14px;
}
.send-btn:disabled { opacity: .5; cursor: not-allowed; }

@media (max-width: 960px) {
  html, body { height: 100%; overflow: hidden; }
  body { overflow: hidden; }

  .app-shell {
    position: relative;
    grid-template-columns: 1fr;
    height: 100dvh;
    min-height: 100dvh;
    overflow: hidden;
  }

  .sidebar {
    position: fixed;
    top: 0;
    left: 0;
    bottom: 0;
    width: min(82vw, 320px);
    max-width: 320px;
    height: 100dvh;
    z-index: 30;
    transform: translate3d(-105%, 0, 0);
    transition: transform .22s ease;
    box-shadow: 0 20px 50px rgba(0,0,0,.28);
    border-right: 1px solid var(--border);
  }
  .app-shell.mobile-sidebar-open .sidebar { transform: translate3d(0, 0, 0); }
  .mobile-sidebar-backdrop {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.35);
    z-index: 20;
  }

  .conversation-item {
    display: grid;
    grid-template-columns: minmax(0, 1fr) 34px;
    gap: 8px;
    align-items: stretch;
  }
  .conversation-delete {
    width: 34px;
    min-width: 34px;
    opacity: .78;
    transform: none;
    pointer-events: auto;
    border-color: var(--border);
  }

  .main {
    height: 100dvh;
    min-height: 0;
    overflow: hidden;
    grid-template-rows: auto minmax(0, 1fr) auto;
  }
  .topbar {
    gap: 8px;
    padding: 2px 12px 6px;
    align-items: stretch;
  }
  .topbar-mobile-row { gap: 8px; }
  .mobile-sidebar-toggle {
    display: inline-grid;
    width: 36px;
    height: 36px;
    place-items: center;
    flex: 0 0 auto;
  }
  .compact-title h1,
  .topbar-title h1 { font-size: 20px; }
  .compact-control-panel { flex: 1 1 auto; width: auto; min-width: 0; justify-content: flex-end; }
  .control-group { width: auto; padding: 4px; border-radius: 14px; }
  .model-group { min-width: 138px; }
  .model-group-plain {
    width: auto;
    min-width: 138px;
    background: transparent;
    border: none;
    padding: 0;
  }
  .model-dropdown-button {
    min-height: 36px;
    padding: 0 34px 0 12px;
    border-radius: 14px;
    font-size: 13px;
  }

  .messages {
    height: 100%;
    -webkit-overflow-scrolling: touch;
  }
  .empty-state,
  .message-row,
  .composer-card { width: min(860px, calc(100% - 20px)); }

  .composer-shell {
    position: relative;
    z-index: 5;
    padding: 6px 10px calc(env(safe-area-inset-bottom, 0px) + 8px);
  }
  .composer-card {
    border-radius: 18px;
    padding: 8px 10px;
    display: flex;
    flex-direction: column;
    gap: 6px;
  }
  .attachment-list {
    width: 100%;
    margin-bottom: 0;
  }
  .composer-bottom {
    margin-top: 0;
    display: grid;
    grid-template-columns: 32px minmax(0, 1fr) auto;
    align-items: end;
    column-gap: 6px;
  }
  #messageInput {
    width: 100%;
    min-height: 32px;
    height: 32px;
    max-height: 220px;
    line-height: 20px;
    padding: 6px 0;
    margin: 0;
    border: none;
    outline: none;
    background: transparent;
    color: var(--text);
    resize: none;
    overflow-y: hidden;
    overscroll-behavior: contain;
    box-sizing: border-box;
    align-self: end;
  }
  .composer-hint {
    display: none;
  }
  .attach-btn {
    width: 32px;
    height: 32px;
    padding: 0;
    border-radius: 10px;
    font-size: 20px;
    align-self: end;
  }
  .single-line-actions {
    display: flex;
    justify-content: flex-end;
    align-self: end;
  }
  .send-btn {
    min-width: 48px;
    height: 32px;
    padding: 0 10px;
    border-radius: 10px;
    font-size: 12px;
    display: inline-grid;
    place-items: center;
  }
}
