/* «Агент»: chat with the agent (OpenCode on FreeLLMAPI, or Claude). */

.ag-page {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font);
  height: 100vh;
  height: 100dvh;
  overflow: hidden;
}

.ag {
  display: flex;
  flex-direction: column;
  height: 100vh;
  height: 100dvh;
  max-width: 860px;
  margin: 0 auto;
}

.ag[hidden],
.ag-off[hidden],
.ag-toast[hidden],
.ag-stop[hidden],
.ag-send[hidden] {
  display: none;
}

/* Top bar */
.ag-bar {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: var(--top) 16px 8px;
}

.ag-title {
  flex: 1;
  min-width: 0;
}

.ag-title h1 {
  margin: 0;
  font-size: 20px;
  font-weight: 700;
}

.ag-sub {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* Router status line */
.ag-status {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 16px 8px;
  font-size: 12px;
  color: var(--muted);
}

.ag-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
  background: var(--subtle);
}

.ag-dot.on { background: var(--accent); }
.ag-dot.off { background: var(--danger); }
.ag-dot.warn { background: #ff9f0a; }

/* Message log */
.ag-log {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding: 8px 16px 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.ag-msg {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 88%;
}

.ag-msg.you {
  align-self: flex-end;
  align-items: flex-end;
}

.ag-msg.agent,
.ag-msg.error {
  align-self: flex-start;
}

.ag-bubble {
  padding: 9px 13px;
  border-radius: 18px;
  font-size: 15px;
  line-height: 1.4;
  word-break: break-word;
}

.ag-msg.you .ag-bubble,
.ag-msg.error .ag-bubble {
  white-space: pre-wrap;
}

.ag-msg.you .ag-bubble {
  background: var(--accent);
  color: var(--accent-ink);
  border-bottom-right-radius: 6px;
}

.ag-msg.agent .ag-bubble {
  background: var(--fill);
  border: .5px solid var(--hairline);
  border-bottom-left-radius: 6px;
}

.ag-msg.error .ag-bubble {
  background: rgba(255, 69, 58, .14);
  color: #ff9f9a;
  border: .5px solid rgba(255, 69, 58, .4);
}

/* Markdown inside agent bubbles */
.ag-bubble.md > :first-child { margin-top: 0; }
.ag-bubble.md > :last-child { margin-bottom: 0; }

.ag-bubble.md p { margin: .4em 0; }

.ag-bubble.md pre {
  background: rgba(0, 0, 0, .35);
  border-radius: 10px;
  padding: 10px;
  overflow-x: auto;
  font-family: var(--mono);
  font-size: 13px;
}

.ag-bubble.md code {
  background: var(--fill-2);
  padding: 1px 5px;
  border-radius: 5px;
  font-family: var(--mono);
  font-size: .9em;
}

.ag-bubble.md pre code {
  background: none;
  padding: 0;
}

.ag-bubble.md ul,
.ag-bubble.md ol {
  padding-left: 1.3em;
  margin: .4em 0;
}

.ag-bubble.md table {
  border-collapse: collapse;
  display: block;
  overflow-x: auto;
  font-size: 13px;
}

.ag-bubble.md th,
.ag-bubble.md td {
  border: .5px solid var(--hairline);
  padding: 4px 8px;
}

.ag-bubble.md a { color: #64d2ff; }

.ag-bubble.md h1,
.ag-bubble.md h2,
.ag-bubble.md h3 {
  font-size: 16px;
  margin: .6em 0 .3em;
}

.ag-bubble.md blockquote {
  margin: .4em 0;
  padding-left: 10px;
  border-left: 3px solid var(--hairline);
  color: var(--muted);
}

.ag-meta {
  font-size: 11px;
  color: var(--subtle);
  padding: 0 6px;
}

/* Collapsed tool steps */
.ag-steps {
  font-size: 12px;
  color: var(--muted);
  padding: 0 6px;
}

.ag-steps summary {
  cursor: pointer;
  list-style: none;
}

.ag-steps summary::-webkit-details-marker { display: none; }

.ag-steps summary::before {
  content: "›";
  display: inline-block;
  margin-right: 6px;
  transition: transform .2s var(--ease-smooth);
}

.ag-steps[open] summary::before { transform: rotate(90deg); }

.ag-steps ol {
  margin: 4px 0 0;
  padding-left: 1.4em;
  max-height: 240px;
  overflow-y: auto;
}

/* Live steps of a running agent */
.ag-live-steps {
  margin: 0;
  padding-left: 1.4em;
  font-size: 12px;
  color: var(--muted);
  max-height: 160px;
  overflow-y: auto;
}

/* Typing dots */
.ag-dots {
  display: inline-flex;
  gap: 3px;
  vertical-align: middle;
  margin-right: 6px;
}

.ag-dots i {
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--muted);
  animation: ag-blink 1.2s infinite;
}

.ag-dots i:nth-child(2) { animation-delay: .2s; }
.ag-dots i:nth-child(3) { animation-delay: .4s; }

@keyframes ag-blink {
  0%, 100% { opacity: .25; }
  50% { opacity: 1; }
}

@media (prefers-reduced-motion: reduce) {
  .ag-dots i { animation: none; }
}

/* Conversation divider */
.ag-divider {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 12px;
  color: var(--subtle);
  text-align: center;
}

.ag-divider::before,
.ag-divider::after {
  content: "";
  flex: 1;
  height: .5px;
  background: var(--hairline);
}

.ag-empty {
  margin: auto;
  text-align: center;
  color: var(--subtle);
  font-size: 15px;
  max-width: 300px;
}

/* Composer */
.ag-input {
  display: flex;
  align-items: flex-end;
  gap: 8px;
  margin: 0 12px max(12px, env(safe-area-inset-bottom));
  padding: 6px 6px 6px 14px;
  border-radius: 22px;
}

.ag-input textarea {
  flex: 1;
  resize: none;
  border: 0;
  background: transparent;
  color: var(--text);
  font-family: var(--font);
  font-size: 16px;
  line-height: 1.35;
  padding: 7px 0;
  max-height: 40vh;
  outline: none;
}

.ag-send,
.ag-stop { flex: none; }

.ag-stop {
  background: var(--danger);
  color: #fff;
}

/* Agent disabled overlay */
.ag-off {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
}

.ag-off-box {
  max-width: 420px;
  text-align: center;
}

.ag-off-box code {
  display: block;
  margin: 12px 0;
  padding: 10px;
  background: var(--fill);
  border-radius: 10px;
  font-family: var(--mono);
  user-select: all;
}

.ag-toast {
  position: fixed;
  left: 50%;
  bottom: 90px;
  transform: translateX(-50%);
  padding: 10px 16px;
  border-radius: 14px;
  font-size: 14px;
  max-width: calc(100vw - 32px);
  z-index: 10;
}

@media (max-width: 600px) {
  .ag-msg { max-width: 94%; }
  /* Phone: title row (back, title, folder, new); the two pills drop to a second row. */
  .ag-bar { padding-left: 12px; padding-right: 12px; flex-wrap: wrap; row-gap: 8px; }
  .ag-title { flex: 1 1 calc(100% - 160px); }
  .ag-bar .ag-engine { order: 10; flex: 1 1 0; min-width: 0; height: 34px; padding: 0 12px; font-size: 14px; overflow: hidden; text-overflow: ellipsis; }
  .ag-log { padding: 8px 12px 12px; }
}

/* Chats: each one works on its own, at the same time. */
.ag-chats {
  display: flex;
  gap: 6px;
  padding: 0 16px 8px;
  overflow-x: auto;
  scrollbar-width: none;
}
.ag-chats::-webkit-scrollbar { display: none; }
.ag-chat {
  display: inline-flex;
  align-items: center;
  flex: none;
  border-radius: 999px;
  background: var(--fill-2);
  color: var(--muted);
  font: 13px var(--font);
}
.ag-chat.on { background: var(--accent); color: var(--accent-ink); }
.ag-chat button {
  border: 0;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  padding: 6px 12px;
}
.ag-chat .ag-chat-x { padding: 6px 10px 6px 0; opacity: .7; }
.ag-chat-add { border: 0; padding: 6px 12px; cursor: pointer; font-weight: 600; }
@media (max-width: 600px) { .ag-chats { padding: 0 12px 8px; } }

/* Image grid in agent responses */
.ag-photos {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 8px;
  margin-top: 8px;
  max-width: 100%;
}

.ag-photo {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  background: var(--fill);
  border: .5px solid var(--hairline);
  aspect-ratio: 1;
}

.ag-photo img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.ag-photo-actions {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  justify-content: space-between;
  padding: 4px;
  background: linear-gradient(transparent, rgba(0,0,0,.8));
  opacity: 0;
  transition: opacity .2s var(--ease-smooth);
}

.ag-photo:hover .ag-photo-actions,
.ag-photo:focus-within .ag-photo-actions {
  opacity: 1;
}

.ag-photo-btn {
  flex: 1;
  margin: 0 2px;
  padding: 4px 8px;
  border: 0;
  border-radius: 6px;
  background: rgba(255,255,255,.9);
  color: var(--text);
  font: 11px var(--font);
  cursor: pointer;
  text-decoration: none;
  text-align: center;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ag-photo-btn:hover {
  background: var(--accent);
  color: var(--accent-ink);
}

@media (max-width: 600px) {
  .ag-photos {
    grid-template-columns: repeat(auto-fill, minmax(80px, 1fr));
    gap: 6px;
  }
  .ag-photo-btn {
    font-size: 10px;
    padding: 3px 5px;
  }
}