:root {
  --bg-0: #101617;
  --bg-1: #171d20;
  --bg-2: #1d272b;
  --bg-3: #243338;
  --panel: rgba(29, 39, 43, 0.9);
  --panel-strong: rgba(21, 28, 31, 0.96);
  --text: #edf1df;
  --muted: #b4c3af;
  --accent: #a7c080;
  --accent-strong: #c0d89a;
  --accent-soft: rgba(167, 192, 128, 0.16);
  --danger: #e67e80;
  --highlight: #d8caac;
  --border: rgba(214, 224, 210, 0.1);
  --shadow: rgba(5, 9, 10, 0.48);
  --radius: 16px;
  --radius-small: 12px;
  --font: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
}

* {
  box-sizing: border-box;
}

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

/* Prefer dynamic viewport units where supported to avoid mobile address-bar resize issues */
:root {
  --app-100vh: 100vh;
}
@supports (height: 100dvh) {
  :root { --app-100vh: 100dvh; }
}

body {
  background: linear-gradient(180deg, var(--bg-0), var(--bg-1));
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  line-height: 1.55;
  /* allow natural page scrolling so the virtual keyboard can resize the viewport */
  overflow: auto;
  min-height: 100%;
}

/* Safe-area helpers */
.safe-bottom-padding {
  padding-bottom: env(safe-area-inset-bottom, 12px);
}

button, input, textarea, select {
  font: inherit;
  color: inherit;
  background: transparent;
}

button {
  border: none;
  padding: 0;
  margin: 0;
  cursor: pointer;
}

input, textarea, select {
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
}

.shell {
  position: relative;
  /* avoid forcing full-viewport sizing to allow natural document scroll */
  min-height: 100%;
  display: grid;
  grid-template-columns: 292px minmax(0, 1fr);
  transition: grid-template-columns 0.2s ease;
}

.shell.sidebar-collapsed {
  grid-template-columns: 0 minmax(0, 1fr);
}

.sidebar {
  background: rgba(21, 28, 31, 0.96);
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  min-height: 0;
  box-shadow: inset -1px 0 0 rgba(255, 255, 255, 0.02);
  transition: transform 0.22s ease, opacity 0.22s ease, width 0.22s ease, border-color 0.22s ease;
  /* detach sidebar scroll from main page: keep sidebar fixed within its column */
  position: sticky;
  top: 0;
  height: calc(100vh - 0px);
  overflow-y: auto;
}

.shell.sidebar-collapsed .sidebar {
  width: 0;
  min-width: 0;
  opacity: 0;
  transform: translateX(-110%);
  pointer-events: none;
  border-right-width: 0;
}

.sidebar-zone {
  padding: 14px 12px;
}

.zone-model {
  border-bottom: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.01);
}

.zone-chats {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  border-bottom: 1px solid var(--border);
}

.zone-providers {
  flex: 0 0 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
}

.zone-chats-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 10px;
}

.zone-label {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  color: var(--muted);
}

#model-select {
  width: 100%;
  background: var(--panel-strong);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  padding: 10px 38px 10px 12px;
  color: var(--text);
  appearance: none;
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
  background-image: linear-gradient(45deg, transparent 50%, var(--muted) 50%), linear-gradient(135deg, var(--muted) 50%, transparent 50%);
  background-position: calc(100% - 18px) calc(50% - 2px), calc(100% - 12px) calc(50% - 2px);
  background-size: 6px 6px;
  background-repeat: no-repeat;
}

.model-search-wrap {
  margin-bottom: 8px;
}

.model-search {
  width: 100%;
  background: rgba(15, 20, 24, 0.28);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  padding: 8px 10px;
  color: var(--text);
}

#model-select:focus-visible,
.field-input:focus-visible,
.chat-item-rename-input:focus-visible,
#provider-name:focus-visible,
#provider-url:focus-visible,
#provider-form-apikey:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.icon-btn,
#new-chat-btn,
.sidebar-toggle {
  width: 32px;
  height: 32px;
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border);
  color: var(--text);
  display: inline-flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.2s ease, border-color 0.2s ease, background 0.2s ease;
}

.sidebar-toggle {
  position: absolute;
  top: 12px;
  left: 12px;
  right: auto;
  z-index: 35;
  background: rgba(21, 28, 31, 0.92);
  box-shadow: 0 10px 24px rgba(5, 9, 10, 0.35);
}

.icon-btn:hover,
#new-chat-btn:hover,
.primary-btn:hover,
.text-btn:hover,
.provider-edit-btn:hover,
#add-provider-btn:hover,
.sidebar-toggle:hover {
  transform: translateY(-1px);
  border-color: rgba(167, 192, 128, 0.4);
}

#chat-list,
#provider-list {
  list-style: none;
  margin: 0;
  padding: 0;
  min-height: 0;
  overflow-y: auto;
}

#chat-list {
  flex: 1 1 auto;
}

.chat-item {
  display: flex;
  align-items: center;
  gap: 8px;
  border-radius: 10px;
  padding: 8px 10px;
  border-left: 2px solid transparent;
  transition: background 0.18s ease, border-color 0.18s ease;
}

.chat-item:hover {
  background: rgba(255, 255, 255, 0.025);
}

.chat-item.active {
  background: rgba(167, 192, 128, 0.08);
  border-left-color: var(--accent);
}

.chat-item-title {
  flex: 1 1 auto;
  text-align: left;
  color: var(--text);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.chat-item-edit {
  color: var(--muted);
  font-size: 12px;
  padding: 3px 5px;
  border-radius: 6px;
}

.chat-item-edit:hover {
  color: var(--accent);
  background: rgba(167, 192, 128, 0.08);
}

.chat-item-rename-input {
  width: 100%;
  padding: 6px 8px;
  background: rgba(15, 20, 24, 0.3);
  color: var(--text);
}

#add-provider-btn {
  width: 100%;
  text-align: left;
  background: rgba(255, 255, 255, 0.02);
  border: 1px solid var(--border);
  border-radius: var(--radius-small);
  padding: 9px 10px;
  color: var(--accent);
  margin-bottom: 8px;
}

.provider-item {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 10px;
  border-radius: 10px;
  transition: background 0.18s ease;
}

.provider-item:hover {
  background: rgba(255, 255, 255, 0.025);
}

.provider-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.provider-edit-btn {
  flex-shrink: 0;
  color: var(--muted);
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.015);
  border-radius: 8px;
  padding: 5px 8px;
}

.provider-empty,
.empty-hint {
  color: var(--muted);
  font-size: 12.5px;
}

.conversation {
  position: relative;
  min-width: 0;
  display: flex;
  flex-direction: column;
  min-height: 0; /* allow natural document flow and body scrolling */
}

.chat-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 16px 24px 12px;
  min-height: 56px;
  border-bottom: 1px solid var(--border);
  background: rgba(14, 18, 21, 0.18);
}

.chat-brand {
  position: absolute;
  top: calc(50% - 48px);
  left: 50%;
  transform: translate(-50%, -50%);
  margin: 0;
  font-size: clamp(2.8rem, 8vw, 7rem);
  font-weight: 700;
  letter-spacing: -0.08em;
  color: rgba(237, 241, 223, 0.16);
  user-select: none;
  pointer-events: none;
}

.chat-model-indicator {
  margin-left: auto;
  padding: 7px 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(255, 255, 255, 0.02);
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
}

#message-stream {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 32px 18px 10px;
}

.message {
  max-width: 760px;
  margin: 0 auto 18px;
  animation: slideUp 0.18s ease;
}

@keyframes slideUp {
  from { opacity: 0; transform: translateY(4px); }
  to { opacity: 1; transform: translateY(0); }
}

.message.message-user {
  /* hug the text, cap at 60% of the stream, and stay anchored to the right edge */
  width: fit-content;
  max-width: 60%;
  margin-left: auto;
  margin-right: 0;
}

.message.message-user .message-body {
  width: 100%;
}

.message.message-assistant {
  max-width: 100%;
  margin: 0 0 18px 0;
}

.message-body {
  background: rgba(29, 39, 43, 0.9);
  border: 1px solid var(--border);
  border-radius: 16px;
  padding: 12px 14px;
  box-shadow: 0 12px 28px var(--shadow);
  overflow-wrap: anywhere;
}

.message.message-user .message-body {
  background: rgba(167, 192, 128, 0.08);
  border-color: rgba(167, 192, 128, 0.22);
}

/* assistant messages: remove visual box and occupy full width */
.message.message-assistant .message-body {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 10px 0;
  max-width: 100%;
}

.message-body.streaming::after {
  content: '▊';
  color: var(--accent);
  display: inline-block;
  margin-left: 6px;
  animation: blink 1s steps(2, start) infinite;
}

@keyframes blink {
  to { visibility: hidden; }
}

/* Live activity indicator shown while the model is working */
.activity-indicator {
  display: inline-flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 10px;
  padding: 6px 12px 6px 10px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(14, 18, 21, 0.55);
  color: var(--muted);
  font-size: 12px;
  line-height: 1;
  animation: slideUp 0.18s ease;
}

.activity-indicator[hidden] {
  display: none;
}

.activity-icon {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 14px;
  height: 14px;
  flex: 0 0 auto;
  color: var(--accent);
}

.activity-icon svg {
  width: 14px;
  height: 14px;
}

.activity-spinner {
  width: 13px;
  height: 13px;
  border-radius: 50%;
  border: 2px solid rgba(167, 192, 128, 0.25);
  border-top-color: currentColor;
  animation: spin 0.7s linear infinite;
}

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

.activity-indicator[data-state="searching"] .activity-icon,
.activity-indicator[data-state="reading"] .activity-icon {
  color: var(--web-accent);
}

.activity-indicator[data-state="searching"] .activity-spinner,
.activity-indicator[data-state="reading"] .activity-spinner {
  border-color: rgba(90, 160, 214, 0.28);
  border-top-color: currentColor;
}

.activity-indicator[data-state="done"] {
  color: var(--accent);
}

.activity-indicator[data-state="error"] {
  color: var(--danger);
  border-color: rgba(230, 126, 128, 0.4);
}

.think-block {
  margin-bottom: 10px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(14, 18, 21, 0.38);
}

.think-block > summary {
  list-style: none;
  cursor: pointer;
  color: var(--muted);
  padding: 7px 10px;
  font-size: 12px;
}

.think-block > summary::-webkit-details-marker { display: none; }
.think-block > summary::before {
  content: '▸';
  margin-right: 6px;
}
.think-block[open] > summary::before {
  content: '▾';
}

.think-body {
  color: var(--muted);
  font-size: 12.5px;
  padding: 0 10px 10px;
  white-space: pre-wrap;
}

.message-body p:first-child { margin-top: 0; }
.message-body p:last-child { margin-bottom: 0; }
.message-body pre {
  background: rgba(15, 20, 24, 0.42);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  overflow-x: auto;
}
.message-body code {
  font-family: ui-monospace, SFMono-Regular, Menlo, monospace;
  background: rgba(15, 20, 24, 0.42);
  border-radius: 6px;
  padding: 2px 6px;
  font-size: 12.5px;
}
.message-body pre code {
  background: transparent;
  padding: 0;
}
.message-body a {
  color: var(--accent);
}
.message-body table {
  width: 100%;
  border-collapse: collapse;
  margin: 10px 0;
}
.message-body th,
.message-body td {
  border: 1px solid var(--border);
  text-align: left;
  padding: 6px 8px;
}
.message-body ul,
.message-body ol {
  padding-left: 20px;
}

/* ==================================
   Composer (bottom message input bar)
   ================================== */
#composer {
  position: sticky;
  bottom: 0;
  flex: 0 0 auto;
  z-index: 30;
  padding: 12px 18px calc(14px + env(safe-area-inset-bottom, 0px));
  border-top: 1px solid var(--border);
  background: linear-gradient(180deg, rgba(14, 18, 21, 0.72), rgba(14, 18, 21, 0.98) 42%);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}

.composer-inner {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  width: 100%;
  max-width: 900px;
  margin: 0 auto;
  padding: 8px 8px 8px 16px;
  background: rgba(37, 45, 49, 0.92);
  border: 1px solid var(--border);
  border-radius: 22px;
  box-shadow: 0 16px 34px var(--shadow);
  transition: border-color 0.2s ease, box-shadow 0.2s ease;
}

.composer-inner:focus-within {
  border-color: rgba(167, 192, 128, 0.5);
  box-shadow: 0 16px 34px var(--shadow), 0 0 0 3px var(--accent-soft);
}

#composer-input {
  flex: 1 1 auto;
  min-width: 0;
  min-height: 40px;
  max-height: 200px;
  padding: 9px 0;
  border: none;
  border-radius: 0;
  background: transparent;
  color: var(--text);
  line-height: 1.5;
  resize: none;
  box-sizing: border-box;
}

#composer-input::placeholder {
  color: var(--muted);
  opacity: 0.8;
}

#composer-input:focus,
#composer-input:focus-visible {
  outline: none;
}

.action-stack {
  display: flex;
  flex-direction: row;
  align-items: center;
  gap: 8px;
  flex: 0 0 auto;
}

.action-btn {
  width: 40px;
  height: 40px;
  border-radius: 12px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  background: rgba(255, 255, 255, 0.03);
  border: 1px solid var(--border);
  color: var(--text);
  transition: background 0.18s ease, border-color 0.18s ease, color 0.18s ease, transform 0.18s ease;
}

.action-btn svg {
  width: 18px;
  height: 18px;
}

.action-btn:hover:not(:disabled) {
  transform: translateY(-1px);
}

.action-btn.web-search:hover:not(:disabled) {
  color: var(--web-accent);
  border-color: rgba(90, 160, 214, 0.5);
  background: rgba(90, 160, 214, 0.1);
}

.action-btn.web-search.active {
  background: var(--web-accent-bg);
  color: var(--web-accent);
  border-color: rgba(90, 160, 214, 0.55);
  box-shadow: 0 0 0 3px rgba(90, 160, 214, 0.12);
}

.action-btn.send {
  background: var(--accent);
  border-color: transparent;
  color: var(--bg-0);
  border-radius: 50%;
  box-shadow: 0 8px 18px rgba(167, 192, 128, 0.28);
}

.action-btn.send:hover:not(:disabled) {
  background: var(--accent-strong);
}

.action-btn.send:active:not(:disabled) {
  transform: scale(0.94);
}

.action-btn.send:disabled {
  opacity: 0.5;
  cursor: not-allowed;
  box-shadow: none;
}

:root {
  --web-accent: #5aa0d6;
  --web-accent-bg: rgba(90, 160, 214, 0.16);
}

.primary-btn,
.secondary-btn,
.text-btn {
  border-radius: 10px;
  transition: transform 0.18s ease, opacity 0.18s ease, background 0.18s ease;
}


.overlay {
  position: fixed;
  inset: 0;
  background: rgba(15, 20, 24, 0.6);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 20;
}

.overlay[hidden] {
  display: none;
}

.overlay-panel {
  width: min(420px, calc(100vw - 32px));
  background: var(--bg-2);
  border: 1px solid var(--border);
  border-radius: 18px;
  padding: 22px 18px 16px;
  box-shadow: 0 20px 40px rgba(15, 20, 24, 0.38);
}

.overlay-title {
  margin: 0 0 12px;
  font-size: 18px;
}

.field-label {
  display: block;
  margin: 10px 0 6px;
  color: var(--muted);
  font-size: 11.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.field-input,
#provider-name,
#provider-url,
#provider-form-apikey {
  width: 100%;
  padding: 10px 12px;
  background: rgba(15, 20, 24, 0.28);
  border: 1px solid var(--border);
  color: var(--text);
}

.overlay-actions {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-top: 18px;
}

.overlay-actions-right {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-left: auto;
}

.secondary-btn,
.primary-btn,
.text-btn {
  padding: 8px 12px;
  border: 1px solid var(--border);
}

.secondary-btn {
  background: rgba(255, 255, 255, 0.02);
  color: var(--text);
}

.primary-btn {
  background: var(--accent);
  border-color: transparent;
  color: var(--bg-1);
  font-weight: 700;
}

.text-btn {
  background: transparent;
  color: var(--text);
}

#provider-delete-btn {
  color: var(--danger);
  border-color: rgba(230, 126, 128, 0.4);
}

.status-banner {
  position: absolute;
  left: 50%;
  top: 12px;
  transform: translateX(-50%);
  z-index: 25;
  max-width: min(560px, calc(100vw - 32px));
  font-size: 12px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: rgba(21, 28, 31, 0.96);
  box-shadow: 0 12px 24px rgba(15, 20, 24, 0.26);
}

.status-banner.success {
  color: var(--accent);
}

.status-banner.error {
  color: var(--danger);
}

@media (max-width: 980px), (pointer: coarse) {
  .shell {
    display: block;
  }

  .mobile-layout .sidebar {
    position: absolute;
    inset: 0 auto 0 0;
    width: min(82vw, 320px);
    z-index: 70; /* keep sidebar above the composer */
    box-shadow: 18px 0 40px rgba(5, 9, 10, 0.38);
    border-right: 1px solid var(--border);
  }

  /* composer should be behind the overlaying sidebar on mobile */
  .mobile-layout #composer {
    z-index: 20;
  }

  .mobile-layout .sidebar-toggle {
    left: 12px;
    right: auto;
    top: 12px;
  }

  .mobile-layout.sidebar-collapsed .sidebar {
    transform: translateX(-105%);
    opacity: 0;
    pointer-events: none;
  }

  .mobile-layout:not(.sidebar-collapsed) .sidebar {
    transform: translateX(0);
    opacity: 1;
    pointer-events: auto;
  }

  .mobile-layout .conversation {
    width: 100%;
    height: 100vh;
  }

  #message-stream {
    padding-top: 24px;
  }

  #composer {
    padding-right: 18px;
  }

  .status-banner {
    top: 12px;
    left: 50%;
    max-width: calc(100vw - 40px);
  }
}

@media (min-width: 981px) {
  .sidebar-toggle {
    display: none;
  }
}

* {
  scrollbar-width: thin;
  scrollbar-color: rgba(255, 255, 255, 0.12) transparent;
}

::-webkit-scrollbar {
  width: 9px;
}

::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 999px;
}
