/* =============================================================
   components/ask-ai.css
   "Ask AI" panel (Figma: Live Map → right column, 306px wide).
   Hidden until the Ask AI button on the map is clicked; then it
   slides in as a third page column (see .app.is-ask-ai-open).
   ============================================================= */

/* The panel lives in a third grid column that is 0 wide while
   closed. Opening animates that column to the panel's width, so the
   map shrinks and the panel slides in from the right; closing runs
   it in reverse. ask-ai.js keeps the panel visible until the
   closing animation ends. MapLibre watches its container size, so
   the map resizes by itself.

   The sidebar → map space is a margin on the map, and the map →
   panel space is a margin on the panel, so a closed (0 wide)
   column adds no extra gap. */
.app {
  grid-template-columns: var(--sidebar-width) 1fr 0px;
  column-gap: 0;
  overflow-x: clip; /* hide the panel while it slides */
  transition: grid-template-columns var(--ask-ai-duration) ease;
}

.app.is-ask-ai-open {
  grid-template-columns: var(--sidebar-width) 1fr calc(var(--ask-ai-width) + var(--page-gap));
}

.map-frame {
  margin-left: var(--page-gap);
}

.ask-ai-panel {
  grid-column: 3;
  grid-row: 1;
  justify-self: start;
  width: var(--ask-ai-width); /* set in tokens.css */
  margin-left: var(--page-gap);
  display: flex;
  flex-direction: column;
  min-height: 0;
  overflow: hidden;
  border-radius: var(--radius-card);
  background: var(--surface-base);
  box-shadow: 0 0 16px rgba(24, 0, 44, 0.1);
}

.ask-ai-panel[hidden] {
  display: none;
}

/* ---------- Header: icon + title, close button ---------- */
.ask-ai-panel__head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border-00);
}

.ask-ai-panel__title {
  display: flex;
  align-items: center;
  gap: 8px;
}

.ask-ai-panel__title h2 {
  font-size: var(--p3-size);
  line-height: normal;
  font-weight: var(--weight-semibold);
  color: var(--color-nav);
}

.ask-ai-panel__close {
  display: grid;
  place-items: center;
  width: 24px;
  height: 24px;
  border-radius: 7.385px;
}

.ask-ai-panel__close:hover {
  background: var(--grey-50);
}

.ask-ai-panel__actions {
  display: flex;
  align-items: center;
  gap: 4px;
}

/* "New chat": clears the thread and starts over */
.ask-ai-panel__new {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  height: 24px;
  padding: 0 8px;
  border-radius: 7.385px;
  font-size: var(--caption-2-size);
  font-weight: var(--weight-semibold);
  color: var(--color-nav);
}

.ask-ai-panel__new[hidden] {
  display: none;
}

.ask-ai-panel__new:hover {
  background: var(--grey-50);
}

/* ---------- Body: greeting + suggestions, centred ---------- */
.ask-ai-panel__body {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  justify-content: safe center; /* top-align if the suggestions overflow */
  gap: 32px;
  padding: 16px 10px;
}

.ask-ai-panel__intro {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.ask-ai-panel__text {
  display: flex;
  flex-direction: column;
  gap: 8px;
  text-align: center;
}

.ask-ai-panel__greeting {
  display: flex;
  flex-direction: column;
  font-size: var(--p1-size);
  line-height: var(--p1-line);
}

.ask-ai-panel__greeting span {
  font-weight: var(--weight-medium);
  color: var(--display-subtle-02);
}

.ask-ai-panel__greeting strong {
  font-weight: var(--weight-semibold);
  color: var(--display-02);
}

.ask-ai-panel__desc {
  font-size: var(--caption-1-size);
  line-height: var(--caption-1-line);
  color: var(--display-subtle-01);
}

/* Suggested questions: full-width grey tiles. */
.ask-ai-panel__suggestions {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.ask-ai-chip {
  width: 100%;
  padding: 8px;
  border-radius: var(--radius-tile);
  background: var(--surface-lvl-00);
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
  color: var(--display-subtle-01);
  text-align: center;
}

.ask-ai-chip:hover {
  background: var(--grey-100);
}

/* ---------- Footer: message box with send button ---------- */
.ask-ai-panel__form {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 10px 10px 16px;
}

.ask-ai-input {
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  justify-content: space-between;
  min-height: 77px;
  padding: 4px;
  border-radius: var(--radius-tile);
  background: var(--surface-lvl-00);
}

.ask-ai-input textarea {
  width: 100%;
  padding: 4px;
  border: 0;
  background: none;
  resize: none;
  font: inherit;
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
  color: var(--grey-900);
}

.ask-ai-input textarea::placeholder {
  color: var(--display-subtle-02);
}

.ask-ai-input textarea:focus {
  outline: none;
}

.ask-ai-input:focus-within {
  outline: 2px solid var(--green-500);
  outline-offset: -2px;
}

/* Round send button: grey when empty, green when there is text. */
.ask-ai-input__send {
  display: grid;
  place-items: center;
  margin: 4px;
  padding: 4px;
  border-radius: 14px;
  background: var(--btn-primary-disable);
}

.ask-ai-input__send:not(:disabled) {
  background: var(--green-500);
}

.ask-ai-input__send:not(:disabled) img {
  filter: brightness(0) invert(1); /* white plane on green */
}

.ask-ai-input__send:disabled {
  cursor: default;
}

/* ---------- Conversation ----------
   Once the first question is sent, the greeting and suggestions
   give way to the thread, which starts at the top. */
.ask-ai-panel__body.has-messages {
  justify-content: flex-start;
}

.ask-ai-panel__body.has-messages .ask-ai-panel__intro,
.ask-ai-panel__body.has-messages .ask-ai-panel__suggestions {
  display: none;
}

.ask-ai-thread {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.ask-ai-thread:empty {
  display: none;
}

.ask-ai-msg {
  max-width: 100%;
  font-size: var(--caption-1-size);
  line-height: var(--caption-1-line);
  color: var(--display-body);
  overflow-wrap: anywhere;
}

.ask-ai-msg--user {
  align-self: flex-end;
  max-width: 85%;
  padding: 8px 12px;
  border-radius: var(--radius-tile) var(--radius-tile) 2px var(--radius-tile);
  background: var(--green-25);
  border: 1px solid var(--green-100);
  color: var(--grey-900);
  white-space: pre-wrap;
}

.ask-ai-msg--assistant {
  align-self: stretch;
  padding: 10px 12px;
  border-radius: var(--radius-tile);
  background: var(--grey-50);
  color: var(--grey-900);
}

.ask-ai-msg__status {
  color: var(--display-subtle-01);
}

/* Pulsing dots while the agent works. */
.ask-ai-msg.is-pending .ask-ai-msg__status::after {
  content: '';
  display: inline-block;
  width: 1.2em;
  text-align: left;
  animation: ask-ai-dots 1.2s steps(4, end) infinite;
}

@keyframes ask-ai-dots {
  0%   { content: ''; }
  25%  { content: '.'; }
  50%  { content: '..'; }
  75%  { content: '...'; }
}

/* Queries the agent ran: live while working, folded after. */
.ask-ai-msg__steps {
  margin: 6px 0 0;
  padding-left: 16px;
  list-style: disc;
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
  color: var(--display-subtle-02);
}

.ask-ai-msg__steps:empty {
  display: none;
}

.ask-ai-msg__steps .is-failed {
  text-decoration: line-through;
}

.ask-ai-msg__details {
  margin-top: 8px;
  font-size: var(--caption-2-size);
  color: var(--display-subtle-02);
}

.ask-ai-msg__details summary {
  cursor: pointer;
}

/* Markdown answer */
.ask-ai-msg__text > * + * {
  margin-top: 8px;
}

/* "More details": the text after the table, folded by default. */
.ask-ai-more > summary {
  cursor: pointer;
  width: fit-content;
  font-weight: var(--weight-medium);
  color: var(--grey-700);
}

.ask-ai-more > summary:hover {
  color: var(--grey-900);
}

.ask-ai-more > * + * {
  margin-top: 8px;
}

.ask-ai-msg__text h4 {
  font-size: var(--caption-1-size);
  font-weight: var(--weight-semibold);
  color: var(--display-02);
}

.ask-ai-msg__text strong {
  font-weight: var(--weight-semibold);
  color: var(--grey-900);
}

.ask-ai-msg__text ul,
.ask-ai-msg__text ol {
  padding-left: 18px;
}

.ask-ai-msg__text ul { list-style: disc; }
.ask-ai-msg__text ol { list-style: decimal; }

.ask-ai-msg__text code {
  padding: 0 3px;
  border-radius: 3px;
  background: var(--grey-100);
  font-size: 0.92em;
}

.ask-ai-table {
  overflow-x: auto;
}

.ask-ai-table table {
  width: 100%;
  border-collapse: collapse;
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
}

.ask-ai-table th,
.ask-ai-table td {
  padding: 2px 6px;
  border-bottom: 1px solid var(--grey-100);
  text-align: left;
  white-space: nowrap;
}

.ask-ai-table th {
  font-weight: var(--weight-semibold);
}

/* ---------- Show on map ----------
   Buttons under an answer, one per step that found places. */
.ask-ai-msg__maps {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 10px;
}

.ask-ai-map-btn {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  max-width: 100%;
  padding: 4px 8px;
  border: 1px solid var(--green-100);
  border-radius: var(--radius-tile);
  background: var(--surface-base);
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
  font-weight: var(--weight-medium);
  color: var(--grey-900);
  text-align: left;
}

.ask-ai-map-btn span {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ask-ai-map-btn:hover {
  background: var(--green-25);
}

.ask-ai-map-btn.is-active {
  border-color: var(--green-500);
  background: var(--green-25);
}

.ask-ai-map-btn.is-loading {
  cursor: progress;
  opacity: 0.6;
}

.ask-ai-msg__map-error {
  width: 100%;
  font-size: var(--caption-2-size);
  color: #962320;
}

/* Chip on the map while an answer is drawn (under the search bar). */
.ai-layer-chip {
  top: 50px;
  left: 9px;
  z-index: 1; /* under the search suggestions */
  display: flex;
  align-items: center;
  gap: 6px;
  max-width: 279px;
  padding: 4px 4px 4px 8px;
  border-radius: var(--radius-tile);
  box-shadow: 0 0 8px rgba(24, 0, 44, 0.1);
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
  font-weight: var(--weight-medium);
  color: var(--grey-900);
}

.ai-layer-chip[hidden] {
  display: none;
}

.ai-layer-chip__title {
  flex: 1;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.ai-layer-chip__clear {
  display: grid;
  place-items: center;
  width: 20px;
  height: 20px;
  border-radius: 6px;
}

.ai-layer-chip__clear:hover {
  background: var(--grey-50);
}

/* Microzone popup */
.ai-layer-popup {
  display: flex;
  flex-direction: column;
  gap: 2px;
  font-size: var(--caption-2-size);
  line-height: var(--caption-2-line);
}

/* Green roof estimate in a building popup. */
.ai-layer-popup__title {
  margin-top: 6px;
  font-weight: var(--weight-medium);
}

/* Cost table: the answer table style, numbers right-aligned. */
.ai-layer-popup__table {
  margin-top: 2px;
}

.ai-layer-popup__table td {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

.ai-layer-popup__sources {
  margin-top: 4px;
  color: var(--grey-700);
}

.ai-layer-popup__sources a {
  color: inherit;
  text-decoration: underline;
}

/* ---------- Small screens: panel covers the page ----------
   It slides in from the right over everything instead. */
@media (max-width: 768px) {
  .app,
  .app.is-ask-ai-open {
    grid-template-columns: 1fr;
    column-gap: normal;
    transition: none;
  }

  .map-frame {
    margin-left: 0;
  }

  .ask-ai-panel {
    width: auto;
    margin-left: 0;
    position: fixed;
    inset: 8px;
    z-index: 10;
    transform: translateX(calc(100% + 8px));
    transition: transform var(--ask-ai-duration) ease;
  }

  .app.is-ask-ai-open .ask-ai-panel {
    transform: none;
  }
}

/* No sliding for people who ask for less motion. */
@media (prefers-reduced-motion: reduce) {
  .app,
  .ask-ai-panel {
    transition: none;
  }
}
