:root {
  --bg: #0b0d12;
  --bg-elev: #141821;
  --bg-elev2: #1c2230;
  --border: #262d3d;
  --text: #e6e9f0;
  --text-dim: #9aa3b2;
  --accent: #4f8cff;
  --accent-2: #7aa8ff;
  --user-bg: #2a3550;
  --code-bg: #0d1117;
  --danger: #ff5b6e;
  --success: #35d07f;
  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --radius: 16px;
}

html[data-theme="light"] {
  --bg: #f7f8fb;
  --bg-elev: #ffffff;
  --bg-elev2: #eef1f7;
  --border: #e1e6ef;
  --text: #1a1f2b;
  --text-dim: #5c6678;
  --accent: #2563eb;
  --accent-2: #3b82f6;
  --user-bg: #e4ecff;
  --code-bg: #f2f4f8;
  --danger: #dc2626;
  --success: #16a34a;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto,
    Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  overscroll-behavior: none;
}

#app {
  display: flex;
  height: 100dvh;
  width: 100%;
}

.btn {
  font: inherit;
  color: var(--text);
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 14px;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.05s ease;
  user-select: none;
}
.btn:active {
  transform: scale(0.97);
}
.btn.ghost {
  background: transparent;
}
.btn.ghost:hover {
  background: var(--bg-elev2);
}
.btn.danger {
  color: var(--danger);
  border-color: var(--danger);
  background: transparent;
}
.btn.icon-btn {
  width: 40px;
  height: 40px;
  padding: 0;
  display: grid;
  place-items: center;
  font-size: 18px;
}
.btn.full {
  width: 100%;
}

/* Sidebar */
.sidebar {
  position: fixed;
  top: 0;
  left: 0;
  bottom: 0;
  width: min(82vw, 320px);
  background: var(--bg-elev);
  border-right: 1px solid var(--border);
  transform: translateX(-100%);
  transition: transform 0.25s ease;
  z-index: 40;
  display: flex;
  flex-direction: column;
  padding-top: var(--safe-top);
  padding-bottom: var(--safe-bottom);
}
.sidebar.open {
  transform: translateX(0);
}
.sidebar-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 14px;
  border-bottom: 1px solid var(--border);
}
.sidebar-title {
  font-weight: 600;
}
.chat-list {
  flex: 1;
  overflow-y: auto;
  padding: 8px;
}
.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 12px;
  border-radius: 12px;
  cursor: pointer;
  margin-bottom: 4px;
}
.chat-item.active {
  background: var(--bg-elev2);
}
.chat-item .title {
  flex: 1;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  font-size: 14px;
}
.chat-item .pin {
  font-size: 13px;
  opacity: 0.6;
}
.chat-item .pin.active {
  opacity: 1;
}
.chat-item .acts {
  display: flex;
  gap: 2px;
}
.chat-item .acts button {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 15px;
  padding: 4px;
  cursor: pointer;
}
.sidebar-foot {
  padding: 10px;
  border-top: 1px solid var(--border);
}
.scrim {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  z-index: 30;
}

/* Main */
.main {
  flex: 1;
  display: flex;
  flex-direction: column;
  height: 100dvh;
  width: 100%;
  padding-top: var(--safe-top);
}
.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  backdrop-filter: saturate(160%) blur(16px);
  -webkit-backdrop-filter: saturate(160%) blur(16px);
  position: sticky;
  top: 0;
  z-index: 10;
}
.model-picker {
  flex: 1;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 9px 12px;
  color: var(--text);
  font: inherit;
  font-weight: 600;
  cursor: pointer;
}
.model-name {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.chev {
  color: var(--text-dim);
  font-size: 12px;
}

/* Messages */
.messages {
  flex: 1;
  overflow-y: auto;
  padding: 16px 12px 8px;
  display: flex;
  flex-direction: column;
  gap: 14px;
  -webkit-overflow-scrolling: touch;
}
.msg {
  max-width: 100%;
  display: flex;
  flex-direction: column;
  gap: 6px;
  animation: fade 0.2s ease;
}
@keyframes fade {
  from {
    opacity: 0;
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    transform: none;
  }
}
.msg.user {
  align-items: flex-end;
}
.msg.user .bubble {
  background: var(--user-bg);
  border-radius: 18px 18px 4px 18px;
  max-width: 85%;
}
.msg.assistant .bubble {
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 18px 18px 18px 4px;
  max-width: 100%;
}
.bubble {
  padding: 12px 14px;
  line-height: 1.5;
  font-size: 15px;
  word-wrap: break-word;
  overflow-wrap: anywhere;
}
.bubble > :first-child {
  margin-top: 0;
}
.bubble > :last-child {
  margin-bottom: 0;
}
.bubble p {
  margin: 0 0 10px;
}
.bubble ul,
.bubble ol {
  margin: 0 0 10px;
  padding-left: 22px;
}
.bubble h1,
.bubble h2,
.bubble h3 {
  margin: 12px 0 8px;
  font-size: 1.1em;
}
.bubble a {
  color: var(--accent-2);
}
.bubble code {
  background: var(--code-bg);
  padding: 2px 5px;
  border-radius: 6px;
  font-family: "SF Mono", ui-monospace, Menlo, monospace;
  font-size: 0.9em;
}
.bubble pre {
  margin: 8px 0;
  background: var(--code-bg);
  border: 1px solid var(--border);
  border-radius: 12px;
  overflow: hidden;
}
.code-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 10px;
  background: rgba(127, 127, 127, 0.12);
  font-size: 12px;
  color: var(--text-dim);
}
.code-head button {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 8px;
  padding: 3px 8px;
  font-size: 12px;
  cursor: pointer;
}
.code-head button:active {
  color: var(--success);
}
.bubble pre code {
  display: block;
  padding: 12px;
  overflow-x: auto;
  background: none;
  border-radius: 0;
  font-size: 13px;
  line-height: 1.5;
  white-space: pre;
}
.msg-actions {
  display: flex;
  gap: 8px;
  padding-left: 4px;
}
.msg-actions button {
  background: none;
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 10px;
  padding: 5px 10px;
  font-size: 12px;
  cursor: pointer;
}
.msg-actions button:hover {
  color: var(--text);
}
.typing::after {
  content: "▋";
  animation: blink 1s steps(2) infinite;
  color: var(--accent);
}
@keyframes blink {
  50% {
    opacity: 0;
  }
}

.empty-state {
  margin: auto;
  text-align: center;
  color: var(--text-dim);
  padding: 40px 20px;
}
.empty-state .big {
  font-size: 40px;
  margin-bottom: 12px;
}

.scroll-bottom {
  position: absolute;
  bottom: calc(var(--safe-bottom) + 116px);
  right: 16px;
  width: 40px;
  height: 40px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--bg-elev);
  color: var(--text);
  z-index: 8;
  cursor: pointer;
}

/* Composer */
.composer {
  border-top: 1px solid var(--border);
  background: var(--bg);
  padding: 6px 10px calc(8px + var(--safe-bottom));
  position: relative;
}
.token-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
  color: var(--text-dim);
  padding: 2px 4px 6px;
}
.chip-btn {
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  color: var(--text-dim);
  border-radius: 20px;
  padding: 4px 10px;
  font-size: 12px;
  cursor: pointer;
}
.input-row {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 22px;
  padding: 6px 6px 6px 14px;
}
#input {
  flex: 1;
  border: none;
  background: none;
  color: var(--text);
  font: inherit;
  font-size: 16px;
  resize: none;
  max-height: 140px;
  line-height: 1.4;
  padding: 8px 0;
  outline: none;
}
.send-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  padding: 0;
  background: var(--accent);
  border: none;
  color: #fff;
  font-size: 16px;
  display: grid;
  place-items: center;
  flex-shrink: 0;
}
.send-btn:disabled {
  opacity: 0.5;
}

/* Popover (model menu) */
.popover {
  position: fixed;
  z-index: 60;
  background: var(--bg-elev);
  border: 1px solid var(--border);
  border-radius: 14px;
  box-shadow: 0 10px 40px rgba(0, 0, 0, 0.35);
  padding: 6px;
  min-width: 220px;
  max-width: 90vw;
  max-height: 60vh;
  overflow-y: auto;
}
.pop-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 11px 12px;
  border-radius: 10px;
  cursor: pointer;
  font-size: 14px;
}
.pop-item:hover,
.pop-item.active {
  background: var(--bg-elev2);
}
.pop-item .dot {
  color: var(--success);
  font-size: 10px;
}

/* Settings sheet */
.sheet {
  position: fixed;
  inset: 0;
  z-index: 70;
  background: rgba(0, 0, 0, 0.5);
  display: flex;
  align-items: flex-end;
  justify-content: center;
}
.sheet-inner {
  width: 100%;
  max-width: 560px;
  max-height: 90dvh;
  background: var(--bg-elev);
  border-radius: 20px 20px 0 0;
  display: flex;
  flex-direction: column;
  padding-bottom: var(--safe-bottom);
  animation: slideUp 0.25s ease;
}
@keyframes slideUp {
  from {
    transform: translateY(100%);
  }
  to {
    transform: none;
  }
}
.sheet-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  font-weight: 600;
}
.sheet-body {
  overflow-y: auto;
  padding: 12px 16px 24px;
}
.sec {
  padding: 12px 0;
  border-bottom: 1px solid var(--border);
}
.sec:last-child {
  border-bottom: none;
}
.sec h3 {
  margin: 0 0 8px;
  font-size: 14px;
}
.hint {
  font-size: 12px;
  color: var(--text-dim);
  margin: 0 0 8px;
}
.text-input {
  width: 100%;
  background: var(--bg-elev2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: 12px;
  padding: 12px;
  font: inherit;
  font-size: 15px;
  outline: none;
  margin-bottom: 8px;
}
input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}
.model-key-row {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-bottom: 8px;
}
.model-key-row .name {
  flex: 0 0 40%;
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.model-key-row input {
  flex: 1;
  margin: 0;
  padding: 9px;
  font-size: 13px;
}
.model-key-row .rm {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 14px;
}
.add-model {
  display: flex;
  gap: 8px;
}
.add-model input {
  margin: 0;
}
.row-btns {
  display: flex;
  gap: 8px;
  margin-bottom: 8px;
}
.row-btns > * {
  flex: 1;
  text-align: center;
}
.file-label {
  display: grid;
  place-items: center;
  cursor: pointer;
}

@media (min-width: 720px) {
  .sidebar {
    position: static;
    transform: none;
    width: 300px;
  }
  .scrim,
  #menuBtn {
    display: none;
  }
}
