html, body {
  margin: 0;
  height: 100%;
  height: 100dvh;
  max-height: 100dvh;
  overflow: hidden;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: #0b1220;
  color: #e5eef7;
  -webkit-text-size-adjust: 100%;
  touch-action: manipulation;
}
#layout {
  display: flex;
  height: 100%;
  height: 100dvh;
  width: 100%;
  max-width: 100vw;
  overflow: hidden;
}
#chat-pane {
  flex: 1 1 48%;
  min-width: 0;
  min-height: 0;
  height: 100%;
  border-right: 1px solid #1e2a3a;
  display: flex;
  flex-direction: column;
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}
#n8n-chat {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  overflow: hidden;
  /* iOS: keep chat input usable */
}
#n8n-chat textarea,
#n8n-chat input {
  font-size: 16px !important; /* prevent iOS zoom on focus */
}
#viewer-pane {
  flex: 1 1 52%;
  min-width: 0;
  min-height: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  background: #ffffff;
  position: relative;
  z-index: 50;
}
#viewer-pane.hidden { display: none; }
#viewer-bar {
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 12px;
  padding-top: max(8px, env(safe-area-inset-top));
  background: #f4f7fb;
  border-bottom: 1px solid #c5d0dc;
  color: #1a2330;
  flex: 0 0 auto;
}
#viewer-sku { font-weight: 600; flex: 1; font-size: 14px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#viewer-close {
  border: 1px solid #c5d0dc;
  background: #ffffff;
  color: #1a2330;
  border-radius: 8px;
  padding: 10px 14px;
  min-height: 44px;
  cursor: pointer;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
}
#viewer-frame {
  flex: 1;
  width: 100%;
  border: 0;
  background: #ffffff;
  min-height: 0;
  /* allow touch orbit inside iframe */
  touch-action: none;
}
#layout:not(:has(#viewer-pane:not(.hidden))) #chat-pane {
  flex: 1 1 100%;
  border-right: none;
}

#prompt-rail {
  flex: 0 0 auto;
  padding: 10px 12px 8px;
  padding-top: max(10px, env(safe-area-inset-top));
  border-bottom: 1px solid #1e2a3a;
  background: #101826;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior-x: contain;
}
.chip-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin-bottom: 6px;
}
.chip-row:last-child { margin-bottom: 0; }
.chip-row.hidden { display: none !important; }
.chip-label { font-size: 12px; opacity: 0.75; margin-right: 4px; flex: 0 0 auto; }
.chip {
  appearance: none;
  -webkit-appearance: none;
  border: 1px solid #3a4d66;
  background: #1a2740;
  color: #e5eef7;
  border-radius: 999px;
  padding: 10px 14px;
  min-height: 44px;
  cursor: pointer;
  font-size: 14px;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.chip:hover, .chip:active { background: #243552; }
.chip-cards { align-items: stretch; flex-wrap: nowrap; }
.chip-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-width: 92px;
  padding: 10px 12px 8px;
  border-radius: 12px;
  flex: 0 0 auto;
}
.chip-svg { width: 56px; height: 56px; display: block; pointer-events: none; }
.chip-svg-sm { width: 48px; height: 48px; }
.chip-name { font-size: 12px; font-weight: 600; letter-spacing: 0.02em; }
.chip-card:hover .chip-svg g, .chip-card:active .chip-svg g { stroke: #cfe0ff; }

/* Phones / narrow tablets: stack; 3D as full-screen sheet when open */
@media (max-width: 900px) {
  #layout {
    flex-direction: column;
    position: relative;
  }
  #chat-pane {
    flex: 1 1 auto;
    width: 100%;
    height: 100%;
    border-right: none;
    border-bottom: none;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #prompt-rail {
    padding-bottom: 10px;
  }
  .chip-cards {
    flex-wrap: nowrap;
  }
  .chip-card {
    min-width: 84px;
  }
  .chip-svg { width: 48px; height: 48px; }
  /* Viewer overlays chat on mobile for usable CAD */
  #viewer-pane:not(.hidden) {
    position: fixed;
    inset: 0;
    z-index: 9999;
    width: 100%;
    height: 100%;
    height: 100dvh;
    flex: none;
    border: 0;
    padding-bottom: env(safe-area-inset-bottom);
  }
  #viewer-bar {
    min-height: 48px;
  }
}

@media (max-width: 420px) {
  .chip-card { min-width: 76px; padding: 8px 8px 6px; }
  .chip-name { font-size: 11px; }
  #prompt-rail { padding-left: max(10px, env(safe-area-inset-left)); padding-right: max(10px, env(safe-area-inset-right)); }
}

/* Prefer landscape phones: side-by-side if wide enough height */
@media (max-width: 900px) and (orientation: landscape) and (min-height: 420px) {
  #layout { flex-direction: row; }
  #chat-pane { flex: 1 1 46%; height: 100%; }
  #viewer-pane:not(.hidden) {
    position: relative;
    inset: auto;
    z-index: auto;
    flex: 1 1 54%;
    height: 100%;
    width: auto;
  }
}

a.chip {
  text-decoration: none;
  color: inherit;
  display: inline-flex;
  align-items: center;
}
