/* Two-column layout: map on left, chat on right */
#app-grid {
  display: grid;
  grid-template-columns: 1fr 520px; /* left flexible, right fixed chat width */
  gap: 0;
  height: 100vh; /* ensure the grid fills the viewport and prevents page scroll */
}

/* Breakpoints: desktop (default), tablet (<=900px), mobile (<=600px) */

/* Tablet: keep two-column layout but reduce chat width for medium screens */
@media (max-width: 900px) {
  #app-grid {
    grid-template-columns: 1fr 420px; /* slightly narrower chat column on tablets */
  }
  #chatbox {
    height: 100%;
    border-radius: 20px;
  }
  #map {
    height: 100%;
    min-height: 240px;
  }
}

/* Mobile: stacked layout with chat limited to a configurable viewport portion */
@media (max-width: 600px) {
  #app-grid {
    grid-template-columns: 1fr;
    grid-template-rows: 1fr var(--mobile-chat-height);
    height: 100vh;
  }
  #map {
    grid-row: 1 / 2;
    height: 100%;
    min-height: 200px;
  }
  #chatbox {
    grid-row: 2 / 3;
    width: 100%;
    height: auto;
    max-height: var(--mobile-chat-height);
    --input-height: 72px; /* slightly larger input on small devices for touch comfort */
  }
}

#chatbox {
  background: var(--container-bg);
  width: 100%;
  height: 100%; /* let the grid control the height */
  position: relative; /* allow absolutely positioned input to sit at the bottom */
  display: flex;
  flex-direction: column;
  border-radius: 24px;
  box-shadow: 0 10px 40px var(--shadow-color);
  overflow: hidden; /* internal overflow allowed, page stays fixed */
  border: 1px solid var(--border-color);
}

/* Map container styles */
#map {
  width: 100%;
  height: 100%; /* follow grid cell height */
  min-height: 400px;
  position: relative; /* make #map a positioned container so children can be absolutely positioned within it */
}

#header {
  padding: 24px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-shrink: 0;
  background: #1f3252;
  border-top-left-radius: 24px;
  border-top-right-radius: 24px;
}

.header-icon {
  width: 40px;
  height: 40px;
  background-color: var(--icon-bg);
  color: white;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  font-weight: 600;
  font-size: 1.2em;
}

.header-title {
  font-size: 1.5em;
  font-weight: 600;
}

#prompt {
  flex: 1; /* Allow textarea to take available space */
  width: 100%;
  padding: 14px 20px;
  border: 1px solid #ffffff;
  border-radius: 14px;
  background: #0f1724;
  outline: 0.7px solid #ffffff;
  outline-offset: 0px;
  color: var(--text-primary);
  font-size: 1em;
  font-family: 'Inter', sans-serif;
  transition: border-color 0.2s, box-shadow 0.2s;
  resize: none; /* Prevent manual resizing */
  overflow: hidden; /* Remove scrollbar */
  max-height: 200px; /* Set a maximum height to prevent overflow */
  line-height: 1.5; /* Adjust line height for better readability */
}

#prompt:focus {
  outline: none;
  border-color: var(--user-message-bg);
  box-shadow: 0 0 0 3px var(--focus-ring);
}

#prompt::placeholder {
  color: var(--text-secondary);
}

button#send-button {
  position: absolute;
  right: 40px;
  top: 50%;
  transform: translateY(-50%);
  background: transparent;
  border: none;
  cursor: pointer;
  padding: 6px;
  border-radius: 8px;
  color: var(--text-secondary);
  transition: color 0.2s;
}

button#send-button:hover {
  color: var(--text-primary);
}

button#send-button svg {
  width: 22px;
  height: 22px;
}

/* ------------------------------------------------------------------ */
/* Layout specifics to ensure #messages scrolls and respects the input */
/* ------------------------------------------------------------------ */

#messages {
  flex: 1;
  padding: 24px;
  /* reserve space at the bottom so messages don't get hidden behind the input */
  padding-bottom: calc(var(--input-height) + 16px);
  overflow-y: auto;
  /* allow this flex child to shrink so overflow works correctly in modern flex layouts */
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 16px;
}

/* Scrollbar styles for the messages pane (tuned for visibility & subtlety) */
#messages {
  /* Firefox scrollbar tuning */
  scrollbar-width: thin; /* auto | thin | none */
  scrollbar-color: rgba(255, 255, 255, 0.08) transparent;
}

/* WebKit-based browsers (Chrome, Edge, Safari) */
#messages::-webkit-scrollbar {
  width: 8px;
}
#messages::-webkit-scrollbar-track {
  background: transparent;
  margin: 6px 0; /* create breathing room at top/bottom */
}
#messages::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.08);
  border-radius: 6px;
  border: 2px solid transparent; /* keep thumb visually inset */
  background-clip: padding-box;
}
#messages:hover::-webkit-scrollbar-thumb {
  background: rgba(255, 255, 255, 0.14);
}

/* Provide a slightly stronger hover state for accessibility */
#messages::-webkit-scrollbar-thumb:active {
  background: rgba(255, 255, 255, 0.18);
}

/* Keep the input area visually pinned without overlaying the messages scrollbar */
#input-area {
  position: sticky;
  bottom: 0;
  display: flex;
  align-items: center;
  gap: 8px; /* Space between textarea and button */
  padding: 12px 24px;
  border-top: 1px solid var(--bot-message-border);
  background: #0f1724;
  box-shadow: 0 -8px 18px rgba(2, 6, 23, 0.06);
  z-index: 10;
}

/* thin divider inset from the sides */
#input-area::before {
  content: '';
  position: absolute;
  top: 0;
  left: 24px;
  right: 24px;
  height: 1px;
  background: rgba(255, 255, 255, 0.03);
  pointer-events: none;
  border-radius: 1px;
}

/* ------------------------------------------------------------------ */
/* Overlay layout: make the map full-screen and pin chatbox on the right */
/* ------------------------------------------------------------------ */
/* Make the map cover the entire viewport (suitable for Mapbox container) */
#map {
  position: fixed;
  inset: 0; /* top:0; right:0; bottom:0; left:0 */
  width: 100%;
  height: 100%;
  z-index: 0; /* behind the chat overlay */
}

/* Pin the chatbox as an overlay on the right side of the screen */
#chatbox {
  position: fixed;
  top: 24px;
  right: 12px;
  width: 520px; /* match previous grid column width */
  max-width: calc(100% - 48px);
  /* make the chatbox almost full viewport height while keeping margins */
  height: calc(100vh - 48px);
  max-height: calc(100vh - 48px);
  z-index: 9999; /* above map and other UI elements */
  background: var(--container-bg);
  border-radius: 24px;
  overflow: hidden;
  /* stronger base shadow so the chatbox clearly appears above the map */
  box-shadow: 0 20px 60px rgba(2, 6, 23, 0.55), 0 4px 18px rgba(0, 0, 0, 0.22);
  border: 1px solid var(--border-color);
}

/* When the viewport is narrower, reduce chat width and keep it visible */
@media (max-width: 900px) {
  #chatbox {
    width: 420px;
    right: 18px;
    top: 18px;
    height: calc(100vh - 36px);
    max-height: calc(100vh - 36px);
    border-radius: 14px;
  }
}

/* On mobile, move the chatbox to a bottom sheet style for better usability */
@media (max-width: 600px) {
  #chatbox {
    position: fixed;
    left: 8px;
    right: 8px;
    top: 8px;
    bottom: 8px;
    /* occupy almost full height on mobile while keeping small margins */
    height: calc(100vh - 16px);
    max-height: calc(100vh - 16px);
    width: auto;
    border-radius: 12px;
  }
  /* ensure the input area remains visible on mobile and sits at bottom */
  #input-area {
    position: sticky;
    bottom: 0;
    z-index: 10;
  }
}
