:root {
  --bg: #ffffff;
  --bg2: #f4f5f7;
  --card: #f1f3f6;
  --text: #14171f;
  --hint: #6b7280;
  --line: rgba(17, 24, 39, 0.08);
  --accent: #2f6fed;
  --accent-text: #ffffff;
  --danger: #e5484d;
  --bubble: #2f6fed;
  --bubble-text: #ffffff;
  --shadow: 0 8px 24px rgba(15, 23, 42, 0.08);
  --radius: 18px;
}

body.dark {
  --bg: #0f1115;
  --bg2: #171a21;
  --card: #1b1f28;
  --text: #e9edf3;
  --hint: #8b93a3;
  --line: rgba(255, 255, 255, 0.08);
  --accent: #4c8dff;
  --accent-text: #0b1020;
  --danger: #ff5d61;
  --bubble: #2b5fd9;
  --bubble-text: #ffffff;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.35);
}

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

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "SF Pro Text", "Segoe UI", Roboto, system-ui, sans-serif;
  overscroll-behavior: none;
}

body {
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}

.app {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  max-width: 720px;
  margin: 0 auto;
}

/* ---------------------------------------------------------------- шапка */

.topbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px 6px;
  background: color-mix(in srgb, var(--bg) 86%, transparent);
  backdrop-filter: blur(14px);
  position: sticky;
  top: 0;
  z-index: 5;
}

.topbar-title { flex: 1; min-width: 0; text-align: center; }
.topbar-name { font-weight: 650; font-size: 16px; letter-spacing: -0.01em; }
.topbar-sub {
  font-size: 11.5px;
  color: var(--hint);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s ease;
}
.icon-btn:active { background: var(--card); }

/* ---------------------------------------------------------------- контролы */

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 4px 12px 10px;
}

.segmented {
  position: relative;
  display: flex;
  padding: 3px;
  border-radius: 13px;
  background: var(--card);
  flex: 0 0 auto;
}

.segmented-thumb {
  position: absolute;
  top: 3px;
  left: 3px;
  width: calc(50% - 3px);
  height: calc(100% - 6px);
  border-radius: 10px;
  background: var(--bg);
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.14);
  transition: transform 0.22s cubic-bezier(0.32, 0.72, 0, 1);
}
.segmented[data-active="codex"] .segmented-thumb { transform: translateX(100%); }

.seg-btn {
  position: relative;
  z-index: 1;
  border: 0;
  background: transparent;
  color: var(--hint);
  font: inherit;
  font-size: 13px;
  font-weight: 600;
  padding: 7px 14px;
  border-radius: 10px;
  cursor: pointer;
  transition: color 0.2s ease;
}
.seg-btn.is-active { color: var(--text); }

.project {
  position: relative;
  flex: 1;
  min-width: 0;
  display: flex;
  align-items: center;
}
.project select {
  appearance: none;
  width: 100%;
  border: 0;
  border-radius: 13px;
  background: var(--card);
  color: var(--text);
  font: inherit;
  font-size: 13px;
  font-weight: 550;
  padding: 9px 30px 9px 12px;
  text-overflow: ellipsis;
}
.project-arrow {
  position: absolute;
  right: 9px;
  color: var(--hint);
  pointer-events: none;
}

/* ---------------------------------------------------------------- лента */

.messages {
  flex: 1;
  overflow-y: auto;
  padding: 6px 14px 18px;
  scroll-behavior: smooth;
  -webkit-overflow-scrolling: touch;
}

.empty {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  height: 100%;
  text-align: center;
  padding: 20px 8px 40px;
  color: var(--hint);
  animation: fade 0.4s ease;
}
.empty-logo { color: var(--accent); opacity: 0.9; margin-bottom: 6px; }
.empty h1 { margin: 8px 0 6px; font-size: 20px; color: var(--text); letter-spacing: -0.02em; }
.empty p { margin: 0; font-size: 13.5px; line-height: 1.5; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 18px; }
.chip {
  border: 1px solid var(--line);
  background: var(--card);
  color: var(--text);
  border-radius: 999px;
  padding: 8px 13px;
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
}
.chip:active { transform: scale(0.97); }

.msg { display: flex; margin: 10px 0; animation: rise 0.18s ease; }
.msg.user { justify-content: flex-end; }
.msg.assistant { justify-content: flex-start; }

.bubble {
  max-width: 86%;
  padding: 9px 13px;
  border-radius: var(--radius);
  white-space: pre-wrap;
  word-break: break-word;
  overflow-wrap: anywhere;
}
.msg.user .bubble {
  background: var(--bubble);
  color: var(--bubble-text);
  border-bottom-right-radius: 6px;
}
.msg.assistant .bubble {
  background: var(--card);
  color: var(--text);
  border-bottom-left-radius: 6px;
}
.msg.error .bubble {
  background: color-mix(in srgb, var(--danger) 14%, transparent);
  color: var(--danger);
}

.bubble p { margin: 0 0 8px; }
.bubble p:last-child { margin-bottom: 0; }
.bubble ul, .bubble ol { margin: 4px 0 8px; padding-left: 20px; }
.bubble li { margin: 2px 0; }
.bubble code {
  font: 13px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  background: rgba(127, 127, 127, 0.16);
  padding: 1px 5px;
  border-radius: 5px;
}
.bubble pre {
  margin: 8px 0;
  padding: 10px 12px;
  border-radius: 12px;
  background: rgba(127, 127, 127, 0.16);
  overflow-x: auto;
}
.bubble pre code { background: none; padding: 0; white-space: pre; }
.bubble a { color: var(--accent); }
.msg.user .bubble a { color: inherit; text-decoration: underline; }
.bubble strong { font-weight: 650; }

.statusline {
  display: flex;
  align-items: center;
  gap: 8px;
  margin: 2px 0 0 4px;
  color: var(--hint);
  font-size: 12.5px;
  min-height: 20px;
}
.dots { display: inline-flex; gap: 3px; }
.dots i {
  width: 5px; height: 5px; border-radius: 50%;
  background: currentColor; opacity: 0.5;
  animation: pulse 1.2s infinite ease-in-out;
}
.dots i:nth-child(2) { animation-delay: 0.15s; }
.dots i:nth-child(3) { animation-delay: 0.3s; }

.logs {
  margin: 6px 0 0 4px;
  font: 11.5px/1.4 ui-monospace, SFMono-Regular, Menlo, monospace;
  color: var(--hint);
  max-height: 96px;
  overflow: hidden;
  opacity: 0.85;
}
.logs div { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

/* ---------------------------------------------------------------- ввод */

.composer-wrap {
  padding: 8px 12px calc(10px + env(safe-area-inset-bottom));
  background: linear-gradient(to top, var(--bg) 55%, transparent);
  position: sticky;
  bottom: 0;
}

.composer {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  padding: 6px 6px 6px 14px;
  border-radius: 22px;
  background: var(--card);
  border: 1px solid var(--line);
  transition: border-color 0.15s ease;
}
.composer:focus-within { border-color: color-mix(in srgb, var(--accent) 40%, var(--line)); }

.composer textarea {
  flex: 1;
  border: 0;
  outline: none;
  resize: none;
  background: transparent;
  color: var(--text);
  font: inherit;
  font-size: 15px;
  line-height: 1.4;
  max-height: 132px;
  padding: 8px 0;
}
.composer textarea::placeholder { color: var(--hint); }

.attach-btn {
  flex: 0 0 auto;
  width: 34px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 12px;
  background: transparent;
  color: var(--hint);
  cursor: pointer;
}
.attach-btn:active { color: var(--text); }
.attach-btn.uploading { color: var(--accent); animation: pulse 1s infinite; }

.send-btn {
  flex: 0 0 auto;
  width: 40px;
  height: 40px;
  display: grid;
  place-items: center;
  border: 0;
  border-radius: 50%;
  background: var(--accent);
  color: var(--accent-text);
  cursor: pointer;
  transition: transform 0.12s ease, opacity 0.15s ease;
}
.send-btn:active { transform: scale(0.94); }
.send-btn:disabled { opacity: 0.4; }
.stop-btn { background: color-mix(in srgb, var(--danger) 18%, transparent); color: var(--danger); }
.hidden { display: none !important; }

/* ---------------------------------------------------------------- шторка */

.sheet-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.45);
  backdrop-filter: blur(2px);
  z-index: 20;
  animation: fade 0.18s ease;
}

.sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  max-height: 78dvh;
  display: flex;
  flex-direction: column;
  background: var(--bg2);
  border-radius: 20px 20px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  animation: slideUp 0.24s cubic-bezier(0.32, 0.72, 0, 1);
  box-shadow: 0 -12px 40px rgba(0, 0, 0, 0.3);
}
.sheet-handle {
  width: 38px; height: 4px; border-radius: 2px;
  background: var(--line); margin: 8px auto 0;
}
.sheet-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px 8px 16px;
  font-weight: 650;
  font-size: 15px;
}
.sheet-body { overflow-y: auto; padding: 0 16px 20px; font-size: 14px; }

.row {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 11px 0;
  border-bottom: 1px solid var(--line);
}
.row:last-child { border-bottom: 0; }

.section {
  padding: 16px 0 4px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.07em;
  text-transform: uppercase;
  color: var(--hint);
}
.section:first-child { padding-top: 4px; }
.row .grow { flex: 1; min-width: 0; }
.row .muted { color: var(--hint); font-size: 12px; }
.row button {
  border: 0; background: var(--card); color: var(--text);
  border-radius: 10px; padding: 7px 11px; font: inherit; font-size: 13px;
}

.mono {
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-word;
  background: var(--card);
  border-radius: 12px;
  padding: 10px 12px;
  max-height: 46dvh;
  overflow: auto;
}

/* ---------------------------------------------------------------- прочее */

.toast {
  position: fixed;
  left: 50%;
  bottom: calc(92px + env(safe-area-inset-bottom));
  transform: translate(-50%, 12px);
  max-width: 88vw;
  padding: 9px 14px;
  border-radius: 12px;
  background: rgba(20, 23, 31, 0.94);
  color: #fff;
  font-size: 13px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s ease, transform 0.2s ease;
  z-index: 30;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@keyframes pulse {
  0%, 100% { opacity: 0.25; transform: translateY(0); }
  50% { opacity: 1; transform: translateY(-2px); }
}
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
@keyframes rise { from { opacity: 0; transform: translateY(4px); } to { opacity: 1; transform: none; } }
@keyframes slideUp { from { transform: translateY(100%); } to { transform: none; } }

@media (prefers-reduced-motion: reduce) {
  * { animation: none !important; transition: none !important; }
}
