/* Standalone host page only: the chat panel brings its own styles (packages/map-chat/ui/styles.js).
   The page look comes from the city's "page" config (cities/<city>.json): body.tpl-default is the plain dev page,
   body.tpl-instant-basic recreates an ArcGIS Instant Apps "Basic" app (header + map widgets) around the panel. */
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  overflow: hidden;
  background: #e9ecef;
  color: #1c2127;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
.stage { position: absolute; inset: 0; overflow: hidden; }
#map-host, #map-host > arcgis-map { position: absolute; inset: 0; width: 100%; height: 100%; }

.map-status {
  position: absolute; top: 16px; left: 50%; transform: translateX(-50%); z-index: 5; margin: 0;
  padding: 8px 14px; border-radius: 8px; background: #fff; color: #1c2127; font-size: 14px;
  box-shadow: 0 2px 10px rgba(0, 0, 0, .15);
}

/* Until the city's page config has been read, the panel isn't placed yet. */
.tpl-loading .chat-host { visibility: hidden; }

/* =============================== default page =============================== */

/* The panel fills this box; the page decides where it floats. */
.tpl-default .chat-host, .tpl-loading .chat-host {
  position: absolute; z-index: 10; top: 16px; right: 16px; bottom: 32px;
  width: min(400px, calc(100vw - 32px));
  border-radius: 12px; overflow: hidden;
  border: 1px solid rgba(15, 23, 32, .1);
  box-shadow: 0 1px 3px rgba(15, 23, 32, .1), 0 10px 30px rgba(15, 23, 32, .16);
}
/* Collapsed: shrink to the launcher pill. */
.tpl-default .chat-host.is-collapsed {
  top: auto; bottom: 36px; width: auto; height: auto;
  border: 0; border-radius: 0; box-shadow: none; overflow: visible;
}

/* Phones: bottom sheet. */
@media (max-width: 640px) {
  .tpl-default .chat-host {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; height: min(62dvh, 560px);
    border-radius: 14px 14px 0 0; border-width: 1px 0 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  .tpl-default .chat-host.is-collapsed { left: auto; right: 12px; bottom: calc(28px + env(safe-area-inset-bottom)); height: auto; padding: 0; }
}

/* Short viewports (landscape phones, 200% zoom): the panel takes the full height instead of leaving a gap above it. */
@media (max-height: 520px) {
  .tpl-default .chat-host { top: 0; height: auto; border-radius: 0; }
}

@media (prefers-color-scheme: dark) {
  .tpl-default { background: #101214; color: #e4e7ea; }
  .tpl-default .map-status { background: #17191c; color: #e4e7ea; }
  .tpl-default .chat-host { border-color: rgba(255, 255, 255, .09); box-shadow: 0 1px 3px rgba(0, 0, 0, .4), 0 10px 30px rgba(0, 0, 0, .45); }
  .tpl-default .chat-host.is-collapsed { box-shadow: none; }
}

/* ========================= Instant Apps "Basic" page ========================= */
/* Light only, like the Instant App. Colours and font come from the city's page config (set on <body> by
   web/instant-basic.js); the values below are the Calcite light theme the app's widgets use. */
.tpl-instant-basic {
  --ia-header-h: 60px;
  --ia-header-bg: #48307b;
  --ia-header-fg: #ffffff;
  --ia-font: "Avenir Next", Avenir, "Helvetica Neue", sans-serif;
  --ia-text-1: #151515;
  --ia-border: #dfdfdf;
  --ia-widget-shadow: 0 1px 2px rgba(0, 0, 0, .3);
  --ia-popover-shadow: 0 6px 20px -4px rgba(0, 0, 0, .1), 0 4px 12px -2px rgba(0, 0, 0, .08);
  /* Map UI grid: widgets sit 16px from the map edges in 32px columns with 10px gaps (measured on the original). */
  --ia-gap: 10px;
  --ia-edge: 16px;
  --ia-btn: 32px;
  color-scheme: light;
  background: #ffffff;
  color: var(--ia-text-1);
  font-family: var(--ia-font);
}
.tpl-instant-basic .stage { top: var(--ia-header-h); }

/* ---------- header ---------- */
.ia-header {
  position: absolute; top: 0; left: 0; right: 0; z-index: 20; height: var(--ia-header-h);
  display: flex; align-items: center; gap: 12px; padding: 0 8px 0 10px;
  background: var(--ia-header-bg); color: var(--ia-header-fg); font-family: var(--ia-font);
}
.ia-title {
  flex: 1 1 auto; min-width: 0; margin: 0;
  font-size: 18px; font-weight: 500; line-height: 1.25; letter-spacing: 0;
  overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.ia-actions { display: flex; align-items: center; gap: 2px; flex: none; }
.ia-action {
  display: grid; place-items: center; width: 32px; height: 32px; padding: 0; margin: 0;
  border: 0; border-radius: 0; background: transparent; color: inherit; cursor: pointer;
}
.ia-action:hover { background: color-mix(in srgb, var(--ia-header-fg) 12%, transparent); }
.ia-action:focus-visible { outline: 2px solid var(--ia-header-fg); outline-offset: -2px; }
.ia-action svg { display: block; }

/* About dialog (the header's info button). */
.ia-dialog {
  width: min(560px, calc(100vw - 32px)); padding: 0; border: 1px solid var(--ia-border); border-radius: 4px;
  background: #fff; color: #4a4a4a; font-family: var(--ia-font); font-size: 14px; line-height: 1.5;
  box-shadow: var(--ia-popover-shadow);
}
.ia-dialog::backdrop { background: rgba(0, 0, 0, .45); }
.ia-dialog-head {
  display: flex; align-items: center; gap: 12px; padding: 12px 8px 12px 16px; border-bottom: 1px solid var(--ia-border);
}
.ia-dialog-head h2 { flex: 1 1 auto; margin: 0; font-size: 16px; font-weight: 500; color: var(--ia-text-1); }
.ia-dialog-close { color: #4a4a4a; }
.ia-dialog-close:hover { background: #f3f3f3; }
.ia-dialog-close:focus-visible { outline-color: var(--ia-header-bg); }
.ia-dialog-body { padding: 16px; }
.ia-dialog-body p { margin: 0 0 12px; }
.ia-dialog-body p:last-child { margin-bottom: 0; }

/* "Link copied" note under the share button. */
.ia-toast {
  position: absolute; top: calc(var(--ia-header-h) + 8px); right: 8px; z-index: 30; margin: 0;
  padding: 8px 12px; border: 1px solid var(--ia-border); border-radius: 4px; background: #fff; color: var(--ia-text-1);
  font-size: 14px; box-shadow: var(--ia-popover-shadow);
}

/* ---------- Map Chat placement ----------
   Collapsed: a 32px launcher in the top-left widget row, one gap to the right of the zoom/home column.
   Expanded: the panel hangs from the same spot down the left side and stops one gap above the bottom-left widgets
   (basemap toggle 69px + scale bar, 32px above the map's bottom edge because of the attribution bar). Its width
   shrinks so it always ends one gap before the open legend / layer list / search, which are 300px wide at the
   top-right (right edge 55px from the map's right edge). Below 760px wide it becomes a full-width sheet instead. */
.tpl-instant-basic .chat-host {
  --ia-chat-left: calc(var(--ia-edge) + var(--ia-btn) + var(--ia-gap));             /* 58px */
  --ia-chat-bottom: 112px;                                                            /* 32 + 69 + 10 (+1) */
  --ia-topright-reserve: calc(var(--ia-edge) + var(--ia-btn) + 7px + 300px + var(--ia-gap)); /* 365px */
  position: absolute; z-index: 10;
  top: var(--ia-edge); left: var(--ia-chat-left); bottom: var(--ia-chat-bottom);
  width: clamp(300px, calc(100% - var(--ia-chat-left) - var(--ia-topright-reserve)), 372px);
  border: 1px solid var(--ia-border); border-radius: 4px; overflow: hidden;
  background: #fff;
  box-shadow: var(--ia-popover-shadow);
  /* Panel theme: the app's light Calcite look, whatever the OS scheme. */
  color-scheme: light;
  --map-chat-surface: #ffffff;
  --map-chat-text: var(--ia-text-1);
  --map-chat-radius: 4px;
  --map-chat-launcher-radius: 4px;
  --map-chat-launcher-height: var(--ia-btn);
  --map-chat-shadow: var(--ia-widget-shadow);
  --map-chat-font: var(--ia-font);
}
.tpl-instant-basic .chat-host.is-collapsed {
  bottom: auto; width: auto; height: auto;
  border: 0; border-radius: 0; box-shadow: none; overflow: visible; background: transparent;
}

/* Narrow screens: a full-width sheet between the widget rows. It starts one gap below the top-right column
   (search / legend / layers end 132px down the map; zoom/home end at 123px) and stops above the bottom widgets, so
   the header, zoom, the top-right buttons, the basemap toggle, scale bar and full screen all stay visible. */
@media (max-width: 759px) {
  .tpl-instant-basic .chat-host {
    top: max(142px, calc(100% - var(--ia-chat-bottom) - 560px)); left: 8px; right: 8px; width: auto;
  }
  .ia-title { font-size: 16px; }
}
/* Short screens (landscape phones, browser zoom): no room between the rows, so the panel stands beside the
   bottom-left widgets instead (scale bar up to 100px + toggle 69px + gaps), clear of the attribution bar. */
@media (max-height: 560px) {
  .tpl-instant-basic .chat-host {
    top: var(--ia-edge); bottom: 32px; left: 206px; right: auto;
    width: clamp(260px, calc(100% - 206px - var(--ia-topright-reserve)), 372px);
  }
}
@media (max-height: 560px) and (max-width: 759px) {
  .tpl-instant-basic .chat-host { right: calc(var(--ia-edge) + var(--ia-btn) + var(--ia-gap)); width: auto; }
}
/* Collapsed, the launcher always sits in the top-left widget row. */
.tpl-instant-basic .chat-host.is-collapsed { top: var(--ia-edge); left: var(--ia-chat-left); right: auto; bottom: auto; width: auto; }
